IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
web_portal.c File Reference
#include "web_portal.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "esp_check.h"
#include "esp_event.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "mdns.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "sdkconfig.h"
#include "app_state.h"
#include "time_service.h"
+ Include dependency graph for web_portal.c:

Go to the source code of this file.

Data Structures

struct  web_portal_timezone_option_t
 

Macros

#define WIFI_CONNECTED_BIT   BIT0
 
#define WIFI_FAILED_BIT   BIT1
 
#define CONFIG_WEB_PORTAL_WIFI_CONNECT_TIMEOUT_MS   15000
 
#define CONFIG_WEB_PORTAL_AP_CHANNEL   1
 
#define CONFIG_WEB_PORTAL_HTTP_MAX_URI_HANDLERS   16
 
#define CONFIG_WEB_PORTAL_REBOOT_DELAY_MS   1000
 
#define CONFIG_WEB_PORTAL_MDNS_ENABLE   1
 
#define CONFIG_WEB_PORTAL_MDNS_HOSTNAME   "alarmclock"
 
#define CONFIG_WEB_PORTAL_MDNS_INSTANCE_NAME   "ESP32-C3 Alarm Clock"
 
#define CONFIG_WEB_PORTAL_LOG_AP_CREDENTIALS   0
 
#define WIFI_CONNECT_TIMEOUT_MS   CONFIG_WEB_PORTAL_WIFI_CONNECT_TIMEOUT_MS
 
#define WEB_PORTAL_TIMEZONE_CUSTOM_ID   "custom"
 

Functions

const uint8_t s_portal_html_start[] asm ("_binary_index_html_start")
 
const uint8_t s_portal_html_end[] asm ("_binary_index_html_end")
 
const uint8_t s_portal_css_start[] asm ("_binary_style_css_start")
 
const uint8_t s_portal_css_end[] asm ("_binary_style_css_end")
 
const uint8_t s_portal_js_start[] asm ("_binary_app_js_start")
 
const uint8_t s_portal_js_end[] asm ("_binary_app_js_end")
 
static void web_portal_copy_text (char *dest, size_t dest_len, const char *src)
 
static void web_portal_reboot_task (void *arg)
 
static void web_portal_schedule_reboot (void)
 
static void web_portal_start_mdns (uint16_t http_port)
 
static void web_portal_stop_mdns (void)
 
static void web_portal_send_redirect (httpd_req_t *req)
 
static char web_portal_hex_to_char (char high, char low)
 
static void web_portal_url_decode (char *dest, size_t dest_len, const char *src)
 
static bool web_portal_form_get_value (const char *body, const char *key, char *out, size_t out_len)
 
static esp_err_t web_portal_read_body (httpd_req_t *req, char *buffer, size_t buffer_len)
 
static int16_t web_portal_clamp_timezone_offset (long parsed)
 
static bool web_portal_timezone_location_valid (const char *location_id)
 
