|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2021 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <app-common/zap-generated/af-structs.h> |
| 19 | +#include <app-common/zap-generated/att-storage.h> |
| 20 | +#include <app-common/zap-generated/attribute-type.h> |
| 21 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 22 | +#include <app-common/zap-generated/cluster-objects.h> |
| 23 | +#include <app-common/zap-generated/ids/Attributes.h> |
| 24 | +#include <app-common/zap-generated/ids/Clusters.h> |
| 25 | +#include <app/AttributeAccessInterface.h> |
| 26 | +#include <app/CommandHandler.h> |
| 27 | +#include <app/ConcreteCommandPath.h> |
| 28 | +#include <app/clusters/mode-select-server/static-supported-modes-manager.h> |
| 29 | +#include <app/util/af.h> |
| 30 | +#include <app/util/attribute-storage.h> |
| 31 | +#include <lib/support/CodeUtils.h> |
| 32 | + |
| 33 | +using namespace std; |
| 34 | +using namespace chip; |
| 35 | +using namespace chip::app; |
| 36 | +using namespace chip::app::Clusters; |
| 37 | + |
| 38 | +namespace { |
| 39 | + |
| 40 | +class ModeSelectAttrAccess : public AttributeAccessInterface |
| 41 | +{ |
| 42 | +public: |
| 43 | + ModeSelectAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), ModeSelect::Id) {} |
| 44 | + |
| 45 | + CHIP_ERROR Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder) override; |
| 46 | +}; |
| 47 | + |
| 48 | +ModeSelectAttrAccess gModeSelectAttrAccess; |
| 49 | + |
| 50 | +CHIP_ERROR ModeSelectAttrAccess::Read(const ConcreteAttributePath & aPath, AttributeValueEncoder & aEncoder) |
| 51 | +{ |
| 52 | + VerifyOrDie(aPath.mClusterId == ModeSelect::Id); |
| 53 | + |
| 54 | + const ModeSelect::StaticSupportedModesManager & gSupportedModeManager = |
| 55 | + ModeSelect::StaticSupportedModesManager::getStaticSupportedModesManagerInstance(); |
| 56 | + |
| 57 | + if (ModeSelect::Attributes::SupportedModes::Id == aPath.mAttributeId) |
| 58 | + { |
| 59 | + const ModeSelect::StaticSupportedModesManager::IteratorFactory * iteratorFactory = |
| 60 | + gSupportedModeManager.getIteratorFactory(aPath.mEndpointId); |
| 61 | + if (iteratorFactory == nullptr) |
| 62 | + { |
| 63 | + aEncoder.Encode(DataModel::List<ModeSelect::Structs::ModeOptionStruct::Type>()); |
| 64 | + return CHIP_NO_ERROR; |
| 65 | + } |
| 66 | + CHIP_ERROR err; |
| 67 | + err = aEncoder.EncodeList([iteratorFactory](const TagBoundEncoder & encoder) -> CHIP_ERROR { |
| 68 | + const auto & end = *(iteratorFactory->end()); |
| 69 | + for (auto it = *(iteratorFactory->begin()); it != end; ++it) |
| 70 | + { |
| 71 | + emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: dereferencing it"); |
| 72 | + emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: it= %p", (void *) it.operator->()); |
| 73 | + auto & modeOption = *it; |
| 74 | + ReturnErrorOnFailure(encoder.Encode(modeOption)); |
| 75 | + } |
| 76 | + return CHIP_NO_ERROR; |
| 77 | + }); |
| 78 | + ReturnErrorOnFailure(err); |
| 79 | + } |
| 80 | + return CHIP_NO_ERROR; |
| 81 | +} |
| 82 | + |
| 83 | +} // anonymous namespace |
| 84 | + |
| 85 | +bool emberAfModeSelectClusterChangeToModeCallback(CommandHandler * commandHandler, const ConcreteCommandPath & commandPath, |
| 86 | + const ModeSelect::Commands::ChangeToMode::DecodableType & commandData) |
| 87 | +{ |
| 88 | + emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: Entering emberAfModeSelectClusterChangeToModeCallback"); |
| 89 | + EndpointId endpointId = commandPath.mEndpointId; |
| 90 | + uint8_t newMode = commandData.newMode; |
| 91 | + // Check that the newMode matches one of the supported options |
| 92 | + const ModeSelect::Structs::ModeOptionStruct::Type * modeOptionPtr; |
| 93 | + const ModeSelect::StaticSupportedModesManager & gSupportedModeManager = |
| 94 | + ModeSelect::StaticSupportedModesManager::getStaticSupportedModesManagerInstance(); |
| 95 | + EmberAfStatus checkSupportedModeStatus = gSupportedModeManager.getModeOptionByMode(endpointId, newMode, &modeOptionPtr); |
| 96 | + if (EMBER_ZCL_STATUS_SUCCESS != checkSupportedModeStatus) |
| 97 | + { |
| 98 | + emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: Failed to find the option with mode %" PRIu8, newMode); |
| 99 | + emberAfSendImmediateDefaultResponse(checkSupportedModeStatus); |
| 100 | + return false; |
| 101 | + } |
| 102 | + ModeSelect::Attributes::CurrentMode::Set(endpointId, newMode); |
| 103 | + // TODO: Implement application logic |
| 104 | + |
| 105 | + emberAfPrintln(EMBER_AF_PRINT_DEBUG, "ModeSelect: ChangeToMode successful"); |
| 106 | + emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS); |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +void MatterModeSelectPluginServerInitCallback(void) |
| 111 | +{ |
| 112 | + registerAttributeAccessOverride(&gModeSelectAttrAccess); |
| 113 | +} |
0 commit comments