Skip to content

Commit cc22f3e

Browse files
Update API docs, command handling to return InvalidCommand, update Modify* to a notification as suggested in the code review.
1 parent ffca294 commit cc22f3e

File tree

5 files changed

+55
-88
lines changed

5 files changed

+55
-88
lines changed

examples/all-clusters-app/all-clusters-common/include/bridged-actions-stub.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ActionsDelegateImpl : public Delegate
5454

5555
CHIP_ERROR ReadActionAtIndex(uint16_t index, ActionStructStorage & action) override;
5656
CHIP_ERROR ReadEndpointListAtIndex(uint16_t index, EndpointListStorage & epList) override;
57-
bool HaveActionWithId(uint16_t actionId) override;
57+
bool HaveActionWithId(uint16_t actionId, uint16_t & actionIndex) override;
5858

5959
Protocols::InteractionModel::Status HandleInstantAction(uint16_t actionId, Optional<uint32_t> invokeId) override;
6060
Protocols::InteractionModel::Status HandleInstantActionWithTransition(uint16_t actionId, uint16_t transitionTime,

examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ CHIP_ERROR ActionsDelegateImpl::ReadEndpointListAtIndex(uint16_t index, Endpoint
4444
return CHIP_NO_ERROR;
4545
}
4646

47-
bool ActionsDelegateImpl::HaveActionWithId(uint16_t actionId)
47+
bool ActionsDelegateImpl::HaveActionWithId(uint16_t actionId, uint16_t & actionIndex)
4848
{
49-
for (size_t i = 0; i < kEndpointList.size(); i++)
49+
for (uint16_t i = 0; i < kEndpointList.size(); i++)
5050
{
5151
if (kActionList[i].actionID == actionId)
52+
{
53+
actionIndex = i;
5254
return true;
55+
}
5356
}
5457
return false;
5558
}

src/app/clusters/actions-server/actions-server.cpp

+24-51
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ CHIP_ERROR ActionsServer::ReadEndpointListAttribute(const ConcreteReadAttributeP
165165
return CHIP_NO_ERROR;
166166
}
167167

168-
bool ActionsServer::HaveActionWithId(EndpointId aEndpointId, uint16_t aActionId)
168+
bool ActionsServer::HaveActionWithId(EndpointId aEndpointId, uint16_t aActionId, uint16_t & aActionIndex)
169169
{
170170
Delegate * delegate = GetDelegate(aEndpointId);
171171
VerifyOrReturnValue(ValidateDelegate(delegate, aEndpointId) == CHIP_NO_ERROR, false);
172-
return delegate->HaveActionWithId(aActionId);
172+
return delegate->HaveActionWithId(aActionId, aActionIndex);
173173
}
174174

175175
template <typename RequestT, typename FuncT>
@@ -193,11 +193,26 @@ void ActionsServer::HandleCommand(HandlerContext & handlerContext, FuncT func)
193193
return;
194194
}
195195

196-
if (!HaveActionWithId(handlerContext.mRequestPath.mEndpointId, requestPayload.actionID))
196+
uint16_t actionIndex = kMaxActionListLength;
197+
if (!HaveActionWithId(handlerContext.mRequestPath.mEndpointId, requestPayload.actionID, actionIndex))
197198
{
198199
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath, Protocols::InteractionModel::Status::NotFound);
199200
return;
200201
}
202+
if (actionIndex != kMaxActionListLength)
203+
{
204+
Delegate * delegate = GetDelegate(handlerContext.mRequestPath.mEndpointId);
205+
ReturnOnFailure(ValidateDelegate(delegate, handlerContext.mRequestPath.mEndpointId));
206+
ActionStructStorage action;
207+
delegate->ReadActionAtIndex(actionIndex, action);
208+
// Check if the command bit is set in the SupportedCommands of an ations.
209+
if (!(action.supportedCommands.Raw() & (1 << handlerContext.mRequestPath.mCommandId)))
210+
{
211+
handlerContext.mCommandHandler.AddStatus(handlerContext.mRequestPath,
212+
Protocols::InteractionModel::Status::InvalidCommand);
213+
return;
214+
}
215+
}
201216

