#include "common/constants.h" #include "io/nvs_handler.h" #include "nvs_flash.h" #include "string.h" NVSStorageHandler::NVSStorageHandler( const char* name_space ) : name_space(name_space) { } NVSStorageHandler::~NVSStorageHandler() { if (this->nvsHandle != 0) { nvs_close(this->nvsHandle); this->nvsHandle = 0; } } void NVSStorageHandler::init(const EventGroupHandle_t& system_event_group) { esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { nvs_flash_erase(); err = nvs_flash_init(); } ESP_ERROR_CHECK(err); err = nvs_open(this->name_space, NVS_READWRITE, &this->nvsHandle); if (err != ESP_OK) { printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err)); } else { xEventGroupSetBits(system_event_group, STORAGE_READY_BIT); printf("NVS Storage initialized.\n"); } } void NVSStorageHandler::put(const char* const& key, const char* const& value) { if (this->nvsHandle == 0) { printf("NVS handle is not initialized.\n"); return; } esp_err_t err = nvs_set_str(this->nvsHandle, key, value); if (err != ESP_OK) { printf("Error (%s) setting key-value pair in NVS!\n", esp_err_to_name(err)); } else { nvs_commit(this->nvsHandle); printf("Key-value pair (%s, %s) stored in NVS.\n", key, value); } } char* NVSStorageHandler::get(const char* const& key) const { if (this->nvsHandle == 0) { printf("NVS handle is not initialized.\n"); return nullptr; } size_t required_size = 0; esp_err_t err = nvs_get_str(this->nvsHandle, key, nullptr, &required_size); if (err == ESP_ERR_NVS_NOT_FOUND) { printf("Key %s not found in NVS.\n", key); return nullptr; } else if (err != ESP_OK) { printf("Error (%s) getting size for key %s from NVS!\n", esp_err_to_name(err), key); return nullptr; } char* value = (char*)malloc(required_size); err = nvs_get_str(this->nvsHandle, key, value, &required_size); if (err != ESP_OK) { printf("Error (%s) getting value for key %s from NVS!\n", esp_err_to_name(err), key); free(value); return nullptr; } return value; } NVSIteratorGuard NVSStorageHandler::create_iterator() const { nvs_iterator_t it = nullptr; esp_err_t err = nvs_entry_find(NVS_DEFAULT_PART_NAME, this->name_space, NVS_TYPE_ANY, &it); if (err != ESP_OK) { printf("Error (%s) creating NVS iterator!\n", esp_err_to_name(err)); return NVSIteratorGuard(nullptr, err); } return NVSIteratorGuard(it, ESP_OK); } esp_err_t NVSStorageHandler::process_all(KeyValueProcessor processor, void* arg) const { NVSIteratorGuard iterator_guard = this->create_iterator(); if (!iterator_guard.is_valid()) { return iterator_guard.get_error(); } const nvs_iterator_t& it = iterator_guard.get_iterator(); for (; it != NULL; iterator_guard.advance_iter()) { nvs_entry_info_t info; esp_err_t err = nvs_entry_info(it, &info); if (err != ESP_OK) { printf("Error (%s) getting NVS entry info!\n", esp_err_to_name(err)); return err; } nvs_handle_t temp_handle; err = nvs_open(this->name_space, NVS_READONLY, &temp_handle); if (err != ESP_OK) { printf("Error (%s) opening NVS handle for reading!\n", esp_err_to_name(err)); return err; } // call the processor with the key and value processor(arg, info.key, this->get(info.key)); } return ESP_OK; } esp_err_t NVSStorageHandler::process_filtered(const char* const& key_prefix, KeyValueProcessor processor, void* arg) const { NVSIteratorGuard iterator_guard = this->create_iterator(); if (!iterator_guard.is_valid()) { return iterator_guard.get_error(); } const nvs_iterator_t& it = iterator_guard.get_iterator(); for (; it != NULL; iterator_guard.advance_iter()) { nvs_entry_info_t info; esp_err_t err = nvs_entry_info(it, &info); if (err != ESP_OK) { printf("Error (%s) getting NVS entry info!\n", esp_err_to_name(err)); return err; } // check if the key matches the prefix if (strncmp(info.key, key_prefix, strlen(key_prefix)) == 0) { nvs_handle_t temp_handle; err = nvs_open(this->name_space, NVS_READONLY, &temp_handle); if (err != ESP_OK) { printf("Error (%s) opening NVS handle for reading!\n", esp_err_to_name(err)); return err; } // call the processor with the key and value processor(arg, info.key, this->get(info.key)); } } return ESP_OK; } esp_err_t NVSStorageHandler::process_filtered(FilterFunc filter_func, KeyValueProcessor processor, void* arg) const { NVSIteratorGuard iterator_guard = this->create_iterator(); if (!iterator_guard.is_valid()) { return iterator_guard.get_error(); } const nvs_iterator_t& it = iterator_guard.get_iterator(); for (; it != NULL; iterator_guard.advance_iter()) { nvs_entry_info_t info; esp_err_t err = nvs_entry_info(it, &info); if (err != ESP_OK) { printf("Error (%s) getting NVS entry info!\n", esp_err_to_name(err)); return err; } // check if the key matches the filter function if (filter_func(info.key)) { nvs_handle_t temp_handle; err = nvs_open(this->name_space, NVS_READONLY, &temp_handle); if (err != ESP_OK) { printf("Error (%s) opening NVS handle for reading!\n", esp_err_to_name(err)); return err; } // call the processor with the key and value processor(arg, info.key, this->get(info.key)); } } return ESP_OK; } void NVSStorageHandler::remove(const char* const& key) { if (this->nvsHandle == 0) { printf("NVS handle is not initialized.\n"); return; } esp_err_t err = nvs_erase_key(this->nvsHandle, key); if (err != ESP_OK) { printf("Error (%s) deleting key %s from NVS!\n", esp_err_to_name(err), key); } else { nvs_commit(this->nvsHandle); printf("Key %s deleted from NVS.\n", key); } }