static void web_portal_wifi_event_handler (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
 
static esp_err_t web_portal_wifi_init_once (void)
 
static esp_err_t web_portal_start_ap (const app_config_t *config)
 
static esp_err_t web_portal_start_sta (const app_config_t *config)
 
static esp_err_t web_portal_root_get (httpd_req_t *req)
 
static esp_err_t web_portal_css_get (httpd_req_t *req)
 
static esp_err_t web_portal_js_get (httpd_req_t *req)
 
static esp_err_t web_portal_status_get (httpd_req_t *req)
 
static esp_err_t web_portal_config_get (httpd_req_t *req)
 
static esp_err_t web_portal_config_post (httpd_req_t *req)
 
static esp_err_t web_portal_time_post (httpd_req_t *req)
 
static esp_err_t web_portal_alarms_get (httpd_req_t *req)
 
static esp_err_t web_portal_alarms_post (httpd_req_t *req)
 
static esp_err_t web_portal_reset_wifi_post (httpd_req_t *req)
 
static esp_err_t web_portal_captive_redirect (httpd_req_t *req)
 
static esp_err_t web_portal_not_found_handler (httpd_req_t *req, httpd_err_code_t error)
 
static esp_err_t web_portal_start_http_server (void)
 
esp_err_t web_portal_start (void)
 Start Wi-Fi mode handling and HTTP portal service.
 

Variables

static const char * TAG = "web_portal"
 
static const web_portal_timezone_option_t s_timezone_options []
 
static const size_t s_timezone_option_count
 
static EventGroupHandle_t s_wifi_event_group
 
static httpd_handle_t s_http_server
 
static bool s_wifi_initialized
 
static int s_retry_count
 
static bool s_reboot_scheduled
 
static bool s_mdns_initialized
 
static uint16_t s_http_port = 80
 
static char s_last_sta_ssid [APP_WIFI_SSID_MAX_LEN+1]
 

Macro Definition Documentation

◆ CONFIG_WEB_PORTAL_AP_CHANNEL

#define CONFIG_WEB_PORTAL_AP_CHANNEL   1

Definition at line 35 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_HTTP_MAX_URI_HANDLERS

#define CONFIG_WEB_PORTAL_HTTP_MAX_URI_HANDLERS   16

Definition at line 39 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_LOG_AP_CREDENTIALS

#define CONFIG_WEB_PORTAL_LOG_AP_CREDENTIALS   0

Definition at line 59 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_MDNS_ENABLE

#define CONFIG_WEB_PORTAL_MDNS_ENABLE   1

Definition at line 47 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_MDNS_HOSTNAME

#define CONFIG_WEB_PORTAL_MDNS_HOSTNAME   "alarmclock"

Definition at line 51 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_MDNS_INSTANCE_NAME

#define CONFIG_WEB_PORTAL_MDNS_INSTANCE_NAME   "ESP32-C3 Alarm Clock"

Definition at line 55 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_REBOOT_DELAY_MS

#define CONFIG_WEB_PORTAL_REBOOT_DELAY_MS   1000

Definition at line 43 of file web_portal.c.

◆ CONFIG_WEB_PORTAL_WIFI_CONNECT_TIMEOUT_MS

#define CONFIG_WEB_PORTAL_WIFI_CONNECT_TIMEOUT_MS   15000

Definition at line 31 of file web_portal.c.

◆ WEB_PORTAL_TIMEZONE_CUSTOM_ID

#define WEB_PORTAL_TIMEZONE_CUSTOM_ID   "custom"

Definition at line 63 of file web_portal.c.

◆ WIFI_CONNECT_TIMEOUT_MS

#define WIFI_CONNECT_TIMEOUT_MS   CONFIG_WEB_PORTAL_WIFI_CONNECT_TIMEOUT_MS

Definition at line 62 of file web_portal.c.

◆ WIFI_CONNECTED_BIT

#define WIFI_CONNECTED_BIT   BIT0

Definition at line 27 of file web_portal.c.

◆ WIFI_FAILED_BIT

#define WIFI_FAILED_BIT   BIT1

Definition at line 28 of file web_portal.c.

Function Documentation

◆ asm() [1/6]

const uint8_t s_portal_js_end[] asm ( "_binary_app_js_end"  )
extern

◆ asm() [2/6]

const uint8_t s_portal_js_start[] asm ( "_binary_app_js_start"  )
extern

◆ asm() [3/6]

const uint8_t s_portal_html_end[] asm ( "_binary_index_html_end"  )
extern

◆ asm() [4/6]

const uint8_t s_portal_html_start[] asm ( "_binary_index_html_start"  )
extern

◆ asm() [5/6]

const uint8_t s_portal_css_end[] asm ( "_binary_style_css_end"  )
extern

◆ asm() [6/6]

const uint8_t s_portal_css_start[] asm ( "_binary_style_css_start"  )
extern

◆ web_portal_alarms_get()

static esp_err_t web_portal_alarms_get ( httpd_req_t *  req)
static

Definition at line 697 of file web_portal.c.

698{
700 uint8_t count = 0;
701 char json[512];
702 int written;
703 uint8_t i;
704
705 app_state_get_alarms(alarms, &count);
706
707 written = snprintf(json, sizeof(json), "{\"count\":%u,\"alarms\":[", (unsigned int)count);
708 for (i = 0; i < count && i < APP_ALARM_MAX_COUNT; i++) {
709 written += snprintf(json + written, (int)sizeof(json) - written,
710 "%s{\"index\":%u,\"hour\":%u,\"minute\":%u,\"enabled\":%s}",
711 (i > 0) ? "," : "",
712 (unsigned int)i,
713 (unsigned int)alarms[i].hour,
714 (unsigned int)alarms[i].minute,
715 alarms[i].enabled ? "true" : "false");
716 }
717 snprintf(json + written, (int)sizeof(json) - written, "]}");
718
719 httpd_resp_set_type(req, "application/json");
720 httpd_resp_send(req, json, HTTPD_RESP_USE_STRLEN);
721 return ESP_OK;
722}
void app_state_get_alarms(alarm_entry_t *out, uint8_t *count)
Get current alarm list.
Definition app_state.c:296
#define APP_ALARM_MAX_COUNT
Maximum number of configurable alarms.
Definition app_state.h:26
Single alarm entry.
Definition app_state.h:31
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_alarms_post()

static esp_err_t web_portal_alarms_post ( httpd_req_t *  req)
static

Definition at line 724 of file web_portal.c.

725{
726 char body[256];
727 char action[16];
728 char value[16];
729 esp_err_t ret = ESP_FAIL;
730
731 ESP_RETURN_ON_ERROR(web_portal_read_body(req, body, sizeof(body)), TAG, "read alarm body");
732
733 if (!web_portal_form_get_value(body, "action", action, sizeof(action))) {
734 httpd_resp_set_status(req, "400 Bad Request");
735 httpd_resp_set_type(req, "application/json");
736 httpd_resp_send(req, "{\"ok\":false,\"error\":\"missing action\"}", HTTPD_RESP_USE_STRLEN);
737 return ESP_OK;
738 }
739
740 if (strcmp(action, "add") == 0) {
741 uint8_t hour = 0, minute = 0;
742 bool enabled = true;
743 if (web_portal_form_get_value(body, "hour", value, sizeof(value))) {
744 long v = strtol(value, NULL, 10);
745 hour = (uint8_t)(v < 0 ? 0 : v > 23 ? 23 : v);
746 }
747 if (web_portal_form_get_value(body, "minute", value, sizeof(value))) {
748 long v = strtol(value, NULL, 10);
749 minute = (uint8_t)(v < 0 ? 0 : v > 59 ? 59 : v);
750 }
751 if (web_portal_form_get_value(body, "enabled", value, sizeof(value))) {
752 enabled = (strtol(value, NULL, 10) != 0);
753 }
754 ret = app_state_add_alarm(hour, minute, enabled);
755 } else if (strcmp(action, "update") == 0) {
756 uint8_t index = 0, hour = 0, minute = 0;
757 bool enabled = true;
758 if (web_portal_form_get_value(body, "index", value, sizeof(value))) {
759 long v = strtol(value, NULL, 10);
760 index = (uint8_t)(v < 0 ? 0 : v);
761 }
762 if (web_portal_form_get_value(body, "hour", value, sizeof(value))) {
763 long v = strtol(value, NULL, 10);
764 hour = (uint8_t)(v < 0 ? 0 : v > 23 ? 23 : v);
765 }
766 if (web_portal_form_get_value(body, "minute", value, sizeof(value))) {
767 long v = strtol(value, NULL, 10);
768 minute = (uint8_t)(v < 0 ? 0 : v > 59 ? 59 : v);
769 }
770 if (web_portal_form_get_value(body, "enabled", value, sizeof(value))) {
771 enabled = (strtol(value, NULL, 10) != 0);
772 }
773 ret = app_state_update_alarm(index, hour, minute, enabled);
774 } else if (strcmp(action, "delete") == 0) {
775 uint8_t index = 0;
776 if (web_portal_form_get_value(body, "index", value, sizeof(value))) {
777 long v = strtol(value, NULL, 10);
778 index = (uint8_t)(v < 0 ? 0 : v);
779 }
780 ret = app_state_delete_alarm(index);
781 } else if (strcmp(action, "toggle") == 0) {
782 uint8_t index = 0;
783 if (web_portal_form_get_value(body, "index", value, sizeof(value))) {
784 long v = strtol(value, NULL, 10);
785 index = (uint8_t)(v < 0 ? 0 : v);
786 }
787 ret = app_state_toggle_alarm(index);
788 }
789
790 httpd_resp_set_type(req, "application/json");
791 httpd_resp_send(req,
792 ret == ESP_OK ? "{\"ok\":true}" : "{\"ok\":false}",
793 HTTPD_RESP_USE_STRLEN);
794 return ESP_OK;
795}
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.
Definition app_state.c:322
esp_err_t app_state_toggle_alarm(uint8_t index)
Toggle the enabled flag of an alarm by index.
Definition app_state.c:356
esp_err_t app_state_delete_alarm(uint8_t index)
Delete an alarm by index (subsequent alarms shift down).
Definition app_state.c:338
esp_err_t app_state_add_alarm(uint8_t hour, uint8_t minute, bool enabled)
Add a new alarm.
Definition app_state.c:304
static bool web_portal_form_get_value(const char *body, const char *key, char *out, size_t out_len)
Definition web_portal.c:223
static const char * TAG
Definition web_portal.c:72
static esp_err_t web_portal_read_body(httpd_req_t *req, char *buffer, size_t buffer_len)
Definition web_portal.c:254
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_captive_redirect()

static esp_err_t web_portal_captive_redirect ( httpd_req_t *  req)
static

Definition at line 808 of file web_portal.c.

809{
811 return ESP_OK;
812}
static void web_portal_send_redirect(httpd_req_t *req)
Definition web_portal.c:189
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_clamp_timezone_offset()

static int16_t web_portal_clamp_timezone_offset ( long  parsed)
static

Definition at line 276 of file web_portal.c.

277{
278 if (parsed < -720) {
279 parsed = -720;
280 }
281 if (parsed > 840) {
282 parsed = 840;
283 }
284 return (int16_t)parsed;
285}
+ Here is the caller graph for this function:

◆ web_portal_config_get()

static esp_err_t web_portal_config_get ( httpd_req_t *  req)
static

Definition at line 551 of file web_portal.c.

552{
553 app_state_snapshot_t snapshot;
554 char json[512];
555
556 app_state_get_snapshot(&snapshot);
557 snprintf(json, sizeof(json),
558 "{\"wifi_ssid\":\"%s\",\"wifi_configured\":%s,"
559 "\"timezone\":%d,\"timezone_location\":\"%s\","
560 "\"brightness\":%u,\"ap_ssid\":\"%s\"}",
561 snapshot.config.wifi_ssid,
562 snapshot.config.wifi_configured ? "true" : "false",
563 snapshot.config.tz_offset_min,
564 snapshot.config.timezone_location,
565 snapshot.config.brightness,
566 snapshot.config.ap_ssid);
567
568 httpd_resp_set_type(req, "application/json");
569 httpd_resp_send(req, json, HTTPD_RESP_USE_STRLEN);
570 return ESP_OK;
571}
void app_state_get_snapshot(app_state_snapshot_t *snapshot)
Get a thread-safe snapshot of the full state.
Definition app_state.c:207
char wifi_ssid[APP_WIFI_SSID_MAX_LEN+1]
Definition app_state.h:41
bool wifi_configured
Definition app_state.h:43
Snapshot of full app state.
Definition app_state.h:69
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:

◆ web_portal_config_post()

static esp_err_t web_portal_config_post ( httpd_req_t *  req)
static

Definition at line 573 of file web_portal.c.

574{
575 app_state_snapshot_t snapshot;
576 app_config_t config;
577 char body[512];
578 char value[128];
579 char requested_timezone_location[APP_TIMEZONE_LOCATION_MAX_LEN + 1];
580 int16_t requested_tz_offset;
581 bool reboot_required = false;
582 bool timezone_offset_updated = false;
583
584 ESP_RETURN_ON_ERROR(web_portal_read_body(req, body, sizeof(body)), TAG, "read body failed");
585
586 app_state_get_snapshot(&snapshot);
587 config = snapshot.config;
588 requested_tz_offset = config.tz_offset_min;
589 web_portal_copy_text(requested_timezone_location,
590 sizeof(requested_timezone_location),
591 config.timezone_location);
592 if (!web_portal_timezone_location_valid(requested_timezone_location)) {
593 web_portal_copy_text(requested_timezone_location,
594 sizeof(requested_timezone_location),
596 }
597
598 if (web_portal_form_get_value(body, "wifi_ssid", value, sizeof(value))) {
599 if (strcmp(config.wifi_ssid, value) != 0) {
600 reboot_required = true;
601 }
602 web_portal_copy_text(config.wifi_ssid, sizeof(config.wifi_ssid), value);
603 config.wifi_configured = (value[0] != '\0');
604 }
605
606 if (web_portal_form_get_value(body, "wifi_password", value, sizeof(value))) {
607 if (strcmp(config.wifi_password, value) != 0) {
608 reboot_required = true;
609 }
610 web_portal_copy_text(config.wifi_password, sizeof(config.wifi_password), value);
611 }
612
613 if (web_portal_form_get_value(body, "timezone_location", value, sizeof(value))) {
614 web_portal_copy_text(requested_timezone_location,
615 sizeof(requested_timezone_location),
616 value);
617 if (!web_portal_timezone_location_valid(requested_timezone_location)) {
618 web_portal_copy_text(requested_timezone_location,
619 sizeof(requested_timezone_location),
621 }
622 }
623
624 if (web_portal_form_get_value(body, "timezone", value, sizeof(value))) {
625 requested_tz_offset = web_portal_clamp_timezone_offset(strtol(value, NULL, 10));
626 timezone_offset_updated = true;
627 }
628
629 /* For named locations, tz_offset_min is unused by the POSIX TZ lookup but
630 * preserved for the "custom" fallback. Only update it for custom mode. */
631 if (strcmp(requested_timezone_location, WEB_PORTAL_TIMEZONE_CUSTOM_ID) == 0
632 && timezone_offset_updated) {
633 config.tz_offset_min = requested_tz_offset;
634 }
635
637 sizeof(config.timezone_location),
638 requested_timezone_location);
639
640 if (web_portal_form_get_value(body, "brightness", value, sizeof(value))) {
641 long parsed = strtol(value, NULL, 10);
642 if (parsed < 0) {
643 parsed = 0;
644 }
645 if (parsed > 255) {
646 parsed = 255;
647 }
648 config.brightness = (uint8_t)parsed;
649 }
650
651 if (web_portal_form_get_value(body, "epoch", value, sizeof(value))) {
652 int64_t epoch = strtoll(value, NULL, 10);
653 if (epoch > 0) {
654 config.saved_epoch = epoch;
655 config.saved_time_valid = true;
656 ESP_RETURN_ON_ERROR(time_service_set_epoch(epoch), TAG, "set epoch failed");
657 }
658 }
659
660 ESP_RETURN_ON_ERROR(app_state_update_config(&config), TAG, "save config failed");
661
662 /* Apply new timezone immediately so the device clock is correct at once. */
664
665 httpd_resp_set_type(req, "text/html");
666 httpd_resp_send(req,
667 reboot_required
668 ? "<html><body><p>Saved. Rebooting into the updated Wi-Fi mode...</p></body></html>"
669 : "<html><body><p>Saved.</p><p><a href=\"/\">Back</a></p></body></html>",
670 HTTPD_RESP_USE_STRLEN);
671
672 if (reboot_required && config.wifi_configured) {
674 }
675
676 return ESP_OK;
677}
esp_err_t app_state_update_config(const app_config_t *config)
Replace persisted configuration with provided values.
Definition app_state.c:221
#define APP_TIMEZONE_LOCATION_MAX_LEN
Maximum timezone location identifier length (without null terminator).
Definition app_state.h:22
Persisted configuration model.
Definition app_state.h:40
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
int16_t tz_offset_min
Definition app_state.h:49
uint8_t brightness
Definition app_state.h:50
bool saved_time_valid
Definition app_state.h:52
int64_t saved_epoch
Definition app_state.h:51
void time_service_apply_timezone(const char *location_id, int16_t fallback_offset_min)
Apply a timezone by IANA location ID with DST support.
esp_err_t time_service_set_epoch(int64_t epoch)
Set current UTC epoch and persist it as valid time.
static int16_t web_portal_clamp_timezone_offset(long parsed)
Definition web_portal.c:276
static void web_portal_copy_text(char *dest, size_t dest_len, const char *src)
Definition web_portal.c:109
static bool web_portal_timezone_location_valid(const char *location_id)
Definition web_portal.c:287
static void web_portal_schedule_reboot(void)
Definition web_portal.c:129
#define WEB_PORTAL_TIMEZONE_CUSTOM_ID
Definition web_portal.c:63
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_copy_text()

static void web_portal_copy_text ( char *  dest,
size_t  dest_len,
const char *  src 
)
static

Definition at line 109 of file web_portal.c.

110{
111 size_t copy_len;
112
113 if (dest_len == 0) {
114 return;
115 }
116
117 copy_len = strnlen(src, dest_len - 1);
118 memcpy(dest, src, copy_len);
119 dest[copy_len] = '\0';
120}
+ Here is the caller graph for this function:

◆ web_portal_css_get()

static esp_err_t web_portal_css_get ( httpd_req_t *  req)
static

Definition at line 503 of file web_portal.c.

504{
505 size_t len = (size_t)(s_portal_css_end - s_portal_css_start);
506
507 httpd_resp_set_type(req, "text/css");
508 httpd_resp_send(req, (const char *)s_portal_css_start, (ssize_t)len);
509 return ESP_OK;
510}
+ Here is the caller graph for this function:

◆ web_portal_form_get_value()

static bool web_portal_form_get_value ( const char *  body,
const char *  key,
char *  out,
size_t  out_len 
)
static

Definition at line 223 of file web_portal.c.

224{
225 const size_t key_len = strlen(key);
226 const char *cursor = body;
227
228 while (cursor != NULL && *cursor != '\0') {
229 if (strncmp(cursor, key, key_len) == 0 && cursor[key_len] == '=') {
230 const char *value_start = cursor + key_len + 1;
231 const char *value_end = strchr(value_start, '&');
232 size_t value_len = value_end != NULL ? (size_t)(value_end - value_start) : strlen(value_start);
233 char encoded[128];
234
235 if (value_len >= sizeof(encoded)) {
236 value_len = sizeof(encoded) - 1;
237 }
238
239 memcpy(encoded, value_start, value_len);
240 encoded[value_len] = '\0';
241 web_portal_url_decode(out, out_len, encoded);
242 return true;
243 }
244
245 cursor = strchr(cursor, '&');
246 if (cursor != NULL) {
247 cursor++;
248 }
249 }
250
251 return false;
252}
static void web_portal_url_decode(char *dest, size_t dest_len, const char *src)
Definition web_portal.c:204
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_hex_to_char()

static char web_portal_hex_to_char ( char  high,
char  low 
)
static

Definition at line 196 of file web_portal.c.

197{
198 int high_val = isdigit((unsigned char)high) ? high - '0' : (toupper((unsigned char)high) - 'A' + 10);
199 int low_val = isdigit((unsigned char)low) ? low - '0' : (toupper((unsigned char)low) - 'A' + 10);
200
201 return (char)((high_val << 4) | low_val);
202}
+ Here is the caller graph for this function:

◆ web_portal_js_get()

static esp_err_t web_portal_js_get ( httpd_req_t *  req)
static

Definition at line 512 of file web_portal.c.

513{
514 size_t len = (size_t)(s_portal_js_end - s_portal_js_start);
515
516 httpd_resp_set_type(req, "application/javascript");
517 httpd_resp_send(req, (const char *)s_portal_js_start, (ssize_t)len);
518 return ESP_OK;
519}
+ Here is the caller graph for this function:

◆ web_portal_not_found_handler()

static esp_err_t web_portal_not_found_handler ( httpd_req_t *  req,
httpd_err_code_t  error 
)
static

Definition at line 814 of file web_portal.c.

815{
816 (void)error;
817
818 httpd_resp_set_status(req, "303 See Other");
819 httpd_resp_set_hdr(req, "Location", "/");
820 httpd_resp_send(req, "Redirect to captive portal", HTTPD_RESP_USE_STRLEN);
821 return ESP_OK;
822}
+ Here is the caller graph for this function:

◆ web_portal_read_body()

static esp_err_t web_portal_read_body ( httpd_req_t *  req,
char *  buffer,
size_t  buffer_len 
)
static

Definition at line 254 of file web_portal.c.

255{
256 const int total_len = req->content_len;
257 int received = 0;
258
259 if (total_len < 0 || (size_t)total_len >= buffer_len) {
260 return ESP_ERR_NO_MEM;
261 }
262
263 while (received < total_len) {
264 const int read_len = httpd_req_recv(req, buffer + received, total_len - received);
265 if (read_len <= 0) {
266 return ESP_FAIL;
267 }
268
269 received += read_len;
270 }
271
272 buffer[received] = '\0';
273 return ESP_OK;
274}
+ Here is the caller graph for this function:

◆ web_portal_reboot_task()

static void web_portal_reboot_task ( void *  arg)
static

Definition at line 122 of file web_portal.c.

123{
124 (void)arg;
125 vTaskDelay(pdMS_TO_TICKS(CONFIG_WEB_PORTAL_REBOOT_DELAY_MS));
126 esp_restart();
127}
#define CONFIG_WEB_PORTAL_REBOOT_DELAY_MS
Definition web_portal.c:43
+ Here is the caller graph for this function:

◆ web_portal_reset_wifi_post()

static esp_err_t web_portal_reset_wifi_post ( httpd_req_t *  req)
static

Definition at line 797 of file web_portal.c.

798{
799 ESP_RETURN_ON_ERROR(app_state_reset_wifi_credentials(), TAG, "reset wifi failed");
800 httpd_resp_set_type(req, "text/html");
801 httpd_resp_send(req,
802 "<html><body><p>Wi-Fi settings cleared. Rebooting into setup mode...</p></body></html>",
803 HTTPD_RESP_USE_STRLEN);
805 return ESP_OK;
806}
esp_err_t app_state_reset_wifi_credentials(void)
Clear persisted Wi-Fi credentials.
Definition app_state.c:282
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_root_get()

static esp_err_t web_portal_root_get ( httpd_req_t *  req)
static

Definition at line 494 of file web_portal.c.

495{
496 size_t len = (size_t)(s_portal_html_end - s_portal_html_start);
497
498 httpd_resp_set_type(req, "text/html");
499 httpd_resp_send(req, (const char *)s_portal_html_start, (ssize_t)len);
500 return ESP_OK;
501}
+ Here is the caller graph for this function:

◆ web_portal_schedule_reboot()

static void web_portal_schedule_reboot ( void  )
static

Definition at line 129 of file web_portal.c.

130{
131 if (s_reboot_scheduled) {
132 return;
133 }
134
135 s_reboot_scheduled = true;
136 xTaskCreate(web_portal_reboot_task, "portal_reboot", 2048, NULL, 3, NULL);
137}
static void web_portal_reboot_task(void *arg)
Definition web_portal.c:122
static bool s_reboot_scheduled
Definition web_portal.c:104
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_send_redirect()

static void web_portal_send_redirect ( httpd_req_t *  req)
static

Definition at line 189 of file web_portal.c.

190{
191 httpd_resp_set_status(req, "302 Found");
192 httpd_resp_set_hdr(req, "Location", "/");
193 httpd_resp_send(req, NULL, 0);
194}
+ Here is the caller graph for this function:

◆ web_portal_start()

esp_err_t web_portal_start ( void  )

Start Wi-Fi mode handling and HTTP portal service.

Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 882 of file web_portal.c.

883{
884 app_config_t config;
885
886 ESP_RETURN_ON_ERROR(web_portal_wifi_init_once(), TAG, "wifi init failed");
887
888 app_state_get_config(&config);
889
890 if (config.wifi_configured && config.wifi_ssid[0] != '\0' && web_portal_start_sta(&config) == ESP_OK) {
892 } else {
893 ESP_RETURN_ON_ERROR(web_portal_start_ap(&config), TAG, "AP start failed");
894 }
895
897}
void app_state_get_config(app_config_t *config)
Get current persisted configuration.
Definition app_state.c:214
void app_state_set_portal_mode(bool portal_mode)
Update runtime portal mode flag.
Definition app_state.c:256
static esp_err_t web_portal_start_sta(const app_config_t *config)
Definition web_portal.c:451
static esp_err_t web_portal_start_ap(const app_config_t *config)
Definition web_portal.c:402
static esp_err_t web_portal_start_http_server(void)
Definition web_portal.c:824
static esp_err_t web_portal_wifi_init_once(void)
Definition web_portal.c:367
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_start_ap()

static esp_err_t web_portal_start_ap ( const app_config_t *  config)
static

Definition at line 402 of file web_portal.c.

403{
404 wifi_config_t ap_cfg = {0};
405 size_t ap_password_len;
406
407 web_portal_copy_text((char *)ap_cfg.ap.ssid, sizeof(ap_cfg.ap.ssid), config->ap_ssid);
408 ap_cfg.ap.ssid_len = strlen(config->ap_ssid);
409 ap_cfg.ap.channel = CONFIG_WEB_PORTAL_AP_CHANNEL;
410 ap_cfg.ap.max_connection = 4;
411
412 ap_password_len = strnlen(config->ap_password, sizeof(config->ap_password));
413 if (ap_password_len >= 8 && ap_password_len <= 63) {
414 web_portal_copy_text((char *)ap_cfg.ap.password, sizeof(ap_cfg.ap.password), config->ap_password);
415 ap_cfg.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
416 ap_cfg.ap.pmf_cfg.required = false;
417 ap_cfg.ap.pmf_cfg.capable = true;
418 } else {
419 ap_cfg.ap.password[0] = '\0';
420 ap_cfg.ap.authmode = WIFI_AUTH_OPEN;
421 ap_cfg.ap.pmf_cfg.required = false;
422 ap_cfg.ap.pmf_cfg.capable = false;
423 ESP_LOGW(TAG,
424 "AP password length %u invalid for WPA/WPA2, starting open AP",
425 (unsigned int)ap_password_len);
426 }
427
428 esp_wifi_stop();
430 ESP_RETURN_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_AP), TAG, "set AP mode failed");
431 ESP_RETURN_ON_ERROR(esp_wifi_set_config(WIFI_IF_AP, &ap_cfg), TAG, "set AP config failed");
432 ESP_RETURN_ON_ERROR(esp_wifi_start(), TAG, "start AP failed");
433
435 app_state_set_wifi_connected(false, NULL);
437 ESP_LOGI(TAG,
438 "Started setup AP %s (ch=%u, auth=%s)",
439 config->ap_ssid,
440 (unsigned int)ap_cfg.ap.channel,
441 ap_cfg.ap.authmode == WIFI_AUTH_OPEN ? "open" : "wpa/wpa2");
442#if CONFIG_WEB_PORTAL_LOG_AP_CREDENTIALS
443 ESP_LOGW(TAG,
444 "DEBUG AP credentials: ssid=\"%s\" password=\"%s\"",
445 config->ap_ssid,
446 ap_cfg.ap.authmode == WIFI_AUTH_OPEN ? "<open>" : config->ap_password);
447#endif
448 return ESP_OK;
449}
void app_state_set_wifi_connected(bool connected, const char *ip_address)
Update runtime Wi-Fi status and optional station IP.
Definition app_state.c:263
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
void time_service_notify_network_connected(bool connected)
Notify the service about network connectivity changes.
static void web_portal_stop_mdns(void)
Definition web_portal.c:176
#define CONFIG_WEB_PORTAL_AP_CHANNEL
Definition web_portal.c:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_start_http_server()

