|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Project CHIP Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "closure-control-server.h" |
| 18 | + |
| 19 | +#include <app/AttributeAccessInterface.h> |
| 20 | +#include <app/AttributeAccessInterfaceRegistry.h> |
| 21 | +#include <app/CommandHandlerInterfaceRegistry.h> |
| 22 | +#include <app/ConcreteAttributePath.h> |
| 23 | +#include <app/InteractionModelEngine.h> |
| 24 | +#include <app/util/attribute-storage.h> |
| 25 | + |
| 26 | +using namespace chip; |
| 27 | +using namespace chip::app; |
| 28 | +using namespace chip::app::DataModel; |
| 29 | +using namespace chip::app::Clusters; |
| 30 | +using namespace chip::app::Clusters::ClosureControl; |
| 31 | +using namespace chip::app::Clusters::ClosureControl::Attributes; |
| 32 | +using namespace chip::app::Clusters::ClosureControl::Commands; |
| 33 | +using chip::Protocols::InteractionModel::Status; |
| 34 | + |
| 35 | +namespace chip { |
| 36 | +namespace app { |
| 37 | +namespace Clusters { |
| 38 | +namespace ClosureControl { |
| 39 | + |
| 40 | +CHIP_ERROR Instance::Init() |
| 41 | +{ |
| 42 | + ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this)); |
| 43 | + VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INCORRECT_STATE); |
| 44 | + |
| 45 | + return CHIP_NO_ERROR; |
| 46 | +} |
| 47 | + |
| 48 | +void Instance::Shutdown() |
| 49 | +{ |
| 50 | + CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this); |
| 51 | + AttributeAccessInterfaceRegistry::Instance().Unregister(this); |
| 52 | +} |
| 53 | + |
| 54 | +bool Instance::HasFeature(Feature aFeatures) const |
| 55 | +{ |
| 56 | + return mFeatures.Has(aFeatures); |
| 57 | +} |
| 58 | + |
| 59 | +bool Instance::SupportsOptAttr(OptionalAttribute aOptionalAttrs) const |
| 60 | +{ |
| 61 | + return mOptionalAttrs.Has(aOptionalAttrs); |
| 62 | +} |
| 63 | + |
| 64 | +bool Instance::IsSupportedState(MainStateEnum aMainState) |
| 65 | +{ |
| 66 | + switch (aMainState) |
| 67 | + { |
| 68 | + case MainStateEnum::kCalibrating: |
| 69 | + return HasFeature(Feature::kCalibration); |
| 70 | + case MainStateEnum::kProtected: |
| 71 | + return HasFeature(Feature::kProtection); |
| 72 | + case MainStateEnum::kDisengaged: |
| 73 | + return HasFeature(Feature::kManuallyOperable); |
| 74 | + case MainStateEnum::kPendingFallback: |
| 75 | + return HasFeature(Feature::kFallback); |
| 76 | + default: |
| 77 | + // Remaining MainState have Mandatory conformance,so will be supported. |
| 78 | + return true; |
| 79 | + } |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +CHIP_ERROR Instance::SetMainState(MainStateEnum aMainState) |
| 84 | +{ |
| 85 | + if (!IsSupportedState(aMainState)) |
| 86 | + { |
| 87 | + return CHIP_ERROR_INVALID_ARGUMENT; |
| 88 | + } |
| 89 | + // If the Main State has changed, trigger the attribute change callback |
| 90 | + if (mMainState != aMainState) |
| 91 | + { |
| 92 | + mMainState = aMainState; |
| 93 | + MatterReportingAttributeChangeCallback(mDelegate.GetEndpointId(), ClosureControl::Id, Attributes::MainState::Id); |
| 94 | + UpdateCountdownTimeFromClusterLogic(); |
| 95 | + } |
| 96 | + return CHIP_NO_ERROR; |
| 97 | +} |
| 98 | + |
| 99 | +CHIP_ERROR Instance::SetOverallState(const GenericOverallState & aOverallState) |
| 100 | +{ |
| 101 | + // If the overall state has changed, trigger the attribute change callback |
| 102 | + if (!(mOverallState == aOverallState)) |
| 103 | + { |
| 104 | + mOverallState = aOverallState; |
| 105 | + MatterReportingAttributeChangeCallback(mDelegate.GetEndpointId(), ClosureControl::Id, Attributes::OverallState::Id); |
| 106 | + } |
| 107 | + |
| 108 | + return CHIP_NO_ERROR; |
| 109 | +} |
| 110 | + |
| 111 | +CHIP_ERROR Instance::SetOverallTarget(const GenericOverallTarget & aOverallTarget) |
| 112 | +{ |
| 113 | + // If the overall target has changed, trigger the attribute change callback |
| 114 | + if (!(mOverallTarget == aOverallTarget)) |
| 115 | + { |
| 116 | + mOverallTarget = aOverallTarget; |
| 117 | + MatterReportingAttributeChangeCallback(mDelegate.GetEndpointId(), ClosureControl::Id, Attributes::OverallTarget::Id); |
| 118 | + } |
| 119 | + |
| 120 | + return CHIP_NO_ERROR; |
| 121 | +} |
| 122 | + |
| 123 | +MainStateEnum Instance::GetMainState() const |
| 124 | +{ |
| 125 | + return mMainState; |
| 126 | +} |
| 127 | + |
| 128 | +const GenericOverallState & Instance::GetOverallState() const |
| 129 | +{ |
| 130 | + return mOverallState; |
| 131 | +} |
| 132 | + |
| 133 | +const GenericOverallTarget & Instance::GetOverallTarget() const |
| 134 | +{ |
| 135 | + return mOverallTarget; |
| 136 | +} |
| 137 | + |
| 138 | +void Instance::UpdateCountdownTime(bool fromDelegate) |
| 139 | +{ |
| 140 | + app::DataModel::Nullable<uint32_t> newCountdownTime = mDelegate.GetCountdownTime(); |
| 141 | + auto now = System::SystemClock().GetMonotonicTimestamp(); |
| 142 | + |
| 143 | + bool markDirty = false; |
| 144 | + |
| 145 | + if (fromDelegate) |
| 146 | + { |
| 147 | + // Updates from delegate are reduce-reported to every 1s max (choice of this implementation), in addition |
| 148 | + // to default change-from-null, change-from-zero and increment policy. |
| 149 | + System::Clock::Milliseconds64 reportInterval = System::Clock::Milliseconds64(1000); |
| 150 | + auto predicate = mCountdownTime.GetPredicateForSufficientTimeSinceLastDirty(reportInterval); |
| 151 | + markDirty = (mCountdownTime.SetValue(newCountdownTime, now, predicate) == AttributeDirtyState::kMustReport); |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + auto predicate = [](const decltype(mCountdownTime)::SufficientChangePredicateCandidate &) -> bool { return true; }; |
| 156 | + markDirty = (mCountdownTime.SetValue(newCountdownTime, now, predicate) == AttributeDirtyState::kMustReport); |
| 157 | + } |
| 158 | + |
| 159 | + if (markDirty) |
| 160 | + { |
| 161 | + MatterReportingAttributeChangeCallback(mDelegate.GetEndpointId(), ClosureControl::Id, Attributes::CountdownTime::Id); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// AttributeAccessInterface |
| 166 | +CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
| 167 | +{ |
| 168 | + VerifyOrDie(aPath.mClusterId == ClosureControl::Id); |
| 169 | + |
| 170 | + switch (aPath.mAttributeId) |
| 171 | + { |
| 172 | + case CountdownTime::Id: |
| 173 | + // Optional Attribute |
| 174 | + if (SupportsOptAttr(OptionalAttribute::kCountdownTime)) |
| 175 | + { |
| 176 | + return aEncoder.Encode(mDelegate.GetCountdownTime()); |
| 177 | + } |
| 178 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 179 | + |
| 180 | + case MainState::Id: |
| 181 | + return aEncoder.Encode(GetMainState()); |
| 182 | + |
| 183 | + case CurrentErrorList::Id: |
| 184 | + return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR { return this->EncodeCurrentErrorList(encoder); }); |
| 185 | + |
| 186 | + case OverallState::Id: |
| 187 | + return aEncoder.Encode(GetOverallState()); |
| 188 | + |
| 189 | + case OverallTarget::Id: |
| 190 | + return aEncoder.Encode(GetOverallTarget()); |
| 191 | + |
| 192 | + /* FeatureMap - is held locally */ |
| 193 | + case FeatureMap::Id: |
| 194 | + return aEncoder.Encode(mFeatures); |
| 195 | + } |
| 196 | + |
| 197 | + return CHIP_NO_ERROR; |
| 198 | +} |
| 199 | + |
| 200 | +CHIP_ERROR Instance::EncodeCurrentErrorList(const AttributeValueEncoder::ListEncodeHelper & encoder) |
| 201 | +{ |
| 202 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 203 | + |
| 204 | + ReturnErrorOnFailure(mDelegate.StartCurrentErrorListRead()); |
| 205 | + for (size_t i = 0; true; i++) |
| 206 | + { |
| 207 | + ClosureErrorEnum error; |
| 208 | + |
| 209 | + err = mDelegate.GetCurrentErrorListAtIndex(i, error); |
| 210 | + // Convert end of list to CHIP_NO_ERROR |
| 211 | + VerifyOrExit(err != CHIP_ERROR_PROVIDER_LIST_EXHAUSTED, err = CHIP_NO_ERROR); |
| 212 | + |
| 213 | + // Check if another error occurred before trying to encode |
| 214 | + SuccessOrExit(err); |
| 215 | + |
| 216 | + err = encoder.Encode(error); |
| 217 | + SuccessOrExit(err); |
| 218 | + } |
| 219 | + |
| 220 | +exit: |
| 221 | + // Tell the delegate the read is complete |
| 222 | + ReturnErrorOnFailure(mDelegate.EndCurrentErrorListRead()); |
| 223 | + return err; |
| 224 | +} |
| 225 | + |
| 226 | +CHIP_ERROR Instance::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) |
| 227 | +{ |
| 228 | + VerifyOrDie(aPath.mClusterId == ClosureControl::Id); |
| 229 | + |
| 230 | + switch (aPath.mAttributeId) |
| 231 | + { |
| 232 | + case CountdownTime::Id: |
| 233 | + if (SupportsOptAttr(OptionalAttribute::kCountdownTime)) |
| 234 | + { |
| 235 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedWrite); |
| 236 | + } |
| 237 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 238 | + case MainState::Id: |
| 239 | + case CurrentErrorList::Id: |
| 240 | + case OverallState::Id: |
| 241 | + case OverallTarget::Id: |
| 242 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedWrite); |
| 243 | + default: |
| 244 | + return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +// CommandHandlerInterface |
| 249 | +void Instance::InvokeCommand(HandlerContext & handlerContext) |
| 250 | +{ |
| 251 | + using namespace Commands; |
| 252 | + |
| 253 | + switch (handlerContext.mRequestPath.mCommandId) |
| 254 | + { |
| 255 | + case Stop::Id: |
| 256 | + if (!HasFeature(Feature::kInstantaneous)) |
| 257 | + { |
| 258 | + HandleCommand<Stop::DecodableType>( |
| 259 | + handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleStop(ctx, commandData); }); |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand); |
| 264 | + } |
| 265 | + break; |
| 266 | + case MoveTo::Id: |
| 267 | + HandleCommand<MoveTo::DecodableType>( |
| 268 | + handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleMoveTo(ctx, commandData); }); |
| 269 | + break; |
| 270 | + case Calibrate::Id: |
| 271 | + if (HasFeature(Feature::kCalibration)) |
| 272 | + { |
| 273 | + HandleCommand<Calibrate::DecodableType>( |
| 274 | + handlerContext, [this](HandlerContext & ctx, const auto & commandData) { HandleCalibrate(ctx, commandData); }); |
| 275 | + } |
| 276 | + else |
| 277 | + { |
| 278 | + handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Status::UnsupportedCommand); |
| 279 | + } |
| 280 | + break; |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +void Instance::HandleStop(HandlerContext & ctx, const Commands::Stop::DecodableType & commandData) |
| 285 | +{ |
| 286 | + Status status = mDelegate.Stop(); |
| 287 | + |
| 288 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status); |
| 289 | +} |
| 290 | + |
| 291 | +void Instance::HandleMoveTo(HandlerContext & ctx, const Commands::MoveTo::DecodableType & commandData) |
| 292 | +{ |
| 293 | + Status status = mDelegate.MoveTo(commandData.tag, commandData.latch, commandData.speed); |
| 294 | + |
| 295 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status); |
| 296 | +} |
| 297 | + |
| 298 | +void Instance::HandleCalibrate(HandlerContext & ctx, const Commands::Calibrate::DecodableType & commandData) |
| 299 | +{ |
| 300 | + Status status = mDelegate.Calibrate(); |
| 301 | + |
| 302 | + ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status); |
| 303 | +} |
| 304 | + |
| 305 | +} // namespace ClosureControl |
| 306 | +} // namespace Clusters |
| 307 | +} // namespace app |
| 308 | +} // namespace chip |
| 309 | + |
| 310 | +// ----------------------------------------------------------------------------- |
| 311 | +// Plugin initialization |
| 312 | + |
| 313 | +void MatterClosureControlPluginServerInitCallback() {} |
0 commit comments