-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmicrowave-oven-control-server.cpp
387 lines (325 loc) · 18 KB
/
microwave-oven-control-server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/CommandHandlerInterfaceRegistry.h>
#include <app/ConcreteClusterPath.h>
#include <app/InteractionModelEngine.h>
#include <app/clusters/microwave-oven-control-server/microwave-oven-control-server.h>
#include <app/clusters/mode-base-server/mode-base-server.h>
#include <app/data-model-provider/MetadataList.h>
#include <app/data-model-provider/MetadataTypes.h>
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OperationalState;
using namespace chip::app::Clusters::MicrowaveOvenControl;
using namespace chip::app::Clusters::ModeBase;
using namespace chip::app::Clusters::MicrowaveOvenMode;
using namespace chip::app::Clusters::MicrowaveOvenControl::Attributes;
using Status = Protocols::InteractionModel::Status;
namespace chip {
namespace app {
namespace Clusters {
namespace MicrowaveOvenControl {
Instance::Instance(Delegate * aDelegate, EndpointId aEndpointId, ClusterId aClusterId,
BitMask<MicrowaveOvenControl::Feature> aFeature, Clusters::OperationalState::Instance & aOpStateInstance,
Clusters::ModeBase::Instance & aMicrowaveOvenModeInstance) :
CommandHandlerInterface(MakeOptional(aEndpointId), aClusterId),
AttributeAccessInterface(MakeOptional(aEndpointId), aClusterId), mDelegate(aDelegate), mEndpointId(aEndpointId),
mClusterId(aClusterId), mFeature(aFeature), mOpStateInstance(aOpStateInstance),
mMicrowaveOvenModeInstance(aMicrowaveOvenModeInstance)
{
mDelegate->SetInstance(this);
}
Instance::~Instance()
{
CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler(this);
AttributeAccessInterfaceRegistry::Instance().Unregister(this);
}
CHIP_ERROR Instance::Init()
{
// Check if the cluster has been selected in zap
VerifyOrReturnError(
emberAfContainsServer(mEndpointId, mClusterId), CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Zcl, "Microwave Oven Control: The cluster with ID %lu was not enabled in zap.", long(mClusterId)));
// Exactly one of the PowerAsNumber and PowerInWatts features must be supported, per spec.
VerifyOrReturnError(
mFeature.Has(MicrowaveOvenControl::Feature::kPowerAsNumber) || mFeature.Has(MicrowaveOvenControl::Feature::kPowerInWatts),
CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Zcl,
"Microwave Oven Control: feature bits error, feature must support one of PowerInWatts and PowerAsNumber"));
// Check that the feature bits do not include both PowerAsNumber and PowerInWatts
VerifyOrReturnError(
!(mFeature.Has(MicrowaveOvenControl::Feature::kPowerAsNumber) &&
mFeature.Has(MicrowaveOvenControl::Feature::kPowerInWatts)),
CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Zcl,
"Microwave Oven Control: feature bits error, feature could not support both PowerAsNumber and PowerInWatts"));
// Per spec, the PowerNumberLimits feature is only allowed if the PowerAsNumber feature is supported.
VerifyOrReturnError(
!mFeature.Has(MicrowaveOvenControl::Feature::kPowerNumberLimits) ||
mFeature.Has(MicrowaveOvenControl::Feature::kPowerAsNumber),
CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(
Zcl,
"Microwave Oven Control: feature bits error, if feature supports PowerNumberLimits it must support PowerAsNumber"));
ReturnErrorOnFailure(CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(this));
VerifyOrReturnError(AttributeAccessInterfaceRegistry::Instance().Register(this), CHIP_ERROR_INCORRECT_STATE);
// If the PowerInWatts feature is supported, get the count of supported watt levels so we can later
// ensure incoming watt level values are valid.
if (HasFeature(MicrowaveOvenControl::Feature::kPowerInWatts))
{
mSupportedWattLevels = GetCountOfSupportedWattLevels();
VerifyOrReturnError(mSupportedWattLevels > 0, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Zcl, "Microwave Oven Control: supported watt levels is empty"));
}
return CHIP_NO_ERROR;
}
bool Instance::HasFeature(MicrowaveOvenControl::Feature feature) const
{
return mFeature.Has(feature);
}
uint8_t Instance::GetCountOfSupportedWattLevels() const
{
uint8_t wattIndex = 0;
uint16_t watt = 0;
while (mDelegate->GetWattSettingByIndex(wattIndex, watt) == CHIP_NO_ERROR)
{
wattIndex++;
}
return wattIndex;
}
uint32_t Instance::GetCookTimeSec() const
{
return mCookTimeSec;
}
void Instance::SetCookTimeSec(uint32_t cookTimeSec)
{
uint32_t oldCookTimeSec = mCookTimeSec;
mCookTimeSec = cookTimeSec;
if (mCookTimeSec != oldCookTimeSec)
{
MatterReportingAttributeChangeCallback(mEndpointId, mClusterId, Attributes::CookTime::Id);
}
}
CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
ChipLogError(Zcl, "Microwave Oven Control: Reading");
switch (aPath.mAttributeId)
{
case MicrowaveOvenControl::Attributes::CookTime::Id:
return aEncoder.Encode(GetCookTimeSec());
case MicrowaveOvenControl::Attributes::MaxCookTime::Id:
return aEncoder.Encode(mDelegate->GetMaxCookTimeSec());
case MicrowaveOvenControl::Attributes::PowerSetting::Id:
VerifyOrReturnError(HasFeature(MicrowaveOvenControl::Feature::kPowerAsNumber), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE,
ChipLogError(Zcl, "Microwave Oven Control: can not get PowerSetting number, feature is not supported"));
return aEncoder.Encode(mDelegate->GetPowerSettingNum());
case MicrowaveOvenControl::Attributes::MinPower::Id:
VerifyOrReturnError(HasFeature(MicrowaveOvenControl::Feature::kPowerAsNumber), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE,
ChipLogError(Zcl, "Microwave Oven Control: can not get MinPower number, feature is not supported"));
return aEncoder.Encode(HasFeature(MicrowaveOvenControl::Feature::kPowerNumberLimits) ? mDelegate->GetMinPowerNum()
: kDefaultMinPowerNum);
case MicrowaveOvenControl::Attributes::MaxPower::Id:
VerifyOrReturnError(HasFeature(MicrowaveOvenControl::Feature::kPowerAsNumber), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE,
ChipLogError(Zcl, "Microwave Oven Control: can not get MaxPower number, feature is not supported"));
return aEncoder.Encode(HasFeature(MicrowaveOvenControl::Feature::kPowerNumberLimits) ? mDelegate->GetMaxPowerNum()
: kDefaultMaxPowerNum);
case MicrowaveOvenControl::Attributes::PowerStep::Id:
VerifyOrReturnError(HasFeature(MicrowaveOvenControl::Feature::kPowerAsNumber), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE,
ChipLogError(Zcl, "Microwave Oven Control: can not get PowerStep number, feature is not supported"));
return aEncoder.Encode(HasFeature(MicrowaveOvenControl::Feature::kPowerNumberLimits) ? mDelegate->GetPowerStepNum()
: kDefaultPowerStepNum);
case MicrowaveOvenControl::Attributes::SupportedWatts::Id:
VerifyOrReturnError(HasFeature(MicrowaveOvenControl::Feature::kPowerInWatts), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE,
ChipLogError(Zcl, "Microwave Oven Control: can not get SuppoertWatts list, feature is not supported"));
return aEncoder.EncodeList([delegate = mDelegate](const auto & encoder) -> CHIP_ERROR {
uint16_t wattRating;
uint8_t index = 0;
CHIP_ERROR err = CHIP_NO_ERROR;
while ((err = delegate->GetWattSettingByIndex(index, wattRating)) == CHIP_NO_ERROR)
{
ReturnErrorOnFailure(encoder.Encode(wattRating));
index++;
}
if (err == CHIP_ERROR_NOT_FOUND)
{
return CHIP_NO_ERROR;
}
return err;
});
case MicrowaveOvenControl::Attributes::SelectedWattIndex::Id:
VerifyOrReturnError(
HasFeature(MicrowaveOvenControl::Feature::kPowerInWatts), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE,
ChipLogError(Zcl, "Microwave Oven Control: can not get SelectedWattIndex number, feature is not supported"));
return aEncoder.Encode(mDelegate->GetCurrentWattIndex());
case MicrowaveOvenControl::Attributes::WattRating::Id:
return aEncoder.Encode(mDelegate->GetWattRating());
case MicrowaveOvenControl::Attributes::FeatureMap::Id:
return aEncoder.Encode(mFeature.Raw());
}
return CHIP_NO_ERROR;
}
void Instance::InvokeCommand(HandlerContext & handlerContext)
{
ChipLogDetail(Zcl, "Microwave Oven Control: InvokeCommand");
switch (handlerContext.mRequestPath.mCommandId)
{
case Commands::SetCookingParameters::Id:
ChipLogDetail(Zcl, "Microwave Oven Control: Entering SetCookingParameters");
CommandHandlerInterface::HandleCommand<Commands::SetCookingParameters::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & req) { HandleSetCookingParameters(ctx, req); });
break;
case Commands::AddMoreTime::Id:
ChipLogDetail(Zcl, "Microwave Oven Control: Entering AddMoreTime");
CommandHandlerInterface::HandleCommand<Commands::AddMoreTime::DecodableType>(
handlerContext, [this](HandlerContext & ctx, const auto & req) { HandleAddMoreTime(ctx, req); });
break;
}
}
void Instance::HandleSetCookingParameters(HandlerContext & ctx, const Commands::SetCookingParameters::DecodableType & req)
{
ChipLogDetail(Zcl, "Microwave Oven Control: HandleSetCookingParameters");
Status status = Status::Success;
uint8_t opState;
uint8_t modeValue;
uint8_t reqCookMode;
uint32_t reqCookTimeSec;
bool reqStartAfterSetting;
auto & cookMode = req.cookMode;
auto & cookTimeSec = req.cookTime;
auto & powerSetting = req.powerSetting;
auto & wattSettingIndex = req.wattSettingIndex;
auto & startAfterSetting = req.startAfterSetting;
opState = mOpStateInstance.GetCurrentOperationalState();
VerifyOrExit(opState == to_underlying(OperationalStateEnum::kStopped), status = Status::InvalidInState);
if (startAfterSetting.HasValue())
{
DataModel::ListBuilder<DataModel::AcceptedCommandEntry> acceptedCommandsList;
InteractionModelEngine::GetInstance()->GetDataModelProvider()->AcceptedCommands(
ConcreteClusterPath(mEndpointId, OperationalState::Id), acceptedCommandsList);
auto acceptedCommands = acceptedCommandsList.TakeBuffer();
bool commandExists =
std::find_if(acceptedCommands.begin(), acceptedCommands.end(), [](const DataModel::AcceptedCommandEntry & entry) {
return entry.commandId == OperationalState::Commands::Start::Id;
}) != acceptedCommands.end();
VerifyOrExit(
commandExists, status = Status::InvalidCommand; ChipLogError(
Zcl,
"Microwave Oven Control: Failed to set cooking parameters, Start command of operational state is not supported"));
}
reqStartAfterSetting = startAfterSetting.ValueOr(false);
modeValue = 0;
VerifyOrExit(mMicrowaveOvenModeInstance.GetModeValueByModeTag(to_underlying(MicrowaveOvenMode::ModeTag::kNormal), modeValue) ==
CHIP_NO_ERROR,
status = Status::InvalidCommand;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set cookMode, Normal mode is not found"));
reqCookMode = cookMode.ValueOr(modeValue);
VerifyOrExit(mMicrowaveOvenModeInstance.IsSupportedMode(reqCookMode), status = Status::ConstraintError;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set cookMode, cookMode is not supported"));
reqCookTimeSec = cookTimeSec.ValueOr(MicrowaveOvenControl::kDefaultCookTimeSec);
VerifyOrExit(IsCookTimeSecondsInRange(reqCookTimeSec, mDelegate->GetMaxCookTimeSec()), status = Status::ConstraintError;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set cookTime, cookTime value is out of range"));
if (HasFeature(MicrowaveOvenControl::Feature::kPowerAsNumber))
{
// if using power as number, check if the param is invalid and set PowerSetting number.
uint8_t reqPowerSettingNum;
uint8_t maxPowerNum = kDefaultMaxPowerNum;
uint8_t minPowerNum = kDefaultMinPowerNum;
uint8_t powerStepNum = kDefaultPowerStepNum;
VerifyOrExit(!wattSettingIndex.HasValue(), status = Status::InvalidCommand; ChipLogError(
Zcl, "Microwave Oven Control: Failed to set cooking parameters, should have no value for wattSettingIndex"));
VerifyOrExit(
cookMode.HasValue() || cookTimeSec.HasValue() || powerSetting.HasValue(), status = Status::InvalidCommand;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set cooking parameters, all command fields are missing "));
if (HasFeature(MicrowaveOvenControl::Feature::kPowerNumberLimits))
{
maxPowerNum = mDelegate->GetMaxPowerNum();
minPowerNum = mDelegate->GetMinPowerNum();
powerStepNum = mDelegate->GetPowerStepNum();
}
reqPowerSettingNum = powerSetting.ValueOr(maxPowerNum);
VerifyOrExit(IsPowerSettingNumberInRange(reqPowerSettingNum, minPowerNum, maxPowerNum), status = Status::ConstraintError;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set PowerSetting, PowerSetting value is out of range"));
VerifyOrExit(
(reqPowerSettingNum - minPowerNum) % powerStepNum == 0, status = Status::ConstraintError; ChipLogError(
Zcl,
"Microwave Oven Control: Failed to set PowerSetting, PowerSetting value must be multiple of PowerStep number"));
status = mDelegate->HandleSetCookingParametersCallback(reqCookMode, reqCookTimeSec, reqStartAfterSetting,
MakeOptional(reqPowerSettingNum), NullOptional);
}
else
{
// if using power in watt, check if the param is invalid and set wattSettingIndex number.
uint8_t reqWattSettingIndex;
VerifyOrExit(!powerSetting.HasValue(), status = Status::InvalidCommand; ChipLogError(
Zcl, "Microwave Oven Control: Failed to set cooking parameters, should have no value for powerSetting "));
VerifyOrExit(
cookMode.HasValue() || cookTimeSec.HasValue() || wattSettingIndex.HasValue(), status = Status::InvalidCommand;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set cooking parameters, all command fields are missing "));
// count of supported watt levels must greater than 0
VerifyOrExit(
mSupportedWattLevels > 0,
ChipLogError(Zcl, "Microwave Oven Control: Failed to set wattSettingIndex, count of supported watt levels is 0"));
uint8_t maxWattSettingIndex = static_cast<uint8_t>(mSupportedWattLevels - 1);
reqWattSettingIndex = wattSettingIndex.ValueOr(maxWattSettingIndex);
VerifyOrExit(reqWattSettingIndex <= maxWattSettingIndex, status = Status::ConstraintError;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set wattSettingIndex, wattSettingIndex is out of range"));
status = mDelegate->HandleSetCookingParametersCallback(reqCookMode, reqCookTimeSec, reqStartAfterSetting, NullOptional,
MakeOptional(reqWattSettingIndex));
}
exit:
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
void Instance::HandleAddMoreTime(HandlerContext & ctx, const Commands::AddMoreTime::DecodableType & req)
{
ChipLogDetail(Zcl, "Microwave Oven Control: HandleAddMoreTime");
Status status;
uint8_t opState;
uint32_t finalCookTimeSec;
opState = mOpStateInstance.GetCurrentOperationalState();
VerifyOrExit(opState != to_underlying(OperationalStateEnum::kError), status = Status::InvalidInState);
// if the added cooking time is greater than the max cooking time, the cooking time stay unchanged.
VerifyOrExit(req.timeToAdd + GetCookTimeSec() <= mDelegate->GetMaxCookTimeSec(), status = Status::ConstraintError;
ChipLogError(Zcl, "Microwave Oven Control: Failed to set cookTime, cookTime value is out of range"));
finalCookTimeSec = GetCookTimeSec() + req.timeToAdd;
status = mDelegate->HandleModifyCookTimeSecondsCallback(finalCookTimeSec);
exit:
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}
bool IsCookTimeSecondsInRange(uint32_t cookTimeSec, uint32_t maxCookTimeSec)
{
return MicrowaveOvenControl::kMinCookTimeSec <= cookTimeSec && cookTimeSec <= maxCookTimeSec;
}
bool IsPowerSettingNumberInRange(uint8_t powerSettingNum, uint8_t minCookPowerNum, uint8_t maxCookPowerNum)
{
return minCookPowerNum <= powerSettingNum && powerSettingNum <= maxCookPowerNum;
}
} // namespace MicrowaveOvenControl
} // namespace Clusters
} // namespace app
} // namespace chip
/** @brief Microwave Oven Control Cluster Server Init
*
* Server Init
*
*/
void MatterMicrowaveOvenControlPluginServerInitCallback() {}