feat: access point name and password setting, ap_on in status
- ap_ssid and ap_password on /api/network: empty name uses led-XXXXXX, a password of 8-63 characters makes the access point WPA2, empty is open; the password is never sent back (ap_password_set) - a running access point restarts when its name or password changes - /api/status reports ap_on; the web page gets the fields Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
af82cb4c4a
commit
59eb37cad5
7 files changed
+97
-6
No files matched your search
+37
-5
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user