diff --git a/main/net.cpp b/main/net.cpp index e2dea9d..0c62628 100644 --- a/main/net.cpp +++ b/main/net.cpp @@ -47,21 +47,31 @@ static void apply_sta_config(const wifi_creds_t *c) esp_wifi_set_config(WIFI_IF_STA, &cfg); } -// Turn the access point on (with its led-XXXXXX name, open) or off. The client keeps running either way. +// Turn the access point on (its saved name and password, or led-XXXXXX and open) or off. +// The client keeps running either way. static void apply_ap(bool on) { if (on) { - char ssid[16]; + net_cfg_t n; + settings_load_net(&n); + char ssid[33]; net_ap_ssid(ssid, sizeof(ssid)); wifi_config_t ap = {}; strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1); ap.ap.ssid_len = strlen(ssid); ap.ap.channel = AP_CHANNEL; ap.ap.max_connection = AP_MAX_CLIENTS; - ap.ap.authmode = WIFI_AUTH_OPEN; + // A password of 8-63 characters is WPA2; anything else is an open network + const size_t plen = strlen(n.ap_pass); + if (plen >= 8) { + strncpy((char *)ap.ap.password, n.ap_pass, sizeof(ap.ap.password) - 1); + ap.ap.authmode = WIFI_AUTH_WPA2_PSK; + } else { + ap.ap.authmode = WIFI_AUTH_OPEN; + } esp_wifi_set_config(WIFI_IF_AP, &ap); esp_wifi_set_mode(WIFI_MODE_APSTA); - ESP_LOGI(TAG, "Access point %s on", ssid); + ESP_LOGI(TAG, "Access point %s on (%s)", ssid, plen >= 8 ? "WPA2" : "open"); } else { esp_wifi_set_mode(WIFI_MODE_STA); ESP_LOGI(TAG, "Access point off"); @@ -69,13 +79,19 @@ static void apply_ap(bool on) s_ap_on = on; } -// Switch the access point later, from a timer task (not inside a Wi-Fi event) +// Switch the access point later, from a timer task (not inside a Wi-Fi event). +// s_ap_reapply: the access point stays on but its name or password changed, so restart it with them. +static bool s_ap_reapply; + static void ap_timer_cb(void *arg) { (void)arg; if (s_ap_want != s_ap_on) { apply_ap(s_ap_want); + } else if (s_ap_want && s_ap_reapply) { + apply_ap(true); } + s_ap_reapply = false; } static esp_err_t apply_client_ip(const net_cfg_t *c); @@ -186,6 +202,12 @@ static void on_ip_event(void *arg, esp_event_base_t base, int32_t id, void *data void net_ap_ssid(char *buf, size_t len) { + net_cfg_t n; + settings_load_net(&n); + if (n.ap_ssid[0]) { + snprintf(buf, len, "%s", n.ap_ssid); + return; + } uint8_t mac[6]; esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); snprintf(buf, len, "led-%02X%02X%02X", mac[3], mac[4], mac[5]); @@ -255,6 +277,11 @@ esp_err_t net_start(void) return ESP_OK; } +bool net_ap_on(void) +{ + return s_ap_on; +} + bool net_sta_status(char *ip, size_t ip_len) { if (ip_len) { @@ -296,9 +323,14 @@ esp_err_t net_save_network(const net_cfg_t *c) if (err != ESP_OK) { return err; } + const bool ap_cfg_changed = strcmp(old.ap_ssid, c->ap_ssid) != 0 || strcmp(old.ap_pass, c->ap_pass) != 0; if (c->ap_enabled != s_ap_on) { // After the reply has gone out, so the page that asked still gets it ap_switch_later(c->ap_enabled, c->ap_enabled ? 1000000 : 1500000); + } else if (c->ap_enabled && ap_cfg_changed) { + // Still on, with a new name or password: restart it after the reply has gone out + s_ap_reapply = true; + ap_switch_later(true, 1500000); } // Reconnect with the new address settings if a client is configured (after the reply, see reconnect_cb) wifi_creds_t w; diff --git a/main/net.h b/main/net.h index 0e9c274..03ab733 100644 --- a/main/net.h +++ b/main/net.h @@ -16,6 +16,9 @@ esp_err_t net_start(void); /** @brief Fill buf with the AP SSID, e.g. "led-A1B2C3". */ void net_ap_ssid(char *buf, size_t len); +/** @brief True while the access point is running. */ +bool net_ap_on(void); + /** @brief Client link state and its IPv4 address ("" if not connected). */ bool net_sta_status(char *ip, size_t ip_len); diff --git a/main/settings.cpp b/main/settings.cpp index b6c6e7d..23bd158 100644 --- a/main/settings.cpp +++ b/main/settings.cpp @@ -134,6 +134,8 @@ esp_err_t settings_load_net(net_cfg_t *c) char ap[8]; get_str(h, "ap", ap, sizeof(ap), "on"); c->ap_enabled = strcmp(ap, "off") != 0; + get_str(h, "ap_ssid", c->ap_ssid, sizeof(c->ap_ssid), ""); + get_str(h, "ap_pass", c->ap_pass, sizeof(c->ap_pass), ""); char ddp[8]; get_str(h, "ddp", ddp, sizeof(ddp), "on"); c->ddp_enabled = strcmp(ddp, "off") != 0; @@ -155,6 +157,8 @@ esp_err_t settings_save_net(const net_cfg_t *c) if (err == ESP_OK) err = nvs_set_str(h, "gateway", c->gateway); if (err == ESP_OK) err = nvs_set_str(h, "dns", c->dns); if (err == ESP_OK) err = nvs_set_str(h, "ap", c->ap_enabled ? "on" : "off"); + if (err == ESP_OK) err = nvs_set_str(h, "ap_ssid", c->ap_ssid); + if (err == ESP_OK) err = nvs_set_str(h, "ap_pass", c->ap_pass); if (err == ESP_OK) err = nvs_set_str(h, "ddp", c->ddp_enabled ? "on" : "off"); if (err == ESP_OK) err = nvs_commit(h); nvs_close(h); diff --git a/main/settings.h b/main/settings.h index ff5e677..f1530f1 100644 --- a/main/settings.h +++ b/main/settings.h @@ -20,6 +20,8 @@ struct net_cfg_t { char gateway[16]; char dns[16]; // optional; empty uses the gateway bool ap_enabled; // the access point (led-XXXXXX) is on; default on + char ap_ssid[33]; // access point name; empty uses led-XXXXXX from the MAC + char ap_pass[65]; // access point password, 8-63 characters (WPA2); empty is an open network bool ddp_enabled; // DDP pixels and frame sync are received; default on }; diff --git a/main/web.cpp b/main/web.cpp index d0526d5..c616dd7 100644 --- a/main/web.cpp +++ b/main/web.cpp @@ -244,6 +244,7 @@ static esp_err_t status_get(api_req_t *req) settings_load_net(&n); cJSON *o = preset_to_json(&p); cJSON_AddStringToObject(o, "ap_ssid", ap); + cJSON_AddBoolToObject(o, "ap_on", net_ap_on()); cJSON_AddStringToObject(o, "hostname", n.hostname); cJSON_AddBoolToObject(o, "sta_connected", up); cJSON_AddStringToObject(o, "sta_ip", ip); @@ -389,6 +390,11 @@ static esp_err_t network_get(api_req_t *req) cJSON_AddBoolToObject(o, "connected", up); cJSON_AddStringToObject(o, "current_ip", ip); cJSON_AddBoolToObject(o, "ap_enabled", n.ap_enabled); + char ap_name[33]; + net_ap_ssid(ap_name, sizeof(ap_name)); + cJSON_AddStringToObject(o, "ap_ssid", ap_name); // the name in use now + cJSON_AddBoolToObject(o, "ap_ssid_custom", n.ap_ssid[0] != '\0'); + cJSON_AddBoolToObject(o, "ap_password_set", strlen(n.ap_pass) >= 8); // the password itself is never sent cJSON_AddBoolToObject(o, "ddp_enabled", n.ddp_enabled); return send_json(req, o); } @@ -448,6 +454,24 @@ static esp_err_t network_post(api_req_t *req) if (cJSON_IsBool(ap)) { n.ap_enabled = cJSON_IsTrue(ap); } + // Access point name: empty goes back to led-XXXXXX. Password: empty is open, else 8-63 characters. + cJSON *apn = cJSON_GetObjectItem(in, "ap_ssid"); + if (cJSON_IsString(apn)) { + if (strlen(apn->valuestring) > 32) { + cJSON_Delete(in); + return send_error(req, "400 Bad Request", "ap_ssid: up to 32 characters, or empty for the default name"); + } + snprintf(n.ap_ssid, sizeof(n.ap_ssid), "%s", apn->valuestring); + } + cJSON *app = cJSON_GetObjectItem(in, "ap_password"); + if (cJSON_IsString(app)) { + const size_t plen = strlen(app->valuestring); + if (plen != 0 && (plen < 8 || plen > 63)) { + cJSON_Delete(in); + return send_error(req, "400 Bad Request", "ap_password: empty (open) or 8-63 characters"); + } + snprintf(n.ap_pass, sizeof(n.ap_pass), "%s", app->valuestring); + } cJSON *ddp = cJSON_GetObjectItem(in, "ddp_enabled"); if (cJSON_IsBool(ddp)) { n.ddp_enabled = cJSON_IsTrue(ddp); diff --git a/web/app.js b/web/app.js index a1a276d..a828912 100644 --- a/web/app.js +++ b/web/app.js @@ -596,6 +596,8 @@ $('globalOut').textContent = level; } $('apName').textContent = s.ap_ssid; + $('apInfo').hidden = !s.ap_on; + $('apOffInfo').hidden = !!s.ap_on; $('staState').textContent = s.sta_connected ? 'connected (' + s.sta_ip + ')' : 'not connected'; await loadPatterns(s); } @@ -888,6 +890,11 @@ $('sGw').value = n.gateway; $('sDns').value = n.dns; $('apOn').checked = n.ap_enabled !== false; + $('apSsid').value = n.ap_ssid_custom ? n.ap_ssid : ''; + $('apSsid').placeholder = n.ap_ssid; + $('apPass').value = ''; + $('apOpen').checked = false; + $('apPassHint').textContent = n.ap_password_set ? '(set; blank keeps it)' : '(open network now)'; $('ddpOn').checked = n.ddp_enabled !== false; updateStaticFields(); } @@ -942,12 +949,23 @@ dns: $('sDns').value.trim(), ap_enabled: $('apOn').checked, ddp_enabled: $('ddpOn').checked, + ap_ssid: $('apSsid').value.trim(), }; + if ($('apOpen').checked) { + body.ap_password = ''; + } else if ($('apPass').value) { + body.ap_password = $('apPass').value; + } $('netMsg').textContent = 'Saving…'; try { const j = await post('/api/network', body); $('netMsg').textContent = `Saved. The client reconnects; open http://${j.address}/ in a few seconds.`; $('mdnsName').textContent = body.hostname; + $('apPass').value = ''; + $('apOpen').checked = false; + if (body.ap_password !== undefined) { + $('apPassHint').textContent = body.ap_password ? '(set; blank keeps it)' : '(open network now)'; + } } catch (e) { $('netMsg').textContent = e.message; } diff --git a/web/index.html b/web/index.html index 456c27e..2a44fb1 100644 --- a/web/index.html +++ b/web/index.html @@ -117,7 +117,8 @@
Network … (open). Address 192.168.4.1.
+Network … (open). Address 192.168.4.1.
+Access point is off.
Status: …
@@ -161,6 +162,13 @@Turn it off to use only your Wi-Fi network. Turning it off needs a connected Wi-Fi network. If the network stays down for about a minute, the access point comes back on by itself.