Skip to content

Commit 98bd3e2

Browse files
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 <andreilitvin@google.com>
1 parent 9238d4b commit 98bd3e2

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/app/util/attribute-storage.cpp

+21-29
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,7 @@ const EmberAfCluster * emberAfFindClusterInType(const EmberAfEndpointType * endp
726726
{
727727
const EmberAfCluster * cluster = &(endpointType->cluster[i]);
728728

729-
if ((mask == 0 || (mask == CLUSTER_MASK_CLIENT && emberAfClusterIsClient(cluster)) ||
730-
(mask == CLUSTER_MASK_SERVER && emberAfClusterIsServer(cluster))))
729+
if (mask == 0 || ((cluster->mask & mask) != 0))
731730
{
732731
if (cluster->clusterId == clusterId)
733732
{
@@ -1038,20 +1037,10 @@ uint8_t emberAfClusterCountByIndex(uint16_t endpointIndex, bool server)
10381037

10391038
uint8_t emberAfClusterCountForEndpointType(const EmberAfEndpointType * type, bool server)
10401039
{
1041-
uint8_t c = 0;
1042-
for (uint8_t i = 0; i < type->clusterCount; i++)
1043-
{
1044-
auto * cluster = &(type->cluster[i]);
1045-
if (server && emberAfClusterIsServer(cluster))
1046-
{
1047-
c++;
1048-
}
1049-
if ((!server) && emberAfClusterIsClient(cluster))
1050-
{
1051-
c++;
1052-
}
1053-
}
1054-
return c;
1040+
const EmberAfClusterMask cluster_mask = server ? CLUSTER_MASK_SERVER : CLUSTER_MASK_CLIENT;
1041+
1042+
return static_cast<uint8_t>(std::count_if(type->cluster, type->cluster + type->clusterCount,
1043+
[=](const EmberAfCluster & cluster) { return (cluster.mask & cluster_mask) != 0; }));
10551044
}
10561045

10571046
uint8_t emberAfGetClusterCountForEndpoint(EndpointId endpoint)
@@ -1121,28 +1110,31 @@ CHIP_ERROR SetTagList(EndpointId endpoint, Span<const Clusters::Descriptor::Stru
11211110
const EmberAfCluster * emberAfGetNthCluster(EndpointId endpoint, uint8_t n, bool server)
11221111
{
11231112
uint16_t index = emberAfIndexFromEndpoint(endpoint);
1124-
EmberAfDefinedEndpoint * de;
1125-
uint8_t i, c = 0;
1126-
const EmberAfCluster * cluster;
1127-
11281113
if (index == kEmberInvalidEndpointIndex)
11291114
{
11301115
return nullptr;
11311116
}
1132-
de = &(emAfEndpoints[index]);
11331117

1134-
for (i = 0; i < de->endpointType->clusterCount; i++)
1118+
const EmberAfEndpointType * endpointType = emAfEndpoints[index].endpointType;
1119+
const EmberAfClusterMask cluster_mask = server ? CLUSTER_MASK_SERVER : CLUSTER_MASK_CLIENT;
1120+
const uint8_t clusterCount = endpointType->clusterCount;
1121+
1122+
uint8_t c = 0;
1123+
for (uint8_t i = 0; i < clusterCount; i++)
11351124
{
1136-
cluster = &(de->endpointType->cluster[i]);
1125+
const EmberAfCluster * cluster = &(endpointType->cluster[i]);
11371126

1138-
if ((server && emberAfClusterIsServer(cluster)) || ((!server) && emberAfClusterIsClient(cluster)))
1127+
if ((cluster->mask & cluster_mask) == 0)
11391128
{
1140-
if (c == n)
1141-
{
1142-
return cluster;
1143-
}
1144-
c++;
1129+
continue;
11451130
}
1131+
1132+
if (c == n)
1133+
{
1134+
return cluster;
1135+
}
1136+
1137+
c++;
11461138
}
11471139
return nullptr;
11481140
}

src/app/util/attribute-storage.h

-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ extern uint8_t attributeData[]; // main storage bucket for all attributes
8282

8383
void emAfCallInits(void);
8484

85-
#define emberAfClusterIsClient(cluster) ((bool) (((cluster)->mask & CLUSTER_MASK_CLIENT) != 0))
86-
#define emberAfClusterIsServer(cluster) ((bool) (((cluster)->mask & CLUSTER_MASK_SERVER) != 0))
87-
8885
// Initial configuration
8986
void emberAfEndpointConfigure(void);
9087

0 commit comments

Comments
 (0)