Skip to content

Commit

Permalink
Restructured the storage structs, command handler and removed vector …
Browse files Browse the repository at this point in the history
…usage.
  • Loading branch information
jadhavrohit924 committed Dec 24, 2024
1 parent 2f165c5 commit ad82166
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 73 deletions.
1 change: 0 additions & 1 deletion scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
'src/app/clusters/application-launcher-server/application-launcher-server.cpp': {'string'},
'src/app/clusters/application-launcher-server/application-launcher-delegate.h': {'list'},
'src/app/clusters/audio-output-server/audio-output-delegate.h': {'list'},
'src/app/clusters/actions-server/actions-server.h': {'vector'},
# EcosystemInformationCluster is for Fabric Sync and is intended to run on device that are capable of handling these types.
'src/app/clusters/ecosystem-information-server/ecosystem-information-server.h': {'map', 'string', 'vector'},
'src/app/clusters/channel-server/channel-delegate.h': {'list'},
Expand Down
78 changes: 25 additions & 53 deletions src/app/clusters/actions-server/actions-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,119 +204,91 @@ void ActionsServer::HandleCommand(HandlerContext & handlerContext, FuncT func)

void ActionsServer::HandleInstantAction(HandlerContext & ctx, const Commands::InstantAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleInstantAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleInstantAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleInstantActionWithTransition(HandlerContext & ctx,
const Commands::InstantActionWithTransition::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
uint16_t transitionTime = commandData.transitionTime;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleInstantActionWithTransition(actionId, transitionTime, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status =
delegate->HandleInstantActionWithTransition(commandData.actionID, commandData.transitionTime, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleStartAction(HandlerContext & ctx, const Commands::StartAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleStartAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleStartAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleStartActionWithDuration(HandlerContext & ctx,
const Commands::StartActionWithDuration::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
uint32_t duration = commandData.duration;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleStartActionWithDuration(actionId, duration, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleStartActionWithDuration(commandData.actionID, commandData.duration, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleStopAction(HandlerContext & ctx, const Commands::StopAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleStopAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleStopAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandlePauseAction(HandlerContext & ctx, const Commands::PauseAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandlePauseAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandlePauseAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandlePauseActionWithDuration(HandlerContext & ctx,
const Commands::PauseActionWithDuration::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
uint32_t duration = commandData.duration;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandlePauseActionWithDuration(actionId, duration, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandlePauseActionWithDuration(commandData.actionID, commandData.duration, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleResumeAction(HandlerContext & ctx, const Commands::ResumeAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleResumeAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleResumeAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleEnableAction(HandlerContext & ctx, const Commands::EnableAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleEnableAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleEnableAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleEnableActionWithDuration(HandlerContext & ctx,
const Commands::EnableActionWithDuration::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
uint32_t duration = commandData.duration;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleEnableActionWithDuration(actionId, duration, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleEnableActionWithDuration(commandData.actionID, commandData.duration, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleDisableAction(HandlerContext & ctx, const Commands::DisableAction::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleDisableAction(actionId, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleDisableAction(commandData.actionID, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

void ActionsServer::HandleDisableActionWithDuration(HandlerContext & ctx,
const Commands::DisableActionWithDuration::DecodableType & commandData)
{
uint16_t actionId = commandData.actionID;
Optional<uint32_t> invokeId = commandData.invokeID;
uint32_t duration = commandData.duration;
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleDisableActionWithDuration(actionId, duration, invokeId);
Delegate * delegate = GetDelegate(ctx.mRequestPath.mEndpointId);
Status status = delegate->HandleDisableActionWithDuration(commandData.actionID, commandData.duration, commandData.invokeID);
ctx.mCommandHandler.AddStatus(ctx.mRequestPath, status);
}

Expand Down
39 changes: 20 additions & 19 deletions src/app/clusters/actions-server/actions-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,32 @@
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandlerInterface.h>
#include <protocols/interaction_model/StatusCode.h>
#include <vector>

namespace chip {
namespace app {
namespace Clusters {
namespace Actions {

static constexpr size_t kActionNameMaxSize = 128u;
static constexpr size_t kEndpointListNameMaxSize = 128u;
static constexpr size_t kEndpointListMaxSize = 256u;
// Last byte is reserved of '\0' terminator.
static constexpr size_t kActionNameMaxSize = 127u;
static constexpr size_t kEndpointListNameMaxSize = 127u;

static constexpr size_t kEndpointListMaxSize = 256u;

class Delegate;

struct ActionStructStorage : public Structs::ActionStruct::Type
{
ActionStructStorage() : mActionName(mBuffer){};
ActionStructStorage() : mActionName(mBuffer, sizeof(mBuffer)){};

ActionStructStorage(uint16_t action, const CharSpan & actionName, ActionTypeEnum actionType, uint16_t epListID,
BitMask<CommandBits> commands, ActionStateEnum actionState) :
mActionName(mBuffer)
mActionName(mBuffer, sizeof(mBuffer))
{
Set(action, actionName, actionType, epListID, commands, actionState);
}

ActionStructStorage(const ActionStructStorage & action) : mActionName(mBuffer) { *this = action; }
ActionStructStorage(const ActionStructStorage & action) : mActionName(mBuffer, sizeof(mBuffer)) { *this = action; }

ActionStructStorage & operator=(const ActionStructStorage & action)
{
Expand All @@ -72,16 +73,16 @@ struct ActionStructStorage : public Structs::ActionStruct::Type

struct EndpointListStorage : public Structs::EndpointListStruct::Type
{
EndpointListStorage() : mEpListName(mBuffer){};
EndpointListStorage() : mEpListName(mBuffer, sizeof(mBuffer)){};

EndpointListStorage(uint16_t epListId, const CharSpan & epListName, EndpointListTypeEnum epListType,
const DataModel::List<const EndpointId> & endpointList) :
mEpListName(mBuffer)
mEpListName(mBuffer, sizeof(mBuffer))
{
Set(epListId, epListName, epListType, endpointList);
}

EndpointListStorage(const EndpointListStorage & epList) : mEpListName(mBuffer) { *this = epList; }
EndpointListStorage(const EndpointListStorage & epList) : mEpListName(mBuffer, sizeof(mBuffer)) { *this = epList; }

EndpointListStorage & operator=(const EndpointListStorage & epList)
{
Expand All @@ -92,24 +93,23 @@ struct EndpointListStorage : public Structs::EndpointListStruct::Type
void Set(uint16_t epListId, const CharSpan & epListName, EndpointListTypeEnum epListType,
const DataModel::List<const EndpointId> & endpointList)
{
endpointListID = epListId;
type = epListType;
endpointListID = epListId;
type = epListType;
size_t epListSize = std::min(endpointList.size(), kEndpointListMaxSize);

for (uint8_t index = 0; index < std::min(endpointList.size(), kEndpointListMaxSize); index++)
for (uint8_t index = 0; index < epListSize && index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; index++)
{
mEpList.push_back(endpointList[index]);
mEpList[index] = endpointList[index];
}
mEpListSpan = Span(mEpList.data(), mEpList.size());
endpoints = DataModel::List<const EndpointId>(mEpListSpan);
endpoints = DataModel::List<const EndpointId>(Span(mEpList, epListSize));
CopyCharSpanToMutableCharSpanWithTruncation(epListName, mEpListName);
name = mEpListName;
}

private:
char mBuffer[kEndpointListNameMaxSize];
MutableCharSpan mEpListName;
std::vector<EndpointId> mEpList;
Span<const EndpointId> mEpListSpan;
EndpointId mEpList[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT];
};

class ActionsServer : public AttributeAccessInterface, public CommandHandlerInterface
Expand Down Expand Up @@ -150,7 +150,8 @@ class ActionsServer : public AttributeAccessInterface, public CommandHandlerInte
const AttributeValueEncoder::ListEncodeHelper & encoder);
bool FindActionIdInActionList(EndpointId endpointId, uint16_t actionId);

// CommandHandlerInterface
// Cannot use CommandHandlerInterface::HandleCommand directly because we need to do the FindActionIdInActionList() check before
// sending a command.
template <typename RequestT, typename FuncT>
void HandleCommand(HandlerContext & handlerContext, FuncT func);

Expand Down

0 comments on commit ad82166

Please sign in to comment.