From b5fd0fde311de4383ea948a003a989704fc8c4c5 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Wed, 8 Dec 2021 10:19:00 -0800 Subject: [PATCH] Record events when diagnostic events occurs on platform side (#12709) --- .../general_diagnostics_server.cpp | 85 +++++++++++++++++++ .../software_diagnostics_server.cpp | 23 ++++- .../wifi_network_diagnostics_server.cpp | 51 +++++++++++ src/include/platform/GeneralFaults.h | 6 +- 4 files changed, 158 insertions(+), 7 deletions(-) diff --git a/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp b/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp index 53bcbcbb3d5db3..a2570a8f5582d3 100644 --- a/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp +++ b/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -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 currentList = DataModel::List( + reinterpret_cast(current.data()), current.size()); + DataModel::List previousList = DataModel::List( + reinterpret_cast(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. @@ -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 currentList = DataModel::List( + reinterpret_cast(current.data()), current.size()); + DataModel::List previousList = DataModel::List( + reinterpret_cast(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. @@ -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 currentList = DataModel::List( + reinterpret_cast(current.data()), current.size()); + DataModel::List previousList = DataModel::List( + reinterpret_cast(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"); + } + } + } + } } }; diff --git a/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp b/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp index 2d3c2f1338bfc1..4a8c99d0a77440 100644 --- a/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp +++ b/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -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(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(&softwareFault)); } }; diff --git a/src/app/clusters/wifi_network_diagnostics_server/wifi_network_diagnostics_server.cpp b/src/app/clusters/wifi_network_diagnostics_server/wifi_network_diagnostics_server.cpp index 207e87a65bfcb8..0d7ddc374fac7b 100644 --- a/src/app/clusters/wifi_network_diagnostics_server/wifi_network_diagnostics_server.cpp +++ b/src/app/clusters/wifi_network_diagnostics_server/wifi_network_diagnostics_server.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -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(context) }; + EventNumber eventNumber; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) + { + ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record Disconnection event"); + } + + return Loop::Continue; + }, + static_cast(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), 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(context); + EventNumber eventNumber; + + if (CHIP_NO_ERROR != LogEvent(*pEvent, endpoint, eventNumber)) + { + ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record AssociationFailure event"); + } + + return Loop::Continue; + }, + reinterpret_cast(&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(context) }; + EventNumber eventNumber; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) + { + ChipLogError(Zcl, "WiFiDiagnosticsDelegate: Failed to record ConnectionStatus event"); + } + + return Loop::Continue; + }, + static_cast(connectionStatus)); } }; diff --git a/src/include/platform/GeneralFaults.h b/src/include/platform/GeneralFaults.h index 08ac92be0b08d6..82bf3bbdac795b 100644 --- a/src/include/platform/GeneralFaults.h +++ b/src/include/platform/GeneralFaults.h @@ -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; @@ -85,9 +85,9 @@ inline CHIP_ERROR GeneralFaults::add(const uint8_t value) } template -inline int GeneralFaults::size() const +inline size_t GeneralFaults::size() const { - return mSize; + return static_cast(mSize); } template