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