blackmagic-esp32-s2/main/nvs.c

88 lines
2.5 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-12-03 00:33:06 +00:00
ESP_LOGI(TAG, "init " NVS_DEFAULT_PART_NAME);
esp_err_t ret = nvs_flash_init_partition(NVS_DEFAULT_PART_NAME);
2021-10-14 09:30:43 +01:00
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
2021-12-03 00:33:06 +00:00
ESP_LOGI(TAG, "erasing " NVS_DEFAULT_PART_NAME);
ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_DEFAULT_PART_NAME));
ret = nvs_flash_init_partition(NVS_DEFAULT_PART_NAME);
2021-10-14 09:30:43 +01:00
}
ESP_ERROR_CHECK(ret);
2021-12-03 00:33:06 +00:00
ESP_LOGI(TAG, "init " NVS_STORE);
ret = nvs_flash_init_partition(NVS_STORE);
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGI(TAG, "erasing " NVS_STORE);
ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_STORE));
ret = nvs_flash_init_partition(NVS_STORE);
}
ESP_ERROR_CHECK(ret);
2021-11-22 11:24:16 +00:00
ESP_LOGI(TAG, "init done");
2021-10-31 19:59:10 +00:00
}
2021-12-03 00:33:06 +00:00
void nvs_erase(void) {
ESP_LOGI(TAG, "erasing " NVS_DEFAULT_PART_NAME);
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
ESP_LOGI(TAG, "erasing " NVS_STORE);
ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_STORE));
ESP_ERROR_CHECK(nvs_flash_init_partition(NVS_STORE));
ESP_LOGI(TAG, "erasing done");
}
2021-10-31 19:59:10 +00:00
esp_err_t nvs_save_string(const char* key, const mstring_t* value) {
nvs_handle_t nvs_handle;
esp_err_t err;
2021-10-31 20:02:58 +00:00
do {
2021-12-06 15:38:35 +00:00
err = nvs_open_from_partition(NVS_STORE, "config", NVS_READWRITE, &nvs_handle);
2021-10-31 20:02:58 +00:00
if(err != ESP_OK) break;
2021-10-31 19:59:10 +00:00
2021-10-31 20:02:58 +00:00
err = nvs_set_str(nvs_handle, key, mstring_get_cstr(value));
if(err != ESP_OK) break;
2021-10-31 19:59:10 +00:00
2021-10-31 20:02:58 +00:00
err = nvs_commit(nvs_handle);
if(err != ESP_OK) break;
nvs_close(nvs_handle);
err = ESP_OK;
} while(0);
2021-10-31 19:59:10 +00:00
2021-10-31 20:02:58 +00:00
return err;
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 {
2021-12-06 15:38:35 +00:00
err = nvs_open_from_partition(NVS_STORE, "config", NVS_READONLY, &nvs_handle);
2021-10-31 20:02:58 +00:00
if(err != ESP_OK) break;
2021-10-31 19:59:10 +00:00
size_t required_size = 0;
err = nvs_get_str(nvs_handle, key, NULL, &required_size);
2021-10-31 20:02:58 +00:00
if(err != ESP_OK) break;
2021-10-31 19:59:10 +00:00
buffer = malloc(required_size + sizeof(uint32_t));
err = nvs_get_str(nvs_handle, key, buffer, &required_size);
2021-10-31 20:02:58 +00:00
if(err != ESP_OK) break;
2021-10-31 19:59:10 +00:00
mstring_set(value, buffer);
nvs_close(nvs_handle);
err = ESP_OK;
} while(0);
if(buffer != NULL) free(buffer);
return err;
}