static esp_err_t web_portal_start_http_server ( void  )
static

Definition at line 824 of file web_portal.c.

825{
826 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
827 app_state_snapshot_t snapshot;
828
829 if (s_http_server != NULL) {
830 return ESP_OK;
831 }
832
833 config.max_uri_handlers = CONFIG_WEB_PORTAL_HTTP_MAX_URI_HANDLERS;
834 config.lru_purge_enable = true;
835
836 ESP_RETURN_ON_ERROR(httpd_start(&s_http_server, &config), TAG, "httpd_start failed");
837
838 httpd_uri_t root = {.uri = "/", .method = HTTP_GET, .handler = web_portal_root_get, .user_ctx = NULL};
839 httpd_uri_t css = {.uri = "/style.css", .method = HTTP_GET, .handler = web_portal_css_get, .user_ctx = NULL};
840 httpd_uri_t js = {.uri = "/app.js", .method = HTTP_GET, .handler = web_portal_js_get, .user_ctx = NULL};
841 httpd_uri_t status = {.uri = "/api/status", .method = HTTP_GET, .handler = web_portal_status_get, .user_ctx = NULL};
842 httpd_uri_t config_get = {.uri = "/api/config", .method = HTTP_GET, .handler = web_portal_config_get, .user_ctx = NULL};
843 httpd_uri_t config_post = {.uri = "/api/config", .method = HTTP_POST, .handler = web_portal_config_post, .user_ctx = NULL};
844 httpd_uri_t alarms_get = {.uri = "/api/alarms", .method = HTTP_GET, .handler = web_portal_alarms_get, .user_ctx = NULL};
845 httpd_uri_t alarms_post = {.uri = "/api/alarms", .method = HTTP_POST, .handler = web_portal_alarms_post, .user_ctx = NULL};
846 httpd_uri_t time_post = {.uri = "/api/time", .method = HTTP_POST, .handler = web_portal_time_post, .user_ctx = NULL};
847 httpd_uri_t reset_post = {.uri = "/api/reset-wifi", .method = HTTP_POST, .handler = web_portal_reset_wifi_post, .user_ctx = NULL};
848 httpd_uri_t gen204a = {.uri = "/generate_204", .method = HTTP_GET, .handler = web_portal_captive_redirect, .user_ctx = NULL};
849 httpd_uri_t gen204b = {.uri = "/gen_204", .method = HTTP_GET, .handler = web_portal_captive_redirect, .user_ctx = NULL};
850 httpd_uri_t hotspot = {.uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = web_portal_captive_redirect, .user_ctx = NULL};
851 httpd_uri_t conntest = {.uri = "/connecttest.txt", .method = HTTP_GET, .handler = web_portal_captive_redirect, .user_ctx = NULL};
852 httpd_uri_t ncsi = {.uri = "/ncsi.txt", .method = HTTP_GET, .handler = web_portal_captive_redirect, .user_ctx = NULL};
853 httpd_uri_t fwlink = {.uri = "/fwlink", .method = HTTP_GET, .handler = web_portal_captive_redirect, .user_ctx = NULL};
854
855 httpd_register_uri_handler(s_http_server, &root);
856 httpd_register_uri_handler(s_http_server, &css);
857 httpd_register_uri_handler(s_http_server, &js);
858 httpd_register_uri_handler(s_http_server, &status);
859 httpd_register_uri_handler(s_http_server, &config_get);
860 httpd_register_uri_handler(s_http_server, &config_post);
861 httpd_register_uri_handler(s_http_server, &alarms_get);
862 httpd_register_uri_handler(s_http_server, &alarms_post);
863 httpd_register_uri_handler(s_http_server, &time_post);
864 httpd_register_uri_handler(s_http_server, &reset_post);
865 httpd_register_uri_handler(s_http_server, &gen204a);
866 httpd_register_uri_handler(s_http_server, &gen204b);
867 httpd_register_uri_handler(s_http_server, &hotspot);
868 httpd_register_uri_handler(s_http_server, &conntest);
869 httpd_register_uri_handler(s_http_server, &ncsi);
870 httpd_register_uri_handler(s_http_server, &fwlink);
871 httpd_register_err_handler(s_http_server, HTTPD_404_NOT_FOUND, web_portal_not_found_handler);
872
873 s_http_port = config.server_port;
874 app_state_get_snapshot(&snapshot);
875 if (!snapshot.runtime.portal_mode && snapshot.runtime.wifi_connected) {
877 }
878
879 return ESP_OK;
880}
bool portal_mode
Definition app_state.h:59
bool wifi_connected
Definition app_state.h:60
app_runtime_t runtime
Definition app_state.h:71
static void web_portal_start_mdns(uint16_t http_port)
Definition web_portal.c:139
static uint16_t s_http_port
Definition web_portal.c:106
static esp_err_t web_portal_reset_wifi_post(httpd_req_t *req)
Definition web_portal.c:797
static esp_err_t web_portal_alarms_post(httpd_req_t *req)
Definition web_portal.c:724
static esp_err_t web_portal_status_get(httpd_req_t *req)
Definition web_portal.c:521
static esp_err_t web_portal_root_get(httpd_req_t *req)
Definition web_portal.c:494
static esp_err_t web_portal_alarms_get(httpd_req_t *req)
Definition web_portal.c:697
static esp_err_t web_portal_js_get(httpd_req_t *req)
Definition web_portal.c:512
static esp_err_t web_portal_not_found_handler(httpd_req_t *req, httpd_err_code_t error)
Definition web_portal.c:814
static esp_err_t web_portal_time_post(httpd_req_t *req)
Definition web_portal.c:679
static esp_err_t web_portal_config_get(httpd_req_t *req)
Definition web_portal.c:551
static esp_err_t web_portal_config_post(httpd_req_t *req)
Definition web_portal.c:573
#define CONFIG_WEB_PORTAL_HTTP_MAX_URI_HANDLERS
Definition web_portal.c:39
static esp_err_t web_portal_captive_redirect(httpd_req_t *req)
Definition web_portal.c:808
static httpd_handle_t s_http_server
Definition web_portal.c:101
static esp_err_t web_portal_css_get(httpd_req_t *req)
Definition web_portal.c:503
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_start_mdns()

