From 070a7741f0ceb7cf56f6f75ce79e0144423c4c19 Mon Sep 17 00:00:00 2001 From: Arkadiusz Balys Date: Fri, 22 Sep 2023 14:40:18 +0200 Subject: [PATCH] Test for Removing Thread credentials after last fabric remove --- config/nrfconnect/chip-module/Kconfig | 28 ++++++++ examples/lock-app/nrfconnect/main/AppTask.cpp | 2 + .../nrfconnect/main/include/AppTask.h | 3 +- .../util/include/FabricTableDelegate.h | 65 +++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 examples/platform/nrfconnect/util/include/FabricTableDelegate.h diff --git a/config/nrfconnect/chip-module/Kconfig b/config/nrfconnect/chip-module/Kconfig index 95e49ac59e0262..3eec07d96149d2 100644 --- a/config/nrfconnect/chip-module/Kconfig +++ b/config/nrfconnect/chip-module/Kconfig @@ -99,6 +99,34 @@ config CHIP_DEBUG_SYMBOLS config CHIP_MALLOC_SYS_HEAP default y if !ARCH_POSIX +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_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 + config CHIP_FACTORY_DATA bool "Factory data provider" select ZCBOR diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index 3ef85657363043..25a23ddb80249c 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -19,6 +19,7 @@ #include "AppTask.h" #include "AppConfig.h" #include "BoltLockManager.h" +#include "FabricTableDelegate.h" #include "LEDUtil.h" #include "LEDWidget.h" @@ -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); diff --git a/examples/lock-app/nrfconnect/main/include/AppTask.h b/examples/lock-app/nrfconnect/main/include/AppTask.h index a0a50da9ed23db..8f901a925a6042 100644 --- a/examples/lock-app/nrfconnect/main/include/AppTask.h +++ b/examples/lock-app/nrfconnect/main/include/AppTask.h @@ -56,6 +56,8 @@ class AppTask static void IdentifyStartHandler(Identify *); static void IdentifyStopHandler(Identify *); + static void StartBLEAdvertisementHandler(const AppEvent & event); + private: CHIP_ERROR Init(); @@ -67,7 +69,6 @@ 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 ChipEventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg); diff --git a/examples/platform/nrfconnect/util/include/FabricTableDelegate.h b/examples/platform/nrfconnect/util/include/FabricTableDelegate.h new file mode 100644 index 00000000000000..f74139eb12229d --- /dev/null +++ b/examples/platform/nrfconnect/util/include/FabricTableDelegate.h @@ -0,0 +1,65 @@ +/* + * + * 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 +#include + +namespace chip { + +class AppFabricTableDelegate : public FabricTable::Delegate +{ +public: + ~AppFabricTableDelegate() { Server::GetInstance().GetFabricTable().RemoveFabricDelegate(this); } + + static void Init() + { + static AppFabricTableDelegate sAppFabricDelegate; + Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppFabricDelegate); + } + +private: + void OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) + { + 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) { + // Delete all fabrics and emit Leave event. + Server::GetInstance().GetFabricTable().DeleteAllFabrics(); + /* Erase Matter data */ + DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset(); + /* Erase Thread credentials and disconnect */ + DeviceLayer::ConnectivityMgr().ErasePersistentInfo(); +#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 + }); +#endif + } + } +}; +} // namespace chip