ESPHome 2025.11.0b4
Loading...
Searching...
No Matches
wifi_component_esp_idf.cpp
Go to the documentation of this file.
1#include "wifi_component.h"
2
3#ifdef USE_WIFI
4#ifdef USE_ESP32
5
6#include <esp_event.h>
7#include <esp_netif.h>
8#include <esp_system.h>
9#include <esp_wifi.h>
10#include <esp_wifi_types.h>
11#include <freertos/FreeRTOS.h>
12#include <freertos/event_groups.h>
13#include <freertos/task.h>
14
15#include <algorithm>
16#include <cinttypes>
17#include <utility>
18#ifdef USE_WIFI_WPA2_EAP
19#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
20#include <esp_eap_client.h>
21#else
22#include <esp_wpa2.h>
23#endif
24#endif
25
26#ifdef USE_WIFI_AP
27#include "dhcpserver/dhcpserver.h"
28#endif // USE_WIFI_AP
29
30#ifdef USE_CAPTIVE_PORTAL
32#endif
33
34#include "lwip/apps/sntp.h"
35#include "lwip/dns.h"
36#include "lwip/err.h"
37
39#include "esphome/core/hal.h"
41#include "esphome/core/log.h"
42#include "esphome/core/util.h"
43
44namespace esphome {
45namespace wifi {
46
47static const char *const TAG = "wifi_esp32";
48
49static EventGroupHandle_t s_wifi_event_group; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
50static QueueHandle_t s_event_queue; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
51static esp_netif_t *s_sta_netif = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
52#ifdef USE_WIFI_AP
53static esp_netif_t *s_ap_netif = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
54#endif // USE_WIFI_AP
55static bool s_sta_started = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
56static bool s_sta_connected = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
57static bool s_ap_started = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
58static bool s_sta_connect_not_found = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
59static bool s_sta_connect_error = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
60static bool s_sta_connecting = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
61static bool s_wifi_started = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
62
63struct IDFWiFiEvent {
64 esp_event_base_t event_base;
65 int32_t event_id;
66 union {
67 wifi_event_sta_scan_done_t sta_scan_done;
68 wifi_event_sta_connected_t sta_connected;
69 wifi_event_sta_disconnected_t sta_disconnected;
70 wifi_event_sta_authmode_change_t sta_authmode_change;
71 wifi_event_ap_staconnected_t ap_staconnected;
72 wifi_event_ap_stadisconnected_t ap_stadisconnected;
73 wifi_event_ap_probe_req_rx_t ap_probe_req_rx;
74 wifi_event_bss_rssi_low_t bss_rssi_low;
75 ip_event_got_ip_t ip_got_ip;
76#if USE_NETWORK_IPV6
77 ip_event_got_ip6_t ip_got_ip6;
78#endif /* USE_NETWORK_IPV6 */
79 ip_event_ap_staipassigned_t ip_ap_staipassigned;
80 } data;
81};
82
83// general design: event handler translates events and pushes them to a queue,
84// events get processed in the main loop
85void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
86 IDFWiFiEvent event;
87 memset(&event, 0, sizeof(IDFWiFiEvent));
88 event.event_base = event_base;
89 event.event_id = event_id;
90 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { // NOLINT(bugprone-branch-clone)
91 // no data
92 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_STOP) { // NOLINT(bugprone-branch-clone)
93 // no data
94 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
95 memcpy(&event.data.sta_authmode_change, event_data, sizeof(wifi_event_sta_authmode_change_t));
96 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
97 memcpy(&event.data.sta_connected, event_data, sizeof(wifi_event_sta_connected_t));
98 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
99 memcpy(&event.data.sta_disconnected, event_data, sizeof(wifi_event_sta_disconnected_t));
100 } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
101 memcpy(&event.data.ip_got_ip, event_data, sizeof(ip_event_got_ip_t));
102#if USE_NETWORK_IPV6
103 } else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
104 memcpy(&event.data.ip_got_ip6, event_data, sizeof(ip_event_got_ip6_t));
105#endif
106 } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { // NOLINT(bugprone-branch-clone)
107 // no data
108 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
109 memcpy(&event.data.sta_scan_done, event_data, sizeof(wifi_event_sta_scan_done_t));
110 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) { // NOLINT(bugprone-branch-clone)
111 // no data
112 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STOP) { // NOLINT(bugprone-branch-clone)
113 // no data
114 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
115 memcpy(&event.data.ap_probe_req_rx, event_data, sizeof(wifi_event_ap_probe_req_rx_t));
116 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
117 memcpy(&event.data.ap_staconnected, event_data, sizeof(wifi_event_ap_staconnected_t));
118 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
119 memcpy(&event.data.ap_stadisconnected, event_data, sizeof(wifi_event_ap_stadisconnected_t));
120 } else if (event_base == IP_EVENT && event_id == IP_EVENT_AP_STAIPASSIGNED) {
121 memcpy(&event.data.ip_ap_staipassigned, event_data, sizeof(ip_event_ap_staipassigned_t));
122 } else {
123 // did not match any event, don't send anything
124 return;
125 }
126
127 // copy to heap to keep queue object small
128 auto *to_send = new IDFWiFiEvent; // NOLINT(cppcoreguidelines-owning-memory)
129 memcpy(to_send, &event, sizeof(IDFWiFiEvent));
130 // don't block, we may miss events but the core can handle that
131 if (xQueueSend(s_event_queue, &to_send, 0L) != pdPASS) {
132 delete to_send; // NOLINT(cppcoreguidelines-owning-memory)
133 }
134}
135
137 uint8_t mac[6];
140 set_mac_address(mac);
141 }
142 esp_err_t err = esp_netif_init();
143 if (err != ERR_OK) {
144 ESP_LOGE(TAG, "esp_netif_init failed: %s", esp_err_to_name(err));
145 return;
146 }
147 s_wifi_event_group = xEventGroupCreate();
148 if (s_wifi_event_group == nullptr) {
149 ESP_LOGE(TAG, "xEventGroupCreate failed");
150 return;
151 }
152 // NOLINTNEXTLINE(bugprone-sizeof-expression)
153 s_event_queue = xQueueCreate(64, sizeof(IDFWiFiEvent *));
154 if (s_event_queue == nullptr) {
155 ESP_LOGE(TAG, "xQueueCreate failed");
156 return;
157 }
158 err = esp_event_loop_create_default();
159 if (err != ERR_OK) {
160 ESP_LOGE(TAG, "esp_event_loop_create_default failed: %s", esp_err_to_name(err));
161 return;
162 }
163 esp_event_handler_instance_t instance_wifi_id, instance_ip_id;
164 err = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, nullptr, &instance_wifi_id);
165 if (err != ERR_OK) {
166 ESP_LOGE(TAG, "esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
167 return;
168 }
169 err = esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, nullptr, &instance_ip_id);
170 if (err != ERR_OK) {
171 ESP_LOGE(TAG, "esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
172 return;
173 }
174
175 s_sta_netif = esp_netif_create_default_wifi_sta();
176
177#ifdef USE_WIFI_AP
178 s_ap_netif = esp_netif_create_default_wifi_ap();
179#endif // USE_WIFI_AP
180
181 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
182 // cfg.nvs_enable = false;
183 err = esp_wifi_init(&cfg);
184 if (err != ERR_OK) {
185 ESP_LOGE(TAG, "esp_wifi_init failed: %s", esp_err_to_name(err));
186 return;
187 }
188 err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
189 if (err != ERR_OK) {
190 ESP_LOGE(TAG, "esp_wifi_set_storage failed: %s", esp_err_to_name(err));
191 return;
192 }
193}
194
195bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
196 esp_err_t err;
197 wifi_mode_t current_mode = WIFI_MODE_NULL;
198 if (s_wifi_started) {
199 err = esp_wifi_get_mode(&current_mode);
200 if (err != ERR_OK) {
201 ESP_LOGW(TAG, "esp_wifi_get_mode failed: %s", esp_err_to_name(err));
202 return false;
203 }
204 }
205 bool current_sta = current_mode == WIFI_MODE_STA || current_mode == WIFI_MODE_APSTA;
206 bool current_ap = current_mode == WIFI_MODE_AP || current_mode == WIFI_MODE_APSTA;
207
208 bool set_sta = sta.value_or(current_sta);
209 bool set_ap = ap.value_or(current_ap);
210
211 wifi_mode_t set_mode;
212 if (set_sta && set_ap) {
213 set_mode = WIFI_MODE_APSTA;
214 } else if (set_sta && !set_ap) {
215 set_mode = WIFI_MODE_STA;
216 } else if (!set_sta && set_ap) {
217 set_mode = WIFI_MODE_AP;
218 } else {
219 set_mode = WIFI_MODE_NULL;
220 }
221
222 if (current_mode == set_mode)
223 return true;
224
225 if (set_sta && !current_sta) {
226 ESP_LOGV(TAG, "Enabling STA");
227 } else if (!set_sta && current_sta) {
228 ESP_LOGV(TAG, "Disabling STA");
229 }
230 if (set_ap && !current_ap) {
231 ESP_LOGV(TAG, "Enabling AP");
232 } else if (!set_ap && current_ap) {
233 ESP_LOGV(TAG, "Disabling AP");
234 }
235
236 if (set_mode == WIFI_MODE_NULL && s_wifi_started) {
237 err = esp_wifi_stop();
238 if (err != ESP_OK) {
239 ESP_LOGV(TAG, "esp_wifi_stop failed: %s", esp_err_to_name(err));
240 return false;
241 }
242 s_wifi_started = false;
243 return true;
244 }
245
246 err = esp_wifi_set_mode(set_mode);
247 if (err != ERR_OK) {
248 ESP_LOGW(TAG, "esp_wifi_set_mode failed: %s", esp_err_to_name(err));
249 return false;
250 }
251
252 if (set_mode != WIFI_MODE_NULL && !s_wifi_started) {
253 err = esp_wifi_start();
254 if (err != ESP_OK) {
255 ESP_LOGV(TAG, "esp_wifi_start failed: %s", esp_err_to_name(err));
256 return false;
257 }
258 s_wifi_started = true;
259 }
260
261 return true;
262}
263
264bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
265
266bool WiFiComponent::wifi_apply_output_power_(float output_power) {
267 int8_t val = static_cast<int8_t>(output_power * 4);
268 return esp_wifi_set_max_tx_power(val) == ESP_OK;
269}
270
272 wifi_ps_type_t power_save;
273 switch (this->power_save_) {
275 power_save = WIFI_PS_MIN_MODEM;
276 break;
278 power_save = WIFI_PS_MAX_MODEM;
279 break;
281 default:
282 power_save = WIFI_PS_NONE;
283 break;
284 }
285 return esp_wifi_set_ps(power_save) == ESP_OK;
286}
287
288bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
289 // enable STA
290 if (!this->wifi_mode_(true, {}))
291 return false;
292
293 // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv417wifi_sta_config_t
294 wifi_config_t conf;
295 memset(&conf, 0, sizeof(conf));
296 if (ap.get_ssid().size() > sizeof(conf.sta.ssid)) {
297 ESP_LOGE(TAG, "SSID too long");
298 return false;
299 }
300 if (ap.get_password().size() > sizeof(conf.sta.password)) {
301 ESP_LOGE(TAG, "Password too long");
302 return false;
303 }
304 memcpy(reinterpret_cast<char *>(conf.sta.ssid), ap.get_ssid().c_str(), ap.get_ssid().size());
305 memcpy(reinterpret_cast<char *>(conf.sta.password), ap.get_password().c_str(), ap.get_password().size());
306
307 // The weakest authmode to accept in the fast scan mode
308 if (ap.get_password().empty()) {
309 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
310 } else {
311 // Set threshold based on configured minimum auth mode
312 switch (this->min_auth_mode_) {
314 conf.sta.threshold.authmode = WIFI_AUTH_WPA_PSK;
315 break;
317 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
318 break;
320 conf.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK;
321 break;
322 }
323 }
324
325#ifdef USE_WIFI_WPA2_EAP
326 if (ap.get_eap().has_value()) {
327 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_ENTERPRISE;
328 }
329#endif
330
331#ifdef USE_WIFI_11KV_SUPPORT
332 conf.sta.btm_enabled = this->btm_;
333 conf.sta.rm_enabled = this->rrm_;
334#endif
335
336 if (ap.get_bssid().has_value()) {
337 conf.sta.bssid_set = true;
338 memcpy(conf.sta.bssid, ap.get_bssid()->data(), 6);
339 } else {
340 conf.sta.bssid_set = false;
341 }
342 if (ap.get_channel().has_value()) {
343 conf.sta.channel = *ap.get_channel();
344 conf.sta.scan_method = WIFI_FAST_SCAN;
345 } else {
346 conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
347 }
348 // Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set.
349 // Units: AP beacon intervals. Defaults to 3 if set to 0.
350 conf.sta.listen_interval = 0;
351
352 // Protected Management Frame
353 // Device will prefer to connect in PMF mode if other device also advertises PMF capability.
354 conf.sta.pmf_cfg.capable = true;
355 conf.sta.pmf_cfg.required = false;
356
357 // note, we do our own filtering
358 // The minimum rssi to accept in the fast scan mode
359 conf.sta.threshold.rssi = -127;
360
361 wifi_config_t current_conf;
362 esp_err_t err;
363 err = esp_wifi_get_config(WIFI_IF_STA, &current_conf);
364 if (err != ERR_OK) {
365 ESP_LOGW(TAG, "esp_wifi_get_config failed: %s", esp_err_to_name(err));
366 // can continue
367 }
368
369 if (memcmp(&current_conf, &conf, sizeof(wifi_config_t)) != 0) { // NOLINT
370 err = esp_wifi_disconnect();
371 if (err != ESP_OK) {
372 ESP_LOGV(TAG, "esp_wifi_disconnect failed: %s", esp_err_to_name(err));
373 return false;
374 }
375 }
376
377 err = esp_wifi_set_config(WIFI_IF_STA, &conf);
378 if (err != ESP_OK) {
379 ESP_LOGV(TAG, "esp_wifi_set_config failed: %s", esp_err_to_name(err));
380 return false;
381 }
382
383#ifdef USE_WIFI_MANUAL_IP
384 if (!this->wifi_sta_ip_config_(ap.get_manual_ip())) {
385 return false;
386 }
387#else
388 if (!this->wifi_sta_ip_config_({})) {
389 return false;
390 }
391#endif
392
393 // setup enterprise authentication if required
394#ifdef USE_WIFI_WPA2_EAP
395 if (ap.get_eap().has_value()) {
396 // note: all certificates and keys have to be null terminated. Lengths are appended by +1 to include \0.
397 EAPAuth eap = ap.get_eap().value();
398#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
399 err = esp_eap_client_set_identity((uint8_t *) eap.identity.c_str(), eap.identity.length());
400#else
401 err = esp_wifi_sta_wpa2_ent_set_identity((uint8_t *) eap.identity.c_str(), eap.identity.length());
402#endif
403 if (err != ESP_OK) {
404 ESP_LOGV(TAG, "set_identity failed %d", err);
405 }
406 int ca_cert_len = strlen(eap.ca_cert);
407 int client_cert_len = strlen(eap.client_cert);
408 int client_key_len = strlen(eap.client_key);
409 if (ca_cert_len) {
410#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
411 err = esp_eap_client_set_ca_cert((uint8_t *) eap.ca_cert, ca_cert_len + 1);
412#else
413 err = esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *) eap.ca_cert, ca_cert_len + 1);
414#endif
415 if (err != ESP_OK) {
416 ESP_LOGV(TAG, "set_ca_cert failed %d", err);
417 }
418 }
419 // workout what type of EAP this is
420 // validation is not required as the config tool has already validated it
421 if (client_cert_len && client_key_len) {
422 // if we have certs, this must be EAP-TLS
423#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
424 err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.client_cert, client_cert_len + 1,
425 (uint8_t *) eap.client_key, client_key_len + 1,
426 (uint8_t *) eap.password.c_str(), eap.password.length());
427#else
428 err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1,
429 (uint8_t *) eap.client_key, client_key_len + 1,
430 (uint8_t *) eap.password.c_str(), eap.password.length());
431#endif
432 if (err != ESP_OK) {
433 ESP_LOGV(TAG, "set_cert_key failed %d", err);
434 }
435 } else {
436 // in the absence of certs, assume this is username/password based
437#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
438 err = esp_eap_client_set_username((uint8_t *) eap.username.c_str(), eap.username.length());
439#else
440 err = esp_wifi_sta_wpa2_ent_set_username((uint8_t *) eap.username.c_str(), eap.username.length());
441#endif
442 if (err != ESP_OK) {
443 ESP_LOGV(TAG, "set_username failed %d", err);
444 }
445#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
446 err = esp_eap_client_set_password((uint8_t *) eap.password.c_str(), eap.password.length());
447#else
448 err = esp_wifi_sta_wpa2_ent_set_password((uint8_t *) eap.password.c_str(), eap.password.length());
449#endif
450 if (err != ESP_OK) {
451 ESP_LOGV(TAG, "set_password failed %d", err);
452 }
453 // set TTLS Phase 2, defaults to MSCHAPV2
454#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
455 err = esp_eap_client_set_ttls_phase2_method(eap.ttls_phase_2);
456#else
457 err = esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(eap.ttls_phase_2);
458#endif
459 if (err != ESP_OK) {
460 ESP_LOGV(TAG, "set_ttls_phase2_method failed %d", err);
461 }
462 }
463#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
464 err = esp_wifi_sta_enterprise_enable();
465#else
466 err = esp_wifi_sta_wpa2_ent_enable();
467#endif
468 if (err != ESP_OK) {
469 ESP_LOGV(TAG, "enterprise_enable failed %d", err);
470 }
471 }
472#endif // USE_WIFI_WPA2_EAP
473
474 // Reset flags, do this _before_ wifi_station_connect as the callback method
475 // may be called from wifi_station_connect
476 s_sta_connecting = true;
477 s_sta_connected = false;
478 s_sta_connect_error = false;
479 s_sta_connect_not_found = false;
480
481 err = esp_wifi_connect();
482 if (err != ESP_OK) {
483 ESP_LOGW(TAG, "esp_wifi_connect failed: %s", esp_err_to_name(err));
484 return false;
485 }
486
487 return true;
488}
489
490bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
491 // enable STA
492 if (!this->wifi_mode_(true, {}))
493 return false;
494
495 // Check if the STA interface is initialized before using it
496 if (s_sta_netif == nullptr) {
497 ESP_LOGW(TAG, "STA interface not initialized");
498 return false;
499 }
500
501 esp_netif_dhcp_status_t dhcp_status;
502 esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
503 if (err != ESP_OK) {
504 ESP_LOGV(TAG, "esp_netif_dhcpc_get_status failed: %s", esp_err_to_name(err));
505 return false;
506 }
507
508 if (!manual_ip.has_value()) {
509 // lwIP starts the SNTP client if it gets an SNTP server from DHCP. We don't need the time, and more importantly,
510 // the built-in SNTP client has a memory leak in certain situations. Disable this feature.
511 // https://github.com/esphome/issues/issues/2299
512 sntp_servermode_dhcp(false);
513
514 // No manual IP is set; use DHCP client
515 if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
516 err = esp_netif_dhcpc_start(s_sta_netif);
517 if (err != ESP_OK) {
518 ESP_LOGV(TAG, "Starting DHCP client failed: %d", err);
519 }
520 return err == ESP_OK;
521 }
522 return true;
523 }
524
525 esp_netif_ip_info_t info; // struct of ip4_addr_t with ip, netmask, gw
526 info.ip = manual_ip->static_ip;
527 info.gw = manual_ip->gateway;
528 info.netmask = manual_ip->subnet;
529 err = esp_netif_dhcpc_stop(s_sta_netif);
530 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
531 ESP_LOGV(TAG, "Stopping DHCP client failed: %s", esp_err_to_name(err));
532 }
533
534 err = esp_netif_set_ip_info(s_sta_netif, &info);
535 if (err != ESP_OK) {
536 ESP_LOGV(TAG, "Setting manual IP info failed: %s", esp_err_to_name(err));
537 }
538
539 esp_netif_dns_info_t dns;
540 if (manual_ip->dns1.is_set()) {
541 dns.ip = manual_ip->dns1;
542 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
543 }
544 if (manual_ip->dns2.is_set()) {
545 dns.ip = manual_ip->dns2;
546 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
547 }
548
549 return true;
550}
551
553 if (!this->has_sta())
554 return {};
555 network::IPAddresses addresses;
556 esp_netif_ip_info_t ip;
557 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
558 if (err != ESP_OK) {
559 ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
560 // TODO: do something smarter
561 // return false;
562 } else {
563 addresses[0] = network::IPAddress(&ip.ip);
564 }
565#if USE_NETWORK_IPV6
566 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
567 uint8_t count = 0;
568 count = esp_netif_get_all_ip6(s_sta_netif, if_ip6s);
569 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
570 for (int i = 0; i < count; i++) {
571 addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
572 }
573#endif /* USE_NETWORK_IPV6 */
574 return addresses;
575}
576
578 // setting is done in SYSTEM_EVENT_STA_START callback
579 return true;
580}
581const char *get_auth_mode_str(uint8_t mode) {
582 switch (mode) {
583 case WIFI_AUTH_OPEN:
584 return "OPEN";
585 case WIFI_AUTH_WEP:
586 return "WEP";
587 case WIFI_AUTH_WPA_PSK:
588 return "WPA PSK";
589 case WIFI_AUTH_WPA2_PSK:
590 return "WPA2 PSK";
591 case WIFI_AUTH_WPA_WPA2_PSK:
592 return "WPA/WPA2 PSK";
593 case WIFI_AUTH_WPA2_ENTERPRISE:
594 return "WPA2 Enterprise";
595 case WIFI_AUTH_WPA3_PSK:
596 return "WPA3 PSK";
597 case WIFI_AUTH_WPA2_WPA3_PSK:
598 return "WPA2/WPA3 PSK";
599 case WIFI_AUTH_WAPI_PSK:
600 return "WAPI PSK";
601 default:
602 return "UNKNOWN";
603 }
604}
605
606std::string format_ip4_addr(const esp_ip4_addr_t &ip) { return str_snprintf(IPSTR, 15, IP2STR(&ip)); }
607#if LWIP_IPV6
608std::string format_ip6_addr(const esp_ip6_addr_t &ip) { return str_snprintf(IPV6STR, 39, IPV62STR(ip)); }
609#endif /* LWIP_IPV6 */
610const char *get_disconnect_reason_str(uint8_t reason) {
611 switch (reason) {
612 case WIFI_REASON_AUTH_EXPIRE:
613 return "Auth Expired";
614 case WIFI_REASON_AUTH_LEAVE:
615 return "Auth Leave";
616 case WIFI_REASON_ASSOC_EXPIRE:
617 return "Association Expired";
618 case WIFI_REASON_ASSOC_TOOMANY:
619 return "Too Many Associations";
620 case WIFI_REASON_NOT_AUTHED:
621 return "Not Authenticated";
622 case WIFI_REASON_NOT_ASSOCED:
623 return "Not Associated";
624 case WIFI_REASON_ASSOC_LEAVE:
625 return "Association Leave";
626 case WIFI_REASON_ASSOC_NOT_AUTHED:
627 return "Association not Authenticated";
628 case WIFI_REASON_DISASSOC_PWRCAP_BAD:
629 return "Disassociate Power Cap Bad";
630 case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
631 return "Disassociate Supported Channel Bad";
632 case WIFI_REASON_IE_INVALID:
633 return "IE Invalid";
634 case WIFI_REASON_MIC_FAILURE:
635 return "Mic Failure";
636 case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
637 return "4-Way Handshake Timeout";
638 case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
639 return "Group Key Update Timeout";
640 case WIFI_REASON_IE_IN_4WAY_DIFFERS:
641 return "IE In 4-Way Handshake Differs";
642 case WIFI_REASON_GROUP_CIPHER_INVALID:
643 return "Group Cipher Invalid";
644 case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
645 return "Pairwise Cipher Invalid";
646 case WIFI_REASON_AKMP_INVALID:
647 return "AKMP Invalid";
648 case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
649 return "Unsupported RSN IE version";
650 case WIFI_REASON_INVALID_RSN_IE_CAP:
651 return "Invalid RSN IE Cap";
652 case WIFI_REASON_802_1X_AUTH_FAILED:
653 return "802.1x Authentication Failed";
654 case WIFI_REASON_CIPHER_SUITE_REJECTED:
655 return "Cipher Suite Rejected";
656 case WIFI_REASON_BEACON_TIMEOUT:
657 return "Beacon Timeout";
658 case WIFI_REASON_NO_AP_FOUND:
659 return "AP Not Found";
660 case WIFI_REASON_AUTH_FAIL:
661 return "Authentication Failed";
662 case WIFI_REASON_ASSOC_FAIL:
663 return "Association Failed";
664 case WIFI_REASON_HANDSHAKE_TIMEOUT:
665 return "Handshake Failed";
666 case WIFI_REASON_CONNECTION_FAIL:
667 return "Connection Failed";
668 case WIFI_REASON_AP_TSF_RESET:
669 return "AP TSF reset";
670 case WIFI_REASON_ROAMING:
671 return "Station Roaming";
672 case WIFI_REASON_ASSOC_COMEBACK_TIME_TOO_LONG:
673 return "Association comeback time too long";
674 case WIFI_REASON_SA_QUERY_TIMEOUT:
675 return "SA query timeout";
676#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 2)
677 case WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY:
678 return "No AP found with compatible security";
679 case WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD:
680 return "No AP found in auth mode threshold";
681 case WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD:
682 return "No AP found in RSSI threshold";
683#endif
684 case WIFI_REASON_UNSPECIFIED:
685 default:
686 return "Unspecified";
687 }
688}
689
691 while (true) {
692 IDFWiFiEvent *data;
693 if (xQueueReceive(s_event_queue, &data, 0L) != pdTRUE) {
694 // no event ready
695 break;
696 }
697
698 // process event
700
701 delete data; // NOLINT(cppcoreguidelines-owning-memory)
702 }
703}
704void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
705 esp_err_t err;
706 if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_START) {
707 ESP_LOGV(TAG, "STA start");
708 // apply hostname
709 err = esp_netif_set_hostname(s_sta_netif, App.get_name().c_str());
710 if (err != ERR_OK) {
711 ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
712 }
713
714 s_sta_started = true;
715 // re-apply power save mode
717
718 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
719 ESP_LOGV(TAG, "STA stop");
720 s_sta_started = false;
721
722 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
723 const auto &it = data->data.sta_authmode_change;
724 ESP_LOGV(TAG, "Authmode Change old=%s new=%s", get_auth_mode_str(it.old_mode), get_auth_mode_str(it.new_mode));
725
726 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_CONNECTED) {
727 const auto &it = data->data.sta_connected;
728 char buf[33];
729 assert(it.ssid_len <= 32);
730 memcpy(buf, it.ssid, it.ssid_len);
731 buf[it.ssid_len] = '\0';
732 ESP_LOGV(TAG, "Connected ssid='%s' bssid=" LOG_SECRET("%s") " channel=%u, authmode=%s", buf,
733 format_mac_address_pretty(it.bssid).c_str(), it.channel, get_auth_mode_str(it.authmode));
734 s_sta_connected = true;
735
736 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_DISCONNECTED) {
737 const auto &it = data->data.sta_disconnected;
738 char buf[33];
739 assert(it.ssid_len <= 32);
740 memcpy(buf, it.ssid, it.ssid_len);
741 buf[it.ssid_len] = '\0';
742 if (it.reason == WIFI_REASON_NO_AP_FOUND) {
743 ESP_LOGW(TAG, "Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf);
744 s_sta_connect_not_found = true;
745 } else if (it.reason == WIFI_REASON_ROAMING) {
746 ESP_LOGI(TAG, "Disconnected ssid='%s' reason='Station Roaming'", buf);
747 return;
748 } else {
749 ESP_LOGW(TAG, "Disconnected ssid='%s' bssid=" LOG_SECRET("%s") " reason='%s'", buf,
750 format_mac_address_pretty(it.bssid).c_str(), get_disconnect_reason_str(it.reason));
751 s_sta_connect_error = true;
752 }
753 s_sta_connected = false;
754 s_sta_connecting = false;
756
757 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
758 const auto &it = data->data.ip_got_ip;
759#if USE_NETWORK_IPV6
760 esp_netif_create_ip6_linklocal(s_sta_netif);
761#endif /* USE_NETWORK_IPV6 */
762 ESP_LOGV(TAG, "static_ip=%s gateway=%s", format_ip4_addr(it.ip_info.ip).c_str(),
763 format_ip4_addr(it.ip_info.gw).c_str());
764 this->got_ipv4_address_ = true;
765
766#if USE_NETWORK_IPV6
767 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
768 const auto &it = data->data.ip_got_ip6;
769 ESP_LOGV(TAG, "IPv6 address=%s", format_ip6_addr(it.ip6_info.ip).c_str());
770 this->num_ipv6_addresses_++;
771#endif /* USE_NETWORK_IPV6 */
772
773 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_LOST_IP) {
774 ESP_LOGV(TAG, "Lost IP");
775 this->got_ipv4_address_ = false;
776
777 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_SCAN_DONE) {
778 const auto &it = data->data.sta_scan_done;
779 ESP_LOGV(TAG, "Scan done: status=%" PRIu32 " number=%u scan_id=%u", it.status, it.number, it.scan_id);
780
781 scan_result_.clear();
782 this->scan_done_ = true;
783 if (it.status != 0) {
784 // scan error
785 return;
786 }
787
788 if (it.number == 0) {
789 // no results
790 return;
791 }
792
793 uint16_t number = it.number;
794 auto records = std::make_unique<wifi_ap_record_t[]>(number);
795 err = esp_wifi_scan_get_ap_records(&number, records.get());
796 if (err != ESP_OK) {
797 ESP_LOGW(TAG, "esp_wifi_scan_get_ap_records failed: %s", esp_err_to_name(err));
798 return;
799 }
800
801 scan_result_.init(number);
802 for (int i = 0; i < number; i++) {
803 auto &record = records[i];
804 bssid_t bssid;
805 std::copy(record.bssid, record.bssid + 6, bssid.begin());
806 std::string ssid(reinterpret_cast<const char *>(record.ssid));
807 scan_result_.emplace_back(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN,
808 ssid.empty());
809 }
810
811 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {
812 ESP_LOGV(TAG, "AP start");
813 s_ap_started = true;
814
815 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
816 ESP_LOGV(TAG, "AP stop");
817 s_ap_started = false;
818
819 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
820 const auto &it = data->data.ap_probe_req_rx;
821 ESP_LOGVV(TAG, "AP receive Probe Request MAC=%s RSSI=%d", format_mac_address_pretty(it.mac).c_str(), it.rssi);
822
823 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STACONNECTED) {
824 const auto &it = data->data.ap_staconnected;
825 ESP_LOGV(TAG, "AP client connected MAC=%s", format_mac_address_pretty(it.mac).c_str());
826
827 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STADISCONNECTED) {
828 const auto &it = data->data.ap_stadisconnected;
829 ESP_LOGV(TAG, "AP client disconnected MAC=%s", format_mac_address_pretty(it.mac).c_str());
830
831 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_AP_STAIPASSIGNED) {
832 const auto &it = data->data.ip_ap_staipassigned;
833 ESP_LOGV(TAG, "AP client assigned IP %s", format_ip4_addr(it.ip).c_str());
834 }
835}
836
838 if (s_sta_connected && this->got_ipv4_address_) {
839#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
840 if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
842 }
843#else
845#endif /* USE_NETWORK_IPV6 */
846 }
847 if (s_sta_connect_error) {
849 }
850 if (s_sta_connect_not_found) {
852 }
853 if (s_sta_connecting) {
855 }
857}
858bool WiFiComponent::wifi_scan_start_(bool passive) {
859 // enable STA
860 if (!this->wifi_mode_(true, {}))
861 return false;
862
863 wifi_scan_config_t config{};
864 config.ssid = nullptr;
865 config.bssid = nullptr;
866 config.channel = 0;
867 config.show_hidden = true;
868 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
869 if (passive) {
870 config.scan_time.passive = 300;
871 } else {
872 config.scan_time.active.min = 100;
873 config.scan_time.active.max = 300;
874 }
875
876 esp_err_t err = esp_wifi_scan_start(&config, false);
877 if (err != ESP_OK) {
878 ESP_LOGV(TAG, "esp_wifi_scan_start failed: %s", esp_err_to_name(err));
879 return false;
880 }
881
882 this->scan_done_ = false;
883 return true;
884}
885
886#ifdef USE_WIFI_AP
887bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
888 esp_err_t err;
889
890 // enable AP
891 if (!this->wifi_mode_({}, true))
892 return false;
893
894 // Check if the AP interface is initialized before using it
895 if (s_ap_netif == nullptr) {
896 ESP_LOGW(TAG, "AP interface not initialized");
897 return false;
898 }
899
900 esp_netif_ip_info_t info;
901 if (manual_ip.has_value()) {
902 info.ip = manual_ip->static_ip;
903 info.gw = manual_ip->gateway;
904 info.netmask = manual_ip->subnet;
905 } else {
906 info.ip = network::IPAddress(192, 168, 4, 1);
907 info.gw = network::IPAddress(192, 168, 4, 1);
908 info.netmask = network::IPAddress(255, 255, 255, 0);
909 }
910
911 err = esp_netif_dhcps_stop(s_ap_netif);
912 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
913 ESP_LOGE(TAG, "esp_netif_dhcps_stop failed: %s", esp_err_to_name(err));
914 return false;
915 }
916
917 err = esp_netif_set_ip_info(s_ap_netif, &info);
918 if (err != ESP_OK) {
919 ESP_LOGE(TAG, "esp_netif_set_ip_info failed: %d", err);
920 return false;
921 }
922
923 dhcps_lease_t lease;
924 lease.enable = true;
925 network::IPAddress start_address = network::IPAddress(&info.ip);
926 start_address += 99;
927 lease.start_ip = start_address;
928 ESP_LOGV(TAG, "DHCP server IP lease start: %s", start_address.str().c_str());
929 start_address += 10;
930 lease.end_ip = start_address;
931 ESP_LOGV(TAG, "DHCP server IP lease end: %s", start_address.str().c_str());
932 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease, sizeof(lease));
933
934 if (err != ESP_OK) {
935 ESP_LOGE(TAG, "esp_netif_dhcps_option failed: %d", err);
936 return false;
937 }
938
939#if defined(USE_CAPTIVE_PORTAL) && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
940 // Configure DHCP Option 114 (Captive Portal URI) if captive portal is enabled
941 // This provides a standards-compliant way for clients to discover the captive portal
943 static char captive_portal_uri[32];
944 snprintf(captive_portal_uri, sizeof(captive_portal_uri), "http://%s", network::IPAddress(&info.ip).str().c_str());
945 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_CAPTIVEPORTAL_URI, captive_portal_uri,
946 strlen(captive_portal_uri));
947 if (err != ESP_OK) {
948 ESP_LOGV(TAG, "Failed to set DHCP captive portal URI: %s", esp_err_to_name(err));
949 } else {
950 ESP_LOGV(TAG, "DHCP Captive Portal URI set to: %s", captive_portal_uri);
951 }
952 }
953#endif
954
955 err = esp_netif_dhcps_start(s_ap_netif);
956
957 if (err != ESP_OK) {
958 ESP_LOGE(TAG, "esp_netif_dhcps_start failed: %d", err);
959 return false;
960 }
961
962 return true;
963}
964
965bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
966 // enable AP
967 if (!this->wifi_mode_({}, true))
968 return false;
969
970 wifi_config_t conf;
971 memset(&conf, 0, sizeof(conf));
972 if (ap.get_ssid().size() > sizeof(conf.ap.ssid)) {
973 ESP_LOGE(TAG, "AP SSID too long");
974 return false;
975 }
976 memcpy(reinterpret_cast<char *>(conf.ap.ssid), ap.get_ssid().c_str(), ap.get_ssid().size());
977 conf.ap.channel = ap.get_channel().value_or(1);
978 conf.ap.ssid_hidden = ap.get_ssid().size();
979 conf.ap.max_connection = 5;
980 conf.ap.beacon_interval = 100;
981
982 if (ap.get_password().empty()) {
983 conf.ap.authmode = WIFI_AUTH_OPEN;
984 *conf.ap.password = 0;
985 } else {
986 conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
987 if (ap.get_password().size() > sizeof(conf.ap.password)) {
988 ESP_LOGE(TAG, "AP password too long");
989 return false;
990 }
991 memcpy(reinterpret_cast<char *>(conf.ap.password), ap.get_password().c_str(), ap.get_password().size());
992 }
993
994 // pairwise cipher of SoftAP, group cipher will be derived using this.
995 conf.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
996
997 esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &conf);
998 if (err != ESP_OK) {
999 ESP_LOGE(TAG, "esp_wifi_set_config failed: %d", err);
1000 return false;
1001 }
1002
1003#ifdef USE_WIFI_MANUAL_IP
1004 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
1005 ESP_LOGE(TAG, "wifi_ap_ip_config_ failed:");
1006 return false;
1007 }
1008#else
1009 if (!this->wifi_ap_ip_config_({})) {
1010 ESP_LOGE(TAG, "wifi_ap_ip_config_ failed:");
1011 return false;
1012 }
1013#endif
1014
1015 return true;
1016}
1017
1018network::IPAddress WiFiComponent::wifi_soft_ap_ip() {
1019 esp_netif_ip_info_t ip;
1020 esp_netif_get_ip_info(s_ap_netif, &ip);
1021 return network::IPAddress(&ip.ip);
1022}
1023#endif // USE_WIFI_AP
1024
1025bool WiFiComponent::wifi_disconnect_() { return esp_wifi_disconnect(); }
1026
1028 bssid_t bssid{};
1029 wifi_ap_record_t info;
1030 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1031 if (err != ESP_OK) {
1032 // Very verbose only: this is expected during dump_config() before connection is established (PR #9823)
1033 ESP_LOGVV(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1034 return bssid;
1035 }
1036 std::copy(info.bssid, info.bssid + 6, bssid.begin());
1037 return bssid;
1038}
1039std::string WiFiComponent::wifi_ssid() {
1040 wifi_ap_record_t info{};
1041 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1042 if (err != ESP_OK) {
1043 // Very verbose only: this is expected during dump_config() before connection is established (PR #9823)
1044 ESP_LOGVV(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1045 return "";
1046 }
1047 auto *ssid_s = reinterpret_cast<const char *>(info.ssid);
1048 size_t len = strnlen(ssid_s, sizeof(info.ssid));
1049 return {ssid_s, len};
1050}
1051int8_t WiFiComponent::wifi_rssi() {
1052 wifi_ap_record_t info;
1053 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1054 if (err != ESP_OK) {
1055 // Very verbose only: this is expected during dump_config() before connection is established (PR #9823)
1056 ESP_LOGVV(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1057 return WIFI_RSSI_DISCONNECTED;
1058 }
1059 return info.rssi;
1060}
1062 uint8_t primary;
1063 wifi_second_chan_t second;
1064 esp_err_t err = esp_wifi_get_channel(&primary, &second);
1065 if (err != ESP_OK) {
1066 ESP_LOGW(TAG, "esp_wifi_get_channel failed: %s", esp_err_to_name(err));
1067 return 0;
1068 }
1069 return primary;
1070}
1071network::IPAddress WiFiComponent::wifi_subnet_mask_() {
1072 esp_netif_ip_info_t ip;
1073 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1074 if (err != ESP_OK) {
1075 ESP_LOGW(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1076 return {};
1077 }
1078 return network::IPAddress(&ip.netmask);
1079}
1080network::IPAddress WiFiComponent::wifi_gateway_ip_() {
1081 esp_netif_ip_info_t ip;
1082 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1083 if (err != ESP_OK) {
1084 ESP_LOGW(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1085 return {};
1086 }
1087 return network::IPAddress(&ip.gw);
1088}
1089network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
1090 const ip_addr_t *dns_ip = dns_getserver(num);
1091 return network::IPAddress(dns_ip);
1092}
1093
1094} // namespace wifi
1095} // namespace esphome
1096
1097#endif // USE_ESP32
1098#endif
BedjetMode mode
BedJet operating mode.
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made.
void set_sta(const WiFiAP &ap)
wifi_scan_vector_t< WiFiScanResult > scan_result_
bool wifi_sta_ip_config_(optional< ManualIP > manual_ip)
void wifi_process_event_(IDFWiFiEvent *data)
bool wifi_ap_ip_config_(optional< ManualIP > manual_ip)
network::IPAddress wifi_dns_ip_(int num)
bool wifi_apply_output_power_(float output_power)
WiFiSTAConnectStatus wifi_sta_connect_status_()
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
network::IPAddresses wifi_sta_ip_addresses()
uint8_t second
in_addr ip_addr_t
Definition ip_address.h:22
mopeka_std_values val[4]
CaptivePortal * global_captive_portal
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:144
const char *const TAG
Definition spi.cpp:8
std::string format_ip6_addr(const esp_ip6_addr_t &ip)
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)
void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
std::string format_ip4_addr(const esp_ip4_addr_t &ip)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:483
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
Definition helpers.cpp:93
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:208
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
Definition helpers.cpp:91
std::string format_mac_address_pretty(const uint8_t *mac)
Definition helpers.cpp:282
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:73
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t event_id
Definition tt21100.cpp:3