From c7ca2e6a6148a22564241774a96655da38c4cb4d Mon Sep 17 00:00:00 2001 From: lpbeliveau-silabs Date: Tue, 24 Oct 2023 14:19:02 -0400 Subject: [PATCH] f --- .../clusters/scenes-server/scenes-server.cpp | 49 ++++++++++++------- .../clusters/scenes-server/scenes-server.h | 4 +- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/app/clusters/scenes-server/scenes-server.cpp b/src/app/clusters/scenes-server/scenes-server.cpp index 229a5d9fa2a8fb..741952e210bc1c 100644 --- a/src/app/clusters/scenes-server/scenes-server.cpp +++ b/src/app/clusters/scenes-server/scenes-server.cpp @@ -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]; } @@ -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]; @@ -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; } @@ -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(sceneInfoStructIndex + 1); uint8_t moveNum = static_cast(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) { @@ -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) { @@ -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; }); diff --git a/src/app/clusters/scenes-server/scenes-server.h b/src/app/clusters/scenes-server/scenes-server.h index 5d69097f4af88d..7140cd20e2e348 100644 --- a/src/app/clusters/scenes-server/scenes-server.h +++ b/src/app/clusters/scenes-server/scenes-server.h @@ -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();