From 163f946e502945d118b2db045ba3b69a5fd849cd Mon Sep 17 00:00:00 2001 From: Asklv Date: Sun, 8 Mar 2026 23:04:14 +0800 Subject: [PATCH] feat: keep admin portal online --- main/mimi.c | 6 +++++- main/onboard/wifi_onboard.h | 13 +++++++++---- main/wifi/wifi_manager.c | 13 ++++++++++++- main/wifi/wifi_manager.h | 5 +++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/main/mimi.c b/main/mimi.c index 3712b72..b949970 100644 --- a/main/mimi.c +++ b/main/mimi.c @@ -159,10 +159,14 @@ void app_main(void) if (!wifi_ok) { ESP_LOGW(TAG, "Entering WiFi onboarding mode..."); - wifi_onboard_start(); /* blocks, restarts on success */ + wifi_onboard_start(WIFI_ONBOARD_MODE_CAPTIVE); /* blocks, restarts on success */ return; /* unreachable */ } + if (wifi_onboard_start(WIFI_ONBOARD_MODE_ADMIN) != ESP_OK) { + ESP_LOGW(TAG, "Local admin portal unavailable; continuing without config hotspot"); + } + { /* Outbound dispatch task should start first to avoid dropping early replies. */ ESP_ERROR_CHECK((xTaskCreatePinnedToCore( diff --git a/main/onboard/wifi_onboard.h b/main/onboard/wifi_onboard.h index 79b53b3..a6e0d66 100644 --- a/main/onboard/wifi_onboard.h +++ b/main/onboard/wifi_onboard.h @@ -2,9 +2,14 @@ #include "esp_err.h" +typedef enum { + WIFI_ONBOARD_MODE_CAPTIVE = 0, + WIFI_ONBOARD_MODE_ADMIN, +} wifi_onboard_mode_t; + /** - * Start WiFi onboarding captive portal. - * Opens a soft AP, DNS hijacker, and HTTP configuration server. - * Blocks until the user submits credentials, then saves to NVS and restarts. + * Start WiFi onboarding/configuration portal. + * CAPTIVE mode opens DNS hijack + config page and blocks forever. + * ADMIN mode keeps a local config hotspot alive without captive redirects. */ -esp_err_t wifi_onboard_start(void); +esp_err_t wifi_onboard_start(wifi_onboard_mode_t mode); diff --git a/main/wifi/wifi_manager.c b/main/wifi/wifi_manager.c index 0bb34b6..9ca967b 100644 --- a/main/wifi/wifi_manager.c +++ b/main/wifi/wifi_manager.c @@ -15,6 +15,7 @@ static EventGroupHandle_t s_wifi_event_group; static int s_retry_count = 0; static char s_ip_str[16] = "0.0.0.0"; static bool s_connected = false; +static bool s_reconnect_enabled = true; static const char *wifi_reason_to_str(wifi_err_reason_t reason) { @@ -44,7 +45,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, if (disc) { ESP_LOGW(TAG, "Disconnected (reason=%d:%s)", disc->reason, wifi_reason_to_str(disc->reason)); } - if (s_retry_count < MIMI_WIFI_MAX_RETRY) { + if (s_reconnect_enabled && s_retry_count < MIMI_WIFI_MAX_RETRY) { /* Exponential backoff: 1s, 2s, 4s, 8s, ... capped at 30s */ uint32_t delay_ms = MIMI_WIFI_RETRY_BASE_MS << s_retry_count; if (delay_ms > MIMI_WIFI_RETRY_MAX_MS) { @@ -120,6 +121,7 @@ esp_err_t wifi_manager_start(void) return ESP_ERR_NOT_FOUND; } + s_reconnect_enabled = true; ESP_LOGI(TAG, "Connecting to SSID: %s", wifi_cfg.sta.ssid); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); @@ -252,6 +254,7 @@ bool wifi_manager_has_credentials(void) esp_err_t wifi_manager_stop(void) { + s_reconnect_enabled = false; esp_wifi_disconnect(); esp_wifi_stop(); s_connected = false; @@ -260,3 +263,11 @@ esp_err_t wifi_manager_stop(void) ESP_LOGI(TAG, "WiFi stopped"); return ESP_OK; } + +void wifi_manager_set_reconnect_enabled(bool enabled) +{ + s_reconnect_enabled = enabled; + if (!enabled) { + s_retry_count = 0; + } +} diff --git a/main/wifi/wifi_manager.h b/main/wifi/wifi_manager.h index d25e316..e6f6661 100644 --- a/main/wifi/wifi_manager.h +++ b/main/wifi/wifi_manager.h @@ -59,3 +59,8 @@ bool wifi_manager_has_credentials(void); * Stop WiFi (for mode switching during onboarding). */ esp_err_t wifi_manager_stop(void); + +/** + * Enable or disable STA auto-reconnect on disconnect events. + */ +void wifi_manager_set_reconnect_enabled(bool enabled);