diff --git a/config/nrfconnect/chip-module/Kconfig.features b/config/nrfconnect/chip-module/Kconfig.features index 8eee94b807..28660f8dd0 100644 --- a/config/nrfconnect/chip-module/Kconfig.features +++ b/config/nrfconnect/chip-module/Kconfig.features @@ -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 diff --git a/examples/platform/nrfconnect/util/include/FabricTableDelegate.h b/examples/platform/nrfconnect/util/include/FabricTableDelegate.h index 01b5e40be8..ca33243dcc 100644 --- a/examples/platform/nrfconnect/util/include/FabricTableDelegate.h +++ b/examples/platform/nrfconnect/util/include/FabricTableDelegate.h @@ -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 */ @@ -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; };