From 1606642f26aabd469d46879b7c29cbe44e056fb8 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Thu, 2 Dec 2021 09:13:16 -0800 Subject: [PATCH] Add diagnostic delegate interfaces for event support (#12451) --- .../general_diagnostics_server.cpp | 16 ++--- .../software_diagnostics_server.cpp | 11 ++++ .../wifi_network_diagnostics_server.cpp | 17 +++++ src/include/platform/DiagnosticDataProvider.h | 62 +++++++++++++++++-- src/platform/Linux/PlatformManagerImpl.cpp | 55 ++++++++++++++-- src/platform/Linux/PlatformManagerImpl.h | 3 + 6 files changed, 144 insertions(+), 20 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 75aef817892d89..688c3ba1ef6722 100644 --- a/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp +++ b/src/app/clusters/general_diagnostics_server/general_diagnostics_server.cpp @@ -167,13 +167,13 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & a return CHIP_NO_ERROR; } -class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegate, public DeviceLayer::DiagnosticsDelegate +class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelegate, public DeviceLayer::GeneralDiagnosticsDelegate { // Gets called when any network interface on the Node is updated. void OnNetworkInfoChanged() override { - ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnNetworkInfoChanged"); + ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnNetworkInfoChanged"); for (uint16_t index = 0; index < emberAfEndpointCount(); index++) { @@ -194,7 +194,7 @@ class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegat // Gets called when the device has been rebooted. void OnDeviceRebooted() override { - ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnDeviceRebooted"); + ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnDeviceRebooted"); for (uint16_t index = 0; index < emberAfEndpointCount(); index++) { @@ -217,7 +217,7 @@ class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegat // Get called when the Node detects a hardware fault has been raised. void OnHardwareFaultsDetected() override { - ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnHardwareFaultsDetected"); + ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected"); for (uint16_t index = 0; index < emberAfEndpointCount(); index++) { @@ -238,7 +238,7 @@ class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegat // Get called when the Node detects a radio fault has been raised. void OnRadioFaultsDetected() override { - ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnHardwareFaultsDetected"); + ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected"); for (uint16_t index = 0; index < emberAfEndpointCount(); index++) { @@ -259,7 +259,7 @@ class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegat // Get called when the Node detects a network fault has been raised. void OnNetworkFaultsDetected() override { - ChipLogProgress(Zcl, "GeneralDiagnosticDelegate: OnHardwareFaultsDetected"); + ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnHardwareFaultsDetected"); for (uint16_t index = 0; index < emberAfEndpointCount(); index++) { @@ -278,7 +278,7 @@ class GeneralDiagnosticDelegate : public DeviceLayer::ConnectivityManagerDelegat } }; -GeneralDiagnosticDelegate gDiagnosticDelegate; +GeneralDiagnosticsDelegate gDiagnosticDelegate; } // anonymous namespace @@ -286,6 +286,6 @@ void MatterGeneralDiagnosticsPluginServerInitCallback() { registerAttributeAccessOverride(&gAttrAccess); - GetDiagnosticDataProvider().SetDelegate(&gDiagnosticDelegate); ConnectivityMgr().SetDelegate(&gDiagnosticDelegate); + GetDiagnosticDataProvider().SetGeneralDiagnosticsDelegate(&gDiagnosticDelegate); } 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 667601f56a03cd..7ed40d6d29a0a7 100644 --- a/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp +++ b/src/app/clusters/software_diagnostics_server/software_diagnostics_server.cpp @@ -33,6 +33,7 @@ using namespace chip::app::Clusters; using namespace chip::app::Clusters::SoftwareDiagnostics; using namespace chip::app::Clusters::SoftwareDiagnostics::Attributes; using chip::DeviceLayer::DiagnosticDataProvider; +using chip::DeviceLayer::GetDiagnosticDataProvider; namespace { @@ -122,6 +123,15 @@ CHIP_ERROR SoftwareDiagosticsAttrAccess::ReadThreadMetrics(AttributeValueEncoder return err; } + +class SoftwareDiagnosticsDelegate : public DeviceLayer::SoftwareDiagnosticsDelegate +{ + // Gets called when a software fault that has taken place on the Node. + void OnSoftwareFaultDetected() override { ChipLogProgress(Zcl, "SoftwareDiagnosticsDelegate: OnSoftwareFaultDetected"); } +}; + +SoftwareDiagnosticsDelegate gDiagnosticDelegate; + } // anonymous namespace bool emberAfSoftwareDiagnosticsClusterResetWatermarksCallback(app::CommandHandler * commandObj, @@ -151,4 +161,5 @@ bool emberAfSoftwareDiagnosticsClusterResetWatermarksCallback(app::CommandHandle void MatterSoftwareDiagnosticsPluginServerInitCallback() { registerAttributeAccessOverride(&gAttrAccess); + GetDiagnosticDataProvider().SetSoftwareDiagnosticsDelegate(&gDiagnosticDelegate); } 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 0e0bbc1f88025a..9871283e38c71c 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 @@ -33,6 +33,7 @@ using namespace chip::app::Clusters; using namespace chip::app::Clusters::WiFiNetworkDiagnostics; using namespace chip::app::Clusters::WiFiNetworkDiagnostics::Attributes; using chip::DeviceLayer::DiagnosticDataProvider; +using chip::DeviceLayer::GetDiagnosticDataProvider; namespace { @@ -144,6 +145,21 @@ CHIP_ERROR WiFiDiagosticsAttrAccess::Read(const ConcreteReadAttributePath & aPat } return CHIP_NO_ERROR; } + +class WiFiDiagnosticsDelegate : public DeviceLayer::WiFiDiagnosticsDelegate +{ + // Gets called when the Node detects Node’s Wi-Fi connection has been disconnected. + void OnDisconnectionDetected() override { ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnDisconnectionDetected"); } + + // Gets called when the Node fails to associate or authenticate an access point. + void OnAssociationFailureDetected() override { ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnAssociationFailureDetected"); } + + // Gets when the Node’s connection status to a Wi-Fi network has changed. + void OnConnectionStatusChanged() override { ChipLogProgress(Zcl, "WiFiDiagnosticsDelegate: OnConnectionStatusChanged"); } +}; + +WiFiDiagnosticsDelegate gDiagnosticDelegate; + } // anonymous namespace bool emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler * commandObj, @@ -186,4 +202,5 @@ bool emberAfWiFiNetworkDiagnosticsClusterResetCountsCallback(app::CommandHandler void MatterWiFiNetworkDiagnosticsPluginServerInitCallback() { registerAttributeAccessOverride(&gAttrAccess); + GetDiagnosticDataProvider().SetWiFiDiagnosticsDelegate(&gDiagnosticDelegate); } diff --git a/src/include/platform/DiagnosticDataProvider.h b/src/include/platform/DiagnosticDataProvider.h index 25a67bfc21dfa1..3ac8a47f6ae05b 100644 --- a/src/include/platform/DiagnosticDataProvider.h +++ b/src/include/platform/DiagnosticDataProvider.h @@ -50,12 +50,12 @@ struct NetworkInterface : public app::Clusters::GeneralDiagnostics::Structs::Net }; /** - * Defines the delegate class of Platform Manager to notify platform updates. + * Defines the General Diagnostics Delegate class to notify platform events. */ -class DiagnosticsDelegate +class GeneralDiagnosticsDelegate { public: - virtual ~DiagnosticsDelegate() {} + virtual ~GeneralDiagnosticsDelegate() {} /** * @brief @@ -82,14 +82,62 @@ class DiagnosticsDelegate virtual void OnNetworkFaultsDetected() {} }; +/** + * Defines the Software Diagnostics Delegate class to notify software events. + */ +class SoftwareDiagnosticsDelegate +{ +public: + virtual ~SoftwareDiagnosticsDelegate() {} + + /** + * @brief + * Called when a software fault that has taken place on the Node. + */ + virtual void OnSoftwareFaultDetected() {} +}; + +/** + * Defines the WiFi Diagnostics Delegate class to notify WiFi network events. + */ +class WiFiDiagnosticsDelegate +{ +public: + virtual ~WiFiDiagnosticsDelegate() {} + + /** + * @brief + * Called when the Node detects Node’s Wi-Fi connection has been disconnected. + */ + virtual void OnDisconnectionDetected() {} + + /** + * @brief + * Called when the Node fails to associate or authenticate an access point. + */ + virtual void OnAssociationFailureDetected() {} + + /** + * @brief + * Called when the Node’s connection status to a Wi-Fi network has changed. + */ + virtual void OnConnectionStatusChanged() {} +}; + /** * Provides access to runtime and build-time configuration information for a chip device. */ class DiagnosticDataProvider { public: - void SetDelegate(DiagnosticsDelegate * delegate) { mDelegate = delegate; } - DiagnosticsDelegate * GetDelegate() const { return mDelegate; } + void SetGeneralDiagnosticsDelegate(GeneralDiagnosticsDelegate * delegate) { mGeneralDiagnosticsDelegate = delegate; } + GeneralDiagnosticsDelegate * GetGeneralDiagnosticsDelegate() const { return mGeneralDiagnosticsDelegate; } + + void SetSoftwareDiagnosticsDelegate(SoftwareDiagnosticsDelegate * delegate) { mSoftwareDiagnosticsDelegate = delegate; } + SoftwareDiagnosticsDelegate * GetSoftwareDiagnosticsDelegate() const { return mSoftwareDiagnosticsDelegate; } + + void SetWiFiDiagnosticsDelegate(WiFiDiagnosticsDelegate * delegate) { mWiFiDiagnosticsDelegate = delegate; } + WiFiDiagnosticsDelegate * GetWiFiDiagnosticsDelegate() const { return mWiFiDiagnosticsDelegate; } /** * General Diagnostics methods. @@ -163,7 +211,9 @@ class DiagnosticDataProvider virtual ~DiagnosticDataProvider() = default; private: - DiagnosticsDelegate * mDelegate = nullptr; + GeneralDiagnosticsDelegate * mGeneralDiagnosticsDelegate = nullptr; + SoftwareDiagnosticsDelegate * mSoftwareDiagnosticsDelegate = nullptr; + WiFiDiagnosticsDelegate * mWiFiDiagnosticsDelegate = nullptr; // No copy, move or assignment. DiagnosticDataProvider(const DiagnosticDataProvider &) = delete; diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index cf0faa78ebe0bb..c3c7b668e2b6ff 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -42,6 +42,8 @@ #include #include +using namespace ::chip::app::Clusters; + namespace chip { namespace DeviceLayer { @@ -84,19 +86,27 @@ void SignalHandler(int signum) ConfigurationMgr().StoreBootReason(EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_UPDATE_COMPLETED); err = CHIP_ERROR_REBOOT_SIGNAL_RECEIVED; break; + case SIGTRAP: + PlatformMgrImpl().HandleSoftwareFault(SoftwareDiagnostics::Events::SoftwareFault::kEventId); + break; + case SIGILL: + PlatformMgrImpl().HandleGeneralFault(GeneralDiagnostics::Events::HardwareFaultChange::kEventId); + break; + case SIGALRM: + PlatformMgrImpl().HandleGeneralFault(GeneralDiagnostics::Events::RadioFaultChange::kEventId); + break; + case SIGVTALRM: + PlatformMgrImpl().HandleGeneralFault(GeneralDiagnostics::Events::NetworkFaultChange::kEventId); + break; default: break; } - if (err != CHIP_NO_ERROR) + if (err == CHIP_ERROR_REBOOT_SIGNAL_RECEIVED) { PlatformMgr().Shutdown(); exit(EXIT_FAILURE); } - else - { - ChipLogDetail(DeviceLayer, "Ignore signal %d", signum); - } } #if CHIP_WITH_GIO @@ -253,7 +263,7 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() void PlatformManagerImpl::HandleDeviceRebooted(intptr_t arg) { - DiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetDelegate(); + GeneralDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetGeneralDiagnosticsDelegate(); if (delegate != nullptr) { @@ -261,6 +271,39 @@ void PlatformManagerImpl::HandleDeviceRebooted(intptr_t arg) } } +void PlatformManagerImpl::HandleGeneralFault(uint32_t EventId) +{ + GeneralDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetGeneralDiagnosticsDelegate(); + + if (delegate != nullptr) + { + switch (EventId) + { + case GeneralDiagnostics::Events::HardwareFaultChange::kEventId: + delegate->OnHardwareFaultsDetected(); + break; + case GeneralDiagnostics::Events::RadioFaultChange::kEventId: + delegate->OnRadioFaultsDetected(); + break; + case GeneralDiagnostics::Events::NetworkFaultChange::kEventId: + delegate->OnNetworkFaultsDetected(); + break; + default: + break; + } + } +} + +void PlatformManagerImpl::HandleSoftwareFault(uint32_t EventId) +{ + SoftwareDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetSoftwareDiagnosticsDelegate(); + + if (delegate != nullptr) + { + delegate->OnSoftwareFaultDetected(); + } +} + #if CHIP_WITH_GIO GDBusConnection * PlatformManagerImpl::GetGDBusConnection() { diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index 96f241fbb533ed..1102971c983036 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -56,6 +56,9 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener System::Clock::Timestamp GetStartTime() { return mStartTime; } + void HandleGeneralFault(uint32_t EventId); + void HandleSoftwareFault(uint32_t EventId); + private: // ===== Methods that implement the PlatformManager abstract interface.