From ba0be91448e4f7dc3190ab6ca7956e9787b5baee Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 22 Jan 2024 11:44:23 -0500 Subject: [PATCH] Reduce ember attribute-storage API surface: remove emberAfIsCluster* methods (#31562) * Remove emberAfIsCluster* methods * Another slight intent and scoping update, removed unused variable * Restyle * Fix return value * Restyle * Remove one extra bracket for readability * Simplify the code even more * Be explicit on types, to make sure no copy is done * Restyle --------- Co-authored-by: Andrei Litvin --- src/app/util/attribute-storage.cpp | 50 +++++++++++++----------------- src/app/util/attribute-storage.h | 3 -- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/app/util/attribute-storage.cpp b/src/app/util/attribute-storage.cpp index 94ff5920adca58..e810c7cfb32edf 100644 --- a/src/app/util/attribute-storage.cpp +++ b/src/app/util/attribute-storage.cpp @@ -726,8 +726,7 @@ const EmberAfCluster * emberAfFindClusterInType(const EmberAfEndpointType * endp { const EmberAfCluster * cluster = &(endpointType->cluster[i]); - if ((mask == 0 || (mask == CLUSTER_MASK_CLIENT && emberAfClusterIsClient(cluster)) || - (mask == CLUSTER_MASK_SERVER && emberAfClusterIsServer(cluster)))) + if (mask == 0 || ((cluster->mask & mask) != 0)) { if (cluster->clusterId == clusterId) { @@ -1038,20 +1037,10 @@ uint8_t emberAfClusterCountByIndex(uint16_t endpointIndex, bool server) uint8_t emberAfClusterCountForEndpointType(const EmberAfEndpointType * type, bool server) { - uint8_t c = 0; - for (uint8_t i = 0; i < type->clusterCount; i++) - { - auto * cluster = &(type->cluster[i]); - if (server && emberAfClusterIsServer(cluster)) - { - c++; - } - if ((!server) && emberAfClusterIsClient(cluster)) - { - c++; - } - } - return c; + const EmberAfClusterMask cluster_mask = server ? CLUSTER_MASK_SERVER : CLUSTER_MASK_CLIENT; + + return static_cast(std::count_if(type->cluster, type->cluster + type->clusterCount, + [=](const EmberAfCluster & cluster) { return (cluster.mask & cluster_mask) != 0; })); } uint8_t emberAfGetClusterCountForEndpoint(EndpointId endpoint) @@ -1121,28 +1110,31 @@ CHIP_ERROR SetTagList(EndpointId endpoint, SpanendpointType->clusterCount; i++) + const EmberAfEndpointType * endpointType = emAfEndpoints[index].endpointType; + const EmberAfClusterMask cluster_mask = server ? CLUSTER_MASK_SERVER : CLUSTER_MASK_CLIENT; + const uint8_t clusterCount = endpointType->clusterCount; + + uint8_t c = 0; + for (uint8_t i = 0; i < clusterCount; i++) { - cluster = &(de->endpointType->cluster[i]); + const EmberAfCluster * cluster = &(endpointType->cluster[i]); - if ((server && emberAfClusterIsServer(cluster)) || ((!server) && emberAfClusterIsClient(cluster))) + if ((cluster->mask & cluster_mask) == 0) { - if (c == n) - { - return cluster; - } - c++; + continue; } + + if (c == n) + { + return cluster; + } + + c++; } return nullptr; } diff --git a/src/app/util/attribute-storage.h b/src/app/util/attribute-storage.h index 5107bf66573f49..175af556933eac 100644 --- a/src/app/util/attribute-storage.h +++ b/src/app/util/attribute-storage.h @@ -82,9 +82,6 @@ extern uint8_t attributeData[]; // main storage bucket for all attributes void emAfCallInits(void); -#define emberAfClusterIsClient(cluster) ((bool) (((cluster)->mask & CLUSTER_MASK_CLIENT) != 0)) -#define emberAfClusterIsServer(cluster) ((bool) (((cluster)->mask & CLUSTER_MASK_SERVER) != 0)) - // Initial configuration void emberAfEndpointConfigure(void);