8#if (ESP_IDF_VERSION_MAJOR >= 5 && ESP_IDF_VERSION_MINOR >= 1)
9#include <esp_eap_client.h>
19#include <user_interface.h>
35#ifdef USE_CAPTIVE_PORTAL
43#ifdef USE_IMPROV_SERIAL
49static const char *
const TAG =
"wifi";
68 char *heap_data =
new char[
len + 1];
69 std::memcpy(heap_data, str,
len);
70 heap_data[
len] =
'\0';
87 std::memcpy(this->storage_, other.storage_, INLINE_CAPACITY + 1);
90 other.storage_[0] =
'\0';
108 return this->
size() == other.
size() && std::memcmp(this->
data(), other.
data(), this->size()) == 0;
111 return this->
size() == other.
size() && std::memcmp(this->
data(), other.
c_str(), this->size()) == 0;
304static const LogString *retry_phase_to_log_string(
WiFiRetryPhase phase) {
306 return LOG_STR(
"INITIAL_CONNECT");
307#ifdef USE_WIFI_FAST_CONNECT
309 return LOG_STR(
"FAST_CONNECT_CYCLING");
312 return LOG_STR(
"EXPLICIT_HIDDEN");
314 return LOG_STR(
"SCAN_CONNECTING");
316 return LOG_STR(
"RETRY_HIDDEN");
318 return LOG_STR(
"RESTARTING");
319 return LOG_STR(
"UNKNOWN");
325 return !this->
sta_.empty() && this->
sta_[0].get_hidden();
331 for (
size_t i = 0; i < this->
sta_.size(); i++) {
332 if (!this->
sta_[i].get_hidden()) {
333 return static_cast<int8_t
>(i);
344static constexpr uint8_t WIFI_RETRY_COUNT_PER_BSSID = 2;
348static constexpr uint8_t WIFI_RETRY_COUNT_PER_SSID = 1;
352static constexpr uint8_t WIFI_RETRY_COUNT_PER_AP = 1;
356static constexpr uint32_t WIFI_COOLDOWN_DURATION_MS = 500;
361static constexpr uint32_t WIFI_COOLDOWN_WITH_AP_ACTIVE_MS = 30000;
366static constexpr uint32_t WIFI_SCAN_TIMEOUT_MS = 31000;
376static constexpr uint32_t WIFI_CONNECT_TIMEOUT_MS = 46000;
378static constexpr uint8_t get_max_retries_for_phase(
WiFiRetryPhase phase) {
381#ifdef USE_WIFI_FAST_CONNECT
385 return WIFI_RETRY_COUNT_PER_AP;
388 return WIFI_RETRY_COUNT_PER_SSID;
391 return WIFI_RETRY_COUNT_PER_BSSID;
394 return WIFI_RETRY_COUNT_PER_SSID;
396 return WIFI_RETRY_COUNT_PER_BSSID;
400static void apply_scan_result_to_params(WiFiAP ¶ms,
const WiFiScanResult &scan) {
401 params.set_hidden(
false);
402 params.set_ssid(scan.get_ssid());
403 params.set_bssid(scan.get_bssid());
404 params.set_channel(scan.get_channel());
419 for (
const auto &conf : this->
sta_) {
420 if (conf.ssid_ == ssid && conf.get_hidden()) {
427 if (scan.ssid_ == ssid) {
441#ifdef USE_CAPTIVE_PORTAL
448#ifdef USE_IMPROV_SERIAL
467 if (ssid[0] ==
'\0') {
470 for (
const auto &sta : this->
sta_) {
472 if (sta.get_hidden()) {
476 if (sta.ssid_.empty()) {
477 if (sta.has_bssid() && std::memcmp(sta.get_bssid().data(), bssid, 6) == 0) {
483 if (sta.ssid_ == ssid) {
491#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
497 char bssid_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
499 ESP_LOGV(TAG,
"- " LOG_SECRET(
"'%s'")
" " LOG_SECRET(
"(%s)")
" %ddB Ch:%u", ssid, bssid_s, rssi, channel);
523 for (
size_t i = start_index + 1; i < this->
sta_.size(); i++) {
524 const auto &sta = this->
sta_[i];
529 if (!include_explicit_hidden && sta.get_hidden()) {
531 if (first_non_hidden_idx < 0 ||
static_cast<int8_t
>(i) < first_non_hidden_idx) {
532 ESP_LOGD(TAG,
"Skipping " LOG_SECRET(
"'%s'")
" (explicit hidden, already tried)", sta.ssid_.c_str());
540 ESP_LOGD(TAG,
"Hidden candidate " LOG_SECRET(
"'%s'")
" at index %d", sta.ssid_.c_str(),
static_cast<int>(i));
541 return static_cast<int8_t
>(i);
543 ESP_LOGD(TAG,
"Skipping hidden retry for visible network " LOG_SECRET(
"'%s'"), sta.ssid_.c_str());
552 if (!this->
sta_.empty() && this->sta_[0].get_hidden()) {
553 ESP_LOGI(TAG,
"Starting with explicit hidden network (highest priority)");
563#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP) && ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
564static const char *eap_phase2_to_str(esp_eap_ttls_phase2_types
type) {
566 case ESP_EAP_TTLS_PHASE2_PAP:
568 case ESP_EAP_TTLS_PHASE2_CHAP:
570 case ESP_EAP_TTLS_PHASE2_MSCHAP:
572 case ESP_EAP_TTLS_PHASE2_MSCHAPV2:
574 case ESP_EAP_TTLS_PHASE2_EAP:
587#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
592 ESP_LOGE(TAG,
"Failed semaphore");
610 ESP_LOGCONFIG(TAG,
"Starting");
616#ifdef USE_WIFI_FAST_CONNECT
622 ESP_LOGD(TAG,
"Loaded settings: %s", save.ssid);
626 sta.set_password(save.password);
633 ESP_LOGV(TAG,
"Setting Output Power Option failed");
636#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
640 if (semaphore_count > 0) {
650 ESP_LOGV(TAG,
"Setting Power Save Option failed");
654#ifdef USE_WIFI_FAST_CONNECT
659 if (loaded_fast_connect) {
660 ESP_LOGI(TAG,
"Starting fast_connect (saved) " LOG_SECRET(
"'%s'"), params.
ssid_.
c_str());
662 }
else if (!this->
sta_.empty() && !this->sta_[0].get_hidden()) {
664 ESP_LOGI(TAG,
"Starting fast_connect (config) " LOG_SECRET(
"'%s'"), this->
sta_[0].ssid_.c_str());
677 }
else if (this->
has_ap()) {
680 ESP_LOGV(TAG,
"Setting Output Power Option failed");
682#ifdef USE_CAPTIVE_PORTAL
701 ESP_LOGW(TAG,
"Restarting adapter");
716#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
718#ifdef USE_WIFI_DISCONNECT_TRIGGER
723#ifdef USE_WIFI_CONNECT_TRIGGER
743 uint32_t cooldown_duration = portal_active ? WIFI_COOLDOWN_WITH_AP_ACTIVE_MS : WIFI_COOLDOWN_DURATION_MS;
766 ESP_LOGW(TAG,
"Connection lost; reconnecting");
798 ESP_LOGI(TAG,
"Starting fallback AP");
800#ifdef USE_CAPTIVE_PORTAL
825 ESP_LOGE(TAG,
"Can't connect; rebooting");
831#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
839 ESP_LOGV(TAG,
"Switching to high-performance mode (%" PRIu32
" active %s)", (uint32_t) semaphore_count,
840 semaphore_count == 1 ?
"request" :
"requests");
847 ESP_LOGV(TAG,
"Restoring power save mode to configured setting");
862#ifdef USE_WIFI_11KV_SUPPORT
897 static constexpr size_t AP_SSID_MAX_LEN = 32;
898 static constexpr size_t AP_SSID_PREFIX_LEN = 25;
899 static constexpr size_t AP_SSID_SUFFIX_LEN = 7;
902 const char *name_ptr = app_name.c_str();
903 size_t name_len = app_name.length();
905 if (name_len <= AP_SSID_MAX_LEN) {
910 char ssid_buf[AP_SSID_MAX_LEN + 1];
913 memcpy(ssid_buf, name_ptr, AP_SSID_PREFIX_LEN);
914 memcpy(ssid_buf + AP_SSID_PREFIX_LEN, name_ptr + name_len - AP_SSID_SUFFIX_LEN, AP_SSID_SUFFIX_LEN);
916 memcpy(ssid_buf, name_ptr, AP_SSID_MAX_LEN);
918 ssid_buf[AP_SSID_MAX_LEN] =
'\0';
924 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
928 " AP Password: '%s'\n"
930 this->
ap_.
ssid_.
c_str(), this->ap_.password_.c_str(), this->wifi_soft_ap_ip().str_to(ip_buf));
932#ifdef USE_WIFI_MANUAL_IP
934 if (manual_ip.has_value()) {
935 char static_ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
936 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
937 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
939 " AP Static IP: '%s'\n"
940 " AP Gateway: '%s'\n"
942 manual_ip->static_ip.str_to(static_ip_buf), manual_ip->gateway.str_to(gateway_buf),
943 manual_ip->subnet.str_to(subnet_buf));
981 if (config ==
nullptr) {
982 ESP_LOGE(TAG,
"No valid network config (selected_sta_index_=%d, sta_.size()=%zu)",
992#ifdef USE_WIFI_FAST_CONNECT
1010 apply_scan_result_to_params(params, this->
scan_result_[0]);
1024 return config ? *config :
WiFiAP{};
1031 strncpy(save.ssid, ssid,
sizeof(save.ssid) - 1);
1032 strncpy(save.password, password,
sizeof(save.password) - 1);
1039 sta.set_password(password);
1049 ESP_LOGD(TAG,
"Exiting cooldown early due to new WiFi credentials");
1065 "Connecting to " LOG_SECRET(
"'%s'")
" " LOG_SECRET(
"(%s)")
" (priority %d, attempt %u/%u in phase %s)...",
1069#ifdef ESPHOME_LOG_HAS_VERBOSE
1071 "Connection Params:\n"
1075 ESP_LOGV(TAG,
" BSSID: %s", bssid_s);
1077 ESP_LOGV(TAG,
" BSSID: Not Set");
1080#ifdef USE_WIFI_WPA2_EAP
1081 if (ap.
get_eap().has_value()) {
1086 " WPA2 Enterprise authentication configured:\n"
1087 " Identity: " LOG_SECRET(
"'%s'")
"\n"
1088 " Username: " LOG_SECRET(
"'%s'")
"\n"
1089 " Password: " LOG_SECRET(
"'%s'"),
1092#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP) && ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
1093 ESP_LOGV(TAG,
" TTLS Phase 2: " LOG_SECRET(
"'%s'"), eap_phase2_to_str(eap_config.
ttls_phase_2));
1095 bool ca_cert_present = eap_config.
ca_cert !=
nullptr && strlen(eap_config.
ca_cert);
1100 " Client Cert: %s\n"
1102 ca_cert_present ?
"present" :
"not present", client_cert_present ?
"present" :
"not present",
1103 client_key_present ?
"present" :
"not present");
1106 ESP_LOGV(TAG,
" Password: " LOG_SECRET(
"'%s'"), ap.
password_.
c_str());
1107#ifdef USE_WIFI_WPA2_EAP
1113 ESP_LOGV(TAG,
" Channel not set");
1115#ifdef USE_WIFI_MANUAL_IP
1118 char static_ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
1119 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
1120 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
1121 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
1122 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
1123 ESP_LOGV(TAG,
" Manual IP: Static IP=%s Gateway=%s Subnet=%s DNS1=%s DNS2=%s",
m.static_ip.str_to(static_ip_buf),
1124 m.gateway.str_to(gateway_buf),
m.subnet.str_to(subnet_buf),
m.dns1.str_to(dns1_buf),
1125 m.dns2.str_to(dns2_buf));
1129 ESP_LOGV(TAG,
" Using DHCP IP");
1131 ESP_LOGV(TAG,
" Hidden: %s", YESNO(ap.
get_hidden()));
1141 ESP_LOGE(TAG,
"wifi_sta_connect_ failed");
1161 return LOG_STR(
"\033[0;32m"
1167 }
else if (rssi >= -65) {
1168 return LOG_STR(
"\033[0;33m"
1175 }
else if (rssi >= -85) {
1176 return LOG_STR(
"\033[0;33m"
1184 return LOG_STR(
"\033[0;31m"
1196 char bssid_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
1199 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
1202 ESP_LOGCONFIG(TAG,
" IP Address: %s", ip.str_to(ip_buf));
1207 char ssid_buf[SSID_BUFFER_SIZE];
1208 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
1209 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
1210 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
1211 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
1214 " SSID: " LOG_SECRET(
"'%s'")
"\n"
1215 " BSSID: " LOG_SECRET(
"%s")
"\n"
1217 " Signal strength: %d dB %s\n"
1218 " Channel: %" PRId32
"\n"
1227#ifdef ESPHOME_LOG_HAS_VERBOSE
1229 ESP_LOGV(TAG,
" Priority: %d", this->
get_sta_priority(config->get_bssid()));
1232#ifdef USE_WIFI_11KV_SUPPORT
1236 this->
btm_ ?
"enabled" :
"disabled", this->
rrm_ ?
"enabled" :
"disabled");
1244 ESP_LOGD(TAG,
"Enabling");
1253 ESP_LOGD(TAG,
"Disabling");
1263 ESP_LOGD(TAG,
"Starting scan");
1322template<
typename VectorType>
static void insertion_sort_scan_results(VectorType &results) {
1323 const size_t size = results.size();
1324 for (
size_t i = 1; i <
size; i++) {
1326 WiFiScanResult key = results[i];
1331 while (j >= 0 && wifi_scan_result_is_better(key, results[j])) {
1332 results[j + 1] = results[j];
1335 results[j + 1] = key;
1347 char bssid_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
1351#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
1353 ESP_LOGI(TAG,
"- '%s' %s" LOG_SECRET(
"(%s) ")
"%s Ch:%2u %3ddB P:%d", res.
get_ssid().
c_str(),
1354 res.
get_is_hidden() ? LOG_STR_LITERAL(
"(HIDDEN) ") : LOG_STR_LITERAL(
""), bssid_s,
1357 ESP_LOGI(TAG,
"- '%s' %s" LOG_SECRET(
"(%s) ")
"%s", res.
get_ssid().
c_str(),
1358 res.
get_is_hidden() ? LOG_STR_LITERAL(
"(HIDDEN) ") : LOG_STR_LITERAL(
""), bssid_s,
1366 ESP_LOGE(TAG,
"Scan timeout");
1377 ESP_LOGW(TAG,
"No networks found");
1382 ESP_LOGD(TAG,
"Found networks:");
1384 for (
auto &ap : this->
sta_) {
1385 if (res.matches(ap)) {
1386 res.set_matches(
true);
1388 const bssid_t &bssid = res.get_bssid();
1399 insertion_sort_scan_results(this->scan_result_);
1402 for (
auto &res : this->scan_result_) {
1403 if (res.get_matches()) {
1404 log_scan_result(res);
1413 bool found_match =
false;
1415 for (
size_t i = 0; i < this->
sta_.size(); i++) {
1416 if (scan_res.
matches(this->sta_[i])) {
1427 ESP_LOGW(TAG,
"No matching network found");
1449 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
1456 ESP_LOGCONFIG(TAG,
" Disabled");
1468 char ssid_buf[SSID_BUFFER_SIZE];
1470 ESP_LOGW(TAG,
"Connection incomplete");
1475 ESP_LOGI(TAG,
"Connected");
1481 ESP_LOGW(TAG, LOG_SECRET(
"'%s'")
" should be marked hidden", config->ssid_.c_str());
1487#ifdef USE_CAPTIVE_PORTAL
1492 ESP_LOGD(TAG,
"Disabling AP");
1511 ESP_LOGD(TAG,
"Roam successful");
1526#ifdef USE_WIFI_FAST_CONNECT
1532#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
1538#if defined(USE_ESP8266) && defined(USE_WIFI_IP_STATE_LISTENERS) && defined(USE_WIFI_MANUAL_IP)
1550 ESP_LOGW(TAG,
"Connection timeout, aborting connection attempt");
1560 ESP_LOGW(TAG,
"Connecting to network failed (callback)");
1571 ESP_LOGW(TAG,
"Network no longer found");
1577 ESP_LOGW(TAG,
"Connecting to network failed");
1582 ESP_LOGW(TAG,
"Unknown connection status %d", (
int)
status);
1595#ifdef USE_WIFI_FAST_CONNECT
1604 if (!this->
sta_.empty() && this->sta_[0].get_hidden()) {
1612 if (this->
num_retried_ + 1 < WIFI_RETRY_COUNT_PER_SSID) {
1619 if (next_index < this->
sta_.size() && this->sta_[next_index].get_hidden()) {
1635 if (this->
scan_result_.empty() || !this->scan_result_[0].get_matches()) {
1639 if (this->
num_retried_ + 1 < WIFI_RETRY_COUNT_PER_BSSID) {
1658 if (this->
num_retried_ + 1 < WIFI_RETRY_COUNT_PER_SSID) {
1735 if (old_phase == new_phase) {
1739 ESP_LOGD(TAG,
"Retry phase: %s → %s", LOG_STR_ARG(retry_phase_to_log_string(old_phase)),
1740 LOG_STR_ARG(retry_phase_to_log_string(new_phase)));
1746 switch (new_phase) {
1747#ifdef USE_WIFI_FAST_CONNECT
1762#ifdef USE_WIFI_FAST_CONNECT
1764 ESP_LOGI(TAG,
"Fast connect exhausted, falling back to scan");
1792 ESP_LOGD(TAG,
"All SSIDs visible or already tried, skipping hidden mode");
1845 if (first_priority != std::numeric_limits<int8_t>::min()) {
1850 if (pri.priority != first_priority) {
1856 ESP_LOGD(TAG,
"Clearing BSSID priorities (all at minimum)");
1887 failed_bssid = config->get_bssid();
1895 const char *ssid =
nullptr;
1899 ssid = config->ssid_.c_str();
1904 uint8_t max_retries = get_max_retries_for_phase(this->
retry_phase_);
1905 bool is_last_attempt = (this->
num_retried_ + 1 >= max_retries);
1909 int8_t new_priority = old_priority;
1911 if (is_last_attempt) {
1914 (old_priority > std::numeric_limits<int8_t>::min()) ? (old_priority - 1) : std::numeric_limits<int8_t>::min();
1919 ESP_LOGD(TAG,
"Failed " LOG_SECRET(
"'%s'")
" " LOG_SECRET(
"(%s)")
", priority %d → %d", ssid !=
nullptr ? ssid :
"",
1920 bssid_s, old_priority, new_priority);
1941#ifdef USE_WIFI_FAST_CONNECT
1946 ESP_LOGD(TAG,
"Next AP in %s", LOG_STR_ARG(retry_phase_to_log_string(this->
retry_phase_)));
1955 if (next_index < this->
sta_.size() && this->sta_[next_index].get_hidden()) {
1958 ESP_LOGD(TAG,
"Next explicit hidden network at index %d",
static_cast<int>(next_index));
1970 if (next_index != -1) {
1990 ESP_LOGD(TAG,
"Retry attempt %u/%u in phase %s", this->
num_retried_ + 1,
2022 if (next_phase == current_phase) {
2057#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
2065#ifdef USE_CAPTIVE_PORTAL
2079#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
2111#ifdef USE_WIFI_FAST_CONNECT
2117 if (fast_connect_save.ap_index < 0 ||
static_cast<size_t>(fast_connect_save.ap_index) >= this->sta_.size()) {
2118 ESP_LOGW(TAG,
"AP index out of bounds");
2126 params = this->
sta_[fast_connect_save.ap_index];
2130 std::copy(fast_connect_save.bssid, fast_connect_save.bssid + 6, bssid.begin());
2136 ESP_LOGD(TAG,
"Loaded fast_connect settings");
2152 if (this->
fast_connect_pref_.
load(&previous_save) && memcmp(previous_save.bssid, bssid.data(), 6) == 0 &&
2153 previous_save.channel == channel && previous_save.ap_index == ap_index) {
2158 memcpy(fast_connect_save.bssid, bssid.data(), 6);
2159 fast_connect_save.channel = channel;
2160 fast_connect_save.ap_index = ap_index;
2164 ESP_LOGD(TAG,
"Saved fast_connect settings");
2176#ifdef USE_WIFI_WPA2_EAP
2181#ifdef USE_WIFI_MANUAL_IP
2187#ifdef USE_WIFI_WPA2_EAP
2192#ifdef USE_WIFI_MANUAL_IP
2198 bool with_auth,
bool is_hidden)
2202 ssid_(ssid, ssid_len),
2203 with_auth_(with_auth),
2204 is_hidden_(is_hidden) {}
2222#ifdef USE_WIFI_WPA2_EAP
2260#if defined(USE_RP2040) || defined(USE_ESP32)
2270#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
2274 this->
pending_.connect_state =
false;
2276 char ssid_buf[SSID_BUFFER_SIZE];
2280 listener->on_wifi_connect_state(
StringRef(ssid, strlen(ssid)), bssid);
2285 constexpr uint8_t empty_bssid[6] = {};
2287 listener->on_wifi_connect_state(
StringRef(), empty_bssid);
2292#ifdef USE_WIFI_IP_STATE_LISTENERS
2300#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
2311 if (selected ==
nullptr || selected->
get_hidden()) {
2322 ESP_LOGV(TAG,
"Roam check skipped, signal good (%d dBm, attempt %u/%u)", rssi, this->
roaming_attempts_,
2338 int8_t current_rssi = this->
wifi_rssi();
2340 if (current_rssi == WIFI_RSSI_DISCONNECTED) {
2345 char ssid_buf[SSID_BUFFER_SIZE];
2351 char bssid_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
2355 if (result.ssid_ != current_ssid || result.get_bssid() == current_bssid)
2358#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
2360 ESP_LOGV(TAG,
"Roam candidate %s %d dBm", bssid_buf, result.get_rssi());
2371 int8_t improvement = (best ==
nullptr) ? 0 : best->
get_rssi() - current_rssi;
2380 ESP_LOGI(TAG,
"Roaming to %s (%+d dB)", bssid_buf, improvement);
2382 WiFiAP roam_params = *selected;
2383 apply_scan_result_to_params(roam_params, *best);
bool is_name_add_mac_suffix_enabled() const
const std::string & get_name() const
Get the name of this Application set by pre_setup().
constexpr uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
virtual bool sync()=0
Commit pending writes to flash.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
constexpr size_type size() const
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
value_type const & value() const
20-byte string: 18 chars inline + null, heap for longer.
const char * data() const
CompactString & operator=(const CompactString &other)
bool operator==(const CompactString &other) const
static constexpr uint8_t INLINE_CAPACITY
const char * c_str() const
char storage_[INLINE_CAPACITY+1]
static constexpr uint8_t MAX_LENGTH
void set_heap_ptr_(char *ptr)
char * get_heap_ptr_() const
uint8_t get_channel() const
void set_ssid(const std::string &ssid)
const optional< EAPAuth > & get_eap() const
void set_bssid(const bssid_t &bssid)
void set_channel(uint8_t channel)
optional< ManualIP > manual_ip_
void set_eap(optional< EAPAuth > eap_auth)
void set_password(const std::string &password)
void set_manual_ip(optional< ManualIP > manual_ip)
const optional< ManualIP > & get_manual_ip() const
void set_hidden(bool hidden)
const bssid_t & get_bssid() const
This component is responsible for managing the ESP WiFi interface.
void notify_scan_results_listeners_()
Notify scan results listeners with current scan results.
bool is_captive_portal_active_()
bool error_from_callback_
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP ¶ms)
bool wifi_apply_power_save_()
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made.
bool request_high_performance()
Request high-performance mode (no power saving) for improved WiFi latency.
ESPPreferenceObject pref_
void set_sta(const WiFiAP &ap)
WiFiPowerSaveMode power_save_
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
WiFiSTAConnectStatus wifi_sta_connect_status_() const
uint32_t roaming_last_check_
int8_t get_sta_priority(const bssid_t bssid)
void log_and_adjust_priority_for_failed_connect_()
Log failed connection and decrease BSSID priority to avoid repeated attempts.
void init_sta(size_t count)
void save_wifi_sta(const std::string &ssid, const std::string &password)
void notify_connect_state_listeners_()
Notify connect state listeners (called after state machine reaches STA_CONNECTED)
struct esphome::wifi::WiFiComponent::@175 pending_
WiFiComponentState state_
wifi_scan_vector_t< WiFiScanResult > scan_result_
bool skip_cooldown_next_cycle_
WiFiPowerSaveMode configured_power_save_
void set_sta_priority(const bssid_t bssid, int8_t priority)
StaticVector< WiFiScanResultsListener *, ESPHOME_WIFI_SCAN_RESULTS_LISTENERS > scan_results_listeners_
void print_connect_params_()
void loop() override
Reconnect WiFi if required.
void notify_ip_state_listeners_()
Notify IP state listeners with current addresses.
void start_connecting(const WiFiAP &ap)
void advance_to_next_target_or_increment_retry_()
Advance to next target (AP/SSID) within current phase, or increment retry counter Called when staying...
void check_roaming_(uint32_t now)
static constexpr uint32_t ROAMING_CHECK_INTERVAL
RoamingState roaming_state_
void check_scanning_finished()
void process_roaming_scan_()
SemaphoreHandle_t high_performance_semaphore_
network::IPAddress get_dns_address(int num)
WiFiComponent()
Construct a WiFiComponent.
std::vector< WiFiSTAPriority > sta_priorities_
int8_t selected_sta_index_
static constexpr int8_t ROAMING_GOOD_RSSI
void save_fast_connect_settings_()
void notify_disconnect_state_listeners_()
Notify connect state listeners of disconnection.
bool wifi_start_ap_(const WiFiAP &ap)
StaticVector< WiFiConnectStateListener *, ESPHOME_WIFI_CONNECT_STATE_LISTENERS > connect_state_listeners_
void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel)
Log a discarded scan result at VERBOSE level (skipped during roaming scans to avoid log overflow)
int32_t get_wifi_channel()
const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
network::IPAddress wifi_subnet_mask_()
static constexpr uint8_t ROAMING_MAX_ATTEMPTS
network::IPAddress wifi_soft_ap_ip()
void set_passive_scan(bool passive)
uint8_t roaming_attempts_
network::IPAddress wifi_gateway_ip_()
void set_power_save_mode(WiFiPowerSaveMode power_save)
bool is_ap_active() const
int8_t find_next_hidden_sta_(int8_t start_index)
Find next SSID that wasn't in scan results (might be hidden) Returns index of next potentially hidden...
ESPPreferenceObject fast_connect_pref_
void clear_priorities_if_all_min_()
Clear BSSID priority tracking if all priorities are at minimum (saves memory)
WiFiRetryPhase determine_next_phase_()
Determine next retry phase based on current state and failure conditions.
network::IPAddress wifi_dns_ip_(int num)
float get_loop_priority() const override
bool wifi_apply_hostname_()
bool wifi_sta_pre_setup_()
network::IPAddresses get_ip_addresses()
static constexpr int8_t ROAMING_MIN_IMPROVEMENT
bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const
Check if network matches any configured network (for scan result filtering) Matches by SSID when conf...
float get_setup_priority() const override
WIFI setup_priority.
bool is_esp32_improv_active_()
StaticVector< WiFiIPStateListener *, ESPHOME_WIFI_IP_STATE_LISTENERS > ip_state_listeners_
FixedVector< WiFiAP > sta_
int8_t find_first_non_hidden_index_() const
Find the index of the first non-hidden network Returns where EXPLICIT_HIDDEN phase would have stopped...
void release_scan_results_()
Free scan results memory unless a component needs them.
bool needs_scan_results_() const
Check if we need valid scan results for the current phase but don't have any Returns true if the phas...
WiFiRetryPhase retry_phase_
bool transition_to_phase_(WiFiRetryPhase new_phase)
Transition to a new retry phase with logging Returns true if a scan was started (caller should wait),...
bool needs_full_scan_results_() const
Check if full scan results are needed (captive portal active, improv, listeners)
WiFiAP build_params_for_current_phase_()
bool is_high_performance_mode_
void dump_config() override
void clear_roaming_state_()
bool release_high_performance()
Release a high-performance mode request.
RetryHiddenMode retry_hidden_mode_
bool wifi_apply_output_power_(float output_power)
bool has_completed_scan_after_captive_portal_start_
const char * get_use_address() const
bool is_connected() const
bool wifi_sta_connect_(const WiFiAP &ap)
bool went_through_explicit_hidden_phase_() const
Check if we went through EXPLICIT_HIDDEN phase (first network is marked hidden) Used in RETRY_HIDDEN ...
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
void set_reboot_timeout(uint32_t reboot_timeout)
bool can_proceed() override
network::IPAddresses wifi_sta_ip_addresses()
void check_connecting_finished(uint32_t now)
void start_initial_connection_()
Start initial connection - either scan or connect directly to hidden networks.
bool wifi_scan_start_(bool passive)
bool ssid_was_seen_in_scan_(const CompactString &ssid) const
Check if an SSID was seen in the most recent scan results Used to skip hidden mode for SSIDs we know ...
bool handled_connected_state_
void setup() override
Setup WiFi interface.
void clear_all_bssid_priorities_()
Clear all BSSID priority penalties after successful connection (stale after disconnect)
void set_use_address(const char *use_address)
bool post_connect_roaming_
Trigger disconnect_trigger_
int8_t get_priority() const
uint8_t get_channel() const
bool get_with_auth() const
WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
const bssid_t & get_bssid() const
StringRef get_ssid() const
bool matches(const WiFiAP &config) const
void set_matches(bool matches)
bool get_is_hidden() const
bool operator==(const WiFiScanResult &rhs) const
struct @65::@66 __attribute__
CaptivePortal * global_captive_portal
ESP32ImprovComponent * global_improv_component
ImprovSerialComponent * global_improv_serial_component
std::array< IPAddress, 5 > IPAddresses
std::array< uint8_t, 6 > bssid_t
const LogString * get_signal_bars(int8_t rssi)
@ BLIND_RETRY
Blind retry mode: scanning disabled (captive portal/improv active), try ALL configured networks seque...
@ SCAN_BASED
Normal mode: scan completed, only try networks NOT visible in scan results (truly hidden networks tha...
@ ERROR_NETWORK_NOT_FOUND
WiFiRetryPhase
Tracks the current retry strategy/phase for WiFi connection attempts.
@ RETRY_HIDDEN
Retry networks not found in scan (might be hidden)
@ RESTARTING_ADAPTER
Restarting WiFi adapter to clear stuck state.
@ INITIAL_CONNECT
Initial connection attempt (varies based on fast_connect setting)
@ EXPLICIT_HIDDEN
Explicitly hidden networks (user marked as hidden, try before scanning)
@ FAST_CONNECT_CYCLING_APS
Fast connect mode: cycling through configured APs (config-only, no scan)
@ SCAN_CONNECTING
Scan-based: connecting to best AP from scan results.
WiFiComponent * global_wifi_component
@ SCANNING
Scanning for better AP.
@ CONNECTING
Attempting to connect to better AP found in scan.
@ IDLE
Not roaming, waiting for next check interval.
@ RECONNECTING
Roam connection failed, reconnecting to any available AP.
@ WIFI_COMPONENT_STATE_DISABLED
WiFi is disabled.
@ WIFI_COMPONENT_STATE_AP
WiFi is in AP-only mode and internal AP is already enabled.
@ WIFI_COMPONENT_STATE_STA_CONNECTING
WiFi is in STA(+AP) mode and currently connecting to an AP.
@ WIFI_COMPONENT_STATE_OFF
Nothing has been initialized yet.
@ WIFI_COMPONENT_STATE_STA_SCANNING
WiFi is in STA-only mode and currently scanning for APs.
@ WIFI_COMPONENT_STATE_COOLDOWN
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
@ WIFI_COMPONENT_STATE_STA_CONNECTED
WiFi is in STA(+AP) mode and successfully connected.
ESPPreferences * global_preferences
void IRAM_ATTR HOT yield()
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
esp_eap_ttls_phase2_types ttls_phase_2
Struct for setting static IPs in WiFiComponent.