Skip to content

Commit

Permalink
Power source server: Use ember function for array indexing (#29892)
Browse files Browse the repository at this point in the history
* Use ember function for array indexing

Also fix warning when there are no power servers and no dynamic eps.

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 5, 2024
1 parent 55b1ce4 commit 4778383
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
42 changes: 9 additions & 33 deletions src/app/clusters/power-source-server/power-source-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <app-common/zap-generated/cluster-objects.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -171,8 +169,8 @@ CHIP_ERROR PowerSourceServer::SetEndpointList(EndpointId powerSourceClusterEndpo
}
const Span<EndpointId> * PowerSourceServer::GetEndpointList(EndpointId powerSourceClusterEndpoint) const
{
size_t idx = PowerSourceClusterEndpointIndex(powerSourceClusterEndpoint);
if (idx != std::numeric_limits<size_t>::max())
uint16_t idx = emberAfGetClusterServerEndpointIndex(powerSourceClusterEndpoint, Clusters::PowerSource::Id, kNumStaticEndpoints);
if (idx != kEmberInvalidEndpointIndex && sPowerSourceClusterInfo[idx].mEndpointList.size() > 0)
{
return &sPowerSourceClusterInfo[idx].mEndpointList;
}
Expand All @@ -181,41 +179,19 @@ const Span<EndpointId> * 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
{
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<size_t>::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<size_t>::max();
}

} // namespace Clusters
} // namespace app
} // namespace chip
5 changes: 0 additions & 5 deletions src/app/clusters/power-source-server/power-source-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ class PowerSourceServer
const Span<EndpointId> * GetEndpointList(EndpointId powerSourceClusterEndpoint) const;
void Shutdown();
size_t GetNumSupportedEndpointLists() const;

private:
// Both return std::numeric_limits<size_t>::max() for not found
size_t PowerSourceClusterEndpointIndex(EndpointId endpointId) const;
size_t NextEmptyIndex() const;
};

class PowerSourceAttrAccess : public AttributeAccessInterface
Expand Down
18 changes: 17 additions & 1 deletion src/app/tests/TestPowerSourceCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "lib/support/CHIPMem.h"
#include <app-common/zap-generated/cluster-objects.h>
#include <app/clusters/power-source-server/power-source-server.h>
#include <app/util/af.h>
#include <lib/core/ErrorStr.h>
#include <lib/core/TLV.h>
#include <lib/core/TLVDebug.h>
Expand All @@ -34,6 +35,21 @@

#include <vector>

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 {

Expand Down Expand Up @@ -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<EndpointId>(powerSourceServer.GetNumSupportedEndpointLists());
numEndpoints = static_cast<EndpointId>(powerSourceServer.GetNumSupportedEndpointLists());

// Endpoint 0 - list of 5
err = powerSourceServer.SetEndpointList(0, Span<EndpointId>(list0));
Expand Down

0 comments on commit 4778383

Please sign in to comment.