|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 19 | +#include <app/util/config.h> |
| 20 | + |
| 21 | +using namespace chip; |
| 22 | +using namespace chip::app; |
| 23 | +using namespace chip::app::Clusters; |
| 24 | +using chip::Protocols::InteractionModel::Status; |
| 25 | +template <typename T> |
| 26 | +using List = chip::app::DataModel::List<T>; |
| 27 | +using ModeTagStructType = chip::app::Clusters::detail::Structs::ModeTagStruct::Type; |
| 28 | + |
| 29 | +#ifdef MATTER_DM_PLUGIN_DISHWASHER_MODE_SERVER |
| 30 | +#include <chef-dishwasher-mode-delegate-impl.h> |
| 31 | +using namespace chip::app::Clusters::DishwasherMode; |
| 32 | + |
| 33 | +static std::unique_ptr<DishwasherModeDelegate> gDishwasherModeDelegate; |
| 34 | +static std::unique_ptr<ModeBase::Instance> gDishwasherModeInstance; |
| 35 | + |
| 36 | +CHIP_ERROR DishwasherModeDelegate::Init() |
| 37 | +{ |
| 38 | + return CHIP_NO_ERROR; |
| 39 | +} |
| 40 | + |
| 41 | +void DishwasherModeDelegate::HandleChangeToMode(uint8_t NewMode, ModeBase::Commands::ChangeToModeResponse::Type & response) |
| 42 | +{ |
| 43 | + response.status = to_underlying(ModeBase::StatusCode::kSuccess); |
| 44 | +} |
| 45 | + |
| 46 | +CHIP_ERROR DishwasherModeDelegate::GetModeLabelByIndex(uint8_t modeIndex, chip::MutableCharSpan & label) |
| 47 | +{ |
| 48 | + if (modeIndex >= ArraySize(kModeOptions)) |
| 49 | + { |
| 50 | + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; |
| 51 | + } |
| 52 | + return chip::CopyCharSpanToMutableCharSpan(kModeOptions[modeIndex].label, label); |
| 53 | +} |
| 54 | + |
| 55 | +CHIP_ERROR DishwasherModeDelegate::GetModeValueByIndex(uint8_t modeIndex, uint8_t & value) |
| 56 | +{ |
| 57 | + if (modeIndex >= ArraySize(kModeOptions)) |
| 58 | + { |
| 59 | + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; |
| 60 | + } |
| 61 | + value = kModeOptions[modeIndex].mode; |
| 62 | + return CHIP_NO_ERROR; |
| 63 | +} |
| 64 | + |
| 65 | +CHIP_ERROR DishwasherModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List<ModeTagStructType> & tags) |
| 66 | +{ |
| 67 | + if (modeIndex >= ArraySize(kModeOptions)) |
| 68 | + { |
| 69 | + return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED; |
| 70 | + } |
| 71 | + |
| 72 | + if (tags.size() < kModeOptions[modeIndex].modeTags.size()) |
| 73 | + { |
| 74 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 75 | + } |
| 76 | + |
| 77 | + std::copy(kModeOptions[modeIndex].modeTags.begin(), kModeOptions[modeIndex].modeTags.end(), tags.begin()); |
| 78 | + tags.reduce_size(kModeOptions[modeIndex].modeTags.size()); |
| 79 | + |
| 80 | + return CHIP_NO_ERROR; |
| 81 | +} |
| 82 | + |
| 83 | +// ModeBase::Instance * DishwasherMode::Instance() |
| 84 | +// { |
| 85 | +// return gDishwasherModeInstance; |
| 86 | +// } |
| 87 | + |
| 88 | +void DishwasherMode::Shutdown() |
| 89 | +{ |
| 90 | + gDishwasherModeInstance.reset(); |
| 91 | + gDishwasherModeDelegate.reset(); |
| 92 | +} |
| 93 | + |
| 94 | +chip::Protocols::InteractionModel::Status chefDishwasherModeWriteCallback(chip::EndpointId endpointId, chip::ClusterId clusterId, |
| 95 | + const EmberAfAttributeMetadata * attributeMetadata, |
| 96 | + uint8_t * buffer) |
| 97 | +{ |
| 98 | + VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1 |
| 99 | + VerifyOrDie(gDishwasherModeInstance != nullptr); |
| 100 | + chip::Protocols::InteractionModel::Status ret; |
| 101 | + chip::AttributeId attributeId = attributeMetadata->attributeId; |
| 102 | + |
| 103 | + switch (attributeId) |
| 104 | + { |
| 105 | + case chip::app::Clusters::DishwasherMode::Attributes::CurrentMode::Id: { |
| 106 | + uint8_t m = static_cast<uint8_t>(buffer[0]); |
| 107 | + ret = gDishwasherModeInstance->UpdateCurrentMode(m); |
| 108 | + if (chip::Protocols::InteractionModel::Status::Success != ret) |
| 109 | + { |
| 110 | + ChipLogError(DeviceLayer, "Invalid Attribute Write to CurrentMode : %d", static_cast<int>(ret)); |
| 111 | + } |
| 112 | + } |
| 113 | + break; |
| 114 | + default: |
| 115 | + ret = chip::Protocols::InteractionModel::Status::UnsupportedWrite; |
| 116 | + ChipLogError(DeviceLayer, "Unsupported Writng Attribute ID: %d", static_cast<int>(attributeId)); |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + return ret; |
| 121 | +} |
| 122 | + |
| 123 | +chip::Protocols::InteractionModel::Status chefDishwasherModeReadCallback(chip::EndpointId endpointId, chip::ClusterId clusterId, |
| 124 | + const EmberAfAttributeMetadata * attributeMetadata, |
| 125 | + uint8_t * buffer, uint16_t maxReadLength) |
| 126 | +{ |
| 127 | + VerifyOrReturnValue(maxReadLength == 1, chip::Protocols::InteractionModel::Status::ResourceExhausted); |
| 128 | + buffer[0] = gDishwasherModeInstance->GetCurrentMode(); |
| 129 | + |
| 130 | + chip::Protocols::InteractionModel::Status ret = chip::Protocols::InteractionModel::Status::Success; |
| 131 | + chip::AttributeId attributeId = attributeMetadata->attributeId; |
| 132 | + |
| 133 | + switch (attributeId) |
| 134 | + { |
| 135 | + case chip::app::Clusters::DishwasherMode::Attributes::CurrentMode::Id: { |
| 136 | + *buffer = gDishwasherModeInstance->GetCurrentMode(); |
| 137 | + ChipLogDetail(DeviceLayer, "Reading DishwasherMode CurrentMode : %d", static_cast<int>(attributeId)); |
| 138 | + } |
| 139 | + break; |
| 140 | + default: |
| 141 | + ret = chip::Protocols::InteractionModel::Status::UnsupportedRead; |
| 142 | + ChipLogDetail(DeviceLayer, "Unsupported attributeId %d from reading DishwasherMode", static_cast<int>(attributeId)); |
| 143 | + break; |
| 144 | + } |
| 145 | + |
| 146 | + return ret; |
| 147 | +} |
| 148 | + |
| 149 | +void emberAfDishwasherModeClusterInitCallback(chip::EndpointId endpointId) |
| 150 | +{ |
| 151 | + VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1. |
| 152 | + VerifyOrDie(!gDishwasherModeDelegate && !gDishwasherModeInstance); |
| 153 | + |
| 154 | + gDishwasherModeDelegate = std::make_unique<DishwasherModeDelegate>(); |
| 155 | + gDishwasherModeInstance = std::make_unique<ModeBase::Instance>(gDishwasherModeDelegate.get(), endpointId, DishwasherMode::Id, |
| 156 | + chip::to_underlying(Feature::kOnOff)); |
| 157 | + gDishwasherModeInstance->Init(); |
| 158 | +} |
| 159 | +#endif // MATTER_DM_PLUGIN_DISHWASHER_MODE_SERVER |
0 commit comments