IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
app_state.h File Reference

Thread-safe application state and persisted configuration APIs. More...

#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
+ Include dependency graph for app_state.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  alarm_entry_t
 Single alarm entry. More...
 
struct  app_config_t
 Persisted configuration model. More...
 
struct  app_runtime_t
 Runtime state model. More...
 
struct  app_state_snapshot_t
 Snapshot of full app state. More...
 

Macros

#define APP_WIFI_SSID_MAX_LEN   32
 Maximum Wi-Fi SSID length (without null terminator).
 
#define APP_WIFI_PASSWORD_MAX_LEN   64
 Maximum Wi-Fi password length (without null terminator).
 
#define APP_AP_SSID_MAX_LEN   32
 Maximum provisioning AP SSID length (without null terminator).
 
#define APP_AP_PASSWORD_MAX_LEN   16
 Maximum provisioning AP password length (without null terminator).
 
#define APP_TIMEZONE_LOCATION_MAX_LEN   32
 Maximum timezone location identifier length (without null terminator).
 
#define APP_IP_ADDRESS_MAX_LEN   16
 Maximum IPv4 address string length including null terminator.
 
#define APP_ALARM_MAX_COUNT   8
 Maximum number of configurable alarms.
 

Functions

esp_err_t app_state_init (void)
 Initialize state, defaults, and NVS-backed values.
 
void app_state_get_snapshot (app_state_snapshot_t *snapshot)
 Get a thread-safe snapshot of the full state.
 
void app_state_get_config (app_config_t *config)
 Get current persisted configuration.
 
esp_err_t app_state_update_config (const app_config_t *config)
 Replace persisted configuration with provided values.
 
void app_state_get_time_epoch (int64_t *epoch, bool *valid)
 Get stored epoch value and validity flag.
 
esp_err_t app_state_set_time_epoch (int64_t epoch, bool valid)
 Update stored epoch and validity flag.
 
void app_state_set_portal_mode (bool portal_mode)
 Update runtime portal mode flag.
 
void app_state_set_wifi_connected (bool connected, const char *ip_address)
 Update runtime Wi-Fi status and optional station IP.
 
void app_state_set_alarm_active (bool active)
 Update runtime alarm active flag.
 
esp_err_t app_state_reset_wifi_credentials (void)
 Clear persisted Wi-Fi credentials.
 
void app_state_get_alarms (alarm_entry_t *out, uint8_t *count)
 Get current alarm list.
 
esp_err_t app_state_add_alarm (uint8_t hour, uint8_t minute, bool enabled)
 Add a new alarm.
 
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.
 
esp_err_t app_state_delete_alarm (uint8_t index)
 Delete an alarm by index (subsequent alarms shift down).
 
esp_err_t app_state_toggle_alarm (uint8_t index)
 Toggle the enabled flag of an alarm by index.
 

Detailed Description

Thread-safe application state and persisted configuration APIs.

Definition in file app_state.h.

Macro Definition Documentation

◆ APP_ALARM_MAX_COUNT

#define APP_ALARM_MAX_COUNT   8

Maximum number of configurable alarms.

Definition at line 26 of file app_state.h.

◆ APP_AP_PASSWORD_MAX_LEN

#define APP_AP_PASSWORD_MAX_LEN   16

Maximum provisioning AP password length (without null terminator).

Definition at line 20 of file app_state.h.

◆ APP_AP_SSID_MAX_LEN

#define APP_AP_SSID_MAX_LEN   32

Maximum provisioning AP SSID length (without null terminator).

Definition at line 18 of file app_state.h.

◆ APP_IP_ADDRESS_MAX_LEN

#define APP_IP_ADDRESS_MAX_LEN   16

Maximum IPv4 address string length including null terminator.

Definition at line 24 of file app_state.h.

◆ APP_TIMEZONE_LOCATION_MAX_LEN

#define APP_TIMEZONE_LOCATION_MAX_LEN   32

Maximum timezone location identifier length (without null terminator).

Definition at line 22 of file app_state.h.

◆ APP_WIFI_PASSWORD_MAX_LEN

#define APP_WIFI_PASSWORD_MAX_LEN   64

Maximum Wi-Fi password length (without null terminator).

Definition at line 16 of file app_state.h.

◆ APP_WIFI_SSID_MAX_LEN

#define APP_WIFI_SSID_MAX_LEN   32

Maximum Wi-Fi SSID length (without null terminator).

Definition at line 14 of file app_state.h.

Function Documentation

◆ app_state_add_alarm()

