2024-07-27 13:31:17 +00:00
|
|
|
#include "constants.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "nvs.h"
|
2024-08-17 15:19:47 +00:00
|
|
|
#include "esp_log.h"
|
2024-07-27 13:31:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define STORAGE_NAMESPACE "storage"
|
|
|
|
|
|
|
|
#define DEV_NAME_KEY "dev_name"
|
2024-08-15 11:09:01 +00:00
|
|
|
#define PRIVATE_KEY "prikey"
|
2024-08-18 15:40:41 +00:00
|
|
|
#define SEC_ORIG_KEY "secureOrig"
|
2024-08-18 17:42:40 +00:00
|
|
|
#define SEC_ROOT_CA_KEY "rootCA"
|
2024-07-27 13:31:17 +00:00
|
|
|
|
2024-08-17 15:19:47 +00:00
|
|
|
static const char *TAG = "storage";
|
|
|
|
|
2024-07-27 13:31:17 +00:00
|
|
|
bool storage_init()
|
|
|
|
{
|
|
|
|
esp_err_t err = nvs_flash_init();
|
|
|
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
|
|
|
{
|
2024-08-17 15:19:47 +00:00
|
|
|
ESP_LOGI(TAG, "Need to reset storage\n");
|
2024-07-27 13:31:17 +00:00
|
|
|
|
|
|
|
// NVS partition was truncated and needs to be erased
|
|
|
|
// Retry nvs_flash_init
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
err = nvs_flash_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return err == ESP_OK;
|
|
|
|
}
|
|
|
|
|
2024-08-18 15:40:41 +00:00
|
|
|
static void storage_set_str(const char *key, const char *value)
|
2024-07-27 13:31:17 +00:00
|
|
|
{
|
|
|
|
nvs_handle_t my_handle;
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
|
|
|
|
|
2024-08-18 15:40:41 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_set_blob(my_handle, key, value, strlen(value) + 1));
|
2024-07-27 13:31:17 +00:00
|
|
|
|
|
|
|
nvs_close(my_handle);
|
|
|
|
}
|
|
|
|
|
2024-08-18 15:40:41 +00:00
|
|
|
static size_t storage_get_str(const char *key, size_t dest_len, char *dest)
|
2024-07-27 13:31:17 +00:00
|
|
|
{
|
|
|
|
nvs_handle_t my_handle;
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
|
|
|
|
|
2024-08-18 15:40:41 +00:00
|
|
|
size_t len = (dest == NULL ? 0 : dest_len);
|
|
|
|
esp_err_t res = nvs_get_blob(my_handle, key, dest, &len);
|
2024-07-27 13:31:17 +00:00
|
|
|
|
|
|
|
nvs_close(my_handle);
|
|
|
|
|
|
|
|
if (res == ESP_ERR_NVS_NOT_FOUND || len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(res);
|
|
|
|
|
2024-08-15 11:09:01 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2024-08-18 15:40:41 +00:00
|
|
|
void storage_set_dev_name(const char *name)
|
|
|
|
{
|
|
|
|
storage_set_str(DEV_NAME_KEY, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t storage_get_dev_name(char *dest)
|
|
|
|
{
|
|
|
|
return storage_get_str(DEV_NAME_KEY, DEV_NAME_LEN, dest);
|
|
|
|
}
|
|
|
|
|
2024-08-15 11:09:01 +00:00
|
|
|
void storage_set_priv_key(unsigned char *key, size_t len)
|
|
|
|
{
|
|
|
|
nvs_handle_t my_handle;
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(nvs_set_blob(my_handle, PRIVATE_KEY, key, len));
|
|
|
|
|
|
|
|
nvs_close(my_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t storage_get_priv_key(unsigned char *key)
|
|
|
|
{
|
|
|
|
nvs_handle_t my_handle;
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
|
|
|
|
|
|
|
|
size_t len = (key == NULL ? 0 : PRV_KEY_DER_MAX_BYTES);
|
|
|
|
esp_err_t res = nvs_get_blob(my_handle, PRIVATE_KEY, key, &len);
|
|
|
|
|
|
|
|
nvs_close(my_handle);
|
|
|
|
|
|
|
|
if (res == ESP_ERR_NVS_NOT_FOUND || len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(res);
|
|
|
|
|
2024-07-27 13:31:17 +00:00
|
|
|
return len;
|
2024-08-18 15:40:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-18 17:42:40 +00:00
|
|
|
void storage_set_secure_origin(const char *orig) { storage_set_str(SEC_ORIG_KEY, orig); }
|
2024-08-18 15:40:41 +00:00
|
|
|
|
2024-08-18 17:42:40 +00:00
|
|
|
size_t storage_get_secure_origin(char *dest) { return storage_get_str(SEC_ORIG_KEY, SEC_ORIG_LEN, dest); }
|
|
|
|
|
|
|
|
void storage_set_root_ca(const char *ca) { storage_set_str(SEC_ROOT_CA_KEY, ca); }
|
|
|
|
|
|
|
|
size_t storage_get_root_ca(char *dest) { return storage_get_str(SEC_ROOT_CA_KEY, ROOT_CA_MAX_BYTES, dest); }
|