Skip to content

Commit

Permalink
[zephyr] Fix factory recent after recent KVS changes (#20419)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Damian-Nordic authored and web-flow committed Jul 12, 2022
1 parent accf306 commit 8c8e283
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/platform/Zephyr/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct ReadEntry

struct DeleteSubtreeEntry
{
CHIP_ERROR result;
int result;
};

// Random magic bytes to represent an empty value.
Expand Down Expand Up @@ -128,11 +128,16 @@ int DeleteSubtreeCallback(const char * name, size_t /* entrySize */, settings_re
void * param)
{
DeleteSubtreeEntry & entry = *static_cast<DeleteSubtreeEntry *>(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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/platform/tests/TestKeyValueStoreMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8c8e283

Please sign in to comment.