/src/connectedhomeip/src/app/clusters/laundry-dryer-controls-server/laundry-dryer-controls-server.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * |
3 | | * Copyright (c) 2023 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/AttributeAccessInterfaceRegistry.h> |
19 | | #include <app/util/attribute-storage.h> |
20 | | #include <app/util/config.h> |
21 | | |
22 | | #include "laundry-dryer-controls-delegate.h" |
23 | | #include "laundry-dryer-controls-server.h" |
24 | | #include <app-common/zap-generated/attributes/Accessors.h> |
25 | | #include <app-common/zap-generated/callback.h> |
26 | | #include <app-common/zap-generated/cluster-enums.h> |
27 | | #include <app-common/zap-generated/cluster-objects.h> |
28 | | #include <app-common/zap-generated/ids/Attributes.h> |
29 | | #include <app-common/zap-generated/ids/Clusters.h> |
30 | | #include <app/AttributeValueEncoder.h> |
31 | | #include <app/CommandHandler.h> |
32 | | #include <app/ConcreteAttributePath.h> |
33 | | #include <app/ConcreteCommandPath.h> |
34 | | #include <app/server/Server.h> |
35 | | #include <lib/core/CHIPEncoding.h> |
36 | | |
37 | | using namespace chip; |
38 | | using namespace chip::app; |
39 | | using namespace chip::app::Clusters; |
40 | | using namespace chip::app::Clusters::LaundryDryerControls; |
41 | | using namespace chip::app::Clusters::LaundryDryerControls::Attributes; |
42 | | using chip::Protocols::InteractionModel::Status; |
43 | | |
44 | | static constexpr size_t kLaundryDryerControlsDelegateTableSize = |
45 | | MATTER_DM_LAUNDRY_DRYER_CONTROLS_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; |
46 | | |
47 | | // ----------------------------------------------------------------------------- |
48 | | // Delegate Implementation |
49 | | // |
50 | | namespace { |
51 | | Delegate * gDelegateTable[kLaundryDryerControlsDelegateTableSize] = { nullptr }; |
52 | | } |
53 | | |
54 | | namespace { |
55 | | Delegate * GetDelegate(EndpointId endpoint) |
56 | 0 | { |
57 | 0 | uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, LaundryDryerControls::Id, |
58 | 0 | MATTER_DM_LAUNDRY_DRYER_CONTROLS_CLUSTER_SERVER_ENDPOINT_COUNT); |
59 | 0 | return (ep >= kLaundryDryerControlsDelegateTableSize ? nullptr : gDelegateTable[ep]); |
60 | 0 | } |
61 | | |
62 | | } // namespace |
63 | | |
64 | | LaundryDryerControlsServer LaundryDryerControlsServer::sInstance; |
65 | | |
66 | | /********************************************************** |
67 | | * LaundryDryerControlsServer public methods |
68 | | *********************************************************/ |
69 | | void LaundryDryerControlsServer::SetDefaultDelegate(EndpointId endpoint, Delegate * delegate) |
70 | 1 | { |
71 | 1 | uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, LaundryDryerControls::Id, |
72 | 1 | MATTER_DM_LAUNDRY_DRYER_CONTROLS_CLUSTER_SERVER_ENDPOINT_COUNT); |
73 | | // if endpoint is found |
74 | 1 | if (ep < kLaundryDryerControlsDelegateTableSize) |
75 | 1 | { |
76 | 1 | gDelegateTable[ep] = delegate; |
77 | 1 | } |
78 | 1 | } |
79 | | |
80 | | LaundryDryerControlsServer & LaundryDryerControlsServer::Instance() |
81 | 1 | { |
82 | 1 | return sInstance; |
83 | 1 | } |
84 | | |
85 | | Status LaundryDryerControlsServer::SetSelectedDrynessLevel(EndpointId endpointId, DrynessLevelEnum newSelectedDrynessLevel) |
86 | 0 | { |
87 | 0 | DataModel::Nullable<DrynessLevelEnum> selectedDrynessLevel; |
88 | 0 | Status res = SelectedDrynessLevel::Get(endpointId, selectedDrynessLevel); |
89 | |
|
90 | 0 | if ((res == Status::Success) && (selectedDrynessLevel != newSelectedDrynessLevel)) |
91 | 0 | { |
92 | 0 | res = SelectedDrynessLevel::Set(endpointId, newSelectedDrynessLevel); |
93 | 0 | } |
94 | |
|
95 | 0 | return res; |
96 | 0 | } |
97 | | |
98 | | Status LaundryDryerControlsServer::GetSelectedDrynessLevel(EndpointId endpointId, |
99 | | DataModel::Nullable<DrynessLevelEnum> & selectedDrynessLevel) |
100 | 0 | { |
101 | 0 | return SelectedDrynessLevel::Get(endpointId, selectedDrynessLevel); |
102 | 0 | } |
103 | | |
104 | | /********************************************************** |
105 | | * LaundryDryerControlsServer private methods |
106 | | *********************************************************/ |
107 | | CHIP_ERROR LaundryDryerControlsServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
108 | 0 | { |
109 | 0 | if (aPath.mClusterId != LaundryDryerControls::Id) |
110 | 0 | { |
111 | | // We shouldn't have been called at all. |
112 | 0 | return CHIP_ERROR_INVALID_ARGUMENT; |
113 | 0 | } |
114 | 0 | switch (aPath.mAttributeId) |
115 | 0 | { |
116 | 0 | case Attributes::SupportedDrynessLevels::Id: |
117 | 0 | return ReadSupportedDrynessLevels(aPath, aEncoder); |
118 | 0 | default: |
119 | 0 | break; |
120 | 0 | } |
121 | 0 | return CHIP_NO_ERROR; |
122 | 0 | } |
123 | | |
124 | | CHIP_ERROR LaundryDryerControlsServer::ReadSupportedDrynessLevels(const ConcreteReadAttributePath & aPath, |
125 | | AttributeValueEncoder & aEncoder) |
126 | 0 | { |
127 | 0 | Delegate * delegate = GetDelegate(aPath.mEndpointId); |
128 | 0 | VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INCORRECT_STATE, ChipLogError(Zcl, "Delegate is nullptr")); |
129 | | |
130 | 0 | return aEncoder.EncodeList([delegate](const auto & encoder) -> CHIP_ERROR { |
131 | 0 | for (uint8_t i = 0; true; i++) |
132 | 0 | { |
133 | 0 | DrynessLevelEnum supportedDrynessLevel; |
134 | 0 | auto err = delegate->GetSupportedDrynessLevelAtIndex(i, supportedDrynessLevel); |
135 | 0 | if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED) |
136 | 0 | { |
137 | 0 | return CHIP_NO_ERROR; |
138 | 0 | } |
139 | 0 | ReturnErrorOnFailure(err); |
140 | 0 | ReturnErrorOnFailure(encoder.Encode(supportedDrynessLevel)); |
141 | 0 | } |
142 | 0 | }); |
143 | 0 | } |
144 | | |
145 | | /********************************************************** |
146 | | * Register LaundryDryerControlsServer |
147 | | *********************************************************/ |
148 | | |
149 | | void MatterLaundryDryerControlsPluginServerInitCallback() |
150 | 1 | { |
151 | 1 | LaundryDryerControlsServer & laundryDryerControlsServer = LaundryDryerControlsServer::Instance(); |
152 | 1 | AttributeAccessInterfaceRegistry::Instance().Register(&laundryDryerControlsServer); |
153 | 1 | } |
154 | | |
155 | | void MatterLaundryDryerControlsPluginServerShutdownCallback() |
156 | 0 | { |
157 | 0 | LaundryDryerControlsServer & laundryDryerControlsServer = LaundryDryerControlsServer::Instance(); |
158 | 0 | AttributeAccessInterfaceRegistry::Instance().Unregister(&laundryDryerControlsServer); |
159 | 0 | } |
160 | | |
161 | | Status MatterLaundryDryerControlsClusterServerPreAttributeChangedCallback(const chip::app::ConcreteAttributePath & attributePath, |
162 | | EmberAfAttributeType attributeType, uint16_t size, |
163 | | uint8_t * value) |
164 | 0 | { |
165 | 0 | Delegate * delegate = GetDelegate(attributePath.mEndpointId); |
166 | 0 | VerifyOrDie((delegate != nullptr) && "Dryer Controls implementation requires a registered delegate for validation."); |
167 | 0 | switch (attributePath.mAttributeId) |
168 | 0 | { |
169 | 0 | case Attributes::SelectedDrynessLevel::Id: { |
170 | 0 | uint8_t drynessLevelIdx = 0; |
171 | 0 | if (NumericAttributeTraits<uint8_t>::IsNullValue(*value)) |
172 | 0 | { |
173 | 0 | return Status::Success; |
174 | 0 | } |
175 | 0 | while (true) |
176 | 0 | { |
177 | 0 | DrynessLevelEnum supportedDryness; |
178 | 0 | auto err = delegate->GetSupportedDrynessLevelAtIndex(drynessLevelIdx, supportedDryness); |
179 | 0 | if (err != CHIP_NO_ERROR) |
180 | 0 | { |
181 | | // Can't find the attribute to be written in the supported list (CHIP_ERROR_PROVIDER_LIST_EXHAUSTED) |
182 | | // Or can't get the correct supported list |
183 | 0 | return Status::ConstraintError; |
184 | 0 | } |
185 | 0 | static_assert(sizeof(DrynessLevelEnum) == sizeof(*value), "Enum size doesn't match parameter size"); |
186 | 0 | if (supportedDryness == static_cast<DrynessLevelEnum>(*value)) |
187 | 0 | { |
188 | | // The written attribute is one of the supported item |
189 | 0 | return Status::Success; |
190 | 0 | } |
191 | 0 | drynessLevelIdx++; |
192 | 0 | } |
193 | 0 | } |
194 | 0 | } |
195 | 0 | return Status::Success; |
196 | 0 | } |