Skip to content

Commit

Permalink
[nrfconnect] Add a delay to last fabric remove action (#29844)
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
CHIP_LAST_FABRIC_REMOVED_ACTION_DELAY kconfig.
By default it has been set to 500 ms.
  • Loading branch information
ArekBalysNordic authored and pull[bot] committed Nov 14, 2023
1 parent a05d0d4 commit 3915838
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 35 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 @@ -267,4 +267,13 @@ choice CHIP_LAST_FABRIC_REMOVED_ACTION

endchoice

config CHIP_LAST_FABRIC_REMOVED_ACTION_DELAY
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
3 changes: 0 additions & 3 deletions examples/all-clusters-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@

struct k_timer;
struct Identify;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance(void)
{
static AppTask sAppTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@

struct k_timer;
struct Identify;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance(void)
{
static AppTask sAppTask;
Expand Down
3 changes: 0 additions & 3 deletions examples/light-switch-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@

struct k_timer;
struct Identify;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance()
{
static AppTask sAppTask;
Expand Down
3 changes: 0 additions & 3 deletions examples/lighting-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@

struct k_timer;
struct Identify;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance()
{
static AppTask sAppTask;
Expand Down
3 changes: 0 additions & 3 deletions examples/lock-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@

struct k_timer;
struct Identify;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance()
{
static AppTask sAppTask;
Expand Down
33 changes: 22 additions & 11 deletions examples/platform/nrfconnect/util/include/FabricTableDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#pragma once

#include "AppTask.h"

#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#ifdef CONFIG_CHIP_WIFI
Expand All @@ -42,36 +40,49 @@ 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(&sFabricRemovedTimer, &OnFabricRemovedTimerCallback, nullptr);
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
}

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

static void OnFabricRemovedTimerCallback(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 */
// Erase Matter data
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();
/* Erase Network credentials and disconnect */
// Erase Network credentials and disconnect
chip::DeviceLayer::ConnectivityMgr().ErasePersistentInfo();
#ifdef CONFIG_CHIP_WIFI
chip::DeviceLayer::WiFiManager::Instance().Disconnect();
chip::DeviceLayer::ConnectivityMgr().ClearWiFiStationProvision();
#endif
#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START
/* Start the New BLE advertising */
AppEvent event;
event.Handler = AppTask::StartBLEAdvertisementHandler;
AppTask::Instance().PostEvent(event);
// Start the New BLE advertising
if (!chip::DeviceLayer::ConnectivityMgr().IsBLEAdvertisingEnabled())
{
if (CHIP_NO_ERROR == chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow())
{
return;
}
}
ChipLogError(FabricProvisioning, "Could not start Bluetooth LE advertising");
#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 sFabricRemovedTimer;
};
3 changes: 0 additions & 3 deletions examples/pump-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@
#endif

struct k_timer;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance(void)
{
static AppTask sAppTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@
#endif

struct k_timer;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance(void)
{
static AppTask sAppTask;
Expand Down
3 changes: 0 additions & 3 deletions examples/window-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@

struct k_timer;
struct Identify;
class AppFabricTableDelegate;

class AppTask
{
public:
friend class AppFabricTableDelegate;

static AppTask & Instance(void)
{
static AppTask sAppTask;
Expand Down

0 comments on commit 3915838

Please sign in to comment.