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 if (ap.
get_ssid().size() >
sizeof(conf.sta.ssid)) {
293 ESP_LOGE(TAG,
"SSID is too long");
296 if (ap.
get_password().size() >
sizeof(conf.sta.password)) {
297 ESP_LOGE(TAG,
"password is too long");
300 memcpy(reinterpret_cast<char *>(conf.sta.ssid), ap.
get_ssid().c_str(), ap.
get_ssid().size());
305 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
307 conf.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
310 #ifdef USE_WIFI_WPA2_EAP 311 if (ap.
get_eap().has_value()) {
312 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_ENTERPRISE;
316 #ifdef USE_WIFI_11KV_SUPPORT 317 conf.sta.btm_enabled = this->btm_;
318 conf.sta.rm_enabled = this->rrm_;
322 conf.sta.bssid_set =
true;
323 memcpy(conf.sta.bssid, ap.
get_bssid()->data(), 6);
325 conf.sta.bssid_set =
false;
329 conf.sta.scan_method = WIFI_FAST_SCAN;
331 conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
335 conf.sta.listen_interval = 0;
339 conf.sta.pmf_cfg.capable =
true;
340 conf.sta.pmf_cfg.required =
false;
344 conf.sta.threshold.rssi = -127;
346 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
348 wifi_config_t current_conf;
350 err = esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
352 ESP_LOGW(TAG,
"esp_wifi_get_config failed: %s", esp_err_to_name(err));
356 if (memcmp(¤t_conf, &conf,
sizeof(wifi_config_t)) != 0) {
357 err = esp_wifi_disconnect();
359 ESP_LOGV(TAG,
"esp_wifi_disconnect failed: %s", esp_err_to_name(err));
364 err = esp_wifi_set_config(WIFI_IF_STA, &conf);
366 ESP_LOGV(TAG,
"esp_wifi_set_config failed: %s", esp_err_to_name(err));
375 #ifdef USE_WIFI_WPA2_EAP 376 if (ap.
get_eap().has_value()) {
379 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 380 err = esp_eap_client_set_identity((uint8_t *) eap.
identity.c_str(), eap.
identity.length());
382 err = esp_wifi_sta_wpa2_ent_set_identity((uint8_t *) eap.
identity.c_str(), eap.
identity.length());
385 ESP_LOGV(TAG,
"set_identity failed %d", err);
387 int ca_cert_len = strlen(eap.
ca_cert);
391 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 392 err = esp_eap_client_set_ca_cert((uint8_t *) eap.
ca_cert, ca_cert_len + 1);
394 err = esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *) eap.
ca_cert, ca_cert_len + 1);
397 ESP_LOGV(TAG,
"set_ca_cert failed %d", err);
402 if (client_cert_len && client_key_len) {
404 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 405 err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.
client_cert, client_cert_len + 1,
406 (uint8_t *) eap.
client_key, client_key_len + 1,
409 err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.
client_cert, client_cert_len + 1,
410 (uint8_t *) eap.
client_key, client_key_len + 1,
414 ESP_LOGV(TAG,
"set_cert_key failed %d", err);
418 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 419 err = esp_eap_client_set_username((uint8_t *) eap.
username.c_str(), eap.
username.length());
421 err = esp_wifi_sta_wpa2_ent_set_username((uint8_t *) eap.
username.c_str(), eap.
username.length());
424 ESP_LOGV(TAG,
"set_username failed %d", err);
426 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 427 err = esp_eap_client_set_password((uint8_t *) eap.
password.c_str(), eap.
password.length());
429 err = esp_wifi_sta_wpa2_ent_set_password((uint8_t *) eap.
password.c_str(), eap.
password.length());
432 ESP_LOGV(TAG,
"set_password failed %d", err);
435 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 436 err = esp_eap_client_set_ttls_phase2_method(eap.
ttls_phase_2);
438 err = esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(eap.
ttls_phase_2);
441 ESP_LOGV(TAG,
"set_ttls_phase2_method failed %d", err);
444 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 445 err = esp_wifi_sta_enterprise_enable();
447 err = esp_wifi_sta_wpa2_ent_enable();
450 ESP_LOGV(TAG,
"enterprise_enable failed %d", err);
453 #endif // USE_WIFI_WPA2_EAP 457 s_sta_connecting =
true;
458 s_sta_connected =
false;
459 s_sta_connect_error =
false;
460 s_sta_connect_not_found =
false;
462 err = esp_wifi_connect();
464 ESP_LOGW(TAG,
"esp_wifi_connect failed: %s", esp_err_to_name(err));
473 if (!this->wifi_mode_(
true, {}))
476 esp_netif_dhcp_status_t dhcp_status;
477 esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
479 ESP_LOGV(TAG,
"esp_netif_dhcpc_get_status failed: %s", esp_err_to_name(err));
487 sntp_servermode_dhcp(
false);
490 if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
491 err = esp_netif_dhcpc_start(s_sta_netif);
493 ESP_LOGV(TAG,
"Starting DHCP client failed! %d", err);
495 return err == ESP_OK;
500 esp_netif_ip_info_t info;
501 info.ip = manual_ip->static_ip;
502 info.gw = manual_ip->gateway;
503 info.netmask = manual_ip->subnet;
504 err = esp_netif_dhcpc_stop(s_sta_netif);
505 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
506 ESP_LOGV(TAG,
"Stopping DHCP client failed! %s", esp_err_to_name(err));
509 err = esp_netif_set_ip_info(s_sta_netif, &info);
511 ESP_LOGV(TAG,
"Setting manual IP info failed! %s", esp_err_to_name(err));
514 esp_netif_dns_info_t dns;
515 if (manual_ip->dns1.is_set()) {
516 dns.ip = manual_ip->dns1;
517 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
519 if (manual_ip->dns2.is_set()) {
520 dns.ip = manual_ip->dns2;
521 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
528 if (!this->has_sta())
531 esp_netif_ip_info_t ip;
532 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
534 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
541 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
543 count = esp_netif_get_all_ip6(s_sta_netif, if_ip6s);
544 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
545 for (
int i = 0; i < count; i++) {
562 case WIFI_AUTH_WPA_PSK:
564 case WIFI_AUTH_WPA2_PSK:
566 case WIFI_AUTH_WPA_WPA2_PSK:
567 return "WPA/WPA2 PSK";
568 case WIFI_AUTH_WPA2_ENTERPRISE:
569 return "WPA2 Enterprise";
570 case WIFI_AUTH_WPA3_PSK:
572 case WIFI_AUTH_WPA2_WPA3_PSK:
573 return "WPA2/WPA3 PSK";
574 case WIFI_AUTH_WAPI_PSK:
587 case WIFI_REASON_AUTH_EXPIRE:
588 return "Auth Expired";
589 case WIFI_REASON_AUTH_LEAVE:
591 case WIFI_REASON_ASSOC_EXPIRE:
592 return "Association Expired";
593 case WIFI_REASON_ASSOC_TOOMANY:
594 return "Too Many Associations";
595 case WIFI_REASON_NOT_AUTHED:
596 return "Not Authenticated";
597 case WIFI_REASON_NOT_ASSOCED:
598 return "Not Associated";
599 case WIFI_REASON_ASSOC_LEAVE:
600 return "Association Leave";
601 case WIFI_REASON_ASSOC_NOT_AUTHED:
602 return "Association not Authenticated";
603 case WIFI_REASON_DISASSOC_PWRCAP_BAD:
604 return "Disassociate Power Cap Bad";
605 case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
606 return "Disassociate Supported Channel Bad";
607 case WIFI_REASON_IE_INVALID:
609 case WIFI_REASON_MIC_FAILURE:
610 return "Mic Failure";
611 case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
612 return "4-Way Handshake Timeout";
613 case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
614 return "Group Key Update Timeout";
615 case WIFI_REASON_IE_IN_4WAY_DIFFERS:
616 return "IE In 4-Way Handshake Differs";
617 case WIFI_REASON_GROUP_CIPHER_INVALID:
618 return "Group Cipher Invalid";
619 case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
620 return "Pairwise Cipher Invalid";
621 case WIFI_REASON_AKMP_INVALID:
622 return "AKMP Invalid";
623 case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
624 return "Unsupported RSN IE version";
625 case WIFI_REASON_INVALID_RSN_IE_CAP:
626 return "Invalid RSN IE Cap";
627 case WIFI_REASON_802_1X_AUTH_FAILED:
628 return "802.1x Authentication Failed";
629 case WIFI_REASON_CIPHER_SUITE_REJECTED:
630 return "Cipher Suite Rejected";
631 case WIFI_REASON_BEACON_TIMEOUT:
632 return "Beacon Timeout";
633 case WIFI_REASON_NO_AP_FOUND:
634 return "AP Not Found";
635 case WIFI_REASON_AUTH_FAIL:
636 return "Authentication Failed";
637 case WIFI_REASON_ASSOC_FAIL:
638 return "Association Failed";
639 case WIFI_REASON_HANDSHAKE_TIMEOUT:
640 return "Handshake Failed";
641 case WIFI_REASON_CONNECTION_FAIL:
642 return "Connection Failed";
643 case WIFI_REASON_ROAMING:
644 return "Station Roaming";
645 case WIFI_REASON_UNSPECIFIED:
647 return "Unspecified";
654 if (xQueueReceive(s_event_queue, &data, 0L) != pdTRUE) {
660 wifi_process_event_(data);
667 if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_START) {
668 ESP_LOGV(TAG,
"Event: WiFi STA start");
670 err = esp_netif_set_hostname(s_sta_netif,
App.
get_name().c_str());
672 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
675 s_sta_started =
true;
677 wifi_apply_power_save_();
679 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
680 ESP_LOGV(TAG,
"Event: WiFi STA stop");
681 s_sta_started =
false;
683 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
684 const auto &it = data->data.sta_authmode_change;
685 ESP_LOGV(TAG,
"Event: Authmode Change old=%s new=%s",
get_auth_mode_str(it.old_mode),
688 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_CONNECTED) {
689 const auto &it = data->data.sta_connected;
691 assert(it.ssid_len <= 32);
692 memcpy(buf, it.ssid, it.ssid_len);
693 buf[it.ssid_len] =
'\0';
694 ESP_LOGV(TAG,
"Event: Connected ssid='%s' bssid=" LOG_SECRET(
"%s")
" channel=%u, authmode=%s", buf,
696 s_sta_connected =
true;
698 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_DISCONNECTED) {
699 const auto &it = data->data.sta_disconnected;
701 assert(it.ssid_len <= 32);
702 memcpy(buf, it.ssid, it.ssid_len);
703 buf[it.ssid_len] =
'\0';
704 if (it.reason == WIFI_REASON_NO_AP_FOUND) {
705 ESP_LOGW(TAG,
"Event: Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf);
706 s_sta_connect_not_found =
true;
707 }
else if (it.reason == WIFI_REASON_ROAMING) {
708 ESP_LOGI(TAG,
"Event: Disconnected ssid='%s' reason='Station Roaming'", buf);
711 ESP_LOGW(TAG,
"Event: Disconnected ssid='%s' bssid=" LOG_SECRET(
"%s")
" reason='%s'", buf,
713 s_sta_connect_error =
true;
715 s_sta_connected =
false;
716 s_sta_connecting =
false;
717 error_from_callback_ =
true;
719 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
720 const auto &it = data->data.ip_got_ip;
722 esp_netif_create_ip6_linklocal(s_sta_netif);
724 ESP_LOGV(TAG,
"Event: Got IP static_ip=%s gateway=%s",
format_ip4_addr(it.ip_info.ip).c_str(),
726 this->got_ipv4_address_ =
true;
729 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
730 const auto &it = data->data.ip_got_ip6;
731 ESP_LOGV(TAG,
"Event: Got IPv6 address=%s",
format_ip6_addr(it.ip6_info.ip).c_str());
732 this->num_ipv6_addresses_++;
735 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_LOST_IP) {
736 ESP_LOGV(TAG,
"Event: Lost IP");
737 this->got_ipv4_address_ =
false;
739 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_SCAN_DONE) {
740 const auto &it = data->data.sta_scan_done;
741 ESP_LOGV(TAG,
"Event: WiFi Scan Done status=%" PRIu32
" number=%u scan_id=%u", it.status, it.number, it.scan_id);
743 scan_result_.clear();
744 this->scan_done_ =
true;
745 if (it.status != 0) {
750 if (it.number == 0) {
755 uint16_t number = it.number;
756 std::vector<wifi_ap_record_t> records(number);
757 err = esp_wifi_scan_get_ap_records(&number, records.data());
759 ESP_LOGW(TAG,
"esp_wifi_scan_get_ap_records failed: %s", esp_err_to_name(err));
762 records.resize(number);
764 scan_result_.reserve(number);
765 for (
int i = 0; i < number; i++) {
766 auto &record = records[i];
768 std::copy(record.bssid, record.bssid + 6, bssid.begin());
769 std::string ssid(reinterpret_cast<const char *>(record.ssid));
770 WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty());
771 scan_result_.push_back(result);
774 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {
775 ESP_LOGV(TAG,
"Event: WiFi AP start");
778 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
779 ESP_LOGV(TAG,
"Event: WiFi AP stop");
780 s_ap_started =
false;
782 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
783 const auto &it = data->data.ap_probe_req_rx;
784 ESP_LOGVV(TAG,
"Event: AP receive Probe Request MAC=%s RSSI=%d", format_mac_addr(it.mac).c_str(), it.rssi);
786 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STACONNECTED) {
787 const auto &it = data->data.ap_staconnected;
788 ESP_LOGV(TAG,
"Event: AP client connected MAC=%s", format_mac_addr(it.mac).c_str());
790 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STADISCONNECTED) {
791 const auto &it = data->data.ap_stadisconnected;
792 ESP_LOGV(TAG,
"Event: AP client disconnected MAC=%s", format_mac_addr(it.mac).c_str());
794 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_AP_STAIPASSIGNED) {
795 const auto &it = data->data.ip_ap_staipassigned;
796 ESP_LOGV(TAG,
"Event: AP client assigned IP %s",
format_ip4_addr(it.ip).c_str());
801 if (s_sta_connected && this->got_ipv4_address_) {
802 #if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) 803 if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
810 if (s_sta_connect_error) {
813 if (s_sta_connect_not_found) {
816 if (s_sta_connecting) {
823 if (!this->wifi_mode_(
true, {}))
826 wifi_scan_config_t config{};
827 config.ssid =
nullptr;
828 config.bssid =
nullptr;
830 config.show_hidden =
true;
831 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
833 config.scan_time.passive = 300;
835 config.scan_time.active.min = 100;
836 config.scan_time.active.max = 300;
839 esp_err_t err = esp_wifi_scan_start(&config,
false);
841 ESP_LOGV(TAG,
"esp_wifi_scan_start failed: %s", esp_err_to_name(err));
845 this->scan_done_ =
false;
854 if (!this->wifi_mode_({},
true))
857 esp_netif_ip_info_t info;
859 info.ip = manual_ip->static_ip;
860 info.gw = manual_ip->gateway;
861 info.netmask = manual_ip->subnet;
868 err = esp_netif_dhcps_stop(s_ap_netif);
869 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
870 ESP_LOGE(TAG,
"esp_netif_dhcps_stop failed: %s", esp_err_to_name(err));
874 err = esp_netif_set_ip_info(s_ap_netif, &info);
876 ESP_LOGE(TAG,
"esp_netif_set_ip_info failed! %d", err);
884 lease.start_ip = start_address;
885 ESP_LOGV(TAG,
"DHCP server IP lease start: %s", start_address.
str().c_str());
887 lease.end_ip = start_address;
888 ESP_LOGV(TAG,
"DHCP server IP lease end: %s", start_address.str().c_str());
889 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease,
sizeof(lease));
892 ESP_LOGE(TAG,
"esp_netif_dhcps_option failed! %d", err);
896 err = esp_netif_dhcps_start(s_ap_netif);
899 ESP_LOGE(TAG,
"esp_netif_dhcps_start failed! %d", err);
908 if (!this->wifi_mode_({},
true))
912 memset(&conf, 0,
sizeof(conf));
913 if (ap.
get_ssid().size() >
sizeof(conf.ap.ssid)) {
914 ESP_LOGE(TAG,
"AP SSID is too long");
917 memcpy(reinterpret_cast<char *>(conf.ap.ssid), ap.
get_ssid().c_str(), ap.
get_ssid().size());
919 conf.ap.ssid_hidden = ap.
get_ssid().size();
920 conf.ap.max_connection = 5;
921 conf.ap.beacon_interval = 100;
924 conf.ap.authmode = WIFI_AUTH_OPEN;
925 *conf.ap.password = 0;
927 conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
928 if (ap.
get_password().size() >
sizeof(conf.ap.password)) {
929 ESP_LOGE(TAG,
"AP password is too long");
936 conf.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
938 esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &conf);
940 ESP_LOGE(TAG,
"esp_wifi_set_config failed! %d", err);
945 ESP_LOGE(TAG,
"wifi_ap_ip_config_ failed!");
953 esp_netif_ip_info_t ip;
954 esp_netif_get_ip_info(s_ap_netif, &ip);
957 #endif // USE_WIFI_AP 963 wifi_ap_record_t info;
964 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
966 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
969 std::copy(info.bssid, info.bssid + 6, bssid.begin());
973 wifi_ap_record_t info{};
974 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
976 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
979 auto *ssid_s =
reinterpret_cast<const char *
>(info.ssid);
980 size_t len = strnlen(ssid_s,
sizeof(info.ssid));
981 return {ssid_s, len};
984 wifi_ap_record_t info;
985 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
987 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
994 wifi_second_chan_t
second;
995 esp_err_t err = esp_wifi_get_channel(&primary, &second);
997 ESP_LOGW(TAG,
"esp_wifi_get_channel failed: %s", esp_err_to_name(err));
1003 esp_netif_ip_info_t ip;
1004 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1005 if (err != ESP_OK) {
1006 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1012 esp_netif_ip_info_t ip;
1013 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1014 if (err != ESP_OK) {
1015 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1021 const ip_addr_t *dns_ip = dns_getserver(num);
1028 #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)
int32_t get_wifi_channel()
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).