From 2345a3bc4151caad8e7a6567e58600e227d8a3db Mon Sep 17 00:00:00 2001 From: Doru Gucea Date: Fri, 3 Sep 2021 02:54:59 -0700 Subject: [PATCH 1/4] Fix KVS implementation Old implementation was not taking into account collissions. The new implementation keeps an unorderded hash having as keys the matter string keys and as values the key ids used as PDM identifiers. Used key ids are saved in a list which is updated at each Put/Delete operation. Also, in order to be able to restore from flash the unordered hash, the matter string keys are saved in flash. Signed-off-by: Doru Gucea --- .../k32w/k32w0/KeyValueStoreManagerImpl.cpp | 178 ++++++++++++++---- 1 file changed, 145 insertions(+), 33 deletions(-) diff --git a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp index 46ec80d759feab..8d14cf04be81e8 100644 --- a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp @@ -24,39 +24,104 @@ #include #include -#include #include +#include +#include #include #include "PDM.h" +#include + namespace chip { namespace DeviceLayer { namespace PersistedStorage { /* TODO: adjust this value */ -#define MAX_NO_OF_KEYS 255 +#define MAX_NO_OF_KEYS 20 KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance; +/* hashmap having: + * - the matter key string as key; + * - internal PDM identifier as value; + */ +std::unordered_map g_kvs_map; + +/* list containing used PDM identifiers */ +std::list g_key_ids_list; + +/* max no of bytes for a key */ +#define MAX_KEY_VALUE 255 + +/* used to check if we need to restore values from flash (e.g.: reset) */ +static bool g_restored_from_flash = FALSE; + +CHIP_ERROR RestoreFromFlash() +{ + CHIP_ERROR err = CHIP_NO_ERROR; + uint8_t key_id = 0; + char key_string_id[MAX_KEY_VALUE] = { 0 }; + size_t key_string_id_size = 0; + uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; + + if (g_restored_from_flash) + { + /* already restored from flash, nothing to do */ + return err; + } + + for (key_id = 0; key_id < MAX_NO_OF_KEYS; key_id++) + { + /* key was saved as string in flash (key_string_id) using (key_id + MAX_NO_OF_KEYS) as PDM key + * value corresponding to key_string_id was saved in flash using key_id as PDM key + */ + + err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueStr( + chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS), key_string_id, MAX_KEY_VALUE, + key_string_id_size); + + if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND) + { + continue; + } + + if (key_string_id_size) + { + g_key_ids_list.push_back(key_id); + g_kvs_map.insert(std::make_pair(std::string(key_string_id), key_id)); + key_string_id_size = 0; + + ChipLogProgress(DeviceLayer, "KVS, restored key [%s] from flash with PDM key: %i", key_string_id, key_id); + } + } + + g_restored_from_flash = TRUE; + + return err; +} + CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size, size_t offset_bytes) { - CHIP_ERROR err = CHIP_NO_ERROR; - std::hash hash_fn; - uint16_t key_id; + CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; - size_t read_bytes; + std::unordered_map::const_iterator it; + size_t read_bytes = 0; + uint8_t key_id = 0; - VerifyOrExit((key != NULL) && (value != NULL), err = CHIP_ERROR_INVALID_ARGUMENT); - key_id = hash_fn(key) % MAX_NO_OF_KEYS; + VerifyOrExit((key != NULL) && (value != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); - ChipLogProgress(DeviceLayer, "KVS, get key id:: %i", key_id); + if ((it = g_kvs_map.find(key)) != g_kvs_map.end()) + { + key_id = it->second; - err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t *) value, value_size, read_bytes); + err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin( + chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t *) value, value_size, read_bytes); + *read_bytes_size = read_bytes; - *read_bytes_size = read_bytes; + ChipLogProgress(DeviceLayer, "KVS, get key [%s] with PDM key: %i", key, key_id); + } exit: return err; @@ -64,18 +129,51 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size) { - CHIP_ERROR err = CHIP_NO_ERROR; - std::hash hash_fn; - uint16_t key_id; + CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT; + uint8_t key_id; + bool_t put_key = FALSE; uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; - - VerifyOrExit((key != NULL) && (value != NULL), err = CHIP_ERROR_INVALID_ARGUMENT); - key_id = hash_fn(key) % MAX_NO_OF_KEYS; - - ChipLogProgress(DeviceLayer, "KVS, put key id:: %i", key_id); - - err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueBin( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t *) value, value_size); + std::unordered_map::const_iterator it; + + VerifyOrExit((key != NULL) && (value != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); + + if ((it = g_kvs_map.find(key)) == g_kvs_map.end()) /* new key */ + { + for (key_id = 0; key_id < MAX_NO_OF_KEYS; key_id++) + { + std::list::iterator iter = std::find(g_key_ids_list.begin(), g_key_ids_list.end(), key_id); + + if (iter == g_key_ids_list.end()) + { + g_key_ids_list.push_back(key_id); + + put_key = TRUE; + break; + } + } + } + else /* overwrite key */ + { + put_key = TRUE; + key_id = it->second; + } + + if (put_key) + { + ChipLogProgress(DeviceLayer, "KVS, put key [%s] with PDM key: %i", key, key_id); + + g_kvs_map.insert(std::make_pair(std::string(key), key_id)); + err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueBin( + chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t *) value, value_size); + + /* save the 'key' in flash such that it can be retrieved later on */ + if (err == CHIP_NO_ERROR) + { + ChipLogProgress(DeviceLayer, "KVS, save in flash key [%s] with PDM key: %i", key, key_id + MAX_NO_OF_KEYS); + err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueStr( + chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS), key, strlen(key)); + } + } exit: return err; @@ -83,17 +181,31 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) { - CHIP_ERROR err = CHIP_NO_ERROR; - std::hash hash_fn; - uint16_t key_id; + CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; + std::unordered_map::const_iterator it; uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; - - VerifyOrExit(key != NULL, err = CHIP_ERROR_INVALID_ARGUMENT); - key_id = hash_fn(key) % MAX_NO_OF_KEYS; - - ChipLogProgress(DeviceLayer, "KVS, deleting key id:: %i", key_id); - - err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue(chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id)); + uint8_t key_id = 0; + + VerifyOrExit((key != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); + + if ((it = g_kvs_map.find(key)) != g_kvs_map.end()) + { + key_id = it->second; + g_key_ids_list.remove(key_id); + g_kvs_map.erase(it); + + ChipLogProgress(DeviceLayer, "KVS, delete key [%s] with PDM key: %i", key, key_id); + err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue( + chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id)); + + /* also delete the 'key string' from flash */ + if (err == CHIP_NO_ERROR) + { + ChipLogProgress(DeviceLayer, "KVS, delete key [%s] with PDM key: %i", key, key_id + MAX_NO_OF_KEYS); + err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue( + chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS)); + } + } exit: return err; From 5e2e47840315ee235de66c95a9c6e5c27d90ee70 Mon Sep 17 00:00:00 2001 From: Doru Gucea Date: Mon, 4 Oct 2021 00:01:50 -0700 Subject: [PATCH 2/4] Fixes after review Signed-off-by: Doru Gucea --- .../k32w/k32w0/KeyValueStoreManagerImpl.cpp | 128 +++++++++++------- 1 file changed, 82 insertions(+), 46 deletions(-) diff --git a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp index 8d14cf04be81e8..4e7f286d79cfc4 100644 --- a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp @@ -37,8 +37,9 @@ namespace chip { namespace DeviceLayer { namespace PersistedStorage { -/* TODO: adjust this value */ -#define MAX_NO_OF_KEYS 20 +/* TODO: adjust these values */ +constexpr size_t kMaxNumberOfKeys = 20; +constexpr size_t kMaxKeyValueBytes = 255; KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance; @@ -48,22 +49,20 @@ KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance; */ std::unordered_map g_kvs_map; -/* list containing used PDM identifiers */ -std::list g_key_ids_list; - -/* max no of bytes for a key */ -#define MAX_KEY_VALUE 255 +/* set containing used PDM identifiers */ +std::set g_key_ids_set; /* used to check if we need to restore values from flash (e.g.: reset) */ -static bool g_restored_from_flash = FALSE; +static bool g_restored_from_flash = false; CHIP_ERROR RestoreFromFlash() { - CHIP_ERROR err = CHIP_NO_ERROR; - uint8_t key_id = 0; - char key_string_id[MAX_KEY_VALUE] = { 0 }; - size_t key_string_id_size = 0; - uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; + CHIP_ERROR err = CHIP_NO_ERROR; + uint8_t key_id = 0; + char key_string_id[kMaxKeyValueBytes] = { 0 }; + size_t key_string_id_size = 0; + uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; + uint16_t pdm_internal_id = 0; if (g_restored_from_flash) { @@ -71,32 +70,44 @@ CHIP_ERROR RestoreFromFlash() return err; } - for (key_id = 0; key_id < MAX_NO_OF_KEYS; key_id++) + for (key_id = 0; key_id < kMaxNumberOfKeys; key_id++) { - /* key was saved as string in flash (key_string_id) using (key_id + MAX_NO_OF_KEYS) as PDM key + /* key was saved as string in flash (key_string_id) using (key_id + kMaxNumberOfKeys) as PDM key * value corresponding to key_string_id was saved in flash using key_id as PDM key */ - err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueStr( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS), key_string_id, MAX_KEY_VALUE, - key_string_id_size); + pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); + err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueStr(pdm_internal_id, key_string_id, + kMaxKeyValueBytes, key_string_id_size); if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND) { + err = CHIP_NO_ERROR; continue; } + else if (err != CHIP_NO_ERROR) + { + ChipLogProgress(DeviceLayer, "KVS, Error while restoring Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); + break; + } if (key_string_id_size) { - g_key_ids_list.push_back(key_id); - g_kvs_map.insert(std::make_pair(std::string(key_string_id), key_id)); + g_key_ids_set.insert(key_id); key_string_id_size = 0; - - ChipLogProgress(DeviceLayer, "KVS, restored key [%s] from flash with PDM key: %i", key_string_id, key_id); + if (!g_kvs_map.insert(std::make_pair(std::string(key_string_id), key_id)).second) + { + /* key collision is not expected when restoring from flash */ + ChipLogProgress(DeviceLayer, "KVS, Unexpected collision while restoring Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); + } + else + { + ChipLogProgress(DeviceLayer, "KVS, restored Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); + } } } - g_restored_from_flash = TRUE; + g_restored_from_flash = true; return err; } @@ -109,18 +120,19 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t std::unordered_map::const_iterator it; size_t read_bytes = 0; uint8_t key_id = 0; + uint16_t pdm_internal_id = 0; VerifyOrExit((key != NULL) && (value != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); if ((it = g_kvs_map.find(key)) != g_kvs_map.end()) { key_id = it->second; + pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id); - err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t *) value, value_size, read_bytes); + err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin(pdm_internal_id, (uint8_t *) value, value_size, read_bytes); *read_bytes_size = read_bytes; - ChipLogProgress(DeviceLayer, "KVS, get key [%s] with PDM key: %i", key, key_id); + ChipLogProgress(DeviceLayer, "KVS, get Matter key [%s] with PDM id: %i", key, pdm_internal_id); } exit: @@ -131,47 +143,59 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, { CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT; uint8_t key_id; - bool_t put_key = FALSE; + bool_t put_key = false; uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; std::unordered_map::const_iterator it; + uint16_t pdm_internal_id = 0; VerifyOrExit((key != NULL) && (value != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); if ((it = g_kvs_map.find(key)) == g_kvs_map.end()) /* new key */ { - for (key_id = 0; key_id < MAX_NO_OF_KEYS; key_id++) + for (key_id = 0; key_id < kMaxNumberOfKeys; key_id++) { - std::list::iterator iter = std::find(g_key_ids_list.begin(), g_key_ids_list.end(), key_id); + std::set::iterator iter = std::find(g_key_ids_set.begin(), g_key_ids_set.end(), key_id); - if (iter == g_key_ids_list.end()) + if (iter == g_key_ids_set.end()) { - g_key_ids_list.push_back(key_id); + assert(g_key_ids_set.size() < kMaxNumberOfKeys); + g_key_ids_set.insert(key_id); - put_key = TRUE; + put_key = true; break; } } } else /* overwrite key */ { - put_key = TRUE; + put_key = true; key_id = it->second; } if (put_key) { - ChipLogProgress(DeviceLayer, "KVS, put key [%s] with PDM key: %i", key, key_id); + pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id); + ChipLogProgress(DeviceLayer, "KVS, save in flash Matter key [%s] with PDM id: %i", key, pdm_internal_id); g_kvs_map.insert(std::make_pair(std::string(key), key_id)); - err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueBin( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t *) value, value_size); + err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueBin(pdm_internal_id, (uint8_t *) value, value_size); /* save the 'key' in flash such that it can be retrieved later on */ if (err == CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "KVS, save in flash key [%s] with PDM key: %i", key, key_id + MAX_NO_OF_KEYS); - err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueStr( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS), key, strlen(key)); + pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); + ChipLogProgress(DeviceLayer, "KVS, save in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + + err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueStr(pdm_internal_id, key, strlen(key)); + + if (err != CHIP_NO_ERROR) + { + ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + } + } + else + { + ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash Matter key [%s] with PDM id: %i", key, pdm_internal_id); } } @@ -185,25 +209,37 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) std::unordered_map::const_iterator it; uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; uint8_t key_id = 0; + uint16_t pdm_internal_id = 0; VerifyOrExit((key != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); if ((it = g_kvs_map.find(key)) != g_kvs_map.end()) { key_id = it->second; - g_key_ids_list.remove(key_id); + pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id); + + g_key_ids_set.erase(key_id); g_kvs_map.erase(it); - ChipLogProgress(DeviceLayer, "KVS, delete key [%s] with PDM key: %i", key, key_id); - err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id)); + ChipLogProgress(DeviceLayer, "KVS, delete from flash the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue(pdm_internal_id); /* also delete the 'key string' from flash */ if (err == CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "KVS, delete key [%s] with PDM key: %i", key, key_id + MAX_NO_OF_KEYS); - err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue( - chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS)); + pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); + ChipLogProgress(DeviceLayer, "KVS, delete from flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + + err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue(pdm_internal_id); + + if (err != CHIP_NO_ERROR) + { + ChipLogProgress(DeviceLayer, "KVS, Error while deleting from flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + } + } + else + { + ChipLogProgress(DeviceLayer, "KVS, Error while deleting from flash the Matter key [%s] with PDM id: %i", key, pdm_internal_id); } } From 3e459e34ffba61420b9f07cfa303cf95486a838b Mon Sep 17 00:00:00 2001 From: "restyled-io[bot]" <32688539+restyled-io[bot]@users.noreply.github.com> Date: Thu, 4 Nov 2021 18:04:01 +0200 Subject: [PATCH 3/4] Restyled by clang-format (#11433) Co-authored-by: Restyled.io Signed-off-by: Doru Gucea --- .../k32w/k32w0/KeyValueStoreManagerImpl.cpp | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp index 4e7f286d79cfc4..eae1ef68b1ca8d 100644 --- a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp @@ -24,8 +24,8 @@ #include #include -#include #include +#include #include #include @@ -77,8 +77,8 @@ CHIP_ERROR RestoreFromFlash() */ pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); - err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueStr(pdm_internal_id, key_string_id, - kMaxKeyValueBytes, key_string_id_size); + err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueStr(pdm_internal_id, key_string_id, kMaxKeyValueBytes, + key_string_id_size); if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND) { @@ -87,7 +87,8 @@ CHIP_ERROR RestoreFromFlash() } else if (err != CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "KVS, Error while restoring Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Error while restoring Matter key [%s] from flash with PDM id: %i", key_string_id, + pdm_internal_id); break; } @@ -98,11 +99,13 @@ CHIP_ERROR RestoreFromFlash() if (!g_kvs_map.insert(std::make_pair(std::string(key_string_id), key_id)).second) { /* key collision is not expected when restoring from flash */ - ChipLogProgress(DeviceLayer, "KVS, Unexpected collision while restoring Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Unexpected collision while restoring Matter key [%s] from flash with PDM id: %i", + key_string_id, pdm_internal_id); } else { - ChipLogProgress(DeviceLayer, "KVS, restored Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, restored Matter key [%s] from flash with PDM id: %i", key_string_id, + pdm_internal_id); } } } @@ -118,18 +121,19 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; std::unordered_map::const_iterator it; - size_t read_bytes = 0; - uint8_t key_id = 0; + size_t read_bytes = 0; + uint8_t key_id = 0; uint16_t pdm_internal_id = 0; VerifyOrExit((key != NULL) && (value != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); if ((it = g_kvs_map.find(key)) != g_kvs_map.end()) { - key_id = it->second; + key_id = it->second; pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id); - err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin(pdm_internal_id, (uint8_t *) value, value_size, read_bytes); + err = + chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin(pdm_internal_id, (uint8_t *) value, value_size, read_bytes); *read_bytes_size = read_bytes; ChipLogProgress(DeviceLayer, "KVS, get Matter key [%s] with PDM id: %i", key, pdm_internal_id); @@ -175,7 +179,7 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, if (put_key) { pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id); - ChipLogProgress(DeviceLayer, "KVS, save in flash Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, save in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); g_kvs_map.insert(std::make_pair(std::string(key), key_id)); err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueBin(pdm_internal_id, (uint8_t *) value, value_size); @@ -184,18 +188,20 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, if (err == CHIP_NO_ERROR) { pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); - ChipLogProgress(DeviceLayer, "KVS, save in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, save in flash the Matter key [%s] with PDM id: %i", key, + pdm_internal_id); err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueStr(pdm_internal_id, key, strlen(key)); if (err != CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the Matter key [%s] with PDM id: %i", + key, pdm_internal_id); } } else { - ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); } } @@ -207,15 +213,15 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) { CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; std::unordered_map::const_iterator it; - uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; - uint8_t key_id = 0; + uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS; + uint8_t key_id = 0; uint16_t pdm_internal_id = 0; VerifyOrExit((key != NULL) && (RestoreFromFlash() == CHIP_NO_ERROR), err = CHIP_ERROR_INVALID_ARGUMENT); if ((it = g_kvs_map.find(key)) != g_kvs_map.end()) { - key_id = it->second; + key_id = it->second; pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id); g_key_ids_set.erase(key_id); @@ -228,18 +234,22 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) if (err == CHIP_NO_ERROR) { pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); - ChipLogProgress(DeviceLayer, "KVS, delete from flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, delete from flash the value of the Matter key [%s] with PDM id: %i", key, + pdm_internal_id); err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue(pdm_internal_id); if (err != CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "KVS, Error while deleting from flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, + "KVS, Error while deleting from flash the value of the Matter key [%s] with PDM id: %i", key, + pdm_internal_id); } } else { - ChipLogProgress(DeviceLayer, "KVS, Error while deleting from flash the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Error while deleting from flash the Matter key [%s] with PDM id: %i", key, + pdm_internal_id); } } From 06014a7536db1cd338114232263c8d6ee630f468 Mon Sep 17 00:00:00 2001 From: "restyled-io[bot]" <32688539+restyled-io[bot]@users.noreply.github.com> Date: Fri, 5 Nov 2021 12:43:34 +0200 Subject: [PATCH 4/4] Restyled by clang-format (#11464) Co-authored-by: Restyled.io Signed-off-by: Doru Gucea --- .../k32w/k32w0/KeyValueStoreManagerImpl.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp index eae1ef68b1ca8d..5753f614a0550a 100644 --- a/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp +++ b/src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp @@ -89,7 +89,7 @@ CHIP_ERROR RestoreFromFlash() { ChipLogProgress(DeviceLayer, "KVS, Error while restoring Matter key [%s] from flash with PDM id: %i", key_string_id, pdm_internal_id); - break; + return err; } if (key_string_id_size) @@ -162,7 +162,11 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, if (iter == g_key_ids_set.end()) { - assert(g_key_ids_set.size() < kMaxNumberOfKeys); + if (g_key_ids_set.size() >= kMaxNumberOfKeys) + { + return CHIP_ERROR_NO_MEMORY; + } + g_key_ids_set.insert(key_id); put_key = true; @@ -188,20 +192,20 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, if (err == CHIP_NO_ERROR) { pdm_internal_id = chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys); - ChipLogProgress(DeviceLayer, "KVS, save in flash the Matter key [%s] with PDM id: %i", key, - pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, save in flash the Matter key [%s] with PDM id: %i", key, pdm_internal_id); err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueStr(pdm_internal_id, key, strlen(key)); if (err != CHIP_NO_ERROR) { - ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the Matter key [%s] with PDM id: %i", - key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the Matter key [%s] with PDM id: %i", key, + pdm_internal_id); } } else { - ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the value of the Matter key [%s] with PDM id: %i", key, pdm_internal_id); + ChipLogProgress(DeviceLayer, "KVS, Error while saving in flash the value of the Matter key [%s] with PDM id: %i", key, + pdm_internal_id); } }