Skip to content

Commit

Permalink
Remove duplicate logic to cleanup FailSafeContext (#16854)
Browse files Browse the repository at this point in the history
* Remove duplicate logic to cleanup FailSafeContext

* Update src/include/platform/FailSafeContext.h

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>

* Address review comments

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
  • Loading branch information
2 people authored and pull[bot] committed Aug 21, 2023
1 parent 624670b commit 4964738
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 42 deletions.
9 changes: 7 additions & 2 deletions src/include/platform/FailSafeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,7 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::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:
Expand Down Expand Up @@ -901,22 +889,6 @@ void GenericConfigurationManagerImpl<ConfigClass>::LogDeviceConfig()
}
}

template <class ConfigClass>
void GenericConfigurationManagerImpl<ConfigClass>::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
Expand Down
20 changes: 12 additions & 8 deletions src/platform/FailSafeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<intptr_t>(this));
}

Expand Down

0 comments on commit 4964738

Please sign in to comment.