diff --git a/src/app/clusters/power-source-server/power-source-server.cpp b/src/app/clusters/power-source-server/power-source-server.cpp index b09e25d7ad75b5..3baf2df84949f4 100644 --- a/src/app/clusters/power-source-server/power-source-server.cpp +++ b/src/app/clusters/power-source-server/power-source-server.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -71,9 +72,11 @@ PowerSourceServer gPowerSourceServer; PowerSourceAttrAccess gAttrAccess; #ifdef ZCL_USING_POWER_SOURCE_CLUSTER_SERVER +static constexpr uint16_t kNumStaticEndpoints = EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT; #define POWER_SERVER_NUM_SUPPORTED_ENDPOINTS \ (EMBER_AF_POWER_SOURCE_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT) #else +static constexpr uint16_t kNumStaticEndpoints = 0; #define POWER_SERVER_NUM_SUPPORTED_ENDPOINTS CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT #endif static constexpr size_t kNumSupportedEndpoints = POWER_SERVER_NUM_SUPPORTED_ENDPOINTS; @@ -146,13 +149,8 @@ CHIP_ERROR PowerSourceServer::SetEndpointList(EndpointId powerSourceClusterEndpo { // TODO: should check here that the power source cluster exists on the endpoint, but for now let's take the caller's word // for it - - size_t idx = PowerSourceClusterEndpointIndex(powerSourceClusterEndpoint); - if (idx >= kNumSupportedEndpoints) - { - idx = NextEmptyIndex(); - } - if (idx >= kNumSupportedEndpoints) + uint16_t idx = emberAfGetClusterServerEndpointIndex(powerSourceClusterEndpoint, Clusters::PowerSource::Id, kNumStaticEndpoints); + if (idx == kEmberInvalidEndpointIndex) { return CHIP_ERROR_NO_MEMORY; } @@ -171,8 +169,8 @@ CHIP_ERROR PowerSourceServer::SetEndpointList(EndpointId powerSourceClusterEndpo } const Span * PowerSourceServer::GetEndpointList(EndpointId powerSourceClusterEndpoint) const { - size_t idx = PowerSourceClusterEndpointIndex(powerSourceClusterEndpoint); - if (idx != std::numeric_limits::max()) + uint16_t idx = emberAfGetClusterServerEndpointIndex(powerSourceClusterEndpoint, Clusters::PowerSource::Id, kNumStaticEndpoints); + if (idx != kEmberInvalidEndpointIndex && sPowerSourceClusterInfo[idx].mEndpointList.size() > 0) { return &sPowerSourceClusterInfo[idx].mEndpointList; } @@ -181,10 +179,12 @@ const Span * PowerSourceServer::GetEndpointList(EndpointId powerSour void PowerSourceServer::Shutdown() { +#if POWER_SERVER_NUM_SUPPORTED_ENDPOINTS > 0 for (size_t i = 0; i < kNumSupportedEndpoints; ++i) { sPowerSourceClusterInfo[i].Clear(); } +#endif } size_t PowerSourceServer::GetNumSupportedEndpointLists() const @@ -192,30 +192,6 @@ size_t PowerSourceServer::GetNumSupportedEndpointLists() const return kNumSupportedEndpoints; } -size_t PowerSourceServer::PowerSourceClusterEndpointIndex(EndpointId endpointId) const -{ - for (size_t i = 0; i < kNumSupportedEndpoints; ++i) - { - if (sPowerSourceClusterInfo[i].mClusterEndpoint == endpointId) - { - return i; - } - } - return std::numeric_limits::max(); -} - -size_t PowerSourceServer::NextEmptyIndex() const -{ - for (size_t i = 0; i < kNumSupportedEndpoints; ++i) - { - if (sPowerSourceClusterInfo[i].mClusterEndpoint == kInvalidEndpointId) - { - return i; - } - } - return std::numeric_limits::max(); -} - } // namespace Clusters } // namespace app } // namespace chip diff --git a/src/app/clusters/power-source-server/power-source-server.h b/src/app/clusters/power-source-server/power-source-server.h index a02f54cd5375c1..5199753e16692c 100644 --- a/src/app/clusters/power-source-server/power-source-server.h +++ b/src/app/clusters/power-source-server/power-source-server.h @@ -43,11 +43,6 @@ class PowerSourceServer const Span * GetEndpointList(EndpointId powerSourceClusterEndpoint) const; void Shutdown(); size_t GetNumSupportedEndpointLists() const; - -private: - // Both return std::numeric_limits::max() for not found - size_t PowerSourceClusterEndpointIndex(EndpointId endpointId) const; - size_t NextEmptyIndex() const; }; class PowerSourceAttrAccess : public AttributeAccessInterface diff --git a/src/app/tests/TestPowerSourceCluster.cpp b/src/app/tests/TestPowerSourceCluster.cpp index be2b65b3ebcab2..e9983368d57d65 100644 --- a/src/app/tests/TestPowerSourceCluster.cpp +++ b/src/app/tests/TestPowerSourceCluster.cpp @@ -19,6 +19,7 @@ #include "lib/support/CHIPMem.h" #include #include +#include #include #include #include @@ -34,6 +35,21 @@ #include +namespace { +chip::EndpointId numEndpoints = 0; +} +extern uint16_t emberAfGetClusterServerEndpointIndex(chip::EndpointId endpoint, chip::ClusterId cluster, + uint16_t fixedClusterServerEndpointCount) +{ + // Very simple mapping here, we're just going to return the endpoint that matches the given endpoint index because the test + // uses the endpoints in order. + if (endpoint >= numEndpoints) + { + return kEmberInvalidEndpointIndex; + } + return endpoint; +} + namespace chip { namespace app { @@ -144,7 +160,7 @@ void TestPowerSourceCluster::TestEndpointList(nlTestSuite * apSuite, void * apCo // we checked earlier that this fit // This test just uses endpoints in order, so we want to set endpoints from // 0 to numEndpoints - 1, and use this for overflow checking - EndpointId numEndpoints = static_cast(powerSourceServer.GetNumSupportedEndpointLists()); + numEndpoints = static_cast(powerSourceServer.GetNumSupportedEndpointLists()); // Endpoint 0 - list of 5 err = powerSourceServer.SetEndpointList(0, Span(list0));