Skip to content

Commit

Permalink
[nrf fromlist] [nrfconnect] Add a delay to last fabric remove action
Browse files Browse the repository at this point in the history
In some cases, a last fabric removal action should be delayed
to avoid race conditions and allow the device to finish all
currently running tasks.

To control the delay use the
CONFIG_CHIP_LAST_FABRIC_REMOVED_SCHEDULE_TIME kconfig.
By default it has been set to 500 ms.
  • Loading branch information
ArekBalysNordic committed Oct 13, 2023
1 parent 90100b7 commit 724804a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions config/nrfconnect/chip-module/Kconfig.features
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,13 @@ choice CHIP_LAST_FABRIC_REMOVED_ACTION

endchoice

config CHIP_LAST_FABRIC_REMOVED_SCHEDULE_TIME
int "After removing the last fabric wait defined time [in milliseconds] to perform an action"
depends on !CHIP_LAST_FABRIC_REMOVED_NONE
default 500
help
After removing the last fabric the device will wait for the defined time and then perform
an action chosen by the CHIP_LAST_FABRIC_REMOVED_ACTION option. This schedule will allow for
avoiding race conditions before the device removes non-volatile data.

endif # CHIP
14 changes: 11 additions & 3 deletions examples/platform/nrfconnect/util/include/FabricTableDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,25 @@ class AppFabricTableDelegate : public chip::FabricTable::Delegate
#ifndef CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
static AppFabricTableDelegate sAppFabricDelegate;
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppFabricDelegate);
k_timer_init(&sFactoryResetTimer, &FactoryResetTimerCallback, nullptr);
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
}

private:
void OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
{
k_timer_start(&sFactoryResetTimer, K_MSEC(CONFIG_CHIP_LAST_FABRIC_REMOVED_SCHEDULE_TIME), K_NO_WAIT);
}

static void FactoryResetTimerCallback(k_timer * timer)
{
#ifndef CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0)
{
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) {
#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
chip::Server::GetInstance().ScheduleFactoryReset();
chip::Server::GetInstance().ScheduleFactoryReset();
#elif defined(CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_ONLY) || defined(CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START)
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) {
/* Erase Matter data */
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();
/* Erase Network credentials and disconnect */
Expand All @@ -69,9 +75,11 @@ class AppFabricTableDelegate : public chip::FabricTable::Delegate
event.Handler = AppTask::StartBLEAdvertisementHandler;
AppTask::Instance().PostEvent(event);
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START
});
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
});
}
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
}

inline static k_timer sFactoryResetTimer;
};

0 comments on commit 724804a

Please sign in to comment.