From 49647383b16caeb78b471e738f6734850a058bc4 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Thu, 31 Mar 2022 09:19:02 -0700 Subject: [PATCH] Remove duplicate logic to cleanup FailSafeContext (#16854) * Remove duplicate logic to cleanup FailSafeContext * Update src/include/platform/FailSafeContext.h Co-authored-by: Boris Zbarsky * Address review comments Co-authored-by: Boris Zbarsky --- src/include/platform/FailSafeContext.h | 9 ++++-- .../GenericConfigurationManagerImpl.h | 3 -- .../GenericConfigurationManagerImpl.ipp | 30 +------------------ src/platform/FailSafeContext.cpp | 20 ++++++++----- 4 files changed, 20 insertions(+), 42 deletions(-) diff --git a/src/include/platform/FailSafeContext.h b/src/include/platform/FailSafeContext.h index f3d515a3426545..b3a2a677310871 100644 --- a/src/include/platform/FailSafeContext.h +++ b/src/include/platform/FailSafeContext.h @@ -44,6 +44,13 @@ class FailSafeContext CHIP_ERROR SetAddNocCommandInvoked(FabricIndex nocFabricIndex); CHIP_ERROR SetUpdateNocCommandInvoked(); + /** + * @brief + * Schedules a work to cleanup the FailSafe Context asynchronously after various cleanup work + * has completed. + */ + void ScheduleFailSafeCleanup(FabricIndex fabricIndex, bool addNocCommandInvoked, bool updateNocCommandInvoked); + inline bool IsFailSafeArmed(FabricIndex accessingFabricIndex) const { return mFailSafeArmed && MatchesFabricIndex(accessingFabricIndex); @@ -55,8 +62,6 @@ class FailSafeContext inline bool IsFailSafeArmed() const { return mFailSafeArmed; } - inline void SetFailSafeBusy(bool val) { mFailSafeBusy = val; } - inline bool MatchesFabricIndex(FabricIndex accessingFabricIndex) const { VerifyOrDie(mFailSafeArmed); diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.h b/src/include/platform/internal/GenericConfigurationManagerImpl.h index c4b18171783c9d..9cc4f523049483 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.h +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.h @@ -153,9 +153,6 @@ class GenericConfigurationManagerImpl : public ConfigurationManager virtual CHIP_ERROR WriteConfigValueStr(Key key, const char * str, size_t strLen) = 0; virtual CHIP_ERROR WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen) = 0; virtual void RunConfigUnitTest(void) = 0; - -private: - static void HandleFailSafeContextCleanup(intptr_t arg); }; } // namespace Internal diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.ipp b/src/include/platform/internal/GenericConfigurationManagerImpl.ipp index 6dc37c1257c2b3..4ec3933d4660c4 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.ipp +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.ipp @@ -267,19 +267,7 @@ CHIP_ERROR GenericConfigurationManagerImpl::Init() err = FailSafeContext::LoadFromStorage(fabricIndex, addNocCommandInvoked, updateNocCommandInvoked); SuccessOrExit(err); - ChipDeviceEvent event; - event.Type = DeviceEventType::kFailSafeTimerExpired; - event.FailSafeTimerExpired.PeerFabricIndex = fabricIndex; - event.FailSafeTimerExpired.AddNocCommandHasBeenInvoked = addNocCommandInvoked; - event.FailSafeTimerExpired.UpdateNocCommandHasBeenInvoked = updateNocCommandInvoked; - - err = PlatformMgr().PostEvent(&event); - SuccessOrExit(err); - - DeviceControlServer::DeviceControlSvr().GetFailSafeContext().SetFailSafeBusy(true); - - // Ensure HandleFailSafeContextCleanup runs after the timer-expired event has been processed. - PlatformMgr().ScheduleWork(HandleFailSafeContextCleanup); + DeviceControlServer::DeviceControlSvr().GetFailSafeContext().ScheduleFailSafeCleanup(fabricIndex, addNocCommandInvoked, updateNocCommandInvoked); } exit: @@ -901,22 +889,6 @@ void GenericConfigurationManagerImpl::LogDeviceConfig() } } -template -void GenericConfigurationManagerImpl::HandleFailSafeContextCleanup(intptr_t arg) -{ - if (ConfigurationMgr().SetFailSafeArmed(false) != CHIP_NO_ERROR) - { - ChipLogError(DeviceLayer, "Failed to set FailSafeArmed config to false"); - } - - if (FailSafeContext::DeleteFromStorage() != CHIP_NO_ERROR) - { - ChipLogError(DeviceLayer, "Failed to delete FailSafeContext from configuration"); - } - - DeviceControlServer::DeviceControlSvr().GetFailSafeContext().SetFailSafeBusy(false); -} - } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/FailSafeContext.cpp b/src/platform/FailSafeContext.cpp index 2b72302d574d2f..9373811f5f4f7a 100644 --- a/src/platform/FailSafeContext.cpp +++ b/src/platform/FailSafeContext.cpp @@ -60,24 +60,28 @@ void FailSafeContext::HandleDisarmFailSafe(intptr_t arg) void FailSafeContext::FailSafeTimerExpired() { - ChipDeviceEvent event; - event.Type = DeviceEventType::kFailSafeTimerExpired; - event.FailSafeTimerExpired.PeerFabricIndex = mFabricIndex; - event.FailSafeTimerExpired.AddNocCommandHasBeenInvoked = mAddNocCommandHasBeenInvoked; - event.FailSafeTimerExpired.UpdateNocCommandHasBeenInvoked = mUpdateNocCommandHasBeenInvoked; - CHIP_ERROR status = PlatformMgr().PostEvent(&event); + ScheduleFailSafeCleanup(mFabricIndex, mAddNocCommandHasBeenInvoked, mUpdateNocCommandHasBeenInvoked); +} +void FailSafeContext::ScheduleFailSafeCleanup(FabricIndex fabricIndex, bool addNocCommandInvoked, bool updateNocCommandInvoked) +{ mFailSafeArmed = false; mAddNocCommandHasBeenInvoked = false; mUpdateNocCommandHasBeenInvoked = false; + mFailSafeBusy = true; + + ChipDeviceEvent event; + event.Type = DeviceEventType::kFailSafeTimerExpired; + event.FailSafeTimerExpired.PeerFabricIndex = fabricIndex; + event.FailSafeTimerExpired.AddNocCommandHasBeenInvoked = addNocCommandInvoked; + event.FailSafeTimerExpired.UpdateNocCommandHasBeenInvoked = updateNocCommandInvoked; + CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Failed to post fail-safe timer expired: %" CHIP_ERROR_FORMAT, status.Format()); } - mFailSafeBusy = true; - PlatformMgr().ScheduleWork(HandleDisarmFailSafe, reinterpret_cast(this)); }