// File: src/http_portal.c #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_log.h" #include "esp_wifi.h" #include "esp_http_server.h" #include "http_portal.h" #include "storage.h" // Provided by WiFi Manager (defined in wifi_mgr.c) bool wifi_mgr_load_sta_creds(char **out_ssid, char **out_pass); void wifi_mgr_save_sta_creds_and_reconnect(const char* ssid, const char* pass); // NEW: Save device properties (implemented in wifi_mgr.c) void wifi_mgr_save_device_info(const char* devicename, const char* token, const char* bucket, const char* org); static char* html_escape(const char* s) { if (!s) return strdup(""); size_t len = 0; for (const char* p = s; *p; ++p) { switch(*p){ case '&': len+=5; break; case '<': case '>': len+=4; break; case '"': case '\'': len+=6; break; default: len+=1; } } char* out = (char*)malloc(len+1); if(!out) return NULL; char* o = out; for (const char* p = s; *p; ++p) { switch(*p){ case '&': memcpy(o,"&",5); o+=5; break; case '<': memcpy(o,"<",4); o+=4; break; case '>': memcpy(o,">",4); o+=4; break; case '"': memcpy(o,""",6);o+=6; break; case '\'':memcpy(o,"'",5); o+=5; break; default: *o++ = *p; } } *o = '\0'; return out; } static const char *TAG = "portal_http"; static httpd_handle_t s_http = NULL; static EventGroupHandle_t s_wifi_events = NULL; static EventBits_t s_BIT_GOTIP = 0; static EventBits_t s_BIT_FAIL = 0; static char s_ap_ssid[33] = {0}; // ---------- HTML (template with placeholders) ---------- static const char form_html_tpl[] = "" "" "Wi-Fi Setup" "" "
" "

Wi-Fi Configuration

" "

Connected to %s. Choose your Wi-Fi network or enter it manually:

" "
" "
" "" "" "" "" "" "" "" "
" "

After saving, the device will connect to your Wi-Fi network. " "The access point will automatically shut down shortly afterward.

" "
" "" ""; static const char saved_html[] = "" "" "Saved" "

✔️ Saved

" "

The credentials have been saved. The device is now connecting to your Wi-Fi…

" "" ""; static const char status_tpl[] = "" "" "Status" "

Connection Status

" "

%s

" "

Back

" ""; // ---------- simple URL decoder ---------- static int hexval(char c){ if(c>='0'&&c<='9')return c-'0'; if(c>='a'&&c<='f')return 10+c-'a'; if(c>='A'&&c<='F')return 10+c-'A'; return -1; } static void urldecode(char* s){ char* o = s; for(char* i=s; *i; ++i){ if(*i=='+'){ *o++=' '; } else if(*i=='%' && hexval(i[1])>=0 && hexval(i[2])>=0){ *o++ = (char)((hexval(i[1])<<4)|hexval(i[2])); i+=2; } else { *o++=*i; } } *o='\0'; } // ---------- HTTP Handlers ---------- static esp_err_t root_get(httpd_req_t *req) { char *org = NULL, *bucket = NULL, *token = NULL; if (!storage_get_effective_influx(&org, &bucket, &token)) { org = strdup(""); bucket = strdup(""); token = strdup(""); if (!org || !bucket || !token) { if (org) free(org); if (bucket) free(bucket); if (token) free(token); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom"); } } char *dev_raw = NULL; if (!storage_get_effective_device_name(&dev_raw)) { dev_raw = strdup(""); if (!dev_raw) { free(org); free(bucket); free(token); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom"); } } char *dev_esc = html_escape(dev_raw); char *org_esc = html_escape(org ? org : ""); char *bucket_esc = html_escape(bucket ? bucket : ""); if (!dev_esc || !org_esc || !bucket_esc) { if (dev_esc) free(dev_esc); if (org_esc) free(org_esc); if (bucket_esc) free(bucket_esc); free(org); free(bucket); free(token); free(dev_raw); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom"); } int need = snprintf(NULL, 0, form_html_tpl, s_ap_ssid, dev_esc, org_esc, bucket_esc); if (need < 0) { free(org); free(bucket); free(token); free(dev_raw); free(dev_esc); free(org_esc); free(bucket_esc); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "format error"); } char *page = (char*)malloc((size_t)need + 1); if (!page) { free(org); free(bucket); free(token); free(dev_raw); free(dev_esc); free(org_esc); free(bucket_esc); return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom"); } snprintf(page, (size_t)need + 1, form_html_tpl, s_ap_ssid, dev_esc, org_esc, bucket_esc); httpd_resp_set_type(req, "text/html; charset=utf-8"); esp_err_t r = httpd_resp_send(req, page, HTTPD_RESP_USE_STRLEN); free(page); free(org); free(bucket); free(token); free(dev_raw); free(dev_esc); free(org_esc); free(bucket_esc); return r; } static esp_err_t captive_ok(httpd_req_t *req) { EventBits_t bits = xEventGroupGetBits(s_wifi_events); if (bits & s_BIT_GOTIP) { httpd_resp_set_status(req, "204 No Content"); httpd_resp_set_type(req, "text/plain"); return httpd_resp_send(req, NULL, 0); } else { httpd_resp_set_type(req, "text/html; charset=utf-8"); const char msg[] = "OK"; return httpd_resp_send(req, msg, HTTPD_RESP_USE_STRLEN); } } static esp_err_t status_get(httpd_req_t *req) { EventBits_t bits = xEventGroupGetBits(s_wifi_events); const char *msg = (bits & s_BIT_GOTIP) ? "✅ Connected to external Wi-Fi." : (bits & s_BIT_FAIL) ? "❌ Connection failed. Please check your credentials." : "⏳ Connecting..."; int need = snprintf(NULL, 0, status_tpl, msg); if (need < 0) return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "format error"); char *page = (char*)malloc((size_t)need + 1); if (!page) return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "oom"); snprintf(page, (size_t)need + 1, status_tpl, msg); httpd_resp_set_type(req, "text/html; charset=utf-8"); esp_err_t r = httpd_resp_send(req, page, HTTPD_RESP_USE_STRLEN); free(page); return r; } static esp_err_t favicon_get(httpd_req_t *req) { httpd_resp_set_status(req, "204 No Content"); httpd_resp_set_type(req, "image/x-icon"); return httpd_resp_send(req, NULL, 0); } // The rest of the logic (save_post, scan_get, start_http, portal_http_start, portal_http_stop) // remains IDENTICAL to your provided code above — only text/UI changes were made.