This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include "storage_manager.h"
|
||||
#include "scryfall_client.h"
|
||||
#include "wifi_manager.h"
|
||||
#include "dns_server.h"
|
||||
#include "web_server.h"
|
||||
|
||||
static const char *TAG = "web";
|
||||
@@ -30,13 +31,25 @@ static const char SPA_PAGE[] =
|
||||
"body{font-family:system-ui,-apple-system,sans-serif;background:#141424;color:#eee;max-width:600px;margin:1.5em auto;padding:1em}"
|
||||
"h1{color:#ffd75a;margin-bottom:0.2em}h2{color:#ffd75a;font-size:1.15em;margin-top:1.2em}"
|
||||
".uid{font-size:1.5em;font-family:monospace;letter-spacing:2px;color:#7fd77f;word-break:break-all;background:#1e1e32;padding:.4em .6em;border-radius:6px;border:1px solid #333}"
|
||||
"input{font-size:1em;padding:.5em .8em;border-radius:6px;border:1px solid #555;background:#222;color:#eee;width:calc(100% - 1.6em);box-sizing:border-box}"
|
||||
"input,select{font-size:1em;padding:.5em .8em;border-radius:6px;border:1px solid #555;background:#222;color:#eee;width:100%;box-sizing:border-box}"
|
||||
"button,.btn{font-size:.95em;padding:.5em 1em;border-radius:6px;border:1px solid #555;background:#2a2a44;color:#eee;cursor:pointer;margin:.3em 0}"
|
||||
"button:hover,.btn:hover{background:#3a3a5c}"
|
||||
".btn-primary{background:#ffd75a;color:#111;border:none;font-weight:bold}"
|
||||
".btn-primary:hover{background:#ffe380}"
|
||||
".btn-del{background:#4a1e1e;color:#ff9999;border:1px solid #772222;font-size:.85em;padding:.4em .8em;cursor:pointer;border-radius:6px}"
|
||||
".btn-del:hover{background:#772222;color:#fff}"
|
||||
".tabs{display:flex;gap:8px;margin-bottom:1.5em}"
|
||||
".tab-btn{flex:1;font-size:1em;padding:.6em;border-radius:8px;background:#1e1e32;color:#aaa;border:1px solid #33334c;cursor:pointer;font-weight:600;text-align:center}"
|
||||
".tab-btn.active{background:#ffd75a;color:#111;border-color:#ffd75a}"
|
||||
".tab-pane{display:none}"
|
||||
".tab-pane.active{display:block}"
|
||||
".status-badge{display:inline-block;padding:.3em .7em;border-radius:12px;font-size:.85em;font-weight:bold;margin-bottom:.8em}"
|
||||
".badge-ok{background:#1b4332;color:#7fd77f;border:1px solid #2d6a4f}"
|
||||
".badge-bad{background:#4a1e1e;color:#ff9999;border:1px solid #772222}"
|
||||
".input-grp{margin-bottom:1em}"
|
||||
".input-grp label{display:block;color:#ccc;font-size:.85em;margin-bottom:.3em}"
|
||||
".pwd-row{display:flex;gap:6px}"
|
||||
".pwd-row input{flex:1}"
|
||||
".sug{list-style:none;padding:0;margin:.4em 0;background:#1f1f33;border-radius:6px;border:1px solid #333}"
|
||||
".sug li{cursor:pointer;padding:.5em .8em;border-bottom:1px solid #333}"
|
||||
".sug li:hover{background:#2a2a48;color:#ffd75a}"
|
||||
@@ -50,7 +63,13 @@ static const char SPA_PAGE[] =
|
||||
".header-row{display:flex;align-items:center;justify-content:space-between}"
|
||||
"</style></head><body>"
|
||||
"<h1>MTG RFID Companion</h1>"
|
||||
"<p style=\"color:#aaa;margin-top:0\">Pair card sleeves and manage your scanned deck.</p>"
|
||||
"<p style=\"color:#aaa;margin-top:0\">Manage card tags, decks, and device Wi-Fi connectivity.</p>"
|
||||
"<div class=\"tabs\">"
|
||||
"<button id=\"tb-cards\" class=\"tab-btn active\" onclick=\"tab('cards')\">🃏 Card Pairing</button>"
|
||||
"<button id=\"tb-wifi\" class=\"tab-btn\" onclick=\"tab('wifi')\">📶 Wi-Fi Settings</button>"
|
||||
"</div>"
|
||||
"<!-- Tab: Cards -->"
|
||||
"<div id=\"pane-cards\" class=\"tab-pane active\">"
|
||||
"<div style=\"background:#1a1a2e;padding:1em;border-radius:8px;border:1px solid #2d2d48;margin-bottom:1.5em\">"
|
||||
"<h2 style=\"margin-top:0\">Last Scanned Tag</h2>"
|
||||
"<p class=\"uid\" id=\"uid\">waiting...</p>"
|
||||
@@ -65,7 +84,55 @@ static const char SPA_PAGE[] =
|
||||
"<button class=\"btn\" style=\"font-size:.8em\" onclick=\"loadCards()\">Refresh</button>"
|
||||
"</div>"
|
||||
"<div id=\"cards-box\"><p class=\"card-none\">Loading paired cards...</p></div>"
|
||||
"</div>"
|
||||
"<!-- Tab: Wi-Fi -->"
|
||||
"<div id=\"pane-wifi\" class=\"tab-pane\">"
|
||||
"<div style=\"background:#1a1a2e;padding:1em;border-radius:8px;border:1px solid #2d2d48;margin-bottom:1.5em\">"
|
||||
"<h2 style=\"margin-top:0\">Active Connection</h2>"
|
||||
"<div id=\"wf-badge\" class=\"status-badge badge-bad\">Checking Wi-Fi...</div>"
|
||||
"<div id=\"wf-info\" style=\"font-size:.9em;color:#aaa;line-height:1.6\"></div>"
|
||||
"</div>"
|
||||
"<div style=\"background:#1a1a2e;padding:1em;border-radius:8px;border:1px solid #2d2d48;margin-bottom:1.5em\">"
|
||||
"<div class=\"header-row\">"
|
||||
"<h2 style=\"margin-top:0\">Connect to Wi-Fi</h2>"
|
||||
"<button class=\"btn\" style=\"font-size:.8em\" id=\"wf-scan-btn\" onclick=\"scanWifi()\">🔍 Scan Networks</button>"
|
||||
"</div>"
|
||||
"<div class=\"input-grp\">"
|
||||
"<label>Discovered Networks</label>"
|
||||
"<select id=\"wf-select\" onchange=\"pickNet()\">"
|
||||
"<option value=\"\">-- Click Scan Networks above --</option>"
|
||||
"</select>"
|
||||
"</div>"
|
||||
"<div class=\"input-grp\">"
|
||||
"<label>Network Name (SSID)</label>"
|
||||
"<input id=\"wf-ssid\" placeholder=\"e.g. MyHomeWifi or Mobile Hotspot\">"
|
||||
"</div>"
|
||||
"<div class=\"input-grp\">"
|
||||
"<label>Password</label>"
|
||||
"<div class=\"pwd-row\">"
|
||||
"<input type=\"password\" id=\"wf-pass\" placeholder=\"Network password (empty if open)\">"
|
||||
"<button type=\"button\" class=\"btn\" onclick=\"togglePass()\" style=\"margin:0;width:auto\">👁</button>"
|
||||
"</div>"
|
||||
"</div>"
|
||||
"<div style=\"margin-top:.6em\">"
|
||||
"<button class=\"btn-primary\" style=\"width:100%\" onclick=\"connectWifi()\">Connect & Save</button>"
|
||||
"</div>"
|
||||
"<p id=\"wf-status\" style=\"margin-top:.6em\"></p>"
|
||||
"</div>"
|
||||
"<div class=\"header-row\">"
|
||||
"<h2>Saved Networks</h2>"
|
||||
"<button class=\"btn\" style=\"font-size:.8em\" onclick=\"loadSavedWifi()\">Refresh</button>"
|
||||
"</div>"
|
||||
"<div id=\"saved-box\"><p class=\"card-none\">Loading saved networks...</p></div>"
|
||||
"</div>"
|
||||
"<script>"
|
||||
"function tab(t){"
|
||||
"document.getElementById('tb-cards').className='tab-btn'+(t==='cards'?' active':'');"
|
||||
"document.getElementById('tb-wifi').className='tab-btn'+(t==='wifi'?' active':'');"
|
||||
"document.getElementById('pane-cards').className='tab-pane'+(t==='cards'?' active':'');"
|
||||
"document.getElementById('pane-wifi').className='tab-pane'+(t==='wifi'?' active':'');"
|
||||
"if(t==='wifi'){refreshWifiStatus();loadSavedWifi();}"
|
||||
"}"
|
||||
"var lastUid='';var suggestions=[];"
|
||||
"function esc(s){return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');}"
|
||||
"function show(cls,msg){document.getElementById('status').className=cls;document.getElementById('status').textContent=msg;}"
|
||||
@@ -115,7 +182,93 @@ static const char SPA_PAGE[] =
|
||||
" box.innerHTML=html;"
|
||||
" }).catch(function(e){document.getElementById('cards-box').innerHTML='<p class=\"bad\">Failed to load cards: '+e+'</p>';});"
|
||||
"}"
|
||||
"poll();loadCards();"
|
||||
"function showWf(cls,msg){var el=document.getElementById('wf-status');el.className=cls;el.textContent=msg;}"
|
||||
"function togglePass(){var f=document.getElementById('wf-pass');f.type=f.type==='password'?'text':'password';}"
|
||||
"function refreshWifiStatus(){"
|
||||
" fetch('/api/wifi/status').then(function(r){return r.json();}).then(function(j){"
|
||||
" var b=document.getElementById('wf-badge');var d=document.getElementById('wf-info');"
|
||||
" if(j.connected){"
|
||||
" b.className='status-badge badge-ok';b.textContent='Connected to '+j.ssid;"
|
||||
" d.innerHTML='IP Address: <strong>'+j.ip+'</strong><br>Signal: '+j.rssi+' dBm<br>Hotspot Portal: 192.168.4.1';"
|
||||
" }else{"
|
||||
" b.className='status-badge badge-bad';b.textContent='Disconnected';"
|
||||
" d.innerHTML='Device Wi-Fi is not connected to any network.<br>Hotspot Portal: <strong>192.168.4.1</strong>';"
|
||||
" }"
|
||||
" }).catch(function(){});"
|
||||
"}"
|
||||
"function scanWifi(){"
|
||||
" var btn=document.getElementById('wf-scan-btn');"
|
||||
" btn.disabled=true;btn.textContent='Scanning...';"
|
||||
" var sel=document.getElementById('wf-select');"
|
||||
" sel.innerHTML='<option>Scanning in progress...</option>';"
|
||||
" fetch('/api/wifi/scan').then(function(r){return r.json();}).then(function(list){"
|
||||
" btn.disabled=false;btn.textContent='🔍 Scan Networks';"
|
||||
" if(!list||list.length===0){"
|
||||
" sel.innerHTML='<option value=\"\">No networks found</option>';return;"
|
||||
" }"
|
||||
" var h='<option value=\"\">-- Select a discovered network --</option>';"
|
||||
" list.forEach(function(ap){"
|
||||
" var lock=ap.auth==='Open'?'':' 🔒';"
|
||||
" h+='<option value=\"'+esc(ap.ssid)+'\">'+esc(ap.ssid)+' ('+ap.rssi+' dBm, '+ap.auth+lock+')</option>';"
|
||||
" });"
|
||||
" sel.innerHTML=h;"
|
||||
" }).catch(function(e){"
|
||||
" btn.disabled=false;btn.textContent='🔍 Scan Networks';"
|
||||
" sel.innerHTML='<option value=\"\">Scan failed: '+e+'</option>';"
|
||||
" });"
|
||||
"}"
|
||||
"function pickNet(){"
|
||||
" var val=document.getElementById('wf-select').value;"
|
||||
" if(val){"
|
||||
" document.getElementById('wf-ssid').value=val;"
|
||||
" document.getElementById('wf-pass').focus();"
|
||||
" }"
|
||||
"}"
|
||||
"function connectWifi(){"
|
||||
" var s=document.getElementById('wf-ssid').value.trim();"
|
||||
" var p=document.getElementById('wf-pass').value;"
|
||||
" if(!s){showWf('bad','Please enter or select a network SSID');return;}"
|
||||
" showWf('','Connecting to '+s+'... Please wait 5-10s.');"
|
||||
" fetch('/api/wifi/connect',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},"
|
||||
" body:'ssid='+encodeURIComponent(s)+'&pass='+encodeURIComponent(p)})"
|
||||
" .then(function(r){return r.json();}).then(function(j){"
|
||||
" if(j.ok){"
|
||||
" showWf('ok','Saved & initiated connection to '+s+'. Checking status...');"
|
||||
" loadSavedWifi();"
|
||||
" setTimeout(function(){refreshWifiStatus();showWf('ok','Connected profile saved!');},6000);"
|
||||
" }else{"
|
||||
" showWf('bad','Failed to connect: '+(j.error||'unknown'));"
|
||||
" }"
|
||||
" }).catch(function(e){showWf('bad','Network error: '+e);});"
|
||||
"}"
|
||||
"function loadSavedWifi(){"
|
||||
" fetch('/api/wifi/saved').then(function(r){return r.json();}).then(function(list){"
|
||||
" var box=document.getElementById('saved-box');"
|
||||
" if(!list||list.length===0){"
|
||||
" box.innerHTML='<p class=\"card-none\">No saved networks. Connect to a network above.</p>';return;"
|
||||
" }"
|
||||
" var h='';"
|
||||
" list.forEach(function(item){"
|
||||
" h+='<div class=\"card-item\">';"
|
||||
" h+='<div class=\"card-info\"><div class=\"card-title\">'+esc(item.ssid)+'</div><div class=\"card-uid\">Saved Profile</div></div>';"
|
||||
" h+='<button class=\"btn-del\" onclick=\"forgetWifi(\\''+esc(item.ssid)+'\\')\">Forget</button>';"
|
||||
" h+='</div>';"
|
||||
" });"
|
||||
" box.innerHTML=h;"
|
||||
" }).catch(function(e){"
|
||||
" document.getElementById('saved-box').innerHTML='<p class=\"bad\">Error loading saved networks: '+e+'</p>';"
|
||||
" });"
|
||||
"}"
|
||||
"function forgetWifi(s){"
|
||||
" if(!confirm('Forget network \"'+s+'\"?'))return;"
|
||||
" fetch('/api/wifi/forget',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},"
|
||||
" body:'ssid='+encodeURIComponent(s)})"
|
||||
" .then(function(r){return r.json();}).then(function(j){"
|
||||
" if(j.ok){loadSavedWifi();refreshWifiStatus();}"
|
||||
" else{alert('Failed to forget '+s);}"
|
||||
" }).catch(function(e){alert('Error: '+e);});"
|
||||
"}"
|
||||
"poll();loadCards();refreshWifiStatus();"
|
||||
"</script></body></html>";
|
||||
|
||||
static httpd_handle_t s_server = NULL;
|
||||
@@ -126,6 +279,12 @@ static esp_err_t handle_bind(httpd_req_t *req);
|
||||
static esp_err_t handle_cards(httpd_req_t *req);
|
||||
static esp_err_t handle_art(httpd_req_t *req);
|
||||
static esp_err_t handle_unbind(httpd_req_t *req);
|
||||
static esp_err_t handle_captive_portal(httpd_req_t *req);
|
||||
static esp_err_t handle_wifi_status(httpd_req_t *req);
|
||||
static esp_err_t handle_wifi_scan(httpd_req_t *req);
|
||||
static esp_err_t handle_wifi_saved(httpd_req_t *req);
|
||||
static esp_err_t handle_wifi_connect(httpd_req_t *req);
|
||||
static esp_err_t handle_wifi_forget(httpd_req_t *req);
|
||||
|
||||
static const httpd_uri_t URI_ROOT = {
|
||||
.uri = "/", .method = HTTP_GET, .handler = handle_root, .user_ctx = NULL
|
||||
@@ -146,6 +305,46 @@ static const httpd_uri_t URI_UNBIND = {
|
||||
.uri = "/api/unbind", .method = HTTP_POST, .handler = handle_unbind, .user_ctx = NULL
|
||||
};
|
||||
|
||||
/* Wi-Fi Management URIs */
|
||||
static const httpd_uri_t URI_WIFI_STATUS = {
|
||||
.uri = "/api/wifi/status", .method = HTTP_GET, .handler = handle_wifi_status, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_WIFI_SCAN = {
|
||||
.uri = "/api/wifi/scan", .method = HTTP_GET, .handler = handle_wifi_scan, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_WIFI_SAVED = {
|
||||
.uri = "/api/wifi/saved", .method = HTTP_GET, .handler = handle_wifi_saved, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_WIFI_CONNECT = {
|
||||
.uri = "/api/wifi/connect", .method = HTTP_POST, .handler = handle_wifi_connect, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_WIFI_FORGET = {
|
||||
.uri = "/api/wifi/forget", .method = HTTP_POST, .handler = handle_wifi_forget, .user_ctx = NULL
|
||||
};
|
||||
|
||||
/* Captive Portal Probe URIs */
|
||||
static const httpd_uri_t URI_CP_APPLE = {
|
||||
.uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_CP_204_1 = {
|
||||
.uri = "/generate_204", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_CP_204_2 = {
|
||||
.uri = "/gen_204", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_CP_WIN_1 = {
|
||||
.uri = "/connecttest.txt", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_CP_WIN_2 = {
|
||||
.uri = "/ncsi.txt", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_CP_FF_1 = {
|
||||
.uri = "/canonical.html", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
static const httpd_uri_t URI_CP_FF_2 = {
|
||||
.uri = "/success.txt", .method = HTTP_GET, .handler = handle_captive_portal, .user_ctx = NULL
|
||||
};
|
||||
|
||||
static void send_json(httpd_req_t *req, int status, const char *body)
|
||||
{
|
||||
httpd_resp_set_status(req, status == 200 ? "200 OK" : "400 Bad Request");
|
||||
@@ -380,6 +579,193 @@ static esp_err_t handle_unbind(httpd_req_t *req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Captive Portal & Wi-Fi Management Handlers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static esp_err_t handle_captive_portal(httpd_req_t *req)
|
||||
{
|
||||
httpd_resp_set_status(req, "302 Found");
|
||||
httpd_resp_set_hdr(req, "Location", "http://192.168.4.1/");
|
||||
httpd_resp_send(req, NULL, 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t handle_wifi_status(httpd_req_t *req)
|
||||
{
|
||||
bool conn = wifi_manager_connected();
|
||||
const char *ssid = wifi_manager_get_connected_ssid();
|
||||
const char *ip = wifi_manager_get_ip();
|
||||
int rssi = wifi_manager_get_rssi();
|
||||
|
||||
char buf[160];
|
||||
snprintf(buf, sizeof(buf),
|
||||
"{\"connected\":%s,\"ssid\":\"%s\",\"ip\":\"%s\",\"rssi\":%d}\n",
|
||||
conn ? "true" : "false",
|
||||
ssid ? ssid : "",
|
||||
ip ? ip : "0.0.0.0",
|
||||
rssi);
|
||||
send_json(req, 200, buf);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t handle_wifi_scan(httpd_req_t *req)
|
||||
{
|
||||
wifi_ap_record_t *records = NULL;
|
||||
uint16_t count = 0;
|
||||
esp_err_t err = wifi_manager_scan_networks(&records, &count);
|
||||
cJSON *root = cJSON_CreateArray();
|
||||
if (err == ESP_OK && records != NULL && root != NULL) {
|
||||
for (uint16_t i = 0; i < count; i++) {
|
||||
cJSON *item = cJSON_CreateObject();
|
||||
if (item) {
|
||||
cJSON_AddStringToObject(item, "ssid", (const char *)records[i].ssid);
|
||||
cJSON_AddNumberToObject(item, "rssi", records[i].rssi);
|
||||
const char *auth_str = "Open";
|
||||
if (records[i].authmode == WIFI_AUTH_WEP) auth_str = "WEP";
|
||||
else if (records[i].authmode == WIFI_AUTH_WPA_PSK) auth_str = "WPA";
|
||||
else if (records[i].authmode == WIFI_AUTH_WPA2_PSK) auth_str = "WPA2";
|
||||
else if (records[i].authmode == WIFI_AUTH_WPA_WPA2_PSK) auth_str = "WPA/WPA2";
|
||||
else if (records[i].authmode >= WIFI_AUTH_WPA3_PSK) auth_str = "WPA3";
|
||||
cJSON_AddStringToObject(item, "auth", auth_str);
|
||||
cJSON_AddItemToArray(root, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (records) {
|
||||
free(records);
|
||||
}
|
||||
char *out = root ? cJSON_PrintUnformatted(root) : NULL;
|
||||
if (root) cJSON_Delete(root);
|
||||
if (out) {
|
||||
send_json(req, 200, out);
|
||||
free(out);
|
||||
} else {
|
||||
send_json(req, 200, "[]\n");
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t handle_wifi_saved(httpd_req_t *req)
|
||||
{
|
||||
wifi_profile_t profiles[WIFI_MAX_PROFILES];
|
||||
int count = wifi_manager_get_saved_profiles(profiles, WIFI_MAX_PROFILES);
|
||||
cJSON *root = cJSON_CreateArray();
|
||||
if (root) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
cJSON *item = cJSON_CreateObject();
|
||||
if (item) {
|
||||
cJSON_AddStringToObject(item, "ssid", profiles[i].ssid);
|
||||
cJSON_AddItemToArray(root, item);
|
||||
}
|
||||
}
|
||||
char *out = cJSON_PrintUnformatted(root);
|
||||
cJSON_Delete(root);
|
||||
if (out) {
|
||||
send_json(req, 200, out);
|
||||
free(out);
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
send_json(req, 200, "[]\n");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t handle_wifi_connect(httpd_req_t *req)
|
||||
{
|
||||
if (req->content_len <= 0 || req->content_len >= HTTPD_BODY_MAX) {
|
||||
send_json(req, 400, "{\"ok\":false,\"error\":\"bad body\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
char *form = malloc((size_t)req->content_len + 1);
|
||||
if (form == NULL) {
|
||||
send_json(req, 500, "{\"ok\":false,\"error\":\"no mem\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
int got = httpd_req_recv(req, form, (size_t)req->content_len);
|
||||
if (got != req->content_len) {
|
||||
free(form);
|
||||
send_json(req, 400, "{\"ok\":false,\"error\":\"short body\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
form[got] = '\0';
|
||||
|
||||
char *ssid = get_form_param(form, "ssid");
|
||||
char *pass = get_form_param(form, "pass");
|
||||
free(form);
|
||||
|
||||
if (ssid) {
|
||||
size_t slen = strlen(ssid);
|
||||
while (slen > 0 && (ssid[slen - 1] == ' ' || ssid[slen - 1] == '\r' || ssid[slen - 1] == '\n')) {
|
||||
ssid[--slen] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if (ssid == NULL || strlen(ssid) == 0) {
|
||||
free(ssid);
|
||||
free(pass);
|
||||
send_json(req, 400, "{\"ok\":false,\"error\":\"missing ssid\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
const char *p = pass ? pass : "";
|
||||
wifi_manager_connect_new(ssid, p, true);
|
||||
|
||||
char ok[128];
|
||||
snprintf(ok, sizeof(ok), "{\"ok\":true,\"ssid\":\"%s\"}\n", ssid);
|
||||
send_json(req, 200, ok);
|
||||
|
||||
free(ssid);
|
||||
free(pass);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t handle_wifi_forget(httpd_req_t *req)
|
||||
{
|
||||
if (req->content_len <= 0 || req->content_len >= HTTPD_BODY_MAX) {
|
||||
send_json(req, 400, "{\"ok\":false,\"error\":\"bad body\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
char *form = malloc((size_t)req->content_len + 1);
|
||||
if (form == NULL) {
|
||||
send_json(req, 500, "{\"ok\":false,\"error\":\"no mem\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
int got = httpd_req_recv(req, form, (size_t)req->content_len);
|
||||
if (got != req->content_len) {
|
||||
free(form);
|
||||
send_json(req, 400, "{\"ok\":false,\"error\":\"short body\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
form[got] = '\0';
|
||||
|
||||
char *ssid = get_form_param(form, "ssid");
|
||||
free(form);
|
||||
|
||||
if (ssid) {
|
||||
size_t slen = strlen(ssid);
|
||||
while (slen > 0 && (ssid[slen - 1] == ' ' || ssid[slen - 1] == '\r' || ssid[slen - 1] == '\n')) {
|
||||
ssid[--slen] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if (ssid == NULL || strlen(ssid) == 0) {
|
||||
free(ssid);
|
||||
send_json(req, 400, "{\"ok\":false,\"error\":\"missing ssid\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
wifi_manager_delete_profile(ssid);
|
||||
char ok[128];
|
||||
snprintf(ok, sizeof(ok), "{\"ok\":true,\"ssid\":\"%s\"}\n", ssid);
|
||||
send_json(req, 200, ok);
|
||||
|
||||
free(ssid);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* SoftAP + server */
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -454,8 +840,8 @@ esp_err_t web_server_start(void)
|
||||
(void)esp_netif_dhcps_start(ap); /* harmless if someone else started it */
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.stack_size = 6 * 1024;
|
||||
config.max_uri_handlers = 12;
|
||||
config.stack_size = 8 * 1024;
|
||||
config.max_uri_handlers = 24;
|
||||
config.lru_purge_enable = true;
|
||||
config.keep_alive_enable = true;
|
||||
config.max_open_sockets = 4;
|
||||
@@ -466,6 +852,7 @@ esp_err_t web_server_start(void)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Core Pairing & Card APIs */
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_ROOT));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_LAST_UID));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_BIND));
|
||||
@@ -473,6 +860,25 @@ esp_err_t web_server_start(void)
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_ART));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_UNBIND));
|
||||
|
||||
/* Wi-Fi Management APIs */
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_WIFI_STATUS));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_WIFI_SCAN));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_WIFI_SAVED));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_WIFI_CONNECT));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_WIFI_FORGET));
|
||||
|
||||
/* Captive Portal OS Probes */
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_APPLE));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_204_1));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_204_2));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_WIN_1));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_WIN_2));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_FF_1));
|
||||
ESP_ERROR_CHECK(httpd_register_uri_handler(s_server, &URI_CP_FF_2));
|
||||
|
||||
/* Start Captive Portal DNS redirection server */
|
||||
dns_server_start();
|
||||
|
||||
ESP_LOGI(TAG, "Web server ready on all interfaces (Hotspot: 192.168.4.1, Wi-Fi: %s)", wifi_manager_get_ip());
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user