From c3b67660b4419b9ed402afb2f16d3729db06e585 Mon Sep 17 00:00:00 2001 From: Misha Tkachenko Date: Wed, 24 May 2023 11:17:45 +0300 Subject: [PATCH 1/7] [Telink] Added storage reset in case of fabric count == 0 Signed-off-by: Misha Tkachenko --- .../telink/common/src/AppTaskCommon.cpp | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index c6e455b611dc07..63867f493609c0 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -33,6 +33,11 @@ #include "OTAUtil.h" #endif +#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS +#include +#include +#endif + LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL); namespace { @@ -111,7 +116,37 @@ class AppFabricTableDelegate : public FabricTable::Delegate { if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0) { - chip::Server::GetInstance().ScheduleFactoryReset(); + // chip::Server::GetInstance().ScheduleFactoryReset(); + 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) + { + status = nvs_clear(static_cast(storage)); + } + + if (!status) + { + status = nvs_mount(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()); + } + + ConnectivityMgr().ErasePersistentInfo(); + #endif } } }; From 4caa8b84f89ab3f584a3ff5fa64fec3c84f2d0a7 Mon Sep 17 00:00:00 2001 From: Misha Tkachenko Date: Wed, 24 May 2023 11:28:10 +0300 Subject: [PATCH 2/7] [Telink] Disable openthread before reset to prevent writing to NVS Signed-off-by: Misha Tkachenko --- .../platform/telink/common/src/AppTaskCommon.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index 63867f493609c0..cb244e2a2af97d 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -151,6 +151,18 @@ class AppFabricTableDelegate : public FabricTable::Delegate } }; +class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate +{ + void OnShutDown() override + { + if (ThreadStackManagerImpl().IsThreadEnabled()) + { + ThreadStackManagerImpl().SetThreadEnabled(false); + ThreadStackManagerImpl().SetRadioBlocked(true); + } + } +}; + #if CONFIG_CHIP_LIB_SHELL #include #include @@ -264,6 +276,10 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void) emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); #endif + // We need to disable OpenThread to prevent writing to the NVS storage when factory reset occurs + // For this the OnShutdown function is used + PlatformMgr().SetDelegate(new PlatformMgrDelegate); + // Add CHIP event handler and start CHIP thread. // Note that all the initialization code should happen prior to this point to avoid data races // between the main and the CHIP threads. From 03b33597b49999cf8fbc76dddeb61ada0dc18477 Mon Sep 17 00:00:00 2001 From: Alex Tsitsiura Date: Wed, 24 May 2023 11:33:38 +0300 Subject: [PATCH 3/7] [Telink] remove commented code --- examples/platform/telink/common/src/AppTaskCommon.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index cb244e2a2af97d..338b931190310a 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -116,7 +116,6 @@ class AppFabricTableDelegate : public FabricTable::Delegate { if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0) { - // chip::Server::GetInstance().ScheduleFactoryReset(); ChipLogProgress(DeviceLayer, "Performing erasing of settings partition"); #ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS From 9b8341f7c037ebb2908a0f513647dd1444746edb Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 24 May 2023 08:39:16 +0000 Subject: [PATCH 4/7] Restyled by clang-format --- .../telink/common/src/AppTaskCommon.cpp | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index 338b931190310a..bc863a522868e7 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -118,34 +118,34 @@ class AppFabricTableDelegate : public FabricTable::Delegate { 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) - { - status = nvs_clear(static_cast(storage)); - } - - if (!status) - { - status = nvs_mount(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()); - } - - ConnectivityMgr().ErasePersistentInfo(); - #endif +#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS + void * storage = nullptr; + int status = settings_storage_get(&storage); + + if (status == 0) + { + status = nvs_clear(static_cast(storage)); + } + + if (!status) + { + status = nvs_mount(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()); + } + + ConnectivityMgr().ErasePersistentInfo(); +#endif } } }; From 2222b744c87bc89614af153de6d53db946d460d2 Mon Sep 17 00:00:00 2001 From: Misha Tkachenko Date: Thu, 25 May 2023 12:06:15 +0300 Subject: [PATCH 5/7] [Telink]: Added a clear explanation for preventing NVS writing on shutdown Signed-off-by: Misha Tkachenko --- examples/platform/telink/common/src/AppTaskCommon.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index ad6e27afe8745b..b0419482fef164 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -285,6 +285,11 @@ CHIP_ERROR AppTaskCommon::InitCommonParts(void) #endif // We need to disable OpenThread to prevent writing to the NVS storage when factory reset occurs + // The OpenThread thread is running during factory reset. The nvs_clear function is called during + // factory reset, which makes the NVS storage innaccessible, but the OpenThread knows nothing + // about this and tries to store the parameters to NVS. Because of this the OpenThread need to be + // shut down before NVS. This delegate fixes the issue "Failed to store setting , ret -13", + // which means that the NVS is already disabled. // For this the OnShutdown function is used PlatformMgr().SetDelegate(new PlatformMgrDelegate); From dc7ef87248d0fd208fa8ef3503dbb69bee559dde Mon Sep 17 00:00:00 2001 From: Misha Tkachenko Date: Thu, 25 May 2023 23:17:30 +0300 Subject: [PATCH 6/7] [Telink]: Updated OpenThread finalization function Signed-off-by: Misha Tkachenko --- examples/platform/telink/common/src/AppTaskCommon.cpp | 3 +-- src/platform/telink/ThreadStackManagerImpl.cpp | 5 +++++ src/platform/telink/ThreadStackManagerImpl.h | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/platform/telink/common/src/AppTaskCommon.cpp b/examples/platform/telink/common/src/AppTaskCommon.cpp index b0419482fef164..dcf86b872b0734 100644 --- a/examples/platform/telink/common/src/AppTaskCommon.cpp +++ b/examples/platform/telink/common/src/AppTaskCommon.cpp @@ -161,8 +161,7 @@ class PlatformMgrDelegate : public DeviceLayer::PlatformManagerDelegate { if (ThreadStackManagerImpl().IsThreadEnabled()) { - ThreadStackManagerImpl().SetThreadEnabled(false); - ThreadStackManagerImpl().SetRadioBlocked(true); + ThreadStackManagerImpl().Finalize(); } } }; diff --git a/src/platform/telink/ThreadStackManagerImpl.cpp b/src/platform/telink/ThreadStackManagerImpl.cpp index 09ee1ab68b666e..a8f0a4d5f18b8b 100644 --- a/src/platform/telink/ThreadStackManagerImpl.cpp +++ b/src/platform/telink/ThreadStackManagerImpl.cpp @@ -112,5 +112,10 @@ ThreadStackManagerImpl::_AttachToThreadNetwork(const Thread::OperationalDataset return result; } +void ThreadStackManagerImpl::Finalize(void) +{ + otInstanceFinalize(openthread_get_default_instance()); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/telink/ThreadStackManagerImpl.h b/src/platform/telink/ThreadStackManagerImpl.h index c598af1e7501a0..51b8d24601bc22 100644 --- a/src/platform/telink/ThreadStackManagerImpl.h +++ b/src/platform/telink/ThreadStackManagerImpl.h @@ -64,6 +64,7 @@ class ThreadStackManagerImpl final : public ThreadStackManager, CHIP_ERROR _InitThreadStack(); void SetRadioBlocked(bool state) { mRadioBlocked = state; } bool IsReadyToAttach(void) const { return mReadyToAttach; } + void Finalize(void); protected: // ===== Methods that implement the ThreadStackManager abstract interface. From c36afeba6fa89d4d235dfef800f098f3f57f3026 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 25 May 2023 20:24:02 +0000 Subject: [PATCH 7/7] Restyled by whitespace --- src/platform/telink/ThreadStackManagerImpl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/telink/ThreadStackManagerImpl.cpp b/src/platform/telink/ThreadStackManagerImpl.cpp index 70b74c08c0d0f2..e66735b1de68f2 100644 --- a/src/platform/telink/ThreadStackManagerImpl.cpp +++ b/src/platform/telink/ThreadStackManagerImpl.cpp @@ -135,7 +135,7 @@ CHIP_ERROR ThreadStackManagerImpl::_StartThreadScan(NetworkCommissioning::Thread return CHIP_NO_ERROR; } - + void ThreadStackManagerImpl::Finalize(void) { otInstanceFinalize(openthread_get_default_instance());