static void web_portal_start_mdns ( uint16_t  http_port)
static

Definition at line 139 of file web_portal.c.

140{
141#if CONFIG_WEB_PORTAL_MDNS_ENABLE
142 esp_err_t ret;
143
144 if (s_mdns_initialized) {
145 return;
146 }
147
148 ret = mdns_init();
149 if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) {
150 ESP_LOGW(TAG, "mDNS init failed: %s", esp_err_to_name(ret));
151 return;
152 }
153
154 if (mdns_hostname_set(CONFIG_WEB_PORTAL_MDNS_HOSTNAME) != ESP_OK) {
155 ESP_LOGW(TAG, "mDNS hostname setup failed");
156 return;
157 }
158
159 if (mdns_instance_name_set(CONFIG_WEB_PORTAL_MDNS_INSTANCE_NAME) != ESP_OK) {
160 ESP_LOGW(TAG, "mDNS instance setup failed");
161 return;
162 }
163
164 if (mdns_service_add(NULL, "_http", "_tcp", http_port, NULL, 0) != ESP_OK) {
165 ESP_LOGW(TAG, "mDNS HTTP service registration failed");
166 return;
167 }
168
169 s_mdns_initialized = true;
170 ESP_LOGI(TAG, "Primary UI URL: http://%s.local", CONFIG_WEB_PORTAL_MDNS_HOSTNAME);
171#else
172 (void)http_port;
173#endif
174}
#define CONFIG_WEB_PORTAL_MDNS_INSTANCE_NAME
Definition web_portal.c:55
static bool s_mdns_initialized
Definition web_portal.c:105
#define CONFIG_WEB_PORTAL_MDNS_HOSTNAME
Definition web_portal.c:51
+ Here is the caller graph for this function:

