6#include "freertos/FreeRTOS.h"
7#include "freertos/semphr.h"
12#include "esp_random.h"
15#define APP_STATE_NAMESPACE "clock"
16#define APP_TIMEZONE_LOCATION_CUSTOM "custom"
18static const char *
TAG =
"app_state";
24#define ALARM_BLOB_VERSION 1
34 memset(config, 0,
sizeof(*config));
47 esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
48 snprintf(buffer, buffer_len,
"AlarmClock-%02X%02X", mac[4], mac[5]);
53 static const char alphabet[] =
"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
54 const size_t alphabet_len = strlen(alphabet);
64 buffer[index] = alphabet[esp_random() % alphabet_len];
75 size_t required = buffer_len;
77 if (nvs_get_str(handle, key, buffer, &required) != ESP_OK) {
91 "save wifi_ssid failed");
93 "save wifi_pass failed");
95 exit,
TAG,
"save wifi_cfg failed");
97 "save ap_pass failed");
103 ESP_GOTO_ON_ERROR(nvs_set_blob(handle,
"alarm_list", &blob,
sizeof(blob)), exit,
TAG,
104 "save alarm_list failed");
107 "save tz_loc failed");
109 "save tz_off failed");
111 "save bright failed");
113 "save epoch failed");
115 exit,
TAG,
"save epoch_ok failed");
116 ret = nvs_commit(handle);
128 return ESP_ERR_NO_MEM;
139 uint8_t u8_value = 0;
140 int16_t i16_value = 0;
141 int64_t i64_value = 0;
152 if (nvs_get_u8(handle,
"wifi_cfg", &u8_value) == ESP_OK) {
157 size_t blob_len =
sizeof(blob);
158 if (nvs_get_blob(handle,
"alarm_list", &blob, &blob_len) == ESP_OK
160 && blob_len ==
sizeof(blob)) {
166 if (nvs_get_i16(handle,
"tz_off", &i16_value) == ESP_OK) {
169 if (nvs_get_u8(handle,
"bright", &u8_value) == ESP_OK) {
172 if (nvs_get_i64(handle,
"epoch", &i64_value) == ESP_OK) {
175 if (nvs_get_u8(handle,
"epoch_ok", &u8_value) == ESP_OK) {
203 ESP_LOGI(
TAG,
"State initialized");
267 if (connected && ip_address != NULL) {
306 esp_err_t ret = ESP_ERR_NO_MEM;
324 esp_err_t ret = ESP_ERR_INVALID_ARG;
340 esp_err_t ret = ESP_ERR_INVALID_ARG;
358 esp_err_t ret = ESP_ERR_INVALID_ARG;
static void app_state_make_ap_ssid(char *buffer, size_t buffer_len)
#define ALARM_BLOB_VERSION
esp_err_t app_state_update_alarm(uint8_t index, uint8_t hour, uint8_t minute, bool enabled)
Replace an existing alarm entry by index.
static void app_state_load_string(nvs_handle_t handle, const char *key, char *buffer, size_t buffer_len)
esp_err_t app_state_init(void)
Initialize state, defaults, and NVS-backed values.
esp_err_t app_state_toggle_alarm(uint8_t index)
Toggle the enabled flag of an alarm by index.
void app_state_get_time_epoch(int64_t *epoch, bool *valid)
Get stored epoch value and validity flag.
void app_state_get_alarms(alarm_entry_t *out, uint8_t *count)
Get current alarm list.
#define APP_TIMEZONE_LOCATION_CUSTOM
static SemaphoreHandle_t s_state_lock
esp_err_t app_state_delete_alarm(uint8_t index)
Delete an alarm by index (subsequent alarms shift down).
static app_state_snapshot_t s_state
esp_err_t app_state_add_alarm(uint8_t hour, uint8_t minute, bool enabled)
Add a new alarm.
void app_state_set_wifi_connected(bool connected, const char *ip_address)
Update runtime Wi-Fi status and optional station IP.
esp_err_t app_state_reset_wifi_credentials(void)
Clear persisted Wi-Fi credentials.
void app_state_set_alarm_active(bool active)
Update runtime alarm active flag.
void app_state_get_config(app_config_t *config)
Get current persisted configuration.
#define APP_STATE_NAMESPACE
void app_state_get_snapshot(app_state_snapshot_t *snapshot)
Get a thread-safe snapshot of the full state.
static void app_state_set_defaults(app_config_t *config)
static esp_err_t app_state_save_config_locked(void)
esp_err_t app_state_set_time_epoch(int64_t epoch, bool valid)
Update stored epoch and validity flag.
static void app_state_generate_ap_password(char *buffer, size_t buffer_len)
void app_state_set_portal_mode(bool portal_mode)
Update runtime portal mode flag.
esp_err_t app_state_update_config(const app_config_t *config)
Replace persisted configuration with provided values.
Thread-safe application state and persisted configuration APIs.
#define APP_AP_PASSWORD_MAX_LEN
Maximum provisioning AP password length (without null terminator).
#define APP_ALARM_MAX_COUNT
Maximum number of configurable alarms.
alarm_entry_t entries[APP_ALARM_MAX_COUNT]
Persisted configuration model.
char wifi_password[APP_WIFI_PASSWORD_MAX_LEN+1]
char timezone_location[APP_TIMEZONE_LOCATION_MAX_LEN+1]
char ap_ssid[APP_AP_SSID_MAX_LEN+1]
char ap_password[APP_AP_PASSWORD_MAX_LEN+1]
char wifi_ssid[APP_WIFI_SSID_MAX_LEN+1]
alarm_entry_t alarms[APP_ALARM_MAX_COUNT]
char sta_ip[APP_IP_ADDRESS_MAX_LEN]
Snapshot of full app state.