修复了WS连接稳定性问题

This commit is contained in:
CSBOOK\44744
2026-06-27 20:29:09 +08:00
parent b829cd8ebc
commit caa4a7eded
16 changed files with 2197 additions and 248 deletions
+20 -18
View File
@@ -254,39 +254,41 @@ esp_err_t wifi_portal_start_config_ap(const char *ssid_prefix)
esp_err_t wifi_portal_connect_sta(int timeout_ms)
{
ESP_RETURN_ON_ERROR(wifi_portal_lock(), TAG, "Failed to lock Wi-Fi portal");
PortalUnlockGuard guard;
{
ESP_RETURN_ON_ERROR(wifi_portal_lock(), TAG, "Failed to lock Wi-Fi portal");
PortalUnlockGuard guard;
if (!wifi_portal_has_saved_credentials()) {
return ESP_ERR_NOT_FOUND;
}
if (!wifi_portal_has_saved_credentials()) {
return ESP_ERR_NOT_FOUND;
}
ESP_LOGI(TAG,
"Starting STA connection with %u saved credential candidate(s)",
(unsigned)SsidManager::GetInstance().GetSsidList().size());
log_saved_wifi_candidates("STA connect");
ESP_LOGI(TAG,
"Starting STA connection with %u saved credential candidate(s)",
(unsigned)SsidManager::GetInstance().GetSsidList().size());
log_saved_wifi_candidates("STA connect");
ESP_RETURN_ON_ERROR(ensure_wifi_manager_initialized(), TAG, "Failed to initialize Wi-Fi manager");
ESP_RETURN_ON_ERROR(ensure_wifi_manager_initialized(), TAG, "Failed to initialize Wi-Fi manager");
auto &manager = WifiManager::GetInstance();
if (manager.IsConnected()) {
auto &manager = WifiManager::GetInstance();
if (manager.IsConnected()) {
(void)apply_sta_stability_policy();
return ESP_OK;
}
manager.StartStation();
(void)apply_sta_stability_policy();
return ESP_OK;
}
manager.StartStation();
(void)apply_sta_stability_policy();
int effective_timeout_ms = normalize_connect_timeout_ms(timeout_ms);
int64_t deadline_us = esp_timer_get_time() + (int64_t)effective_timeout_ms * 1000;
while (esp_timer_get_time() < deadline_us) {
if (manager.IsConnected()) {
if (WifiManager::GetInstance().IsConnected()) {
return ESP_OK;
}
vTaskDelay(pdMS_TO_TICKS(WIFI_PORTAL_CONNECT_POLL_MS));
}
manager.StopStation();
WifiManager::GetInstance().StopStation();
ESP_LOGW(TAG, "Wi-Fi connect timed out after %d ms", effective_timeout_ms);
return ESP_ERR_TIMEOUT;
}