202217
func(handlerContext, requestPayload);
203218
}
@@ -401,58 +416,16 @@ void ActionsServer::InvokeCommand(HandlerContext & handlerContext)
401416
}
402417
}
403418

404-
CHIP_ERROR ActionsServer::ModifyActionList(EndpointId aEndpoint, const ActionStructStorage & aAction)
419+
void ActionsServer::ActionListModified(EndpointId aEndpoint)
405420
{
406-
Delegate * delegate = GetDelegate(aEndpoint);
407-
ReturnErrorOnFailure(ValidateDelegate(delegate, aEndpoint));
408-
409-
// Read through the list to find and update the existing action that matches the passed-in action's ID.
410-
for (uint16_t i = 0; i < kMaxActionListLength; i++)
411-
{
412-
ActionStructStorage existingAction;
413-
CHIP_ERROR err = delegate->ReadActionAtIndex(i, existingAction);
414-
if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
415-
{
416-
break;
417-
}
418-
419-
if (existingAction.actionID == aAction.actionID)
420-
{
421-
existingAction.Set(aAction.actionID, aAction.name, aAction.type, aAction.endpointListID, aAction.supportedCommands,
422-
aAction.state);
423-
424-
MarkDirty(aEndpoint, Attributes::ActionList::Id);
425-
return CHIP_NO_ERROR;
426-
}
427-
}
428-
429-
return CHIP_ERROR_NOT_FOUND;
421+
MarkDirty(aEndpoint, Attributes::ActionList::Id);
422+
return;
430423
}
431424

432-
CHIP_ERROR ActionsServer::ModifyEndpointList(EndpointId aEndpoint, const EndpointListStorage & aEpList)
425+
void ActionsServer::EndpointListModified(EndpointId aEndpoint)
433426
{
434-
Delegate * delegate = GetDelegate(aEndpoint);
435-
ReturnErrorOnFailure(ValidateDelegate(delegate, aEndpoint));
436-
437-
// Read through the list to find and update the existing action that matches the passed-in endpoint-list's ID
438-
for (uint16_t i = 0; i < kMaxEndpointListLength; i++)
439-
{
440-
EndpointListStorage existingEpList;
441-
CHIP_ERROR err = delegate->ReadEndpointListAtIndex(i, existingEpList);
442-
if (err == CHIP_ERROR_PROVIDER_LIST_EXHAUSTED)
443-
{
444-
break;
445-
}
446-
447-
if (existingEpList.endpointListID == aEpList.endpointListID)
448-
{
449-
existingEpList.Set(aEpList.endpointListID, aEpList.name, aEpList.type, aEpList.endpoints);
450-
MarkDirty(aEndpoint, Attributes::EndpointLists::Id);
451-
return CHIP_NO_ERROR;
452-
}
453-
}
454-
455-
return CHIP_ERROR_NOT_FOUND;
427+
MarkDirty(aEndpoint, Attributes::EndpointLists::Id);
428+
return;
456429
}
457430

458431
void MatterActionsPluginServerInitCallback()

src/app/clusters/actions-server/actions-server.h

+23-33
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,18 @@ class ActionsServer : public AttributeAccessInterface, public CommandHandlerInte
138138
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
139139

140140
/**
141-
* Update an existing action in the action list for the given endpoint.
142-
* If the action with the given actionID doesn't exist, returns CHIP_ERROR_NOT_FOUND.
141+
* A notification from an application to the sever that an ActionList is modified..
143142
*
144143
* @param aEndpoint The endpoint ID where the action should be updated
145-
* @param aAction The action structure containing the updated action details
146-
* @return CHIP_ERROR_INCORRECT_STATE if delegate is null
147-
* CHIP_ERROR_NOT_FOUND if action doesn't exist
148-
* CHIP_NO_ERROR if successful
149144
*/
150-
CHIP_ERROR ModifyActionList(EndpointId aEndpoint, const ActionStructStorage & aAction);
145+
void ActionListModified(EndpointId aEndpoint);
151146

