ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
wifi_component_esp8266.cpp
Go to the documentation of this file.
1#include "wifi_component.h"
3
4#ifdef USE_WIFI
5#ifdef USE_ESP8266
6
7#include <user_interface.h>
8
9#include <cassert>
10#include <utility>
11#include <algorithm>
12#ifdef USE_WIFI_WPA2_EAP
13#include <wpa2_enterprise.h>
14#endif
15
16extern "C" {
17#include "lwip/err.h"
18#include "lwip/dns.h"
19#include "lwip/dhcp.h"
20#include "lwip/init.h" // LWIP_VERSION_
21#include "lwip/apps/sntp.h"
22#include "lwip/netif.h" // struct netif
23#include <AddrList.h>
24#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 0, 0)
25#include "LwipDhcpServer.h"
26#if USE_ARDUINO_VERSION_CODE < VERSION_CODE(3, 1, 0)
27#include <ESP8266WiFi.h>
28#include "ESP8266WiFiAP.h"
29#define wifi_softap_set_dhcps_lease(lease) dhcpSoftAP.set_dhcps_lease(lease)
30#define wifi_softap_set_dhcps_lease_time(time) dhcpSoftAP.set_dhcps_lease_time(time)
31#define wifi_softap_set_dhcps_offer_option(offer, mode) dhcpSoftAP.set_dhcps_offer_option(offer, mode)
32#endif
33#endif
34}
35
37#include "esphome/core/hal.h"
39#include "esphome/core/log.h"
41#include "esphome/core/util.h"
42
43namespace esphome::wifi {
44
45static const char *const TAG = "wifi_esp8266";
46
47enum class ESP8266WiFiSTAState : uint8_t {
48 IDLE, // Not connecting
49 CONNECTING, // Connection in progress
50 ASSOCIATED, // Associated to AP, waiting for IP
51 CONNECTED, // Successfully connected with IP
52 ERROR_NOT_FOUND, // AP not found (probe failed)
53 ERROR_FAILED, // Connection failed (auth, timeout, etc.)
54};
55
56bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
57 uint8_t current_mode = wifi_get_opmode();
58 bool current_sta = current_mode & 0b01;
59 bool current_ap = current_mode & 0b10;
60 bool target_sta = sta.value_or(current_sta);
61 bool target_ap = ap.value_or(current_ap);
62 if (current_sta == target_sta && current_ap == target_ap)
63 return true;
64
65 if (target_sta && !current_sta) {
66 ESP_LOGV(TAG, "Enabling STA");
67 } else if (!target_sta && current_sta) {
68 ESP_LOGV(TAG, "Disabling STA");
69 // Stop DHCP client when disabling STA
70 // See https://github.com/esp8266/Arduino/pull/5703
71 wifi_station_dhcpc_stop();
72 }
73 if (target_ap && !current_ap) {
74 ESP_LOGV(TAG, "Enabling AP");
75 } else if (!target_ap && current_ap) {
76 ESP_LOGV(TAG, "Disabling AP");
77 }
78
79 ETS_UART_INTR_DISABLE();
80 uint8_t mode = 0;
81 if (target_sta)
82 mode |= 0b01;
83 if (target_ap)
84 mode |= 0b10;
85 bool ret = wifi_set_opmode_current(mode);
86 ETS_UART_INTR_ENABLE();
87
88 if (!ret) {
89 ESP_LOGW(TAG, "Set mode failed");
90 return false;
91 }
92
93 this->ap_started_ = target_ap;
94
95 return ret;
96}
98 // ESP8266 sleep types have confusing names — LIGHT_SLEEP_T is the MORE aggressive mode.
99 // SDK enum: NONE_SLEEP_T=0, LIGHT_SLEEP_T=1, MODEM_SLEEP_T=2
100 // https://github.com/esp8266/Arduino/blob/3.1.2/tools/sdk/include/user_interface.h#L447-L451
101 // Arduino ESP32 compat confirms: WIFI_PS_MIN_MODEM=MODEM_SLEEP, WIFI_PS_MAX_MODEM=LIGHT_SLEEP
102 // https://github.com/esp8266/Arduino/blob/3.1.2/libraries/ESP8266WiFi/src/ESP8266WiFiType.h#L53-L55
103 sleep_type_t power_save;
104 switch (this->power_save_) {
106 // MODEM_SLEEP_T: only the WiFi modem sleeps between DTIM beacons, CPU stays active.
107 // Matches ESP32's WIFI_PS_MIN_MODEM.
108 power_save = MODEM_SLEEP_T;
109 break;
111 // LIGHT_SLEEP_T: both WiFi modem AND CPU suspend between DTIM beacons.
112 // Most aggressive — prevents TCP processing during sleep. Matches ESP32's WIFI_PS_MAX_MODEM.
113 // See https://github.com/esphome/esphome/issues/14999
114 power_save = LIGHT_SLEEP_T;
115 break;
117 default:
118 power_save = NONE_SLEEP_T;
119 break;
120 }
121 wifi_fpm_auto_sleep_set_in_null_mode(1);
122 bool success = wifi_set_sleep_type(power_save);
123#ifdef USE_WIFI_POWER_SAVE_LISTENERS
124 if (success) {
125 for (auto *listener : this->power_save_listeners_) {
126 listener->on_wifi_power_save(this->power_save_);
127 }
128 }
129#endif
130 return success;
131}
132
133#if LWIP_VERSION_MAJOR != 1
134/*
135 lwip v2 needs to be notified of IP changes, see also
136 https://github.com/d-a-v/Arduino/blob/0e7d21e17144cfc5f53c016191daca8723e89ee8/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp#L251
137 */
138#undef netif_set_addr // need to call lwIP-v1.4 netif_set_addr()
139extern "C" {
140struct netif *eagle_lwip_getif(int netif_index);
141void netif_set_addr(struct netif *netif, const ip4_addr_t *ip, const ip4_addr_t *netmask, const ip4_addr_t *gw);
142};
143#endif
144
145bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
146 // enable STA
147 if (!this->wifi_mode_(true, {}))
148 return false;
149
150 enum dhcp_status dhcp_status = wifi_station_dhcpc_status();
151 if (!manual_ip.has_value()) {
152 // lwIP starts the SNTP client if it gets an SNTP server from DHCP. We don't need the time, and more importantly,
153 // the built-in SNTP client has a memory leak in certain situations. Disable this feature.
154 // https://github.com/esphome/issues/issues/2299
155 sntp_servermode_dhcp(false);
156
157 // Use DHCP client
158 if (dhcp_status != DHCP_STARTED) {
159 bool ret = wifi_station_dhcpc_start();
160 if (!ret) {
161 ESP_LOGV(TAG, "Starting DHCP client failed");
162 }
163 return ret;
164 }
165 return true;
166 }
167
168 bool ret = true;
169
170#if LWIP_VERSION_MAJOR != 1
171 // get current->previous IP address
172 // (check below)
173 ip_info previp{};
174 wifi_get_ip_info(STATION_IF, &previp);
175#endif
176
177 struct ip_info info {};
178 info.ip = manual_ip->static_ip;
179 info.gw = manual_ip->gateway;
180 info.netmask = manual_ip->subnet;
181
182 if (dhcp_status == DHCP_STARTED) {
183 bool dhcp_stop_ret = wifi_station_dhcpc_stop();
184 if (!dhcp_stop_ret) {
185 ESP_LOGV(TAG, "Stopping DHCP client failed");
186 ret = false;
187 }
188 }
189 bool wifi_set_info_ret = wifi_set_ip_info(STATION_IF, &info);
190 if (!wifi_set_info_ret) {
191 ESP_LOGV(TAG, "Set manual IP info failed");
192 ret = false;
193 }
194
195 ip_addr_t dns;
196 if (manual_ip->dns1.is_set()) {
197 dns = manual_ip->dns1;
198 dns_setserver(0, &dns);
199 }
200 if (manual_ip->dns2.is_set()) {
201 dns = manual_ip->dns2;
202 dns_setserver(1, &dns);
203 }
204
205#if LWIP_VERSION_MAJOR != 1
206 // trigger address change by calling lwIP-v1.4 api
207 // only when ip is already set by other mean (generally dhcp)
208 if (previp.ip.addr != 0 && previp.ip.addr != info.ip.addr) {
209 netif_set_addr(eagle_lwip_getif(STATION_IF), reinterpret_cast<const ip4_addr_t *>(&info.ip),
210 reinterpret_cast<const ip4_addr_t *>(&info.netmask), reinterpret_cast<const ip4_addr_t *>(&info.gw));
211 }
212#endif
213 return ret;
214}
215
217 if (!this->has_sta())
218 return {};
219 network::IPAddresses addresses;
220 uint8_t index = 0;
221 for (auto &addr : addrList) {
222 assert(index < addresses.size());
223 addresses[index++] = addr.ipFromNetifNum();
224 }
225 return addresses;
226}
228 const auto &hostname = App.get_name();
229 bool ret = wifi_station_set_hostname(const_cast<char *>(hostname.c_str()));
230 if (!ret) {
231 ESP_LOGV(TAG, "Set hostname failed");
232 }
233
234 // Update hostname on all lwIP interfaces so DHCP packets include it.
235 // lwIP includes the hostname in DHCP DISCOVER/REQUEST automatically
236 // via LWIP_NETIF_HOSTNAME — no dhcp_renew() needed. The hostname is
237 // fixed at compile time and never changes at runtime.
238 for (netif *intf = netif_list; intf; intf = intf->next) {
239#if LWIP_VERSION_MAJOR == 1
240 intf->hostname = (char *) wifi_station_get_hostname();
241#else
242 intf->hostname = wifi_station_get_hostname();
243#endif
244 }
245
246 return ret;
247}
248
250 // enable STA
251 if (!this->wifi_mode_(true, {}))
252 return false;
253
254 this->wifi_disconnect_();
255
256 struct station_config conf {};
257 memset(&conf, 0, sizeof(conf));
258 if (ap.ssid_.size() > sizeof(conf.ssid)) {
259 ESP_LOGE(TAG, "SSID too long");
260 return false;
261 }
262 if (ap.password_.size() > sizeof(conf.password)) {
263 ESP_LOGE(TAG, "Password too long");
264 return false;
265 }
266 memcpy(reinterpret_cast<char *>(conf.ssid), ap.ssid_.c_str(), ap.ssid_.size());
267 memcpy(reinterpret_cast<char *>(conf.password), ap.password_.c_str(), ap.password_.size());
268
269 if (ap.has_bssid()) {
270 conf.bssid_set = 1;
271 memcpy(conf.bssid, ap.get_bssid().data(), 6);
272 } else {
273 conf.bssid_set = 0;
274 }
275
276#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0)
277 if (ap.password_.empty()) {
278 conf.threshold.authmode = AUTH_OPEN;
279 } else {
280 // Set threshold based on configured minimum auth mode
281 // Note: ESP8266 doesn't support WPA3
282 switch (this->min_auth_mode_) {
284 conf.threshold.authmode = AUTH_WPA_PSK;
285 break;
287 case WIFI_MIN_AUTH_MODE_WPA3: // Fall back to WPA2 for ESP8266
288 conf.threshold.authmode = AUTH_WPA2_PSK;
289 break;
290 }
291 }
292 conf.threshold.rssi = -127;
293#endif
294
295 ETS_UART_INTR_DISABLE();
296 bool ret = wifi_station_set_config_current(&conf);
297 ETS_UART_INTR_ENABLE();
298
299 if (!ret) {
300 ESP_LOGV(TAG, "Set Station config failed");
301 return false;
302 }
303
304#ifdef USE_WIFI_MANUAL_IP
305 if (!this->wifi_sta_ip_config_(ap.get_manual_ip())) {
306 return false;
307 }
308#else
309 if (!this->wifi_sta_ip_config_({})) {
310 return false;
311 }
312#endif
313
314 // setup enterprise authentication if required
315#ifdef USE_WIFI_WPA2_EAP
316 const auto &eap_opt = ap.get_eap();
317 if (eap_opt.has_value()) {
318 // note: all certificates and keys have to be null terminated. Lengths are appended by +1 to include \0.
319 const EAPAuth &eap = *eap_opt;
320 ret = wifi_station_set_enterprise_identity((uint8_t *) eap.identity.c_str(), eap.identity.length());
321 if (ret) {
322 ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_identity failed: %d", ret);
323 }
324 int ca_cert_len = strlen(eap.ca_cert);
325 int client_cert_len = strlen(eap.client_cert);
326 int client_key_len = strlen(eap.client_key);
327 if (ca_cert_len) {
328 ret = wifi_station_set_enterprise_ca_cert((uint8_t *) eap.ca_cert, ca_cert_len + 1);
329 if (ret) {
330 ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_ca_cert failed: %d", ret);
331 }
332 }
333 // workout what type of EAP this is
334 // validation is not required as the config tool has already validated it
335 if (client_cert_len && client_key_len) {
336 // if we have certs, this must be EAP-TLS
337 ret = wifi_station_set_enterprise_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1,
338 (uint8_t *) eap.client_key, client_key_len + 1,
339 (uint8_t *) eap.password.c_str(), eap.password.length());
340 if (ret) {
341 ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_cert_key failed: %d", ret);
342 }
343 } else {
344 // in the absence of certs, assume this is username/password based
345 ret = wifi_station_set_enterprise_username((uint8_t *) eap.username.c_str(), eap.username.length());
346 if (ret) {
347 ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_username failed: %d", ret);
348 }
349 ret = wifi_station_set_enterprise_password((uint8_t *) eap.password.c_str(), eap.password.length());
350 if (ret) {
351 ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_set_password failed: %d", ret);
352 }
353 }
354 ret = wifi_station_set_wpa2_enterprise_auth(true);
355 if (ret) {
356 ESP_LOGV(TAG, "esp_wifi_sta_wpa2_ent_enable failed: %d", ret);
357 }
358 }
359#endif // USE_WIFI_WPA2_EAP
360
361 this->wifi_apply_hostname_();
362
363 // Reset flags, do this _before_ wifi_station_connect as the callback method
364 // may be called from wifi_station_connect
365 this->sta_state_ = static_cast<uint8_t>(ESP8266WiFiSTAState::CONNECTING);
366
367 ETS_UART_INTR_DISABLE();
368 ret = wifi_station_connect();
369 ETS_UART_INTR_ENABLE();
370 if (!ret) {
371 ESP_LOGV(TAG, "wifi_station_connect failed");
372 return false;
373 }
374
375#if USE_NETWORK_IPV6
376 bool connected = false;
377 while (!connected) {
378 uint8_t ipv6_addr_count = 0;
379 for (auto addr : addrList) {
380 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
381 ESP_LOGV(TAG, "Address %s", network::IPAddress(addr.ipFromNetifNum()).str_to(ip_buf));
382 if (addr.isV6()) {
383 ipv6_addr_count++;
384 }
385 }
386 delay(500); // NOLINT
387 connected = (ipv6_addr_count >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
388 }
389#endif /* USE_NETWORK_IPV6 */
390
391 if (ap.has_channel()) {
392 ret = wifi_set_channel(ap.get_channel());
393 if (!ret) {
394 ESP_LOGV(TAG, "wifi_set_channel failed");
395 return false;
396 }
397 }
398
399 return true;
400}
401
402class WiFiMockClass : public ESP8266WiFiGenericClass {
403 public:
404 static void _event_callback(void *event) { ESP8266WiFiGenericClass::_eventCallback(event); } // NOLINT
405};
406
407// Auth mode strings indexed by AUTH_* constants (0-4), with UNKNOWN at last index
408// Static asserts verify the SDK constants are contiguous as expected
409static_assert(AUTH_OPEN == 0 && AUTH_WEP == 1 && AUTH_WPA_PSK == 2 && AUTH_WPA2_PSK == 3 && AUTH_WPA_WPA2_PSK == 4,
410 "AUTH_* constants are not contiguous");
411PROGMEM_STRING_TABLE(AuthModeStrings, "OPEN", "WEP", "WPA PSK", "WPA2 PSK", "WPA/WPA2 PSK", "UNKNOWN");
412
413const LogString *get_auth_mode_str(uint8_t mode) {
414 return AuthModeStrings::get_log_str(mode, AuthModeStrings::LAST_INDEX);
415}
416
417// WiFi op mode strings indexed by WIFI_* constants (0-3), with UNKNOWN at last index
418static_assert(WIFI_OFF == 0 && WIFI_STA == 1 && WIFI_AP == 2 && WIFI_AP_STA == 3,
419 "WIFI_* op mode constants are not contiguous");
420PROGMEM_STRING_TABLE(OpModeStrings, "OFF", "STA", "AP", "AP+STA", "UNKNOWN");
421
422const LogString *get_op_mode_str(uint8_t mode) { return OpModeStrings::get_log_str(mode, OpModeStrings::LAST_INDEX); }
423
424// Use if-chain instead of switch to avoid jump tables in RODATA (wastes RAM on ESP8266).
425// A single switch would generate a sparse lookup table with ~175 default entries, wasting 700 bytes of RAM.
426// Even split switches still generate smaller jump tables in RODATA.
427const LogString *get_disconnect_reason_str(uint8_t reason) {
428 if (reason == REASON_AUTH_EXPIRE)
429 return LOG_STR("Auth Expired");
430 if (reason == REASON_AUTH_LEAVE)
431 return LOG_STR("Auth Leave");
432 if (reason == REASON_ASSOC_EXPIRE)
433 return LOG_STR("Association Expired");
434 if (reason == REASON_ASSOC_TOOMANY)
435 return LOG_STR("Too Many Associations");
436 if (reason == REASON_NOT_AUTHED)
437 return LOG_STR("Not Authenticated");
438 if (reason == REASON_NOT_ASSOCED)
439 return LOG_STR("Not Associated");
440 if (reason == REASON_ASSOC_LEAVE)
441 return LOG_STR("Association Leave");
442 if (reason == REASON_ASSOC_NOT_AUTHED)
443 return LOG_STR("Association not Authenticated");
444 if (reason == REASON_DISASSOC_PWRCAP_BAD)
445 return LOG_STR("Disassociate Power Cap Bad");
446 if (reason == REASON_DISASSOC_SUPCHAN_BAD)
447 return LOG_STR("Disassociate Supported Channel Bad");
448 if (reason == REASON_IE_INVALID)
449 return LOG_STR("IE Invalid");
450 if (reason == REASON_MIC_FAILURE)
451 return LOG_STR("Mic Failure");
452 if (reason == REASON_4WAY_HANDSHAKE_TIMEOUT)
453 return LOG_STR("4-Way Handshake Timeout");
454 if (reason == REASON_GROUP_KEY_UPDATE_TIMEOUT)
455 return LOG_STR("Group Key Update Timeout");
456 if (reason == REASON_IE_IN_4WAY_DIFFERS)
457 return LOG_STR("IE In 4-Way Handshake Differs");
458 if (reason == REASON_GROUP_CIPHER_INVALID)
459 return LOG_STR("Group Cipher Invalid");
460 if (reason == REASON_PAIRWISE_CIPHER_INVALID)
461 return LOG_STR("Pairwise Cipher Invalid");
462 if (reason == REASON_AKMP_INVALID)
463 return LOG_STR("AKMP Invalid");
464 if (reason == REASON_UNSUPP_RSN_IE_VERSION)
465 return LOG_STR("Unsupported RSN IE version");
466 if (reason == REASON_INVALID_RSN_IE_CAP)
467 return LOG_STR("Invalid RSN IE Cap");
468 if (reason == REASON_802_1X_AUTH_FAILED)
469 return LOG_STR("802.1x Authentication Failed");
470 if (reason == REASON_CIPHER_SUITE_REJECTED)
471 return LOG_STR("Cipher Suite Rejected");
472 if (reason == REASON_BEACON_TIMEOUT)
473 return LOG_STR("Beacon Timeout");
474 if (reason == REASON_NO_AP_FOUND)
475 return LOG_STR("AP Not Found");
476 if (reason == REASON_AUTH_FAIL)
477 return LOG_STR("Authentication Failed");
478 if (reason == REASON_ASSOC_FAIL)
479 return LOG_STR("Association Failed");
480 if (reason == REASON_HANDSHAKE_TIMEOUT)
481 return LOG_STR("Handshake Failed");
482 return LOG_STR("Unspecified");
483}
484
485void WiFiComponent::wifi_event_callback(System_Event_t *event) {
486 switch (event->event) {
487 case EVENT_STAMODE_CONNECTED: {
488 auto it = event->event_info.connected;
489#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
490 char bssid_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
491 format_mac_addr_upper(it.bssid, bssid_buf);
492 ESP_LOGV(TAG, "Connected ssid='%.*s' bssid=%s channel=%u", it.ssid_len, (const char *) it.ssid, bssid_buf,
493 it.channel);
494#endif
496#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
497 // Defer listener notification until state machine reaches STA_CONNECTED
498 // This ensures wifi.connected condition returns true in listener automations
500#endif
501 break;
502 }
503 case EVENT_STAMODE_DISCONNECTED: {
504 auto it = event->event_info.disconnected;
505 if (it.reason == REASON_NO_AP_FOUND) {
506 ESP_LOGW(TAG, "Disconnected ssid='%.*s' reason='Probe Request Unsuccessful'", it.ssid_len,
507 (const char *) it.ssid);
509 } else {
510 char bssid_s[18];
511 format_mac_addr_upper(it.bssid, bssid_s);
512 ESP_LOGW(TAG, "Disconnected ssid='%.*s' bssid=" LOG_SECRET("%s") " reason='%s'", it.ssid_len,
513 (const char *) it.ssid, bssid_s, LOG_STR_ARG(get_disconnect_reason_str(it.reason)));
515 }
517#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
519#endif
520 break;
521 }
522 case EVENT_STAMODE_AUTHMODE_CHANGE: {
523 auto it = event->event_info.auth_change;
524 ESP_LOGV(TAG, "Changed Authmode old=%s new=%s", LOG_STR_ARG(get_auth_mode_str(it.old_mode)),
525 LOG_STR_ARG(get_auth_mode_str(it.new_mode)));
526 // Mitigate CVE-2020-12638
527 // https://lbsfilm.at/blog/wpa2-authenticationmode-downgrade-in-espressif-microprocessors
528 if (it.old_mode != AUTH_OPEN && it.new_mode == AUTH_OPEN) {
529 ESP_LOGW(TAG, "Potential Authmode downgrade detected, disconnecting");
530 wifi_station_disconnect();
532 }
533 break;
534 }
535 case EVENT_STAMODE_GOT_IP: {
536 auto it = event->event_info.got_ip;
537 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE], gw_buf[network::IP_ADDRESS_BUFFER_SIZE],
538 mask_buf[network::IP_ADDRESS_BUFFER_SIZE];
539 ESP_LOGV(TAG, "static_ip=%s gateway=%s netmask=%s", network::IPAddress(&it.ip).str_to(ip_buf),
540 network::IPAddress(&it.gw).str_to(gw_buf), network::IPAddress(&it.mask).str_to(mask_buf));
542#ifdef USE_WIFI_IP_STATE_LISTENERS
543 // Defer listener callbacks to main loop - system context has limited stack
545#endif
546 break;
547 }
548 case EVENT_STAMODE_DHCP_TIMEOUT: {
549 ESP_LOGW(TAG, "DHCP request timeout");
550 break;
551 }
552 case EVENT_SOFTAPMODE_STACONNECTED: {
553#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
554 auto it = event->event_info.sta_connected;
555 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
556 format_mac_addr_upper(it.mac, mac_buf);
557 ESP_LOGV(TAG, "AP client connected MAC=%s aid=%u", mac_buf, it.aid);
558#endif
559 break;
560 }
561 case EVENT_SOFTAPMODE_STADISCONNECTED: {
562#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
563 auto it = event->event_info.sta_disconnected;
564 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
565 format_mac_addr_upper(it.mac, mac_buf);
566 ESP_LOGV(TAG, "AP client disconnected MAC=%s aid=%u", mac_buf, it.aid);
567#endif
568 break;
569 }
570 case EVENT_SOFTAPMODE_PROBEREQRECVED: {
571#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
572 auto it = event->event_info.ap_probereqrecved;
573 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
574 format_mac_addr_upper(it.mac, mac_buf);
575 ESP_LOGVV(TAG, "AP receive Probe Request MAC=%s RSSI=%d", mac_buf, it.rssi);
576#endif
577 break;
578 }
579#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0)
580 case EVENT_OPMODE_CHANGED: {
581 auto it = event->event_info.opmode_changed;
582 ESP_LOGV(TAG, "Changed Mode old=%s new=%s", LOG_STR_ARG(get_op_mode_str(it.old_opmode)),
583 LOG_STR_ARG(get_op_mode_str(it.new_opmode)));
584 break;
585 }
586 case EVENT_SOFTAPMODE_DISTRIBUTE_STA_IP: {
587#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
588 auto it = event->event_info.distribute_sta_ip;
589 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
590 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
591 format_mac_addr_upper(it.mac, mac_buf);
592 ESP_LOGV(TAG, "AP Distribute Station IP MAC=%s IP=%s aid=%u", mac_buf, network::IPAddress(&it.ip).str_to(ip_buf),
593 it.aid);
594#endif
595 break;
596 }
597#endif
598 default:
599 break;
600 }
601
602 WiFiMockClass::_event_callback(event);
603}
604
606 uint8_t val = static_cast<uint8_t>(output_power * 4);
607 system_phy_set_max_tpw(val);
608 return true;
609}
611 if (!this->wifi_mode_(true, {}))
612 return false;
613
614 bool ret1, ret2;
615 ETS_UART_INTR_DISABLE();
616 ret1 = wifi_station_set_auto_connect(0);
617 ret2 = wifi_station_set_reconnect_policy(false);
618 ETS_UART_INTR_ENABLE();
619
620 if (!ret1 || !ret2) {
621 ESP_LOGV(TAG, "Disabling Auto-Connect failed");
622 }
623
624#ifdef USE_WIFI_PHY_MODE
625 if (!this->wifi_apply_phy_mode_()) {
626 ESP_LOGV(TAG, "Setting PHY Mode failed");
627 }
628#endif
629
630 delay(10);
631 return true;
632}
633
634#ifdef USE_WIFI_PHY_MODE
637 return true;
638 // Values of WiFi8266PhyMode are aligned with the SDK's phy_mode_t enum.
639 return wifi_set_phy_mode(static_cast<phy_mode_t>(this->phy_mode_));
640}
641#endif
642
644 wifi_set_event_handler_cb(&WiFiComponent::wifi_event_callback);
645
646 // Make sure WiFi is in clean state before anything starts
647 this->wifi_mode_(false, false);
648}
649
651 // Use cached state from wifi_event_callback() instead of calling
652 // wifi_station_get_connect_status() which queries the SDK every time.
653 // Use if statements with early returns instead of switch to avoid GCC
654 // generating a CSWTCH lookup table in .rodata (flash) on ESP8266.
655 auto state = static_cast<ESP8266WiFiSTAState>(this->sta_state_);
665}
666
668 // enable STA
669 if (!this->wifi_mode_(true, {}))
670 return false;
671
672 // Reset scan_done_ before starting new scan to prevent stale flag from previous scan
673 // (e.g., roaming scan completed just before unexpected disconnect)
674 this->scan_done_ = false;
675
676 struct scan_config config {};
677 memset(&config, 0, sizeof(config));
678 config.ssid = nullptr;
679 config.bssid = nullptr;
680 config.channel = 0;
681 config.show_hidden = 1;
682#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 4, 0)
683 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
684 // Use shorter dwell times for roaming scans - we only need to detect strong
685 // nearby APs, not do a thorough survey. This also reduces off-channel time
686 // which can cause Beacon Timeout disconnects on some APs.
687 // Roaming times match the ESP32 IDF scan defaults.
688 static constexpr uint32_t SCAN_PASSIVE_DEFAULT_MS = 500;
689 static constexpr uint32_t SCAN_PASSIVE_ROAMING_MS = 300;
690 static constexpr uint32_t SCAN_ACTIVE_MIN_DEFAULT_MS = 400;
691 static constexpr uint32_t SCAN_ACTIVE_MAX_DEFAULT_MS = 500;
692 static constexpr uint32_t SCAN_ACTIVE_MIN_ROAMING_MS = 100;
693 static constexpr uint32_t SCAN_ACTIVE_MAX_ROAMING_MS = 300;
694 bool roaming = this->roaming_state_ == RoamingState::SCANNING;
695 if (passive) {
696 config.scan_time.passive = roaming ? SCAN_PASSIVE_ROAMING_MS : SCAN_PASSIVE_DEFAULT_MS;
697 } else {
698 config.scan_time.active.min = roaming ? SCAN_ACTIVE_MIN_ROAMING_MS : SCAN_ACTIVE_MIN_DEFAULT_MS;
699 config.scan_time.active.max = roaming ? SCAN_ACTIVE_MAX_ROAMING_MS : SCAN_ACTIVE_MAX_DEFAULT_MS;
700 }
701#endif
702 bool ret = wifi_station_scan(&config, &WiFiComponent::s_wifi_scan_done_callback);
703 if (!ret) {
704 ESP_LOGV(TAG, "wifi_station_scan failed");
705 return false;
706 }
707
708 return ret;
709}
711 bool ret = true;
712 // Only call disconnect if interface is up
713 if (wifi_get_opmode() & WIFI_STA)
714 ret = wifi_station_disconnect();
715 station_config conf{};
716 memset(&conf, 0, sizeof(conf));
717 ETS_UART_INTR_DISABLE();
718 wifi_station_set_config_current(&conf);
719 ETS_UART_INTR_ENABLE();
720 return ret;
721}
725
726void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) {
727 this->scan_result_.clear();
728
729 if (status != OK) {
730 ESP_LOGV(TAG, "Scan failed: %d", status);
731 // Don't call retry_connect() here - this callback runs in SDK system context
732 // where yield() cannot be called. Instead, just set scan_done_ and let
733 // check_scanning_finished() handle the empty scan_result_ from loop context.
734 this->scan_done_ = true;
735 return;
736 }
737
738 auto *head = reinterpret_cast<bss_info *>(arg);
739 bool needs_full = this->needs_full_scan_results_();
740
741 // First pass: count matching networks (linked list is non-destructive)
742 size_t total = 0;
743 size_t count = 0;
744 for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) {
745 total++;
746 const char *ssid_cstr = reinterpret_cast<const char *>(it->ssid);
747 if (needs_full || this->matches_configured_network_(ssid_cstr, it->bssid)) {
748 count++;
749 }
750 }
751
752 this->scan_result_.init(count); // Exact allocation
753
754 // Second pass: store matching networks
755 for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) {
756 const char *ssid_cstr = reinterpret_cast<const char *>(it->ssid);
757 if (needs_full || this->matches_configured_network_(ssid_cstr, it->bssid)) {
758 this->scan_result_.emplace_back(
759 bssid_t{it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, ssid_cstr,
760 it->ssid_len, it->channel, it->rssi, it->authmode != AUTH_OPEN, it->is_hidden != 0);
761 } else {
762 this->log_discarded_scan_result_(ssid_cstr, it->bssid, it->rssi, it->channel);
763 }
764 }
765 ESP_LOGV(TAG, "Scan complete: %zu found, %zu stored%s", total, this->scan_result_.size(),
766 needs_full ? LOG_STR_LITERAL("") : LOG_STR_LITERAL(" (filtered)"));
767 this->scan_done_ = true;
768#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
769 this->pending_.scan_complete = true; // Defer listener callbacks to main loop
770#endif
771}
772
773#ifdef USE_WIFI_AP
774bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
775 // enable AP
776 if (!this->wifi_mode_({}, true))
777 return false;
778
779 struct ip_info info {};
780 if (manual_ip.has_value()) {
781 info.ip = manual_ip->static_ip;
782 info.gw = manual_ip->gateway;
783 info.netmask = manual_ip->subnet;
784 } else {
785 info.ip = network::IPAddress(192, 168, 4, 1);
786 info.gw = network::IPAddress(192, 168, 4, 1);
787 info.netmask = network::IPAddress(255, 255, 255, 0);
788 }
789
790 if (wifi_softap_dhcps_status() == DHCP_STARTED) {
791 if (!wifi_softap_dhcps_stop()) {
792 ESP_LOGW(TAG, "Stopping DHCP server failed");
793 }
794 }
795
796 if (!wifi_set_ip_info(SOFTAP_IF, &info)) {
797 ESP_LOGE(TAG, "Set SoftAP info failed");
798 return false;
799 }
800
801#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 0, 0) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(3, 1, 0)
802 dhcpSoftAP.begin(&info);
803#endif
804
805 struct dhcps_lease lease {};
806 lease.enable = true;
807 network::IPAddress start_address = network::IPAddress(&info.ip);
808 start_address += 99;
809 lease.start_ip = start_address;
810#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
811 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
812#endif
813 ESP_LOGV(TAG, "DHCP server IP lease start: %s", start_address.str_to(ip_buf));
814 start_address += 10;
815 lease.end_ip = start_address;
816 ESP_LOGV(TAG, "DHCP server IP lease end: %s", start_address.str_to(ip_buf));
817 if (!wifi_softap_set_dhcps_lease(&lease)) {
818 ESP_LOGE(TAG, "Set SoftAP DHCP lease failed");
819 return false;
820 }
821
822 // lease time 1440 minutes (=24 hours)
823 if (!wifi_softap_set_dhcps_lease_time(1440)) {
824 ESP_LOGE(TAG, "Set SoftAP DHCP lease time failed");
825 return false;
826 }
827
828#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(3, 1, 0)
829 ESP8266WiFiClass::softAPDhcpServer().setRouter(true); // send ROUTER option with netif's gateway IP
830#else
831 uint8_t mode = 1;
832 // bit0, 1 enables router information from ESP8266 SoftAP DHCP server.
833 if (!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
834 ESP_LOGE(TAG, "wifi_softap_set_dhcps_offer_option failed");
835 return false;
836 }
837#endif
838
839 if (!wifi_softap_dhcps_start()) {
840 ESP_LOGE(TAG, "Starting SoftAP DHCPS failed");
841 return false;
842 }
843
844 return true;
845}
846
848 // enable AP
849 if (!this->wifi_mode_({}, true))
850 return false;
851
852 struct softap_config conf {};
853 if (ap.ssid_.size() > sizeof(conf.ssid)) {
854 ESP_LOGE(TAG, "AP SSID too long");
855 return false;
856 }
857 memcpy(reinterpret_cast<char *>(conf.ssid), ap.ssid_.c_str(), ap.ssid_.size());
858 conf.ssid_len = static_cast<uint8>(ap.ssid_.size());
859 conf.channel = ap.has_channel() ? ap.get_channel() : 1;
860 conf.ssid_hidden = ap.get_hidden();
861 conf.max_connection = 5;
862 conf.beacon_interval = 100;
863
864 if (ap.password_.empty()) {
865 conf.authmode = AUTH_OPEN;
866 *conf.password = 0;
867 } else {
868 conf.authmode = AUTH_WPA2_PSK;
869 if (ap.password_.size() > sizeof(conf.password)) {
870 ESP_LOGE(TAG, "AP password too long");
871 return false;
872 }
873 memcpy(reinterpret_cast<char *>(conf.password), ap.password_.c_str(), ap.password_.size());
874 }
875
876 ETS_UART_INTR_DISABLE();
877 bool ret = wifi_softap_set_config_current(&conf);
878 ETS_UART_INTR_ENABLE();
879
880 if (!ret) {
881 ESP_LOGV(TAG, "wifi_softap_set_config_current failed");
882 return false;
883 }
884
885#ifdef USE_WIFI_MANUAL_IP
886 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
887 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
888 return false;
889 }
890#else
891 if (!this->wifi_ap_ip_config_({})) {
892 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
893 return false;
894 }
895#endif
896
897 return true;
898}
899
901 struct ip_info ip {};
902 wifi_get_ip_info(SOFTAP_IF, &ip);
903 return network::IPAddress(&ip.ip);
904}
905#endif // USE_WIFI_AP
906
908 bssid_t bssid{};
909 struct station_config conf {};
910 if (wifi_station_get_config(&conf)) {
911 std::copy_n(conf.bssid, bssid.size(), bssid.begin());
912 }
913 return bssid;
914}
915std::string WiFiComponent::wifi_ssid() {
916 struct station_config conf {};
917 if (!wifi_station_get_config(&conf)) {
918 return "";
919 }
920 // conf.ssid is uint8[32], not null-terminated if full
921 auto *ssid_s = reinterpret_cast<const char *>(conf.ssid);
922 size_t len = strnlen(ssid_s, sizeof(conf.ssid));
923 return {ssid_s, len};
924}
925const char *WiFiComponent::wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer) {
926 struct station_config conf {};
927 if (!wifi_station_get_config(&conf)) {
928 buffer[0] = '\0';
929 return buffer.data();
930 }
931 // conf.ssid is uint8[32], not null-terminated if full
932 size_t len = strnlen(reinterpret_cast<const char *>(conf.ssid), sizeof(conf.ssid));
933 memcpy(buffer.data(), conf.ssid, len);
934 buffer[len] = '\0';
935 return buffer.data();
936}
938 if (wifi_station_get_connect_status() != STATION_GOT_IP)
939 return WIFI_RSSI_DISCONNECTED;
940 sint8 rssi = wifi_station_get_rssi();
941 // Values >= 31 are error codes per NONOS SDK API, not valid RSSI readings
942 return rssi >= 31 ? WIFI_RSSI_DISCONNECTED : rssi;
943}
944int32_t WiFiComponent::get_wifi_channel() { return wifi_get_channel(); }
946 struct ip_info ip {};
947 wifi_get_ip_info(STATION_IF, &ip);
948 return network::IPAddress(&ip.netmask);
949}
951 struct ip_info ip {};
952 wifi_get_ip_info(STATION_IF, &ip);
953 return network::IPAddress(&ip.gw);
954}
958 return true;
959}
960
962 // Process callbacks deferred from ESP8266 SDK system context (~2KB stack)
963 // to main loop context (full stack). Connect state listeners are handled
964 // by notify_connect_state_listeners_() in the shared state machine code.
965
966#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
967 if (this->pending_.disconnect) {
968 this->pending_.disconnect = false;
969 // Refresh is_connected() cache here, not in the SDK callback (sys context).
972 }
973#endif
974
975#ifdef USE_WIFI_IP_STATE_LISTENERS
976 if (this->pending_.got_ip) {
977 this->pending_.got_ip = false;
979 }
980#endif
981
982#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
983 if (this->pending_.scan_complete) {
984 this->pending_.scan_complete = false;
986 }
987#endif
988}
989
990} // namespace esphome::wifi
991#endif
992#endif
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
const char * c_str() const
uint8_t get_channel() const
const optional< EAPAuth > & get_eap() const
const optional< ManualIP > & get_manual_ip() const
const bssid_t & get_bssid() const
void wifi_scan_done_callback_(void *arg, STATUS status)
void notify_scan_results_listeners_()
Notify scan results listeners with current scan results.
WiFiSTAConnectStatus wifi_sta_connect_status_() const
wifi_scan_vector_t< WiFiScanResult > scan_result_
struct esphome::wifi::WiFiComponent::@190 pending_
void notify_ip_state_listeners_()
Notify IP state listeners with current addresses.
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
static void wifi_event_callback(System_Event_t *event)
void notify_disconnect_state_listeners_()
Notify connect state listeners of disconnection.
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.
static void s_wifi_scan_done_callback(void *arg, STATUS status)
network::IPAddress wifi_dns_ip_(int num)
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...
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
bool needs_full_scan_results_() const
Check if full scan results are needed (captive portal active, improv, listeners)
StaticVector< WiFiPowerSaveListener *, ESPHOME_WIFI_POWER_SAVE_LISTENERS > power_save_listeners_
bool wifi_apply_output_power_(float output_power)
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
network::IPAddresses wifi_sta_ip_addresses()
bool state
Definition fan.h:2
in_addr ip_addr_t
Definition ip_address.h:22
in_addr ip4_addr_t
Definition ip_address.h:23
mopeka_std_values val[3]
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:223
const char *const TAG
Definition spi.cpp:7
std::array< uint8_t, 6 > bssid_t
const LogString * get_auth_mode_str(uint8_t mode)
const LogString * get_disconnect_reason_str(uint8_t reason)
struct netif * eagle_lwip_getif(int netif_index)
void netif_set_addr(struct netif *netif, const ip4_addr_t *ip, const ip4_addr_t *netmask, const ip4_addr_t *gw)
WiFiComponent * global_wifi_component
PROGMEM_STRING_TABLE(AuthModeStrings, "OPEN", "WEP", "WPA PSK", "WPA2 PSK", "WPA/WPA2 PSK", "UNKNOWN")
@ SCANNING
Scanning for better AP.
const LogString * get_op_mode_str(uint8_t mode)
const void size_t len
Definition hal.h:64
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1453
static void uint32_t