#include "wifi_configuration_ap.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ssid_manager.h" #define TAG "WifiConfigurationAp" #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 extern const char index_html_start[] asm("_binary_wifi_configuration_html_start"); extern const char done_html_start[] asm("_binary_wifi_configuration_done_html_start"); extern const unsigned char server_cert_pem_start[] asm("_binary_server_cert_pem_start"); extern const unsigned char server_cert_pem_end[] asm("_binary_server_cert_pem_end"); extern const unsigned char server_key_pem_start[] asm("_binary_server_key_pem_start"); extern const unsigned char server_key_pem_end[] asm("_binary_server_key_pem_end"); static const char kHttpsConfigDomain[] = "https://borlanet-dev.lkall.com"; namespace { constexpr const char* kJsonContentType = "application/json; charset=utf-8"; constexpr const char* kHtmlContentType = "text/html; charset=utf-8"; constexpr const char* kTextContentType = "text/plain; charset=utf-8"; size_t SafeSsidLen(const uint8_t* ssid) { return strnlen(reinterpret_cast(ssid), 32); } void SendJsonEscaped(httpd_req_t* req, const char* data, size_t len) { char buf[8]; for (size_t i = 0; i < len; ++i) { unsigned char c = static_cast(data[i]); switch (c) { case '\"': httpd_resp_sendstr_chunk(req, "\\\""); break; case '\\': httpd_resp_sendstr_chunk(req, "\\\\"); break; case '\b': httpd_resp_sendstr_chunk(req, "\\b"); break; case '\f': httpd_resp_sendstr_chunk(req, "\\f"); break; case '\n': httpd_resp_sendstr_chunk(req, "\\n"); break; case '\r': httpd_resp_sendstr_chunk(req, "\\r"); break; case '\t': httpd_resp_sendstr_chunk(req, "\\t"); break; default: if (c < 0x20) { snprintf(buf, sizeof(buf), "\\u%04x", c); httpd_resp_sendstr_chunk(req, buf); } else { char out[2] = {static_cast(c), 0}; httpd_resp_sendstr_chunk(req, out); } } } } void AppendJsonEscaped(std::string& out, const uint8_t* data, size_t len) { char buf[8]; for (size_t i = 0; i < len; ++i) { unsigned char c = data[i]; switch (c) { case '\"': out += "\\\""; break; case '\\': out += "\\\\"; break; case '\b': out += "\\b"; break; case '\f': out += "\\f"; break; case '\n': out += "\\n"; break; case '\r': out += "\\r"; break; case '\t': out += "\\t"; break; default: if (c < 0x20) { snprintf(buf, sizeof(buf), "\\u%04x", c); out += buf; } else { out.push_back(static_cast(c)); } } } } void LogRequestInfo(const char* tag, httpd_req_t* req) { char host[128] = {0}; if (httpd_req_get_hdr_value_str(req, "Host", host, sizeof(host)) == ESP_OK) { ESP_LOGD(TAG, "%s host=%s uri=%s", tag, host, req->uri); } else { ESP_LOGD(TAG, "%s uri=%s", tag, req->uri); } } } // namespace WifiConfigurationAp& WifiConfigurationAp::GetInstance() { static WifiConfigurationAp instance; return instance; } WifiConfigurationAp::WifiConfigurationAp() { event_group_ = xEventGroupCreate(); language_ = "zh-CN"; sleep_mode_ = false; scan_config_.ssid = nullptr; scan_config_.bssid = nullptr; scan_config_.channel = 0; scan_config_.show_hidden = true; scan_config_.scan_type = WIFI_SCAN_TYPE_ACTIVE; scan_config_.scan_time.active.min = 100; scan_config_.scan_time.active.max = 300; } std::vector WifiConfigurationAp::GetAccessPoints() { std::lock_guard lock(mutex_); return ap_records_; } WifiConfigurationAp::~WifiConfigurationAp() { if (scan_timer_) { esp_timer_stop(scan_timer_); esp_timer_delete(scan_timer_); } if (event_group_) { vEventGroupDelete(event_group_); } // Unregister event handlers if they were registered if (instance_any_id_) { esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id_); } if (instance_got_ip_) { esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip_); } } void WifiConfigurationAp::SetLanguage(const std::string &&language) { language_ = language; } void WifiConfigurationAp::SetSsidPrefix(const std::string &&ssid_prefix) { ssid_prefix_ = ssid_prefix; } void WifiConfigurationAp::SetConfigSuccessCallback(std::function callback) { on_config_success_ = std::move(callback); } void WifiConfigurationAp::Start() { // Mute noisy TLS connection resets during captive HTTPS (client often closes early). esp_log_level_set("Dynamic Impl", ESP_LOG_NONE); config_success_notified_ = false; if (config_success_timer_) { esp_timer_stop(config_success_timer_); esp_timer_delete(config_success_timer_); config_success_timer_ = nullptr; } // Register event handlers ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &WifiConfigurationAp::WifiEventHandler, this, &instance_any_id_)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &WifiConfigurationAp::IpEventHandler, this, &instance_got_ip_)); StartAccessPoint(); StartWebServer(); } void WifiConfigurationAp::NotifyConfigSuccess() { if (config_success_notified_) { return; } config_success_notified_ = true; if (!on_config_success_) { return; } if (config_success_timer_) { esp_timer_stop(config_success_timer_); esp_timer_delete(config_success_timer_); config_success_timer_ = nullptr; } esp_timer_create_args_t timer_args = { .callback = [](void* arg) { auto* self = static_cast(arg); if (self->on_config_success_) { self->on_config_success_(); } }, .arg = this, .dispatch_method = ESP_TIMER_TASK, .name = "wifi_cfg_done" }; ESP_ERROR_CHECK(esp_timer_create(&timer_args, &config_success_timer_)); // Delay slightly to ensure HTTP response is flushed before stopping server. ESP_ERROR_CHECK(esp_timer_start_once(config_success_timer_, 300 * 1000)); } void WifiConfigurationAp::StartScan() { constexpr int64_t kMinScanIntervalUs = 5 * 1000000; int64_t now = esp_timer_get_time(); { std::lock_guard lock(mutex_); if (scan_in_progress_) { return; } if (last_scan_time_us_ != 0 && (now - last_scan_time_us_) < kMinScanIntervalUs) { return; } scan_in_progress_ = true; } auto err = esp_wifi_scan_start(&scan_config_, false); if (err == ESP_ERR_WIFI_STATE || err == ESP_ERR_INVALID_STATE) { ESP_LOGW(TAG, "WiFi scan already running"); std::lock_guard lock(mutex_); scan_in_progress_ = false; return; } if (err != ESP_OK) { ESP_LOGW(TAG, "Failed to start WiFi scan: %d", err); std::lock_guard lock(mutex_); scan_in_progress_ = false; } } std::string WifiConfigurationAp::GetSsid() { // Get MAC and use it to generate a unique SSID uint8_t mac[6]; #if CONFIG_IDF_TARGET_ESP32P4 esp_wifi_get_mac(WIFI_IF_AP, mac); #else ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP)); #endif char ssid[32]; snprintf(ssid, sizeof(ssid), "%s-%02X%02X", ssid_prefix_.c_str(), mac[4], mac[5]); return std::string(ssid); } std::string WifiConfigurationAp::GetWebServerUrl() { return kHttpsConfigDomain; } void WifiConfigurationAp::StartAccessPoint() { // Initialize the TCP/IP stack ESP_ERROR_CHECK(esp_netif_init()); // Create the default event loop ap_netif_ = esp_netif_create_default_wifi_ap(); // Set the router IP address to 192.168.4.1 esp_netif_ip_info_t ip_info; IP4_ADDR(&ip_info.ip, 192, 168, 4, 1); IP4_ADDR(&ip_info.gw, 192, 168, 4, 1); IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0); esp_err_t dhcp_stop_err = esp_netif_dhcps_stop(ap_netif_); if (dhcp_stop_err != ESP_OK && dhcp_stop_err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) { ESP_ERROR_CHECK(dhcp_stop_err); } ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif_, &ip_info)); // Initialize the WiFi stack in Access Point mode wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_initialized_ = true; // Get the SSID std::string ssid = GetSsid(); // Set the WiFi configuration wifi_config_t wifi_config = {}; strcpy((char *)wifi_config.ap.ssid, ssid.c_str()); wifi_config.ap.ssid_len = ssid.length(); wifi_config.ap.max_connection = 4; wifi_config.ap.authmode = WIFI_AUTH_OPEN; // Start the WiFi Access Point ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); //ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); ESP_ERROR_CHECK(esp_wifi_start()); wifi_started_ = true; // esp_wifi_set_band_mode is ESP-IDF 5.4+ API, skip for 5.3 // Configure DHCP after Wi-Fi start so the AP advertises itself as the DNS // server and captive portal endpoint for borlanet-dev.lkall.com. esp_netif_dns_info_t dns_info = {}; dns_info.ip.type = ESP_IPADDR_TYPE_V4; dns_info.ip.u_addr.ip4 = ip_info.gw; ESP_ERROR_CHECK(esp_netif_set_dns_info(ap_netif_, ESP_NETIF_DNS_MAIN, &dns_info)); uint8_t dhcps_offer_dns = 1; ESP_ERROR_CHECK(esp_netif_dhcps_option( ap_netif_, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_offer_dns, sizeof(dhcps_offer_dns))); // ESP_NETIF_CAPTIVEPORTAL_URI is ESP-IDF 5.4+ API, skip for 5.3 esp_err_t dhcp_start_err = esp_netif_dhcps_start(ap_netif_); if (dhcp_start_err != ESP_OK && dhcp_start_err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) { ESP_ERROR_CHECK(dhcp_start_err); } dns_server_.Start(ip_info.gw); ESP_LOGI(TAG, "Access Point started with SSID %s", ssid.c_str()); // 加载高级配置 nvs_handle_t nvs; esp_err_t err = nvs_open("wifi", NVS_READONLY, &nvs); if (err == ESP_OK) { // 读取OTA URL char ota_url[256] = {0}; size_t ota_url_size = sizeof(ota_url); err = nvs_get_str(nvs, "ota_url", ota_url, &ota_url_size); if (err == ESP_OK) { ota_url_ = ota_url; } // 读取WiFi功率 err = nvs_get_i8(nvs, "max_tx_power", &max_tx_power_); if (err == ESP_OK) { ESP_LOGI(TAG, "WiFi max tx power from NVS: %d", max_tx_power_); ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(max_tx_power_)); } else { esp_wifi_get_max_tx_power(&max_tx_power_); } // 读取BSSID记忆设置 uint8_t remember_bssid = 0; err = nvs_get_u8(nvs, "remember_bssid", &remember_bssid); if (err == ESP_OK) { remember_bssid_ = remember_bssid != 0; } else { remember_bssid_ = false; // 默认值 } // 读取睡眠模式设置 uint8_t sleep_mode = 0; err = nvs_get_u8(nvs, "sleep_mode", &sleep_mode); if (err == ESP_OK) { sleep_mode_ = sleep_mode != 0; } else { sleep_mode_ = true; // 默认值 } nvs_close(nvs); } } void WifiConfigurationAp::StartWebServer() { auto register_handlers = [this](httpd_handle_t server) { auto send_empty_response = [](httpd_req_t* req) -> esp_err_t { httpd_resp_set_hdr(req, "Cache-Control", "no-store"); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_set_status(req, "204 No Content"); httpd_resp_send(req, NULL, 0); return ESP_OK; }; // Register the index.html file httpd_uri_t index_html = { .uri = "/", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { LogRequestInfo("serve_root", req); httpd_resp_set_type(req, kHtmlContentType); httpd_resp_set_hdr(req, "Cache-Control", "no-store"); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, index_html_start, strlen(index_html_start)); return ESP_OK; }, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &index_html)); // Register the /saved/list URI httpd_uri_t saved_list = { .uri = "/saved/list", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { auto ssid_list = SsidManager::GetInstance().GetSsidList(); httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_sendstr_chunk(req, "["); for (size_t i = 0; i < ssid_list.size(); ++i) { if (i > 0) { httpd_resp_sendstr_chunk(req, ","); } httpd_resp_sendstr_chunk(req, "\""); SendJsonEscaped(req, ssid_list[i].ssid.c_str(), ssid_list[i].ssid.size()); httpd_resp_sendstr_chunk(req, "\""); } httpd_resp_sendstr_chunk(req, "]"); httpd_resp_sendstr_chunk(req, NULL); return ESP_OK; }, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &saved_list)); // Register the /saved/set_default URI httpd_uri_t saved_set_default = { .uri = "/saved/set_default", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { std::string uri = req->uri; auto pos = uri.find("?index="); if (pos != std::string::npos) { int index = -1; sscanf(&req->uri[pos+7], "%d", &index); ESP_LOGI(TAG, "Set default item %d", index); SsidManager::GetInstance().SetDefaultSsid(index); } // send {} httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, "{}", HTTPD_RESP_USE_STRLEN); return ESP_OK; }, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &saved_set_default)); // Register the /saved/delete URI httpd_uri_t saved_delete = { .uri = "/saved/delete", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { std::string uri = req->uri; auto pos = uri.find("?index="); if (pos != std::string::npos) { int index = -1; sscanf(&req->uri[pos+7], "%d", &index); ESP_LOGI(TAG, "Delete saved list item %d", index); SsidManager::GetInstance().RemoveSsid(index); } // send {} httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, "{}", HTTPD_RESP_USE_STRLEN); return ESP_OK; }, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &saved_delete)); // Register the /scan URI httpd_uri_t scan = { .uri = "/scan", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { LogRequestInfo("serve_scan", req); auto *this_ = static_cast(req->user_ctx); std::vector ap_records; { std::lock_guard lock(this_->mutex_); ap_records = this_->ap_records_; } // Check if 5G is supported bool support_5g = false; #ifdef CONFIG_SOC_WIFI_SUPPORT_5G support_5g = true; #endif uint8_t sta_mac[6] = {0}; esp_err_t mac_err = esp_wifi_get_mac(WIFI_IF_STA, sta_mac); char mac_str[18] = {0}; if (mac_err == ESP_OK) { snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X", sta_mac[0], sta_mac[1], sta_mac[2], sta_mac[3], sta_mac[4], sta_mac[5]); } // Build JSON in memory and send once to avoid HTTPS chunked edge-cases. std::string response; response.reserve(128 + ap_records.size() * 96); response += "{\"support_5g\":"; response += support_5g ? "true" : "false"; response += ",\"sta_mac\":"; if (mac_err == ESP_OK) { response += "\""; response += mac_str; response += "\""; } else { response += "null"; } response += ",\"aps\":["; for (int i = 0; i < ap_records.size(); i++) { size_t ssid_len = SafeSsidLen(ap_records[i].ssid); ESP_LOGD(TAG, "SSID: %.*s, RSSI: %d, Authmode: %d", static_cast(ssid_len), reinterpret_cast(ap_records[i].ssid), ap_records[i].rssi, ap_records[i].authmode); response += "{\"ssid\":\""; AppendJsonEscaped(response, ap_records[i].ssid, ssid_len); response += "\",\"rssi\":"; char buf[32]; snprintf(buf, sizeof(buf), "%d", ap_records[i].rssi); response += buf; response += ",\"authmode\":"; snprintf(buf, sizeof(buf), "%d", ap_records[i].authmode); response += buf; response += "}"; if (i < ap_records.size() - 1) { response += ","; } } response += "]}"; httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Cache-Control", "no-store"); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, response.c_str(), response.size()); // Trigger a background scan to refresh the next response. this_->StartScan(); return ESP_OK; }, .user_ctx = this }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &scan)); httpd_uri_t service_worker = { .uri = "/service-worker.js", .method = HTTP_GET, .handler = send_empty_response, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &service_worker)); httpd_uri_t favicon = { .uri = "/favicon.ico", .method = HTTP_GET, .handler = send_empty_response, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &favicon)); httpd_uri_t cert_probe = { .uri = "/*.crt", .method = HTTP_GET, .handler = send_empty_response, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &cert_probe)); // Register the form submission httpd_uri_t form_submit = { .uri = "/submit", .method = HTTP_POST, .handler = [](httpd_req_t *req) -> esp_err_t { char *buf; size_t buf_len = req->content_len; if (buf_len > 1024) { // 限制最大请求体大小 httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Payload too large"); return ESP_FAIL; } buf = (char *)malloc(buf_len + 1); if (!buf) { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to allocate memory"); return ESP_FAIL; } int ret = httpd_req_recv(req, buf, buf_len); if (ret <= 0) { free(buf); if (ret == HTTPD_SOCK_ERR_TIMEOUT) { httpd_resp_send_408(req); } else { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Failed to receive request"); } return ESP_FAIL; } buf[ret] = '\0'; // 解析 JSON 数据 cJSON *json = cJSON_Parse(buf); free(buf); if (!json) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON"); return ESP_FAIL; } cJSON *ssid_item = cJSON_GetObjectItemCaseSensitive(json, "ssid"); cJSON *password_item = cJSON_GetObjectItemCaseSensitive(json, "password"); if (!cJSON_IsString(ssid_item) || (ssid_item->valuestring == NULL) || (strlen(ssid_item->valuestring) >= 33)) { cJSON_Delete(json); httpd_resp_send(req, "{\"success\":false,\"error\":\"Invalid SSID\"}", HTTPD_RESP_USE_STRLEN); return ESP_OK; } std::string ssid_str = ssid_item->valuestring; std::string password_str = ""; if (cJSON_IsString(password_item) && (password_item->valuestring != NULL) && (strlen(password_item->valuestring) < 65)) { password_str = password_item->valuestring; } // 获取当前对象 auto *this_ = static_cast(req->user_ctx); if (!this_->ConnectToWifi(ssid_str, password_str)) { cJSON_Delete(json); httpd_resp_send(req, "{\"success\":false,\"error\":\"Failed to connect to the Access Point\"}", HTTPD_RESP_USE_STRLEN); return ESP_OK; } this_->Save(ssid_str, password_str); cJSON_Delete(json); // 设置成功响应 httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, "{\"success\":true}", HTTPD_RESP_USE_STRLEN); this_->NotifyConfigSuccess(); return ESP_OK; }, .user_ctx = this }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &form_submit)); // Register the done.html page httpd_uri_t done_html = { .uri = "/done.html", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { httpd_resp_set_type(req, kHtmlContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, done_html_start, strlen(done_html_start)); return ESP_OK; }, .user_ctx = NULL }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &done_html)); // Register the reboot endpoint httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = [](httpd_req_t *req) -> esp_err_t { auto* this_ = static_cast(req->user_ctx); // 设置响应头,防止浏览器缓存 httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Cache-Control", "no-store"); httpd_resp_set_hdr(req, "Connection", "close"); // 发送响应 httpd_resp_send(req, "{\"success\":true}", HTTPD_RESP_USE_STRLEN); // 创建一个延迟重启任务 ESP_LOGI(TAG, "Rebooting..."); xTaskCreate([](void *ctx) { // 等待200ms确保HTTP响应完全发送 vTaskDelay(pdMS_TO_TICKS(200)); // 停止Web服务器 auto* self = static_cast(ctx); if (self->server_) { httpd_stop(self->server_); } if (self->https_server_) { httpd_ssl_stop(self->https_server_); } // 再等待100ms确保所有连接都已关闭 vTaskDelay(pdMS_TO_TICKS(100)); // 执行重启 esp_restart(); }, "reboot_task", 4096, this_, 5, NULL); return ESP_OK; }, .user_ctx = this }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &reboot)); auto captive_portal_handler = [](httpd_req_t *req) -> esp_err_t { const char* uri = req->uri; httpd_resp_set_hdr(req, "Connection", "close"); if (strncmp(uri, "/generate_204", 13) == 0) { httpd_resp_set_status(req, "204 No Content"); httpd_resp_send(req, NULL, 0); return ESP_OK; } if (strcmp(uri, "/hotspot-detect.html") == 0 || strcmp(uri, "/library/test/success.html") == 0) { httpd_resp_set_type(req, kHtmlContentType); httpd_resp_send(req, "SuccessSuccess", HTTPD_RESP_USE_STRLEN); return ESP_OK; } if (strcmp(uri, "/ncsi.txt") == 0) { httpd_resp_set_type(req, kTextContentType); httpd_resp_send(req, "Microsoft NCSI", HTTPD_RESP_USE_STRLEN); return ESP_OK; } if (strncmp(uri, "/fwlink", 7) == 0) { httpd_resp_set_type(req, kTextContentType); httpd_resp_send(req, "Microsoft Connect Test", HTTPD_RESP_USE_STRLEN); return ESP_OK; } if (strcmp(uri, "/success.txt") == 0 || strcmp(uri, "/connectivity-check.html") == 0) { httpd_resp_set_type(req, kTextContentType); httpd_resp_send(req, "success", HTTPD_RESP_USE_STRLEN); return ESP_OK; } httpd_resp_set_type(req, kTextContentType); httpd_resp_send(req, "OK", HTTPD_RESP_USE_STRLEN); return ESP_OK; }; // Register all common captive portal detection endpoints const char* captive_portal_urls[] = { "/hotspot-detect.html", // Apple "/generate_204*", // Android "/mobile/status.php", // Android "/check_network_status.txt", // Windows "/ncsi.txt", // Windows "/fwlink/", // Microsoft "/connectivity-check.html", // Firefox "/success.txt", // Various "/portal.html", // Various "/library/test/success.html" // Apple }; for (const auto& url : captive_portal_urls) { httpd_uri_t redirect_uri = { .uri = url, .method = HTTP_GET, .handler = captive_portal_handler, .user_ctx = this }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &redirect_uri)); } // Register the /advanced/config URI httpd_uri_t advanced_config = { .uri = "/advanced/config", .method = HTTP_GET, .handler = [](httpd_req_t *req) -> esp_err_t { // 获取当前对象 auto *this_ = static_cast(req->user_ctx); // 创建JSON对象 cJSON *json = cJSON_CreateObject(); if (!json) { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to create JSON"); return ESP_FAIL; } // 添加配置项到JSON if (!this_->ota_url_.empty()) { cJSON_AddStringToObject(json, "ota_url", this_->ota_url_.c_str()); } cJSON_AddNumberToObject(json, "max_tx_power", this_->max_tx_power_); cJSON_AddBoolToObject(json, "remember_bssid", this_->remember_bssid_); cJSON_AddBoolToObject(json, "sleep_mode", this_->sleep_mode_); // 发送JSON响应 char *json_str = cJSON_PrintUnformatted(json); cJSON_Delete(json); if (!json_str) { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to print JSON"); return ESP_FAIL; } httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, json_str, strlen(json_str)); free(json_str); return ESP_OK; }, .user_ctx = this }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &advanced_config)); // Register the /advanced/submit URI httpd_uri_t advanced_submit = { .uri = "/advanced/submit", .method = HTTP_POST, .handler = [](httpd_req_t *req) -> esp_err_t { char *buf; size_t buf_len = req->content_len; if (buf_len > 1024) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Payload too large"); return ESP_FAIL; } buf = (char *)malloc(buf_len + 1); if (!buf) { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to allocate memory"); return ESP_FAIL; } int ret = httpd_req_recv(req, buf, buf_len); if (ret <= 0) { free(buf); if (ret == HTTPD_SOCK_ERR_TIMEOUT) { httpd_resp_send_408(req); } else { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Failed to receive request"); } return ESP_FAIL; } buf[ret] = '\0'; // 解析JSON数据 cJSON *json = cJSON_Parse(buf); free(buf); if (!json) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON"); return ESP_FAIL; } // 获取当前对象 auto *this_ = static_cast(req->user_ctx); // 打开NVS nvs_handle_t nvs; esp_err_t err = nvs_open("wifi", NVS_READWRITE, &nvs); if (err != ESP_OK) { cJSON_Delete(json); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to open NVS"); return ESP_FAIL; } // 保存OTA URL cJSON *ota_url = cJSON_GetObjectItem(json, "ota_url"); if (cJSON_IsString(ota_url) && ota_url->valuestring) { this_->ota_url_ = ota_url->valuestring; err = nvs_set_str(nvs, "ota_url", this_->ota_url_.c_str()); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to save OTA URL: %d", err); } } // 保存WiFi功率 cJSON *max_tx_power = cJSON_GetObjectItem(json, "max_tx_power"); if (cJSON_IsNumber(max_tx_power)) { this_->max_tx_power_ = max_tx_power->valueint; err = esp_wifi_set_max_tx_power(this_->max_tx_power_); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi power: %d", err); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to set WiFi power"); return ESP_FAIL; } err = nvs_set_i8(nvs, "max_tx_power", this_->max_tx_power_); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to save WiFi power: %d", err); } } // 保存BSSID记忆设置 cJSON *remember_bssid = cJSON_GetObjectItem(json, "remember_bssid"); if (cJSON_IsBool(remember_bssid)) { this_->remember_bssid_ = cJSON_IsTrue(remember_bssid); err = nvs_set_u8(nvs, "remember_bssid", this_->remember_bssid_ ? 1 : 0); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to save remember_bssid: %d", err); } } // 保存睡眠模式设置 cJSON *sleep_mode = cJSON_GetObjectItem(json, "sleep_mode"); if (cJSON_IsBool(sleep_mode)) { this_->sleep_mode_ = cJSON_IsTrue(sleep_mode); err = nvs_set_u8(nvs, "sleep_mode", this_->sleep_mode_ ? 1 : 0); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to save sleep_mode: %d", err); } } // 提交更改 err = nvs_commit(nvs); nvs_close(nvs); cJSON_Delete(json); if (err != ESP_OK) { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to save configuration"); return ESP_FAIL; } // 发送成功响应 httpd_resp_set_type(req, kJsonContentType); httpd_resp_set_hdr(req, "Connection", "close"); httpd_resp_send(req, "{\"success\":true}", HTTPD_RESP_USE_STRLEN); ESP_LOGI(TAG, "Saved settings: ota_url=%s, max_tx_power=%d, remember_bssid=%d, sleep_mode=%d", this_->ota_url_.c_str(), this_->max_tx_power_, this_->remember_bssid_, this_->sleep_mode_); return ESP_OK; }, .user_ctx = this }; ESP_ERROR_CHECK(httpd_register_uri_handler(server, &advanced_submit)); }; // Start the HTTP server httpd_config_t config = HTTPD_DEFAULT_CONFIG(); config.max_uri_handlers = 32; // Mobile clients open several speculative connections; keep a wider pool. config.max_open_sockets = 4; config.backlog_conn = 6; config.uri_match_fn = httpd_uri_match_wildcard; config.recv_wait_timeout = 8; config.send_wait_timeout = 8; // Ensure stale sockets don't block new requests. config.lru_purge_enable = true; config.keep_alive_enable = false; ESP_ERROR_CHECK(httpd_start(&server_, &config)); register_handlers(server_); // Start the HTTPS server httpd_ssl_config_t ssl_config = HTTPD_SSL_CONFIG_DEFAULT(); ssl_config.httpd.max_uri_handlers = config.max_uri_handlers; ssl_config.httpd.max_open_sockets = config.max_open_sockets; ssl_config.httpd.backlog_conn = config.backlog_conn; ssl_config.httpd.uri_match_fn = config.uri_match_fn; ssl_config.httpd.recv_wait_timeout = config.recv_wait_timeout; ssl_config.httpd.send_wait_timeout = config.send_wait_timeout; ssl_config.httpd.lru_purge_enable = config.lru_purge_enable; ssl_config.httpd.keep_alive_enable = config.keep_alive_enable; ssl_config.servercert = server_cert_pem_start; ssl_config.servercert_len = server_cert_pem_end - server_cert_pem_start; ssl_config.prvtkey_pem = server_key_pem_start; ssl_config.prvtkey_len = server_key_pem_end - server_key_pem_start; esp_err_t https_err = httpd_ssl_start(&https_server_, &ssl_config); if (https_err == ESP_OK) { register_handlers(https_server_); } else { ESP_LOGW(TAG, "Failed to start HTTPS server: %d", https_err); } ESP_LOGI(TAG, "Web server started"); } bool WifiConfigurationAp::ConnectToWifi(const std::string &ssid, const std::string &password) { if (ssid.empty()) { ESP_LOGE(TAG, "SSID cannot be empty"); return false; } if (ssid.length() > 32) { // WiFi SSID 最大长度 ESP_LOGE(TAG, "SSID too long"); return false; } if (password.length() > 64) { ESP_LOGE(TAG, "Password too long"); return false; } is_connecting_ = true; esp_wifi_scan_stop(); xEventGroupClearBits(event_group_, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT); wifi_config_t wifi_config; bzero(&wifi_config, sizeof(wifi_config)); strlcpy((char *)wifi_config.sta.ssid, ssid.c_str(), 32); strlcpy((char *)wifi_config.sta.password, password.c_str(), 64); wifi_config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN; wifi_config.sta.failure_retry_cnt = 1; ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); auto ret = esp_wifi_connect(); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to connect to WiFi: %d", ret); is_connecting_ = false; return false; } ESP_LOGI(TAG, "Connecting to WiFi %s", ssid.c_str()); // Wait for the connection to complete for 5 seconds EventBits_t bits = xEventGroupWaitBits(event_group_, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000)); is_connecting_ = false; if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(TAG, "Connected to WiFi %s", ssid.c_str()); esp_wifi_disconnect(); return true; } else { ESP_LOGE(TAG, "Failed to connect to WiFi %s", ssid.c_str()); return false; } } void WifiConfigurationAp::Save(const std::string &ssid, const std::string &password) { ESP_LOGI(TAG, "Save SSID %s %d", ssid.c_str(), ssid.length()); SsidManager::GetInstance().AddSsid(ssid, password); } void WifiConfigurationAp::WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { WifiConfigurationAp* self = static_cast(arg); if (event_id == WIFI_EVENT_AP_STACONNECTED) { wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; ESP_LOGI(TAG, "Station " MACSTR " joined, AID=%d", MAC2STR(event->mac), event->aid); } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; ESP_LOGI(TAG, "Station " MACSTR " left, AID=%d", MAC2STR(event->mac), event->aid); } else if (event_id == WIFI_EVENT_STA_CONNECTED) { xEventGroupSetBits(self->event_group_, WIFI_CONNECTED_BIT); } else if (event_id == WIFI_EVENT_STA_DISCONNECTED) { xEventGroupSetBits(self->event_group_, WIFI_FAIL_BIT); } else if (event_id == WIFI_EVENT_SCAN_DONE) { std::lock_guard lock(self->mutex_); uint16_t ap_num = 0; esp_wifi_scan_get_ap_num(&ap_num); self->ap_records_.resize(ap_num); esp_wifi_scan_get_ap_records(&ap_num, self->ap_records_.data()); self->last_scan_time_us_ = esp_timer_get_time(); self->scan_in_progress_ = false; } } void WifiConfigurationAp::IpEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { WifiConfigurationAp* self = static_cast(arg); if (event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip)); xEventGroupSetBits(self->event_group_, WIFI_CONNECTED_BIT); } } void WifiConfigurationAp::StartSmartConfig() { // 注册SmartConfig事件处理器 ESP_ERROR_CHECK(esp_event_handler_instance_register(SC_EVENT, ESP_EVENT_ANY_ID, &WifiConfigurationAp::SmartConfigEventHandler, this, &sc_event_instance_)); // 初始化SmartConfig配置 smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT(); // cfg.esp_touch_v2_enable_crypt = true; // cfg.esp_touch_v2_key = "1234567890123456"; // 设置16字节加密密钥 // 启动SmartConfig服务 ESP_ERROR_CHECK(esp_smartconfig_start(&cfg)); ESP_LOGI(TAG, "SmartConfig started"); } void WifiConfigurationAp::SmartConfigEventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { WifiConfigurationAp *self = static_cast(arg); if (event_base == SC_EVENT){ switch (event_id){ case SC_EVENT_SCAN_DONE: ESP_LOGI(TAG, "SmartConfig scan done"); break; case SC_EVENT_FOUND_CHANNEL: ESP_LOGI(TAG, "Found SmartConfig channel"); break; case SC_EVENT_GOT_SSID_PSWD:{ ESP_LOGI(TAG, "Got SmartConfig credentials"); smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data; char ssid[32], password[64]; memcpy(ssid, evt->ssid, sizeof(evt->ssid)); memcpy(password, evt->password, sizeof(evt->password)); ESP_LOGI(TAG, "SmartConfig SSID: %s, Password: %s", ssid, password); // 尝试连接WiFi会失败,故不连接 self->Save(ssid, password); xTaskCreate([](void *ctx){ ESP_LOGI(TAG, "Restarting in 3 second"); vTaskDelay(pdMS_TO_TICKS(3000)); esp_restart(); }, "restart_task", 4096, NULL, 5, NULL); break; } case SC_EVENT_SEND_ACK_DONE: ESP_LOGI(TAG, "SmartConfig ACK sent"); esp_smartconfig_stop(); break; } } } void WifiConfigurationAp::Stop() { // 停止SmartConfig服务 if (sc_event_instance_) { esp_event_handler_instance_unregister(SC_EVENT, ESP_EVENT_ANY_ID, sc_event_instance_); sc_event_instance_ = nullptr; } esp_smartconfig_stop(); // 停止定时器 if (scan_timer_) { esp_timer_stop(scan_timer_); esp_timer_delete(scan_timer_); scan_timer_ = nullptr; } if (config_success_timer_) { esp_timer_stop(config_success_timer_); esp_timer_delete(config_success_timer_); config_success_timer_ = nullptr; } config_success_notified_ = false; // 停止Web服务器 if (server_) { httpd_stop(server_); server_ = nullptr; } if (https_server_) { httpd_ssl_stop(https_server_); https_server_ = nullptr; } // 停止DNS服务器 dns_server_.Stop(); // 注销事件处理器 if (instance_any_id_) { esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id_); instance_any_id_ = nullptr; } if (instance_got_ip_) { esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip_); instance_got_ip_ = nullptr; } // 停止WiFi并重置模式 esp_err_t err = esp_wifi_stop(); if (err != ESP_OK && err != ESP_ERR_WIFI_NOT_INIT && err != ESP_ERR_WIFI_NOT_STARTED && err != ESP_ERR_INVALID_STATE) { ESP_LOGW(TAG, "esp_wifi_stop failed: %s", esp_err_to_name(err)); } wifi_started_ = false; err = esp_wifi_deinit(); if (err != ESP_OK && err != ESP_ERR_WIFI_NOT_INIT && err != ESP_ERR_INVALID_STATE) { ESP_LOGW(TAG, "esp_wifi_deinit failed: %s", esp_err_to_name(err)); } wifi_initialized_ = false; err = esp_wifi_set_mode(WIFI_MODE_NULL); if (err != ESP_OK && err != ESP_ERR_WIFI_NOT_INIT && err != ESP_ERR_INVALID_STATE) { ESP_LOGW(TAG, "esp_wifi_set_mode(NULL) failed: %s", esp_err_to_name(err)); } // 释放网络接口资源 if (ap_netif_) { esp_netif_destroy(ap_netif_); ap_netif_ = nullptr; } ESP_LOGI(TAG, "Wifi configuration AP stopped"); }