◆ web_portal_start_sta()

static esp_err_t web_portal_start_sta ( const app_config_t *  config)
static

Definition at line 451 of file web_portal.c.

452{
453 wifi_config_t sta_cfg = {0};
454 EventBits_t bits;
455
456 web_portal_copy_text((char *)sta_cfg.sta.ssid, sizeof(sta_cfg.sta.ssid), config->wifi_ssid);
457 web_portal_copy_text((char *)sta_cfg.sta.password, sizeof(sta_cfg.sta.password), config->wifi_password);
459 sta_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN;
460 sta_cfg.sta.pmf_cfg.capable = true;
461 sta_cfg.sta.pmf_cfg.required = false;
462
463 s_retry_count = 0;
465
466 esp_wifi_stop();
467 ESP_RETURN_ON_ERROR(esp_wifi_set_mode(WIFI_MODE_STA), TAG, "set STA mode failed");
468 ESP_RETURN_ON_ERROR(esp_wifi_set_config(WIFI_IF_STA, &sta_cfg), TAG, "set STA config failed");
469 ESP_LOGI(TAG,
470 "Attempting STA connection to SSID \"%s\" (timeout=%u ms)",
472 (unsigned int)WIFI_CONNECT_TIMEOUT_MS);
473 ESP_RETURN_ON_ERROR(esp_wifi_start(), TAG, "start STA failed");
474
475 bits = xEventGroupWaitBits(s_wifi_event_group,
477 pdFALSE,
478 pdFALSE,
479 pdMS_TO_TICKS(WIFI_CONNECT_TIMEOUT_MS));
480
481 if ((bits & WIFI_CONNECTED_BIT) != 0) {
483 ESP_LOGI(TAG, "STA connected confirmation received for \"%s\"", s_last_sta_ssid);
484 return ESP_OK;
485 }
486
488 app_state_set_wifi_connected(false, NULL);
490 ESP_LOGW(TAG, "STA connection failed, falling back to AP mode");
491 return ESP_FAIL;
492}
#define WIFI_CONNECT_TIMEOUT_MS
Definition web_portal.c:62
static char s_last_sta_ssid[APP_WIFI_SSID_MAX_LEN+1]
Definition web_portal.c:107
static EventGroupHandle_t s_wifi_event_group
Definition web_portal.c:100
#define WIFI_FAILED_BIT
Definition web_portal.c:28
#define WIFI_CONNECTED_BIT
Definition web_portal.c:27
static int s_retry_count
Definition web_portal.c:103
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_status_get()

