ESPHome 2026.2.3
Loading...
Searching...
No Matches
wifi_component.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_WIFI
10
11#include <span>
12#include <string>
13#include <vector>
14
15#ifdef USE_LIBRETINY
16#include <WiFi.h>
17#endif
18
19#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
20#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
21#include <esp_eap_client.h>
22#else
23#include <esp_wpa2.h>
24#endif
25#endif
26
27#ifdef USE_ESP8266
28#include <ESP8266WiFi.h>
29#include <ESP8266WiFiType.h>
30
31#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
32extern "C" {
33#include <user_interface.h>
34};
35#endif
36#endif
37
38#ifdef USE_RP2040
39extern "C" {
40#include "cyw43.h"
41#include "cyw43_country.h"
42#include "pico/cyw43_arch.h"
43}
44
45#include <WiFi.h>
46#endif
47
48#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
49#include <freertos/FreeRTOS.h>
50#include <freertos/semphr.h>
51#endif
52
53namespace esphome::wifi {
54
56static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
57
59static constexpr size_t SSID_BUFFER_SIZE = 33;
60
62 char ssid[33];
63 char password[65];
64} PACKED; // NOLINT
65
67 uint8_t bssid[6];
68 uint8_t channel;
69 int8_t ap_index;
70} PACKED; // NOLINT
71
88
96
98enum class WiFiRetryPhase : uint8_t {
101#ifdef USE_WIFI_FAST_CONNECT
104#endif
113};
114
116enum class RoamingState : uint8_t {
118 IDLE,
120 SCANNING,
125};
126
128enum class RetryHiddenMode : uint8_t {
135};
136
145
146#ifdef USE_WIFI_WPA2_EAP
147struct EAPAuth {
148 std::string identity; // required for all auth types
149 std::string username;
150 std::string password;
151 const char *ca_cert; // optionally verify authentication server
152 // used for EAP-TLS
153 const char *client_cert;
154 const char *client_key;
155// used for EAP-TTLS
156#ifdef USE_ESP32
157 esp_eap_ttls_phase2_types ttls_phase_2;
158#endif
159};
160#endif // USE_WIFI_WPA2_EAP
161
162using bssid_t = std::array<uint8_t, 6>;
163
165static constexpr size_t WIFI_SCAN_RESULT_FILTERED_RESERVE = 8;
166
167// Use std::vector for RP2040 (callback-based) and ESP32 (destructive scan API)
168// Use FixedVector for ESP8266 and LibreTiny where two-pass exact allocation is possible
169#if defined(USE_RP2040) || defined(USE_ESP32)
170template<typename T> using wifi_scan_vector_t = std::vector<T>;
171#else
172template<typename T> using wifi_scan_vector_t = FixedVector<T>;
173#endif
174
178 public:
179 static constexpr uint8_t MAX_LENGTH = 127;
180 static constexpr uint8_t INLINE_CAPACITY = 18; // 18 chars + null terminator fits in 19 bytes
181
182 CompactString() : length_(0), is_heap_(0) { this->storage_[0] = '\0'; }
183 CompactString(const char *str, size_t len);
184 CompactString(const CompactString &other);
185 CompactString(CompactString &&other) noexcept;
187 CompactString &operator=(CompactString &&other) noexcept;
189
190 const char *data() const { return this->is_heap_ ? this->get_heap_ptr_() : this->storage_; }
191 const char *c_str() const { return this->data(); } // Always null-terminated
192 size_t size() const { return this->length_; }
193 bool empty() const { return this->length_ == 0; }
194
196 StringRef ref() const { return StringRef(this->data(), this->size()); }
197
198 bool operator==(const CompactString &other) const;
199 bool operator!=(const CompactString &other) const { return !(*this == other); }
200 bool operator==(const StringRef &other) const;
201 bool operator!=(const StringRef &other) const { return !(*this == other); }
202 bool operator==(const char *other) const { return *this == StringRef(other); }
203 bool operator!=(const char *other) const { return !(*this == other); }
204
205 protected:
206 char *get_heap_ptr_() const {
207 char *ptr;
208 std::memcpy(&ptr, this->storage_, sizeof(ptr));
209 return ptr;
210 }
211 void set_heap_ptr_(char *ptr) { std::memcpy(this->storage_, &ptr, sizeof(ptr)); }
212
213 // Storage for string data. When is_heap_=0, contains the string directly (null-terminated).
214 // When is_heap_=1, first sizeof(char*) bytes contain pointer to heap allocation.
215 char storage_[INLINE_CAPACITY + 1]; // 19 bytes: 18 chars + null terminator
216 uint8_t length_ : 7; // String length (0-127)
217 uint8_t is_heap_ : 1; // 1 if using heap pointer, 0 if using inline storage
218 // Total size: 20 bytes (19 bytes storage + 1 byte bitfields)
219};
220
221static_assert(sizeof(CompactString) == 20, "CompactString must be exactly 20 bytes");
222
223class WiFiAP {
224 friend class WiFiComponent;
225 friend class WiFiScanResult;
226
227 public:
228 void set_ssid(const std::string &ssid);
229 void set_ssid(const char *ssid);
230 void set_ssid(StringRef ssid) { this->ssid_ = CompactString(ssid.c_str(), ssid.size()); }
231 void set_bssid(const bssid_t &bssid);
232 void clear_bssid();
233 void set_password(const std::string &password);
234 void set_password(const char *password);
235 void set_password(StringRef password) { this->password_ = CompactString(password.c_str(), password.size()); }
236#ifdef USE_WIFI_WPA2_EAP
237 void set_eap(optional<EAPAuth> eap_auth);
238#endif // USE_WIFI_WPA2_EAP
239 void set_channel(uint8_t channel);
240 void clear_channel();
242#ifdef USE_WIFI_MANUAL_IP
243 void set_manual_ip(optional<ManualIP> manual_ip);
244#endif
245 void set_hidden(bool hidden);
246 StringRef get_ssid() const { return this->ssid_.ref(); }
247 StringRef get_password() const { return this->password_.ref(); }
248 const bssid_t &get_bssid() const;
249 bool has_bssid() const;
250#ifdef USE_WIFI_WPA2_EAP
251 const optional<EAPAuth> &get_eap() const;
252#endif // USE_WIFI_WPA2_EAP
253 uint8_t get_channel() const;
254 bool has_channel() const;
255 int8_t get_priority() const { return priority_; }
256#ifdef USE_WIFI_MANUAL_IP
257 const optional<ManualIP> &get_manual_ip() const;
258#endif
259 bool get_hidden() const;
260
261 protected:
264#ifdef USE_WIFI_WPA2_EAP
266#endif // USE_WIFI_WPA2_EAP
267#ifdef USE_WIFI_MANUAL_IP
269#endif
270 // Group small types together to minimize padding
271 bssid_t bssid_{}; // 6 bytes, all zeros = any/not set
272 uint8_t channel_{0}; // 1 byte, 0 = auto/not set
273 int8_t priority_{0}; // 1 byte
274 bool hidden_{false}; // 1 byte (+ 3 bytes end padding to 4-byte align)
275};
276
278 friend class WiFiComponent;
279
280 public:
281 WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth,
282 bool is_hidden);
283
284 bool matches(const WiFiAP &config) const;
285
286 bool get_matches() const;
287 void set_matches(bool matches);
288 const bssid_t &get_bssid() const;
289 StringRef get_ssid() const { return this->ssid_.ref(); }
290 uint8_t get_channel() const;
291 int8_t get_rssi() const;
292 bool get_with_auth() const;
293 bool get_is_hidden() const;
294 int8_t get_priority() const { return priority_; }
296
297 bool operator==(const WiFiScanResult &rhs) const;
298
299 protected:
301 uint8_t channel_;
302 int8_t rssi_;
304 int8_t priority_{0};
305 bool matches_{false};
308};
309
314
320
326
327#ifdef USE_ESP32
328struct IDFWiFiEvent;
329#endif
330
331#ifdef USE_LIBRETINY
332struct LTWiFiEvent;
333#endif
334
344 public:
345 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
346 const network::IPAddress &dns2) = 0;
347};
348
358 public:
360};
361
371 public:
372 virtual void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) = 0;
373};
374
384 public:
386};
387
389class WiFiComponent : public Component {
390 public:
393
394 void set_sta(const WiFiAP &ap);
395 // Returns a copy of the currently selected AP configuration
396 WiFiAP get_sta() const;
397 void init_sta(size_t count);
398 void add_sta(const WiFiAP &ap);
399 void clear_sta();
400
401#ifdef USE_WIFI_AP
409 void set_ap(const WiFiAP &ap);
410 WiFiAP get_ap() { return this->ap_; }
411 void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
412#endif // USE_WIFI_AP
413
414 void enable();
415 void disable();
416 bool is_disabled();
417 void start_scanning();
419 void start_connecting(const WiFiAP &ap);
420 // Backward compatibility overload - ignores 'two' parameter
421 void start_connecting(const WiFiAP &ap, bool /* two */) { this->start_connecting(ap); }
422
423 void check_connecting_finished(uint32_t now);
424
425 void retry_connect();
426
427#ifdef USE_RP2040
428 bool can_proceed() override;
429#endif
430
431 void set_reboot_timeout(uint32_t reboot_timeout);
432
433 bool is_connected() const;
434
435 void set_power_save_mode(WiFiPowerSaveMode power_save);
436 void set_min_auth_mode(WifiMinAuthMode min_auth_mode) { min_auth_mode_ = min_auth_mode; }
437 void set_output_power(float output_power) { output_power_ = output_power; }
438
439 void set_passive_scan(bool passive);
440
441 void save_wifi_sta(const std::string &ssid, const std::string &password);
442 void save_wifi_sta(const char *ssid, const char *password);
443 void save_wifi_sta(StringRef ssid, StringRef password) { this->save_wifi_sta(ssid.c_str(), password.c_str()); }
444
445 // ========== INTERNAL METHODS ==========
446 // (In most use cases you won't need these)
448 void setup() override;
449 void start();
450 void dump_config() override;
451 void restart_adapter();
453 float get_setup_priority() const override;
454 float get_loop_priority() const override;
455
457 void loop() override;
458
459 bool has_sta() const;
460 bool has_ap() const;
461 bool is_ap_active() const;
462
463#ifdef USE_WIFI_11KV_SUPPORT
464 void set_btm(bool btm);
465 void set_rrm(bool rrm);
466#endif
467
470 const char *get_use_address() const;
471 void set_use_address(const char *use_address);
472
474
476
477 bool has_sta_priority(const bssid_t &bssid) {
478 for (auto &it : this->sta_priorities_) {
479 if (it.bssid == bssid)
480 return true;
481 }
482 return false;
483 }
484 int8_t get_sta_priority(const bssid_t bssid) {
485 for (auto &it : this->sta_priorities_) {
486 if (it.bssid == bssid)
487 return it.priority;
488 }
489 return 0;
490 }
491 void set_sta_priority(const bssid_t bssid, int8_t priority) {
492 for (auto &it : this->sta_priorities_) {
493 if (it.bssid == bssid) {
494 it.priority = priority;
495 return;
496 }
497 }
498 this->sta_priorities_.push_back(WiFiSTAPriority{
499 .bssid = bssid,
500 .priority = priority,
501 });
502 }
503
505 std::string wifi_ssid();
508 const char *wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer);
510
511 int8_t wifi_rssi();
512
513 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
514 void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
515 void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
516
517#ifdef USE_WIFI_CONNECT_TRIGGER
519#endif
520#ifdef USE_WIFI_DISCONNECT_TRIGGER
522#endif
523
524 int32_t get_wifi_channel();
525
526#ifdef USE_WIFI_IP_STATE_LISTENERS
530 void add_ip_state_listener(WiFiIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
531#endif // USE_WIFI_IP_STATE_LISTENERS
532#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
535 this->scan_results_listeners_.push_back(listener);
536 }
537#endif // USE_WIFI_SCAN_RESULTS_LISTENERS
538#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
543 this->connect_state_listeners_.push_back(listener);
544 }
545#endif // USE_WIFI_CONNECT_STATE_LISTENERS
546#ifdef USE_WIFI_POWER_SAVE_LISTENERS
550 void add_power_save_listener(WiFiPowerSaveListener *listener) { this->power_save_listeners_.push_back(listener); }
551#endif // USE_WIFI_POWER_SAVE_LISTENERS
552
553#ifdef USE_WIFI_RUNTIME_POWER_SAVE
569
582#endif // USE_WIFI_RUNTIME_POWER_SAVE
583
584 protected:
585#ifdef USE_WIFI_AP
586 void setup_ap_config_();
587#endif // USE_WIFI_AP
588
591
596 bool transition_to_phase_(WiFiRetryPhase new_phase);
599 bool needs_scan_results_() const;
605 int8_t find_first_non_hidden_index_() const;
608 bool ssid_was_seen_in_scan_(const CompactString &ssid) const;
610 bool needs_full_scan_results_() const;
613 bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const;
615 void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel);
619 int8_t find_next_hidden_sta_(int8_t start_index);
631 const WiFiAP *get_selected_sta_() const {
632 if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) {
633 return &this->sta_[this->selected_sta_index_];
634 }
635 return nullptr;
636 }
637
639 if (this->selected_sta_index_ < 0 || static_cast<size_t>(this->selected_sta_index_) >= this->sta_.size()) {
640 this->selected_sta_index_ = this->sta_.empty() ? -1 : 0;
641 }
642 }
643
644 bool all_networks_hidden_() const {
645 if (this->sta_.empty())
646 return false;
647 for (const auto &ap : this->sta_) {
648 if (!ap.get_hidden())
649 return false;
650 }
651 return true;
652 }
653
654 void connect_soon_();
655
656 void wifi_loop_();
657#ifdef USE_ESP8266
659#endif
661 bool wifi_sta_pre_setup_();
662 bool wifi_apply_output_power_(float output_power);
664 bool wifi_sta_ip_config_(const optional<ManualIP> &manual_ip);
666 bool wifi_sta_connect_(const WiFiAP &ap);
667 void wifi_pre_setup_();
669 bool wifi_scan_start_(bool passive);
670
671#ifdef USE_WIFI_AP
672 bool wifi_ap_ip_config_(const optional<ManualIP> &manual_ip);
673 bool wifi_start_ap_(const WiFiAP &ap);
674#endif // USE_WIFI_AP
675
676 bool wifi_disconnect_();
677
681
684
685#ifdef USE_WIFI_FAST_CONNECT
688#endif
689
690 // Post-connect roaming methods
691 void check_roaming_(uint32_t now);
694
697
698#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
703#endif
704#ifdef USE_WIFI_IP_STATE_LISTENERS
707#endif
708#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
711#endif
712
713#ifdef USE_ESP8266
714 static void wifi_event_callback(System_Event_t *event);
715 void wifi_scan_done_callback_(void *arg, STATUS status);
716 static void s_wifi_scan_done_callback(void *arg, STATUS status);
717#endif
718
719#ifdef USE_ESP32
720 void wifi_process_event_(IDFWiFiEvent *data);
721#endif
722
723#ifdef USE_RP2040
724 static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
725 void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
726#endif
727
728#ifdef USE_LIBRETINY
729 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
730 void wifi_process_event_(LTWiFiEvent *event);
732#endif
733
734 // Large/pointer-aligned members first
736 std::vector<WiFiSTAPriority> sta_priorities_;
738#ifdef USE_WIFI_AP
740#endif
741#ifdef USE_WIFI_IP_STATE_LISTENERS
743#endif
744#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
746#endif
747#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
749#endif
750#ifdef USE_WIFI_POWER_SAVE_LISTENERS
752#endif
754#ifdef USE_WIFI_FAST_CONNECT
756#endif
757#ifdef USE_WIFI_CONNECT_TRIGGER
759#endif
760#ifdef USE_WIFI_DISCONNECT_TRIGGER
762#endif
763#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
764 SemaphoreHandle_t high_performance_semaphore_{nullptr};
765#endif
766
767 // Post-connect roaming constants
768 static constexpr uint32_t ROAMING_CHECK_INTERVAL = 5 * 60 * 1000; // 5 minutes
769 static constexpr int8_t ROAMING_MIN_IMPROVEMENT = 10; // dB
770 static constexpr int8_t ROAMING_GOOD_RSSI = -49; // Skip scan if signal is excellent
771 static constexpr uint8_t ROAMING_MAX_ATTEMPTS = 3;
772
773 // 4-byte members
774 float output_power_{NAN};
776 uint32_t last_connected_{0};
777 uint32_t reboot_timeout_{};
779#ifdef USE_WIFI_AP
780 uint32_t ap_timeout_{};
781#endif
782
783 // 1-byte enums and integers
788 uint8_t num_retried_{0};
789 // Index into sta_ array for the currently selected AP configuration (-1 = none selected)
790 // Used to access password, manual_ip, priority, EAP settings, and hidden flag
791 // int8_t limits to 127 APs (enforced in __init__.py via MAX_WIFI_NETWORKS)
794#if USE_NETWORK_IPV6
796#endif /* USE_NETWORK_IPV6 */
800#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
802#endif
803
804 // Bools and bitfields
805 // Pending listener callbacks deferred from platform callbacks to main loop.
806 struct {
807#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
808 // Deferred until state machine reaches STA_CONNECTED so wifi.connected
809 // condition returns true in listener automations.
811#ifdef USE_ESP8266
812 // ESP8266: also defer disconnect notification to main loop
813 bool disconnect : 1;
814#endif
815#endif
816#if defined(USE_ESP8266) && defined(USE_WIFI_IP_STATE_LISTENERS)
817 bool got_ip : 1;
818#endif
819#if defined(USE_ESP8266) && defined(USE_WIFI_SCAN_RESULTS_LISTENERS)
821#endif
823 bool has_ap_{false};
824#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
826#endif
827 bool scan_done_{false};
828 bool ap_setup_{false};
829 bool ap_started_{false};
830 bool passive_scan_{false};
832#ifdef USE_WIFI_11KV_SUPPORT
833 bool btm_{false};
834 bool rrm_{false};
835#endif
836 bool enable_on_boot_{true};
837 bool got_ipv4_address_{false};
840 false}; // Tracks if we've completed a scan after captive portal started
842 bool post_connect_roaming_{true}; // Enabled by default
843#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
845#endif
846
847 private:
848 // Stores a pointer to a string literal (static storage duration).
849 // ONLY set from Python-generated code with string literals - never dynamic strings.
850 const char *use_address_{""};
851};
852
853extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
854
855} // namespace esphome::wifi
856#endif
BedjetMode mode
BedJet operating mode.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:227
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:137
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr size_type size() const
Definition string_ref.h:74
20-byte string: 18 chars inline + null, heap for longer.
const char * data() const
StringRef ref() const
Return a StringRef view of this string (zero-copy)
bool operator!=(const CompactString &other) const
CompactString & operator=(const CompactString &other)
bool operator==(const CompactString &other) const
static constexpr uint8_t INLINE_CAPACITY
bool operator==(const char *other) const
bool operator!=(const StringRef &other) const
const char * c_str() const
char storage_[INLINE_CAPACITY+1]
static constexpr uint8_t MAX_LENGTH
bool operator!=(const char *other) const
uint8_t get_channel() const
StringRef get_ssid() const
void set_ssid(const std::string &ssid)
const optional< EAPAuth > & get_eap() const
void set_ssid(StringRef ssid)
void set_bssid(const bssid_t &bssid)
void set_channel(uint8_t channel)
optional< EAPAuth > eap_
StringRef get_password() const
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
int8_t get_priority() const
void set_hidden(bool hidden)
const bssid_t & get_bssid() const
void set_priority(int8_t priority)
void set_password(StringRef password)
This component is responsible for managing the ESP WiFi interface.
void notify_scan_results_listeners_()
Notify scan results listeners with current scan results.
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP &params)
void add_connect_state_listener(WiFiConnectStateListener *listener)
Add a listener for WiFi connection state changes.
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.
void set_sta(const WiFiAP &ap)
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
WiFiSTAConnectStatus wifi_sta_connect_status_() const
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 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_
wifi_scan_vector_t< WiFiScanResult > scan_result_
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 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 set_enable_on_boot(bool enable_on_boot)
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 add_power_save_listener(WiFiPowerSaveListener *listener)
Add a listener for WiFi power save mode changes.
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
static constexpr uint32_t ROAMING_CHECK_INTERVAL
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
SemaphoreHandle_t high_performance_semaphore_
network::IPAddress get_dns_address(int num)
static void wifi_event_callback(System_Event_t *event)
WiFiComponent()
Construct a WiFiComponent.
void wifi_process_event_(IDFWiFiEvent *data)
std::vector< WiFiSTAPriority > sta_priorities_
static constexpr int8_t ROAMING_GOOD_RSSI
void notify_disconnect_state_listeners_()
Notify connect state listeners of disconnection.
void set_min_auth_mode(WifiMinAuthMode min_auth_mode)
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)
const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
void start_connecting(const WiFiAP &ap, bool)
static constexpr uint8_t ROAMING_MAX_ATTEMPTS
void set_passive_scan(bool passive)
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
static void s_wifi_scan_done_callback(void *arg, STATUS status)
void set_power_save_mode(WiFiPowerSaveMode power_save)
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...
void add_ip_state_listener(WiFiIPStateListener *listener)
Add a listener for IP state changes.
ESPPreferenceObject fast_connect_pref_
void clear_priorities_if_all_min_()
Clear BSSID priority tracking if all priorities are at minimum (saves memory)
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
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
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.
void set_output_power(float output_power)
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 wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
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...
void add_scan_results_listener(WiFiScanResultsListener *listener)
Add a listener for WiFi scan results.
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)
void set_ap_timeout(uint32_t ap_timeout)
bool release_high_performance()
Release a high-performance mode request.
StaticVector< WiFiPowerSaveListener *, ESPHOME_WIFI_POWER_SAVE_LISTENERS > power_save_listeners_
bool wifi_apply_output_power_(float output_power)
void set_post_connect_roaming(bool enabled)
const char * get_use_address() const
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)
network::IPAddresses wifi_sta_ip_addresses()
void check_connecting_finished(uint32_t now)
void set_keep_scan_results(bool keep_scan_results)
void start_initial_connection_()
Start initial connection - either scan or connect directly to hidden networks.
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 ...
void save_wifi_sta(StringRef ssid, StringRef password)
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)
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
Listener interface for WiFi connection state changes.
virtual void on_wifi_connect_state(StringRef ssid, std::span< const uint8_t, 6 > bssid)=0
Listener interface for WiFi IP state changes.
virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2)=0
Listener interface for WiFi power save mode changes.
virtual void on_wifi_power_save(WiFiPowerSaveMode mode)=0
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
bool matches(const WiFiAP &config) const
void set_priority(int8_t priority)
bool operator==(const WiFiScanResult &rhs) const
Listener interface for WiFi scan results.
virtual void on_wifi_scan_results(const wifi_scan_vector_t< WiFiScanResult > &results)=0
uint8_t priority
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:188
std::array< uint8_t, 6 > bssid_t
RetryHiddenMode
Controls how RETRY_HIDDEN phase selects networks to try.
@ 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...
std::vector< T > wifi_scan_vector_t
struct esphome::wifi::SavedWifiSettings PACKED
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
RoamingState
Tracks post-connect roaming state machine.
@ SCANNING
Scanning for better AP.
@ 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.
std::string size_t len
Definition helpers.h:692
esp_eap_ttls_phase2_types ttls_phase_2
Struct for setting static IPs in WiFiComponent.
network::IPAddress static_ip
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
network::IPAddress gateway
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
network::IPAddress subnet