feat: implement Wi-Fi provisioning and persistent configuration
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static int initialized;
|
||||
static int present;
|
||||
static uint32_t schema;
|
||||
static char ssid[33];
|
||||
static char password[64];
|
||||
|
||||
esp_err_t nvs_flash_init(void)
|
||||
{
|
||||
initialized = 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_flash_deinit(void)
|
||||
{
|
||||
initialized = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_flash_erase(void)
|
||||
{
|
||||
present = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_open(const char *namespace_name, int mode, nvs_handle_t *handle)
|
||||
{
|
||||
if (!initialized || strcmp(namespace_name, "maze32") != 0) return ESP_FAIL;
|
||||
if (mode == NVS_READONLY && !present) return ESP_ERR_NVS_NOT_FOUND;
|
||||
*handle = 1U;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void nvs_close(nvs_handle_t handle)
|
||||
{
|
||||
(void)handle;
|
||||
}
|
||||
|
||||
esp_err_t nvs_get_u32(nvs_handle_t handle, const char *key, uint32_t *value)
|
||||
{
|
||||
(void)handle;
|
||||
if (!present || strcmp(key, "cfg_ver") != 0) return ESP_ERR_NVS_NOT_FOUND;
|
||||
*value = schema;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_set_u32(nvs_handle_t handle, const char *key, uint32_t value)
|
||||
{
|
||||
(void)handle;
|
||||
if (strcmp(key, "cfg_ver") != 0) return ESP_FAIL;
|
||||
schema = value;
|
||||
present = 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_get_str(nvs_handle_t handle, const char *key, char *output, size_t *length)
|
||||
{
|
||||
(void)handle;
|
||||
const char *source = strcmp(key, "wifi_ssid") == 0 ? ssid :
|
||||
strcmp(key, "wifi_pass") == 0 ? password : NULL;
|
||||
if (!present || source == NULL) return ESP_ERR_NVS_NOT_FOUND;
|
||||
size_t required = strlen(source) + 1U;
|
||||
if (*length < required) return ESP_FAIL;
|
||||
memcpy(output, source, required);
|
||||
*length = required;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_set_str(nvs_handle_t handle, const char *key, const char *value)
|
||||
{
|
||||
(void)handle;
|
||||
char *destination = strcmp(key, "wifi_ssid") == 0 ? ssid :
|
||||
strcmp(key, "wifi_pass") == 0 ? password : NULL;
|
||||
size_t capacity = strcmp(key, "wifi_ssid") == 0 ? sizeof(ssid) : sizeof(password);
|
||||
if (destination == NULL || strlen(value) >= capacity) return ESP_FAIL;
|
||||
memcpy(destination, value, strlen(value) + 1U);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_erase_all(nvs_handle_t handle)
|
||||
{
|
||||
(void)handle;
|
||||
present = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t nvs_commit(nvs_handle_t handle)
|
||||
{
|
||||
(void)handle;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void fake_nvs_set_schema(uint32_t version)
|
||||
{
|
||||
schema = version;
|
||||
}
|
||||
Reference in New Issue
Block a user