From ba58daf943e3be4cc7ab0aa510c885bf5e30ece0 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Mon, 24 Jan 2022 19:43:59 +0800 Subject: [PATCH] esp32: erase fabric information during factoryreset --- src/platform/ESP32/ConfigurationManagerImpl.cpp | 8 ++++++++ src/platform/ESP32/KeyValueStoreManagerImpl.cpp | 11 +++++++++++ src/platform/ESP32/KeyValueStoreManagerImpl.h | 2 ++ 3 files changed, 21 insertions(+) diff --git a/src/platform/ESP32/ConfigurationManagerImpl.cpp b/src/platform/ESP32/ConfigurationManagerImpl.cpp index 6dc84c130ea5b8..cbcb9afcde9303 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.cpp +++ b/src/platform/ESP32/ConfigurationManagerImpl.cpp @@ -286,6 +286,14 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg) #elif CHIP_DEVICE_CONFIG_ENABLE_THREAD ThreadStackMgr().ErasePersistentInfo(); #endif + + // Erase all key-values including fabric info. + err = PersistedStorage::KeyValueStoreMgrImpl().EraseAll(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Clear Key-Value Storage failed"); + } + // Restart the system. ChipLogProgress(DeviceLayer, "System restarting"); esp_restart(); diff --git a/src/platform/ESP32/KeyValueStoreManagerImpl.cpp b/src/platform/ESP32/KeyValueStoreManagerImpl.cpp index 86ecca7f3960dd..66a93c4c155b4f 100644 --- a/src/platform/ESP32/KeyValueStoreManagerImpl.cpp +++ b/src/platform/ESP32/KeyValueStoreManagerImpl.cpp @@ -87,6 +87,17 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) return CHIP_NO_ERROR; } +CHIP_ERROR KeyValueStoreManagerImpl::EraseAll(void) +{ + Internal::ScopedNvsHandle handle; + + ReturnErrorOnFailure(handle.Open(kNamespace, NVS_READWRITE)); + ReturnMappedErrorOnFailure(nvs_erase_all(handle)); + ReturnMappedErrorOnFailure(nvs_commit(handle)); + + return CHIP_NO_ERROR; +} + } // namespace PersistedStorage } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/KeyValueStoreManagerImpl.h b/src/platform/ESP32/KeyValueStoreManagerImpl.h index 3cd7b28066ac20..1509c59976d663 100644 --- a/src/platform/ESP32/KeyValueStoreManagerImpl.h +++ b/src/platform/ESP32/KeyValueStoreManagerImpl.h @@ -42,6 +42,8 @@ class KeyValueStoreManagerImpl final : public KeyValueStoreManager CHIP_ERROR _Put(const char * key, const void * value, size_t value_size); + CHIP_ERROR EraseAll(void); + private: const char * kNamespace = "CHIP_KVS";