Skip to content

Commit 5589310

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Remove useless IsInitialized() checks on fabric info. (#18772)
* Remove useless IsInitialized() checks on fabric info. All the consumers are iterating the fabric table, and that only iterates the fabric infos that are in fact initialized. * Fix build on compilers that are trying to be smart.
1 parent fdc3ad9 commit 5589310

File tree

3 files changed

+25
-46
lines changed

3 files changed

+25
-46
lines changed

examples/common/pigweed/rpc_services/Device.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Device : public pw_rpc::nanopb::Device::Service<Device>
8383
size_t count = 0;
8484
for (const FabricInfo & fabricInfo : Server::GetInstance().GetFabricTable())
8585
{
86-
if (count < ArraySize(response.fabric_info) && fabricInfo.IsInitialized())
86+
if (count < ArraySize(response.fabric_info))
8787
{
8888
response.fabric_info[count].fabric_id = fabricInfo.GetFabricId();
8989
response.fabric_info[count].node_id = fabricInfo.GetPeerId().GetNodeId();

src/app/clusters/operational-credentials-server/operational-credentials-server.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ CHIP_ERROR OperationalCredentialsAttrAccess::ReadNOCs(EndpointId endpoint, Attri
122122
{
123123
Clusters::OperationalCredentials::Structs::NOCStruct::Type noc;
124124

125-
if (!fabricInfo.IsInitialized())
126-
continue;
127-
128125
noc.fabricIndex = fabricInfo.GetFabricIndex();
129126

130127
if (accessingFabricIndex == fabricInfo.GetFabricIndex())
@@ -164,9 +161,6 @@ CHIP_ERROR OperationalCredentialsAttrAccess::ReadFabricsList(EndpointId endpoint
164161
return aEncoder.EncodeList([](const auto & encoder) -> CHIP_ERROR {
165162
for (auto & fabricInfo : Server::GetInstance().GetFabricTable())
166163
{
167-
if (!fabricInfo.IsInitialized())
168-
continue;
169-
170164
Clusters::OperationalCredentials::Structs::FabricDescriptor::Type fabricDescriptor;
171165

172166
fabricDescriptor.fabricIndex = fabricInfo.GetFabricIndex();
@@ -193,10 +187,6 @@ CHIP_ERROR OperationalCredentialsAttrAccess::ReadRootCertificates(EndpointId end
193187
for (auto & fabricInfo : Server::GetInstance().GetFabricTable())
194188
{
195189
ByteSpan cert;
196-
197-
if (!fabricInfo.IsInitialized())
198-
continue;
199-
200190
ReturnErrorOnFailure(fabricInfo.GetRootCert(cert));
201191
ReturnErrorOnFailure(encoder.Encode(cert));
202192
}

src/app/server/Dnssd.cpp

+24-35
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,7 @@ bool DnssdServer::HaveOperationalCredentials()
7070
VerifyOrDie(mFabricTable != nullptr);
7171

7272
// Look for any fabric info that has a useful operational identity.
73-
for (const FabricInfo & fabricInfo : *mFabricTable)
74-
{
75-
if (fabricInfo.IsInitialized())
76-
{
77-
return true;
78-
}
79-
}
80-
81-
return false;
73+
return mFabricTable->FabricCount() != 0;
8274
}
8375

8476
#if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
@@ -252,34 +244,31 @@ CHIP_ERROR DnssdServer::AdvertiseOperational()
252244

253245
for (const FabricInfo & fabricInfo : *mFabricTable)
254246
{
255-
if (fabricInfo.IsInitialized())
247+
uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
248+
MutableByteSpan mac(macBuffer);
249+
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
256250
{
257-
uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
258-
MutableByteSpan mac(macBuffer);
259-
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
260-
{
261-
ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
262-
Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
263-
}
264-
265-
const auto advertiseParameters = chip::Dnssd::OperationalAdvertisingParameters()
266-
.SetPeerId(fabricInfo.GetPeerId())
267-
.SetMac(mac)
268-
.SetPort(GetSecuredPort())
269-
.SetInterfaceId(GetInterfaceId())
270-
.SetMRPConfig(GetLocalMRPConfig())
271-
.SetTcpSupported(Optional<bool>(INET_CONFIG_ENABLE_TCP_ENDPOINT))
272-
.EnableIpV4(true);
273-
274-
auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();
275-
276-
ChipLogProgress(Discovery, "Advertise operational node " ChipLogFormatX64 "-" ChipLogFormatX64,
277-
ChipLogValueX64(advertiseParameters.GetPeerId().GetCompressedFabricId()),
278-
ChipLogValueX64(advertiseParameters.GetPeerId().GetNodeId()));
279-
// Should we keep trying to advertise the other operational
280-
// identities on failure?
281-
ReturnErrorOnFailure(mdnsAdvertiser.Advertise(advertiseParameters));
251+
ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
252+
Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
282253
}
254+
255+
const auto advertiseParameters = chip::Dnssd::OperationalAdvertisingParameters()
256+
.SetPeerId(fabricInfo.GetPeerId())
257+
.SetMac(mac)
258+
.SetPort(GetSecuredPort())
259+
.SetInterfaceId(GetInterfaceId())
260+
.SetMRPConfig(GetLocalMRPConfig())
261+
.SetTcpSupported(Optional<bool>(INET_CONFIG_ENABLE_TCP_ENDPOINT))
262+
.EnableIpV4(true);
263+
264+
auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance();
265+
266+
ChipLogProgress(Discovery, "Advertise operational node " ChipLogFormatX64 "-" ChipLogFormatX64,
267+
ChipLogValueX64(advertiseParameters.GetPeerId().GetCompressedFabricId()),
268+
ChipLogValueX64(advertiseParameters.GetPeerId().GetNodeId()));
269+
// Should we keep trying to advertise the other operational
270+
// identities on failure?
271+
ReturnErrorOnFailure(mdnsAdvertiser.Advertise(advertiseParameters));
283272
}
284273
return CHIP_NO_ERROR;
285274
}

0 commit comments

Comments
 (0)