Skip to content

Commit

Permalink
Scenes-cluster: move non-template things into non-template functions …
Browse files Browse the repository at this point in the history
…to save flash (#36288)

* Move non-template things into non-template functions

* Add back comment referencing bug

* Add a comment regarding status ... I did not update the logic but it does seem wrong

---------

Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
andy31415 and andreilitvin authored Oct 30, 2024
1 parent e57dedf commit 79a3224
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/app/clusters/scenes-server/scenes-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ namespace ScenesManagement {

namespace {

Protocols::InteractionModel::Status ResponseStatus(CHIP_ERROR err)
{
// TODO : Properly fix mapping between error types (issue https://github.com/project-chip/connectedhomeip/issues/26885)
if (CHIP_ERROR_NOT_FOUND == err)
{
return Protocols::InteractionModel::Status::NotFound;
}
if (CHIP_ERROR_NO_MEMORY == err)
{
return Protocols::InteractionModel::Status::ResourceExhausted;
}
if (CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute) == err)
{
// TODO: Confirm if we need to add UnsupportedAttribute status as a return for Scene Commands
return Protocols::InteractionModel::Status::InvalidCommand;
}
return StatusIB(err).mStatus;
}

/// @brief Generate and add a response to a command handler context if err parameter is not CHIP_NO_ERROR
/// @tparam ResponseType Type of response, depends on the command
/// @param ctx Command Handler context where to add reponse
Expand All @@ -65,24 +84,7 @@ CHIP_ERROR AddResponseOnError(CommandHandlerInterface::HandlerContext & ctx, Res
{
if (CHIP_NO_ERROR != err)
{
// TODO : Properly fix mapping between error types (issue https://github.com/project-chip/connectedhomeip/issues/26885)
if (CHIP_ERROR_NOT_FOUND == err)
{
resp.status = to_underlying(Protocols::InteractionModel::Status::NotFound);
}
else if (CHIP_ERROR_NO_MEMORY == err)
{
resp.status = to_underlying(Protocols::InteractionModel::Status::ResourceExhausted);
}
else if (CHIP_IM_GLOBAL_STATUS(UnsupportedAttribute) == err)
{
// TODO: Confirm if we need to add UnsupportedAttribute status as a return for Scene Commands
resp.status = to_underlying(Protocols::InteractionModel::Status::InvalidCommand);
}
else
{
resp.status = to_underlying(StatusIB(err).mStatus);
}
resp.status = to_underlying(ResponseStatus(err));
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, resp);
}
return err;
Expand All @@ -97,24 +99,28 @@ CHIP_ERROR AddResponseOnError(CommandHandlerInterface::HandlerContext & ctx, Res
template <typename ResponseType>
CHIP_ERROR AddResponseOnError(CommandHandlerInterface::HandlerContext & ctx, ResponseType & resp, Status status)
{
// TODO: this seems odd: we convert `status` to a CHIP_ERROR and then back to status. This seems
// potentially lossy and not ideal.
return AddResponseOnError(ctx, resp, StatusIB(status).ToChipError());
}

template <typename ResponseType>
CHIP_ERROR UpdateLastConfiguredBy(HandlerContext & ctx, ResponseType resp)
Status SetLastConfiguredBy(HandlerContext & ctx)
{
Access::SubjectDescriptor descriptor = ctx.mCommandHandler.GetSubjectDescriptor();
Status status = Status::Success;
const Access::SubjectDescriptor descriptor = ctx.mCommandHandler.GetSubjectDescriptor();

if (AuthMode::kCase == descriptor.authMode)
{
status = Attributes::LastConfiguredBy::Set(ctx.mRequestPath.mEndpointId, descriptor.subject);
}
else
{
status = Attributes::LastConfiguredBy::SetNull(ctx.mRequestPath.mEndpointId);
return Attributes::LastConfiguredBy::Set(ctx.mRequestPath.mEndpointId, descriptor.subject);
}

return Attributes::LastConfiguredBy::SetNull(ctx.mRequestPath.mEndpointId);
}

template <typename ResponseType>
CHIP_ERROR UpdateLastConfiguredBy(HandlerContext & ctx, ResponseType resp)
{
Status status = SetLastConfiguredBy(ctx);

// LastConfiguredBy is optional, so we don't want to fail the command if it fails to update
VerifyOrReturnValue(!(Status::Success == status || Status::UnsupportedAttribute == status), CHIP_NO_ERROR);
return AddResponseOnError(ctx, resp, status);
Expand Down

0 comments on commit 79a3224

Please sign in to comment.