/* * wifi.c - Wi-Fi station + captive-portal provisioning * * Public API (declare in board.h or the camera file): * void wifi_start_provisioning_if_needed(void); * bool wifi_wait_connected(uint32_t timeout_ms); * bool wifi_is_provisioning(void); * void wifi_erase_credentials(void); * void wifi_request_portal(void); reboot into the portal; login REQUIRED there * void wifi_request_portal_physical(void); reboot into the portal opened by the BOOT button; * no current login needed there * bool wifi_portal_is_physical(void); * * Flow: * - Boot: read SSID/password from NVS. * - No credentials (or too many failed reconnects, or a portal request) -> open AP * "ESP32_Setup" with DNS hijack + web page at http://192.168.4.1/ * - Credentials found -> station mode, WIFI_CONNECTED_BIT is set on got-IP. * * Portal security: * - Normal portal: BOTH cards (Wi-Fi and login) need the current media login. * - Portal opened by holding BOOT: physical access is the proof, so no login is asked. * The flag lives in RTC memory: one-shot, survives esp_restart(), lost on power cycle. * - 5 wrong logins lock the portal until restart (not applied in the physical case). * * IMPORTANT: cam_auth_load() must run BEFORE wifi_start_provisioning_if_needed(), * otherwise cam_auth_verify() sees "auth disabled" and accepts everything. * * The media servers (camera on :80, audio on :81/:82) must be started by the * caller only AFTER wifi_wait_connected() returns true, so they never collide * with the portal's HTTP server on port 80. */ #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "freertos/timers.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "nvs.h" #include "esp_netif.h" #include "esp_http_server.h" #include "lwip/sockets.h" #include "board.h" #include "cam_auth.h" /* ---- Fallbacks, used only if board.h does not define them ---- */ #ifndef WIFI_NS #define WIFI_NS "wifi_cfg" #endif #ifndef WIFI_SSID_KEY #define WIFI_SSID_KEY "ssid" #endif #ifndef WIFI_PASS_KEY #define WIFI_PASS_KEY "pass" #endif #ifndef WIFI_CONNECTED_BIT #define WIFI_CONNECTED_BIT BIT0 #endif #define PROV_TIMEOUT_SEC 300 /* portal closes and board restarts after this */ #define PROV_TIMEOUT_PHYS_SEC 180 /* shorter window when no login is needed (BOOT button) */ #define MAX_RECONNECT_TRIES 30 /* then the board reboots into the portal */ #define MAX_AUTH_FAILS 5 #define AP_SSID "ESP32_Setup" #define AP_CHANNEL 6 #define AP_IP_STR "192.168.4.1" #define HOSTNAME "hodor-cam" #define PORTAL_NS "portal" static const char *TAG = "WIFI"; bool g_wifi_is_shutting_down = false; /* kept for compatibility with other code */ static EventGroupHandle_t s_wifi_event_group = NULL; static httpd_handle_t s_portal_server = NULL; static TimerHandle_t s_prov_timer = NULL; static esp_netif_t *s_sta_netif = NULL; static esp_netif_t *s_ap_netif = NULL; static bool s_provisioning = false; static bool s_physical = false; /* portal opened by the BOOT button */ static int s_retry = 0; static int s_auth_fail = 0; /* wrong logins since the portal started */ /* Survive esp_restart() (software reset), lost on power cycle */ RTC_DATA_ATTR static bool rtc_hint_valid = false; /* fast-connect channel hint */ RTC_DATA_ATTR static uint8_t rtc_hint_channel = 0; /* ------------------------------------------------------------------ */ /* Portal web page */ /* ------------------------------------------------------------------ */ /* The page contains exactly one "" "" ""; /* ------------------------------------------------------------------ */ /* Helpers */ /* ------------------------------------------------------------------ */ static int hex_val(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'A' && c <= 'F') return c - 'A' + 10; if (c >= 'a' && c <= 'f') return c - 'a' + 10; return -1; } static void url_decode(char *dest, const char *src) { while (*src) { if (src[0] == '%' && hex_val(src[1]) >= 0 && hex_val(src[2]) >= 0) { *dest++ = (char)(16 * hex_val(src[1]) + hex_val(src[2])); src += 3; } else if (*src == '+') { *dest++ = ' '; src++; } else { *dest++ = *src++; } } *dest = '\0'; } void wifi_erase_credentials(void) { nvs_handle_t h; if (nvs_open(WIFI_NS, NVS_READWRITE, &h) == ESP_OK) { nvs_erase_key(h, WIFI_SSID_KEY); nvs_erase_key(h, WIFI_PASS_KEY); nvs_commit(h); nvs_close(h); } } static bool read_body(httpd_req_t *req, char *body, size_t size) { size_t total = req->content_len; if (total == 0 || total >= size) return false; size_t got = 0; while (got < total) { int r = httpd_req_recv(req, body + got, total - got); if (r == HTTPD_SOCK_ERR_TIMEOUT) continue; if (r <= 0) return false; got += r; } body[got] = '\0'; return true; } static esp_err_t send_text(httpd_req_t *req, const char *status, const char *msg) { httpd_resp_set_status(req, status); httpd_resp_set_type(req, "text/plain; charset=utf-8"); return httpd_resp_send(req, msg, HTTPD_RESP_USE_STRLEN); } #define MSG_LOCKED "تعداد تلاش‌های ناموفق زیاد است. دستگاه را ری‌استارت کنید." #define MSG_BAD_REQ "درخواست نامعتبر است." #define MSG_BAD_LOGIN "نام کاربری یا رمز عبور فعلی اشتباه است." #define MSG_STORE_ERR "خطا در ذخیره‌سازی." /* ------------------------------------------------------------------ */ /* Portal HTTP server */ /* ------------------------------------------------------------------ */ static esp_err_t root_get_handler(httpd_req_t *req) { httpd_resp_set_type(req, "text/html; charset=utf-8"); httpd_resp_set_hdr(req, "Cache-Control", "no-cache"); /* inject the physical-presence flag in front of the page script */ const char *sp = strstr(html_settings, "