nvs: support iteration over namespace by index

Users of the nvs API are likely to have `nvs_handle_t` in all cases, but
not all of them carry around the partition name and namespace name (as
they aren't needed after creating an `nvs_handle_t`).

Allow this common case to use nvs iteration without them tracking
additional data by using the `nvs_handle_t` to locate the namespace
index and the partition name by introducing an alterate to
`nvs_entry_find` called `nvs_entry_find_in_handle`, which operates
similarly except that it is given a `nvs_handle_t` instead of partition
and namespace strings.

This is somewhat more limited than the `nvs_entry_find` API as one
cannot examine all keys in a given partition.
This commit is contained in:
Cody P Schafer
2023-04-03 16:48:02 -04:00
committed by radek.tandler
parent e44d4260ad
commit f2af1c5305
8 changed files with 138 additions and 19 deletions
+40
View File
@@ -786,6 +786,46 @@ extern "C" esp_err_t nvs_entry_find(const char *part_name, const char *namespace
return ESP_OK;
}
extern "C" esp_err_t nvs_entry_find_in_handle(nvs_handle_t c_handle, nvs_type_t type, nvs_iterator_t *output_iterator)
{
if (output_iterator == nullptr) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t lock_result = Lock::init();
if (lock_result != ESP_OK) {
*output_iterator = nullptr;
return lock_result;
}
Lock lock;
nvs::Storage *pStorage;
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
*output_iterator = nullptr;
return err;
}
pStorage = handle->get_storage();
nvs_iterator_t it = create_iterator(pStorage, type);
if (it == nullptr) {
*output_iterator = nullptr;
return ESP_ERR_NO_MEM;
}
bool entryFound = handle->findEntryNs(it);
if (!entryFound) {
free(it);
*output_iterator = nullptr;
return ESP_ERR_NVS_NOT_FOUND;
}
*output_iterator = it;
return ESP_OK;
}
extern "C" esp_err_t nvs_entry_next(nvs_iterator_t *iterator)
{
if (iterator == nullptr) {