Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
lpbeliveau-silabs committed Oct 24, 2023
1 parent 2bf5ae8 commit c7ca2e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
49 changes: 30 additions & 19 deletions src/app/clusters/scenes-server/scenes-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Structs::SceneInfoStruct::Type * ScenesServer::FabricSceneInfo::GetSceneInfoStru
size_t endpointIndex = 0;
VerifyOrReturnValue(CHIP_NO_ERROR == FindFabricSceneInfoIndex(endpoint, endpointIndex), nullptr);
uint8_t sceneInfoStructIndex = 0;
VerifyOrReturnValue(CHIP_NO_ERROR == FindSceneInfoStructIndex(endpoint, fabric, sceneInfoStructIndex), nullptr);
VerifyOrReturnValue(CHIP_NO_ERROR == FindSceneInfoStructIndex(fabric, endpointIndex, sceneInfoStructIndex), nullptr);

return &mSceneInfoStructs[endpoint][sceneInfoStructIndex];
}
Expand All @@ -151,7 +151,7 @@ CHIP_ERROR ScenesServer::FabricSceneInfo::SetSceneInfoStruct(EndpointId endpoint
size_t endpointIndex = 0;
ReturnErrorOnFailure(FindFabricSceneInfoIndex(endpoint, endpointIndex));
uint8_t sceneInfoStructIndex = 0;
if (CHIP_ERROR_NOT_FOUND == FindSceneInfoStructIndex(endpoint, fabric, sceneInfoStructIndex))
if (CHIP_ERROR_NOT_FOUND == FindSceneInfoStructIndex(fabric, endpointIndex, sceneInfoStructIndex))
{
VerifyOrReturnError(mSceneInfoStructsCount[endpointIndex] < kScenesServerMaxFabricCount, CHIP_ERROR_NO_MEMORY);
sceneInfoStructIndex = mSceneInfoStructsCount[endpointIndex];
Expand All @@ -160,7 +160,12 @@ CHIP_ERROR ScenesServer::FabricSceneInfo::SetSceneInfoStruct(EndpointId endpoint
mSceneInfoStructsCount[endpointIndex]++;
}

mSceneInfoStructs[endpointIndex][sceneInfoStructIndex] = sceneInfoStruct;
// TODO : Try mSceneInfoStructs[endpointIndex][sceneInfoStructIndex] = sceneInfoStruct; once sanity check passes
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].fabricIndex = sceneInfoStruct.fabricIndex;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].sceneCount = sceneInfoStruct.sceneCount;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].currentScene = sceneInfoStruct.currentScene;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].currentGroup = sceneInfoStruct.currentGroup;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].remainingCapacity = sceneInfoStruct.remainingCapacity;

return CHIP_NO_ERROR;
}
Expand All @@ -173,31 +178,32 @@ void ScenesServer::FabricSceneInfo::ClearSceneInfoStruct(EndpointId endpoint, Fa
size_t endpointIndex = 0;
ReturnOnFailure(FindFabricSceneInfoIndex(endpoint, endpointIndex));
uint8_t sceneInfoStructIndex = 0;
ReturnOnFailure(FindSceneInfoStructIndex(endpoint, fabric, sceneInfoStructIndex));
ReturnOnFailure(FindSceneInfoStructIndex(fabric, endpointIndex, sceneInfoStructIndex));

uint8_t nextIndex = static_cast<uint8_t>(sceneInfoStructIndex + 1);
uint8_t moveNum = static_cast<uint8_t>(kScenesServerMaxFabricCount - nextIndex);
// Compress the endpoint's SceneInfoStruct array
if (moveNum)
{
memmove(&mSceneInfoStructs[endpoint][sceneInfoStructIndex], &mSceneInfoStructs[endpoint][nextIndex],
memmove(&mSceneInfoStructs[endpointIndex][sceneInfoStructIndex], &mSceneInfoStructs[endpointIndex][nextIndex],
sizeof(Structs::SceneInfoStruct::Type) * moveNum);
}

// Decrement the SceneInfoStruct count
mSceneInfoStructsCount[endpoint]--;
mSceneInfoStructsCount[endpointIndex]--;

// Clear the last populated SceneInfoStruct
mSceneInfoStructs[endpoint][mSceneInfoStructsCount[endpoint]].fabricIndex = 0;
mSceneInfoStructs[endpoint][mSceneInfoStructsCount[endpoint]].sceneCount = 0;
mSceneInfoStructs[endpoint][mSceneInfoStructsCount[endpoint]].currentScene = 0;
mSceneInfoStructs[endpoint][mSceneInfoStructsCount[endpoint]].currentGroup = 0;
mSceneInfoStructs[endpoint][mSceneInfoStructsCount[endpoint]].remainingCapacity = 0;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].fabricIndex = 0;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].sceneCount = 0;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].currentScene = 0;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].currentGroup = 0;
mSceneInfoStructs[endpointIndex][mSceneInfoStructsCount[endpointIndex]].remainingCapacity = 0;
}

