/src/connectedhomeip/src/app/clusters/energy-preference-server/energy-preference-server.cpp
Line | Count | Source |
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 | | #include "energy-preference-server.h" |
19 | | |
20 | | #include <app/AttributeAccessInterfaceRegistry.h> |
21 | | #include <app/util/attribute-storage.h> // Needed for AttributeAccessInterfaceRegistry::Instance().Register |
22 | | |
23 | | #include <app-common/zap-generated/attributes/Accessors.h> |
24 | | #include <app-common/zap-generated/callback.h> |
25 | | #include <app-common/zap-generated/cluster-objects.h> |
26 | | #include <app-common/zap-generated/ids/Attributes.h> |
27 | | #include <app/AttributeAccessInterface.h> |
28 | | #include <app/ConcreteAttributePath.h> |
29 | | #include <lib/core/CHIPEncoding.h> |
30 | | |
31 | | using namespace chip; |
32 | | using namespace chip::app; |
33 | | using namespace chip::app::Clusters; |
34 | | using namespace chip::app::Clusters::EnergyPreference; |
35 | | using namespace chip::app::Clusters::EnergyPreference::Structs; |
36 | | using namespace chip::app::Clusters::EnergyPreference::Attributes; |
37 | | |
38 | | using Status = Protocols::InteractionModel::Status; |
39 | | |
40 | | namespace { |
41 | | |
42 | | class EnergyPrefAttrAccess : public AttributeAccessInterface |
43 | | { |
44 | | public: |
45 | 0 | EnergyPrefAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), EnergyPreference::Id) {} |
46 | | |
47 | | CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override; |
48 | | }; |
49 | | |
50 | | EnergyPrefAttrAccess gEnergyPrefAttrAccess; |
51 | | Delegate * gsDelegate = nullptr; |
52 | | |
53 | | CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) |
54 | 0 | { |
55 | 0 | VerifyOrDie(aPath.mClusterId == EnergyPreference::Id); |
56 | 0 | EndpointId endpoint = aPath.mEndpointId; |
57 | 0 | uint32_t ourFeatureMap = 0; |
58 | 0 | const bool featureMapIsGood = FeatureMap::Get(aPath.mEndpointId, &ourFeatureMap) == Status::Success; |
59 | 0 | const bool balanceSupported = featureMapIsGood && ((ourFeatureMap & to_underlying(Feature::kEnergyBalance)) != 0); |
60 | 0 | const bool lowPowerSupported = featureMapIsGood && ((ourFeatureMap & to_underlying(Feature::kLowPowerModeSensitivity)) != 0); |
61 | |
|
62 | 0 | switch (aPath.mAttributeId) |
63 | 0 | { |
64 | 0 | case EnergyBalances::Id: |
65 | 0 | if (!balanceSupported) |
66 | 0 | { |
67 | 0 | return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
68 | 0 | } |
69 | | |
70 | 0 | if (gsDelegate != nullptr) |
71 | 0 | { |
72 | 0 | return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR { |
73 | 0 | size_t index = 0; |
74 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
75 | 0 | do |
76 | 0 | { |
77 | 0 | Percent step; |
78 | 0 | char buffer[64]; |
79 | 0 | Optional<MutableCharSpan> label{ MutableCharSpan(buffer) }; |
80 | 0 | if ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR) |
81 | 0 | { |
82 | 0 | BalanceStruct::Type balance = { step, Optional<CharSpan>(label) }; |
83 | 0 | ReturnErrorOnFailure(encoder.Encode(balance)); |
84 | 0 | index++; |
85 | 0 | } |
86 | 0 | } while (err == CHIP_NO_ERROR); |
87 | | |
88 | 0 | if (err == CHIP_ERROR_NOT_FOUND) |
89 | 0 | { |
90 | 0 | return CHIP_NO_ERROR; |
91 | 0 | } |
92 | 0 | return err; |
93 | 0 | }); |
94 | 0 | } |
95 | 0 | return CHIP_ERROR_INCORRECT_STATE; |
96 | 0 | case EnergyPriorities::Id: |
97 | 0 | if (balanceSupported == false) |
98 | 0 | { |
99 | 0 | return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
100 | 0 | } |
101 | | |
102 | 0 | if (gsDelegate != nullptr) |
103 | 0 | { |
104 | 0 | return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR { |
105 | 0 | EnergyPriorityEnum priority; |
106 | 0 | size_t index = 0; |
107 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
108 | 0 | while ((err = gsDelegate->GetEnergyPriorityAtIndex(endpoint, index, priority)) == CHIP_NO_ERROR) |
109 | 0 | { |
110 | 0 | ReturnErrorOnFailure(encoder.Encode(priority)); |
111 | 0 | index++; |
112 | 0 | } |
113 | 0 | if (err == CHIP_ERROR_NOT_FOUND) |
114 | 0 | { |
115 | 0 | return CHIP_NO_ERROR; |
116 | 0 | } |
117 | 0 | return err; |
118 | 0 | }); |
119 | 0 | } |
120 | 0 | return CHIP_ERROR_INCORRECT_STATE; |
121 | 0 | case LowPowerModeSensitivities::Id: |
122 | 0 | if (lowPowerSupported == false) |
123 | 0 | { |
124 | 0 | return CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute); |
125 | 0 | } |
126 | | |
127 | 0 | if (gsDelegate != nullptr) |
128 | 0 | { |
129 | 0 | return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR { |
130 | 0 | size_t index = 0; |
131 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
132 | 0 | do |
133 | 0 | { |
134 | 0 | Percent step; |
135 | 0 | char buffer[64]; |
136 | 0 | Optional<MutableCharSpan> label{ MutableCharSpan(buffer) }; |
137 | 0 | if ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR) |
138 | 0 | { |
139 | 0 | BalanceStruct::Type balance = { step, Optional<CharSpan>(label) }; |
140 | 0 | ReturnErrorOnFailure(encoder.Encode(balance)); |
141 | 0 | index++; |
142 | 0 | } |
143 | 0 | } while (err == CHIP_NO_ERROR); |
144 | 0 | if (err == CHIP_ERROR_NOT_FOUND) |
145 | 0 | { |
146 | 0 | return CHIP_NO_ERROR; |
147 | 0 | } |
148 | 0 | return err; |
149 | 0 | }); |
150 | 0 | } |
151 | 0 | return CHIP_ERROR_INCORRECT_STATE; |
152 | 0 | default: // return CHIP_NO_ERROR and just read from the attribute store in default |
153 | 0 | break; |
154 | 0 | } |
155 | | |
156 | 0 | return CHIP_NO_ERROR; |
157 | 0 | } |
158 | | |
159 | | } // anonymous namespace |
160 | | |
161 | | namespace chip::app::Clusters::EnergyPreference { |
162 | | |
163 | | void SetDelegate(Delegate * aDelegate) |
164 | 0 | { |
165 | 0 | gsDelegate = aDelegate; |
166 | 0 | } |
167 | | |
168 | | Delegate * GetDelegate() |
169 | 0 | { |
170 | 0 | return gsDelegate; |
171 | 0 | } |
172 | | |
173 | | } // namespace chip::app::Clusters::EnergyPreference |
174 | | |
175 | | Status MatterEnergyPreferenceClusterServerPreAttributeChangedCallback(const ConcreteAttributePath & attributePath, |
176 | | EmberAfAttributeType attributeType, uint16_t size, |
177 | | uint8_t * value) |
178 | 0 | { |
179 | 0 | EndpointId endpoint = attributePath.mEndpointId; |
180 | 0 | Delegate * delegate = GetDelegate(); |
181 | 0 | uint32_t ourFeatureMap; |
182 | 0 | const bool featureMapIsGood = FeatureMap::Get(attributePath.mEndpointId, &ourFeatureMap) == Status::Success; |
183 | 0 | const bool balanceSupported = featureMapIsGood && ((ourFeatureMap & to_underlying(Feature::kEnergyBalance)) != 0); |
184 | 0 | const bool lowPowerSupported = featureMapIsGood && ((ourFeatureMap & to_underlying(Feature::kLowPowerModeSensitivity)) != 0); |
185 | |
|
186 | 0 | if (delegate == nullptr) |
187 | 0 | { |
188 | 0 | return Status::UnsupportedWrite; |
189 | 0 | } |
190 | | |
191 | 0 | switch (attributePath.mAttributeId) |
192 | 0 | { |
193 | 0 | case CurrentEnergyBalance::Id: { |
194 | 0 | if (balanceSupported == false) |
195 | 0 | { |
196 | 0 | return Status::UnsupportedAttribute; |
197 | 0 | } |
198 | | |
199 | 0 | uint8_t index = Encoding::Get8(value); |
200 | 0 | size_t arraySize = delegate->GetNumEnergyBalances(endpoint); |
201 | 0 | if (index >= arraySize) |
202 | 0 | { |
203 | 0 | return Status::ConstraintError; |
204 | 0 | } |
205 | | |
206 | 0 | return Status::Success; |
207 | 0 | } |
208 | | |
209 | 0 | case CurrentLowPowerModeSensitivity::Id: { |
210 | 0 | if (lowPowerSupported == false) |
211 | 0 | { |
212 | 0 | return Status::UnsupportedAttribute; |
213 | 0 | } |
214 | | |
215 | 0 | uint8_t index = Encoding::Get8(value); |
216 | 0 | size_t arraySize = delegate->GetNumLowPowerModeSensitivities(endpoint); |
217 | 0 | if (index >= arraySize) |
218 | 0 | { |
219 | 0 | return Status::ConstraintError; |
220 | 0 | } |
221 | | |
222 | 0 | return Status::Success; |
223 | 0 | } |
224 | 0 | default: |
225 | 0 | return Status::Success; |
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | | void MatterEnergyPreferencePluginServerInitCallback() |
230 | 0 | { |
231 | 0 | AttributeAccessInterfaceRegistry::Instance().Register(&gEnergyPrefAttrAccess); |
232 | 0 | } |
233 | | |
234 | | void MatterEnergyPreferencePluginServerShutdownCallback() |
235 | 0 | { |
236 | 0 | AttributeAccessInterfaceRegistry::Instance().Unregister(&gEnergyPrefAttrAccess); |
237 | 0 | } |