Merge branch 'fix/fix_softap_sta_example_deafult_dns_addr_v5.2' into 'release/v5.2'

fix(wifi): fix softap_sta example default dns addr invaild issue backport for 5.2

See merge request espressif/esp-idf!33132
This commit is contained in:
Jiang Jiang Jian 2024-08-28 13:35:41 +08:00
commit 685c25d3ac
2 changed files with 18 additions and 3 deletions

View File

@ -56,9 +56,9 @@ menu "Example Configuration"
default ESP_WIFI_AUTH_WPA2_PSK default ESP_WIFI_AUTH_WPA2_PSK
help help
The weakest authmode to accept in the scan mode. The weakest authmode to accept in the scan mode.
This value defaults to ESP_WIFI_AUTH_WPA2_PSK incase password is present This value defaults to ESP_WIFI_AUTH_WPA2_PSK in case password is present
and ESP_WIFI_AUTH_OPEN is used. Please select ESP_WIFI_AUTH_WEP / ESP_WIFI_AUTH_WPA_PSK and ESP_WIFI_AUTH_OPEN is used. Please select ESP_WIFI_AUTH_WEP / ESP_WIFI_AUTH_WPA_PSK
incase AP is operating in WEP / WPA mode. in case AP is operating in WEP / WPA mode.
config ESP_WIFI_AUTH_OPEN config ESP_WIFI_AUTH_OPEN
bool "OPEN" bool "OPEN"

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -73,6 +73,9 @@
#define WIFI_CONNECTED_BIT BIT0 #define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1 #define WIFI_FAIL_BIT BIT1
/*DHCP server option*/
#define DHCPS_OFFER_DNS 0x02
static const char *TAG_AP = "WiFi SoftAP"; static const char *TAG_AP = "WiFi SoftAP";
static const char *TAG_STA = "WiFi Sta"; static const char *TAG_STA = "WiFi Sta";
@ -162,6 +165,17 @@ esp_netif_t *wifi_init_sta(void)
return esp_netif_sta; return esp_netif_sta;
} }
void softap_set_dns_addr(esp_netif_t *esp_netif_ap,esp_netif_t *esp_netif_sta)
{
esp_netif_dns_info_t dns;
esp_netif_get_dns_info(esp_netif_sta,ESP_NETIF_DNS_MAIN,&dns);
uint8_t dhcps_offer_option = DHCPS_OFFER_DNS;
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_dhcps_stop(esp_netif_ap));
ESP_ERROR_CHECK(esp_netif_dhcps_option(esp_netif_ap, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_offer_option, sizeof(dhcps_offer_option)));
ESP_ERROR_CHECK(esp_netif_set_dns_info(esp_netif_ap, ESP_NETIF_DNS_MAIN, &dns));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_dhcps_start(esp_netif_ap));
}
void app_main(void) void app_main(void)
{ {
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
@ -223,6 +237,7 @@ void app_main(void)
if (bits & WIFI_CONNECTED_BIT) { if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG_STA, "connected to ap SSID:%s password:%s", ESP_LOGI(TAG_STA, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD); EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);
softap_set_dns_addr(esp_netif_ap,esp_netif_sta);
} else if (bits & WIFI_FAIL_BIT) { } else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG_STA, "Failed to connect to SSID:%s, password:%s", ESP_LOGI(TAG_STA, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD); EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);