Skip to content

Commit

Permalink
Fixes after review
Browse files Browse the repository at this point in the history
Signed-off-by: Doru Gucea <[email protected]>
  • Loading branch information
doru91 committed Nov 4, 2021
1 parent b6597c8 commit 060b63b
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions src/platform/nxp/k32w/k32w0/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -48,55 +49,61 @@ KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
*/
std::unordered_map<std::string, uint8_t> g_kvs_map;

/* list containing used PDM identifiers */
std::list<uint8_t> g_key_ids_list;

/* max no of bytes for a key */
#define MAX_KEY_VALUE 255
/* set containing used PDM identifiers */
std::set<uint8_t> 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;

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++)
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,
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys), 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)
{
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);
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 */
abort();
}
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;
g_restored_from_flash = true;

return err;
}
Expand Down Expand Up @@ -131,30 +138,31 @@ 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<std::string, uint8_t>::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++)
for (key_id = 0; key_id < kMaxNumberOfKeys; key_id++)
{
std::list<uint8_t>::iterator iter = std::find(g_key_ids_list.begin(), g_key_ids_list.end(), key_id);
std::set<uint8_t>::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;
}

Expand All @@ -169,9 +177,9 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value,
/* 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);
ChipLogProgress(DeviceLayer, "KVS, save in flash key [%s] with PDM key: %i", key, key_id + kMaxNumberOfKeys);
err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueStr(
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS), key, strlen(key));
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys), key, strlen(key));
}
}

Expand All @@ -191,7 +199,7 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
if ((it = g_kvs_map.find(key)) != g_kvs_map.end())
{
key_id = it->second;
g_key_ids_list.remove(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);
Expand All @@ -201,9 +209,9 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
/* 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);
ChipLogProgress(DeviceLayer, "KVS, delete key [%s] with PDM key: %i", key, key_id + kMaxNumberOfKeys);
err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue(
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + MAX_NO_OF_KEYS));
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id + kMaxNumberOfKeys));
}
}

Expand Down

0 comments on commit 060b63b

Please sign in to comment.