Skip to content

Commit

Permalink
[nrfconnect] Added erasing network credentials after last fabric removal
Browse files Browse the repository at this point in the history
Users can decide now what should happen after removing the last fabric:
- Do nothing.
- Erase the saved network credentials (Thread/WI-FI).
- Erase the saved network credentials and start Bluetooth LE advertising.
- Erase the saved network credentials and reboot the device.
  • Loading branch information
ArekBalysNordic committed Sep 26, 2023
1 parent 6bb0b71 commit 773900b
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 1 deletion.
35 changes: 35 additions & 0 deletions config/nrfconnect/chip-module/Kconfig.features
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,39 @@ config CHIP_WIFI_CONNECTION_RECOVERY_JITTER
a random jitter interval is added to it to avoid periodicity. The random jitter is selected
within range [-JITTER; +JITTER].

choice CHIP_LAST_FABRIC_REMOVED_ACTION
prompt "An action to perform after removing the last fabric"
default CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT

config CHIP_LAST_FABRIC_REMOVED_NONE
bool "After removing the last fabric do not perform any action"
help
After removing the last fabric the device will not perform factory reset
or reboot. The current state will be left as it is and the BLE advertising
will not start automatically.

config CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
bool "After removing the last fabric erase NVS and reboot"
help
After removing the last fabric the device will perform the factory reset and
then reboot. The current RAM state will be removed and the new commissioning to
the new fabric will use the initial fabric index. This option is the most safe.

config CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START
bool "After removing the last fabric erase NVS and start Bluetooth LE advertising"
help
After removing the last fabric the device will perform the factory reset without
rebooting and start the Bluetooth LE advertisement automatically.
The current RAM state will be saved and the new commissioning to the next
fabric will use the next possible fabric index.

config CHIP_LAST_FABRIC_REMOVED_ERASE_ONLY
bool "After removing the last fabric erase NVS only"
help
After removing the last fabric the device will perform the factory reset only without
rebooting. The current RAM state will be saved and the new commissioning to the next
fabric will use the next possible fabric index.

endchoice

endif # CHIP
2 changes: 2 additions & 0 deletions examples/all-clusters-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "binding-handler.h"

Expand Down Expand Up @@ -207,6 +208,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
2 changes: 2 additions & 0 deletions examples/all-clusters-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct Identify;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance(void)
{
Expand Down
2 changes: 2 additions & 0 deletions examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "binding-handler.h"

Expand Down Expand Up @@ -156,6 +157,7 @@ CHIP_ERROR AppTask::Init()
static chip::CommonCaseDeviceServerInitParams initParams;
(void) initParams.InitializeStaticResourcesBeforeServerInit();
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

// We only have network commissioning on endpoint 0.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct Identify;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance(void)
{
Expand Down
2 changes: 2 additions & 0 deletions examples/light-switch-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "BoardUtil.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "LightSwitch.h"

Expand Down Expand Up @@ -218,6 +219,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
2 changes: 2 additions & 0 deletions examples/light-switch-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct Identify;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance()
{
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "AppConfig.h"
#include "AppEvent.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "PWMDevice.h"

Expand Down Expand Up @@ -246,6 +247,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct Identify;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance()
{
Expand Down
2 changes: 2 additions & 0 deletions examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "BoltLockManager.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "LEDWidget.h"

Expand Down Expand Up @@ -212,6 +213,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
4 changes: 3 additions & 1 deletion examples/lock-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct Identify;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance()
{
Expand Down Expand Up @@ -67,8 +69,8 @@ class AppTask
static void FunctionHandler(const AppEvent & event);
static void StartBLEAdvertisementAndLockActionEventHandler(const AppEvent & event);
static void LockActionEventHandler(const AppEvent & event);
static void StartBLEAdvertisementHandler(const AppEvent & event);
static void UpdateLedStateEventHandler(const AppEvent & event);
static void StartBLEAdvertisementHandler(const AppEvent & event);

static void ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
static void ButtonEventHandler(uint32_t buttonState, uint32_t hasChanged);
Expand Down
74 changes: 74 additions & 0 deletions examples/platform/nrfconnect/util/include/FabricTableDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "AppTask.h"

#include <app/server/Server.h>
#include <app/util/attribute-storage.h>

namespace chip {

class AppFabricTableDelegate : public FabricTable::Delegate
{
public:
~AppFabricTableDelegate() { Server::GetInstance().GetFabricTable().RemoveFabricDelegate(this); }

/**
* @brief Initialize module and add a delegation to the Fabric Table.
*
* To use the OnFabricRemoved method defined within this class and allow to react on the last fabric removal
* this method should be called in the application code.
*/
static void Init()
{
#ifndef CONFIG_CHIP_LAST_FABRIC_NONE
static AppFabricTableDelegate sAppFabricDelegate;
Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppFabricDelegate);
#endif // CONFIG_CHIP_LAST_FABRIC_NONE
}

private:
void OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex)
{
#ifndef CONFIG_CHIP_LAST_FABRIC_NONE
if (Server::GetInstance().GetFabricTable().FabricCount() == 0)
{
#ifdef CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
Server::GetInstance().ScheduleFactoryReset();
#elif defined(CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_ONLY) || defined(CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START)
DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) {
/* Erase Matter data */
DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset();
/* Erase Network credentials and disconnect */
DeviceLayer::ConnectivityMgr().ErasePersistentInfo();
DeviceLayer::ConnectivityMgr().ClearWiFiStationProvision();
#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);
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_PAIRING_START
});
#endif // CONFIG_CHIP_LAST_FABRIC_REMOVED_ERASE_AND_REBOOT
}
#endif // CONFIG_CHIP_LAST_FABRIC_NONE
}
};
} // namespace chip
2 changes: 2 additions & 0 deletions examples/pump-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "AppTask.h"
#include "AppConfig.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "LEDWidget.h"
#include "PumpManager.h"
Expand Down Expand Up @@ -189,6 +190,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
2 changes: 2 additions & 0 deletions examples/pump-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct k_timer;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance(void)
{
Expand Down
2 changes: 2 additions & 0 deletions examples/pump-controller-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "AppTask.h"
#include "AppConfig.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "LEDWidget.h"
#include "PumpManager.h"
Expand Down Expand Up @@ -187,6 +188,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct k_timer;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance(void)
{
Expand Down
2 changes: 2 additions & 0 deletions examples/window-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "FabricTableDelegate.h"
#include "LEDUtil.h"
#include "WindowCovering.h"

Expand Down Expand Up @@ -191,6 +192,7 @@ CHIP_ERROR AppTask::Init()
(void) initParams.InitializeStaticResourcesBeforeServerInit();
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams));
AppFabricTableDelegate::Init();

gExampleDeviceInfoProvider.SetStorageDelegate(&Server::GetInstance().GetPersistentStorage());
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
Expand Down
2 changes: 2 additions & 0 deletions examples/window-app/nrfconnect/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct Identify;

class AppTask
{
friend class FabricTableDelegate;

public:
static AppTask & Instance(void)
{
Expand Down

0 comments on commit 773900b

Please sign in to comment.