|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 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 | + |
| 19 | +#include "static-supported-modes-manager.h" |
| 20 | +#include <platform/ESP32/ESP32Config.h> |
| 21 | + |
| 22 | +using namespace chip; |
| 23 | +using namespace chip::app::Clusters; |
| 24 | +using namespace chip::DeviceLayer::Internal; |
| 25 | +using namespace chip::app::Clusters::ModeSelect; |
| 26 | +using chip::Protocols::InteractionModel::Status; |
| 27 | + |
| 28 | +using ModeOptionStructType = Structs::ModeOptionStruct::Type; |
| 29 | +using SemanticTag = Structs::SemanticTagStruct::Type; |
| 30 | + |
| 31 | +template <typename T> |
| 32 | +using List = app::DataModel::List<T>; |
| 33 | + |
| 34 | +SupportedModesManager::ModeOptionsProvider * StaticSupportedModesManager::epModeOptionsProviderList = nullptr; |
| 35 | + |
| 36 | +const StaticSupportedModesManager StaticSupportedModesManager::instance = StaticSupportedModesManager(); |
| 37 | + |
| 38 | +int StaticSupportedModesManager::mSize = 0; |
| 39 | + |
| 40 | +CHIP_ERROR StaticSupportedModesManager::InitEndpointArray(int size) |
| 41 | +{ |
| 42 | + if (epModeOptionsProviderList != nullptr) |
| 43 | + { |
| 44 | + ChipLogError(Zcl, "Cannot allocate epModeOptionsProviderList"); |
| 45 | + return CHIP_ERROR_INCORRECT_STATE; |
| 46 | + } |
| 47 | + mSize = size; |
| 48 | + epModeOptionsProviderList = new SupportedModesManager::ModeOptionsProvider[mSize]; |
| 49 | + if (epModeOptionsProviderList == nullptr) |
| 50 | + { |
| 51 | + ChipLogError(Zcl, "Failed to allocate memory to epModeOptionsProviderList"); |
| 52 | + return CHIP_ERROR_NO_MEMORY; |
| 53 | + } |
| 54 | + for (int i = 0; i < mSize; i++) |
| 55 | + { |
| 56 | + epModeOptionsProviderList[i] = ModeOptionsProvider(); |
| 57 | + } |
| 58 | + return CHIP_NO_ERROR; |
| 59 | +} |
| 60 | + |
| 61 | +SupportedModesManager::ModeOptionsProvider StaticSupportedModesManager::getModeOptionsProvider(EndpointId endpointId) const |
| 62 | +{ |
| 63 | + if (epModeOptionsProviderList[endpointId].begin() != nullptr && epModeOptionsProviderList[endpointId].end() != nullptr) |
| 64 | + { |
| 65 | + return ModeOptionsProvider(epModeOptionsProviderList[endpointId].begin(), epModeOptionsProviderList[endpointId].end()); |
| 66 | + } |
| 67 | + |
| 68 | + ModeOptionStructType * modeOptionStructList = nullptr; |
| 69 | + SemanticTag * semanticTags = nullptr; |
| 70 | + |
| 71 | + char keyBuf[ESP32Config::kMaxConfigKeyNameLength]; |
| 72 | + uint32_t supportedModeCount = 0; |
| 73 | + |
| 74 | + VerifyOrReturnValue(ESP32Config::KeyAllocator::SupportedModesCount(keyBuf, sizeof(keyBuf), endpointId) == CHIP_NO_ERROR, |
| 75 | + ModeOptionsProvider(nullptr, nullptr)); |
| 76 | + ESP32Config::Key countKey(ESP32Config::kConfigNamespace_ChipFactory, keyBuf); |
| 77 | + VerifyOrReturnValue(ESP32Config::ReadConfigValue(countKey, supportedModeCount) == CHIP_NO_ERROR, |
| 78 | + ModeOptionsProvider(nullptr, nullptr)); |
| 79 | + |
| 80 | + modeOptionStructList = new ModeOptionStructType[supportedModeCount]; |
| 81 | + if (modeOptionStructList == nullptr) |
| 82 | + { |
| 83 | + return ModeOptionsProvider(nullptr, nullptr); |
| 84 | + } |
| 85 | + |
| 86 | + epModeOptionsProviderList[endpointId] = ModeOptionsProvider(modeOptionStructList, modeOptionStructList + supportedModeCount); |
| 87 | + |
| 88 | + for (int index = 0; index < supportedModeCount; index++) |
| 89 | + { |
| 90 | + Structs::ModeOptionStruct::Type option; |
| 91 | + uint32_t supportedModeMode = 0; |
| 92 | + uint32_t semanticTagCount = 0; |
| 93 | + size_t outLen = 0; |
| 94 | + |
| 95 | + memset(keyBuf, 0, sizeof(char) * ESP32Config::kMaxConfigKeyNameLength); |
| 96 | + VerifyOrReturnValue(ESP32Config::KeyAllocator::SupportedModesLabel(keyBuf, sizeof(keyBuf), endpointId, index) == |
| 97 | + CHIP_NO_ERROR, |
| 98 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 99 | + ESP32Config::Key labelKey(ESP32Config::kConfigNamespace_ChipFactory, keyBuf); |
| 100 | + VerifyOrReturnValue(ESP32Config::ReadConfigValueStr(labelKey, nullptr, 0, outLen) == CHIP_NO_ERROR, |
| 101 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 102 | + |
| 103 | + char * modeLabel = new char[outLen + 1]; |
| 104 | + if (modeLabel == nullptr) |
| 105 | + { |
| 106 | + CleanUp(endpointId); |
| 107 | + return ModeOptionsProvider(nullptr, nullptr); |
| 108 | + } |
| 109 | + |
| 110 | + VerifyOrReturnValue(ESP32Config::ReadConfigValueStr(labelKey, modeLabel, outLen + 1, outLen) == CHIP_NO_ERROR, |
| 111 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 112 | + |
| 113 | + memset(keyBuf, 0, sizeof(char) * ESP32Config::kMaxConfigKeyNameLength); |
| 114 | + VerifyOrReturnValue(ESP32Config::KeyAllocator::SupportedModesValue(keyBuf, sizeof(keyBuf), endpointId, index) == |
| 115 | + CHIP_NO_ERROR, |
| 116 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 117 | + ESP32Config::Key modeKey(ESP32Config::kConfigNamespace_ChipFactory, keyBuf); |
| 118 | + VerifyOrReturnValue(ESP32Config::ReadConfigValue(labelKey, supportedModeMode) == CHIP_NO_ERROR, |
| 119 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 120 | + |
| 121 | + memset(keyBuf, 0, sizeof(char) * ESP32Config::kMaxConfigKeyNameLength); |
| 122 | + VerifyOrReturnValue(ESP32Config::KeyAllocator::SemanticTagsCount(keyBuf, sizeof(keyBuf), endpointId, index) == |
| 123 | + CHIP_NO_ERROR, |
| 124 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 125 | + ESP32Config::Key stCountKey(ESP32Config::kConfigNamespace_ChipFactory, keyBuf); |
| 126 | + VerifyOrReturnValue(ESP32Config::ReadConfigValue(stCountKey, semanticTagCount) == CHIP_NO_ERROR, |
| 127 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 128 | + |
| 129 | + semanticTags = new SemanticTag[semanticTagCount]; |
| 130 | + if (semanticTags == nullptr) |
| 131 | + { |
| 132 | + CleanUp(endpointId); |
| 133 | + return ModeOptionsProvider(nullptr, nullptr); |
| 134 | + } |
| 135 | + for (auto stIndex = 0; stIndex < semanticTagCount; stIndex++) |
| 136 | + { |
| 137 | + |
| 138 | + uint32_t semanticTagValue = 0; |
| 139 | + uint32_t semanticTagMfgCode = 0; |
| 140 | + SemanticTag tag; |
| 141 | + |
| 142 | + memset(keyBuf, 0, sizeof(char) * ESP32Config::kMaxConfigKeyNameLength); |
| 143 | + VerifyOrReturnValue(ESP32Config::KeyAllocator::SemanticTagValue(keyBuf, sizeof(keyBuf), endpointId, index, stIndex) == |
| 144 | + CHIP_NO_ERROR, |
| 145 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 146 | + ESP32Config::Key stValueKey(ESP32Config::kConfigNamespace_ChipFactory, keyBuf); |
| 147 | + VerifyOrReturnValue(ESP32Config::ReadConfigValue(stValueKey, semanticTagValue) == CHIP_NO_ERROR, |
| 148 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 149 | + |
| 150 | + memset(keyBuf, 0, sizeof(char) * ESP32Config::kMaxConfigKeyNameLength); |
| 151 | + VerifyOrReturnValue(ESP32Config::KeyAllocator::SemanticTagMfgCode(keyBuf, sizeof(keyBuf), endpointId, index, stIndex) == |
| 152 | + CHIP_NO_ERROR, |
| 153 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 154 | + ESP32Config::Key stMfgCodeKey(ESP32Config::kConfigNamespace_ChipFactory, keyBuf); |
| 155 | + VerifyOrReturnValue(ESP32Config::ReadConfigValue(stMfgCodeKey, semanticTagMfgCode) == CHIP_NO_ERROR, |
| 156 | + ModeOptionsProvider(nullptr, nullptr), CleanUp(endpointId)); |
| 157 | + |
| 158 | + tag.value = static_cast<uint16_t>(semanticTagValue); |
| 159 | + tag.mfgCode = static_cast<chip::VendorId>(semanticTagMfgCode); |
| 160 | + semanticTags[stIndex] = tag; |
| 161 | + } |
| 162 | + |
| 163 | + option.label = chip::CharSpan::fromCharString(modeLabel); |
| 164 | + option.mode = static_cast<uint8_t>(supportedModeMode); |
| 165 | + option.semanticTags = DataModel::List<const SemanticTag>(semanticTags, semanticTagCount); |
| 166 | + |
| 167 | + modeOptionStructList[index] = option; |
| 168 | + } |
| 169 | + |
| 170 | + return ModeOptionsProvider(modeOptionStructList, modeOptionStructList + supportedModeCount); |
| 171 | +} |
| 172 | + |
| 173 | +Status StaticSupportedModesManager::getModeOptionByMode(unsigned short endpointId, unsigned char mode, |
| 174 | + const ModeOptionStructType ** dataPtr) const |
| 175 | +{ |
| 176 | + auto modeOptionsProvider = this->getModeOptionsProvider(endpointId); |
| 177 | + if (modeOptionsProvider.begin() == nullptr) |
| 178 | + { |
| 179 | + return Status::UnsupportedCluster; |
| 180 | + } |
| 181 | + auto * begin = modeOptionsProvider.begin(); |
| 182 | + auto * end = modeOptionsProvider.end(); |
| 183 | + |
| 184 | + for (auto * it = begin; it != end; ++it) |
| 185 | + { |
| 186 | + auto & modeOption = *it; |
| 187 | + if (modeOption.mode == mode) |
| 188 | + { |
| 189 | + *dataPtr = &modeOption; |
| 190 | + return Status::Success; |
| 191 | + } |
| 192 | + } |
| 193 | + ChipLogProgress(Zcl, "Cannot find the mode %u", mode); |
| 194 | + return Status::InvalidCommand; |
| 195 | +} |
| 196 | + |
| 197 | +const ModeSelect::SupportedModesManager * ModeSelect::getSupportedModesManager() |
| 198 | +{ |
| 199 | + return &StaticSupportedModesManager::getStaticSupportedModesManagerInstance(); |
| 200 | +} |
| 201 | + |
| 202 | +void StaticSupportedModesManager::FreeSupportedModes(EndpointId endpointId) const |
| 203 | +{ |
| 204 | + if (epModeOptionsProviderList[endpointId].begin() != nullptr) |
| 205 | + { |
| 206 | + auto * begin = epModeOptionsProviderList[endpointId].begin(); |
| 207 | + auto * end = epModeOptionsProviderList[endpointId].end(); |
| 208 | + for (auto * it = begin; it != end; ++it) |
| 209 | + { |
| 210 | + auto & modeOption = *it; |
| 211 | + delete[] modeOption.label.data(); |
| 212 | + delete[] modeOption.semanticTags.data(); |
| 213 | + } |
| 214 | + delete[] begin; |
| 215 | + } |
| 216 | + epModeOptionsProviderList[endpointId] = ModeOptionsProvider(); |
| 217 | +} |
| 218 | + |
| 219 | +void StaticSupportedModesManager::CleanUp(EndpointId endpointId) const |
| 220 | +{ |
| 221 | + ChipLogError(Zcl, "Supported mode data is in incorrect format"); |
| 222 | + FreeSupportedModes(endpointId); |
| 223 | +} |
0 commit comments