Skip to content

Commit

Permalink
Record events when diagnostic events occurs on platform side (#12709)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Nov 21, 2023
1 parent 9aa2c3f commit 2256962
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/AttributeAccessInterface.h>
#include <app/EventLogging.h>
#include <app/reporting/reporting.h>
#include <app/util/attribute-storage.h>
#include <platform/ConnectivityManager.h>
Expand Down Expand Up @@ -211,6 +212,34 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected");

ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::ActiveHardwareFaults::Id);

for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
{
if (emberAfEndpointIndexIsEnabled(index))
{
EndpointId endpointId = emberAfEndpointFromIndex(index);

if (emberAfContainsServer(endpointId, 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.
Expand All @@ -219,6 +248,34 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected");

ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::ActiveRadioFaults::Id);

for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
{
if (emberAfEndpointIndexIsEnabled(index))
{
EndpointId endpointId = emberAfEndpointFromIndex(index);

if (emberAfContainsServer(endpointId, 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.
Expand All @@ -227,6 +284,34 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected");

ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::ActiveNetworkFaults::Id);

for (uint16_t index = 0; index < emberAfEndpointCount(); index++)
{
if (emberAfEndpointIndexIsEnabled(index))
{
EndpointId endpointId = emberAfEndpointFromIndex(index);

if (emberAfContainsServer(endpointId, 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 @@ -22,6 +22,7 @@
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/core/Optional.h>
Expand Down Expand Up @@ -131,10 +132,24 @@ class SoftwareDiagnosticsDelegate : public DeviceLayer::SoftwareDiagnosticsDeleg
{
ChipLogProgress(Zcl, "SoftwareDiagnosticsDelegate: OnSoftwareFaultDetected");

ForAllEndpointsWithServerCluster(SoftwareDiagnostics::Id, [](EndpointId endpoint, intptr_t) -> Loop {
// TODO: Log SoftwareFault event and walk them all.
return Loop::Break;
});
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));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <app/AttributeAccessInterface.h>
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/EventLogging.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/core/Optional.h>
Expand Down Expand Up @@ -152,18 +153,68 @@ class WiFiDiagnosticsDelegate : public DeviceLayer::WiFiDiagnosticsDelegate
void OnDisconnectionDetected(uint16_t reasonCode) override
{
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnDisconnectionDetected");

ForAllEndpointsWithServerCluster(
WiFiNetworkDiagnostics::Id,
[](EndpointId endpoint, intptr_t context) -> Loop {
// If WiFi Network Diagnostics cluster is implemented on this endpoint
Events::Disconnection::Type event{ static_cast<uint16_t>(context) };
EventNumber eventNumber;

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

return Loop::Continue;
},
static_cast<intptr_t>(reasonCode));
}

// Gets called when the Node fails to associate or authenticate an access point.
void OnAssociationFailureDetected(uint8_t associationFailureCause, uint16_t status) override
{
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnAssociationFailureDetected");

Events::AssociationFailure::Type event{ static_cast<AssociationFailureCause>(associationFailureCause), status };

ForAllEndpointsWithServerCluster(
WiFiNetworkDiagnostics::Id,
[](EndpointId endpoint, intptr_t context) -> Loop {
// If WiFi Network Diagnostics cluster is implemented on this endpoint
Events::AssociationFailure::Type * pEvent = reinterpret_cast<Events::AssociationFailure::Type *>(context);
EventNumber eventNumber;

if (CHIP_NO_ERROR != LogEvent(*pEvent, endpoint, eventNumber))
{
ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record AssociationFailure event");
}

return Loop::Continue;
},
reinterpret_cast<intptr_t>(&event));
}

// Gets when the Node’s connection status to a Wi-Fi network has changed.
void OnConnectionStatusChanged(uint8_t connectionStatus) override
{
ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnConnectionStatusChanged");

ForAllEndpointsWithServerCluster(
WiFiNetworkDiagnostics::Id,
[](EndpointId endpoint, intptr_t context) -> Loop {
// If WiFi Network Diagnostics cluster is implemented on this endpoint
Events::ConnectionStatus::Type event{ static_cast<WiFiConnectionStatus>(context) };
EventNumber eventNumber;

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

return Loop::Continue;
},
static_cast<intptr_t>(connectionStatus));
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/include/platform/GeneralFaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GeneralFaults
CHIP_ERROR add(const uint8_t value);

uint8_t * data() { return mData; }
int size() const;
size_t size() const;
uint8_t operator[](int index) const;

Iterator begin() const;
Expand Down Expand Up @@ -85,9 +85,9 @@ inline CHIP_ERROR GeneralFaults<N>::add(const uint8_t value)
}

template <size_t N>
inline int GeneralFaults<N>::size() const
inline size_t GeneralFaults<N>::size() const
{
return mSize;
return static_cast<size_t>(mSize);
}

template <size_t N>
Expand Down

0 comments on commit 2256962

Please sign in to comment.