static esp_err_t web_portal_status_get ( httpd_req_t *  req)
static

Definition at line 521 of file web_portal.c.

522{
523 app_state_snapshot_t snapshot;
524 struct tm local_time;
525 int64_t now_epoch = 0;
526 bool time_valid;
527 char json[512];
528
529 app_state_get_snapshot(&snapshot);
530 time_valid = time_service_get_local_time(&local_time, &now_epoch);
531
532 snprintf(json, sizeof(json),
533 "{\"portal_mode\":%s,\"wifi_connected\":%s,\"ip\":\"%s\","
534 "\"time_valid\":%s,\"epoch\":%lld,\"alarm_active\":%s,"
535 "\"timezone\":%d,\"timezone_location\":\"%s\",\"brightness\":%u}",
536 snapshot.runtime.portal_mode ? "true" : "false",
537 snapshot.runtime.wifi_connected ? "true" : "false",
538 snapshot.runtime.wifi_connected ? snapshot.runtime.sta_ip : "",
539 time_valid ? "true" : "false",
540 (long long)(time_valid ? now_epoch : 0),
541 snapshot.runtime.alarm_active ? "true" : "false",
542 snapshot.config.tz_offset_min,
543 snapshot.config.timezone_location,
544 snapshot.config.brightness);
545
546 httpd_resp_set_type(req, "application/json");
547 httpd_resp_send(req, json, HTTPD_RESP_USE_STRLEN);
548 return ESP_OK;
549}
bool time_service_get_local_time(struct tm *out_tm, int64_t *out_epoch)
Get local time adjusted by the configured timezone offset.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_stop_mdns()

