Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zephyr] Fix factory recent after recent KVS changes #20419

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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