From 7532ad4519e0be4bb35b2cff836258a3988a8319 Mon Sep 17 00:00:00 2001 From: Damian Krolik Date: Thu, 7 Jul 2022 14:08:15 +0200 Subject: [PATCH] [zephyr] Fix factory recent after recent KVS changes I forgot to update the factory reset routine after adding escaping of forbidden characters in KVS keys and the routine is escaping such keys twice which results in a failure. Signed-off-by: Damian Krolik --- .../Zephyr/KeyValueStoreManagerImpl.cpp | 25 +++++++++++++------ src/platform/tests/TestKeyValueStoreMgr.cpp | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/platform/Zephyr/KeyValueStoreManagerImpl.cpp b/src/platform/Zephyr/KeyValueStoreManagerImpl.cpp index 35c205db549815..da254e615263bc 100644 --- a/src/platform/Zephyr/KeyValueStoreManagerImpl.cpp +++ b/src/platform/Zephyr/KeyValueStoreManagerImpl.cpp @@ -44,7 +44,7 @@ struct ReadEntry struct DeleteSubtreeEntry { - CHIP_ERROR result; + int result; }; // Random magic bytes to represent an empty value. @@ -128,11 +128,16 @@ int DeleteSubtreeCallback(const char * name, size_t /* entrySize */, settings_re void * param) { DeleteSubtreeEntry & entry = *static_cast(param); - const CHIP_ERROR error = KeyValueStoreMgr().Delete(name); + char fullKey[SETTINGS_MAX_NAME_LEN + 1]; + + // name comes from Zephyr settings subsystem so it is guaranteed to fit in the buffer. + (void) snprintf(fullKey, sizeof(fullKey), CHIP_DEVICE_CONFIG_SETTINGS_KEY "/%s", name); + const int result = settings_delete(fullKey); - if (entry.result == CHIP_NO_ERROR) + // Return the first error, but continue removing remaining keys anyway. + if (entry.result == 0) { - entry.result = error; + entry.result = result; } return 0; @@ -199,11 +204,15 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) CHIP_ERROR KeyValueStoreManagerImpl::DoFactoryReset() { - DeleteSubtreeEntry entry{ CHIP_NO_ERROR }; - const int result = settings_load_subtree_direct(CHIP_DEVICE_CONFIG_SETTINGS_KEY, DeleteSubtreeCallback, &entry); + DeleteSubtreeEntry entry{ /* success */ 0 }; + int result = settings_load_subtree_direct(CHIP_DEVICE_CONFIG_SETTINGS_KEY, DeleteSubtreeCallback, &entry); - VerifyOrReturnError(result == 0, System::MapErrorZephyr(result)); - return entry.result; + if (result == 0) + { + result = entry.result; + } + + return System::MapErrorZephyr(result); } } // namespace PersistedStorage diff --git a/src/platform/tests/TestKeyValueStoreMgr.cpp b/src/platform/tests/TestKeyValueStoreMgr.cpp index acb3e5533e8a3a..ec2c35723a043c 100644 --- a/src/platform/tests/TestKeyValueStoreMgr.cpp +++ b/src/platform/tests/TestKeyValueStoreMgr.cpp @@ -287,7 +287,7 @@ static void TestKeyValueStoreMgr_MultiRead(nlTestSuite * inSuite, void * inConte #ifdef __ZEPHYR__ static void TestKeyValueStoreMgr_DoFactoryReset(nlTestSuite * inSuite, void * inContext) { - constexpr const char * kStrKey = "some_string_key"; + constexpr const char * kStrKey = "string_with_weird_chars\\=_key"; constexpr const char * kUintKey = "some_uint_key"; NL_TEST_ASSERT(inSuite, KeyValueStoreMgr().Put(kStrKey, "some_string") == CHIP_NO_ERROR);