static void web_portal_stop_mdns ( void  )
static

Definition at line 176 of file web_portal.c.

177{
178#if CONFIG_WEB_PORTAL_MDNS_ENABLE
179 if (!s_mdns_initialized) {
180 return;
181 }
182
183 mdns_service_remove("_http", "_tcp");
184 mdns_free();
185 s_mdns_initialized = false;
186#endif
187}
+ Here is the caller graph for this function:

◆ web_portal_time_post()

static esp_err_t web_portal_time_post ( httpd_req_t *  req)
static

Definition at line 679 of file web_portal.c.

680{
681 char body[128];
682 char value[64];
683
684 ESP_RETURN_ON_ERROR(web_portal_read_body(req, body, sizeof(body)), TAG, "read body failed");
685
686 if (web_portal_form_get_value(body, "epoch", value, sizeof(value))) {
687 int64_t epoch = strtoll(value, NULL, 10);
688 if (epoch > 0) {
689 ESP_RETURN_ON_ERROR(time_service_set_epoch(epoch), TAG, "set epoch failed");
690 }
691 }
692
694 return ESP_OK;
695}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_timezone_location_valid()

static bool web_portal_timezone_location_valid ( const char *  location_id)
static

Definition at line 287 of file web_portal.c.

288{
289 size_t index;
290
291 for (index = 0; index < s_timezone_option_count; ++index) {
292 if (strcmp(s_timezone_options[index].id, location_id) == 0) {
293 return true;
294 }
295 }
296
297 return false;
298}
static const size_t s_timezone_option_count
Definition web_portal.c:97
static const web_portal_timezone_option_t s_timezone_options[]
Definition web_portal.c:79
+ Here is the caller graph for this function:

◆ web_portal_url_decode()

static void web_portal_url_decode ( char *  dest,
size_t  dest_len,
const char *  src 
)
static

Definition at line 204 of file web_portal.c.

205{
206 size_t write_index = 0;
207
208 while (*src != '\0' && write_index + 1 < dest_len) {
209 if (*src == '+') {
210 dest[write_index++] = ' ';
211 src++;
212 } else if (*src == '%' && isxdigit((unsigned char)src[1]) && isxdigit((unsigned char)src[2])) {
213 dest[write_index++] = web_portal_hex_to_char(src[1], src[2]);
214 src += 3;
215 } else {
216 dest[write_index++] = *src++;
217 }
218 }
219
220 dest[write_index] = '\0';
221}
static char web_portal_hex_to_char(char high, char low)
Definition web_portal.c:196
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_wifi_event_handler()

static void web_portal_wifi_event_handler ( void *  arg,
esp_event_base_t  event_base,
int32_t  event_id,
void *  event_data 
)
static

Definition at line 304 of file web_portal.c.