152147
/**
153-
* Update an existing endpoint list for the given endpoint.
154-
* If the endpoint list with the given ID doesn't exist, returns CHIP_ERROR_NOT_FOUND.
148+
* A notification from an application to the sever that an EndpointList is modified..
155149
*
156-
* @param aEndpoint The endpoint ID where the endpoint list should be updated
157-
* @param aEpList The endpoint list structure containing the updated list details
158-
* @return CHIP_ERROR_INCORRECT_STATE if delegate is null
159-
* CHIP_ERROR_NOT_FOUND if endpoint list doesn't exist
160-
* CHIP_NO_ERROR if successful
150+
* @param aEndpoint The endpoint ID where the action should be updated
161151
*/
162-
CHIP_ERROR ModifyEndpointList(EndpointId aEndpoint, const EndpointListStorage & aEpList);
152+
void EndpointListModified(EndpointId aEndpoint);
163153

164154
private:
165155
static ActionsServer sInstance;
@@ -170,7 +160,7 @@ class ActionsServer : public AttributeAccessInterface, public CommandHandlerInte
170160
const AttributeValueEncoder::ListEncodeHelper & aEncoder);
171161
CHIP_ERROR ReadEndpointListAttribute(const ConcreteReadAttributePath & aPath,
172162
const AttributeValueEncoder::ListEncodeHelper & aEncoder);
173-
bool HaveActionWithId(EndpointId aEndpointId, uint16_t aActionId);
163+
bool HaveActionWithId(EndpointId aEndpointId, uint16_t aActionId, uint16_t & aActionIndex);
174164

175165
// TODO: We should move to non-global dirty marker.
176166
void MarkDirty(EndpointId aEndpointId, AttributeId aAttributeId)
@@ -227,26 +217,26 @@ class Delegate
227217

228218
/**
229219
* Check whether there is an action with the given actionId in the list of actions.
230-
* @param actionId The action ID to search for.
220+
* @param aActionId The action ID to search for.
221+
* @param aActionIndex A reference to the index at which an action with matching aActionId.
231222
* @return Returns a true if matching action is found otherwise false.
232223
*/
233-
virtual bool HaveActionWithId(uint16_t actionId) = 0;
224+
virtual bool HaveActionWithId(uint16_t aActionId, uint16_t & aActionIndex) = 0;
234225

235226
/**
236-
* On receipt of each and every command,
237-
* the server shall generate a either StateChanged or ActionFailed event.
238-
* If the command is not supproted by the action, Status::InvalidCommand shall be reported.
227+
* The implementations of the Handle* command callbacks below are expected to call OnStateChanged or
228+
* OnActionFailed as needed to generate the events required by the spec.
239229
*/
240230

241231
/**
242-
* @brief Delegate should implement a handler to InstantAction.
232+
* @brief Callback that will be called to handle an InstantAction command.
243233
* @param actionId The actionId of an action.
244234
* It should report Status::Success if successful and may report other Status codes if it fails.
245235
*/
246236
virtual Protocols::InteractionModel::Status HandleInstantAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
247237

248238
/**
249-
* @brief Delegate should implement a handler to InstantActionWithTransition.
239+
* @brief Callback that will be called to handle an InstantActionWithTransition command.
250240
* @param actionId The actionId of an action.
251241
* @param transitionTime The time for transition from the current state to the new state.
252242
* It should report Status::Success if successful and may report other Status codes if it fails.
@@ -255,14 +245,14 @@ class Delegate
255245
Optional<uint32_t> invokeId) = 0;
256246

257247
/**
258-
* @brief Delegate should implement a handler to StartAction.
248+
* @brief Callback that will be called to handle a StartAction command.
259249
* @param actionId The actionId of an action.
260250
* It should report Status::Success if successful and may report other Status codes if it fails.
261251
*/
262252
virtual Protocols::InteractionModel::Status HandleStartAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
263253

