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) {
133 #ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC 139 esp_err_t err = esp_netif_init();
141 ESP_LOGE(TAG,
"esp_netif_init failed: %s", esp_err_to_name(err));
144 s_wifi_event_group = xEventGroupCreate();
145 if (s_wifi_event_group ==
nullptr) {
146 ESP_LOGE(TAG,
"xEventGroupCreate failed");
150 s_event_queue = xQueueCreate(64,
sizeof(IDFWiFiEvent *));
151 if (s_event_queue ==
nullptr) {
152 ESP_LOGE(TAG,
"xQueueCreate failed");
155 err = esp_event_loop_create_default();
157 ESP_LOGE(TAG,
"esp_event_loop_create_default failed: %s", esp_err_to_name(err));
160 esp_event_handler_instance_t instance_wifi_id, instance_ip_id;
161 err = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &
event_handler,
nullptr, &instance_wifi_id);
163 ESP_LOGE(TAG,
"esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
166 err = esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &
event_handler,
nullptr, &instance_ip_id);
168 ESP_LOGE(TAG,
"esp_event_handler_instance_register failed: %s", esp_err_to_name(err));
172 s_sta_netif = esp_netif_create_default_wifi_sta();
175 s_ap_netif = esp_netif_create_default_wifi_ap();
176 #endif // USE_WIFI_AP 178 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
180 err = esp_wifi_init(&cfg);
182 ESP_LOGE(TAG,
"esp_wifi_init failed: %s", esp_err_to_name(err));
185 err = esp_wifi_set_storage(WIFI_STORAGE_RAM);
187 ESP_LOGE(TAG,
"esp_wifi_set_storage failed: %s", esp_err_to_name(err));
194 wifi_mode_t current_mode = WIFI_MODE_NULL;
195 if (s_wifi_started) {
196 err = esp_wifi_get_mode(¤t_mode);
198 ESP_LOGW(TAG,
"esp_wifi_get_mode failed: %s", esp_err_to_name(err));
202 bool current_sta = current_mode == WIFI_MODE_STA || current_mode == WIFI_MODE_APSTA;
203 bool current_ap = current_mode == WIFI_MODE_AP || current_mode == WIFI_MODE_APSTA;
205 bool set_sta = sta.
value_or(current_sta);
206 bool set_ap = ap.
value_or(current_ap);
208 wifi_mode_t set_mode;
209 if (set_sta && set_ap) {
210 set_mode = WIFI_MODE_APSTA;
211 }
else if (set_sta && !set_ap) {
212 set_mode = WIFI_MODE_STA;
213 }
else if (!set_sta && set_ap) {
214 set_mode = WIFI_MODE_AP;
216 set_mode = WIFI_MODE_NULL;
219 if (current_mode == set_mode)
222 if (set_sta && !current_sta) {
223 ESP_LOGV(TAG,
"Enabling STA.");
224 }
else if (!set_sta && current_sta) {
225 ESP_LOGV(TAG,
"Disabling STA.");
227 if (set_ap && !current_ap) {
228 ESP_LOGV(TAG,
"Enabling AP.");
229 }
else if (!set_ap && current_ap) {
230 ESP_LOGV(TAG,
"Disabling AP.");
233 if (set_mode == WIFI_MODE_NULL && s_wifi_started) {
234 err = esp_wifi_stop();
236 ESP_LOGV(TAG,
"esp_wifi_stop failed: %s", esp_err_to_name(err));
239 s_wifi_started =
false;
243 err = esp_wifi_set_mode(set_mode);
245 ESP_LOGW(TAG,
"esp_wifi_set_mode failed: %s", esp_err_to_name(err));
249 if (set_mode != WIFI_MODE_NULL && !s_wifi_started) {
250 err = esp_wifi_start();
252 ESP_LOGV(TAG,
"esp_wifi_start failed: %s", esp_err_to_name(err));
255 s_wifi_started =
true;
264 int8_t
val =
static_cast<int8_t
>(output_power * 4);
265 return esp_wifi_set_max_tx_power(val) == ESP_OK;
269 wifi_ps_type_t power_save;
270 switch (this->power_save_) {
272 power_save = WIFI_PS_MIN_MODEM;
275 power_save = WIFI_PS_MAX_MODEM;
279 power_save = WIFI_PS_NONE;
282 return esp_wifi_set_ps(power_save) == ESP_OK;
287 if (!this->wifi_mode_(
true, {}))
292 memset(&conf, 0,
sizeof(conf));
293 strncpy(reinterpret_cast<char *>(conf.sta.ssid), ap.
get_ssid().c_str(),
sizeof(conf.sta.ssid));
294 strncpy(reinterpret_cast<char *>(conf.sta.password), ap.
get_password().c_str(),
sizeof(conf.sta.password));
298 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
300 conf.sta.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK;
303 #ifdef USE_WIFI_WPA2_EAP 304 if (ap.
get_eap().has_value()) {
305 conf.sta.threshold.authmode = WIFI_AUTH_WPA2_ENTERPRISE;
309 #ifdef USE_WIFI_11KV_SUPPORT 310 conf.sta.btm_enabled = this->btm_;
311 conf.sta.rm_enabled = this->rrm_;
315 conf.sta.bssid_set =
true;
316 memcpy(conf.sta.bssid, ap.
get_bssid()->data(), 6);
318 conf.sta.bssid_set =
false;
322 conf.sta.scan_method = WIFI_FAST_SCAN;
324 conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
328 conf.sta.listen_interval = 0;
332 conf.sta.pmf_cfg.capable =
true;
333 conf.sta.pmf_cfg.required =
false;
337 conf.sta.threshold.rssi = -127;
339 conf.sta.threshold.authmode = WIFI_AUTH_OPEN;
341 wifi_config_t current_conf;
343 err = esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
345 ESP_LOGW(TAG,
"esp_wifi_get_config failed: %s", esp_err_to_name(err));
349 if (memcmp(¤t_conf, &conf,
sizeof(wifi_config_t)) != 0) {
350 err = esp_wifi_disconnect();
352 ESP_LOGV(TAG,
"esp_wifi_disconnect failed: %s", esp_err_to_name(err));
357 err = esp_wifi_set_config(WIFI_IF_STA, &conf);
359 ESP_LOGV(TAG,
"esp_wifi_set_config failed: %s", esp_err_to_name(err));
368 #ifdef USE_WIFI_WPA2_EAP 369 if (ap.
get_eap().has_value()) {
372 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 373 err = esp_eap_client_set_identity((uint8_t *) eap.
identity.c_str(), eap.
identity.length());
375 err = esp_wifi_sta_wpa2_ent_set_identity((uint8_t *) eap.
identity.c_str(), eap.
identity.length());
378 ESP_LOGV(TAG,
"set_identity failed %d", err);
380 int ca_cert_len = strlen(eap.
ca_cert);
384 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 385 err = esp_eap_client_set_ca_cert((uint8_t *) eap.
ca_cert, ca_cert_len + 1);
387 err = esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *) eap.
ca_cert, ca_cert_len + 1);
390 ESP_LOGV(TAG,
"set_ca_cert failed %d", err);
395 if (client_cert_len && client_key_len) {
397 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 398 err = esp_eap_client_set_certificate_and_key((uint8_t *) eap.
client_cert, client_cert_len + 1,
399 (uint8_t *) eap.
client_key, client_key_len + 1,
402 err = esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *) eap.
client_cert, client_cert_len + 1,
403 (uint8_t *) eap.
client_key, client_key_len + 1,
407 ESP_LOGV(TAG,
"set_cert_key failed %d", err);
411 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 412 err = esp_eap_client_set_username((uint8_t *) eap.
username.c_str(), eap.
username.length());
414 err = esp_wifi_sta_wpa2_ent_set_username((uint8_t *) eap.
username.c_str(), eap.
username.length());
417 ESP_LOGV(TAG,
"set_username failed %d", err);
419 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 420 err = esp_eap_client_set_password((uint8_t *) eap.
password.c_str(), eap.
password.length());
422 err = esp_wifi_sta_wpa2_ent_set_password((uint8_t *) eap.
password.c_str(), eap.
password.length());
425 ESP_LOGV(TAG,
"set_password failed %d", err);
428 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 429 err = esp_eap_client_set_ttls_phase2_method(eap.
ttls_phase_2);
431 err = esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(eap.
ttls_phase_2);
434 ESP_LOGV(TAG,
"set_ttls_phase2_method failed %d", err);
437 #if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1) 438 err = esp_wifi_sta_enterprise_enable();
440 err = esp_wifi_sta_wpa2_ent_enable();
443 ESP_LOGV(TAG,
"enterprise_enable failed %d", err);
446 #endif // USE_WIFI_WPA2_EAP 450 s_sta_connecting =
true;
451 s_sta_connected =
false;
452 s_sta_connect_error =
false;
453 s_sta_connect_not_found =
false;
455 err = esp_wifi_connect();
457 ESP_LOGW(TAG,
"esp_wifi_connect failed: %s", esp_err_to_name(err));
466 if (!this->wifi_mode_(
true, {}))
469 esp_netif_dhcp_status_t dhcp_status;
470 esp_err_t err = esp_netif_dhcpc_get_status(s_sta_netif, &dhcp_status);
472 ESP_LOGV(TAG,
"esp_netif_dhcpc_get_status failed: %s", esp_err_to_name(err));
480 sntp_servermode_dhcp(
false);
483 if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
484 err = esp_netif_dhcpc_start(s_sta_netif);
486 ESP_LOGV(TAG,
"Starting DHCP client failed! %d", err);
488 return err == ESP_OK;
493 esp_netif_ip_info_t info;
494 info.ip = manual_ip->static_ip;
495 info.gw = manual_ip->gateway;
496 info.netmask = manual_ip->subnet;
497 err = esp_netif_dhcpc_stop(s_sta_netif);
498 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
499 ESP_LOGV(TAG,
"Stopping DHCP client failed! %s", esp_err_to_name(err));
502 err = esp_netif_set_ip_info(s_sta_netif, &info);
504 ESP_LOGV(TAG,
"Setting manual IP info failed! %s", esp_err_to_name(err));
507 esp_netif_dns_info_t dns;
508 if (manual_ip->dns1.is_set()) {
509 dns.ip = manual_ip->dns1;
510 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
512 if (manual_ip->dns2.is_set()) {
513 dns.ip = manual_ip->dns2;
514 esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_BACKUP, &dns);
521 if (!this->has_sta())
524 esp_netif_ip_info_t ip;
525 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
527 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
534 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
536 count = esp_netif_get_all_ip6(s_sta_netif, if_ip6s);
537 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
538 for (
int i = 0; i < count; i++) {
555 case WIFI_AUTH_WPA_PSK:
557 case WIFI_AUTH_WPA2_PSK:
559 case WIFI_AUTH_WPA_WPA2_PSK:
560 return "WPA/WPA2 PSK";
561 case WIFI_AUTH_WPA2_ENTERPRISE:
562 return "WPA2 Enterprise";
563 case WIFI_AUTH_WPA3_PSK:
565 case WIFI_AUTH_WPA2_WPA3_PSK:
566 return "WPA2/WPA3 PSK";
567 case WIFI_AUTH_WAPI_PSK:
580 case WIFI_REASON_AUTH_EXPIRE:
581 return "Auth Expired";
582 case WIFI_REASON_AUTH_LEAVE:
584 case WIFI_REASON_ASSOC_EXPIRE:
585 return "Association Expired";
586 case WIFI_REASON_ASSOC_TOOMANY:
587 return "Too Many Associations";
588 case WIFI_REASON_NOT_AUTHED:
589 return "Not Authenticated";
590 case WIFI_REASON_NOT_ASSOCED:
591 return "Not Associated";
592 case WIFI_REASON_ASSOC_LEAVE:
593 return "Association Leave";
594 case WIFI_REASON_ASSOC_NOT_AUTHED:
595 return "Association not Authenticated";
596 case WIFI_REASON_DISASSOC_PWRCAP_BAD:
597 return "Disassociate Power Cap Bad";
598 case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
599 return "Disassociate Supported Channel Bad";
600 case WIFI_REASON_IE_INVALID:
602 case WIFI_REASON_MIC_FAILURE:
603 return "Mic Failure";
604 case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
605 return "4-Way Handshake Timeout";
606 case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
607 return "Group Key Update Timeout";
608 case WIFI_REASON_IE_IN_4WAY_DIFFERS:
609 return "IE In 4-Way Handshake Differs";
610 case WIFI_REASON_GROUP_CIPHER_INVALID:
611 return "Group Cipher Invalid";
612 case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
613 return "Pairwise Cipher Invalid";
614 case WIFI_REASON_AKMP_INVALID:
615 return "AKMP Invalid";
616 case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
617 return "Unsupported RSN IE version";
618 case WIFI_REASON_INVALID_RSN_IE_CAP:
619 return "Invalid RSN IE Cap";
620 case WIFI_REASON_802_1X_AUTH_FAILED:
621 return "802.1x Authentication Failed";
622 case WIFI_REASON_CIPHER_SUITE_REJECTED:
623 return "Cipher Suite Rejected";
624 case WIFI_REASON_BEACON_TIMEOUT:
625 return "Beacon Timeout";
626 case WIFI_REASON_NO_AP_FOUND:
627 return "AP Not Found";
628 case WIFI_REASON_AUTH_FAIL:
629 return "Authentication Failed";
630 case WIFI_REASON_ASSOC_FAIL:
631 return "Association Failed";
632 case WIFI_REASON_HANDSHAKE_TIMEOUT:
633 return "Handshake Failed";
634 case WIFI_REASON_CONNECTION_FAIL:
635 return "Connection Failed";
636 case WIFI_REASON_ROAMING:
637 return "Station Roaming";
638 case WIFI_REASON_UNSPECIFIED:
640 return "Unspecified";
647 if (xQueueReceive(s_event_queue, &data, 0L) != pdTRUE) {
653 wifi_process_event_(data);
660 if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_START) {
661 ESP_LOGV(TAG,
"Event: WiFi STA start");
663 err = esp_netif_set_hostname(s_sta_netif,
App.
get_name().c_str());
665 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
668 s_sta_started =
true;
670 wifi_apply_power_save_();
672 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_STOP) {
673 ESP_LOGV(TAG,
"Event: WiFi STA stop");
674 s_sta_started =
false;
676 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
677 const auto &it = data->data.sta_authmode_change;
678 ESP_LOGV(TAG,
"Event: Authmode Change old=%s new=%s",
get_auth_mode_str(it.old_mode),
681 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_CONNECTED) {
682 const auto &it = data->data.sta_connected;
684 assert(it.ssid_len <= 32);
685 memcpy(buf, it.ssid, it.ssid_len);
686 buf[it.ssid_len] =
'\0';
687 ESP_LOGV(TAG,
"Event: Connected ssid='%s' bssid=" LOG_SECRET(
"%s")
" channel=%u, authmode=%s", buf,
689 s_sta_connected =
true;
691 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_STA_DISCONNECTED) {
692 const auto &it = data->data.sta_disconnected;
694 assert(it.ssid_len <= 32);
695 memcpy(buf, it.ssid, it.ssid_len);
696 buf[it.ssid_len] =
'\0';
697 if (it.reason == WIFI_REASON_NO_AP_FOUND) {
698 ESP_LOGW(TAG,
"Event: Disconnected ssid='%s' reason='Probe Request Unsuccessful'", buf);
699 s_sta_connect_not_found =
true;
700 }
else if (it.reason == WIFI_REASON_ROAMING) {
701 ESP_LOGI(TAG,
"Event: Disconnected ssid='%s' reason='Station Roaming'", buf);
704 ESP_LOGW(TAG,
"Event: Disconnected ssid='%s' bssid=" LOG_SECRET(
"%s")
" reason='%s'", buf,
706 s_sta_connect_error =
true;
708 s_sta_connected =
false;
709 s_sta_connecting =
false;
710 error_from_callback_ =
true;
712 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_GOT_IP) {
713 const auto &it = data->data.ip_got_ip;
715 esp_netif_create_ip6_linklocal(s_sta_netif);
717 ESP_LOGV(TAG,
"Event: Got IP static_ip=%s gateway=%s",
format_ip4_addr(it.ip_info.ip).c_str(),
719 this->got_ipv4_address_ =
true;
722 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_GOT_IP6) {
723 const auto &it = data->data.ip_got_ip6;
724 ESP_LOGV(TAG,
"Event: Got IPv6 address=%s",
format_ip6_addr(it.ip6_info.ip).c_str());
725 this->num_ipv6_addresses_++;
728 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_STA_LOST_IP) {
729 ESP_LOGV(TAG,
"Event: Lost IP");
730 this->got_ipv4_address_ =
false;
732 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_SCAN_DONE) {
733 const auto &it = data->data.sta_scan_done;
734 ESP_LOGV(TAG,
"Event: WiFi Scan Done status=%" PRIu32
" number=%u scan_id=%u", it.status, it.number, it.scan_id);
736 scan_result_.clear();
737 this->scan_done_ =
true;
738 if (it.status != 0) {
743 if (it.number == 0) {
748 uint16_t number = it.number;
749 std::vector<wifi_ap_record_t> records(number);
750 err = esp_wifi_scan_get_ap_records(&number, records.data());
752 ESP_LOGW(TAG,
"esp_wifi_scan_get_ap_records failed: %s", esp_err_to_name(err));
755 records.resize(number);
757 scan_result_.reserve(number);
758 for (
int i = 0; i < number; i++) {
759 auto &record = records[i];
761 std::copy(record.bssid, record.bssid + 6, bssid.begin());
762 std::string ssid(reinterpret_cast<const char *>(record.ssid));
763 WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty());
764 scan_result_.push_back(result);
767 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {
768 ESP_LOGV(TAG,
"Event: WiFi AP start");
771 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STOP) {
772 ESP_LOGV(TAG,
"Event: WiFi AP stop");
773 s_ap_started =
false;
775 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
776 const auto &it = data->data.ap_probe_req_rx;
777 ESP_LOGVV(TAG,
"Event: AP receive Probe Request MAC=%s RSSI=%d", format_mac_addr(it.mac).c_str(), it.rssi);
779 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STACONNECTED) {
780 const auto &it = data->data.ap_staconnected;
781 ESP_LOGV(TAG,
"Event: AP client connected MAC=%s", format_mac_addr(it.mac).c_str());
783 }
else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_STADISCONNECTED) {
784 const auto &it = data->data.ap_stadisconnected;
785 ESP_LOGV(TAG,
"Event: AP client disconnected MAC=%s", format_mac_addr(it.mac).c_str());
787 }
else if (data->event_base == IP_EVENT && data->event_id == IP_EVENT_AP_STAIPASSIGNED) {
788 const auto &it = data->data.ip_ap_staipassigned;
789 ESP_LOGV(TAG,
"Event: AP client assigned IP %s",
format_ip4_addr(it.ip).c_str());
794 if (s_sta_connected && this->got_ipv4_address_) {
795 #if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) 796 if (this->num_ipv6_addresses_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT) {
803 if (s_sta_connect_error) {
806 if (s_sta_connect_not_found) {
809 if (s_sta_connecting) {
816 if (!this->wifi_mode_(
true, {}))
819 wifi_scan_config_t config{};
820 config.ssid =
nullptr;
821 config.bssid =
nullptr;
823 config.show_hidden =
true;
824 config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
826 config.scan_time.passive = 300;
828 config.scan_time.active.min = 100;
829 config.scan_time.active.max = 300;
832 esp_err_t err = esp_wifi_scan_start(&config,
false);
834 ESP_LOGV(TAG,
"esp_wifi_scan_start failed: %s", esp_err_to_name(err));
838 this->scan_done_ =
false;
847 if (!this->wifi_mode_({},
true))
850 esp_netif_ip_info_t info;
852 info.ip = manual_ip->static_ip;
853 info.gw = manual_ip->gateway;
854 info.netmask = manual_ip->subnet;
861 err = esp_netif_dhcps_stop(s_ap_netif);
862 if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
863 ESP_LOGE(TAG,
"esp_netif_dhcps_stop failed: %s", esp_err_to_name(err));
867 err = esp_netif_set_ip_info(s_ap_netif, &info);
869 ESP_LOGE(TAG,
"esp_netif_set_ip_info failed! %d", err);
877 lease.start_ip = start_address;
878 ESP_LOGV(TAG,
"DHCP server IP lease start: %s", start_address.
str().c_str());
880 lease.end_ip = start_address;
881 ESP_LOGV(TAG,
"DHCP server IP lease end: %s", start_address.str().c_str());
882 err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease,
sizeof(lease));
885 ESP_LOGE(TAG,
"esp_netif_dhcps_option failed! %d", err);
889 err = esp_netif_dhcps_start(s_ap_netif);
892 ESP_LOGE(TAG,
"esp_netif_dhcps_start failed! %d", err);
901 if (!this->wifi_mode_({},
true))
905 memset(&conf, 0,
sizeof(conf));
906 strncpy(reinterpret_cast<char *>(conf.ap.ssid), ap.
get_ssid().c_str(),
sizeof(conf.ap.ssid));
908 conf.ap.ssid_hidden = ap.
get_ssid().size();
909 conf.ap.max_connection = 5;
910 conf.ap.beacon_interval = 100;
913 conf.ap.authmode = WIFI_AUTH_OPEN;
914 *conf.ap.password = 0;
916 conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
917 strncpy(reinterpret_cast<char *>(conf.ap.password), ap.
get_password().c_str(),
sizeof(conf.ap.password));
921 conf.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP;
923 esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &conf);
925 ESP_LOGE(TAG,
"esp_wifi_set_config failed! %d", err);
930 ESP_LOGE(TAG,
"wifi_ap_ip_config_ failed!");
938 esp_netif_ip_info_t ip;
939 esp_netif_get_ip_info(s_ap_netif, &ip);
942 #endif // USE_WIFI_AP 948 wifi_ap_record_t info;
949 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
951 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
954 std::copy(info.bssid, info.bssid + 6, bssid.begin());
958 wifi_ap_record_t info{};
959 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
961 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
964 auto *ssid_s =
reinterpret_cast<const char *
>(info.ssid);
965 size_t len = strnlen(ssid_s,
sizeof(info.ssid));
966 return {ssid_s, len};
969 wifi_ap_record_t info;
970 esp_err_t err = esp_wifi_sta_get_ap_info(&info);
972 ESP_LOGW(TAG,
"esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
979 wifi_second_chan_t
second;
980 esp_err_t err = esp_wifi_get_channel(&primary, &second);
982 ESP_LOGW(TAG,
"esp_wifi_get_channel failed: %s", esp_err_to_name(err));
988 esp_netif_ip_info_t ip;
989 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
991 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
997 esp_netif_ip_info_t ip;
998 esp_err_t err = esp_netif_get_ip_info(s_sta_netif, &ip);
1000 ESP_LOGW(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
1006 const ip_addr_t *dns_ip = dns_getserver(num);
1013 #endif // USE_ESP_IDF
WiFiSTAConnectStatus wifi_sta_connect_status_()
std::array< uint8_t, 6 > bssid_t
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 get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
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).