blackmagic-esp32-s2/main/nvs.c

63 lines
1.6 KiB
C
Raw Normal View History

2021-10-14 09:30:43 +01:00
#include <nvs_flash.h>
#include <esp_log.h>
2021-10-31 19:59:10 +00:00
#include "nvs.h"
#define TAG "nvs"
#define NVS_STORE "nvs_storage"
2021-10-14 09:30:43 +01:00
void nvs_init(void) {
2021-10-31 19:59:10 +00:00
ESP_LOGI(TAG, "init");
2021-10-14 09:30:43 +01:00
esp_err_t ret = nvs_flash_init();
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
2021-10-31 19:59:10 +00:00
ESP_LOGI(TAG, "erasing");
2021-10-14 09:30:43 +01:00
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
2021-10-31 19:59:10 +00:00
ESP_LOGI(TAG, "done");
}
esp_err_t nvs_save_string(const char* key, const mstring_t* value) {
nvs_handle_t nvs_handle;
esp_err_t err;
err = nvs_open(NVS_STORE, NVS_READWRITE, &nvs_handle);
if(err != ESP_OK) return err;
err = nvs_set_str(nvs_handle, key, mstring_get_cstr(value));
if(err != ESP_OK) return err;
err = nvs_commit(nvs_handle);
if(err != ESP_OK) return err;
nvs_close(nvs_handle);
return ESP_OK;
2021-10-14 09:30:43 +01:00
}
2021-10-31 19:59:10 +00:00
esp_err_t nvs_load_string(const char* key, mstring_t* value) {
nvs_handle_t nvs_handle;
esp_err_t err;
char* buffer = NULL;
do {
err = nvs_open(NVS_STORE, NVS_READWRITE, &nvs_handle);
if(err != ESP_OK) return err;
size_t required_size = 0;
err = nvs_get_str(nvs_handle, key, NULL, &required_size);
if(err != ESP_OK) return err;
buffer = malloc(required_size + sizeof(uint32_t));
err = nvs_get_str(nvs_handle, key, buffer, &required_size);
if(err != ESP_OK) return err;
mstring_set(value, buffer);
nvs_close(nvs_handle);
err = ESP_OK;
} while(0);
if(buffer != NULL) free(buffer);
return err;
}