ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
wifi_component.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_WIFI
9#ifdef USE_ESP32
11#endif
12#if defined(USE_LIBRETINY) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
14#elif defined(USE_LIBRETINY) && defined(ESPHOME_THREAD_MULTI_NO_ATOMICS)
16#endif
18
19#include <algorithm>
20#include <atomic>
21#include <limits>
22#include <span>
23#include <memory>
24#include <string>
25#include <type_traits>
26#include <utility>
27#include <vector>
28
29#ifdef USE_LIBRETINY
30#include <WiFi.h>
31#endif
32
33#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
34#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
35#include <esp_eap_client.h>
36#else
37#include <esp_wpa2.h>
38#endif
39#endif
40
41#ifdef USE_ESP8266
42#include <ESP8266WiFi.h>
43#include <ESP8266WiFiType.h>
44
45#endif
46
47#ifdef USE_RP2
48extern "C" {
49#include "cyw43.h"
50#include "cyw43_country.h"
51#include "pico/cyw43_arch.h"
52}
53
54#include <WiFi.h>
55
56#ifdef USE_WIFI_AP
57// The linker routes the driver's firmware events here; wifi/__init__.py adds the wrap flag
58// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
59extern "C" void __wrap_cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev);
60// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
61#endif
62#endif
63
64#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
65#include <esp_wifi_types.h>
66#endif
67
68#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
69#include <freertos/FreeRTOS.h>
70#include <freertos/semphr.h>
71#endif
72
73#ifdef USE_ESP32
74// Forward declaration matching esp_netif's own typedef; avoids pulling esp_netif.h
75// into this widely-included header.
76using esp_netif_t = struct esp_netif_obj;
77#endif
78
79namespace esphome::wifi {
80
82static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
83
85static constexpr size_t SSID_BUFFER_SIZE = 33;
86
88 char ssid[33];
89 char password[65];
90} PACKED; // NOLINT
91
93 uint8_t bssid[6];
94 uint8_t channel;
95 int8_t ap_index;
96} PACKED; // NOLINT
97
98#ifdef USE_WIFI_LEASE_CACHE
99// Checked on load; a zeroed record (version 0) means no cached lease.
100static constexpr uint8_t SAVED_WIFI_LEASE_VERSION = 1;
101
102// Cached DHCP lease. IPv4 fields in network byte order, times in seconds.
115#endif
116
133
141
143enum class WiFiRetryPhase : uint8_t {
146#ifdef USE_WIFI_FAST_CONNECT
149#endif
158};
159
161enum class RoamingState : uint8_t {
163 IDLE,
165 SCANNING,
170};
171
173enum class RetryHiddenMode : uint8_t {
180};
181
190
191#ifdef USE_WIFI_MANUAL_IP
193static constexpr size_t MANUAL_IP_ENTRY_SIZE = 20;
194static constexpr uint8_t MANUAL_IP_NONE = 0xFF;
196extern const uint8_t MANUAL_IP_TABLE[] PROGMEM;
197#endif
198
199#ifdef USE_WIFI_WPA2_EAP
200struct EAPAuth {
201 std::string identity; // required for all auth types
202 std::string username;
203 std::string password;
204 const char *ca_cert; // optionally verify authentication server
205 // used for EAP-TLS
206 const char *client_cert;
207 const char *client_key;
208// used for EAP-TTLS
209#ifdef USE_ESP32
210 esp_eap_ttls_phase2_types ttls_phase_2;
211#endif
212};
213#endif // USE_WIFI_WPA2_EAP
214
215using bssid_t = std::array<uint8_t, 6>;
216
217// ESP32 with one configured network: the driver filters the scan by its SSID and only this many of
218// its BSSIDs are kept, the strongest ones
219static constexpr size_t WIFI_SCAN_RESULT_BOUND = 12;
220
221// RP2040's callback delivers results one at a time with no count, so it needs a growable vector
222#if defined(USE_RP2)
223template<typename T> using wifi_scan_vector_t = std::vector<T>;
224#else
225template<typename T> using wifi_scan_vector_t = FixedVector<T>;
226#endif
227
228// A consumer component (e.g. the captive portal) reads scan results from another
229// task; guard them with a real lock only on platforms that actually run multiple
230// threads. See ScanResultsLock below the WiFiComponent class.
231#if defined(USE_WIFI_SCAN_RESULTS_LOCK) && !defined(ESPHOME_THREAD_SINGLE)
232#define WIFI_SCAN_RESULTS_LOCK_ENABLED
233#endif
234
238 public:
239 static constexpr uint8_t MAX_LENGTH = 127;
240 static constexpr uint8_t INLINE_CAPACITY = 18; // 18 chars + null terminator fits in 19 bytes
241
242 CompactString() : length_(0), is_heap_(0) { this->storage_[0] = '\0'; }
243 CompactString(const char *str, size_t len);
244 CompactString(const CompactString &other);
245 CompactString(CompactString &&other) noexcept;
247 CompactString &operator=(CompactString &&other) noexcept;
249
250 const char *data() const { return this->is_heap_ ? this->get_heap_ptr_() : this->storage_; }
251 const char *c_str() const { return this->data(); } // Always null-terminated
252 size_t size() const { return this->length_; }
253 bool empty() const { return this->length_ == 0; }
254
256 StringRef ref() const { return StringRef(this->data(), this->size()); }
257
258 bool operator==(const CompactString &other) const;
259 bool operator!=(const CompactString &other) const { return !(*this == other); }
260 bool operator==(const StringRef &other) const;
261 bool operator!=(const StringRef &other) const { return !(*this == other); }
262 bool operator==(const char *other) const { return *this == StringRef(other); }
263 bool operator!=(const char *other) const { return !(*this == other); }
264
265 protected:
266 char *get_heap_ptr_() const {
267 char *ptr;
268 std::memcpy(&ptr, this->storage_, sizeof(ptr));
269 return ptr;
270 }
271 void set_heap_ptr_(char *ptr) { std::memcpy(this->storage_, &ptr, sizeof(ptr)); }
272
273 // Storage for string data. When is_heap_=0, contains the string directly (null-terminated).
274 // When is_heap_=1, first sizeof(char*) bytes contain pointer to heap allocation.
275 char storage_[INLINE_CAPACITY + 1]; // 19 bytes: 18 chars + null terminator
276 uint8_t length_ : 7; // String length (0-127)
277 uint8_t is_heap_ : 1; // 1 if using heap pointer, 0 if using inline storage
278 // Total size: 20 bytes (19 bytes storage + 1 byte bitfields)
279};
280
281static_assert(sizeof(CompactString) == 20, "CompactString must be exactly 20 bytes");
282// CompactString is not trivially copyable (non-trivial destructor/copy for heap case).
283// However, its layout has no self-referential pointers: storage_[] contains either inline
284// data or an external heap pointer — never a pointer to itself. This is unlike libstdc++
285// std::string SSO where _M_p points to _M_local_buf within the same object.
286// This property allows memcpy-based permutation sorting where each element ends up in
287// exactly one slot (no ownership duplication). These asserts document that layout property.
288static_assert(std::is_standard_layout<CompactString>::value, "CompactString must be standard layout");
289static_assert(!std::is_polymorphic<CompactString>::value, "CompactString must not have vtable");
290
291class WiFiAP {
292 friend class WiFiComponent;
293 friend class WiFiScanResult;
294
295 public:
296 void set_ssid(const std::string &ssid) { this->ssid_ = CompactString(ssid.c_str(), ssid.size()); }
297 void set_ssid(const char *ssid) { this->set_ssid(StringRef(ssid)); }
298 void set_ssid(StringRef ssid) { this->ssid_ = CompactString(ssid.c_str(), ssid.size()); }
299 void set_bssid(const bssid_t &bssid) { this->bssid_ = bssid; }
300 void clear_bssid() { this->bssid_ = {}; }
301 void set_password(const std::string &password) { this->password_ = CompactString(password.c_str(), password.size()); }
302 void set_password(const char *password) { this->set_password(StringRef(password)); }
303 void set_password(StringRef password) { this->password_ = CompactString(password.c_str(), password.size()); }
304#ifdef USE_WIFI_WPA2_EAP
305 void set_eap(optional<EAPAuth> eap_auth) { this->eap_ = std::move(eap_auth); }
306#endif // USE_WIFI_WPA2_EAP
307 void set_channel(uint8_t channel) { this->channel_ = channel; }
308 void clear_channel() { this->channel_ = 0; }
310#ifdef USE_WIFI_MANUAL_IP
312 void set_manual_ip(uint8_t index) { this->manual_ip_index_ = index; }
313#endif
314 void set_hidden(bool hidden) { this->hidden_ = hidden; }
315 StringRef get_ssid() const { return this->ssid_.ref(); }
316 StringRef get_password() const { return this->password_.ref(); }
317 const bssid_t &get_bssid() const { return this->bssid_; }
318 bool has_bssid() const { return this->bssid_ != bssid_t{}; }
319#ifdef USE_WIFI_WPA2_EAP
320 const optional<EAPAuth> &get_eap() const { return this->eap_; }
321#endif // USE_WIFI_WPA2_EAP
322 uint8_t get_channel() const { return this->channel_; }
323 bool has_channel() const { return this->channel_ != 0; }
324 int8_t get_priority() const { return priority_; }
325#ifdef USE_WIFI_MANUAL_IP
326 bool has_manual_ip() const { return this->manual_ip_index_ != MANUAL_IP_NONE; }
327 optional<ManualIP> get_manual_ip() const;
328#endif
329 bool get_hidden() const { return this->hidden_; }
330
331 protected:
334#ifdef USE_WIFI_WPA2_EAP
335 optional<EAPAuth> eap_;
336#endif // USE_WIFI_WPA2_EAP
337 // Group small types together to minimize padding
338 bssid_t bssid_{}; // 6 bytes, all zeros = any/not set
339 uint8_t channel_{0}; // 1 byte, 0 = auto/not set
340 int8_t priority_{0}; // 1 byte
341 bool hidden_{false}; // 1 byte; WiFiAP is byte aligned unless EAP adds a 4 byte aligned member
342#ifdef USE_WIFI_MANUAL_IP
343 uint8_t manual_ip_index_{MANUAL_IP_NONE};
344#endif
345};
346
348 friend class WiFiComponent;
349
350 public:
351 WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth,
352 bool is_hidden);
353
354 bool matches(const WiFiAP &config) const;
355
356 bool get_matches() const { return this->matches_; }
357 void set_matches(bool matches) { this->matches_ = matches; }
358 const bssid_t &get_bssid() const { return this->bssid_; }
359 StringRef get_ssid() const { return this->ssid_.ref(); }
360 uint8_t get_channel() const { return this->channel_; }
361 int8_t get_rssi() const { return this->rssi_; }
362 bool get_with_auth() const { return this->with_auth_; }
363 bool get_is_hidden() const { return this->is_hidden_; }
364 bool ssid_equals(const WiFiScanResult &other) const { return this->ssid_ == other.ssid_; }
365 int8_t get_priority() const { return priority_; }
367
368 bool operator==(const WiFiScanResult &rhs) const;
369
370 protected:
372 uint8_t channel_;
373 int8_t rssi_;
375 int8_t priority_{0};
376 bool matches_{false};
379};
380
385
391
397
398#ifdef USE_WIFI_PHY_MODE
399// Values 1-3 match ESP8266 SDK phy_mode_t (PHY_MODE_11B=1, PHY_MODE_11G=2, PHY_MODE_11N=3).
400// AUTO leaves the SDK at its default (no wifi_set_phy_mode() call).
407#endif
408
409#ifdef USE_ESP32
410struct IDFWiFiEvent;
411#endif
412
413#ifdef USE_LIBRETINY
414struct LTWiFiEvent;
415#endif
416
426 public:
427 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
428 const network::IPAddress &dns2) = 0;
429};
430
440 public:
442};
443
453 public:
454 virtual void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) = 0;
455};
456
466 public:
468};
469
470// Scheduler ids
471static constexpr uint32_t AP_HANDOVER_TIMEOUT_ID = 0;
472static constexpr uint32_t LEASE_RENEW_TIMEOUT_ID = 1;
473static constexpr uint32_t AP_DEAUTH_RETRY_TIMEOUT_ID = 2;
474
476class WiFiComponent final : public Component {
477 public:
480
481 void set_sta(const WiFiAP &ap);
482 // Returns a copy of the currently selected AP configuration
483 WiFiAP get_sta() const;
484 // init_sta/add_sta kept out of line: inlining them into the generated setup() grows flash
485 void init_sta(size_t count);
486 void add_sta(const WiFiAP &ap);
487 void clear_sta();
488
489#ifdef USE_WIFI_AP
497 void set_ap(const WiFiAP &ap);
498 WiFiAP get_ap() { return this->ap_; }
499 void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
500#endif // USE_WIFI_AP
501
502#ifdef USE_WIFI_DPP
503 // Code generation passes string literals that live in flash
504 void set_dpp_key(const char *key) { this->dpp_key_ = key; }
505 void set_dpp_device_info(const char *info) { this->dpp_device_info_ = info; }
506 void set_dpp_channels(const char *channels) { this->dpp_channels_ = channels; }
507 void set_dpp_timeout(uint32_t timeout) { this->dpp_timeout_ = timeout; }
508#ifdef USE_WIFI_DPP_URI_TRIGGER
509 template<typename F> void add_on_dpp_uri_callback(F &&callback) {
510 this->dpp_uri_callback_.add(std::forward<F>(callback));
511 }
512#endif
514 const char *get_dpp_uri() const { return this->dpp_uri_.get(); }
515#endif
516
517 void enable();
518 void disable();
520 void start_scanning();
522 void start_connecting(const WiFiAP &ap);
523 // Backward compatibility overload - ignores 'two' parameter
524 void start_connecting(const WiFiAP &ap, bool /* two */) { this->start_connecting(ap); }
525
527
528 void retry_connect();
529
530 void set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeout_ = reboot_timeout; }
531
532 bool is_connected() const { return this->connected_; }
533
536
539 bool is_roaming() const { return this->roaming_state_ != RoamingState::IDLE; }
540
541#ifdef USE_ESP32
545#endif
546
547 void set_power_save_mode(WiFiPowerSaveMode power_save);
548 void set_min_auth_mode(WifiMinAuthMode min_auth_mode) { min_auth_mode_ = min_auth_mode; }
549 void set_output_power(float output_power) { output_power_ = output_power; }
550#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
551 void set_band_mode(wifi_band_mode_t band_mode) { this->band_mode_ = band_mode; }
552#endif
553#ifdef USE_WIFI_PHY_MODE
554 void set_phy_mode(WiFi8266PhyMode phy_mode) { this->phy_mode_ = phy_mode; }
555#endif
556
557 void set_passive_scan(bool passive) { this->passive_scan_ = passive; }
558
559 void save_wifi_sta(const std::string &ssid, const std::string &password);
560 void save_wifi_sta(const char *ssid, const char *password);
561 void save_wifi_sta(StringRef ssid, StringRef password) { this->save_wifi_sta(ssid.c_str(), password.c_str()); }
562
563 // ========== INTERNAL METHODS ==========
564 // (In most use cases you won't need these)
566 void setup() override;
567#if defined(USE_ESP32) || defined(USE_LN882X) || defined(USE_BK72XX)
568 void on_powerdown() override;
569#endif
570 void start();
571 void dump_config() override;
572 void restart_adapter();
574 float get_setup_priority() const override { return setup_priority::WIFI; }
576 void loop() override;
577
578 bool has_sta() const { return !this->sta_.empty(); }
579 bool has_ap() const { return this->has_ap_; }
580 bool is_ap_active() const { return this->ap_started_; }
581
582#ifdef USE_WIFI_11KV_SUPPORT
583 void set_btm(bool btm) { this->btm_ = btm; }
584 void set_rrm(bool rrm) { this->rrm_ = rrm; }
585#endif
586
591 const char *get_use_address() const { return this->use_address_; }
592 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
593
598
600
601 bool has_sta_priority(const bssid_t &bssid) {
602 for (auto &it : this->sta_priorities_) {
603 if (it.bssid == bssid)
604 return true;
605 }
606 return false;
607 }
608 int8_t get_sta_priority(const bssid_t bssid) {
609 for (auto &it : this->sta_priorities_) {
610 if (it.bssid == bssid)
611 return it.priority;
612 }
613 return 0;
614 }
615 void set_sta_priority(bssid_t bssid, int8_t priority);
616
620 const char *wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer);
622
623 int8_t wifi_rssi();
624
625 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
626#ifdef USE_WIFI_LEASE_CACHE
627 void set_lease_cache_max_age(uint32_t seconds) { this->lease_cache_max_age_ = seconds; }
628#endif
629 void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
630 void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
631
636 void force_roam_check();
637
638#ifdef USE_WIFI_CONNECT_TRIGGER
640#endif
641#ifdef USE_WIFI_DISCONNECT_TRIGGER
643#endif
644
645 int32_t get_wifi_channel();
646
647#ifdef USE_WIFI_IP_STATE_LISTENERS
651 void add_ip_state_listener(WiFiIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
652#endif // USE_WIFI_IP_STATE_LISTENERS
653#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
656 this->scan_results_listeners_.push_back(listener);
657 }
658#endif // USE_WIFI_SCAN_RESULTS_LISTENERS
659#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
664 this->connect_state_listeners_.push_back(listener);
665 }
666#endif // USE_WIFI_CONNECT_STATE_LISTENERS
667#ifdef USE_WIFI_POWER_SAVE_LISTENERS
671 void add_power_save_listener(WiFiPowerSaveListener *listener) { this->power_save_listeners_.push_back(listener); }
672#endif // USE_WIFI_POWER_SAVE_LISTENERS
673
674#ifdef USE_WIFI_RUNTIME_POWER_SAVE
690
703#endif // USE_WIFI_RUNTIME_POWER_SAVE
704
705#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
722 uint8_t current = this->roaming_suppression_count_.load(std::memory_order_relaxed);
723 // CAS loop: saturate at max instead of wrapping, so an excess of requests can't roll the
724 // counter back to zero and unintentionally re-enable roaming.
725 while (current < std::numeric_limits<uint8_t>::max() &&
726 !this->roaming_suppression_count_.compare_exchange_weak(current, current + 1, std::memory_order_relaxed)) {
727 }
728 }
729
739 uint8_t current = this->roaming_suppression_count_.load(std::memory_order_relaxed);
740 // CAS loop: decrement only if non-zero, so an unmatched release can't wrap the counter
741 // and permanently suppress roaming.
742 while (current > 0 &&
743 !this->roaming_suppression_count_.compare_exchange_weak(current, current - 1, std::memory_order_relaxed)) {
744 }
745 }
746#endif // USE_ESP32 && USE_WIFI_RUNTIME_ROAMING_SUPPRESSION
747
748 protected:
749#ifdef USE_WIFI_AP
750 void setup_ap_config_();
752 void disable_ap_();
754 void forget_ap_();
755#ifdef USE_WIFI_AP_EXCLUSIVE
757 void pause_exclusive_ap_();
758#endif
759#endif // USE_WIFI_AP
760
761#ifdef USE_WIFI_AP_WISP
766#ifndef USE_WIFI_AP_WISP_STATIC_DNS
769#ifdef USE_WIFI_AP_WISP_PORTAL
771 void drop_ap_clients_(uint8_t attempt = 0);
775#endif
776#endif
777#endif
778
781
786 bool transition_to_phase_(WiFiRetryPhase new_phase);
789 bool needs_scan_results_() const;
795 int8_t find_first_non_hidden_index_() const;
798 bool ssid_was_seen_in_scan_(const CompactString &ssid) const;
800 bool needs_full_scan_results_() const;
803 const WiFiAP *find_configured_network_(const char *ssid, const uint8_t *bssid) const;
804 bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const {
805 return this->find_configured_network_(ssid, bssid) != nullptr;
806 }
808 void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel);
812 int8_t find_next_hidden_sta_(int8_t start_index);
824 const WiFiAP *get_selected_sta_() const {
825 if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) {
826 return &this->sta_[this->selected_sta_index_];
827 }
828 return nullptr;
829 }
830
832 if (this->selected_sta_index_ < 0 || static_cast<size_t>(this->selected_sta_index_) >= this->sta_.size()) {
833 this->selected_sta_index_ = this->sta_.empty() ? -1 : 0;
834 }
835 }
836
837 bool all_networks_hidden_() const {
838 return !this->sta_.empty() &&
839 std::all_of(this->sta_.begin(), this->sta_.end(), [](const WiFiAP &ap) { return ap.get_hidden(); });
840 }
841
842 bool wifi_loop_();
843#ifdef USE_ESP8266
845#endif
846 bool wifi_mode_(optional<bool> sta, optional<bool> ap);
847 bool wifi_sta_pre_setup_();
848 bool wifi_apply_output_power_(float output_power);
852#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
854#endif
855#ifdef USE_WIFI_PHY_MODE
857#endif
858 bool wifi_sta_ip_config_(const optional<ManualIP> &manual_ip);
860 bool wifi_sta_connect_(const WiFiAP &ap);
861 void wifi_pre_setup_();
862#ifdef USE_ESP32
863 // ESP-IDF only: defers esp_wifi_init() + netif creation (which allocate ~15-30KB of
864 // DMA-capable internal SRAM) until wifi actually needs to come up. Idempotent.
865 // Called from setup() only when enable_on_boot_=true, and from enable() on first use.
867#endif
874 bool wifi_scan_start_(bool passive);
875
876#ifdef USE_WIFI_AP
877 bool wifi_ap_ip_config_(const optional<ManualIP> &manual_ip);
878 bool wifi_start_ap_(const WiFiAP &ap);
879#endif // USE_WIFI_AP
880
881 bool wifi_disconnect_();
882
886
887#ifdef USE_WIFI_DPP
888 // Starts listening for a DPP configurator; the platform half reports whether the driver accepted
889 void start_dpp_listen_(uint32_t now);
890 void stop_dpp_listen_();
891#endif
893 void start_cooldown_();
894#ifdef USE_WIFI_DPP
898#endif
899 bool is_dpp_active_();
907 void start_ap_portal_();
908 void end_ap_portal_();
909#ifdef USE_WIFI_AP
911 void start_portal_scan_();
912#endif
914
915#ifdef USE_WIFI_FAST_CONNECT
917 void save_fast_connect_settings_(const bssid_t &bssid, uint8_t channel);
918#endif
919
920#ifdef USE_WIFI_LEASE_CACHE
924 // Cached lease belongs to the selected network and is still provably valid.
927#endif
928
929 // Post-connect roaming methods
930 void check_roaming_(uint32_t now);
933#ifdef USE_ESP32
937 void handle_driver_roam_(const bssid_t &bssid, uint8_t channel);
938#endif
939
941 bool roaming_suppressed_() const {
942#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
943 return this->roaming_suppression_count_.load(std::memory_order_relaxed) != 0;
944#else
945 return false;
946#endif
947 }
948
951
952#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
957#endif
958#ifdef USE_WIFI_IP_STATE_LISTENERS
961#endif
962#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
965#endif
966
967#ifdef USE_ESP8266
968 static void wifi_event_callback(System_Event_t *event);
969 void wifi_scan_done_callback_(void *arg, STATUS status);
970 static void s_wifi_scan_done_callback(void *arg, STATUS status);
971#endif
972
973#ifdef USE_ESP32
974 void wifi_process_event_(IDFWiFiEvent *data);
976 friend void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
977#endif
978
979 friend class ScanResultsLock;
980
981#ifdef USE_RP2
982 static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
983 void wifi_scan_result_(void *env, const cyw43_ev_scan_result_t *result);
984#ifdef USE_WIFI_AP
985 // NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
986 friend void ::__wrap_cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev);
987 // NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
988#endif
989#endif
990
991#ifdef USE_LIBRETINY
992 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
993 void wifi_process_event_(LTWiFiEvent *event);
995#endif
996
997 // Large/pointer-aligned members first
999 std::vector<WiFiSTAPriority> sta_priorities_;
1000 // Guarded by ScanResultsLock (see below this class)
1002#ifdef WIFI_SCAN_RESULTS_LOCK_ENABLED
1004#endif
1005#ifdef USE_WIFI_IP_STATE_LISTENERS
1007#endif
1008#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
1010#endif
1011#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
1013#endif
1014#ifdef USE_WIFI_POWER_SAVE_LISTENERS
1016#endif
1018#ifdef USE_WIFI_FAST_CONNECT
1020#endif
1021#ifdef USE_WIFI_LEASE_CACHE
1024 // Current IP came from the cache (DHCP not run); suppresses saving until a real lease arrives.
1027#endif
1028#ifdef USE_WIFI_CONNECT_TRIGGER
1030#endif
1031#ifdef USE_WIFI_DISCONNECT_TRIGGER
1033#endif
1034#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
1035 SemaphoreHandle_t high_performance_semaphore_{nullptr};
1036#endif
1037
1038 static constexpr uint8_t FIRST_5GHZ_CHANNEL = 36;
1039
1040 // Post-connect roaming constants
1041 static constexpr uint32_t ROAMING_CHECK_INTERVAL = 5 * 60 * 1000; // 5 minutes
1042 static constexpr int8_t ROAMING_MIN_IMPROVEMENT = 10; // dB
1043 static constexpr int8_t ROAMING_GOOD_RSSI = -49; // Skip scan if signal is excellent
1044 static constexpr uint8_t ROAMING_MAX_ATTEMPTS = 3;
1045 // Grace period after roaming scan completes. If WiFi disconnects within this
1046 // window (e.g., ESP8266 Beacon Timeout caused by going off-channel during scan),
1047 // the disconnect is treated as roaming-related and the attempts counter is preserved.
1048 static constexpr uint32_t ROAMING_SCAN_GRACE_PERIOD = 30 * 1000; // 30 seconds
1049
1051 this->last_connected_ = now;
1052#if defined(USE_WIFI_AP) && !defined(USE_WIFI_AP_EXCLUSIVE)
1053 this->ap_start_failures_ = 0;
1054#endif
1055 }
1056
1057 // 4-byte members
1058 float output_power_{NAN};
1061#if defined(USE_WIFI_AP_EXCLUSIVE) || defined(USE_WIFI_AP_PERSISTENT)
1062 uint32_t ap_changed_{0}; // When the AP was last started or paused
1063#endif
1064 uint32_t reboot_timeout_{900000}; // Keep in sync with DEFAULT_REBOOT_TIMEOUT in __init__.py
1066 uint32_t roaming_scan_end_{0}; // Timestamp when last roaming scan completed
1067#ifdef USE_WIFI_AP
1068 uint32_t ap_timeout_{90000}; // Keep in sync with DEFAULT_AP_TIMEOUT in __init__.py
1069 // WiFiAP is byte aligned unless EAP is enabled; placed before the
1070 // 1-byte members so they pack into its trailing bytes instead of padding after it
1072#endif // USE_WIFI_AP
1073#ifdef USE_WIFI_DPP
1074 const char *dpp_key_{nullptr};
1075 const char *dpp_device_info_{nullptr};
1076 const char *dpp_channels_{nullptr};
1077 std::unique_ptr<char[]> dpp_uri_; // written by the event task before the URI event is queued
1078#ifdef USE_WIFI_DPP_URI_TRIGGER
1080#endif
1081 uint32_t dpp_timeout_{60000}; // Keep in sync with DEFAULT_DPP_TIMEOUT in __init__.py
1083#endif
1084
1085 // 1-byte enums and integers
1088#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
1089 wifi_band_mode_t band_mode_{WIFI_BAND_MODE_AUTO};
1090#endif
1091#ifdef USE_WIFI_PHY_MODE
1093#endif
1096 uint8_t num_retried_{0};
1097 // Index into sta_ array for the currently selected AP configuration (-1 = none selected)
1098 // Used to access password, manual_ip, priority, EAP settings, and hidden flag
1099 // int8_t limits to 127 APs (enforced in __init__.py via MAX_WIFI_NETWORKS)
1102#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
1103 // Count of active roaming-suppression requests. Incremented/decremented from any task
1104 // (e.g. audio playback), read in loop(). Roaming scans are paused while non-zero.
1105 // Relaxed ordering is sufficient: the count value is the only data shared across threads,
1106 // so no happens-before relationship with other memory needs to be established.
1107 std::atomic<uint8_t> roaming_suppression_count_{0};
1108#endif
1109#if USE_NETWORK_IPV6
1111#endif /* USE_NETWORK_IPV6 */
1113#if defined(USE_ESP32) && !defined(USE_WIFI_MULTI_SSID)
1116#else
1117 constexpr bool is_scan_driver_filtered_() const { return false; }
1118#endif
1119#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
1120 // Platform-specific STA state enum, defined in platform cpp file.
1121 // On ESP8266, written from SDK system context (wifi_event_callback) —
1122 // uint8_t writes are atomic on Xtensa LX106 so no synchronization is needed.
1123 uint8_t sta_state_{0};
1124#endif
1125#ifdef USE_LIBRETINY
1126 // First attempt since STA-up (re-armed on every STA off->on); the
1127 // pre-attempt teardown is skipped then.
1129 // A self-inflicted disconnect from that teardown is pending; it must not
1130 // consume an ignored-disconnect slot.
1132#endif
1135 bssid_t roaming_target_bssid_{}; // BSSID of the AP we're trying to roam to
1136#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
1138#endif
1139
1140 // Bools and bitfields
1141 // Pending listener callbacks deferred from platform callbacks to main loop.
1142 // Empty when no listener needs deferring (e.g. ESP32 without connect state listeners)
1143 [[no_unique_address]] struct {
1144#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
1145 // Deferred until state machine reaches STA_CONNECTED so wifi.connected
1146 // condition returns true in listener automations.
1148#ifdef USE_ESP8266
1149 // ESP8266: also defer disconnect notification to main loop
1150 bool disconnect : 1;
1151#endif
1152#endif
1153#if defined(USE_ESP8266) && defined(USE_WIFI_IP_STATE_LISTENERS)
1154 bool got_ip : 1;
1155#endif
1156#if defined(USE_ESP8266) && defined(USE_WIFI_SCAN_RESULTS_LISTENERS)
1158#endif
1160 bool has_ap_{false};
1161#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
1163#endif
1164 bool scan_done_{false};
1165 bool ap_setup_{false};
1166 bool ap_started_{false};
1167#ifdef USE_WIFI_DPP
1168 bool dpp_listening_{false};
1169#endif
1170#ifdef USE_WIFI_AP
1171 // Stations joined to the AP; they hold the reboot timeout and keep an exclusive AP from pausing.
1172 // Counted on the main loop on ESP32 and LibreTiny (queued events), in the SDK callback on ESP8266 and
1173 // in the driver's interrupt context on the Pico W. The main loop only reads the byte while the AP is up
1174 // and only writes it (a reset) while the AP is down, so no lock is needed.
1175 uint8_t ap_clients_{0};
1176#ifndef USE_WIFI_AP_EXCLUSIVE
1177 // Fallback AP starts that failed since the last connection; each one waits another ap_timeout
1179#endif
1180#endif
1181#ifdef USE_WIFI_AP_WISP_PORTAL
1182 // DHCP offers the AP's own address; apply_ap_dns_() drops the clients whenever that changes
1184#endif
1185 bool passive_scan_{false};
1187#ifdef USE_WIFI_11KV_SUPPORT
1188 bool btm_{false};
1189 bool rrm_{false};
1190#endif
1192#ifdef USE_ESP32
1193 // Tracks whether esp_wifi_init() + netif creation has happened. Allows enable()
1194 // to be called at runtime without re-allocating, and ensures the heavy init is
1195 // skipped entirely when enable_on_boot_ is false until first enable().
1197#endif
1201 false}; // Tracks if we've completed a scan after captive portal started
1203 bool connected_{false};
1204 bool post_connect_roaming_{true}; // Enabled by default
1205#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
1207#endif
1208
1209#ifdef USE_ESP32
1210 // Lock-free SPSC queue for WiFi events from ESP-IDF event handler.
1211 // 17 slots = 16 usable (ring buffer reserves one slot). WiFi events are rare.
1212 // Placed at end of class to avoid padding between smaller fields.
1214#endif
1215
1216#ifdef USE_LIBRETINY
1217 // Thread-safe queue for WiFi events from LibreTiny callback thread.
1218 // LockFreeQueue on platforms with hardware atomics (RTL87xx, LN882x),
1219 // FreeRTOSQueue on platforms without (BK72xx).
1220 static constexpr uint8_t LT_EVENT_QUEUE_SIZE = 16;
1221#ifdef ESPHOME_THREAD_MULTI_ATOMICS
1222 // Ring buffer reserves one slot, so +1 for 16 usable slots
1224#else
1226#endif
1227#endif
1228
1229 private:
1230 // Stores a pointer to a string literal (static storage duration).
1231 // ONLY set from Python-generated code with string literals - never dynamic strings.
1232 const char *use_address_{nullptr};
1233};
1234
1235extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
1236
1246 public:
1247#ifdef WIFI_SCAN_RESULTS_LOCK_ENABLED
1248 ScanResultsLock(WiFiComponent *parent) : guard_(parent->scan_result_lock_) {}
1249
1250 private:
1251 LockGuard guard_;
1252#else
1254#endif
1255};
1256
1257} // namespace esphome::wifi
1258#endif
BedjetMode mode
BedJet operating mode.
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:669
Helper class that wraps a mutex with a RAII-style API.
Definition helpers.h:2222
Mutex implementation, with API based on the unavailable std::mutex.
Definition helpers.h:2183
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:304
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
constexpr size_type size() const
Definition string_ref.h:79
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
Guards WiFiComponent::scan_result_.
ScanResultsLock(WiFiComponent *parent)
void set_password(const char *password)
uint8_t get_channel() const
StringRef get_ssid() const
void set_manual_ip(uint8_t index)
index is a row of MANUAL_IP_TABLE
void set_ssid(const std::string &ssid)
void set_ssid(const char *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
void set_eap(optional< EAPAuth > eap_auth)
void set_password(const std::string &password)
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.
bool is_provisioning_closed_()
The provisioning window has closed, so no new provisioning surface may start.
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP &params)
constexpr bool is_scan_driver_filtered_() const
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 roaming_suppressed_() const
Returns true if a component has requested that roaming scans be suppressed (e.g. during audio playbac...
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
WiFiSTAConnectStatus wifi_sta_connect_status_() const
void set_band_mode(wifi_band_mode_t band_mode)
int8_t get_sta_priority(const bssid_t bssid)
void set_last_connected_(uint32_t now)
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)
SavedWifiLeaseSettings cached_lease_
wifi_scan_vector_t< WiFiScanResult > scan_result_
void save_fast_connect_settings_(const bssid_t &bssid, uint8_t channel)
WiFiPowerSaveMode configured_power_save_
void wifi_scan_result_(void *env, const cyw43_ev_scan_result_t *result)
void set_sta_priority(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)
std::atomic< uint8_t > roaming_suppression_count_
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.
void set_dpp_channels(const char *channels)
ESPPreferenceObject lease_cache_pref_
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
static constexpr uint32_t ROAMING_CHECK_INTERVAL
esp_netif_t * get_esp_netif_sta()
esp_netif handle of the station interface, used by network for default-route arbitration.
bool is_roaming() const
True while a post-connect roam is in progress (scanning off-channel, reassociating,...
void set_dpp_timeout(uint32_t timeout)
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
SemaphoreHandle_t high_performance_semaphore_
bool ap_dns_known_()
Whether there is an IPv4 resolver to offer AP clients: a configured one, or the station's.
void start_cooldown_()
Wait before the next attempt; while a configurator is waited for, the wait is its listen window.
network::IPAddress get_dns_address(int num)
void pause_exclusive_ap_()
Drop the fallback AP so the networks can be tried; it comes back after ap_timeout.
void drop_ap_clients_(uint8_t attempt=0)
Disconnect the AP clients so they rejoin with the resolver now offered; a few retries,...
static void wifi_event_callback(System_Event_t *event)
void apply_ap_dns_()
Pass the station's resolvers on to AP clients when they changed.
WiFiComponent()
Construct a WiFiComponent.
void wifi_process_event_(IDFWiFiEvent *data)
std::vector< WiFiSTAPriority > sta_priorities_
void apply_radio_settings_()
Output power and power save, wherever the radio first comes up: start() with networks,...
bool lease_valid_for_apply_() const
FreeRTOSQueue< LTWiFiEvent, LT_EVENT_QUEUE_SIZE > event_queue_
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)
void start_portal_scan_()
Scan alongside the AP so the portal can list networks, when there is no station to try.
friend void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
StaticVector< WiFiConnectStateListener *, ESPHOME_WIFI_CONNECT_STATE_LISTENERS > connect_state_listeners_
void release_roaming_suppression()
Release a roaming suppression request.
void set_dpp_key(const char *key)
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 set_dpp_device_info(const char *info)
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
CallbackManager< void(const char *)> dpp_uri_callback_
static void s_wifi_scan_done_callback(void *arg, STATUS status)
void set_power_save_mode(WiFiPowerSaveMode power_save)
bool is_roaming_scan_active() const
True while a post-connect roaming scan holds the radio off-channel.
LockFreeQueue< LTWiFiEvent, LT_EVENT_QUEUE_SIZE+1 > event_queue_
void set_phy_mode(WiFi8266PhyMode phy_mode)
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)
bool is_ap_portal_active_()
captive_portal or the web_server AP mode is serving a user on the access point
static constexpr uint32_t ROAMING_SCAN_GRACE_PERIOD
void set_lease_cache_max_age(uint32_t seconds)
bool offer_own_dns_()
DHCP offers the AP's own address while the portal is up for a station that is down,...
WiFiRetryPhase determine_next_phase_()
Determine next retry phase based on current state and failure conditions.
network::IPAddress wifi_dns_ip_(int num)
network::IPAddresses get_ip_addresses()
static constexpr int8_t ROAMING_MIN_IMPROVEMENT
static constexpr uint8_t LT_EVENT_QUEUE_SIZE
bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const
bool is_provisioning_active_()
A portal, improv or DPP is waiting for credentials, so the retry machine must not disturb the radio.
float get_setup_priority() const override
WIFI setup_priority.
void disable_ap_()
End the portals and turn the AP off.
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.
void request_roaming_suppression()
Request that post-connect roaming scans be suppressed.
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 start_dpp_listen_(uint32_t now)
static constexpr uint8_t FIRST_5GHZ_CHANNEL
void forget_ap_()
End the portals and forget the AP's state, before the radio takes the AP down.
LockFreeQueue< IDFWiFiEvent, 17 > event_queue_
void set_ap_timeout(uint32_t ap_timeout)
bool release_high_performance()
Release a high-performance mode request.
void add_on_dpp_uri_callback(F &&callback)
void force_roam_check()
Force an immediate post-connect roaming check, bypassing the periodic interval and the per-connection...
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
Returns nullptr when no explicit use_address is configured and the address is derived at runtime from...
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)
std::unique_ptr< char[]> dpp_uri_
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 handle_driver_roam_(const bssid_t &bssid, uint8_t channel)
Redo post-connect bookkeeping after a driver-initiated roam (e.g.
bool write_ap_dns_()
Hand the AP DHCP server its resolvers; false when one could not be set.
struct esphome::wifi::WiFiComponent::@199 pending_
const WiFiAP * find_configured_network_(const char *ssid, const uint8_t *bssid) const
The configured network a scan result belongs to, or nullptr (for scan result filtering) Matches by SS...
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 char * get_dpp_uri() const
The Easy Connect URI to show as a QR code, nullptr until the supplicant has generated it.
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
Main-loop callers may read this directly.
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
bool ssid_equals(const WiFiScanResult &other) 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
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
const char * key
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:368
constexpr float WIFI
Definition component.h:50
std::array< uint8_t, 6 > bssid_t
struct esphome::wifi::SavedWifiSettings PACKED
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
const uint8_t MANUAL_IP_TABLE[] PROGMEM
Every configured manual IP, generated by codegen (components/wifi/__init__.py) and kept in flash.
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.
const void size_t len
Definition hal.h:68
struct esp_netif_obj esp_netif_t
static void uint32_t
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
uint8_t event_id
Definition tt21100.cpp:3
void __wrap_cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev)