Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Scenes] MaxCapacity fix #31981

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/app/clusters/scenes-server/scenes-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ CHIP_ERROR AddResponseOnError(CommandHandlerInterface::HandlerContext & ctx, Res
{
resp.status = to_underlying(Protocols::InteractionModel::Status::NotFound);
}
else if (CHIP_ERROR_NO_MEMORY == err)
{
resp.status = to_underlying(Protocols::InteractionModel::Status::ResourceExhausted);
}
else
{
resp.status = to_underlying(StatusIB(err).mStatus);
Expand Down Expand Up @@ -167,7 +171,6 @@ CHIP_ERROR UpdateFabricSceneInfo(EndpointId endpoint, FabricIndex fabric, Option

/// @brief Gets the SceneInfoStruct array associated to an endpoint
/// @param endpoint target endpoint
/// @param fabric target fabric
/// @return Optional with no value not found, Span of SceneInfoStruct
Span<Structs::SceneInfoStruct::Type> ScenesServer::FabricSceneInfo::GetFabricSceneInfo(EndpointId endpoint)
{
Expand Down Expand Up @@ -674,13 +677,21 @@ void ScenesServer::InvokeCommand(HandlerContext & ctxt)
// AttributeAccessInterface
CHIP_ERROR ScenesServer::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
uint16_t endpointTableSize = 0;
ReturnErrorOnFailure(StatusIB(Attributes::SceneTableSize::Get(aPath.mEndpointId, &endpointTableSize)).ToChipError());

// Get Scene Table Instance
SceneTable * sceneTable = scenes::GetSceneTableImpl(aPath.mEndpointId, endpointTableSize);

switch (aPath.mAttributeId)
{
case Attributes::FabricSceneInfo::Id: {
return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
return aEncoder.EncodeList([&, sceneTable](const auto & encoder) -> CHIP_ERROR {
Span<Structs::SceneInfoStruct::Type> fabricSceneInfoSpan = mFabricSceneInfo.GetFabricSceneInfo(aPath.mEndpointId);
for (auto & info : fabricSceneInfoSpan)
{
// Update the SceneInfoStruct's Capacity in case it's capacity was limited by other fabrics
sceneTable->GetRemainingCapacity(info.fabricIndex, info.remainingCapacity);
ReturnErrorOnFailure(encoder.Encode(info));
}
return CHIP_NO_ERROR;
Expand Down Expand Up @@ -917,12 +928,10 @@ void ScenesServer::HandleStoreScene(HandlerContext & ctx, const Commands::StoreS
CHIP_ERROR err = StoreSceneParse(ctx.mCommandHandler.GetAccessingFabricIndex(), ctx.mRequestPath.mEndpointId, req.groupID,
req.sceneID, mGroupProvider);

if (CHIP_NO_ERROR == err)
{
ReturnOnFailure(UpdateLastConfiguredBy(ctx, response));
}
ReturnOnFailure(AddResponseOnError(ctx, response, err));

response.status = to_underlying(StatusIB(err).mStatus);
ReturnOnFailure(UpdateLastConfiguredBy(ctx, response));
response.status = to_underlying(Protocols::InteractionModel::Status::Success);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
}

Expand Down Expand Up @@ -1030,6 +1039,13 @@ void ScenesServer::HandleCopyScene(HandlerContext & ctx, const Commands::CopySce
ReturnOnFailure(AddResponseOnError(ctx, response,
sceneTable->GetRemainingCapacity(ctx.mCommandHandler.GetAccessingFabricIndex(), capacity)));

if (0 == capacity)
{
response.status = to_underlying(Protocols::InteractionModel::Status::ResourceExhausted);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
return;
}

// Checks if we copy a single scene or all of them
if (req.mode.GetField(app::Clusters::ScenesManagement::CopyModeBitmap::kCopyAllScenes))
{
Expand All @@ -1042,13 +1058,6 @@ void ScenesServer::HandleCopyScene(HandlerContext & ctx, const Commands::CopySce
ctx, response,
sceneTable->GetAllSceneIdsInGroup(ctx.mCommandHandler.GetAccessingFabricIndex(), req.groupIdentifierFrom, sceneList)));

if (0 == capacity)
{
response.status = to_underlying(Protocols::InteractionModel::Status::ResourceExhausted);
ctx.mCommandHandler.AddResponse(ctx.mRequestPath, response);
return;
}

for (auto & sceneId : sceneList)
{
SceneTableEntry scene(SceneStorageId(sceneId, req.groupIdentifierFrom));
Expand All @@ -1061,13 +1070,13 @@ void ScenesServer::HandleCopyScene(HandlerContext & ctx, const Commands::CopySce

ReturnOnFailure(AddResponseOnError(
ctx, response, sceneTable->SetSceneTableEntry(ctx.mCommandHandler.GetAccessingFabricIndex(), scene)));
}

// Update SceneInfoStruct Attributes
ReturnOnFailure(
AddResponseOnError(ctx, response,
UpdateFabricSceneInfo(ctx.mRequestPath.mEndpointId, ctx.mCommandHandler.GetAccessingFabricIndex(),
Optional<GroupId>(), Optional<SceneId>(), Optional<bool>())));
// Update SceneInfoStruct Attributes after each insert in case we hit max capacity in the middle of the loop
ReturnOnFailure(AddResponseOnError(
ctx, response,
UpdateFabricSceneInfo(ctx.mRequestPath.mEndpointId, ctx.mCommandHandler.GetAccessingFabricIndex(),
Optional<GroupId>(), Optional<SceneId>(), Optional<bool>() /* = sceneValid*/)));
}

ReturnOnFailure(UpdateLastConfiguredBy(ctx, response));

Expand Down
Loading
Loading