esp_err_t app_state_add_alarm ( uint8_t  hour,
uint8_t  minute,
bool  enabled 
)

Add a new alarm.

Fails if APP_ALARM_MAX_COUNT is already reached.

Returns
ESP_OK on success, ESP_ERR_NO_MEM if list is full.

Definition at line 304 of file app_state.c.

305{
306 esp_err_t ret = ESP_ERR_NO_MEM;
307
308 xSemaphoreTake(s_state_lock, portMAX_DELAY);
310 uint8_t idx = s_state.config.alarm_count;
311 s_state.config.alarms[idx].hour = hour % 24;
312 s_state.config.alarms[idx].minute = minute % 60;
313 s_state.config.alarms[idx].enabled = enabled;
316 }
317 xSemaphoreGive(s_state_lock);
318
319 return ret;
320}
static SemaphoreHandle_t s_state_lock
Definition app_state.c:20
static app_state_snapshot_t s_state
Definition app_state.c:21
static esp_err_t app_state_save_config_locked(void)
Definition app_state.c:82
#define APP_ALARM_MAX_COUNT
Maximum number of configurable alarms.
Definition app_state.h:26
bool enabled
Definition app_state.h:34
uint8_t hour
Definition app_state.h:32
uint8_t minute
Definition app_state.h:33
alarm_entry_t alarms[APP_ALARM_MAX_COUNT]
Definition app_state.h:46
uint8_t alarm_count
Definition app_state.h:47
app_config_t config
Definition app_state.h:70
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_delete_alarm()

esp_err_t app_state_delete_alarm ( uint8_t  index)

Delete an alarm by index (subsequent alarms shift down).

Returns
ESP_OK on success, ESP_ERR_INVALID_ARG if index is out of range.

Definition at line 338 of file app_state.c.

339{
340 esp_err_t ret = ESP_ERR_INVALID_ARG;
341
342 xSemaphoreTake(s_state_lock, portMAX_DELAY);
343 if (index < s_state.config.alarm_count) {
344 for (uint8_t i = index; i + 1 < s_state.config.alarm_count; i++) {
346 }
350 }
351 xSemaphoreGive(s_state_lock);
352
353 return ret;
354}
Single alarm entry.
Definition app_state.h:31
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_get_alarms()

void app_state_get_alarms ( alarm_entry_t *  out,
uint8_t *  count 
)

Get current alarm list.

Parameters
[out]outBuffer of at least APP_ALARM_MAX_COUNT entries.
[out]countNumber of configured alarms.

Definition at line 296 of file app_state.c.

297{
298 xSemaphoreTake(s_state_lock, portMAX_DELAY);
299 *count = s_state.config.alarm_count;
301 xSemaphoreGive(s_state_lock);
302}
+ Here is the caller graph for this function:

◆ app_state_get_config()

void app_state_get_config ( app_config_t *  config)

Get current persisted configuration.

Parameters
[out]configDestination configuration buffer.

Definition at line 214 of file app_state.c.

215{
216 xSemaphoreTake(s_state_lock, portMAX_DELAY);
217 *config = s_state.config;
218 xSemaphoreGive(s_state_lock);
219}
+ Here is the caller graph for this function:

◆ app_state_get_snapshot()

void app_state_get_snapshot ( app_state_snapshot_t *  snapshot)

Get a thread-safe snapshot of the full state.

Parameters
[out]snapshotDestination snapshot buffer.

Definition at line 207 of file app_state.c.

208{
209 xSemaphoreTake(s_state_lock, portMAX_DELAY);
210 *snapshot = s_state;
211 xSemaphoreGive(s_state_lock);
212}
+ Here is the caller graph for this function:

◆ app_state_get_time_epoch()

void app_state_get_time_epoch ( int64_t *  epoch,
bool *  valid 
)

Get stored epoch value and validity flag.

Parameters
[out]epochStored epoch seconds.
[out]validStored validity state.

Definition at line 234 of file app_state.c.

235{
236 xSemaphoreTake(s_state_lock, portMAX_DELAY);
237 *epoch = s_state.config.saved_epoch;
239 xSemaphoreGive(s_state_lock);
240}
bool saved_time_valid
Definition app_state.h:52
int64_t saved_epoch
Definition app_state.h:51
+ Here is the caller graph for this function:

◆ app_state_init()

esp_err_t app_state_init ( void  )

Initialize state, defaults, and NVS-backed values.

Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 123 of file app_state.c.