308{
309 (void)arg;
310
311 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
312 esp_wifi_connect();
313 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
314 wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data;
315
317 app_state_set_wifi_connected(false, NULL);
319 ESP_LOGW(TAG,
320 "STA disconnected (reason=%u)",
321 (unsigned int)event->reason);
322 if (s_retry_count < 5) {
324 ESP_LOGI(TAG, "Retrying STA (%d/5)", s_retry_count);
325 esp_wifi_connect();
326 } else {
327 xEventGroupSetBits(s_wifi_event_group, WIFI_FAILED_BIT);
328 }
329 } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
330 ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
331 char ip_str[APP_IP_ADDRESS_MAX_LEN];
332
333 snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&event->ip_info.ip));
334 app_state_set_wifi_connected(true, ip_str);
337 ESP_LOGI(TAG,
338 "STA connected: ssid=\"%s\" ip=%s",
339 s_last_sta_ssid[0] != '\0' ? s_last_sta_ssid : "<unknown>",
340 ip_str);
341 xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
342 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
343 wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
344 ESP_LOGI(TAG,
345 "AP client connected: %02X:%02X:%02X:%02X:%02X:%02X, aid=%d",
346 event->mac[0],
347 event->mac[1],
348 event->mac[2],
349 event->mac[3],
350 event->mac[4],
351 event->mac[5],
352 event->aid);
353 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
354 wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
355 ESP_LOGI(TAG,
356 "AP client disconnected: %02X:%02X:%02X:%02X:%02X:%02X, aid=%d",
357 event->mac[0],
358 event->mac[1],
359 event->mac[2],
360 event->mac[3],
361 event->mac[4],
362 event->mac[5],
363 event->aid);
364 }
365}
#define APP_IP_ADDRESS_MAX_LEN
Maximum IPv4 address string length including null terminator.
Definition app_state.h:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_portal_wifi_init_once()

static esp_err_t web_portal_wifi_init_once ( void  )
static

Definition at line 367 of file web_portal.c.

368{
369 if (s_wifi_initialized) {
370 return ESP_OK;
371 }
372
373 ESP_RETURN_ON_ERROR(esp_netif_init(), TAG, "esp_netif_init failed");
374
375 esp_err_t ret = esp_event_loop_create_default();
376 if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) {
377 return ret;
378 }
379
380 s_wifi_event_group = xEventGroupCreate();
381 if (s_wifi_event_group == NULL) {
382 return ESP_ERR_NO_MEM;
383 }
384
385 esp_netif_create_default_wifi_ap();
386 esp_netif_create_default_wifi_sta();
387
388 wifi_init_config_t wifi_init_cfg = WIFI_INIT_CONFIG_DEFAULT();
389 ESP_RETURN_ON_ERROR(esp_wifi_init(&wifi_init_cfg), TAG, "esp_wifi_init failed");
390 ESP_RETURN_ON_ERROR(esp_wifi_set_storage(WIFI_STORAGE_RAM), TAG, "set storage failed");
391 ESP_RETURN_ON_ERROR(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
393 TAG, "register WIFI_EVENT failed");
394 ESP_RETURN_ON_ERROR(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
396 TAG, "register IP_EVENT failed");
397
398 s_wifi_initialized = true;
399 return ESP_OK;
400}
static void web_portal_wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
Definition web_portal.c:304
static bool s_wifi_initialized
Definition web_portal.c:102
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ s_http_port

uint16_t s_http_port = 80
static

Definition at line 106 of file web_portal.c.

◆ s_http_server

httpd_handle_t s_http_server
static

Definition at line 101 of file web_portal.c.

◆ s_last_sta_ssid

char s_last_sta_ssid[APP_WIFI_SSID_MAX_LEN+1]
static

Definition at line 107 of file web_portal.c.

◆ s_mdns_initialized

bool s_mdns_initialized
static

Definition at line 105 of file web_portal.c.

◆ s_reboot_scheduled

bool s_reboot_scheduled
static

Definition at line 104 of file web_portal.c.

◆ s_retry_count

int s_retry_count
static

Definition at line 103 of file web_portal.c.

◆ s_timezone_option_count

const size_t s_timezone_option_count
static
Initial value:
=

Definition at line 97 of file web_portal.c.

◆ s_timezone_options

const web_portal_timezone_option_t s_timezone_options[]
static
Initial value:
= {
{WEB_PORTAL_TIMEZONE_CUSTOM_ID, "Custom offset (no DST)"},
{"Europe/Stockholm", "Europe/Stockholm (CET/CEST)"},
{"Europe/London", "Europe/London (GMT/BST)"},
{"America/New_York", "America/New_York (EST/EDT)"},
{"America/Chicago", "America/Chicago (CST/CDT)"},
{"America/Denver", "America/Denver (MST/MDT)"},
{"America/Los_Angeles", "America/Los_Angeles (PST/PDT)"},
{"America/Sao_Paulo", "America/Sao_Paulo (UTC-3)"},
{"Asia/Dubai", "Asia/Dubai (UTC+4)"},
{"Asia/Kolkata", "Asia/Kolkata (UTC+5:30)"},
{"Asia/Bangkok", "Asia/Bangkok (UTC+7)"},
{"Asia/Singapore", "Asia/Singapore (UTC+8)"},
{"Asia/Tokyo", "Asia/Tokyo (JST)"},
{"Australia/Sydney", "Australia/Sydney (AEST/AEDT)"},
{"Pacific/Auckland", "Pacific/Auckland (NZST/NZDT)"},
}

Definition at line 79 of file web_portal.c.

79 {
80 {WEB_PORTAL_TIMEZONE_CUSTOM_ID, "Custom offset (no DST)"},
81 {"Europe/Stockholm", "Europe/Stockholm (CET/CEST)"},
82 {"Europe/London", "Europe/London (GMT/BST)"},
83 {"America/New_York", "America/New_York (EST/EDT)"},
84 {"America/Chicago", "America/Chicago (CST/CDT)"},
85 {"America/Denver", "America/Denver (MST/MDT)"},
86 {"America/Los_Angeles", "America/Los_Angeles (PST/PDT)"},
87 {"America/Sao_Paulo", "America/Sao_Paulo (UTC-3)"},
88 {"Asia/Dubai", "Asia/Dubai (UTC+4)"},
89 {"Asia/Kolkata", "Asia/Kolkata (UTC+5:30)"},
90 {"Asia/Bangkok", "Asia/Bangkok (UTC+7)"},
91 {"Asia/Singapore", "Asia/Singapore (UTC+8)"},
92 {"Asia/Tokyo", "Asia/Tokyo (JST)"},
93 {"Australia/Sydney", "Australia/Sydney (AEST/AEDT)"},
94 {"Pacific/Auckland", "Pacific/Auckland (NZST/NZDT)"},
95};

◆ s_wifi_event_group

EventGroupHandle_t s_wifi_event_group
static

Definition at line 100 of file web_portal.c.

◆ s_wifi_initialized

bool s_wifi_initialized
static

Definition at line 102 of file web_portal.c.

◆ TAG

const char* TAG = "web_portal"
static

Definition at line 72 of file web_portal.c.