ESPHome 2025.10.4
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 conf.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
312 }
313
314#ifdef USE_WIFI_WPA2_EAP
315 if (ap.get_eap().has_value()) {
316 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_ENTERPRISE;
317 }
318#endif
319
320#ifdef USE_WIFI_11KV_SUPPORT
321 conf.sta.btm_enabled = this->btm_;
322 conf.sta.rm_enabled = this->rrm_;
323#endif
324
325 if (ap.get_bssid().has_value()) {
326 conf.sta.bssid_set = true;
327 memcpy(conf.sta.bssid, ap.get_bssid()->data(), 6);
328 } else {
329 conf.sta.bssid_set = false;
330 }
331 if (ap.get_channel().has_value()) {
332 conf.sta.channel = *ap.get_channel();
333 conf.sta.scan_method = WIFI_FAST_SCAN;
334 } else {
335 conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
336 }
337 // Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set.
338 // Units: AP beacon intervals. Defaults to 3 if set to 0.
339 conf.sta.listen_interval = 0;
340
341 // Protected Management Frame
342 // Device will prefer to connect in PMF mode if other device also advertises PMF capability.
343 conf.sta.pmf_cfg.capable = true;
344 conf.sta.pmf_cfg.required = false;
345
346 // note, we do our own filtering
347 // The minimum rssi to accept in the fast scan mode
348 conf.sta.threshold.rssi = -127;
349
350 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
351
352 wifi_config_t current_conf;
353 esp_err_t err;
354 err = esp_wifi_get_config(WIFI_IF_STA, &current_conf);
355 if (err != ERR_OK) {
356 ESP_LOGW(TAG, "esp_wifi_get_config failed: %s", esp_err_to_name(err));
357 // can continue
358 }
359
360 if (memcmp(&current_conf, &conf, sizeof(wifi_config_t)) != 0) { // NOLINT
361 err = esp_wifi_disconnect();
362 if (err != ESP_OK) {
363 ESP_LOGV(TAG, "esp_wifi_disconnect failed: %s", esp_err_to_name(err));
364 return false;
365 }
366 }
367
368 err = esp_wifi_set_config(WIFI_IF_STA, &conf);
369 if (err != ESP_OK) {
370 ESP_LOGV(TAG, "esp_wifi_set_config failed: %s", esp_err_to_name(err));
371 return false;
372 }
373
374 if (!this->wifi_sta_ip_config_(ap.get_manual_ip())) {
375 return false;
376 }
377
378 // setup enterprise authentication if required
379#ifdef USE_WIFI_WPA2_EAP
380 if (ap.get_eap().has_value()) {
381 // note: all certificates and keys have to be null terminated. Lengths are appended by +1 to include \0.
382 EAPAuth eap = ap.get_eap().value();
383#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
384 err = esp_eap_client_set_identity((uint8_t *) eap.identity.c_str(), eap.identity.length());
385#else
386 err = esp_wifi_sta_wpa2_ent_set_identity((uint8_t *) eap.identity.c_str(), eap.identity.length());
387#endif
388 if (err != ESP_OK) {
389 ESP_LOGV(TAG, "set_identity failed %d", err);
390 }
391 int ca_cert_len = strlen(eap.ca_cert);
392 int client_cert_len = strlen(eap.client_cert);
393 int client_key_len = strlen(eap.client_key);
394 if (ca_cert_len) {
395#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
396 err = esp_eap_client_set_ca_cert((uint8_t *) eap.ca_cert, ca_cert_len + 1);
397#else
398 err = esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *) eap.ca_cert, ca_cert_len + 1);
399#endif
400 if (err != ESP_OK) {
401 ESP_LOGV(TAG, "set_ca_cert failed %d", err);
402 }
403 }
404 // workout what type of EAP this is
405 // validation is not required as the config tool has already validated it
406 if (client_cert_len && client_key_len) {
407 // if we have certs, this must be EAP-TLS
408#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
409 err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.client_cert, client_cert_len + 1,
410 (uint8_t *) eap.client_key, client_key_len + 1,
411 (uint8_t *) eap.password.c_str(), eap.password.length());
412#else
413 err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.client_cert, client_cert_len + 1,
414 (uint8_t *) eap.client_key, client_key_len + 1,
415 (uint8_t *) eap.password.c_str(), eap.password.length());
416#endif
417 if (err != ESP_OK) {
418 ESP_LOGV(TAG, "set_cert_key failed %d", err);
419 }
420 } else {
421 // in the absence of certs, assume this is username/password based
422#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
423 err = esp_eap_client_set_username((uint8_t *) eap.username.c_str(), eap.username.length());
424#else
425 err = esp_wifi_sta_wpa2_ent_set_username((uint8_t *) eap.username.c_str(), eap.username.length());
426#endif
427 if (err != ESP_OK) {
428 ESP_LOGV(TAG, "set_username failed %d", err);
429 }
430#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
431 err = esp_eap_client_set_password((uint8_t *) eap.password.c_str(), eap.password.length());
432#else
433 err = esp_wifi_sta_wpa2_ent_set_password((uint8_t *) eap.password.c_str(), eap.password.length());
434#endif
435 if (err != ESP_OK) {
436 ESP_LOGV(TAG, "set_password failed %d", err);
437 }
438 // set TTLS Phase 2, defaults to MSCHAPV2
439#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
440 err = esp_eap_client_set_ttls_phase2_method(eap.ttls_phase_2);
441#else
442 err = esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(eap.ttls_phase_2);
443#endif
444 if (err != ESP_OK) {
445 ESP_LOGV(TAG, "set_ttls_phase2_method failed %d", err);
446 }
447 }
448#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
449 err = esp_wifi_sta_enterprise_enable();
450#else
451 err = esp_wifi_sta_wpa2_ent_enable();
452#endif
453 if (err != ESP_OK) {
454 ESP_LOGV(TAG, "enterprise_enable failed %d", err);
455 }
456 }
457#endif // USE_WIFI_WPA2_EAP
458
459 // Reset flags, do this _before_ wifi_station_connect as the callback method
460 // may be called from wifi_station_connect
461 s_sta_connecting = true;
462 s_sta_connected = false;
463 s_sta_connect_error = false;
464 s_sta_connect_not_found = false;
465
466 err = esp_wifi_connect();
467 if (err != ESP_OK) {
468 ESP_LOGW(TAG, "esp_wifi_connect failed: %s", esp_err_to_name(err));
469 return false;
470 }
471
472 return true;
473}
474
475bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
476 // enable STA
477 if (!this->wifi_mode_(true, {}))
478 return false;
479
480 // Check if the STA interface is initialized before using it
481 if (s_sta_netif == nullptr) {
482 ESP_LOGW(TAG, "STA interface not initialized");
483 return false;
484 }
485
486 esp_netif_dhcp_status_t dhcp_status;
487 esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
488 if (err != ESP_OK) {
489 ESP_LOGV(TAG, "esp_netif_dhcpc_get_status failed: %s", esp_err_to_name(err));
490 return false;
491 }
492
493 if (!manual_ip.has_value()) {
494 // lwIP starts the SNTP client if it gets an SNTP server from DHCP. We don't need the time, and more importantly,
495 // the built-in SNTP client has a memory leak in certain situations. Disable this feature.
496 // https://github.com/esphome/issues/issues/2299
497 sntp_servermode_dhcp(false);
498
499 // No manual IP is set; use DHCP client
500 if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
501 err = esp_netif_dhcpc_start(s_sta_netif);
502 if (err != ESP_OK) {
503 ESP_LOGV(TAG, "Starting DHCP client failed: %d", err);
504 }
505 return err == ESP_OK;
506 }
507 return true;
508 }
509
510 esp_netif_ip_info_t info; // struct of ip4_addr_t with ip, netmask, gw
511 info.ip = manual_ip->static_ip;
512 info.gw = manual_ip->gateway;
513 info.netmask = manual_ip->subnet;
514 err = esp_netif_dhcpc_stop(s_sta_netif);
515 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
516 ESP_LOGV(TAG, "Stopping DHCP client failed: %s", esp_err_to_name(err));
517 }
518
519 err = esp_netif_set_ip_info(s_sta_netif, &info);
520 if (err != ESP_OK) {
521 ESP_LOGV(TAG, "Setting manual IP info failed: %s", esp_err_to_name(err));
522 }
523
524 esp_netif_dns_info_t dns;
525 if (manual_ip->dns1.is_set()) {
526 dns.ip = manual_ip->dns1;
527 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
528 }
529 if (manual_ip->dns2.is_set()) {
530 dns.ip = manual_ip->dns2;
531 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
532 }
533
534 return true;
535}
536
538 if (!this->has_sta())
539 return {};
540 network::IPAddresses addresses;
541 esp_netif_ip_info_t ip;
542 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
543 if (err != ESP_OK) {
544 ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
545 // TODO: do something smarter
546 // return false;
547 } else {
548 addresses[0] = network::IPAddress(&ip.ip);
549 }
550#if USE_NETWORK_IPV6
551 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
552 uint8_t count = 0;
553 count = esp_netif_get_all_ip6(s_sta_netif, if_ip6s);
554 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
555 for (int i = 0; i < count; i++) {
556 addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
557 }
558#endif /* USE_NETWORK_IPV6 */
559 return addresses;
560}
561
563 // setting is done in SYSTEM_EVENT_STA_START callback
564 return true;
565}
566const char *get_auth_mode_str(uint8_t mode) {
567 switch (mode) {
568 case WIFI_AUTH_OPEN:
569 return "OPEN";
570 case WIFI_AUTH_WEP:
571 return "WEP";
572 case WIFI_AUTH_WPA_PSK:
573 return "WPA PSK";
574 case WIFI_AUTH_WPA2_PSK:
575 return "WPA2 PSK";
576 case WIFI_AUTH_WPA_WPA2_PSK:
577 return "WPA/WPA2 PSK";
578 case WIFI_AUTH_WPA2_ENTERPRISE:
579 return "WPA2 Enterprise";
580 case WIFI_AUTH_WPA3_PSK:
581 return "WPA3 PSK";
582 case WIFI_AUTH_WPA2_WPA3_PSK:
583 return "WPA2/WPA3 PSK";
584 case WIFI_AUTH_WAPI_PSK:
585 return "WAPI PSK";
586 default:
587 return "UNKNOWN";
588 }
589}
590
591std::string format_ip4_addr(const esp_ip4_addr_t &ip) { return str_snprintf(IPSTR, 15, IP2STR(&ip)); }
592#if LWIP_IPV6
593std::string format_ip6_addr(const esp_ip6_addr_t &ip) { return str_snprintf(IPV6STR, 39, IPV62STR(ip)); }
594#endif /* LWIP_IPV6 */
595const char *get_disconnect_reason_str(uint8_t reason) {
596 switch (reason) {
597 case WIFI_REASON_AUTH_EXPIRE:
598 return "Auth Expired";
599 case WIFI_REASON_AUTH_LEAVE:
600 return "Auth Leave";
601 case WIFI_REASON_ASSOC_EXPIRE:
602 return "Association Expired";
603 case WIFI_REASON_ASSOC_TOOMANY:
604 return "Too Many Associations";
605 case WIFI_REASON_NOT_AUTHED:
606 return "Not Authenticated";
607 case WIFI_REASON_NOT_ASSOCED:
608 return "Not Associated";
609 case WIFI_REASON_ASSOC_LEAVE:
610 return "Association Leave";
611 case WIFI_REASON_ASSOC_NOT_AUTHED:
612 return "Association not Authenticated";
613 case WIFI_REASON_DISASSOC_PWRCAP_BAD:
614 return "Disassociate Power Cap Bad";
615 case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
616 return "Disassociate Supported Channel Bad";
617 case WIFI_REASON_IE_INVALID:
618 return "IE Invalid";
619 case WIFI_REASON_MIC_FAILURE:
620 return "Mic Failure";
621 case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
622 return "4-Way Handshake Timeout";
623 case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
624 return "Group Key Update Timeout";
625 case WIFI_REASON_IE_IN_4WAY_DIFFERS:
626 return "IE In 4-Way Handshake Differs";
627 case WIFI_REASON_GROUP_CIPHER_INVALID:
628 return "Group Cipher Invalid";
629 case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
630 return "Pairwise Cipher Invalid";
631 case WIFI_REASON_AKMP_INVALID:
632 return "AKMP Invalid";
633 case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
634 return "Unsupported RSN IE version";
635 case WIFI_REASON_INVALID_RSN_IE_CAP:
636 return "Invalid RSN IE Cap";
637 case WIFI_REASON_802_1X_AUTH_FAILED:
638 return "802.1x Authentication Failed";
639 case WIFI_REASON_CIPHER_SUITE_REJECTED:
640 return "Cipher Suite Rejected";
641 case WIFI_REASON_BEACON_TIMEOUT:
642 return "Beacon Timeout";
643 case WIFI_REASON_NO_AP_FOUND:
644 return "AP Not Found";
645 case WIFI_REASON_AUTH_FAIL:
646 return "Authentication Failed";
647 case WIFI_REASON_ASSOC_FAIL:
648 return "Association Failed";
649 case WIFI_REASON_HANDSHAKE_TIMEOUT:
650 return "Handshake Failed";
651 case WIFI_REASON_CONNECTION_FAIL:
652 return "Connection Failed";
653 case WIFI_REASON_AP_TSF_RESET:
654 return "AP TSF reset";
655 case WIFI_REASON_ROAMING:
656 return "Station Roaming";
657 case WIFI_REASON_ASSOC_COMEBACK_TIME_TOO_LONG:
658 return "Association comeback time too long";
659 case WIFI_REASON_SA_QUERY_TIMEOUT:
660 return "SA query timeout";
661#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 2)
662 case WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY:
663 return "No AP found with compatible security";
664 case WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD:
665 return "No AP found in auth mode threshold";
666 case WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD:
667 return "No AP found in RSSI threshold";
668#endif
669 case WIFI_REASON_UNSPECIFIED:
670 default:
671 return "Unspecified";
672 }
673}
674
676 while (true) {
677 IDFWiFiEvent *data;
678 if (xQueueReceive(s_event_queue, &data, 0L) != pdTRUE) {
679 // no event ready
680 break;
681 }
682
683 // process event
685
686 delete data; // NOLINT(cppcoreguidelines-owning-memory)
687 }
688}
689void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
690 esp_err_t err;
691 if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_START) {
692 ESP_LOGV(TAG, "STA start");
693 // apply hostname
694 err = esp_netif_set_hostname(s_sta_netif, App.get_name().c_str());
695 if (err != ERR_OK) {
696 ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
697 }
698
699 s_sta_started = true;
700 // re-apply power save mode
702
703 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
704 ESP_LOGV(TAG, "STA stop");
705 s_sta_started = false;
706
707 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
708 const auto &it = data->data.sta_authmode_change;
709 ESP_LOGV(TAG, "Authmode Change old=%s new=%s", get_auth_mode_str(it.old_mode), get_auth_mode_str(it.new_mode));
710
711 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_CONNECTED) {
712 const auto &it = data->data.sta_connected;
713 char buf[33];
714 assert(it.ssid_len <= 32);
715 memcpy(buf, it.ssid, it.ssid_len);
716 buf[it.ssid_len] = '\0';
717 ESP_LOGV(TAG, "Connected ssid='%s' bssid=" LOG_SECRET("%s") " channel=%u, authmode=%s", buf,
718 format_mac_address_pretty(it.bssid).c_str(), it.channel, get_auth_mode_str(it.authmode));
719 s_sta_connected = true;
720
721 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_DISCONNECTED) {
722 const auto &it = data->data.sta_disconnected;
723 char buf[33];
724 assert(it.ssid_len <= 32);
725 memcpy(buf, it.ssid, it.ssid_len);
726 buf[it.ssid_len] = '\0';
727 if (it.reason == WIFI_REASON_NO_AP_FOUND) {
728 ESP_LOGW(TAG, "Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf);
729 s_sta_connect_not_found = true;
730 } else if (it.reason == WIFI_REASON_ROAMING) {
731 ESP_LOGI(TAG, "Disconnected ssid='%s' reason='Station Roaming'", buf);
732 return;
733 } else {
734 ESP_LOGW(TAG, "Disconnected ssid='%s' bssid=" LOG_SECRET("%s") " reason='%s'", buf,
735 format_mac_address_pretty(it.bssid).c_str(), get_disconnect_reason_str(it.reason));
736 s_sta_connect_error = true;
737 }
738 s_sta_connected = false;
739 s_sta_connecting = false;
741
742 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
743 const auto &it = data->data.ip_got_ip;
744#if USE_NETWORK_IPV6
745 esp_netif_create_ip6_linklocal(s_sta_netif);
746#endif /* USE_NETWORK_IPV6 */
747 ESP_LOGV(TAG, "static_ip=%s gateway=%s", format_ip4_addr(it.ip_info.ip).c_str(),
748 format_ip4_addr(it.ip_info.gw).c_str());
749 this->got_ipv4_address_ = true;
750
751#if USE_NETWORK_IPV6
752 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
753 const auto &it = data->data.ip_got_ip6;
754 ESP_LOGV(TAG, "IPv6 address=%s", format_ip6_addr(it.ip6_info.ip).c_str());
755 this->num_ipv6_addresses_++;
756#endif /* USE_NETWORK_IPV6 */
757
758 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_LOST_IP) {
759 ESP_LOGV(TAG, "Lost IP");
760 this->got_ipv4_address_ = false;
761
762 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_SCAN_DONE) {
763 const auto &it = data->data.sta_scan_done;
764 ESP_LOGV(TAG, "Scan done: status=%" PRIu32 " number=%u scan_id=%u", it.status, it.number, it.scan_id);
765
766 scan_result_.clear();
767 this->scan_done_ = true;
768 if (it.status != 0) {
769 // scan error
770 return;
771 }
772
773 if (it.number == 0) {
774 // no results
775 return;
776 }
777
778 uint16_t number = it.number;
779 std::vector<wifi_ap_record_t> records(number);
780 err = esp_wifi_scan_get_ap_records(&number, records.data());
781 if (err != ESP_OK) {
782 ESP_LOGW(TAG, "esp_wifi_scan_get_ap_records failed: %s", esp_err_to_name(err));
783 return;
784 }
785 records.resize(number);
786
787 scan_result_.reserve(number);
788 for (int i = 0; i < number; i++) {
789 auto &record = records[i];
790 bssid_t bssid;
791 std::copy(record.bssid, record.bssid + 6, bssid.begin());
792 std::string ssid(reinterpret_cast<const char *>(record.ssid));
793 WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty());
794 scan_result_.push_back(result);
795 }
796
797 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {
798 ESP_LOGV(TAG, "AP start");
799 s_ap_started = true;
800
801 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
802 ESP_LOGV(TAG, "AP stop");
803 s_ap_started = false;
804
805 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
806 const auto &it = data->data.ap_probe_req_rx;
807 ESP_LOGVV(TAG, "AP receive Probe Request MAC=%s RSSI=%d", format_mac_address_pretty(it.mac).c_str(), it.rssi);
808
809 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STACONNECTED) {
810 const auto &it = data->data.ap_staconnected;
811 ESP_LOGV(TAG, "AP client connected MAC=%s", format_mac_address_pretty(it.mac).c_str());
812
813 } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STADISCONNECTED) {
814 const auto &it = data->data.ap_stadisconnected;
815 ESP_LOGV(TAG, "AP client disconnected MAC=%s", format_mac_address_pretty(it.mac).c_str());
816
817 } else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_AP_STAIPASSIGNED) {
818 const auto &it = data->data.ip_ap_staipassigned;
819 ESP_LOGV(TAG, "AP client assigned IP %s", format_ip4_addr(it.ip).c_str());
820 }
821}
822
824 if (s_sta_connected && this->got_ipv4_address_) {
825#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
826 if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
828 }
829#else
831#endif /* USE_NETWORK_IPV6 */
832 }
833 if (s_sta_connect_error) {
835 }
836 if (s_sta_connect_not_found) {
838 }
839 if (s_sta_connecting) {
841 }
843}
844bool WiFiComponent::wifi_scan_start_(bool passive) {
845 // enable STA
846 if (!this->wifi_mode_(true, {}))
847 return false;
848
849 wifi_scan_config_t config{};
850 config.ssid = nullptr;
851 config.bssid = nullptr;
852 config.channel = 0;
853 config.show_hidden = true;
854 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
855 if (passive) {
856 config.scan_time.passive = 300;
857 } else {
858 config.scan_time.active.min = 100;
859 config.scan_time.active.max = 300;
860 }
861
862 esp_err_t err = esp_wifi_scan_start(&config, false);
863 if (err != ESP_OK) {
864 ESP_LOGV(TAG, "esp_wifi_scan_start failed: %s", esp_err_to_name(err));
865 return false;
866 }
867
868 this->scan_done_ = false;
869 return true;
870}
871
872#ifdef USE_WIFI_AP
873bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
874 esp_err_t err;
875
876 // enable AP
877 if (!this->wifi_mode_({}, true))
878 return false;
879
880 // Check if the AP interface is initialized before using it
881 if (s_ap_netif == nullptr) {
882 ESP_LOGW(TAG, "AP interface not initialized");
883 return false;
884 }
885
886 esp_netif_ip_info_t info;
887 if (manual_ip.has_value()) {
888 info.ip = manual_ip->static_ip;
889 info.gw = manual_ip->gateway;
890 info.netmask = manual_ip->subnet;
891 } else {
892 info.ip = network::IPAddress(192, 168, 4, 1);
893 info.gw = network::IPAddress(192, 168, 4, 1);
894 info.netmask = network::IPAddress(255, 255, 255, 0);
895 }
896
897 err = esp_netif_dhcps_stop(s_ap_netif);
898 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
899 ESP_LOGE(TAG, "esp_netif_dhcps_stop failed: %s", esp_err_to_name(err));
900 return false;
901 }
902
903 err = esp_netif_set_ip_info(s_ap_netif, &info);
904 if (err != ESP_OK) {
905 ESP_LOGE(TAG, "esp_netif_set_ip_info failed: %d", err);
906 return false;
907 }
908
909 dhcps_lease_t lease;
910 lease.enable = true;
911 network::IPAddress start_address = network::IPAddress(&info.ip);
912 start_address += 99;
913 lease.start_ip = start_address;
914 ESP_LOGV(TAG, "DHCP server IP lease start: %s", start_address.str().c_str());
915 start_address += 10;
916 lease.end_ip = start_address;
917 ESP_LOGV(TAG, "DHCP server IP lease end: %s", start_address.str().c_str());
918 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease, sizeof(lease));
919
920 if (err != ESP_OK) {
921 ESP_LOGE(TAG, "esp_netif_dhcps_option failed: %d", err);
922 return false;
923 }
924
925#if defined(USE_CAPTIVE_PORTAL) && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
926 // Configure DHCP Option 114 (Captive Portal URI) if captive portal is enabled
927 // This provides a standards-compliant way for clients to discover the captive portal
929 static char captive_portal_uri[32];
930 snprintf(captive_portal_uri, sizeof(captive_portal_uri), "http://%s", network::IPAddress(&info.ip).str().c_str());
931 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_CAPTIVEPORTAL_URI, captive_portal_uri,
932 strlen(captive_portal_uri));
933 if (err != ESP_OK) {
934 ESP_LOGV(TAG, "Failed to set DHCP captive portal URI: %s", esp_err_to_name(err));
935 } else {
936 ESP_LOGV(TAG, "DHCP Captive Portal URI set to: %s", captive_portal_uri);
937 }
938 }
939#endif
940
941 err = esp_netif_dhcps_start(s_ap_netif);
942
943 if (err != ESP_OK) {
944 ESP_LOGE(TAG, "esp_netif_dhcps_start failed: %d", err);
945 return false;
946 }
947
948 return true;
949}
950
951bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
952 // enable AP
953 if (!this->wifi_mode_({}, true))
954 return false;
955
956 wifi_config_t conf;
957 memset(&conf, 0, sizeof(conf));
958 if (ap.get_ssid().size() > sizeof(conf.ap.ssid)) {
959 ESP_LOGE(TAG, "AP SSID too long");
960 return false;
961 }
962 memcpy(reinterpret_cast<char *>(conf.ap.ssid), ap.get_ssid().c_str(), ap.get_ssid().size());
963 conf.ap.channel = ap.get_channel().value_or(1);
964 conf.ap.ssid_hidden = ap.get_ssid().size();
965 conf.ap.max_connection = 5;
966 conf.ap.beacon_interval = 100;
967
968 if (ap.get_password().empty()) {
969 conf.ap.authmode = WIFI_AUTH_OPEN;
970 *conf.ap.password = 0;
971 } else {
972 conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
973 if (ap.get_password().size() > sizeof(conf.ap.password)) {
974 ESP_LOGE(TAG, "AP password too long");
975 return false;
976 }
977 memcpy(reinterpret_cast<char *>(conf.ap.password), ap.get_password().c_str(), ap.get_password().size());
978 }
979
980 // pairwise cipher of SoftAP, group cipher will be derived using this.
981 conf.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
982
983 esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &conf);
984 if (err != ESP_OK) {
985 ESP_LOGE(TAG, "esp_wifi_set_config failed: %d", err);
986 return false;
987 }
988
989 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
990 ESP_LOGE(TAG, "wifi_ap_ip_config_ failed:");
991 return false;
992 }
993
994 return true;
995}
996
997network::IPAddress WiFiComponent::wifi_soft_ap_ip() {
998 esp_netif_ip_info_t ip;
999 esp_netif_get_ip_info(s_ap_netif, &ip);
1000 return network::IPAddress(&ip.ip);
1001}
1002#endif // USE_WIFI_AP
1003
1004bool WiFiComponent::wifi_disconnect_() { return esp_wifi_disconnect(); }
1005
1007 bssid_t bssid{};
1008 wifi_ap_record_t info;
1009 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1010 if (err != ESP_OK) {
1011 ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1012 return bssid;
1013 }
1014 std::copy(info.bssid, info.bssid + 6, bssid.begin());
1015 return bssid;
1016}
1017std::string WiFiComponent::wifi_ssid() {
1018 wifi_ap_record_t info{};
1019 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1020 if (err != ESP_OK) {
1021 ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1022 return "";
1023 }
1024 auto *ssid_s = reinterpret_cast<const char *>(info.ssid);
1025 size_t len = strnlen(ssid_s, sizeof(info.ssid));
1026 return {ssid_s, len};
1027}
1028int8_t WiFiComponent::wifi_rssi() {
1029 wifi_ap_record_t info;
1030 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1031 if (err != ESP_OK) {
1032 ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1033 return 0;
1034 }
1035 return info.rssi;
1036}
1038 uint8_t primary;
1039 wifi_second_chan_t second;
1040 esp_err_t err = esp_wifi_get_channel(&primary, &second);
1041 if (err != ESP_OK) {
1042 ESP_LOGW(TAG, "esp_wifi_get_channel failed: %s", esp_err_to_name(err));
1043 return 0;
1044 }
1045 return primary;
1046}
1047network::IPAddress WiFiComponent::wifi_subnet_mask_() {
1048 esp_netif_ip_info_t ip;
1049 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1050 if (err != ESP_OK) {
1051 ESP_LOGW(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1052 return {};
1053 }
1054 return network::IPAddress(&ip.netmask);
1055}
1056network::IPAddress WiFiComponent::wifi_gateway_ip_() {
1057 esp_netif_ip_info_t ip;
1058 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1059 if (err != ESP_OK) {
1060 ESP_LOGW(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1061 return {};
1062 }
1063 return network::IPAddress(&ip.gw);
1064}
1065network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
1066 const ip_addr_t *dns_ip = dns_getserver(num);
1067 return network::IPAddress(dns_ip);
1068}
1069
1070} // namespace wifi
1071} // namespace esphome
1072
1073#endif // USE_ESP32
1074#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)
bool wifi_sta_ip_config_(optional< ManualIP > manual_ip)
std::vector< WiFiScanResult > scan_result_
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:304
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:258
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