From 1f43f025d88ebbe2b575885c4c051bee818cfc86 Mon Sep 17 00:00:00 2001 From: InfiniteYuan Date: Fri, 20 Mar 2020 17:20:05 +0800 Subject: [PATCH] example/eth2ap: fix station multi-connection problem and add channel config item. Closes https://github.com/espressif/esp-idf/issues/5029 --- .../ethernet/eth2ap/main/Kconfig.projbuild | 7 +++++++ .../eth2ap/main/ethernet_example_main.c | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/ethernet/eth2ap/main/Kconfig.projbuild b/examples/ethernet/eth2ap/main/Kconfig.projbuild index 8497f862d..d6e9b15a8 100644 --- a/examples/ethernet/eth2ap/main/Kconfig.projbuild +++ b/examples/ethernet/eth2ap/main/Kconfig.projbuild @@ -143,6 +143,13 @@ menu "Example Configuration" help Set the password of Wi-Fi ap interface. + config EXAMPLE_WIFI_CHANNEL + int "WiFi channel" + range 1 13 + default 1 + help + Set the channel of Wi-Fi ap. + config EXAMPLE_MAX_STA_CONN int "Maximum STA connections" default 4 diff --git a/examples/ethernet/eth2ap/main/ethernet_example_main.c b/examples/ethernet/eth2ap/main/ethernet_example_main.c index bc5adc3ff..224f2c1f3 100644 --- a/examples/ethernet/eth2ap/main/ethernet_example_main.c +++ b/examples/ethernet/eth2ap/main/ethernet_example_main.c @@ -124,16 +124,23 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base, static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + static uint8_t s_con_cnt = 0; switch (event_id) { case WIFI_EVENT_AP_STACONNECTED: ESP_LOGI(TAG, "Wi-Fi AP got a station connected"); - s_sta_is_connected = true; - esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, pkt_wifi2eth); + if (!s_con_cnt) { + s_sta_is_connected = true; + esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, pkt_wifi2eth); + } + s_con_cnt++; break; case WIFI_EVENT_AP_STADISCONNECTED: ESP_LOGI(TAG, "Wi-Fi AP got a station disconnected"); - s_sta_is_connected = false; - esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL); + s_con_cnt--; + if (!s_con_cnt) { + s_sta_is_connected = false; + esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, NULL); + } break; default: break; @@ -205,7 +212,8 @@ static void initialize_wifi(void) .ssid_len = strlen(CONFIG_EXAMPLE_WIFI_SSID), .password = CONFIG_EXAMPLE_WIFI_PASSWORD, .max_connection = CONFIG_EXAMPLE_MAX_STA_CONN, - .authmode = WIFI_AUTH_WPA_WPA2_PSK + .authmode = WIFI_AUTH_WPA_WPA2_PSK, + .channel = CONFIG_EXAMPLE_WIFI_CHANNEL // default: channel 1 }, }; if (strlen(CONFIG_EXAMPLE_WIFI_PASSWORD) == 0) {