From 105093278c2e6c3e8622a9e041458fd72e565793 Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Sat, 2 Sep 2023 02:55:08 +0300 Subject: [PATCH] [Telink] Turn off the CHIP_FACTORY_RESET_ERASE_NVS by Default (#28947) --- config/telink/chip-module/Kconfig | 2 +- .../telink/common/src/AppTaskCommon.cpp | 60 ++++++++----------- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig index fcd205102f22aa..325eb4172f198c 100644 --- a/config/telink/chip-module/Kconfig +++ b/config/telink/chip-module/Kconfig @@ -147,7 +147,7 @@ endif #CHIP_FACTORY_DATA_BUILD # See config/zephyr/Kconfig for full definition config CHIP_FACTORY_RESET_ERASE_NVS bool - default y + default n config CHIP_LOG_SIZE_OPTIMIZATION bool "Disable some detailed logs to decrease flash usage" diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index a64e2c7d5bf2e2..3a831ea0ef8fce 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -39,10 +39,8 @@ #include #endif -#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS #include #include -#endif using namespace chip::app; @@ -103,10 +101,11 @@ Button sThreadStartButton; k_timer sFactoryResetTimer; uint8_t sFactoryResetCntr = 0; -bool sIsThreadProvisioned = false; -bool sIsThreadEnabled = false; -bool sIsThreadAttached = false; -bool sHaveBLEConnections = false; +bool sIsCommissioningFailed = false; +bool sIsThreadProvisioned = false; +bool sIsThreadEnabled = false; +bool sIsThreadAttached = false; +bool sHaveBLEConnections = false; #if APP_SET_DEVICE_INFO_PROVIDER chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider; @@ -138,9 +137,10 @@ class AppCallbacks : public AppDelegate bool isComissioningStarted; public: - void OnCommissioningSessionEstablishmentStarted() {} + void OnCommissioningSessionEstablishmentStarted() override { sIsCommissioningFailed = false; } void OnCommissioningSessionStarted() override { isComissioningStarted = true; } void OnCommissioningSessionStopped() override { isComissioningStarted = false; } + void OnCommissioningSessionEstablishmentError(CHIP_ERROR err) override { sIsCommissioningFailed = true; } void OnCommissioningWindowClosed() override { if (!isComissioningStarted) @@ -157,44 +157,33 @@ class AppFabricTableDelegate : public FabricTable::Delegate { if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0) { - bool isBasicCommissioningMode = chip::Server::GetInstance().GetCommissioningWindowManager().GetCommissioningMode() == - Dnssd::CommissioningMode::kEnabledBasic; ChipLogProgress(DeviceLayer, "Performing erasing of settings partition"); -#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS - void * storage = nullptr; - int status = settings_storage_get(&storage); - - if (status == 0) + // Do FactoryReset in case of failed commissioning to allow new pairing via BLE + if (sIsCommissioningFailed) { - status = nvs_clear(static_cast(storage)); + chip::Server::GetInstance().ScheduleFactoryReset(); } - - if (!isBasicCommissioningMode) + // TC-OPCREDS-3.6 (device doesn't need to reboot automatically after the last fabric is removed) can't use FactoryReset + else { + void * storage = nullptr; + int status = settings_storage_get(&storage); + if (!status) { - status = nvs_mount(static_cast(storage)); + status = nvs_clear(static_cast(storage)); } - } - - if (status) - { - ChipLogError(DeviceLayer, "Storage clearance failed: %d", status); - } -#else - const CHIP_ERROR err = PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset(); - if (err != CHIP_NO_ERROR) - { - ChipLogError(DeviceLayer, "Factory reset failed: %" CHIP_ERROR_FORMAT, err.Format()); - } + if (!status) + { + status = nvs_mount(static_cast(storage)); + } - ConnectivityMgr().ErasePersistentInfo(); -#endif - if (isBasicCommissioningMode) - { - PlatformMgr().Shutdown(); + if (status) + { + ChipLogError(DeviceLayer, "Storage clearance failed: %d", status); + } } } } @@ -202,6 +191,7 @@ class AppFabricTableDelegate : public FabricTable::Delegate class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate { + // Disable openthread before reset to prevent writing to NVS void OnShutDown() override { if (ThreadStackManagerImpl().IsThreadEnabled())