From e810d5f5ac3c5d290f39449c6f46359215c14389 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 7 Dec 2021 15:01:34 -0800 Subject: [PATCH] Implement event support for basic information cluster --- src/app/clusters/basic/basic.cpp | 61 ++++++++++++++++++- .../operational-credentials-server.cpp | 15 +++++ src/include/platform/PlatformManager.h | 27 +++++++- src/platform/Linux/PlatformManagerImpl.cpp | 25 ++++++-- 4 files changed, 122 insertions(+), 6 deletions(-) diff --git a/src/app/clusters/basic/basic.cpp b/src/app/clusters/basic/basic.cpp index 3b5055c915018b..34832f71cfb4f5 100644 --- a/src/app/clusters/basic/basic.cpp +++ b/src/app/clusters/basic/basic.cpp @@ -19,15 +19,71 @@ #include "basic.h" #include +#include +#include +#include #include #include +#include #include using namespace chip; +using namespace chip::app; +using namespace chip::app::Clusters; using namespace chip::app::Clusters::Basic; using namespace chip::DeviceLayer; +namespace { + +class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate +{ + // Gets called by the current Node after completing a boot or reboot process. + void OnStartUp(uint32_t softwareVersion) override + { + 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(context) }; + EventNumber eventNumber; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber, EventOptions::Type::kUrgent)) + { + ChipLogError(Zcl, "PlatformMgrDelegate: Failed to record StartUp event"); + } + + return Loop::Continue; + }, + static_cast(softwareVersion)); + } + + // 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 { + // If Basic cluster is implemented on this endpoint + Events::ShutDown::Type event; + EventNumber eventNumber; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) + { + ChipLogError(Zcl, "PlatformMgrDelegate: Failed to record ShutDown event"); + } + + return Loop::Continue; + }); + } +}; + +PlatformMgrDelegate gPlatformMgrDelegate; + +} // anonymous namespace + void emberAfBasicClusterServerInitCallback(chip::EndpointId endpoint) { EmberAfStatus status; @@ -111,4 +167,7 @@ void emberAfBasicClusterServerInitCallback(chip::EndpointId endpoint) } } -void MatterBasicPluginServerInitCallback() {} +void MatterBasicPluginServerInitCallback() +{ + PlatformMgr().SetDelegate(&gPlatformMgrDelegate); +} diff --git a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp index e9151697e14918..c985701bcc5371 100644 --- a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp +++ b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -228,6 +229,20 @@ 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 { + // If Basic cluster is implemented on this endpoint + Basic::Events::Leave::Type event; + EventNumber eventNumber; + + if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) + { + ChipLogError(Zcl, "OpCredsFabricTableDelegate: Failed to record Leave event"); + } + + return Loop::Continue; + }); } // Gets called when a fabric is loaded into the FabricTable from KVS store. diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h index 0030f1da8cbbad..7ef6f5d9420da4 100644 --- a/src/include/platform/PlatformManager.h +++ b/src/include/platform/PlatformManager.h @@ -66,6 +66,28 @@ template class GenericThreadStackManagerImpl_OpenThread_LwIP; } // namespace Internal +/** + * Defines the delegate class of Platform Manager to notify platform updates. + */ +class PlatformManagerDelegate +{ +public: + virtual ~PlatformManagerDelegate() {} + + /** + * @brief + * Called by the current Node after completing a boot or reboot process. + */ + virtual void OnStartUp(uint32_t softwareVersion) {} + + /** + * @brief + * Called by the current Node prior to any orderly shutdown sequence on a + * best-effort basis. + */ + virtual void OnShutDown() {} +}; + /** * Provides features for initializing and interacting with the chip network * stack on a chip-enabled device. @@ -88,6 +110,8 @@ class PlatformManager CHIP_ERROR InitChipStack(); CHIP_ERROR AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0); void RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0); + void SetDelegate(PlatformManagerDelegate * delegate) { mDelegate = delegate; } + PlatformManagerDelegate * GetDelegate() const { return mDelegate; } /** * ScheduleWork can be called after InitChipStack has been called. Calls @@ -156,7 +180,8 @@ class PlatformManager #endif private: - bool mInitialized = false; + bool mInitialized = false; + PlatformManagerDelegate * mDelegate = nullptr; // ===== Members for internal use by the following friends. diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index e60738e36bb6cf..1fca3864d292b8 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -238,7 +238,14 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() CHIP_ERROR PlatformManagerImpl::_Shutdown() { - uint64_t upTime = 0; + PlatformManagerDelegate * platformManagerDelegate = PlatformMgr().GetDelegate(); + uint64_t upTime = 0; + + // The ShutDown event SHOULD be emitted by a Node prior to any orderly shutdown sequence. + if (platformManagerDelegate != nullptr) + { + platformManagerDelegate->OnShutDown(); + } if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR) { @@ -263,11 +270,21 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown() void PlatformManagerImpl::HandleDeviceRebooted(intptr_t arg) { - GeneralDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetGeneralDiagnosticsDelegate(); + PlatformManagerDelegate * platformManagerDelegate = PlatformMgr().GetDelegate(); + GeneralDiagnosticsDelegate * generalDiagnosticsDelegate = GetDiagnosticDataProvider().GetGeneralDiagnosticsDelegate(); - if (delegate != nullptr) + if (generalDiagnosticsDelegate != nullptr) { - delegate->OnDeviceRebooted(); + generalDiagnosticsDelegate->OnDeviceRebooted(); + } + + // The StartUp event SHALL be emitted by a Node after completing a boot or reboot process + if (platformManagerDelegate != nullptr) + { + uint16_t softwareVersion; + + ReturnOnFailure(ConfigurationMgr().GetSoftwareVersion(softwareVersion)); + platformManagerDelegate->OnStartUp(softwareVersion); } }