Skip to content

Commit 1226734

Browse files
ArekBalysNordicpull[bot]
authored andcommitted
[nrfconnect] Add a delay to last fabric remove action (#29844)
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.
1 parent c5935a7 commit 1226734

File tree

10 files changed

+31
-35
lines changed

10 files changed

+31
-35
lines changed

config/nrfconnect/chip-module/Kconfig.features

+9
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,13 @@ choice CHIP_LAST_FABRIC_REMOVED_ACTION
267267

268268
endchoice
269269

270+
config CHIP_LAST_FABRIC_REMOVED_ACTION_DELAY
271+
int "After removing the last fabric wait defined time [in milliseconds] to perform an action"
272+
depends on !CHIP_LAST_FABRIC_REMOVED_NONE
273+
default 500
274+
help
275+
After removing the last fabric the device will wait for the defined time and then perform
276+
an action chosen by the CHIP_LAST_FABRIC_REMOVED_ACTION option. This schedule will allow for
277+
avoiding race conditions before the device removes non-volatile data.
278+
270279
endif # CHIP

examples/all-clusters-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@
3434

3535
struct k_timer;
3636
struct Identify;
37-
class AppFabricTableDelegate;
3837

3938
class AppTask
4039
{
4140
public:
42-
friend class AppFabricTableDelegate;
43-
4441
static AppTask & Instance(void)
4542
{
4643
static AppTask sAppTask;

examples/all-clusters-minimal-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@
3434

3535
struct k_timer;
3636
struct Identify;
37-
class AppFabricTableDelegate;
3837

3938
class AppTask
4039
{
4140
public:
42-
friend class AppFabricTableDelegate;
43-
4441
static AppTask & Instance(void)
4542
{
4643
static AppTask sAppTask;

examples/light-switch-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@
3737

3838
struct k_timer;
3939
struct Identify;
40-
class AppFabricTableDelegate;
4140

4241
class AppTask
4342
{
4443
public:
45-
friend class AppFabricTableDelegate;
46-
4744
static AppTask & Instance()
4845
{
4946
static AppTask sAppTask;

examples/lighting-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@
4242

4343
struct k_timer;
4444
struct Identify;
45-
class AppFabricTableDelegate;
4645

4746
class AppTask
4847
{
4948
public:
50-
friend class AppFabricTableDelegate;
51-
5249
static AppTask & Instance()
5350
{
5451
static AppTask sAppTask;

examples/lock-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@
3737

3838
struct k_timer;
3939
struct Identify;
40-
class AppFabricTableDelegate;
4140

4241
class AppTask
4342
{
4443
public:
45-
friend class AppFabricTableDelegate;
46-
4744
static AppTask & Instance()
4845
{
4946
static AppTask sAppTask;

examples/platform/nrfconnect/util/include/FabricTableDelegate.h

+22-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#pragma once
2020

21-
#include "AppTask.h"
22-
2321
#include <app/server/Server.h>
2422
#include <app/util/attribute-storage.h>
2523
#ifdef CONFIG_CHIP_WIFI
@@ -42,36 +40,49 @@ class AppFabricTableDelegate : public chip::FabricTable::Delegate
4240
#ifndef CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
4341
static AppFabricTableDelegate sAppFabricDelegate;
4442
chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppFabricDelegate);
43+
k_timer_init(&sFabricRemovedTimer, &OnFabricRemovedTimerCallback, nullptr);
4544
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
4645
}
4746

4847
private:
4948
void OnFabricRemoved(const chip::FabricTable & fabricTable, chip::FabricIndex fabricIndex)
49+
{
50+
k_timer_start(&sFabricRemovedTimer, K_MSEC(CONFIG_CHIP_LAST_FABRIC_REMOVED_ACTION_DELAY), K_NO_WAIT);
51+
}
52+
53+
static void OnFabricRemovedTimerCallback(k_timer * timer)
5054
{
5155
#ifndef CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
5256
if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0)
5357
{
58+
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) {
5459
#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
55-
chip::Server::GetInstance().ScheduleFactoryReset();
60+
chip::Server::GetInstance().ScheduleFactoryReset();
5661
#elif defined(CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_ONLY) || defined(CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START)
57-
chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) {
58-
/* Erase Matter data */
62+
// Erase Matter data
5963
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();
60-
/* Erase Network credentials and disconnect */
64+
// Erase Network credentials and disconnect
6165
chip::DeviceLayer::ConnectivityMgr().ErasePersistentInfo();
6266
#ifdef CONFIG_CHIP_WIFI
6367
chip::DeviceLayer::WiFiManager::Instance().Disconnect();
6468
chip::DeviceLayer::ConnectivityMgr().ClearWiFiStationProvision();
6569
#endif
6670
#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START
67-
/* Start the New BLE advertising */
68-
AppEvent event;
69-
event.Handler = AppTask::StartBLEAdvertisementHandler;
70-
AppTask::Instance().PostEvent(event);
71+
// Start the New BLE advertising
72+
if (!chip::DeviceLayer::ConnectivityMgr().IsBLEAdvertisingEnabled())
73+
{
74+
if (CHIP_NO_ERROR == chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow())
75+
{
76+
return;
77+
}
78+
}
79+
ChipLogError(FabricProvisioning, "Could not start Bluetooth LE advertising");
7180
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START
72-
});
7381
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
82+
});
7483
}
7584
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_NONE
7685
}
86+
87+
inline static k_timer sFabricRemovedTimer;
7788
};

examples/pump-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@
3636
#endif
3737

3838
struct k_timer;
39-
class AppFabricTableDelegate;
4039

4140
class AppTask
4241
{
4342
public:
44-
friend class AppFabricTableDelegate;
45-
4643
static AppTask & Instance(void)
4744
{
4845
static AppTask sAppTask;

examples/pump-controller-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@
3636
#endif
3737

3838
struct k_timer;
39-
class AppFabricTableDelegate;
4039

4140
class AppTask
4241
{
4342
public:
44-
friend class AppFabricTableDelegate;
45-
4643
static AppTask & Instance(void)
4744
{
4845
static AppTask sAppTask;

examples/window-app/nrfconnect/main/include/AppTask.h

-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@
3434

3535
struct k_timer;
3636
struct Identify;
37-
class AppFabricTableDelegate;
3837

3938
class AppTask
4039
{
4140
public:
42-
friend class AppFabricTableDelegate;
43-
4441
static AppTask & Instance(void)
4542
{
4643
static AppTask sAppTask;

0 commit comments

Comments
 (0)