Skip to content

Commit

Permalink
Merge branch 'fix/fix_softap_sta_example_deafult_dns_addr' into 'master'
Browse files Browse the repository at this point in the history
fix(wifi): fix softap_sta example default dns addr invaild issue

Closes IDFGH-10968

See merge request espressif/esp-idf!30115
  • Loading branch information
MaxwellAlan committed Jul 5, 2024
2 parents d5a1e22 + ca03a64 commit 44f2915
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions examples/wifi/softap_sta/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ menu "Example Configuration"
default ESP_WIFI_AUTH_WPA2_PSK
help
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
incase AP is operating in WEP / WPA mode.
in case AP is operating in WEP / WPA mode.

config ESP_WIFI_AUTH_OPEN
bool "OPEN"
Expand Down
17 changes: 16 additions & 1 deletion examples/wifi/softap_sta/main/softap_sta.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -73,6 +73,9 @@
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1

/*DHCP server option*/
#define DHCPS_OFFER_DNS 0x02

static const char *TAG_AP = "WiFi SoftAP";
static const char *TAG_STA = "WiFi Sta";

Expand Down Expand Up @@ -162,6 +165,17 @@ esp_netif_t *wifi_init_sta(void)
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)
{
ESP_ERROR_CHECK(esp_netif_init());
Expand Down Expand Up @@ -223,6 +237,7 @@ void app_main(void)
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG_STA, "connected to ap SSID:%s password:%s",
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) {
ESP_LOGI(TAG_STA, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);
Expand Down

0 comments on commit 44f2915

Please sign in to comment.