1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-19 16:25:50 +00:00
This commit is contained in:
J. Nick Koston
2025-11-06 16:52:30 -06:00
parent 936a6cb71e
commit df35036c8d
2 changed files with 39 additions and 40 deletions

View File

@@ -109,13 +109,15 @@ void WiFiComponent::start() {
} }
#ifdef USE_WIFI_FAST_CONNECT #ifdef USE_WIFI_FAST_CONNECT
this->trying_loaded_ap_ = this->load_fast_connect_settings_(); WiFiAP params;
this->trying_loaded_ap_ = this->load_fast_connect_settings_(params);
if (!this->trying_loaded_ap_) { if (!this->trying_loaded_ap_) {
// FAST CONNECT FALLBACK: No saved settings available // FAST CONNECT FALLBACK: No saved settings available
// Use first config (will use SSID from config since scan_result_ is empty) // Use first config (will use SSID from config)
this->selected_sta_index_ = 0; this->selected_sta_index_ = 0;
params = this->build_wifi_ap_from_selected_();
} }
this->start_connecting_to_selected_(false); this->start_connecting(params, false);
#else #else
this->start_scanning(); this->start_scanning();
#endif #endif
@@ -173,11 +175,13 @@ void WiFiComponent::loop() {
// Safety check: Ensure selected_sta_index_ is valid before retrying // Safety check: Ensure selected_sta_index_ is valid before retrying
// (should already be set by retry_connect(), but check for robustness) // (should already be set by retry_connect(), but check for robustness)
this->reset_selected_ap_to_first_if_invalid_(); this->reset_selected_ap_to_first_if_invalid_();
this->start_connecting_to_selected_(false); WiFiAP params = this->build_wifi_ap_from_selected_();
this->start_connecting(params, false);
#else #else
if (this->retry_hidden_) { if (this->retry_hidden_) {
this->reset_selected_ap_to_first_if_invalid_(); this->reset_selected_ap_to_first_if_invalid_();
this->start_connecting_to_selected_(false); WiFiAP params = this->build_wifi_ap_from_selected_();
this->start_connecting(params, false);
} else { } else {
this->start_scanning(); this->start_scanning();
} }
@@ -343,11 +347,11 @@ void WiFiComponent::clear_sta() {
this->selected_sta_index_ = -1; this->selected_sta_index_ = -1;
} }
void WiFiComponent::start_connecting_to_selected_(bool two) { WiFiAP WiFiComponent::build_wifi_ap_from_selected_() const {
const WiFiAP *config = this->get_selected_sta_(); const WiFiAP *config = this->get_selected_sta_();
if (!config) { if (!config) {
ESP_LOGE(TAG, "No config selected"); ESP_LOGE(TAG, "No config selected");
return; return {};
} }
WiFiAP params; WiFiAP params;
@@ -386,7 +390,7 @@ void WiFiComponent::start_connecting_to_selected_(bool two) {
} }
} }
this->start_connecting(params, two); return params;
} }
bool WiFiComponent::sync_selected_sta_to_best_scan_result_() { bool WiFiComponent::sync_selected_sta_to_best_scan_result_() {
@@ -710,7 +714,7 @@ void WiFiComponent::check_scanning_finished() {
// SYNCHRONIZATION POINT: Establish link between scan_result_[0] and selected_sta_index_ // SYNCHRONIZATION POINT: Establish link between scan_result_[0] and selected_sta_index_
// After sorting, scan_result_[0] contains the best network. Now find which sta_[i] config // After sorting, scan_result_[0] contains the best network. Now find which sta_[i] config
// matches that network and record it in selected_sta_index_. This keeps the two indices // matches that network and record it in selected_sta_index_. This keeps the two indices
// synchronized so start_connecting_to_selected_() can safely use both to build connection parameters. // synchronized so build_wifi_ap_from_selected_() can safely use both to build connection parameters.
if (!this->sync_selected_sta_to_best_scan_result_()) { if (!this->sync_selected_sta_to_best_scan_result_()) {
ESP_LOGW(TAG, "No matching network found"); ESP_LOGW(TAG, "No matching network found");
this->retry_connect(); this->retry_connect();
@@ -719,7 +723,8 @@ void WiFiComponent::check_scanning_finished() {
yield(); yield();
this->start_connecting_to_selected_(false); WiFiAP params = this->build_wifi_ap_from_selected_();
this->start_connecting(params, false);
} }
void WiFiComponent::dump_config() { void WiFiComponent::dump_config() {
@@ -861,7 +866,8 @@ void WiFiComponent::retry_connect() {
if (this->state_ == WIFI_COMPONENT_STATE_STA_CONNECTING) { if (this->state_ == WIFI_COMPONENT_STATE_STA_CONNECTING) {
yield(); yield();
this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTING_2; this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTING_2;
this->start_connecting_to_selected_(true); WiFiAP params = this->build_wifi_ap_from_selected_();
this->start_connecting(params, true);
return; return;
} }
@@ -900,7 +906,7 @@ bool WiFiComponent::is_esp32_improv_active_() {
} }
#ifdef USE_WIFI_FAST_CONNECT #ifdef USE_WIFI_FAST_CONNECT
bool WiFiComponent::load_fast_connect_settings_() { bool WiFiComponent::load_fast_connect_settings_(WiFiAP &params) {
SavedWifiFastConnectSettings fast_connect_save{}; SavedWifiFastConnectSettings fast_connect_save{};
if (this->fast_connect_pref_.load(&fast_connect_save)) { if (this->fast_connect_pref_.load(&fast_connect_save)) {
@@ -910,18 +916,25 @@ bool WiFiComponent::load_fast_connect_settings_() {
return false; return false;
} }
// Load BSSID from saved settings // Set selected index for future operations (save, retry, etc)
this->selected_sta_index_ = fast_connect_save.ap_index;
// Build WiFiAP directly from saved settings + config
const WiFiAP &config = this->sta_[fast_connect_save.ap_index];
params.set_password(config.get_password());
params.set_manual_ip(config.get_manual_ip());
params.set_priority(config.get_priority());
#ifdef USE_WIFI_WPA2_EAP
params.set_eap(config.get_eap());
#endif
// Use saved BSSID/channel from fast connect, SSID from config
params.set_ssid(config.get_ssid());
bssid_t bssid{}; bssid_t bssid{};
std::copy(fast_connect_save.bssid, fast_connect_save.bssid + 6, bssid.begin()); std::copy(fast_connect_save.bssid, fast_connect_save.bssid + 6, bssid.begin());
params.set_bssid(bssid);
// FAST CONNECT SUCCESS: Restore saved settings without scanning params.set_channel(fast_connect_save.channel);
// SYNCHRONIZATION: Link temporary scan result with sta_[saved_index] // hidden defaults to false (network was found before, so not hidden)
// Unlike wifi_scan_done() which sorts then finds the match, here we know exactly
// which config was used before and create a matching temporary scan result
// Use SSID from config for the temporary scan result
const std::string &ssid = this->sta_[fast_connect_save.ap_index].get_ssid();
WiFiScanResult fast_connect_scan(bssid, ssid, fast_connect_save.channel, 0, false, false);
this->set_selected_sta_with_scan_(fast_connect_save.ap_index, fast_connect_scan);
ESP_LOGD(TAG, "Loaded fast_connect settings"); ESP_LOGD(TAG, "Loaded fast_connect settings");
return true; return true;

View File

@@ -339,7 +339,7 @@ class WiFiComponent : public Component {
#endif // USE_WIFI_AP #endif // USE_WIFI_AP
void print_connect_params_(); void print_connect_params_();
void start_connecting_to_selected_(bool two); WiFiAP build_wifi_ap_from_selected_() const;
const WiFiAP *get_selected_sta_() const { const WiFiAP *get_selected_sta_() const {
if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) { if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) {
@@ -354,22 +354,8 @@ class WiFiComponent : public Component {
} }
} }
// SYNCHRONIZATION HELPERS: Encapsulate the relationship between selected_sta_index_ and scan_result_ // SYNCHRONIZATION HELPER: Find which sta_[i] matches scan_result_[0] and set selected_sta_index_
// Returns true if match found, false otherwise (scan done path)
// Add temporary scan result and set selected sta index (fast connect path)
void set_selected_sta_with_scan_(int8_t sta_index, const WiFiScanResult &scan) {
#ifdef USE_RP2040
this->scan_result_.clear();
this->scan_result_.reserve(1);
#else
this->scan_result_.init(1);
#endif
this->scan_result_.push_back(scan);
this->selected_sta_index_ = sta_index;
}
// Find which sta_[i] matches scan_result_[0] and set selected_sta_index_ (scan done path)
// Returns true if match found, false otherwise
bool sync_selected_sta_to_best_scan_result_(); bool sync_selected_sta_to_best_scan_result_();
#ifdef USE_WIFI_FAST_CONNECT #ifdef USE_WIFI_FAST_CONNECT
@@ -408,7 +394,7 @@ class WiFiComponent : public Component {
bool is_esp32_improv_active_(); bool is_esp32_improv_active_();
#ifdef USE_WIFI_FAST_CONNECT #ifdef USE_WIFI_FAST_CONNECT
bool load_fast_connect_settings_(); bool load_fast_connect_settings_(WiFiAP &params);
void save_fast_connect_settings_(); void save_fast_connect_settings_();
#endif #endif