|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 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 | +/****************************************************************************' |
| 19 | + * @file |
| 20 | + * @brief Implementation for the Chime Server Cluster |
| 21 | + ***************************************************************************/ |
| 22 | + |
| 23 | + |
| 24 | +#include "chime-server.h" |
| 25 | + |
| 26 | +#include <app/AttributeAccessInterfaceRegistry.h> |
| 27 | +#include <app/CommandHandlerInterfaceRegistry.h> |
| 28 | +#include <app/ConcreteAttributePath.h> |
| 29 | +#include <app/InteractionModelEngine.h> |
| 30 | +#include <app/util/attribute-storage.h> |
| 31 | +#include <app/util/util.h> |
| 32 | +#include <protocols/interaction_model/StatusCode.h> |
| 33 | + |
| 34 | +using namespace chip; |
| 35 | +using namespace chip::app; |
| 36 | +using namespace chip::app::DataModel; |
| 37 | +using namespace chip::app::Clusters; |
| 38 | +using namespace chip::app::Clusters::Chime; |
| 39 | +using namespace chip::app::Clusters::Chime::Attributes; |
| 40 | +using chip::Protocols::InteractionModel::Status; |
| 41 | + |
| 42 | + |
| 43 | +namespace chip { |
| 44 | +namespace app { |
| 45 | +namespace Clusters { |
| 46 | + |
| 47 | +ChimeServer::ChimeServer(EndpointId endpointId, ChimeDelegate & delegate) : |
| 48 | + AttributeAccessInterface(MakeOptional(endpointId), Chime::Id), |
| 49 | + CommandHandlerInterface(MakeOptional(endpointId), Chime::Id), mDelegate(delegate) |
| 50 | +{ |
| 51 | + mDelegate.SetChimeServer(this); |
| 52 | +} |
| 53 | + |
| 54 | +ChimeServer::~ChimeServer() |
| 55 | +{ |
| 56 | + AttributeAccessInterfaceRegistry::Instance().Unregister(this); |
| 57 | + CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +CHIP_ERROR ChimeServer::Init() |
| 62 | +{ |
| 63 | + VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INTERNAL); |
| 64 | + ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this)); |
| 65 | + return CHIP_NO_ERROR; |
| 66 | +} |
| 67 | + |
| 68 | +// AttributeAccessInterface |
| 69 | +CHIP_ERROR ChimeServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
| 70 | +{ |
| 71 | + VerifyOrDie(aPath.mClusterId == Chime::Id); |
| 72 | + |
| 73 | + switch (aPath.mAttributeId) |
| 74 | + { |
| 75 | + case InstalledChimeSounds::Id: |
| 76 | + return aEncoder.Encode(mDelegate.GetInstalledChimeSounds()); |
| 77 | + |
| 78 | + case ActiveChimeSoundId::Id: |
| 79 | + return aEncoder.Encode(mDelegate.GetActiveChimeSoundId()); |
| 80 | + |
| 81 | + case Enabled::Id: |
| 82 | + return aEncoder.Encode(mDelegate.GetEnabled()); |
| 83 | + } |
| 84 | + |
| 85 | + return CHIP_NO_ERROR; |
| 86 | +} |
| 87 | + |
| 88 | +CHIP_ERROR ChimeServer::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) |
| 89 | +{ |
| 90 | + VerifyOrDie(aPath.mClusterId == Chime::Id); |
| 91 | + |
| 92 | + switch (aPath.mAttributeId) |
| 93 | + { |
| 94 | + case ActiveChimeSoundId::Id: { |
| 95 | + uint8_t newValue; |
| 96 | + ReturnErrorOnFailure(aDecoder.Decode(newValue)); |
| 97 | + ReturnErrorOnFailure(SetActiveChimeSoundId(newValue)); |
| 98 | + return CHIP_NO_ERROR; |
| 99 | + |
| 100 | + } |
| 101 | + case Enabled::Id: { |
| 102 | + bool newValue; |
| 103 | + ReturnErrorOnFailure(aDecoder.Decode(newValue)); |
| 104 | + ReturnErrorOnFailure(mDelegate.SetEnabled(newValue)); |
| 105 | + return CHIP_NO_ERROR; |
| 106 | + } |
| 107 | + |
| 108 | + default: |
| 109 | + // Unknown attribute |
| 110 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +CHIP_ERROR ChimeServer::SetActiveChimeSoundId(uint8_t soundId) |
| 116 | +{ |
| 117 | + uint8_t currentSoundId = mDelegate.GetActiveChimeSoundId(); |
| 118 | + bool activeSoundIdChanged = !(currentSoundId == soundId); |
| 119 | + |
| 120 | + VerifyOrReturnError(activeSoundIdChanged, CHIP_NO_ERROR); |
| 121 | + VerifyOrDie(mDelegate.SetActiveChimeSoundId(soundId) == CHIP_NO_ERROR); |
| 122 | + MatterReportingAttributeChangeCallback(GetEndpointId(), Chime::Id, ActiveChimeSoundId::Id); |
| 123 | + |
| 124 | + return CHIP_NO_ERROR; |
| 125 | +} |
| 126 | + |
| 127 | +CHIP_ERROR ChimeServer::SetEnabled(bool enabled) |
| 128 | +{ |
| 129 | + bool currentlyEnabled = mDelegate.GetEnabled(); |
| 130 | + bool enableChanged = !(currentlyEnabled == enabled); |
| 131 | + |
| 132 | + VerifyOrReturnError(enableChanged, CHIP_NO_ERROR); |
| 133 | + VerifyOrDie(mDelegate.SetEnabled(enabled) == CHIP_NO_ERROR); |
| 134 | + MatterReportingAttributeChangeCallback(GetEndpointId(), Chime::Id, Enabled ::Id); |
| 135 | + |
| 136 | + return CHIP_NO_ERROR; |
| 137 | +} |
| 138 | + |
| 139 | +void ChimeServer::InvokeCommand(HandlerContext & ctx) |
| 140 | +{ |
| 141 | + switch (ctx.mRequestPath.mCommandId) |
| 142 | + { |
| 143 | + case Commands::PlayChimeSound::Id: |
| 144 | + CommandHandlerInterface::HandleCommand<Commands::PlayChimeSound::DecodableType>( |
| 145 | + ctx, [this](HandlerContext & ctx, const auto & req) { HandlePlayChimeSound(ctx, req); }); |
| 146 | + break; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void ChimeServer::HandlePlayChimeSound(HandlerContext & ctx, const Commands::PlayChimeSound::DecodableType & req) |
| 151 | +{ |
| 152 | + |
| 153 | + ChipLogDetail(Zcl, "Chime: PlayChimeSound"); |
| 154 | + |
| 155 | + // call the delegate to play the chime |
| 156 | + Status status = mDelegate.playChimeSound(); |
| 157 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace Clusters |
| 161 | +} // namespace app |
| 162 | +} // namespace chip |
| 163 | + |
| 164 | +/** @brief Chime Cluster Server Init |
| 165 | + * |
| 166 | + * Server Init |
| 167 | + * |
| 168 | + */ |
| 169 | +void MatterChimePluginServerInitCallback(){} |
0 commit comments