10#include <esp_wifi_types.h>
11#include <freertos/FreeRTOS.h>
12#include <freertos/event_groups.h>
13#include <freertos/task.h>
18#ifdef USE_WIFI_WPA2_EAP
19#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
20#include <esp_eap_client.h>
27#include "dhcpserver/dhcpserver.h"
30#ifdef USE_CAPTIVE_PORTAL
34#include "lwip/apps/sntp.h"
46static const char *
const TAG =
"wifi_esp32";
48static EventGroupHandle_t s_wifi_event_group;
49static QueueHandle_t s_event_queue;
50static esp_netif_t *s_sta_netif =
nullptr;
52static esp_netif_t *s_ap_netif =
nullptr;
54static bool s_sta_started =
false;
55static bool s_sta_connected =
false;
56static bool s_sta_connect_not_found =
false;
57static bool s_sta_connect_error =
false;
58static bool s_sta_connecting =
false;
59static bool s_wifi_started =
false;
62 esp_event_base_t event_base;
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;
75 ip_event_got_ip6_t ip_got_ip6;
77 ip_event_ap_staipassigned_t ip_ap_staipassigned;
85 memset(&event, 0,
sizeof(IDFWiFiEvent));
86 event.event_base = event_base;
88 if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_STA_START) {
90 }
else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_STA_STOP) {
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));
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));
104 }
else if (event_base == IP_EVENT &&
event_id == IP_EVENT_STA_LOST_IP) {
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) {
110 }
else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_AP_STOP) {
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));
126 auto *to_send =
new IDFWiFiEvent;
127 memcpy(to_send, &event,
sizeof(IDFWiFiEvent));
129 if (xQueueSend(s_event_queue, &to_send, 0L) != pdPASS) {
140 esp_err_t err = esp_netif_init();
142 ESP_LOGE(TAG,
"esp_netif_init failed: %s", esp_err_to_name(err));
145 s_wifi_event_group = xEventGroupCreate();
146 if (s_wifi_event_group ==
nullptr) {
147 ESP_LOGE(TAG,
"xEventGroupCreate failed");
151 s_event_queue = xQueueCreate(64,
sizeof(IDFWiFiEvent *));
152 if (s_event_queue ==
nullptr) {
153 ESP_LOGE(TAG,
"xQueueCreate failed");
156 err = esp_event_loop_create_default();
158 ESP_LOGE(TAG,
"esp_event_loop_create_default failed: %s", esp_err_to_name(err));
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);
164 ESP_LOGE(TAG,
"esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
167 err = esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &
event_handler,
nullptr, &instance_ip_id);
169 ESP_LOGE(TAG,
"esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
173 s_sta_netif = esp_netif_create_default_wifi_sta();
176 s_ap_netif = esp_netif_create_default_wifi_ap();
179 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
181 err = esp_wifi_init(&cfg);
183 ESP_LOGE(TAG,
"esp_wifi_init failed: %s", esp_err_to_name(err));
186 err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
188 ESP_LOGE(TAG,
"esp_wifi_set_storage failed: %s", esp_err_to_name(err));
195 wifi_mode_t current_mode = WIFI_MODE_NULL;
196 if (s_wifi_started) {
197 err = esp_wifi_get_mode(¤t_mode);
199 ESP_LOGW(TAG,
"esp_wifi_get_mode failed: %s", esp_err_to_name(err));
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;
206 bool set_sta = sta.value_or(current_sta);
207 bool set_ap = ap.value_or(current_ap);
209 wifi_mode_t set_mode;
211 set_mode = WIFI_MODE_APSTA;
213 set_mode = WIFI_MODE_STA;
215 set_mode = WIFI_MODE_AP;
217 set_mode = WIFI_MODE_NULL;
220 if (current_mode == set_mode)
224 ESP_LOGV(TAG,
"Enabling STA");
225 }
else if (!
set_sta && current_sta) {
226 ESP_LOGV(TAG,
"Disabling STA");
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");
234 if (set_mode == WIFI_MODE_NULL && s_wifi_started) {
235 err = esp_wifi_stop();
237 ESP_LOGV(TAG,
"esp_wifi_stop failed: %s", esp_err_to_name(err));
240 s_wifi_started =
false;
244 err = esp_wifi_set_mode(set_mode);
246 ESP_LOGW(TAG,
"esp_wifi_set_mode failed: %s", esp_err_to_name(err));
250 if (set_mode != WIFI_MODE_NULL && !s_wifi_started) {
251 err = esp_wifi_start();
253 ESP_LOGV(TAG,
"esp_wifi_start failed: %s", esp_err_to_name(err));
256 s_wifi_started =
true;
265 int8_t
val =
static_cast<int8_t
>(output_power * 4);
266 return esp_wifi_set_max_tx_power(
val) == ESP_OK;
270 wifi_ps_type_t power_save;
273 power_save = WIFI_PS_MIN_MODEM;
276 power_save = WIFI_PS_MAX_MODEM;
280 power_save = WIFI_PS_NONE;
283 bool success = esp_wifi_set_ps(power_save) == ESP_OK;
284#ifdef USE_WIFI_LISTENERS
301 memset(&conf, 0,
sizeof(conf));
302 if (ap.get_ssid().size() >
sizeof(conf.sta.ssid)) {
303 ESP_LOGE(TAG,
"SSID too long");
306 if (ap.get_password().size() >
sizeof(conf.sta.password)) {
307 ESP_LOGE(TAG,
"Password too long");
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());
314 if (ap.get_password().empty()) {
315 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
320 conf.sta.threshold.authmode = WIFI_AUTH_WPA_PSK;
323 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
326 conf.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK;
331#ifdef USE_WIFI_WPA2_EAP
332 if (ap.get_eap().has_value()) {
333 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_ENTERPRISE;
337#ifdef USE_WIFI_11KV_SUPPORT
338 conf.sta.btm_enabled = this->
btm_;
339 conf.sta.rm_enabled = this->
rrm_;
342 if (ap.get_bssid().has_value()) {
343 conf.sta.bssid_set =
true;
344 memcpy(conf.sta.bssid, ap.get_bssid()->data(), 6);
346 conf.sta.bssid_set =
false;
348 if (ap.get_channel().has_value()) {
349 conf.sta.channel = *ap.get_channel();
350 conf.sta.scan_method = WIFI_FAST_SCAN;
352 conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
356 conf.sta.listen_interval = 0;
360 conf.sta.pmf_cfg.capable =
true;
361 conf.sta.pmf_cfg.required =
false;
365 conf.sta.threshold.rssi = -127;
367 wifi_config_t current_conf;
369 err = esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
371 ESP_LOGW(TAG,
"esp_wifi_get_config failed: %s", esp_err_to_name(err));
375 if (memcmp(¤t_conf, &conf,
sizeof(wifi_config_t)) != 0) {
376 err = esp_wifi_disconnect();
378 ESP_LOGV(TAG,
"esp_wifi_disconnect failed: %s", esp_err_to_name(err));
383 err = esp_wifi_set_config(WIFI_IF_STA, &conf);
385 ESP_LOGV(TAG,
"esp_wifi_set_config failed: %s", esp_err_to_name(err));
389#ifdef USE_WIFI_MANUAL_IP
400#ifdef USE_WIFI_WPA2_EAP
401 if (ap.get_eap().has_value()) {
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());
407 err = esp_wifi_sta_wpa2_ent_set_identity((uint8_t *) eap.identity.c_str(), eap.identity.length());
410 ESP_LOGV(TAG,
"set_identity failed %d", err);
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);
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);
419 err = esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *) eap.ca_cert, ca_cert_len + 1);
422 ESP_LOGV(TAG,
"set_ca_cert failed %d", err);
427 if (client_cert_len && client_key_len) {
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());
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());
439 ESP_LOGV(TAG,
"set_cert_key failed %d", err);
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());
446 err = esp_wifi_sta_wpa2_ent_set_username((uint8_t *) eap.username.c_str(), eap.username.length());
449 ESP_LOGV(TAG,
"set_username failed %d", err);
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());
454 err = esp_wifi_sta_wpa2_ent_set_password((uint8_t *) eap.password.c_str(), eap.password.length());
457 ESP_LOGV(TAG,
"set_password failed %d", err);
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);
463 err = esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(eap.ttls_phase_2);
466 ESP_LOGV(TAG,
"set_ttls_phase2_method failed %d", err);
469#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
470 err = esp_wifi_sta_enterprise_enable();
472 err = esp_wifi_sta_wpa2_ent_enable();
475 ESP_LOGV(TAG,
"enterprise_enable failed %d", err);
482 s_sta_connecting =
true;
483 s_sta_connected =
false;
484 s_sta_connect_error =
false;
485 s_sta_connect_not_found =
false;
487 err = esp_wifi_connect();
489 ESP_LOGW(TAG,
"esp_wifi_connect failed: %s", esp_err_to_name(err));
502 if (s_sta_netif ==
nullptr) {
503 ESP_LOGW(TAG,
"STA interface not initialized");
507 esp_netif_dhcp_status_t dhcp_status;
508 esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
510 ESP_LOGV(TAG,
"esp_netif_dhcpc_get_status failed: %s", esp_err_to_name(err));
514 if (!manual_ip.has_value()) {
518 sntp_servermode_dhcp(
false);
521 if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
522 err = esp_netif_dhcpc_start(s_sta_netif);
524 ESP_LOGV(TAG,
"Starting DHCP client failed: %d", err);
526 return err == ESP_OK;
531 esp_netif_ip_info_t info;
532 info.ip = manual_ip->static_ip;
533 info.gw = manual_ip->gateway;
534 info.netmask = manual_ip->subnet;
535 err = esp_netif_dhcpc_stop(s_sta_netif);
536 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
537 ESP_LOGV(TAG,
"Stopping DHCP client failed: %s", esp_err_to_name(err));
540 err = esp_netif_set_ip_info(s_sta_netif, &info);
542 ESP_LOGV(TAG,
"Setting manual IP info failed: %s", esp_err_to_name(err));
545 esp_netif_dns_info_t dns;
546 if (manual_ip->dns1.is_set()) {
547 dns.ip = manual_ip->dns1;
548 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
550 if (manual_ip->dns2.is_set()) {
551 dns.ip = manual_ip->dns2;
552 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
562 esp_netif_ip_info_t ip;
563 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
565 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
569 addresses[0] = network::IPAddress(&ip.ip);
572 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
574 count = esp_netif_get_all_ip6(s_sta_netif, if_ip6s);
575 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
576 for (
int i = 0; i < count; i++) {
577 addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
593 case WIFI_AUTH_WPA_PSK:
595 case WIFI_AUTH_WPA2_PSK:
597 case WIFI_AUTH_WPA_WPA2_PSK:
598 return "WPA/WPA2 PSK";
599 case WIFI_AUTH_WPA2_ENTERPRISE:
600 return "WPA2 Enterprise";
601 case WIFI_AUTH_WPA3_PSK:
603 case WIFI_AUTH_WPA2_WPA3_PSK:
604 return "WPA2/WPA3 PSK";
605 case WIFI_AUTH_WAPI_PSK:
614 case WIFI_REASON_AUTH_EXPIRE:
615 return "Auth Expired";
616 case WIFI_REASON_AUTH_LEAVE:
618 case WIFI_REASON_ASSOC_EXPIRE:
619 return "Association Expired";
620 case WIFI_REASON_ASSOC_TOOMANY:
621 return "Too Many Associations";
622 case WIFI_REASON_NOT_AUTHED:
623 return "Not Authenticated";
624 case WIFI_REASON_NOT_ASSOCED:
625 return "Not Associated";
626 case WIFI_REASON_ASSOC_LEAVE:
627 return "Association Leave";
628 case WIFI_REASON_ASSOC_NOT_AUTHED:
629 return "Association not Authenticated";
630 case WIFI_REASON_DISASSOC_PWRCAP_BAD:
631 return "Disassociate Power Cap Bad";
632 case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
633 return "Disassociate Supported Channel Bad";
634 case WIFI_REASON_IE_INVALID:
636 case WIFI_REASON_MIC_FAILURE:
637 return "Mic Failure";
638 case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
639 return "4-Way Handshake Timeout";
640 case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
641 return "Group Key Update Timeout";
642 case WIFI_REASON_IE_IN_4WAY_DIFFERS:
643 return "IE In 4-Way Handshake Differs";
644 case WIFI_REASON_GROUP_CIPHER_INVALID:
645 return "Group Cipher Invalid";
646 case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
647 return "Pairwise Cipher Invalid";
648 case WIFI_REASON_AKMP_INVALID:
649 return "AKMP Invalid";
650 case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
651 return "Unsupported RSN IE version";
652 case WIFI_REASON_INVALID_RSN_IE_CAP:
653 return "Invalid RSN IE Cap";
654 case WIFI_REASON_802_1X_AUTH_FAILED:
655 return "802.1x Authentication Failed";
656 case WIFI_REASON_CIPHER_SUITE_REJECTED:
657 return "Cipher Suite Rejected";
658 case WIFI_REASON_BEACON_TIMEOUT:
659 return "Beacon Timeout";
660 case WIFI_REASON_NO_AP_FOUND:
661 return "AP Not Found";
662 case WIFI_REASON_AUTH_FAIL:
663 return "Authentication Failed";
664 case WIFI_REASON_ASSOC_FAIL:
665 return "Association Failed";
666 case WIFI_REASON_HANDSHAKE_TIMEOUT:
667 return "Handshake Failed";
668 case WIFI_REASON_CONNECTION_FAIL:
669 return "Connection Failed";
670 case WIFI_REASON_AP_TSF_RESET:
671 return "AP TSF reset";
672 case WIFI_REASON_ROAMING:
673 return "Station Roaming";
674 case WIFI_REASON_ASSOC_COMEBACK_TIME_TOO_LONG:
675 return "Association comeback time too long";
676 case WIFI_REASON_SA_QUERY_TIMEOUT:
677 return "SA query timeout";
678#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 2)
679 case WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY:
680 return "No AP found with compatible security";
681 case WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD:
682 return "No AP found in auth mode threshold";
683 case WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD:
684 return "No AP found in RSSI threshold";
686 case WIFI_REASON_UNSPECIFIED:
688 return "Unspecified";
695 if (xQueueReceive(s_event_queue, &data, 0L) != pdTRUE) {
708 if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_START) {
709 ESP_LOGV(TAG,
"STA start");
711 err = esp_netif_set_hostname(s_sta_netif,
App.
get_name().c_str());
713 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
716 s_sta_started =
true;
720 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
721 ESP_LOGV(TAG,
"STA stop");
722 s_sta_started =
false;
723 s_sta_connecting =
false;
725 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
726 const auto &it = data->data.sta_authmode_change;
729 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_CONNECTED) {
730 const auto &it = data->data.sta_connected;
732 assert(it.ssid_len <= 32);
733 memcpy(buf, it.ssid, it.ssid_len);
734 buf[it.ssid_len] =
'\0';
735 ESP_LOGV(TAG,
"Connected ssid='%s' bssid=" LOG_SECRET(
"%s")
" channel=%u, authmode=%s", buf,
737 s_sta_connected =
true;
738#ifdef USE_WIFI_LISTENERS
744 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_DISCONNECTED) {
745 const auto &it = data->data.sta_disconnected;
747 assert(it.ssid_len <= 32);
748 memcpy(buf, it.ssid, it.ssid_len);
749 buf[it.ssid_len] =
'\0';
750 if (it.reason == WIFI_REASON_NO_AP_FOUND) {
751 ESP_LOGW(TAG,
"Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf);
752 s_sta_connect_not_found =
true;
753 }
else if (it.reason == WIFI_REASON_ROAMING) {
754 ESP_LOGI(TAG,
"Disconnected ssid='%s' reason='Station Roaming'", buf);
759 ESP_LOGW(TAG,
"Disconnected ssid='%s' bssid=" LOG_SECRET(
"%s")
" reason='%s'", buf, bssid_s,
761 s_sta_connect_error =
true;
763 s_sta_connected =
false;
764 s_sta_connecting =
false;
766#ifdef USE_WIFI_LISTENERS
768 listener->on_wifi_connect_state(
"",
bssid_t({0, 0, 0, 0, 0, 0}));
772 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
773 const auto &it = data->data.ip_got_ip;
775 esp_netif_create_ip6_linklocal(s_sta_netif);
777 ESP_LOGV(TAG,
"static_ip=" IPSTR
" gateway=" IPSTR, IP2STR(&it.ip_info.ip), IP2STR(&it.ip_info.gw));
779#ifdef USE_WIFI_LISTENERS
786 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
787 const auto &it = data->data.ip_got_ip6;
788 ESP_LOGV(TAG,
"IPv6 address=" IPV6STR, IPV62STR(it.ip6_info.ip));
790#ifdef USE_WIFI_LISTENERS
797 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_LOST_IP) {
798 ESP_LOGV(TAG,
"Lost IP");
801 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_SCAN_DONE) {
802 const auto &it = data->data.sta_scan_done;
803 ESP_LOGV(TAG,
"Scan done: status=%" PRIu32
" number=%u scan_id=%u", it.status, it.number, it.scan_id);
807 if (it.status != 0) {
812 if (it.number == 0) {
817 uint16_t number = it.number;
818 auto records = std::make_unique<wifi_ap_record_t[]>(number);
819 err = esp_wifi_scan_get_ap_records(&number, records.get());
821 ESP_LOGW(TAG,
"esp_wifi_scan_get_ap_records failed: %s", esp_err_to_name(err));
826 for (
int i = 0; i < number; i++) {
827 auto &record = records[i];
829 std::copy(record.bssid, record.bssid + 6, bssid.begin());
830 std::string ssid(
reinterpret_cast<const char *
>(record.ssid));
831 scan_result_.emplace_back(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN,
834#ifdef USE_WIFI_LISTENERS
840 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {
841 ESP_LOGV(TAG,
"AP start");
844 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
845 ESP_LOGV(TAG,
"AP stop");
848 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
849 const auto &it = data->data.ap_probe_req_rx;
852 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STACONNECTED) {
853 const auto &it = data->data.ap_staconnected;
856 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STADISCONNECTED) {
857 const auto &it = data->data.ap_stadisconnected;
860 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_AP_STAIPASSIGNED) {
861 const auto &it = data->data.ip_ap_staipassigned;
862 ESP_LOGV(TAG,
"AP client assigned IP " IPSTR, IP2STR(&it.ip));
868#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
876 if (s_sta_connect_error) {
879 if (s_sta_connect_not_found) {
882 if (s_sta_connecting) {
892 wifi_scan_config_t config{};
893 config.ssid =
nullptr;
894 config.bssid =
nullptr;
896 config.show_hidden =
true;
897 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
899 config.scan_time.passive = 300;
901 config.scan_time.active.min = 100;
902 config.scan_time.active.max = 300;
905 esp_err_t err = esp_wifi_scan_start(&config,
false);
907 ESP_LOGV(TAG,
"esp_wifi_scan_start failed: %s", esp_err_to_name(err));
924 if (s_ap_netif ==
nullptr) {
925 ESP_LOGW(TAG,
"AP interface not initialized");
929 esp_netif_ip_info_t info;
930 if (manual_ip.has_value()) {
931 info.ip = manual_ip->static_ip;
932 info.gw = manual_ip->gateway;
933 info.netmask = manual_ip->subnet;
935 info.ip = network::IPAddress(192, 168, 4, 1);
936 info.gw = network::IPAddress(192, 168, 4, 1);
937 info.netmask = network::IPAddress(255, 255, 255, 0);
940 err = esp_netif_dhcps_stop(s_ap_netif);
941 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
942 ESP_LOGE(TAG,
"esp_netif_dhcps_stop failed: %s", esp_err_to_name(err));
946 err = esp_netif_set_ip_info(s_ap_netif, &info);
948 ESP_LOGE(TAG,
"esp_netif_set_ip_info failed: %d", err);
954 network::IPAddress start_address = network::IPAddress(&info.ip);
956 lease.start_ip = start_address;
957 ESP_LOGV(TAG,
"DHCP server IP lease start: %s", start_address.str().c_str());
959 lease.end_ip = start_address;
960 ESP_LOGV(TAG,
"DHCP server IP lease end: %s", start_address.str().c_str());
961 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease,
sizeof(lease));
964 ESP_LOGE(TAG,
"esp_netif_dhcps_option failed: %d", err);
968#if defined(USE_CAPTIVE_PORTAL) && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
972 static char captive_portal_uri[32];
973 snprintf(captive_portal_uri,
sizeof(captive_portal_uri),
"http://%s", network::IPAddress(&info.ip).str().c_str());
974 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_CAPTIVEPORTAL_URI, captive_portal_uri,
975 strlen(captive_portal_uri));
977 ESP_LOGV(TAG,
"Failed to set DHCP captive portal URI: %s", esp_err_to_name(err));
979 ESP_LOGV(TAG,
"DHCP Captive Portal URI set to: %s", captive_portal_uri);
984 err = esp_netif_dhcps_start(s_ap_netif);
987 ESP_LOGE(TAG,
"esp_netif_dhcps_start failed: %d", err);
1000 memset(&conf, 0,
sizeof(conf));
1001 if (ap.get_ssid().size() >
sizeof(conf.ap.ssid)) {
1002 ESP_LOGE(TAG,
"AP SSID too long");
1005 memcpy(
reinterpret_cast<char *
>(conf.ap.ssid), ap.get_ssid().c_str(), ap.get_ssid().size());
1006 conf.ap.channel = ap.get_channel().value_or(1);
1007 conf.ap.ssid_hidden = ap.get_ssid().size();
1008 conf.ap.max_connection = 5;
1009 conf.ap.beacon_interval = 100;
1011 if (ap.get_password().empty()) {
1012 conf.ap.authmode = WIFI_AUTH_OPEN;
1013 *conf.ap.password = 0;
1015 conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
1016 if (ap.get_password().size() >
sizeof(conf.ap.password)) {
1017 ESP_LOGE(TAG,
"AP password too long");
1020 memcpy(
reinterpret_cast<char *
>(conf.ap.password), ap.get_password().c_str(), ap.get_password().size());
1024 conf.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
1026 esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &conf);
1027 if (err != ESP_OK) {
1028 ESP_LOGE(TAG,
"esp_wifi_set_config failed: %d", err);
1032#ifdef USE_WIFI_MANUAL_IP
1034 ESP_LOGE(TAG,
"wifi_ap_ip_config_ failed:");
1039 ESP_LOGE(TAG,
"wifi_ap_ip_config_ failed:");
1048 esp_netif_ip_info_t ip;
1049 esp_netif_get_ip_info(s_ap_netif, &ip);
1050 return network::IPAddress(&ip.ip);
1058 wifi_ap_record_t info;
1059 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1060 if (err != ESP_OK) {
1062 ESP_LOGVV(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1065 std::copy(info.bssid, info.bssid + 6, bssid.begin());
1069 wifi_ap_record_t info{};
1070 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1071 if (err != ESP_OK) {
1073 ESP_LOGVV(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1076 auto *ssid_s =
reinterpret_cast<const char *
>(info.ssid);
1077 size_t len = strnlen(ssid_s,
sizeof(info.ssid));
1078 return {ssid_s,
len};
1081 wifi_ap_record_t info;
1082 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
1083 if (err != ESP_OK) {
1085 ESP_LOGVV(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
1086 return WIFI_RSSI_DISCONNECTED;
1092 wifi_second_chan_t
second;
1093 esp_err_t err = esp_wifi_get_channel(&primary, &
second);
1094 if (err != ESP_OK) {
1095 ESP_LOGW(TAG,
"esp_wifi_get_channel failed: %s", esp_err_to_name(err));
1101 esp_netif_ip_info_t ip;
1102 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1103 if (err != ESP_OK) {
1104 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1107 return network::IPAddress(&ip.netmask);
1110 esp_netif_ip_info_t ip;
1111 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1112 if (err != ESP_OK) {
1113 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1116 return network::IPAddress(&ip.gw);
1119 const ip_addr_t *dns_ip = dns_getserver(num);
1120 return network::IPAddress(dns_ip);
BedjetMode mode
BedJet operating mode.
const std::string & get_name() const
Get the name of this Application set by pre_setup().
bool error_from_callback_
bool wifi_apply_power_save_()
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)
WiFiPowerSaveMode power_save_
std::vector< WiFiIPStateListener * > ip_state_listeners_
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)
bool wifi_start_ap_(const WiFiAP &ap)
int32_t get_wifi_channel()
network::IPAddress wifi_subnet_mask_()
network::IPAddress wifi_soft_ap_ip()
network::IPAddress wifi_gateway_ip_()
network::IPAddress wifi_dns_ip_(int num)
bool wifi_apply_hostname_()
bool wifi_sta_pre_setup_()
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
WifiMinAuthMode min_auth_mode_
bool wifi_apply_output_power_(float output_power)
WiFiSTAConnectStatus wifi_sta_connect_status_()
bool wifi_sta_connect_(const WiFiAP &ap)
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
network::IPAddresses wifi_sta_ip_addresses()
uint8_t num_ipv6_addresses_
bool wifi_scan_start_(bool passive)
std::vector< WiFiConnectStateListener * > connect_state_listeners_
std::vector< WiFiPowerSaveListener * > power_save_listeners_
CaptivePortal * global_captive_portal
std::array< IPAddress, 5 > IPAddresses
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)
@ WIFI_MIN_AUTH_MODE_WPA2
@ WIFI_MIN_AUTH_MODE_WPA3
void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
@ ERROR_NETWORK_NOT_FOUND
void format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase)
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
std::string format_mac_address_pretty(const uint8_t *mac)
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Application App
Global storage of Application pointer - only one Application can exist.