8 #include <esp_system.h> 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 #include "lwip/apps/sntp.h" 43 static const char *
const TAG =
"wifi_esp32";
45 static EventGroupHandle_t s_wifi_event_group;
46 static QueueHandle_t s_event_queue;
47 static esp_netif_t *s_sta_netif =
nullptr;
49 static esp_netif_t *s_ap_netif =
nullptr;
51 static bool s_sta_started =
false;
52 static bool s_sta_connected =
false;
53 static bool s_ap_started =
false;
54 static bool s_sta_connect_not_found =
false;
55 static bool s_sta_connect_error =
false;
56 static bool s_sta_connecting =
false;
57 static bool s_wifi_started =
false;
60 esp_event_base_t event_base;
63 wifi_event_sta_scan_done_t sta_scan_done;
64 wifi_event_sta_connected_t sta_connected;
65 wifi_event_sta_disconnected_t sta_disconnected;
66 wifi_event_sta_authmode_change_t sta_authmode_change;
67 wifi_event_ap_staconnected_t ap_staconnected;
68 wifi_event_ap_stadisconnected_t ap_stadisconnected;
69 wifi_event_ap_probe_req_rx_t ap_probe_req_rx;
70 wifi_event_bss_rssi_low_t bss_rssi_low;
71 ip_event_got_ip_t ip_got_ip;
73 ip_event_got_ip6_t ip_got_ip6;
75 ip_event_ap_staipassigned_t ip_ap_staipassigned;
83 memset(&event, 0,
sizeof(IDFWiFiEvent));
84 event.event_base = event_base;
86 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
88 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_STOP) {
90 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
91 memcpy(&event.data.sta_authmode_change, event_data,
sizeof(wifi_event_sta_authmode_change_t));
92 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
93 memcpy(&event.data.sta_connected, event_data,
sizeof(wifi_event_sta_connected_t));
94 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
95 memcpy(&event.data.sta_disconnected, event_data,
sizeof(wifi_event_sta_disconnected_t));
96 }
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
97 memcpy(&event.data.ip_got_ip, event_data,
sizeof(ip_event_got_ip_t));
99 }
else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
100 memcpy(&event.data.ip_got_ip6, event_data,
sizeof(ip_event_got_ip6_t));
102 }
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) {
104 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
105 memcpy(&event.data.sta_scan_done, event_data,
sizeof(wifi_event_sta_scan_done_t));
106 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) {
108 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STOP) {
110 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
111 memcpy(&event.data.ap_probe_req_rx, event_data,
sizeof(wifi_event_ap_probe_req_rx_t));
112 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
113 memcpy(&event.data.ap_staconnected, event_data,
sizeof(wifi_event_ap_staconnected_t));
114 }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
115 memcpy(&event.data.ap_stadisconnected, event_data,
sizeof(wifi_event_ap_stadisconnected_t));
116 }
else if (event_base == IP_EVENT && event_id == IP_EVENT_AP_STAIPASSIGNED) {
117 memcpy(&event.data.ip_ap_staipassigned, event_data,
sizeof(ip_event_ap_staipassigned_t));
124 auto *to_send =
new IDFWiFiEvent;
125 memcpy(to_send, &event,
sizeof(IDFWiFiEvent));
127 if (xQueueSend(s_event_queue, &to_send, 0L) != pdPASS) {
138 esp_err_t err = esp_netif_init();
140 ESP_LOGE(TAG,
"esp_netif_init failed: %s", esp_err_to_name(err));
143 s_wifi_event_group = xEventGroupCreate();
144 if (s_wifi_event_group ==
nullptr) {
145 ESP_LOGE(TAG,
"xEventGroupCreate failed");
149 s_event_queue = xQueueCreate(64,
sizeof(IDFWiFiEvent *));
150 if (s_event_queue ==
nullptr) {
151 ESP_LOGE(TAG,
"xQueueCreate failed");
154 err = esp_event_loop_create_default();
156 ESP_LOGE(TAG,
"esp_event_loop_create_default failed: %s", esp_err_to_name(err));
159 esp_event_handler_instance_t instance_wifi_id, instance_ip_id;
160 err = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &
event_handler,
nullptr, &instance_wifi_id);
162 ESP_LOGE(TAG,
"esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
165 err = esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &
event_handler,
nullptr, &instance_ip_id);
167 ESP_LOGE(TAG,
"esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
171 s_sta_netif = esp_netif_create_default_wifi_sta();
174 s_ap_netif = esp_netif_create_default_wifi_ap();
175 #endif // USE_WIFI_AP 177 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
179 err = esp_wifi_init(&cfg);
181 ESP_LOGE(TAG,
"esp_wifi_init failed: %s", esp_err_to_name(err));
184 err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
186 ESP_LOGE(TAG,
"esp_wifi_set_storage failed: %s", esp_err_to_name(err));
193 wifi_mode_t current_mode = WIFI_MODE_NULL;
194 if (s_wifi_started) {
195 err = esp_wifi_get_mode(¤t_mode);
197 ESP_LOGW(TAG,
"esp_wifi_get_mode failed: %s", esp_err_to_name(err));
201 bool current_sta = current_mode == WIFI_MODE_STA || current_mode == WIFI_MODE_APSTA;
202 bool current_ap = current_mode == WIFI_MODE_AP || current_mode == WIFI_MODE_APSTA;
204 bool set_sta = sta.
value_or(current_sta);
205 bool set_ap = ap.
value_or(current_ap);
207 wifi_mode_t set_mode;
208 if (set_sta && set_ap) {
209 set_mode = WIFI_MODE_APSTA;
210 }
else if (set_sta && !set_ap) {
211 set_mode = WIFI_MODE_STA;
212 }
else if (!set_sta && set_ap) {
213 set_mode = WIFI_MODE_AP;
215 set_mode = WIFI_MODE_NULL;
218 if (current_mode == set_mode)
221 if (set_sta && !current_sta) {
222 ESP_LOGV(TAG,
"Enabling STA.");
223 }
else if (!set_sta && current_sta) {
224 ESP_LOGV(TAG,
"Disabling STA.");
226 if (set_ap && !current_ap) {
227 ESP_LOGV(TAG,
"Enabling AP.");
228 }
else if (!set_ap && current_ap) {
229 ESP_LOGV(TAG,
"Disabling AP.");
232 if (set_mode == WIFI_MODE_NULL && s_wifi_started) {
233 err = esp_wifi_stop();
235 ESP_LOGV(TAG,
"esp_wifi_stop failed: %s", esp_err_to_name(err));
238 s_wifi_started =
false;
242 err = esp_wifi_set_mode(set_mode);
244 ESP_LOGW(TAG,
"esp_wifi_set_mode failed: %s", esp_err_to_name(err));
248 if (set_mode != WIFI_MODE_NULL && !s_wifi_started) {
249 err = esp_wifi_start();
251 ESP_LOGV(TAG,
"esp_wifi_start failed: %s", esp_err_to_name(err));
254 s_wifi_started =
true;
263 int8_t
val =
static_cast<int8_t
>(output_power * 4);
264 return esp_wifi_set_max_tx_power(val) == ESP_OK;
268 wifi_ps_type_t power_save;
269 switch (this->power_save_) {
271 power_save = WIFI_PS_MIN_MODEM;
274 power_save = WIFI_PS_MAX_MODEM;
278 power_save = WIFI_PS_NONE;
281 return esp_wifi_set_ps(power_save) == ESP_OK;
286 if (!this->wifi_mode_(
true, {}))
291 memset(&conf, 0,
sizeof(conf));
292 strncpy(reinterpret_cast<char *>(conf.sta.ssid), ap.
get_ssid().c_str(),
sizeof(conf.sta.ssid));
293 strncpy(reinterpret_cast<char *>(conf.sta.password), ap.
get_password().c_str(),
sizeof(conf.sta.password));
297 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
299 conf.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
302 #ifdef USE_WIFI_WPA2_EAP 303 if (ap.
get_eap().has_value()) {
304 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_ENTERPRISE;
308 #ifdef USE_WIFI_11KV_SUPPORT 309 conf.sta.btm_enabled = this->btm_;
310 conf.sta.rm_enabled = this->rrm_;
314 conf.sta.bssid_set =
true;
315 memcpy(conf.sta.bssid, ap.
get_bssid()->data(), 6);
317 conf.sta.bssid_set =
false;
321 conf.sta.scan_method = WIFI_FAST_SCAN;
323 conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
327 conf.sta.listen_interval = 0;
331 conf.sta.pmf_cfg.capable =
true;
332 conf.sta.pmf_cfg.required =
false;
336 conf.sta.threshold.rssi = -127;
338 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
340 wifi_config_t current_conf;
342 err = esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
344 ESP_LOGW(TAG,
"esp_wifi_get_config failed: %s", esp_err_to_name(err));
348 if (memcmp(¤t_conf, &conf,
sizeof(wifi_config_t)) != 0) {
349 err = esp_wifi_disconnect();
351 ESP_LOGV(TAG,
"esp_wifi_disconnect failed: %s", esp_err_to_name(err));
356 err = esp_wifi_set_config(WIFI_IF_STA, &conf);
358 ESP_LOGV(TAG,
"esp_wifi_set_config failed: %s", esp_err_to_name(err));
367 #ifdef USE_WIFI_WPA2_EAP 368 if (ap.
get_eap().has_value()) {
371 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 372 err = esp_eap_client_set_identity((uint8_t *) eap.
identity.c_str(), eap.
identity.length());
374 err = esp_wifi_sta_wpa2_ent_set_identity((uint8_t *) eap.
identity.c_str(), eap.
identity.length());
377 ESP_LOGV(TAG,
"set_identity failed %d", err);
379 int ca_cert_len = strlen(eap.
ca_cert);
383 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 384 err = esp_eap_client_set_ca_cert((uint8_t *) eap.
ca_cert, ca_cert_len + 1);
386 err = esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *) eap.
ca_cert, ca_cert_len + 1);
389 ESP_LOGV(TAG,
"set_ca_cert failed %d", err);
394 if (client_cert_len && client_key_len) {
396 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 397 err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.
client_cert, client_cert_len + 1,
398 (uint8_t *) eap.
client_key, client_key_len + 1,
401 err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.
client_cert, client_cert_len + 1,
402 (uint8_t *) eap.
client_key, client_key_len + 1,
406 ESP_LOGV(TAG,
"set_cert_key failed %d", err);
410 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 411 err = esp_eap_client_set_username((uint8_t *) eap.
username.c_str(), eap.
username.length());
413 err = esp_wifi_sta_wpa2_ent_set_username((uint8_t *) eap.
username.c_str(), eap.
username.length());
416 ESP_LOGV(TAG,
"set_username failed %d", err);
418 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 419 err = esp_eap_client_set_password((uint8_t *) eap.
password.c_str(), eap.
password.length());
421 err = esp_wifi_sta_wpa2_ent_set_password((uint8_t *) eap.
password.c_str(), eap.
password.length());
424 ESP_LOGV(TAG,
"set_password failed %d", err);
427 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 428 err = esp_eap_client_set_ttls_phase2_method(eap.
ttls_phase_2);
430 err = esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(eap.
ttls_phase_2);
433 ESP_LOGV(TAG,
"set_ttls_phase2_method failed %d", err);
436 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 437 err = esp_wifi_sta_enterprise_enable();
439 err = esp_wifi_sta_wpa2_ent_enable();
442 ESP_LOGV(TAG,
"enterprise_enable failed %d", err);
445 #endif // USE_WIFI_WPA2_EAP 449 s_sta_connecting =
true;
450 s_sta_connected =
false;
451 s_sta_connect_error =
false;
452 s_sta_connect_not_found =
false;
454 err = esp_wifi_connect();
456 ESP_LOGW(TAG,
"esp_wifi_connect failed: %s", esp_err_to_name(err));
465 if (!this->wifi_mode_(
true, {}))
468 esp_netif_dhcp_status_t dhcp_status;
469 esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
471 ESP_LOGV(TAG,
"esp_netif_dhcpc_get_status failed: %s", esp_err_to_name(err));
479 sntp_servermode_dhcp(
false);
482 if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
483 err = esp_netif_dhcpc_start(s_sta_netif);
485 ESP_LOGV(TAG,
"Starting DHCP client failed! %d", err);
487 return err == ESP_OK;
492 esp_netif_ip_info_t info;
493 info.ip = manual_ip->static_ip;
494 info.gw = manual_ip->gateway;
495 info.netmask = manual_ip->subnet;
496 err = esp_netif_dhcpc_stop(s_sta_netif);
497 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
498 ESP_LOGV(TAG,
"Stopping DHCP client failed! %s", esp_err_to_name(err));
501 err = esp_netif_set_ip_info(s_sta_netif, &info);
503 ESP_LOGV(TAG,
"Setting manual IP info failed! %s", esp_err_to_name(err));
506 esp_netif_dns_info_t dns;
507 if (manual_ip->dns1.is_set()) {
508 dns.ip = manual_ip->dns1;
509 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
511 if (manual_ip->dns2.is_set()) {
512 dns.ip = manual_ip->dns2;
513 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
520 if (!this->has_sta())
523 esp_netif_ip_info_t ip;
524 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
526 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
533 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
535 count = esp_netif_get_all_ip6(s_sta_netif, if_ip6s);
536 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
537 for (
int i = 0; i < count; i++) {
554 case WIFI_AUTH_WPA_PSK:
556 case WIFI_AUTH_WPA2_PSK:
558 case WIFI_AUTH_WPA_WPA2_PSK:
559 return "WPA/WPA2 PSK";
560 case WIFI_AUTH_WPA2_ENTERPRISE:
561 return "WPA2 Enterprise";
562 case WIFI_AUTH_WPA3_PSK:
564 case WIFI_AUTH_WPA2_WPA3_PSK:
565 return "WPA2/WPA3 PSK";
566 case WIFI_AUTH_WAPI_PSK:
579 case WIFI_REASON_AUTH_EXPIRE:
580 return "Auth Expired";
581 case WIFI_REASON_AUTH_LEAVE:
583 case WIFI_REASON_ASSOC_EXPIRE:
584 return "Association Expired";
585 case WIFI_REASON_ASSOC_TOOMANY:
586 return "Too Many Associations";
587 case WIFI_REASON_NOT_AUTHED:
588 return "Not Authenticated";
589 case WIFI_REASON_NOT_ASSOCED:
590 return "Not Associated";
591 case WIFI_REASON_ASSOC_LEAVE:
592 return "Association Leave";
593 case WIFI_REASON_ASSOC_NOT_AUTHED:
594 return "Association not Authenticated";
595 case WIFI_REASON_DISASSOC_PWRCAP_BAD:
596 return "Disassociate Power Cap Bad";
597 case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
598 return "Disassociate Supported Channel Bad";
599 case WIFI_REASON_IE_INVALID:
601 case WIFI_REASON_MIC_FAILURE:
602 return "Mic Failure";
603 case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
604 return "4-Way Handshake Timeout";
605 case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
606 return "Group Key Update Timeout";
607 case WIFI_REASON_IE_IN_4WAY_DIFFERS:
608 return "IE In 4-Way Handshake Differs";
609 case WIFI_REASON_GROUP_CIPHER_INVALID:
610 return "Group Cipher Invalid";
611 case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
612 return "Pairwise Cipher Invalid";
613 case WIFI_REASON_AKMP_INVALID:
614 return "AKMP Invalid";
615 case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
616 return "Unsupported RSN IE version";
617 case WIFI_REASON_INVALID_RSN_IE_CAP:
618 return "Invalid RSN IE Cap";
619 case WIFI_REASON_802_1X_AUTH_FAILED:
620 return "802.1x Authentication Failed";
621 case WIFI_REASON_CIPHER_SUITE_REJECTED:
622 return "Cipher Suite Rejected";
623 case WIFI_REASON_BEACON_TIMEOUT:
624 return "Beacon Timeout";
625 case WIFI_REASON_NO_AP_FOUND:
626 return "AP Not Found";
627 case WIFI_REASON_AUTH_FAIL:
628 return "Authentication Failed";
629 case WIFI_REASON_ASSOC_FAIL:
630 return "Association Failed";
631 case WIFI_REASON_HANDSHAKE_TIMEOUT:
632 return "Handshake Failed";
633 case WIFI_REASON_CONNECTION_FAIL:
634 return "Connection Failed";
635 case WIFI_REASON_ROAMING:
636 return "Station Roaming";
637 case WIFI_REASON_UNSPECIFIED:
639 return "Unspecified";
646 if (xQueueReceive(s_event_queue, &data, 0L) != pdTRUE) {
652 wifi_process_event_(data);
659 if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_START) {
660 ESP_LOGV(TAG,
"Event: WiFi STA start");
662 err = esp_netif_set_hostname(s_sta_netif,
App.
get_name().c_str());
664 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
667 s_sta_started =
true;
669 wifi_apply_power_save_();
671 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
672 ESP_LOGV(TAG,
"Event: WiFi STA stop");
673 s_sta_started =
false;
675 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
676 const auto &it = data->data.sta_authmode_change;
677 ESP_LOGV(TAG,
"Event: Authmode Change old=%s new=%s",
get_auth_mode_str(it.old_mode),
680 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_CONNECTED) {
681 const auto &it = data->data.sta_connected;
683 assert(it.ssid_len <= 32);
684 memcpy(buf, it.ssid, it.ssid_len);
685 buf[it.ssid_len] =
'\0';
686 ESP_LOGV(TAG,
"Event: Connected ssid='%s' bssid=" LOG_SECRET(
"%s")
" channel=%u, authmode=%s", buf,
688 s_sta_connected =
true;
690 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_DISCONNECTED) {
691 const auto &it = data->data.sta_disconnected;
693 assert(it.ssid_len <= 32);
694 memcpy(buf, it.ssid, it.ssid_len);
695 buf[it.ssid_len] =
'\0';
696 if (it.reason == WIFI_REASON_NO_AP_FOUND) {
697 ESP_LOGW(TAG,
"Event: Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf);
698 s_sta_connect_not_found =
true;
699 }
else if (it.reason == WIFI_REASON_ROAMING) {
700 ESP_LOGI(TAG,
"Event: Disconnected ssid='%s' reason='Station Roaming'", buf);
703 ESP_LOGW(TAG,
"Event: Disconnected ssid='%s' bssid=" LOG_SECRET(
"%s")
" reason='%s'", buf,
705 s_sta_connect_error =
true;
707 s_sta_connected =
false;
708 s_sta_connecting =
false;
709 error_from_callback_ =
true;
711 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
712 const auto &it = data->data.ip_got_ip;
714 esp_netif_create_ip6_linklocal(s_sta_netif);
716 ESP_LOGV(TAG,
"Event: Got IP static_ip=%s gateway=%s",
format_ip4_addr(it.ip_info.ip).c_str(),
718 this->got_ipv4_address_ =
true;
721 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
722 const auto &it = data->data.ip_got_ip6;
723 ESP_LOGV(TAG,
"Event: Got IPv6 address=%s",
format_ip6_addr(it.ip6_info.ip).c_str());
724 this->num_ipv6_addresses_++;
727 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_LOST_IP) {
728 ESP_LOGV(TAG,
"Event: Lost IP");
729 this->got_ipv4_address_ =
false;
731 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_SCAN_DONE) {
732 const auto &it = data->data.sta_scan_done;
733 ESP_LOGV(TAG,
"Event: WiFi Scan Done status=%" PRIu32
" number=%u scan_id=%u", it.status, it.number, it.scan_id);
735 scan_result_.clear();
736 this->scan_done_ =
true;
737 if (it.status != 0) {
742 if (it.number == 0) {
747 uint16_t number = it.number;
748 std::vector<wifi_ap_record_t> records(number);
749 err = esp_wifi_scan_get_ap_records(&number, records.data());
751 ESP_LOGW(TAG,
"esp_wifi_scan_get_ap_records failed: %s", esp_err_to_name(err));
754 records.resize(number);
756 scan_result_.reserve(number);
757 for (
int i = 0; i < number; i++) {
758 auto &record = records[i];
760 std::copy(record.bssid, record.bssid + 6, bssid.begin());
761 std::string ssid(reinterpret_cast<const char *>(record.ssid));
762 WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty());
763 scan_result_.push_back(result);
766 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {
767 ESP_LOGV(TAG,
"Event: WiFi AP start");
770 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
771 ESP_LOGV(TAG,
"Event: WiFi AP stop");
772 s_ap_started =
false;
774 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
775 const auto &it = data->data.ap_probe_req_rx;
776 ESP_LOGVV(TAG,
"Event: AP receive Probe Request MAC=%s RSSI=%d", format_mac_addr(it.mac).c_str(), it.rssi);
778 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STACONNECTED) {
779 const auto &it = data->data.ap_staconnected;
780 ESP_LOGV(TAG,
"Event: AP client connected MAC=%s", format_mac_addr(it.mac).c_str());
782 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STADISCONNECTED) {
783 const auto &it = data->data.ap_stadisconnected;
784 ESP_LOGV(TAG,
"Event: AP client disconnected MAC=%s", format_mac_addr(it.mac).c_str());
786 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_AP_STAIPASSIGNED) {
787 const auto &it = data->data.ip_ap_staipassigned;
788 ESP_LOGV(TAG,
"Event: AP client assigned IP %s",
format_ip4_addr(it.ip).c_str());
793 if (s_sta_connected && this->got_ipv4_address_) {
794 #if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) 795 if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
802 if (s_sta_connect_error) {
805 if (s_sta_connect_not_found) {
808 if (s_sta_connecting) {
815 if (!this->wifi_mode_(
true, {}))
818 wifi_scan_config_t config{};
819 config.ssid =
nullptr;
820 config.bssid =
nullptr;
822 config.show_hidden =
true;
823 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
825 config.scan_time.passive = 300;
827 config.scan_time.active.min = 100;
828 config.scan_time.active.max = 300;
831 esp_err_t err = esp_wifi_scan_start(&config,
false);
833 ESP_LOGV(TAG,
"esp_wifi_scan_start failed: %s", esp_err_to_name(err));
837 this->scan_done_ =
false;
846 if (!this->wifi_mode_({},
true))
849 esp_netif_ip_info_t info;
851 info.ip = manual_ip->static_ip;
852 info.gw = manual_ip->gateway;
853 info.netmask = manual_ip->subnet;
860 err = esp_netif_dhcps_stop(s_ap_netif);
861 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
862 ESP_LOGE(TAG,
"esp_netif_dhcps_stop failed: %s", esp_err_to_name(err));
866 err = esp_netif_set_ip_info(s_ap_netif, &info);
868 ESP_LOGE(TAG,
"esp_netif_set_ip_info failed! %d", err);
876 lease.start_ip = start_address;
877 ESP_LOGV(TAG,
"DHCP server IP lease start: %s", start_address.
str().c_str());
879 lease.end_ip = start_address;
880 ESP_LOGV(TAG,
"DHCP server IP lease end: %s", start_address.str().c_str());
881 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease,
sizeof(lease));
884 ESP_LOGE(TAG,
"esp_netif_dhcps_option failed! %d", err);
888 err = esp_netif_dhcps_start(s_ap_netif);
891 ESP_LOGE(TAG,
"esp_netif_dhcps_start failed! %d", err);
900 if (!this->wifi_mode_({},
true))
904 memset(&conf, 0,
sizeof(conf));
905 strncpy(reinterpret_cast<char *>(conf.ap.ssid), ap.
get_ssid().c_str(),
sizeof(conf.ap.ssid));
907 conf.ap.ssid_hidden = ap.
get_ssid().size();
908 conf.ap.max_connection = 5;
909 conf.ap.beacon_interval = 100;
912 conf.ap.authmode = WIFI_AUTH_OPEN;
913 *conf.ap.password = 0;
915 conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
916 strncpy(reinterpret_cast<char *>(conf.ap.password), ap.
get_password().c_str(),
sizeof(conf.ap.password));
920 conf.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
922 esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &conf);
924 ESP_LOGE(TAG,
"esp_wifi_set_config failed! %d", err);
929 ESP_LOGE(TAG,
"wifi_ap_ip_config_ failed!");
937 esp_netif_ip_info_t ip;
938 esp_netif_get_ip_info(s_ap_netif, &ip);
941 #endif // USE_WIFI_AP 947 wifi_ap_record_t info;
948 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
950 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
953 std::copy(info.bssid, info.bssid + 6, bssid.begin());
957 wifi_ap_record_t info{};
958 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
960 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
963 auto *ssid_s =
reinterpret_cast<const char *
>(info.ssid);
964 size_t len = strnlen(ssid_s,
sizeof(info.ssid));
965 return {ssid_s, len};
968 wifi_ap_record_t info;
969 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
971 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
978 wifi_second_chan_t
second;
979 esp_err_t err = esp_wifi_get_channel(&primary, &second);
981 ESP_LOGW(TAG,
"esp_wifi_get_channel failed: %s", esp_err_to_name(err));
987 esp_netif_ip_info_t ip;
988 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
990 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
996 esp_netif_ip_info_t ip;
997 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
999 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1005 const ip_addr_t *dns_ip = dns_getserver(num);
1012 #endif // USE_ESP_IDF
WiFiSTAConnectStatus wifi_sta_connect_status_()
std::array< uint8_t, 6 > bssid_t
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
const optional< EAPAuth > & get_eap() const
const std::string & get_password() const
network::IPAddress wifi_dns_ip_(int num)
network::IPAddresses wifi_sta_ip_addresses()
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
const optional< bssid_t > & get_bssid() const
bool wifi_apply_output_power_(float output_power)
bool wifi_sta_ip_config_(optional< ManualIP > manual_ip)
void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
network::IPAddress wifi_subnet_mask_()
bool wifi_apply_hostname_()
std::string format_ip6_addr(const esp_ip6_addr_t &ip)
bool wifi_start_ap_(const WiFiAP &ap)
void wifi_process_event_(IDFWiFiEvent *data)
const optional< ManualIP > & get_manual_ip() const
const optional< uint8_t > & get_channel() const
BedjetMode mode
BedJet operating mode.
esp_eap_ttls_phase2_types ttls_phase_2
bool wifi_apply_power_save_()
Application App
Global storage of Application pointer - only one Application can exist.
bool wifi_ap_ip_config_(optional< ManualIP > manual_ip)
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
const std::string & get_name() const
Get the name of this Application set by pre_setup().
std::array< IPAddress, 5 > IPAddresses
network::IPAddress wifi_soft_ap_ip()
bool wifi_sta_pre_setup_()
const char * get_auth_mode_str(uint8_t mode)
network::IPAddress wifi_gateway_ip_()
bool wifi_scan_start_(bool passive)
Implementation of SPI Controller mode.
std::string format_ip4_addr(const esphome_ip4_addr_t &ip)
const std::string & get_ssid() const
const char * get_disconnect_reason_str(uint8_t reason)
std::string str_snprintf(const char *fmt, size_t len,...)
value_type value_or(U const &v) const
bool wifi_sta_connect_(const WiFiAP &ap)
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).