IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
app_state.c File Reference
#include "app_state.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "esp_check.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_random.h"
#include "nvs.h"
+ Include dependency graph for app_state.c:

Go to the source code of this file.

Data Structures

struct  alarm_store_blob_t
 

Macros

#define APP_STATE_NAMESPACE   "clock"
 
#define APP_TIMEZONE_LOCATION_CUSTOM   "custom"
 
#define ALARM_BLOB_VERSION   1
 

Functions

static void app_state_set_defaults (app_config_t *config)
 
static void app_state_make_ap_ssid (char *buffer, size_t buffer_len)
 
static void app_state_generate_ap_password (char *buffer, size_t buffer_len)
 
static void app_state_load_string (nvs_handle_t handle, const char *key, char *buffer, size_t buffer_len)
 
static esp_err_t app_state_save_config_locked (void)
 
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.
 

Variables

static const char * TAG = "app_state"
 
static SemaphoreHandle_t s_state_lock
 
static app_state_snapshot_t s_state
 

Macro Definition Documentation

◆ ALARM_BLOB_VERSION

#define ALARM_BLOB_VERSION   1

Definition at line 24 of file app_state.c.

◆ APP_STATE_NAMESPACE

#define APP_STATE_NAMESPACE   "clock"

Definition at line 15 of file app_state.c.

◆ APP_TIMEZONE_LOCATION_CUSTOM

#define APP_TIMEZONE_LOCATION_CUSTOM   "custom"

Definition at line 16 of file app_state.c.

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_generate_ap_password()

static void app_state_generate_ap_password ( char *  buffer,
size_t  buffer_len 
)
static

Definition at line 51 of file app_state.c.

52{
53 static const char alphabet[] = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
54 const size_t alphabet_len = strlen(alphabet);
55
56 if (buffer_len < APP_AP_PASSWORD_MAX_LEN) {
57 if (buffer_len > 0) {
58 buffer[0] = '\0';
59 }
60 return;
61 }
62
63 for (size_t index = 0; index < APP_AP_PASSWORD_MAX_LEN - 1; ++index) {
64 buffer[index] = alphabet[esp_random() % alphabet_len];
65 }
66
67 buffer[APP_AP_PASSWORD_MAX_LEN - 1] = '\0';
68}
#define APP_AP_PASSWORD_MAX_LEN
Maximum provisioning AP password length (without null terminator).
Definition app_state.h:20
+ 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_load_string()

static void app_state_load_string ( nvs_handle_t  handle,
const char *  key,
char *  buffer,
size_t  buffer_len 
)
static

Definition at line 70 of file app_state.c.

74{
75 size_t required = buffer_len;
76
77 if (nvs_get_str(handle, key, buffer, &required) != ESP_OK) {
78 buffer[0] = '\0';
79 }
80}
+ Here is the caller graph for this function:

◆ app_state_make_ap_ssid()

static void app_state_make_ap_ssid ( char *  buffer,
size_t  buffer_len 
)
static

Definition at line 43 of file app_state.c.

44{
45 uint8_t mac[6] = {0};
46
47 esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
48 snprintf(buffer, buffer_len, "AlarmClock-%02X%02X", mac[4], mac[5]);
49}
+ 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_save_config_locked()

static esp_err_t app_state_save_config_locked ( void  )
static

Definition at line 82 of file app_state.c.

83{
84 nvs_handle_t handle;
85 esp_err_t ret = nvs_open(APP_STATE_NAMESPACE, NVS_READWRITE, &handle);
86 if (ret != ESP_OK) {
87 return ret;
88 }
89
90 ESP_GOTO_ON_ERROR(nvs_set_str(handle, "wifi_ssid", s_state.config.wifi_ssid), exit, TAG,
91 "save wifi_ssid failed");
92 ESP_GOTO_ON_ERROR(nvs_set_str(handle, "wifi_pass", s_state.config.wifi_password), exit, TAG,
93 "save wifi_pass failed");
94 ESP_GOTO_ON_ERROR(nvs_set_u8(handle, "wifi_cfg", s_state.config.wifi_configured ? 1 : 0),
95 exit, TAG, "save wifi_cfg failed");
96 ESP_GOTO_ON_ERROR(nvs_set_str(handle, "ap_pass", s_state.config.ap_password), exit, TAG,
97 "save ap_pass failed");
98 {
102 memcpy(blob.entries, s_state.config.alarms, sizeof(blob.entries));
103 ESP_GOTO_ON_ERROR(nvs_set_blob(handle, "alarm_list", &blob, sizeof(blob)), exit, TAG,
104 "save alarm_list failed");
105 }
106 ESP_GOTO_ON_ERROR(nvs_set_str(handle, "tz_loc", s_state.config.timezone_location), exit, TAG,
107 "save tz_loc failed");
108 ESP_GOTO_ON_ERROR(nvs_set_i16(handle, "tz_off", s_state.config.tz_offset_min), exit, TAG,
109 "save tz_off failed");
110 ESP_GOTO_ON_ERROR(nvs_set_u8(handle, "bright", s_state.config.brightness), exit, TAG,
111 "save bright failed");
112 ESP_GOTO_ON_ERROR(nvs_set_i64(handle, "epoch", s_state.config.saved_epoch), exit, TAG,
113 "save epoch failed");
114 ESP_GOTO_ON_ERROR(nvs_set_u8(handle, "epoch_ok", s_state.config.saved_time_valid ? 1 : 0),
115 exit, TAG, "save epoch_ok failed");
116 ret = nvs_commit(handle);
117
118exit:
119 nvs_close(handle);
120 return ret;
121}
+ 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_defaults()

static void app_state_set_defaults ( app_config_t *  config)
static

Definition at line 32 of file app_state.c.

33{
34 memset(config, 0, sizeof(*config));
35 config->alarm_count = 0;
37 config->tz_offset_min = 0;
38 config->brightness = 200;
39 config->saved_epoch = 0;
40 config->saved_time_valid = false;
41}
+ 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:

Variable Documentation

◆ s_state

app_state_snapshot_t s_state
static

Definition at line 21 of file app_state.c.

◆ s_state_lock

SemaphoreHandle_t s_state_lock
static

Definition at line 20 of file app_state.c.

◆ TAG

const char* TAG = "app_state"
static

Definition at line 18 of file app_state.c.