ESPHome 2026.5.1
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 <span>
20#include <string>
21#include <type_traits>
22#include <vector>
23
24#ifdef USE_LIBRETINY
25#include <WiFi.h>
26#endif
27
28#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
29#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
30#include <esp_eap_client.h>
31#else
32#include <esp_wpa2.h>
33#endif
34#endif
35
36#ifdef USE_ESP8266
37#include <ESP8266WiFi.h>
38#include <ESP8266WiFiType.h>
39
40#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
41extern "C" {
42#include <user_interface.h>
43};
44#endif
45#endif
46
47#ifdef USE_RP2040
48extern "C" {
49#include "cyw43.h"
50#include "cyw43_country.h"
51#include "pico/cyw43_arch.h"
52}
53
54#include <WiFi.h>
55#endif
56
57#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
58#include <esp_wifi_types.h>
59#endif
60
61#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
62#include <freertos/FreeRTOS.h>
63#include <freertos/semphr.h>
64#endif
65
66namespace esphome::wifi {
67
69static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
70
72static constexpr size_t SSID_BUFFER_SIZE = 33;
73
75 char ssid[33];
76 char password[65];
77} PACKED; // NOLINT
78
80 uint8_t bssid[6];
81 uint8_t channel;
82 int8_t ap_index;
83} PACKED; // NOLINT
84
101
109
111enum class WiFiRetryPhase : uint8_t {
114#ifdef USE_WIFI_FAST_CONNECT
117#endif
126};
127
129enum class RoamingState : uint8_t {
131 IDLE,
133 SCANNING,
138};
139
141enum class RetryHiddenMode : uint8_t {
148};
149
158
159#ifdef USE_WIFI_WPA2_EAP
160struct EAPAuth {
161 std::string identity; // required for all auth types
162 std::string username;
163 std::string password;
164 const char *ca_cert; // optionally verify authentication server
165 // used for EAP-TLS
166 const char *client_cert;
167 const char *client_key;
168// used for EAP-TTLS
169#ifdef USE_ESP32
170 esp_eap_ttls_phase2_types ttls_phase_2;
171#endif
172};
173#endif // USE_WIFI_WPA2_EAP
174
175using bssid_t = std::array<uint8_t, 6>;
176
178static constexpr size_t WIFI_SCAN_RESULT_FILTERED_RESERVE = 8;
179
180// Use std::vector for RP2040 (callback-based) and ESP32 (destructive scan API)
181// Use FixedVector for ESP8266 and LibreTiny where two-pass exact allocation is possible
182#if defined(USE_RP2040) || defined(USE_ESP32)
183template<typename T> using wifi_scan_vector_t = std::vector<T>;
184#else
185template<typename T> using wifi_scan_vector_t = FixedVector<T>;
186#endif
187
191 public:
192 static constexpr uint8_t MAX_LENGTH = 127;
193 static constexpr uint8_t INLINE_CAPACITY = 18; // 18 chars + null terminator fits in 19 bytes
194
195 CompactString() : length_(0), is_heap_(0) { this->storage_[0] = '\0'; }
196 CompactString(const char *str, size_t len);
197 CompactString(const CompactString &other);
198 CompactString(CompactString &&other) noexcept;
200 CompactString &operator=(CompactString &&other) noexcept;
202
203 const char *data() const { return this->is_heap_ ? this->get_heap_ptr_() : this->storage_; }
204 const char *c_str() const { return this->data(); } // Always null-terminated
205 size_t size() const { return this->length_; }
206 bool empty() const { return this->length_ == 0; }
207
209 StringRef ref() const { return StringRef(this->data(), this->size()); }
210
211 bool operator==(const CompactString &other) const;
212 bool operator!=(const CompactString &other) const { return !(*this == other); }
213 bool operator==(const StringRef &other) const;
214 bool operator!=(const StringRef &other) const { return !(*this == other); }
215 bool operator==(const char *other) const { return *this == StringRef(other); }
216 bool operator!=(const char *other) const { return !(*this == other); }
217
218 protected:
219 char *get_heap_ptr_() const {
220 char *ptr;
221 std::memcpy(&ptr, this->storage_, sizeof(ptr));
222 return ptr;
223 }
224 void set_heap_ptr_(char *ptr) { std::memcpy(this->storage_, &ptr, sizeof(ptr)); }
225
226 // Storage for string data. When is_heap_=0, contains the string directly (null-terminated).
227 // When is_heap_=1, first sizeof(char*) bytes contain pointer to heap allocation.
228 char storage_[INLINE_CAPACITY + 1]; // 19 bytes: 18 chars + null terminator
229 uint8_t length_ : 7; // String length (0-127)
230 uint8_t is_heap_ : 1; // 1 if using heap pointer, 0 if using inline storage
231 // Total size: 20 bytes (19 bytes storage + 1 byte bitfields)
232};
233
234static_assert(sizeof(CompactString) == 20, "CompactString must be exactly 20 bytes");
235// CompactString is not trivially copyable (non-trivial destructor/copy for heap case).
236// However, its layout has no self-referential pointers: storage_[] contains either inline
237// data or an external heap pointer — never a pointer to itself. This is unlike libstdc++
238// std::string SSO where _M_p points to _M_local_buf within the same object.
239// This property allows memcpy-based permutation sorting where each element ends up in
240// exactly one slot (no ownership duplication). These asserts document that layout property.
241static_assert(std::is_standard_layout<CompactString>::value, "CompactString must be standard layout");
242static_assert(!std::is_polymorphic<CompactString>::value, "CompactString must not have vtable");
243
244class WiFiAP {
245 friend class WiFiComponent;
246 friend class WiFiScanResult;
247
248 public:
249 void set_ssid(const std::string &ssid);
250 void set_ssid(const char *ssid);
251 void set_ssid(StringRef ssid) { this->ssid_ = CompactString(ssid.c_str(), ssid.size()); }
252 void set_bssid(const bssid_t &bssid);
253 void clear_bssid();
254 void set_password(const std::string &password);
255 void set_password(const char *password);
256 void set_password(StringRef password) { this->password_ = CompactString(password.c_str(), password.size()); }
257#ifdef USE_WIFI_WPA2_EAP
258 void set_eap(optional<EAPAuth> eap_auth);
259#endif // USE_WIFI_WPA2_EAP
260 void set_channel(uint8_t channel);
261 void clear_channel();
263#ifdef USE_WIFI_MANUAL_IP
264 void set_manual_ip(optional<ManualIP> manual_ip);
265#endif
266 void set_hidden(bool hidden);
267 StringRef get_ssid() const { return this->ssid_.ref(); }
268 StringRef get_password() const { return this->password_.ref(); }
269 const bssid_t &get_bssid() const;
270 bool has_bssid() const;
271#ifdef USE_WIFI_WPA2_EAP
272 const optional<EAPAuth> &get_eap() const;
273#endif // USE_WIFI_WPA2_EAP
274 uint8_t get_channel() const { return this->channel_; }
275 bool has_channel() const { return this->channel_ != 0; }
276 int8_t get_priority() const { return priority_; }
277#ifdef USE_WIFI_MANUAL_IP
278 const optional<ManualIP> &get_manual_ip() const;
279#endif
280 bool get_hidden() const;
281
282 protected:
285#ifdef USE_WIFI_WPA2_EAP
286 optional<EAPAuth> eap_;
287#endif // USE_WIFI_WPA2_EAP
288#ifdef USE_WIFI_MANUAL_IP
289 optional<ManualIP> manual_ip_;
290#endif
291 // Group small types together to minimize padding
292 bssid_t bssid_{}; // 6 bytes, all zeros = any/not set
293 uint8_t channel_{0}; // 1 byte, 0 = auto/not set
294 int8_t priority_{0}; // 1 byte
295 bool hidden_{false}; // 1 byte (+ 3 bytes end padding to 4-byte align)
296};
297
299 friend class WiFiComponent;
300
301 public:
302 WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth,
303 bool is_hidden);
304
305 bool matches(const WiFiAP &config) const;
306
307 bool get_matches() const;
308 void set_matches(bool matches);
309 const bssid_t &get_bssid() const;
310 StringRef get_ssid() const { return this->ssid_.ref(); }
311 uint8_t get_channel() const;
312 int8_t get_rssi() const;
313 bool get_with_auth() const;
314 bool get_is_hidden() const;
315 int8_t get_priority() const { return priority_; }
317
318 bool operator==(const WiFiScanResult &rhs) const;
319
320 protected:
322 uint8_t channel_;
323 int8_t rssi_;
325 int8_t priority_{0};
326 bool matches_{false};
329};
330
335
341
347
348#ifdef USE_WIFI_PHY_MODE
349// Values 1-3 match ESP8266 SDK phy_mode_t (PHY_MODE_11B=1, PHY_MODE_11G=2, PHY_MODE_11N=3).
350// AUTO leaves the SDK at its default (no wifi_set_phy_mode() call).
357#endif
358
359#ifdef USE_ESP32
360struct IDFWiFiEvent;
361#endif
362
363#ifdef USE_LIBRETINY
364struct LTWiFiEvent;
365#endif
366
376 public:
377 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
378 const network::IPAddress &dns2) = 0;
379};
380
390 public:
392};
393
403 public:
404 virtual void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) = 0;
405};
406
416 public:
418};
419
421class WiFiComponent final : public Component {
422 public:
425
426 void set_sta(const WiFiAP &ap);
427 // Returns a copy of the currently selected AP configuration
428 WiFiAP get_sta() const;
429 void init_sta(size_t count);
430 void add_sta(const WiFiAP &ap);
431 void clear_sta();
432
433#ifdef USE_WIFI_AP
441 void set_ap(const WiFiAP &ap);
442 WiFiAP get_ap() { return this->ap_; }
443 void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
444#endif // USE_WIFI_AP
445
446 void enable();
447 void disable();
448 bool is_disabled();
449 void start_scanning();
451 void start_connecting(const WiFiAP &ap);
452 // Backward compatibility overload - ignores 'two' parameter
453 void start_connecting(const WiFiAP &ap, bool /* two */) { this->start_connecting(ap); }
454
456
457 void retry_connect();
458
459 void set_reboot_timeout(uint32_t reboot_timeout);
460
461 bool is_connected() const { return this->connected_; }
462
463 void set_power_save_mode(WiFiPowerSaveMode power_save);
464 void set_min_auth_mode(WifiMinAuthMode min_auth_mode) { min_auth_mode_ = min_auth_mode; }
465 void set_output_power(float output_power) { output_power_ = output_power; }
466#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
467 void set_band_mode(wifi_band_mode_t band_mode) { this->band_mode_ = band_mode; }
468#endif
469#ifdef USE_WIFI_PHY_MODE
470 void set_phy_mode(WiFi8266PhyMode phy_mode) { this->phy_mode_ = phy_mode; }
471#endif
472
473 void set_passive_scan(bool passive);
474
475 void save_wifi_sta(const std::string &ssid, const std::string &password);
476 void save_wifi_sta(const char *ssid, const char *password);
477 void save_wifi_sta(StringRef ssid, StringRef password) { this->save_wifi_sta(ssid.c_str(), password.c_str()); }
478
479 // ========== INTERNAL METHODS ==========
480 // (In most use cases you won't need these)
482 void setup() override;
483 void start();
484 void dump_config() override;
485 void restart_adapter();
487 float get_setup_priority() const override;
489 void loop() override;
490
491 bool has_sta() const { return !this->sta_.empty(); }
492 bool has_ap() const { return this->has_ap_; }
493 bool is_ap_active() const { return this->ap_started_; }
494
495#ifdef USE_WIFI_11KV_SUPPORT
496 void set_btm(bool btm);
497 void set_rrm(bool rrm);
498#endif
499
502 const char *get_use_address() const { return this->use_address_; }
503 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
504
506
508
509 bool has_sta_priority(const bssid_t &bssid) {
510 for (auto &it : this->sta_priorities_) {
511 if (it.bssid == bssid)
512 return true;
513 }
514 return false;
515 }
516 int8_t get_sta_priority(const bssid_t bssid) {
517 for (auto &it : this->sta_priorities_) {
518 if (it.bssid == bssid)
519 return it.priority;
520 }
521 return 0;
522 }
523 void set_sta_priority(bssid_t bssid, int8_t priority);
524
526 // Remove before 2026.9.0
527 ESPDEPRECATED("Use wifi_ssid_to() instead. Removed in 2026.9.0", "2026.3.0")
528 std::string wifi_ssid();
531 const char *wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer);
533
534 int8_t wifi_rssi();
535
536 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
537 void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
538 void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
539
540#ifdef USE_WIFI_CONNECT_TRIGGER
542#endif
543#ifdef USE_WIFI_DISCONNECT_TRIGGER
545#endif
546
547 int32_t get_wifi_channel();
548
549#ifdef USE_WIFI_IP_STATE_LISTENERS
553 void add_ip_state_listener(WiFiIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
554#endif // USE_WIFI_IP_STATE_LISTENERS
555#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
558 this->scan_results_listeners_.push_back(listener);
559 }
560#endif // USE_WIFI_SCAN_RESULTS_LISTENERS
561#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
566 this->connect_state_listeners_.push_back(listener);
567 }
568#endif // USE_WIFI_CONNECT_STATE_LISTENERS
569#ifdef USE_WIFI_POWER_SAVE_LISTENERS
573 void add_power_save_listener(WiFiPowerSaveListener *listener) { this->power_save_listeners_.push_back(listener); }
574#endif // USE_WIFI_POWER_SAVE_LISTENERS
575
576#ifdef USE_WIFI_RUNTIME_POWER_SAVE
592
605#endif // USE_WIFI_RUNTIME_POWER_SAVE
606
607 protected:
608#ifdef USE_WIFI_AP
609 void setup_ap_config_();
610#endif // USE_WIFI_AP
611
614
619 bool transition_to_phase_(WiFiRetryPhase new_phase);
622 bool needs_scan_results_() const;
628 int8_t find_first_non_hidden_index_() const;
631 bool ssid_was_seen_in_scan_(const CompactString &ssid) const;
633 bool needs_full_scan_results_() const;
636 bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const;
638 void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel);
642 int8_t find_next_hidden_sta_(int8_t start_index);
654 const WiFiAP *get_selected_sta_() const {
655 if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) {
656 return &this->sta_[this->selected_sta_index_];
657 }
658 return nullptr;
659 }
660
662 if (this->selected_sta_index_ < 0 || static_cast<size_t>(this->selected_sta_index_) >= this->sta_.size()) {
663 this->selected_sta_index_ = this->sta_.empty() ? -1 : 0;
664 }
665 }
666
667 bool all_networks_hidden_() const {
668 if (this->sta_.empty())
669 return false;
670 for (const auto &ap : this->sta_) {
671 if (!ap.get_hidden())
672 return false;
673 }
674 return true;
675 }
676
677 void connect_soon_();
678
679 bool wifi_loop_();
680#ifdef USE_ESP8266
682#endif
683 bool wifi_mode_(optional<bool> sta, optional<bool> ap);
684 bool wifi_sta_pre_setup_();
685 bool wifi_apply_output_power_(float output_power);
687#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
689#endif
690#ifdef USE_WIFI_PHY_MODE
692#endif
693 bool wifi_sta_ip_config_(const optional<ManualIP> &manual_ip);
695 bool wifi_sta_connect_(const WiFiAP &ap);
696 void wifi_pre_setup_();
703 bool wifi_scan_start_(bool passive);
704
705#ifdef USE_WIFI_AP
706 bool wifi_ap_ip_config_(const optional<ManualIP> &manual_ip);
707 bool wifi_start_ap_(const WiFiAP &ap);
708#endif // USE_WIFI_AP
709
710 bool wifi_disconnect_();
711
715
718
719#ifdef USE_WIFI_FAST_CONNECT
722#endif
723
724 // Post-connect roaming methods
725 void check_roaming_(uint32_t now);
728
731
732#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
737#endif
738#ifdef USE_WIFI_IP_STATE_LISTENERS
741#endif
742#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
745#endif
746
747#ifdef USE_ESP8266
748 static void wifi_event_callback(System_Event_t *event);
749 void wifi_scan_done_callback_(void *arg, STATUS status);
750 static void s_wifi_scan_done_callback(void *arg, STATUS status);
751#endif
752
753#ifdef USE_ESP32
754 void wifi_process_event_(IDFWiFiEvent *data);
755 friend void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
756#endif
757
758#ifdef USE_RP2040
759 static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
760 void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
761#endif
762
763#ifdef USE_LIBRETINY
764 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
765 void wifi_process_event_(LTWiFiEvent *event);
767#endif
768
769 // Large/pointer-aligned members first
771 std::vector<WiFiSTAPriority> sta_priorities_;
773#ifdef USE_WIFI_AP
775#endif
776#ifdef USE_WIFI_IP_STATE_LISTENERS
778#endif
779#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
781#endif
782#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
784#endif
785#ifdef USE_WIFI_POWER_SAVE_LISTENERS
787#endif
789#ifdef USE_WIFI_FAST_CONNECT
791#endif
792#ifdef USE_WIFI_CONNECT_TRIGGER
794#endif
795#ifdef USE_WIFI_DISCONNECT_TRIGGER
797#endif
798#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
799 SemaphoreHandle_t high_performance_semaphore_{nullptr};
800#endif
801
802 static constexpr uint8_t FIRST_5GHZ_CHANNEL = 36;
803
804 // Post-connect roaming constants
805 static constexpr uint32_t ROAMING_CHECK_INTERVAL = 5 * 60 * 1000; // 5 minutes
806 static constexpr int8_t ROAMING_MIN_IMPROVEMENT = 10; // dB
807 static constexpr int8_t ROAMING_GOOD_RSSI = -49; // Skip scan if signal is excellent
808 static constexpr uint8_t ROAMING_MAX_ATTEMPTS = 3;
809 // Grace period after roaming scan completes. If WiFi disconnects within this
810 // window (e.g., ESP8266 Beacon Timeout caused by going off-channel during scan),
811 // the disconnect is treated as roaming-related and the attempts counter is preserved.
812 static constexpr uint32_t ROAMING_SCAN_GRACE_PERIOD = 30 * 1000; // 30 seconds
813
814 // 4-byte members
815 float output_power_{NAN};
820 uint32_t roaming_scan_end_{0}; // Timestamp when last roaming scan completed
821#ifdef USE_WIFI_AP
823#endif
824
825 // 1-byte enums and integers
828#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
829 wifi_band_mode_t band_mode_{WIFI_BAND_MODE_AUTO};
830#endif
831#ifdef USE_WIFI_PHY_MODE
833#endif
836 uint8_t num_retried_{0};
837 // Index into sta_ array for the currently selected AP configuration (-1 = none selected)
838 // Used to access password, manual_ip, priority, EAP settings, and hidden flag
839 // int8_t limits to 127 APs (enforced in __init__.py via MAX_WIFI_NETWORKS)
842#if USE_NETWORK_IPV6
844#endif /* USE_NETWORK_IPV6 */
846#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
847 // Platform-specific STA state enum, defined in platform cpp file.
848 // On ESP8266, written from SDK system context (wifi_event_callback) —
849 // uint8_t writes are atomic on Xtensa LX106 so no synchronization is needed.
850 uint8_t sta_state_{0};
851#endif
854 bssid_t roaming_target_bssid_{}; // BSSID of the AP we're trying to roam to
855#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
857#endif
858
859 // Bools and bitfields
860 // Pending listener callbacks deferred from platform callbacks to main loop.
861 struct {
862#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
863 // Deferred until state machine reaches STA_CONNECTED so wifi.connected
864 // condition returns true in listener automations.
866#ifdef USE_ESP8266
867 // ESP8266: also defer disconnect notification to main loop
868 bool disconnect : 1;
869#endif
870#endif
871#if defined(USE_ESP8266) && defined(USE_WIFI_IP_STATE_LISTENERS)
872 bool got_ip : 1;
873#endif
874#if defined(USE_ESP8266) && defined(USE_WIFI_SCAN_RESULTS_LISTENERS)
876#endif
878 bool has_ap_{false};
879#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
881#endif
882 bool scan_done_{false};
883 bool ap_setup_{false};
884 bool ap_started_{false};
885 bool passive_scan_{false};
887#ifdef USE_WIFI_11KV_SUPPORT
888 bool btm_{false};
889 bool rrm_{false};
890#endif
891 bool enable_on_boot_{true};
892 bool got_ipv4_address_{false};
895 false}; // Tracks if we've completed a scan after captive portal started
897 bool connected_{false};
898 bool post_connect_roaming_{true}; // Enabled by default
899#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
901#endif
902
903#ifdef USE_ESP32
904 // Lock-free SPSC queue for WiFi events from ESP-IDF event handler.
905 // 17 slots = 16 usable (ring buffer reserves one slot). WiFi events are rare.
906 // Placed at end of class to avoid padding between smaller fields.
908#endif
909
910#ifdef USE_LIBRETINY
911 // Thread-safe queue for WiFi events from LibreTiny callback thread.
912 // LockFreeQueue on platforms with hardware atomics (RTL87xx, LN882x),
913 // FreeRTOSQueue on platforms without (BK72xx).
914 static constexpr uint8_t LT_EVENT_QUEUE_SIZE = 16;
915#ifdef ESPHOME_THREAD_MULTI_ATOMICS
916 // Ring buffer reserves one slot, so +1 for 16 usable slots
918#else
920#endif
921#endif
922
923 private:
924 // Stores a pointer to a string literal (static storage duration).
925 // ONLY set from Python-generated code with string literals - never dynamic strings.
926 const char *use_address_{""};
927};
928
929extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
930
931} // namespace esphome::wifi
932#endif
BedjetMode mode
BedJet operating mode.
ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary " "strings. Will stop working in 2026.6.0", "2025.12.0") void mark_failed(const char *message)
Definition component.h:224
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:217
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
void set_band_mode(wifi_band_mode_t band_mode)
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)
wifi_scan_vector_t< WiFiScanResult > scan_result_
WiFiPowerSaveMode configured_power_save_
struct esphome::wifi::WiFiComponent::@190 pending_
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)
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_
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)
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 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)
ESPDEPRECATED("Use wifi_ssid_to() instead. Removed in 2026.9.0", "2026.3.0") std 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)
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)
static constexpr uint32_t ROAMING_SCAN_GRACE_PERIOD
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)
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
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)
static constexpr uint8_t FIRST_5GHZ_CHANNEL
LockFreeQueue< IDFWiFiEvent, 17 > event_queue_
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:187
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
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