Skip to content

Commit

Permalink
Create an iterator over endpoints matching a cluster. (#12955)
Browse files Browse the repository at this point in the history
Very slightly larger code, but nicer API than the current callback
function setup.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Sep 14, 2023
1 parent df91209 commit 8d5589a
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 219 deletions.
31 changes: 13 additions & 18 deletions src/app/clusters/basic/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,26 @@ class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate
{
ChipLogProgress(Zcl, "PlatformMgrDelegate: OnStartUp");

ForAllEndpointsWithServerCluster(
Basic::Id,
[](EndpointId endpoint, intptr_t context) -> Loop {
// If Basic cluster is implemented on this endpoint
Events::StartUp::Type event{ static_cast<uint32_t>(context) };
EventNumber eventNumber;

if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "PlatformMgrDelegate: Failed to record StartUp event");
}
for (auto endpoint : EnabledEndpointsWithServerCluster(Basic::Id))
{
// If Basic cluster is implemented on this endpoint
Events::StartUp::Type event{ softwareVersion };
EventNumber eventNumber;

return Loop::Continue;
},
static_cast<intptr_t>(softwareVersion));
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "PlatformMgrDelegate: Failed to record StartUp event");
}
}
}

// Gets called by the current Node prior to any orderly shutdown sequence on a best-effort basis.
void OnShutDown() override
{
ChipLogProgress(Zcl, "PlatformMgrDelegate: OnShutDown");

ForAllEndpointsWithServerCluster(Basic::Id, [](EndpointId endpoint, intptr_t context) -> Loop {
for (auto endpoint : EnabledEndpointsWithServerCluster(Basic::Id))
{
// If Basic cluster is implemented on this endpoint
Events::ShutDown::Type event;
EventNumber eventNumber;
Expand All @@ -75,9 +72,7 @@ class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate
{
ChipLogError(Zcl, "PlatformMgrDelegate: Failed to record ShutDown event");
}

return Loop::Continue;
});
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,10 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
{
static void ReportAttributeOnAllEndpoints(AttributeId attribute)
{
ForAllEndpointsWithServerCluster(
GeneralDiagnostics::Id,
[](EndpointId endpoint, intptr_t context) -> Loop {
MatterReportingAttributeChangeCallback(endpoint, GeneralDiagnostics::Id, static_cast<AttributeId>(context));
return Loop::Continue;
},
static_cast<intptr_t>(attribute));
for (auto endpoint : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
MatterReportingAttributeChangeCallback(endpoint, GeneralDiagnostics::Id, attribute);
}
}

// Gets called when any network interface on the Node is updated.
Expand All @@ -205,118 +202,83 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::BootReasons::Id);
}

template <size_t N>
struct TFaults
{
GeneralFaults<N> & previous;
GeneralFaults<N> & current;
};

// Get called when the Node detects a hardware fault has been raised.
void OnHardwareFaultsDetected(GeneralFaults<kMaxHardwareFaults> & previousArg,
GeneralFaults<kMaxHardwareFaults> & currentArg) override
void OnHardwareFaultsDetected(GeneralFaults<kMaxHardwareFaults> & previous,
GeneralFaults<kMaxHardwareFaults> & current) override
{
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected");

using Faults = TFaults<kMaxHardwareFaults>;
Faults faults = { previousArg, currentArg };
ForAllEndpointsWithServerCluster(
GeneralDiagnostics::Id,
[](EndpointId endpointId, intptr_t context) -> Loop {
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveHardwareFaults::Id);

auto * faultsPtr = reinterpret_cast<Faults *>(context);
auto & current = faultsPtr->current;
auto & previous = faultsPtr->previous;
// Record HardwareFault event
EventNumber eventNumber;
DataModel::List<const HardwareFaultType> currentList = DataModel::List<const HardwareFaultType>(
reinterpret_cast<const HardwareFaultType *>(current.data()), current.size());
DataModel::List<const HardwareFaultType> previousList = DataModel::List<const HardwareFaultType>(
reinterpret_cast<const HardwareFaultType *>(previous.data()), previous.size());
Events::HardwareFaultChange::Type event{ currentList, previousList };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record HardwareFault event");
}

return Loop::Continue;
},
reinterpret_cast<intptr_t>(&faults));
for (auto endpointId : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveHardwareFaults::Id);

// Record HardwareFault event
EventNumber eventNumber;
DataModel::List<const HardwareFaultType> currentList = DataModel::List<const HardwareFaultType>(
reinterpret_cast<const HardwareFaultType *>(current.data()), current.size());
DataModel::List<const HardwareFaultType> previousList = DataModel::List<const HardwareFaultType>(
reinterpret_cast<const HardwareFaultType *>(previous.data()), previous.size());
Events::HardwareFaultChange::Type event{ currentList, previousList };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record HardwareFault event");
}
}
}

// Get called when the Node detects a radio fault has been raised.
void OnRadioFaultsDetected(GeneralFaults<kMaxRadioFaults> & previousArg, GeneralFaults<kMaxRadioFaults> & currentArg) override
void OnRadioFaultsDetected(GeneralFaults<kMaxRadioFaults> & previous, GeneralFaults<kMaxRadioFaults> & current) override
{
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected");

using Faults = TFaults<kMaxRadioFaults>;
Faults faults = { previousArg, currentArg };
ForAllEndpointsWithServerCluster(
GeneralDiagnostics::Id,
[](EndpointId endpointId, intptr_t context) -> Loop {
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveRadioFaults::Id);

auto * faultsPtr = reinterpret_cast<Faults *>(context);
auto & current = faultsPtr->current;
auto & previous = faultsPtr->previous;
// Record RadioFault event
EventNumber eventNumber;
DataModel::List<const RadioFaultType> currentList =
DataModel::List<const RadioFaultType>(reinterpret_cast<const RadioFaultType *>(current.data()), current.size());
DataModel::List<const RadioFaultType> previousList = DataModel::List<const RadioFaultType>(
reinterpret_cast<const RadioFaultType *>(previous.data()), previous.size());
Events::RadioFaultChange::Type event{ currentList, previousList };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record RadioFault event");
}

return Loop::Continue;
},
reinterpret_cast<intptr_t>(&faults));
for (auto endpointId : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveRadioFaults::Id);