264254
/**
265-
* @brief Delegate should implement a handler to StartActionWithDuration.
255+
* @brief Callback that will be called to handle a StartActionWithDuration command.
266256
* @param actionId The actionId of an action.
267257
* @param duration The time for which an action shall be in start state.
268258
* It should report Status::Success if successful and may report other Status codes if it fails.
@@ -271,21 +261,21 @@ class Delegate
271261
Optional<uint32_t> invokeId) = 0;
272262

273263
/**
274-
* @brief Delegate should implement a handler to StopAction.
264+
* @brief Callback that will be called to handle a StopAction command.
275265
* @param actionId The actionId of an action.
276266
* It should report Status::Success if successful and may report other Status codes if it fails.
277267
*/
278268
virtual Protocols::InteractionModel::Status HandleStopAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
279269

280270
/**
281-
* @brief Delegate should implement a handler to PauseAction.
271+
* @brief Callback that will be called to handle a PauseAction command.
282272
* @param actionId The actionId of an action.
283273
* It should report Status::Success if successful and may report other Status codes if it fails.
284274
*/
285275
virtual Protocols::InteractionModel::Status HandlePauseAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
286276

287277
/**
288-
* @brief Delegate should implement a handler to PauseActionWithDuration.
278+
* @brief Callback that will be called to handle a PauseActionWithDuration command.
289279
* @param actionId The actionId of an action.
290280
* @param duration The time for which an action shall be in pause state.
291281
* It should report Status::Success if successful and may report other Status codes if it fails.
@@ -294,21 +284,21 @@ class Delegate
294284
Optional<uint32_t> invokeId) = 0;
295285

296286
/**
297-
* @brief Delegate should implement a handler to ResumeAction.
287+
* @brief Callback that will be called to handle a ResumeAction command.
298288
* @param actionId The actionId of an action.
299289
* It should report Status::Success if successful and may report other Status codes if it fails.
300290
*/
301291
virtual Protocols::InteractionModel::Status HandleResumeAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
302292

303293
/**
304-
* @brief Delegate should implement a handler to EnableAction.
294+
* @brief Callback that will be called to handle an EnableAction command.
305295
* @param actionId The actionId of an action.
306296
* It should report Status::Success if successful and may report other Status codes if it fails.
307297
*/
308298
virtual Protocols::InteractionModel::Status HandleEnableAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
309299

310300
/**
311-
* @brief Delegate should implement a handler to EnableActionWithDuration.
301+
* @brief Callback that will be called to handle an EnableActionWithDuration command.
312302
* @param actionId The actionId of an action.
313303
* @param duration The time for which an action shall be in active state.
314304
* It should report Status::Success if successful and may report other Status codes if it fails.
@@ -317,14 +307,14 @@ class Delegate
317307
Optional<uint32_t> invokeId) = 0;
318308

319309
/**
320-
* @brief Delegate should implement a handler to DisableAction.
310+
* @brief Callback that will be called to handle a DisableAction command.
321311
* @param actionId The actionId of an action.
322312
* It should report Status::Success if successful and may report other Status codes if it fails.
323313
*/
324314
virtual Protocols::InteractionModel::Status HandleDisableAction(uint16_t actionId, Optional<uint32_t> invokeId) = 0;
325315

326316
/**
327-
* @brief Delegate should implement a handler to DisableActionWithDuration.
317+
* @brief Callback that will be called to handle a DisableActionWithDuration command.
328318
* @param actionId The actionId of an action.
329319
* @param duration The time for which an action shall be in disable state.
330320
* It should report Status::Success if successful and may report other Status codes if it fails.

src/app/tests/TestActionsCluster.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ class TestActionsDelegateImpl : public Clusters::Actions::Delegate
8787
return CHIP_NO_ERROR;
8888
}
8989

90-
bool HaveActionWithId(uint16_t actionId) override
90+
bool HaveActionWithId(uint16_t actionId, uint16_t & aActionIndex) override
9191
{
9292
for (uint16_t i = 0; i < mNumActions; i++)
9393
{
9494
if (mActions[i].actionID == actionId)
9595
{
96+
aActionIndex = i;
9697
return true;
9798
}
9899
}

0 commit comments

Comments
 (0)