From 9c8cb33d71b01470170614827ffc6a1754b99add Mon Sep 17 00:00:00 2001 From: wyhong <30567533+wy-hh@users.noreply.github.com> Date: Wed, 12 Feb 2025 04:04:00 +0800 Subject: [PATCH] [bouffalo lab] open commission window when last fabric is removed (#37517) * [bouffalo lab] open commission window when last fabric is removed * Restyled by whitespace * fix compile error --------- Co-authored-by: Restyled.io --- .../bouffalolab/common/plat/platform.cpp | 32 +++++++++++++++++++ .../BL616/ThreadStackManagerImpl.cpp | 6 ++++ .../BL702/ThreadStackManagerImpl.cpp | 6 ++++ .../BL702L/ThreadStackManagerImpl.cpp | 6 ++++ .../bouffalolab/common/BLConfig_littlefs.cpp | 4 +-- .../common/ConfigurationManagerImpl.cpp | 11 +++++++ .../common/ConfigurationManagerImpl.h | 4 +++ .../common/ThreadStackManagerImpl.h | 1 + 8 files changed, 68 insertions(+), 2 deletions(-) diff --git a/examples/platform/bouffalolab/common/plat/platform.cpp b/examples/platform/bouffalolab/common/plat/platform.cpp index 13b07ec0a3e992..9559641c458233 100644 --- a/examples/platform/bouffalolab/common/plat/platform.cpp +++ b/examples/platform/bouffalolab/common/plat/platform.cpp @@ -178,6 +178,35 @@ void UnlockOpenThreadTask(void) } #endif +class AppFabricTableDelegate : public FabricTable::Delegate +{ + void OnFabricRemoved(const FabricTable & fabricTable, FabricIndex fabricIndex) + { + if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0) + { + ChipLogProgress(DeviceLayer, "Performing erasing of settings partition"); + PlatformMgr().ScheduleWork([](intptr_t) { +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + ConfigurationManagerImpl::GetDefaultInstance().ClearThreadStack(); + ThreadStackMgrImpl().FactoryResetThreadStack(); + ThreadStackMgr().InitThreadStack(); +#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION + ChipLogProgress(DeviceLayer, "Clearing WiFi provision"); + chip::DeviceLayer::ConnectivityMgr().ClearWiFiStationProvision(); +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION + + CHIP_ERROR err = Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(AppServer, "Failed to open the Basic Commissioning Window"); + } + }); + } + } +}; + CHIP_ERROR PlatformManagerImpl::PlatformInit(void) { chip::RendezvousInformationFlags rendezvousMode(chip::RendezvousInformationFlag::kOnNetwork); @@ -265,6 +294,9 @@ CHIP_ERROR PlatformManagerImpl::PlatformInit(void) gExampleDeviceInfoProvider.SetStorageDelegate(&chip::Server::GetInstance().GetPersistentStorage()); + static AppFabricTableDelegate sAppFabricDelegate; + chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&sAppFabricDelegate); + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); #if CHIP_DEVICE_CONFIG_ENABLE_THREAD diff --git a/src/platform/bouffalolab/BL616/ThreadStackManagerImpl.cpp b/src/platform/bouffalolab/BL616/ThreadStackManagerImpl.cpp index 251ccf5113a294..17d73f9ea2309e 100644 --- a/src/platform/bouffalolab/BL616/ThreadStackManagerImpl.cpp +++ b/src/platform/bouffalolab/BL616/ThreadStackManagerImpl.cpp @@ -63,6 +63,12 @@ bool ThreadStackManagerImpl::IsInitialized() return sInstance.mThreadStackLock != NULL; } +void ThreadStackManagerImpl::FactoryResetThreadStack(void) +{ + VerifyOrReturn(ThreadStackMgrImpl().OTInstance() != NULL); + otInstanceFactoryReset(ThreadStackMgrImpl().OTInstance()); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp index 784dfa396cd371..a2072eff6be32d 100644 --- a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp @@ -65,6 +65,12 @@ bool ThreadStackManagerImpl::IsInitialized() return sInstance.mThreadStackLock != NULL; } +void ThreadStackManagerImpl::FactoryResetThreadStack(void) +{ + VerifyOrReturn(ThreadStackMgrImpl().OTInstance() != NULL); + otInstanceFactoryReset(ThreadStackMgrImpl().OTInstance()); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp b/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp index 784dfa396cd371..a2072eff6be32d 100644 --- a/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702L/ThreadStackManagerImpl.cpp @@ -65,6 +65,12 @@ bool ThreadStackManagerImpl::IsInitialized() return sInstance.mThreadStackLock != NULL; } +void ThreadStackManagerImpl::FactoryResetThreadStack(void) +{ + VerifyOrReturn(ThreadStackMgrImpl().OTInstance() != NULL); + otInstanceFactoryReset(ThreadStackMgrImpl().OTInstance()); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/bouffalolab/common/BLConfig_littlefs.cpp b/src/platform/bouffalolab/common/BLConfig_littlefs.cpp index 7f5e221195e8d3..84b7db21ead7b2 100644 --- a/src/platform/bouffalolab/common/BLConfig_littlefs.cpp +++ b/src/platform/bouffalolab/common/BLConfig_littlefs.cpp @@ -278,7 +278,7 @@ CHIP_ERROR BLConfig::WriteConfigValue(const char * key, uint8_t * val, size_t si blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_file_open(blconfig_lfs, &file, write_key, LFS_O_CREAT | LFS_O_RDWR); + ret = lfs_file_open(blconfig_lfs, &file, write_key, LFS_O_CREAT | LFS_O_RDWR | LFS_O_TRUNC); VerifyOrExit(ret == LFS_ERR_OK, err = CHIP_ERROR_PERSISTED_STORAGE_FAILED); lfs_file_write(blconfig_lfs, &file, val, size); @@ -353,7 +353,7 @@ CHIP_ERROR BLConfig::FactoryResetConfig(void) blconfig_lfs->cfg->lock(blconfig_lfs->cfg); - ret = lfs_file_open(blconfig_lfs, &file, reset_key, LFS_O_CREAT | LFS_O_RDWR); + ret = lfs_file_open(blconfig_lfs, &file, reset_key, LFS_O_CREAT | LFS_O_RDWR | LFS_O_TRUNC); if (ret != LFS_ERR_OK) { blconfig_lfs->cfg->unlock(blconfig_lfs->cfg); diff --git a/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp b/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp index 176803d00a1ebe..39d535aba6ade1 100644 --- a/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp +++ b/src/platform/bouffalolab/common/ConfigurationManagerImpl.cpp @@ -190,6 +190,17 @@ void ConfigurationManagerImpl::RunConfigUnitTest(void) BLConfig::RunConfigUnitTest(); } +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +void ConfigurationManagerImpl::ClearThreadStack() +{ +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + ThreadStackMgr().ClearAllSrpHostAndServices(); +#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + ChipLogProgress(DeviceLayer, "Clearing Thread provision"); + ThreadStackMgr().ErasePersistentInfo(); +} +#endif + void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg) { CHIP_ERROR err; diff --git a/src/platform/bouffalolab/common/ConfigurationManagerImpl.h b/src/platform/bouffalolab/common/ConfigurationManagerImpl.h index 1e7bb53922eb79..765d6b53b1a904 100644 --- a/src/platform/bouffalolab/common/ConfigurationManagerImpl.h +++ b/src/platform/bouffalolab/common/ConfigurationManagerImpl.h @@ -38,6 +38,10 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp CHIP_ERROR StoreTotalOperationalHours(uint32_t totalOperationalHours); bool IsFullyProvisioned(); +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + void ClearThreadStack(); +#endif + private: // ===== Members that implement the ConfigurationManager private interface. diff --git a/src/platform/bouffalolab/common/ThreadStackManagerImpl.h b/src/platform/bouffalolab/common/ThreadStackManagerImpl.h index 462d2de76ac32d..cd743bf93848eb 100644 --- a/src/platform/bouffalolab/common/ThreadStackManagerImpl.h +++ b/src/platform/bouffalolab/common/ThreadStackManagerImpl.h @@ -65,6 +65,7 @@ class ThreadStackManagerImpl final : public ThreadStackManager, using ThreadStackManager::InitThreadStack; CHIP_ERROR InitThreadStack(otInstance * otInst); + void FactoryResetThreadStack(void); private: // ===== Methods that implement the ThreadStackManager abstract interface.