124{
125 if (s_state_lock == NULL) {
126 s_state_lock = xSemaphoreCreateMutex();
127 if (s_state_lock == NULL) {
128 return ESP_ERR_NO_MEM;
129 }
130 }
131
132 memset(&s_state, 0, sizeof(s_state));
135
136 nvs_handle_t handle;
137 esp_err_t ret = nvs_open(APP_STATE_NAMESPACE, NVS_READWRITE, &handle);
138 if (ret == ESP_OK) {
139 uint8_t u8_value = 0;
140 int16_t i16_value = 0;
141 int64_t i64_value = 0;
142
143 app_state_load_string(handle, "wifi_ssid", s_state.config.wifi_ssid,
144 sizeof(s_state.config.wifi_ssid));
148 sizeof(s_state.config.ap_password));
151
152 if (nvs_get_u8(handle, "wifi_cfg", &u8_value) == ESP_OK) {
153 s_state.config.wifi_configured = (u8_value != 0);
154 }
155 {
157 size_t blob_len = sizeof(blob);
158 if (nvs_get_blob(handle, "alarm_list", &blob, &blob_len) == ESP_OK
159 && blob.version == ALARM_BLOB_VERSION
160 && blob_len == sizeof(blob)) {
161 uint8_t count = blob.count < APP_ALARM_MAX_COUNT ? blob.count : APP_ALARM_MAX_COUNT;
162 s_state.config.alarm_count = count;
163 memcpy(s_state.config.alarms, blob.entries, count * sizeof(alarm_entry_t));
164 }
165 }
166 if (nvs_get_i16(handle, "tz_off", &i16_value) == ESP_OK) {
167 s_state.config.tz_offset_min = i16_value;
168 }
169 if (nvs_get_u8(handle, "bright", &u8_value) == ESP_OK) {
170 s_state.config.brightness = u8_value;
171 }
172 if (nvs_get_i64(handle, "epoch", &i64_value) == ESP_OK) {
173 s_state.config.saved_epoch = i64_value;
174 }
175 if (nvs_get_u8(handle, "epoch_ok", &u8_value) == ESP_OK) {
176 s_state.config.saved_time_valid = (u8_value != 0);
177 }
178
179 if (s_state.config.timezone_location[0] == '\0') {
181 }
182
183 nvs_close(handle);
184 }
185
186 if (s_state.config.ap_password[0] == '\0') {
188 sizeof(s_state.config.ap_password));
189 xSemaphoreTake(s_state_lock, portMAX_DELAY);
191 xSemaphoreGive(s_state_lock);
192 if (ret != ESP_OK) {
193 return ret;
194 }
195 }
196
201 s_state.runtime.sta_ip[0] = '\0';
202
203 ESP_LOGI(TAG, "State initialized");
204 return ESP_OK;
205}
static void app_state_make_ap_ssid(char *buffer, size_t buffer_len)
Definition app_state.c:43
#define ALARM_BLOB_VERSION
Definition app_state.c:24
static void app_state_load_string(nvs_handle_t handle, const char *key, char *buffer, size_t buffer_len)
Definition app_state.c:70
static const char * TAG
Definition app_state.c:18
#define APP_TIMEZONE_LOCATION_CUSTOM
Definition app_state.c:16
#define APP_STATE_NAMESPACE
Definition app_state.c:15
static void app_state_set_defaults(app_config_t *config)
Definition app_state.c:32
static void app_state_generate_ap_password(char *buffer, size_t buffer_len)
Definition app_state.c:51
alarm_entry_t entries[APP_ALARM_MAX_COUNT]
Definition app_state.c:29
char wifi_password[APP_WIFI_PASSWORD_MAX_LEN+1]
Definition app_state.h:42
char timezone_location[APP_TIMEZONE_LOCATION_MAX_LEN+1]
Definition app_state.h:48
char ap_ssid[APP_AP_SSID_MAX_LEN+1]
Definition app_state.h:44
char ap_password[APP_AP_PASSWORD_MAX_LEN+1]
Definition app_state.h:45
int16_t tz_offset_min
Definition app_state.h:49
char wifi_ssid[APP_WIFI_SSID_MAX_LEN+1]
Definition app_state.h:41
uint8_t brightness
Definition app_state.h:50
bool wifi_configured
Definition app_state.h:43
bool portal_mode
Definition app_state.h:59
bool alarm_active
Definition app_state.h:61
bool time_valid
Definition app_state.h:62
bool wifi_connected
Definition app_state.h:60
char sta_ip[APP_IP_ADDRESS_MAX_LEN]
Definition app_state.h:63
app_runtime_t runtime
Definition app_state.h:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_reset_wifi_credentials()