// Record RadioFault event
EventNumber eventNumber;
DataModel::List<const RadioFaultType> currentList =
DataModel::List<const RadioFaultType>(reinterpret_cast<const RadioFaultType *>(current.data()), current.size());
DataModel::List<const RadioFaultType> previousList =
DataModel::List<const RadioFaultType>(reinterpret_cast<const RadioFaultType *>(previous.data()), previous.size());
Events::RadioFaultChange::Type event{ currentList, previousList };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record RadioFault event");
}
}
}

// Get called when the Node detects a network fault has been raised.
void OnNetworkFaultsDetected(GeneralFaults<kMaxNetworkFaults> & previousArg,
GeneralFaults<kMaxNetworkFaults> & currentArg) override
void OnNetworkFaultsDetected(GeneralFaults<kMaxNetworkFaults> & previous, GeneralFaults<kMaxNetworkFaults> & current) override
{
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected");

using Faults = TFaults<kMaxNetworkFaults>;
Faults faults = { previousArg, currentArg };
ForAllEndpointsWithServerCluster(
GeneralDiagnostics::Id,
[](EndpointId endpointId, intptr_t context) -> Loop {
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveNetworkFaults::Id);

auto * faultsPtr = reinterpret_cast<Faults *>(context);
auto & current = faultsPtr->current;
auto & previous = faultsPtr->previous;
// Record NetworkFault event
EventNumber eventNumber;
DataModel::List<const NetworkFaultType> currentList = DataModel::List<const NetworkFaultType>(
reinterpret_cast<const NetworkFaultType *>(current.data()), current.size());
DataModel::List<const NetworkFaultType> previousList = DataModel::List<const NetworkFaultType>(
reinterpret_cast<const NetworkFaultType *>(previous.data()), previous.size());
Events::NetworkFaultChange::Type event{ currentList, previousList };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record NetworkFault event");
}

return Loop::Continue;
},
reinterpret_cast<intptr_t>(&faults));
for (auto endpointId : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If General Diagnostics cluster is implemented on this endpoint
MatterReportingAttributeChangeCallback(endpointId, GeneralDiagnostics::Id,
GeneralDiagnostics::Attributes::ActiveNetworkFaults::Id);

// Record NetworkFault event
EventNumber eventNumber;
DataModel::List<const NetworkFaultType> currentList =
DataModel::List<const NetworkFaultType>(reinterpret_cast<const NetworkFaultType *>(current.data()), current.size());
DataModel::List<const NetworkFaultType> previousList = DataModel::List<const NetworkFaultType>(
reinterpret_cast<const NetworkFaultType *>(previous.data()), previous.size());
Events::NetworkFaultChange::Type event{ currentList, previousList };

if (CHIP_NO_ERROR != LogEvent(event, endpointId, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record NetworkFault event");
}
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ class OpCredsFabricTableDelegate : public FabricTableDelegate
emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: Fabric 0x%" PRIu8 " was deleted from fabric storage.", fabricId);
fabricListChanged();

// The Leave event SHOULD be emitted by a Node prior to permanently leaving the Fabric.
ForAllEndpointsWithServerCluster(Basic::Id, [](EndpointId endpoint, intptr_t context) -> Loop {
// The Leave event SHOULD be emitted by a Node prior to permanently
// leaving the Fabric.
for (auto endpoint : EnabledEndpointsWithServerCluster(Basic::Id))
{
// If Basic cluster is implemented on this endpoint
Basic::Events::Leave::Type event;
EventNumber eventNumber;
Expand All @@ -240,9 +242,7 @@ class OpCredsFabricTableDelegate : public FabricTableDelegate
{
ChipLogError(Zcl, "OpCredsFabricTableDelegate: Failed to record Leave event");
}

return Loop::Continue;
});
}
}

// Gets called when a fabric is loaded into the FabricTable from KVS store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,17 @@ class SoftwareDiagnosticsDelegate : public DeviceLayer::SoftwareDiagnosticsDeleg
{
ChipLogProgress(Zcl, "SoftwareDiagnosticsDelegate: OnSoftwareFaultDetected");

ForAllEndpointsWithServerCluster(
SoftwareDiagnostics::Id,
[](EndpointId endpoint, intptr_t context) -> Loop {
// If Software Diagnostics cluster is implemented on this endpoint
SoftwareDiagnostics::Structs::SoftwareFault::Type * pSoftwareFault =
reinterpret_cast<SoftwareDiagnostics::Structs::SoftwareFault::Type *>(context);

EventNumber eventNumber;
Events::SoftwareFault::Type event{ *pSoftwareFault };

if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
{
ChipLogError(Zcl, "SoftwareDiagnosticsDelegate: Failed to record SoftwareFault event");
}

return Loop::Continue;
},
reinterpret_cast<intptr_t>(&softwareFault));
for (auto endpoint : EnabledEndpointsWithServerCluster(SoftwareDiagnostics::Id))
{
// If Software Diagnostics cluster is implemented on this endpoint
EventNumber eventNumber;
Events::SoftwareFault::Type event{ softwareFault };

if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber))
{
ChipLogError(Zcl, "SoftwareDiagnosticsDelegate: Failed to record SoftwareFault event");
}
}
}
};

Expand Down
Loading

0 comments on commit 8d5589a

Please sign in to comment.