111 lines
3.0 KiB
C
111 lines
3.0 KiB
C
#include "maze32_config_store.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include "nvs.h"
|
|
#include "nvs_flash.h"
|
|
|
|
static const char *NAMESPACE = "maze32";
|
|
static const char *SCHEMA_KEY = "cfg_ver";
|
|
static const char *SSID_KEY = "wifi_ssid";
|
|
static const char *PASSWORD_KEY = "wifi_pass";
|
|
|
|
esp_err_t maze32_config_store_init(void)
|
|
{
|
|
esp_err_t result = nvs_flash_init();
|
|
if (result == ESP_ERR_NVS_NO_FREE_PAGES || result == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
result = nvs_flash_erase();
|
|
if (result == ESP_OK) {
|
|
result = nvs_flash_init();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
esp_err_t maze32_config_store_load(maze32_wifi_credentials_t *credentials)
|
|
{
|
|
if (credentials == NULL) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
|
|
nvs_handle_t handle;
|
|
esp_err_t result = nvs_open(NAMESPACE, NVS_READONLY, &handle);
|
|
if (result == ESP_ERR_NVS_NOT_FOUND) {
|
|
return ESP_ERR_NOT_FOUND;
|
|
}
|
|
if (result != ESP_OK) {
|
|
return result;
|
|
}
|
|
|
|
uint32_t schema_version = 0U;
|
|
result = nvs_get_u32(handle, SCHEMA_KEY, &schema_version);
|
|
if (result == ESP_OK && schema_version != MAZE32_WIFI_CONFIG_SCHEMA_VERSION) {
|
|
result = ESP_ERR_INVALID_VERSION;
|
|
}
|
|
|
|
maze32_wifi_credentials_t loaded = {0};
|
|
size_t ssid_size = sizeof(loaded.ssid);
|
|
size_t password_size = sizeof(loaded.password);
|
|
if (result == ESP_OK) {
|
|
result = nvs_get_str(handle, SSID_KEY, loaded.ssid, &ssid_size);
|
|
}
|
|
if (result == ESP_OK) {
|
|
result = nvs_get_str(handle, PASSWORD_KEY, loaded.password, &password_size);
|
|
}
|
|
nvs_close(handle);
|
|
|
|
if (result == ESP_ERR_NVS_NOT_FOUND) {
|
|
return ESP_ERR_NOT_FOUND;
|
|
}
|
|
|
|
if (result == ESP_OK &&
|
|
maze32_wifi_credentials_validate(&loaded) != MAZE32_WIFI_CONFIG_VALID) {
|
|
result = ESP_ERR_INVALID_STATE;
|
|
}
|
|
if (result == ESP_OK) {
|
|
memcpy(credentials, &loaded, sizeof(*credentials));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
esp_err_t maze32_config_store_save(const maze32_wifi_credentials_t *credentials)
|
|
{
|
|
if (maze32_wifi_credentials_validate(credentials) != MAZE32_WIFI_CONFIG_VALID) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
|
|
nvs_handle_t handle;
|
|
esp_err_t result = nvs_open(NAMESPACE, NVS_READWRITE, &handle);
|
|
if (result != ESP_OK) {
|
|
return result;
|
|
}
|
|
|
|
result = nvs_set_str(handle, SSID_KEY, credentials->ssid);
|
|
if (result == ESP_OK) {
|
|
result = nvs_set_str(handle, PASSWORD_KEY, credentials->password);
|
|
}
|
|
if (result == ESP_OK) {
|
|
result = nvs_set_u32(handle, SCHEMA_KEY, MAZE32_WIFI_CONFIG_SCHEMA_VERSION);
|
|
}
|
|
if (result == ESP_OK) {
|
|
result = nvs_commit(handle);
|
|
}
|
|
nvs_close(handle);
|
|
return result;
|
|
}
|
|
|
|
esp_err_t maze32_config_store_clear(void)
|
|
{
|
|
nvs_handle_t handle;
|
|
esp_err_t result = nvs_open(NAMESPACE, NVS_READWRITE, &handle);
|
|
if (result != ESP_OK) {
|
|
return result;
|
|
}
|
|
result = nvs_erase_all(handle);
|
|
if (result == ESP_OK) {
|
|
result = nvs_commit(handle);
|
|
}
|
|
nvs_close(handle);
|
|
return result;
|
|
}
|