/// @brief Returns the index of the FabricSceneInfo associated to an endpoint
/// @param[in] endpoint target endpoint
/// @param[out] endpointIndex index of the corresponding FabricSceneInfo inf found, kEmberInvalidEndpointIndex otherwise
/// @param[out] endpointIndex index of the corresponding FabricSceneInfo for an endpoint, corresponds to a row in the
/// mSceneInfoStructs array,
/// @return CHIP_NO_ERROR or CHIP_ERROR_NOT_FOUND, CHIP_ERROR_INVALID_ARGUMENT if invalid endpoint
CHIP_ERROR ScenesServer::FabricSceneInfo::FindFabricSceneInfoIndex(EndpointId endpoint, size_t & endpointIndex)
{
Expand All @@ -215,16 +221,18 @@ CHIP_ERROR ScenesServer::FabricSceneInfo::FindFabricSceneInfoIndex(EndpointId en

/// @brief Returns the SceneInfoStruct () associated to a fabric
/// @param[in] fabric target fabric
/// @param[out] index index of the corresponding SceneInfoStruct inf found, mFabricSceneInfo[endpoint] size otherwise
/// @param[in] endpointIndex index of the corresponding FabricSceneInfo for an endpoint, corresponds to a row in the
/// mSceneInfoStructs array
/// @param[out] index index of the corresponding SceneInfoStruct if found, mFabricSceneInfo[endpoint] number of fabrics otherwise
/// @return CHIP_NO_ERROR or CHIP_ERROR_NOT_FOUND, CHIP_ERROR_INVALID_ARGUMENT if invalid fabric or endpoint
CHIP_ERROR ScenesServer::FabricSceneInfo::FindSceneInfoStructIndex(EndpointId endpoint, FabricIndex fabric, uint8_t & index)
CHIP_ERROR ScenesServer::FabricSceneInfo::FindSceneInfoStructIndex(FabricIndex fabric, size_t endpointIndex, uint8_t & index)
{
VerifyOrReturnError(kInvalidEndpointId != endpoint, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(endpointIndex < kScenesServerMaxEndpointCount, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(kUndefinedFabricIndex != fabric, CHIP_ERROR_INVALID_ARGUMENT);

index = 0;

for (auto & info : mSceneInfoStructs[endpoint])
for (auto & info : mSceneInfoStructs[endpointIndex])
{
if (info.fabricIndex == fabric)
{
Expand Down Expand Up @@ -738,12 +746,15 @@ CHIP_ERROR ScenesServer::Read(const ConcreteReadAttributePath & aPath, Attribute
switch (aPath.mAttributeId)
{
case Attributes::FabricSceneInfo::Id: {
Structs::SceneInfoStruct::Type * SceneInfo = mFabricSceneInfo.GetFabricSceneInfo(aPath.mEndpointId);
return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR {
for (auto & info : Server::GetInstance().GetFabricTable())
{
auto fabric = info.GetFabricIndex();
// TODO: Figure out most efficient way to encode things here
auto fabric = info.GetFabricIndex();
Structs::SceneInfoStruct::Type * SceneInfo = GetSceneInfoStruct(aPath.mEndpointId, fabric);
if (nullptr != SceneInfo)
{
ReturnErrorOnFailure(encoder.Encode(*SceneInfo));
}
}
return CHIP_NO_ERROR;
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/clusters/scenes-server/scenes-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class ScenesServer : public CommandHandlerInterface, public AttributeAccessInter

private:
CHIP_ERROR FindFabricSceneInfoIndex(EndpointId endpoint, size_t & endpointIndex);
CHIP_ERROR FindSceneInfoStructIndex(EndpointId endpoint, FabricIndex fabric, uint8_t & index);
CHIP_ERROR FindSceneInfoStructIndex(FabricIndex fabric, size_t endpointIndex, uint8_t & index);

Structs::SceneInfoStruct::Type mSceneInfoStructs[kScenesServerMaxEndpointCount][kScenesServerMaxFabricCount];
uint8_t mSceneInfoStructsCount[kScenesServerMaxEndpointCount];
uint8_t mSceneInfoStructsCount[kScenesServerMaxEndpointCount] = { 0 };
};

static ScenesServer & Instance();
Expand Down

0 comments on commit c7ca2e6

Please sign in to comment.