esp_err_t app_state_reset_wifi_credentials ( void  )

Clear persisted Wi-Fi credentials.

Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 282 of file app_state.c.

283{
284 esp_err_t ret;
285
286 xSemaphoreTake(s_state_lock, portMAX_DELAY);
287 s_state.config.wifi_ssid[0] = '\0';
288 s_state.config.wifi_password[0] = '\0';
291 xSemaphoreGive(s_state_lock);
292
293 return ret;
294}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_set_alarm_active()

void app_state_set_alarm_active ( bool  active)

Update runtime alarm active flag.

Parameters
activetrue when alarm is active.

Definition at line 275 of file app_state.c.

276{
277 xSemaphoreTake(s_state_lock, portMAX_DELAY);
279 xSemaphoreGive(s_state_lock);
280}
+ Here is the caller graph for this function:

◆ app_state_set_portal_mode()

void app_state_set_portal_mode ( bool  portal_mode)

Update runtime portal mode flag.

Parameters
portal_modetrue for provisioning AP mode, false otherwise.

Definition at line 256 of file app_state.c.

257{
258 xSemaphoreTake(s_state_lock, portMAX_DELAY);
259 s_state.runtime.portal_mode = portal_mode;
260 xSemaphoreGive(s_state_lock);
261}
+ Here is the caller graph for this function:

◆ app_state_set_time_epoch()

esp_err_t app_state_set_time_epoch ( int64_t  epoch,
bool  valid 
)

Update stored epoch and validity flag.

Parameters
epochEpoch seconds to store.
validValidity flag to store.
Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 242 of file app_state.c.

243{
244 esp_err_t ret;
245
246 xSemaphoreTake(s_state_lock, portMAX_DELAY);
247 s_state.config.saved_epoch = epoch;
249 s_state.runtime.time_valid = valid;
251 xSemaphoreGive(s_state_lock);
252
253 return ret;
254}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_set_wifi_connected()

void app_state_set_wifi_connected ( bool  connected,
const char *  ip_address 
)

Update runtime Wi-Fi status and optional station IP.

Parameters
connectedtrue if connected.
ip_addressOptional station IP string when connected.

Definition at line 263 of file app_state.c.

264{
265 xSemaphoreTake(s_state_lock, portMAX_DELAY);
266 s_state.runtime.wifi_connected = connected;
267 if (connected && ip_address != NULL) {
268 snprintf(s_state.runtime.sta_ip, sizeof(s_state.runtime.sta_ip), "%s", ip_address);
269 } else {
270 s_state.runtime.sta_ip[0] = '\0';
271 }
272 xSemaphoreGive(s_state_lock);
273}
+ Here is the caller graph for this function:

◆ app_state_toggle_alarm()

esp_err_t app_state_toggle_alarm ( uint8_t  index)

Toggle the enabled flag of an alarm by index.

Returns
ESP_OK on success, ESP_ERR_INVALID_ARG if index is out of range.

Definition at line 356 of file app_state.c.

357{
358 esp_err_t ret = ESP_ERR_INVALID_ARG;
359
360 xSemaphoreTake(s_state_lock, portMAX_DELAY);
361 if (index < s_state.config.alarm_count) {
364 }
365 xSemaphoreGive(s_state_lock);
366
367 return ret;
368}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_update_alarm()

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.

Returns
ESP_OK on success, ESP_ERR_INVALID_ARG if index is out of range.

Definition at line 322 of file app_state.c.

323{
324 esp_err_t ret = ESP_ERR_INVALID_ARG;
325
326 xSemaphoreTake(s_state_lock, portMAX_DELAY);
327 if (index < s_state.config.alarm_count) {
328 s_state.config.alarms[index].hour = hour % 24;
329 s_state.config.alarms[index].minute = minute % 60;
330 s_state.config.alarms[index].enabled = enabled;
332 }
333 xSemaphoreGive(s_state_lock);
334
335 return ret;
336}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ app_state_update_config()

esp_err_t app_state_update_config ( const app_config_t *  config)

Replace persisted configuration with provided values.

Parameters
configSource configuration.
Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 221 of file app_state.c.

222{
223 esp_err_t ret;
224
225 xSemaphoreTake(s_state_lock, portMAX_DELAY);
226 s_state.config = *config;
229 xSemaphoreGive(s_state_lock);
230
231 return ret;
232}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: