From 5f36eb5d233b36a99331a79939c676f3810ffb27 Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Mon, 1 Sep 2025 17:34:14 -0300 Subject: [PATCH] arch/xtensa: refactor Wi-Fi driver for ESP32|S2|S3 Fixes low and inconsistent bandwidth issues. Adds new configuration options for buffer management. Moves code around to make the implementation cleaner and easier to debug. Signed-off-by: Filipe Cavalcanti --- arch/xtensa/src/common/espressif/Kconfig | 231 +- arch/xtensa/src/common/espressif/Make.defs | 6 +- arch/xtensa/src/common/espressif/Wireless.mk | 9 +- .../src/common/espressif/esp_wifi_api.c | 2158 +++++++++++++ .../espressif/esp_wifi_api.h} | 415 +-- .../common/espressif/esp_wifi_event_handler.c | 359 +++ .../src/common/espressif/esp_wifi_utils.c | 673 +--- .../src/common/espressif/esp_wifi_utils.h | 158 +- .../src/common/espressif/esp_wireless.c | 84 +- arch/xtensa/src/common/espressif/esp_wlan.c | 1902 ------------ .../src/common/espressif/esp_wlan_netdev.c | 1405 +++++++++ .../{esp_wlan.h => esp_wlan_netdev.h} | 154 +- arch/xtensa/src/esp32/Make.defs | 2 +- arch/xtensa/src/esp32/esp32_ble_adapter.c | 1 + arch/xtensa/src/esp32/esp32_wifi_adapter.c | 2691 +--------------- arch/xtensa/src/esp32/esp32_wifi_adapter.h | 748 ----- arch/xtensa/src/esp32/hal.mk | 1 + arch/xtensa/src/esp32s2/Make.defs | 2 +- .../xtensa/src/esp32s2/esp32s2_wifi_adapter.c | 2714 +---------------- arch/xtensa/src/esp32s3/Make.defs | 2 +- arch/xtensa/src/esp32s3/esp32s3_ble_adapter.c | 67 +- .../xtensa/src/esp32s3/esp32s3_wifi_adapter.c | 2705 +--------------- .../xtensa/src/esp32s3/esp32s3_wifi_adapter.h | 734 ----- 23 files changed, 4748 insertions(+), 12473 deletions(-) create mode 100644 arch/xtensa/src/common/espressif/esp_wifi_api.c rename arch/xtensa/src/{esp32s2/esp32s2_wifi_adapter.h => common/espressif/esp_wifi_api.h} (68%) create mode 100644 arch/xtensa/src/common/espressif/esp_wifi_event_handler.c delete mode 100644 arch/xtensa/src/common/espressif/esp_wlan.c create mode 100644 arch/xtensa/src/common/espressif/esp_wlan_netdev.c rename arch/xtensa/src/common/espressif/{esp_wlan.h => esp_wlan_netdev.h} (59%) diff --git a/arch/xtensa/src/common/espressif/Kconfig b/arch/xtensa/src/common/espressif/Kconfig index 0640638c68..c7bf0804ee 100644 --- a/arch/xtensa/src/common/espressif/Kconfig +++ b/arch/xtensa/src/common/espressif/Kconfig @@ -1223,12 +1223,6 @@ menuconfig ESPRESSIF_WIFI_BT_COEXIST menu "Wi-Fi Configuration" depends on ESPRESSIF_WIFI -config ESPRESSIF_WLAN - bool "WLAN" - default y - ---help--- - Enable WLAN support - menu "ESP WPA-Supplicant" config WPA_WAPI_PSK @@ -1241,8 +1235,8 @@ config WPA_WAPI_PSK config WPA_SUITE_B_192 bool "Enable NSA suite B support with 192-bit key" default n - select ESP_WIFI_GCMP_SUPPORT - select ESP_WIFI_GMAC_SUPPORT + select ESPRESSIF_WIFI_GCMP_SUPPORT + select ESPRESSIF_WIFI_GMAC_SUPPORT ---help--- Select this option to enable 192-bit NSA suite-B. This is necessary to support WPA3 192-bit security. @@ -1273,12 +1267,49 @@ config ESPRESSIF_WIFI_SOFTAP config ESPRESSIF_WIFI_STATION_SOFTAP bool "Station + SoftAP" -endchoice # ESP Wi-Fi mode +endchoice # ESPRESSIF_WIFI_MODE + +menu "SoftAP Configuration" + depends on ESPRESSIF_WIFI_SOFTAP || ESPRESSIF_WIFI_STATION_SOFTAP + +config ESPRESSIF_WIFI_SOFTAP_DEFAULT_SSID + string "SoftAP default SSID" + default "NuttX" + ---help--- + Represents the default SSID configured for the SoftAP when the device is started. + +config ESPRESSIF_WIFI_SOFTAP_DEFAULT_PASSWORD + string "SoftAP default password" + default "nuttx12345" + ---help--- + Represents the default password configured for the SoftAP when the device is started. + +config ESPRESSIF_WIFI_SOFTAP_MAX_CONNECTIONS + int "SoftAP max connections" + default 3 + ---help--- + Maximum number of stations connected to the SoftAP. + +config ESPRESSIF_WIFI_SOFTAP_CHANNEL + int "SoftAP channel" + default 1 + ---help--- + Channel number for the SoftAP. + +endmenu # SoftAP Configuration + +config ESPRESSIF_WIFI_ENABLE_WPA3_SAE + bool "Enable WPA3-Personal" + default y + ---help--- + Select this option to allow the device to establish a WPA3-Personal connection with eligible AP's. + PMF (Protected Management Frames) is a prerequisite feature for a WPA3 connection, it needs to be + explicitly configured before attempting connection. Please refer to the Wi-Fi Driver API Guide for details. config ESPRESSIF_WIFI_ENABLE_SAE_PK bool "Enable SAE-PK" + depends on ESPRESSIF_WIFI_ENABLE_WPA3_SAE default y - depends on ESP_WIFI_ENABLE_SAE_H2E ---help--- Select this option to enable SAE-PK @@ -1288,7 +1319,15 @@ config ESPRESSIF_WIFI_ENABLE_SAE_H2E ---help--- Select this option to enable SAE-H2E -config ESP_WIFI_ENABLE_WPA3_OWE_STA +config ESPRESSIF_WIFI_SOFTAP_SAE_SUPPORT + bool "Enable WPA3 Personal(SAE) SoftAP" + default y + depends on ESPRESSIF_WIFI_ENABLE_WPA3_SAE + depends on ESPRESSIF_WIFI_SOFTAP || ESPRESSIF_WIFI_STATION_SOFTAP + ---help--- + Select this option to enable SAE support in softAP mode. + +config ESPRESSIF_WIFI_ENABLE_WPA3_OWE_STA bool "Enable OWE STA" default y ---help--- @@ -1296,41 +1335,166 @@ config ESP_WIFI_ENABLE_WPA3_OWE_STA PMF (Protected Management Frames) is a prerequisite feature for a WPA3 connection, it needs to be explicitly configured before attempting connection. Please refer to the Wi-Fi Driver API Guide for details. -config ESPRESSIF_WIFI_STATIC_RXBUF_NUM - int "Wi-Fi static RX buffer number" +choice ESPRESSIF_WIFI_MGMT_RX_BUFFER + prompt "Type of WiFi RX MGMT buffers" + default ESPRESSIF_WIFI_STATIC_RX_MGMT_BUFFER + help + Select type of WiFi RX MGMT buffers: + + If "Static" is selected, WiFi RX MGMT buffers are allocated when WiFi is initialized and released + when WiFi is de-initialized. The size of each static RX MGMT buffer is fixed to about 500 Bytes. + + If "Dynamic" is selected, each WiFi RX MGMT buffer is allocated as needed when a MGMT data frame is + received. The MGMT buffer is freed after the MGMT data frame has been processed by the WiFi driver. + +config ESPRESSIF_WIFI_STATIC_RX_MGMT_BUFFER + bool "Static" +config ESPRESSIF_WIFI_DYNAMIC_RX_MGMT_BUFFER + bool "Dynamic" +endchoice + +config ESPRESSIF_WIFI_DYNAMIC_RX_MGMT_BUFFER_TYPE + int + default 0 if ESPRESSIF_WIFI_STATIC_RX_MGMT_BUFFER + default 1 if ESPRESSIF_WIFI_DYNAMIC_RX_MGMT_BUFFER + +config ESPRESSIF_WIFI_STATIC_RX_BUFFER_NUM + int "Max number of WiFi static RX buffers" + range 2 25 default 10 + ---help--- + Set the number of WiFi static RX buffers. Each buffer takes approximately 1.6KB of RAM. + The static rx buffers are allocated when esp_wifi_init is called, they are not freed + until esp_wifi_deinit is called. -config ESPRESSIF_WIFI_DYNAMIC_RXBUF_NUM - int "Wi-Fi dynamic RX buffer number" + WiFi hardware use these buffers to receive all 802.11 frames. + A higher number may allow higher throughput but increases memory use. If ESPRESSIF_WIFI_AMPDU_RX_ENABLED + is enabled, this value is recommended to set equal or bigger than ESPRESSIF_WIFI_RX_BA_WIN in order to + achieve better throughput and compatibility with both stations and APs. + +config ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM + int "Max number of WiFi dynamic RX buffers" default 32 + ---help--- + Set the number of WiFi dynamic RX buffers, 0 means unlimited RX buffers will be allocated + (provided sufficient free RAM). The size of each dynamic RX buffer depends on the size of + the received data frame. -config ESPRESSIF_WIFI_DYNAMIC_TXBUF_NUM - int "Wi-Fi dynamic TX buffer number" + For each received data frame, the WiFi driver makes a copy to an RX buffer and then delivers + it to the high layer TCP/IP stack. The dynamic RX buffer is freed after the higher layer has + successfully received the data frame. + + For some applications, WiFi data frames may be received faster than the application can + process them. In these cases we may run out of memory if RX buffer number is unlimited (0). + + If a dynamic RX buffer limit is set, it should be at least the number of static RX buffers. + +choice ESPRESSIF_WIFI_TX_BUFFER + prompt "Type of WiFi TX buffers" + default ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER + help + Select type of WiFi TX buffers: + + If "Static" is selected, WiFi TX buffers are allocated when WiFi is initialized and released + when WiFi is de-initialized. The size of each static TX buffer is fixed to about 1.6KB. + + If "Dynamic" is selected, each WiFi TX buffer is allocated as needed when a data frame is + delivered to the Wifi driver from the TCP/IP stack. The buffer is freed after the data frame + has been sent by the WiFi driver. The size of each dynamic TX buffer depends on the length + of each data frame sent by the TCP/IP layer. + + If PSRAM is enabled, "Static" should be selected to guarantee enough WiFi TX buffers. + If PSRAM is disabled, "Dynamic" should be selected to improve the utilization of RAM. + + TODO: There is a special dependency for Dynamic if SPIRAM is enabled. + +config ESPRESSIF_WIFI_STATIC_TX_BUFFER + bool "Static" +config ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER + bool "Dynamic" +endchoice + +config ESPRESSIF_WIFI_TX_BUFFER_TYPE + int + default 0 if ESPRESSIF_WIFI_STATIC_TX_BUFFER + default 1 if ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER + +config ESPRESSIF_WIFI_STATIC_TX_BUFFER_NUM + int "Max number of WiFi static TX buffers" + range 2 64 + default 16 + ---help--- + Set the number of WiFi static TX buffers. Each buffer takes approximately 1.6KB of RAM. + The static RX buffers are allocated when esp_wifi_init() is called, they are not released + until esp_wifi_deinit() is called. + + This value might be reduced to save memory if the application does not need to send + frames at a high rate. + + For each transmitted data frame from the higher layer TCP/IP stack, the WiFi driver makes a + copy of it in a TX buffer. For some applications especially UDP applications, the upper + layer can deliver frames faster than WiFi layer can transmit. In these cases, we may run out + of TX buffers. + +config ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM + int "Max number of WiFi dynamic TX buffers" + range 1 128 default 32 + ---help--- + Set the number of WiFi dynamic TX buffers. The size of each dynamic TX buffer is not fixed, + it depends on the size of each transmitted data frame. -config ESPRESSIF_WIFI_TX_AMPDU + For each transmitted frame from the higher layer TCP/IP stack, the WiFi driver makes a copy + of it in a TX buffer. For some applications, especially UDP applications, the upper layer + can deliver frames faster than WiFi layer can transmit. In these cases, we may run out of TX + buffers. + +config ESPRESSIF_WIFI_RX_MGMT_BUF_NUM_DEF + int "Max number of WiFi RX MGMT buffers" + range 1 10 + default 5 + help + Set the number of WiFi RX_MGMT buffers. + For Management buffers, the number of dynamic and static management buffers is the same. + +config ESPRESSIF_WIFI_AMPDU_TX_ENABLED bool "Wi-Fi TX AMPDU" default y + ---help--- + Select this option to enable AMPDU TX feature -config ESPRESSIF_WIFI_RX_AMPDU - bool "Wi-Fi RX AMPDU" - default y - -config ESPRESSIF_WIFI_RXBA_AMPDU_WZ - int "Wi-Fi RX BA AMPDU windown size" +config ESPRESSIF_WIFI_TX_BA_WIN + int "WiFi AMPDU TX BA window size" + depends on ESPRESSIF_WIFI_AMPDU_TX_ENABLED + range 2 32 default 6 -config ESPRESSIF_WLAN_PKTBUF_NUM - int "WLAN netcard packet buffer number per netcard" - default 16 +config ESPRESSIF_WIFI_AMPDU_RX_ENABLED + bool "WiFi AMPDU RX" + default y + ---help--- + Select this option to enable AMPDU RX feature -config ESP_WIFI_GCMP_SUPPORT +config ESPRESSIF_WIFI_RX_BA_WIN + int "WiFi AMPDU RX BA window size" + depends on ESPRESSIF_WIFI_AMPDU_RX_ENABLED + range 2 32 + default 6 + ---help--- + Set the size of WiFi Block Ack RX window. Generally a bigger value means higher throughput and better + compatibility but more memory. Most of time we should NOT change the default value unless special + reason, e.g. test the maximum UDP RX throughput with iperf etc. For iperf test in shieldbox, the + recommended value is 9~12. If PSRAM is used and WiFi memory is preferred to allocate in PSRAM first, + the default and minimum value should be 16 to achieve better throughput and compatibility with both + stations and APs. + +config ESPRESSIF_WIFI_GCMP_SUPPORT bool "WiFi GCMP Support(GCMP128 and GCMP256)" default n ---help--- Select this option to enable GCMP support. GCMP support is compulsory for WiFi Suite-B support. -config ESP_WIFI_GMAC_SUPPORT +config ESPRESSIF_WIFI_GMAC_SUPPORT bool "WiFi GMAC Support(GMAC128 and GMAC256)" default n ---help--- @@ -1400,18 +1564,11 @@ config ESPRESSIF_WIFI_LISTEN_INTERVAL For example, if beacon interval is 100 ms and listen interval is 3, the interval for station to listen to beacon is 300 ms. -config ESPRESSIF_WIFI_WLAN_BUFFER_OPTIMIZATION - bool "Enable optimization of WLAN driver buffer" - default n - depends on ARCH_CHIP_ESP32 - ---help--- - Enable optimization of WLAN memory - endmenu # ESPRESSIF_WIFI config ESPRESSIF_ESPNOW_PKTRADIO bool "ESPNOW pktradio support" - depends on ESPRESSIF_WLAN + depends on ESPRESSIF_WIRELESS depends on WIRELESS_PKTRADIO default n ---help--- diff --git a/arch/xtensa/src/common/espressif/Make.defs b/arch/xtensa/src/common/espressif/Make.defs index 1d2fc041ea..760bb965d4 100644 --- a/arch/xtensa/src/common/espressif/Make.defs +++ b/arch/xtensa/src/common/espressif/Make.defs @@ -98,8 +98,10 @@ endif ifeq ($(CONFIG_ESPRESSIF_WIRELESS),y) CHIP_CSRCS += esp_wireless.c CHIP_CSRCS += esp_wifi_utils.c -ifeq ($(CONFIG_ESPRESSIF_WLAN),y) -CHIP_CSRCS += esp_wlan.c +ifeq ($(CONFIG_ESPRESSIF_WIFI),y) +CHIP_CSRCS += esp_wifi_event_handler.c +CHIP_CSRCS += esp_wlan_netdev.c +CHIP_CSRCS += esp_wifi_api.c endif ifeq ($(CONFIG_ESPRESSIF_ESPNOW_PKTRADIO), y) CHIP_CSRCS += esp_espnow_pktradio.c diff --git a/arch/xtensa/src/common/espressif/Wireless.mk b/arch/xtensa/src/common/espressif/Wireless.mk index a6a89c57fc..e8dfa5cc85 100644 --- a/arch/xtensa/src/common/espressif/Wireless.mk +++ b/arch/xtensa/src/common/espressif/Wireless.mk @@ -152,11 +152,11 @@ ifeq ($(CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA),y) CFLAGS += $(DEFINE_PREFIX)CONFIG_OWE_STA endif -ifeq ($(CONFIG_ESP_WIFI_GCMP_SUPPORT),y) +ifeq ($(CONFIG_ESPRESSIF_WIFI_GCMP_SUPPORT),y) CFLAGS += $(DEFINE_PREFIX)CONFIG_GCMP endif -ifeq ($(CONFIG_ESP_WIFI_GMAC_SUPPORT),y) +ifeq ($(CONFIG_ESPRESSIF_WIFI_GMAC_SUPPORT),y) CFLAGS += $(DEFINE_PREFIX)CONFIG_GMAC endif @@ -273,8 +273,13 @@ CHIP_CSRCS += tls_mbedtls.c CHIP_CSRCS += aes-siv.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_wifi$(DELIM)src$(DELIM)wifi_init.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_wifi$(DELIM)src$(DELIM)lib_printf.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_wifi$(DELIM)regulatory$(DELIM)esp_wifi_regulatory.c +ifeq ($(CONFIG_ESPRESSIF_WIFI_BT_COEXIST),y) +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_coex$(DELIM)src$(DELIM)lib_printf.c +endif + endif # Linker scripts diff --git a/arch/xtensa/src/common/espressif/esp_wifi_api.c b/arch/xtensa/src/common/espressif/esp_wifi_api.c new file mode 100644 index 0000000000..c2bb50855d --- /dev/null +++ b/arch/xtensa/src/common/espressif/esp_wifi_api.c @@ -0,0 +1,2158 @@ +/**************************************************************************** + * arch/xtensa/src/common/espressif/esp_wifi_api.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include + +#include "esp_mac.h" +#include "esp_wifi_types_generic.h" +#include "esp_wifi.h" +#include "esp_private/wifi.h" + +#include "esp_wifi_utils.h" +#include "esp_wifi_api.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define WIFI_CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT +#define WIFI_CONNECT_RETRY_CNT 3 + +#define ESP_WIFI_11B_MAX_BITRATE 11 +#define ESP_WIFI_11G_MAX_BITRATE 54 +#define ESP_WIFI_11N_MCS7_HT20_BITRATE 72 +#define ESP_WIFI_11N_MCS7_HT40_BITRATE 150 + +/* Number of fractional bits in values returned by rtc_clk_cal */ + +#define RTC_CLK_CAL_FRACT 19 + +#define PWD_MAX_LEN (64) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_wifi_api_adapter_deinit + * + * Description: + * De-initialize Wi-Fi adapter, freeing all resources allocated by + * esp_wifi_init. Also stops the Wi-Fi task. + * + * Input Parameters: + * None + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_adapter_deinit(void) +{ + int ret; + + ret = esp_wifi_deinit(); + if (ret != 0) + { + ret = esp_wifi_to_errno(ret); + wlerr("Failed to deinitialize Wi-Fi error=%d\n", ret); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_api_adapter_init + * + * Description: + * Initialize the Wi-Fi driver, control structure, buffers and Wi-Fi task. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_adapter_init(void) +{ + int ret; + wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); + + esp_wifi_lock(true); + + esp_evt_work_init(); + + wifi_cfg.nvs_enable = 0; + +#ifdef CONFIG_ESPRESSIF_WIFI_AMPDU_TX_ENABLED + wifi_cfg.ampdu_tx_enable = 1; +#else + wifi_cfg.ampdu_tx_enable = 0; +#endif + +#ifdef CONFIG_ESPRESSIF_WIFI_AMPDU_RX_ENABLED + wifi_cfg.ampdu_rx_enable = 1; +#else + wifi_cfg.ampdu_rx_enable = 0; +#endif + +#ifdef CONFIG_ESPRESSIF_WIFI_STA_DISCONNECT_PM + wifi_cfg.sta_disconnected_pm = true; +#else + wifi_cfg.sta_disconnected_pm = false; +#endif + + wifi_cfg.rx_ba_win = CONFIG_ESPRESSIF_WIFI_TX_BA_WIN; + wifi_cfg.static_rx_buf_num = CONFIG_ESPRESSIF_WIFI_STATIC_RX_BUFFER_NUM; + wifi_cfg.static_tx_buf_num = CONFIG_ESPRESSIF_WIFI_STATIC_TX_BUFFER_NUM; + wifi_cfg.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM; + wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM; + wifi_cfg.tx_buf_type = CONFIG_ESPRESSIF_WIFI_TX_BUFFER_TYPE; + + ret = esp_wifi_init(&wifi_cfg); + if (ret != 0) + { + wlerr("Failed to initialize Wi-Fi error=%d\n", ret); + ret = esp_wifi_to_errno(ret); + goto errout_init_wifi; + } + + wlinfo("Wi-Fi adapter initialized\n"); + + /* Set Wi-Fi mode to NULL, this way it requires ifup call to set + * the proper wifi mode. Otherwise, it starts as STA mode. + */ + + ret = esp_wifi_set_mode(WIFI_MODE_NULL); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi mode to NULL ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + goto errout_init_wifi; + } + + esp_wifi_lock(false); + + return OK; + +errout_init_wifi: + esp_wifi_lock(false); + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_api_start + * + * Description: + * Start Wi-Fi station. This will start the proper Wi-Fi mode based on + * the AP/Station configuration. + * + * Input Parameters: + * start_mode - The Wi-Fi mode to start from + * nuttx/include/nuttx/wireless/wireless.h. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_start(uint32_t start_mode) +{ + int ret; + wifi_mode_t mode; + wifi_mode_t current_mode; + + ret = esp_wifi_get_mode(¤t_mode); + if (ret != 0) + { + wlerr("Failed to get Wi-Fi mode ret=%d. Check if initialized\n", + ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + + mode = esp_wifi_mode_translate(start_mode); + if (mode == -EINVAL) + { + wlerr("Invalid mode=%ld\n", start_mode); + return -EINVAL; + } + + wlinfo("current_mode=%d, mode=%d\n", current_mode, mode); + +#ifdef ESP_WLAN_HAS_APSTA + if ((mode == WIFI_MODE_STA && current_mode == WIFI_MODE_AP) || + (mode == WIFI_MODE_AP && current_mode == WIFI_MODE_STA)) + { + wlinfo("Wi-Fi mode set to APSTA\n"); + mode = WIFI_MODE_APSTA; + } +#endif + + esp_wifi_lock(true); + + ret = esp_wifi_set_mode(mode); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + + /* Use a default AP config for startup */ + +#ifdef ESP_WLAN_HAS_SOFTAP + if (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA) + { + wlinfo("Setting default AP config SSID=%s\n", + CONFIG_ESPRESSIF_WIFI_SOFTAP_DEFAULT_SSID); + wifi_config_t wifi_config = + { + .ap = + { + .ssid = CONFIG_ESPRESSIF_WIFI_SOFTAP_DEFAULT_SSID, + .ssid_len = strlen(CONFIG_ESPRESSIF_WIFI_SOFTAP_DEFAULT_SSID), + .channel = CONFIG_ESPRESSIF_WIFI_SOFTAP_CHANNEL, + .password = CONFIG_ESPRESSIF_WIFI_SOFTAP_DEFAULT_PASSWORD, + .max_connection = CONFIG_ESPRESSIF_WIFI_SOFTAP_MAX_CONNECTIONS, +#ifdef CONFIG_ESPRESSIF_WIFI_SOFTAP_SAE_SUPPORT + .authmode = WIFI_AUTH_WPA3_PSK, + .sae_pwe_h2e = WPA3_SAE_PWE_BOTH, +#else /* CONFIG_ESPRESSIF_WIFI_SOFTAP_SAE_SUPPORT */ + .authmode = WIFI_AUTH_WPA2_PSK, +#endif + .pmf_cfg = + { + .required = true, + }, + }, + }; + + ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_config); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + } +#endif /* ESP_WLAN_HAS_SOFTAP */ + + ret = esp_wifi_start(); + if (ret != 0) + { + wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + + wlinfo("Wi-Fi started with mode=%d\n", mode); + +errout: + esp_wifi_lock(false); + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_api_stop + * + * Description: + * Stops Wi-Fi AP, Station or both. Can be later resumed by + * esp_wifi_restore, but must be reconfigured. + * + * If AP + SoftAP are running, be aware that both will be stopped briefly, + * and then the remaining one will be restarted. + * + * Input Parameters: + * start_mode - The Wi-Fi mode to start from + * nuttx/include/nuttx/wireless/wireless.h. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_stop(uint32_t stop_mode) +{ + int ret; + wifi_mode_t mode; + wifi_mode_t current_mode; + + esp_wifi_lock(true); + + ret = esp_wifi_get_mode(¤t_mode); + if (ret != 0) + { + wlerr("Failed to get Wi-Fi mode ret=%d. Check if initialized\n", + ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + + mode = esp_wifi_mode_translate(stop_mode); + if (mode == -EINVAL) + { + wlerr("Invalid mode=%ld\n", stop_mode); + return -EINVAL; + } + + wlinfo("current_mode=%d, mode=%d\n", current_mode, mode); + + ret = esp_wifi_stop(); + if (ret != 0) + { + wlerr("Failed to stop Wi-Fi ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + + wlinfo("Stopped Wi-Fi\n"); + + if (current_mode == WIFI_MODE_APSTA) + { + if (mode == WIFI_MODE_STA) + { + wlinfo("Restarting Wi-Fi in AP mode\n"); + ret = esp_wifi_set_mode(WIFI_MODE_AP); + } + else if (mode == WIFI_MODE_AP) + { + wlinfo("Restarting Wi-Fi in STA mode\n"); + ret = esp_wifi_set_mode(WIFI_MODE_STA); + } + else + { + wlerr("Invalid mode=%d\n", mode); + goto errout; + } + + if (ret != 0) + { + wlerr("Failed to reset Wi-Fi mode=%d ret=%d\n", + WIFI_MODE_AP, ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + + ret = esp_wifi_start(); + if (ret != 0) + { + wlerr("Failed to restart Wi-Fi ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + goto errout; + } + } + +errout: + esp_wifi_lock(false); + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_api_sta_register_rx_callback + * + * Description: + * Register a callback function for the Wi-Fi station interface. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_sta_register_rx_callback(void *cb) +{ + return esp_wifi_internal_reg_rxcb(WIFI_IF_STA, (wifi_rxcb_t) cb); +} + +/**************************************************************************** + * Name: esp_wifi_api_softap_register_rx_callback + * + * Description: + * Register a callback function for the Wi-Fi softAP interface. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_softap_register_rx_callback(void *cb) +{ + return esp_wifi_internal_reg_rxcb(WIFI_IF_AP, (wifi_rxcb_t) cb); +} + +/**************************************************************************** + * Name: esp_wifi_api_registe_tx_done_callback + * + * Description: + * Register a callback function for transmission done. Valid for + * both station and softAP and needs to be called only once on bringup. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_register_tx_done_callback(void *cb) +{ + return esp_wifi_set_tx_done_cb((wifi_tx_done_cb_t) cb); +} + +/**************************************************************************** + * Name: esp_wifi_api_free_rx_buffer + * + * Description: + * Free the RX buffer allocated by the Wi-Fi driver. + * + * Input Parameters: + * eb - The event buffer to free + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void esp_wifi_api_free_rx_buffer(void *eb) +{ + esp_wifi_internal_free_rx_buffer(eb); +} + +/**************************************************************************** + * Name: esp_wifi_txpower + * + * Description: + * Get/Set station transmit power (dBm). Used by both station and SoftAP, + * since they share the radio. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +static int esp_wifi_txpower(struct iwreq *iwr, bool set) +{ + int ret; + int8_t power; + double power_dbm; + + if (set) + { + if (iwr->u.txpower.flags == IW_TXPOW_RELATIVE) + { + power = (int8_t)iwr->u.txpower.value; + } + else + { + if (iwr->u.txpower.flags == IW_TXPOW_MWATT) + { + power_dbm = ceil(10 * log10(iwr->u.txpower.value)); + } + else + { + power_dbm = iwr->u.txpower.value; + } + + power = (int8_t)(power_dbm * 4); + } + + /* The value set by this API will be mapped to the max_tx_power + * of the structure wifi_country_t variable. Param power unit is + * 0.25dBm, range is [8, 84] corresponding to 2dBm - 20dBm. + * Relationship between set value and actual value. + * As follows: {set value range, actual value} = + * {{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, + * {[34, 43],34}, {[44, 51],44}, {[52, 55],52}, + * {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, + * {[72, 79],72}, {[80, 84],80}}. + */ + + if (power < 8 || power > 84) + { + wlerr("Failed to set transmit power =%d\n", power); + return -ENOSYS; + } + + esp_wifi_set_max_tx_power(power); + return OK; + } + else + { + ret = esp_wifi_get_max_tx_power(&power); + if (ret != 0) + { + wlerr("Failed to get transmit power ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + iwr->u.txpower.disabled = 0; + iwr->u.txpower.flags = IW_TXPOW_DBM; + iwr->u.txpower.value = power / 4; + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_channel + * + * Description: + * Get station range of channel parameters. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +static int esp_wifi_channel(struct iwreq *iwr, bool set) +{ + int ret; + int k; + wifi_country_t country; + struct iw_range *range; + + if (set) + { + return -ENOSYS; + } + else + { + ret = esp_wifi_get_country(&country); + if (ret != 0) + { + wlerr("Failed to get country info ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + range = (struct iw_range *)iwr->u.data.pointer; + range->num_frequency = country.nchan; + for (k = 1; k <= range->num_frequency; k++) + { + range->freq[k - 1].i = k; + range->freq[k - 1].e = 0; + range->freq[k - 1].m = 2407 + 5 * k; + } + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_country + * + * Description: + * Configure country info. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +static int esp_wifi_country(struct iwreq *iwr, bool set) +{ + int ret; + char *country_code; + wifi_country_t country; + + if (set) + { + memset(&country, 0x00, sizeof(wifi_country_t)); + country.schan = 1; + country.policy = 0; + + country_code = (char *)iwr->u.data.pointer; + if (strlen(country_code) != 2) + { + wlerr("Invalid input arguments\n"); + return -EINVAL; + } + + if (strncmp(country_code, "US", 3) == 0 || + strncmp(country_code, "CA", 3) == 0) + { + country.nchan = 11; + } + else if(strncmp(country_code, "JP", 3) == 0) + { + country.nchan = 14; + } + else + { + country.nchan = 13; + } + + memcpy(country.cc, country_code, 2); + ret = esp_wifi_set_country(&country); + if (ret != 0) + { + wlerr("Failed to Configure country ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + return -ENOSYS; + } + + return OK; +} + +/**************************************************************************** + * Wi-Fi Station functions + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA + +/**************************************************************************** + * Name: esp_wifi_sta_connect + * + * Description: + * Trigger Wi-Fi station connection action. + * + * Input Parameters: + * None + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_connect(void) +{ + int ret; + + wlinfo("Wi-Fi station connecting\n"); + + esp_wifi_lock(true); + + ret = esp_wifi_connect(); + if (ret != 0) + { + wlerr("Failed to connect ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + } + + esp_wifi_lock(false); + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_sta_disconnect + * + * Description: + * Trigger Wi-Fi station disconnection action. + * + * Input Parameters: + * allow_reconnect - True if Wi-Fi should attempt to reconnect. + * This is used to prevent the Wi-Fi from reconnecting + * when the user has actually asked to disconnect from + * the AP, otherwise it will attempt reconnections on + * WIFI_EVENT_STA_DISCONNECTED event. + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_disconnect(bool allow_reconnect) +{ + int ret; + wifi_config_t wifi_config; + + esp_wifi_lock(true); + esp_wifi_get_config(WIFI_IF_STA, &wifi_config); + + if (allow_reconnect) + { + wifi_config.sta.failure_retry_cnt = WIFI_CONNECT_RETRY_CNT; + } + else + { + wifi_config.sta.failure_retry_cnt = 0; + } + + esp_wifi_set_config(WIFI_IF_STA, &wifi_config); + + ret = esp_wifi_disconnect(); + if (ret != 0) + { + wlerr("Failed to disconnect ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + } + else + { + wlinfo("Wi-Fi station disconnected\n"); + } + + esp_wifi_lock(false); + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_sta_send_data + * + * Description: + * Use Wi-Fi station interface to send 802.3 frame. + * + * Input Parameters: + * pbuf - Packet buffer pointer + * len - Packet length + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_send_data(void *pbuf, size_t len) +{ + int ret; + + ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len); + if (ret != 0) + { + wlerr("Failed to send Wi-Fi data ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_sta_read_mac + * + * Description: + * Read station interface MAC address from efuse + * + * Input Parameters: + * mac - MAC address buffer pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int esp_wifi_sta_read_mac(uint8_t *mac) +{ + int ret = esp_read_mac(mac, ESP_MAC_WIFI_STA); + return esp_wifi_to_errno(ret); +} + +/**************************************************************************** + * Name: esp_wifi_sta_set_password + * + * Description: + * Set/Get Wi-Fi station password. + * Setting the Wi-Fi will disconnect from the current AP. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_password(struct iwreq *iwr, bool set) +{ + int ret; + int size; + wifi_config_t wifi_cfg; + struct iw_encode_ext *ext = iwr->u.encoding.pointer; + uint8_t *pdata; + uint8_t len; +#ifdef CONFIG_DEBUG_WIRELESS_INFO + char buf[PWD_MAX_LEN + 1]; +#endif + + DEBUGASSERT(ext != NULL); + + pdata = ext->key; + + esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg); + + if (set) + { + len = ext->key_len; + if (len > PWD_MAX_LEN) + { + return -EINVAL; + } + + memset(wifi_cfg.sta.password, 0x0, PWD_MAX_LEN); + + if (ext->alg != IW_ENCODE_ALG_NONE) + { + memcpy(wifi_cfg.sta.password, pdata, len); + } + + ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + ret = esp_wifi_sta_disconnect(true); + if (ret != 0) + { + wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); + return ret; + } + } + else + { + len = iwr->u.encoding.length - sizeof(*ext); + size = strnlen((char *)wifi_cfg.sta.password, PWD_MAX_LEN); + if (len < size) + { + return -EINVAL; + } + else + { + ext->key_len = size; + memcpy(pdata, wifi_cfg.sta.password, ext->key_len); + } + + wifi_ap_record_t ap_info; + + ret = esp_wifi_sta_get_ap_info(&ap_info); + if (ret != 0) + { + wlerr("Failed to get AP record ret=%d", ret); + return esp_wifi_to_errno(ret); + } + + switch (ap_info.pairwise_cipher) + { + case WIFI_CIPHER_TYPE_NONE: + ext->alg = IW_ENCODE_ALG_NONE; + break; + + case WIFI_CIPHER_TYPE_WEP40: + case WIFI_CIPHER_TYPE_WEP104: + ext->alg = IW_ENCODE_ALG_WEP; + break; + + case WIFI_CIPHER_TYPE_TKIP: + ext->alg = IW_ENCODE_ALG_TKIP; + break; + + case WIFI_CIPHER_TYPE_CCMP: + case WIFI_CIPHER_TYPE_TKIP_CCMP: + ext->alg = IW_ENCODE_ALG_CCMP; + break; + + case WIFI_CIPHER_TYPE_AES_CMAC128: + ext->alg = IW_ENCODE_ALG_AES_CMAC; + break; + + default: + wlerr("Failed to transfer wireless authmode: %d", + ap_info.pairwise_cipher); + return -EIO; + } + } + +#ifdef CONFIG_DEBUG_WIRELESS_INFO + memcpy(buf, pdata, len); + buf[len] = 0; + wlinfo("Wi-Fi station password=%s len=%d\n", buf, len); +#endif + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_essid + * + * Description: + * Set/Get Wi-Fi station ESSID. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd. + * set - true: set data; false: get data. + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_essid(struct iwreq *iwr, bool set) +{ + int ret; + int size; + wifi_config_t wifi_cfg; + struct iw_point *essid = &iwr->u.essid; + uint8_t *pdata; + uint8_t len; +#ifdef CONFIG_DEBUG_WIRELESS_INFO + char buf[IW_ESSID_MAX_SIZE + 1]; +#endif + + DEBUGASSERT(essid != NULL); + + pdata = essid->pointer; + len = essid->length; + + if (set && len > IW_ESSID_MAX_SIZE) + { + return -EINVAL; + } + + esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg); + + if (set) + { + memset(wifi_cfg.sta.ssid, 0x0, IW_ESSID_MAX_SIZE); + memcpy(wifi_cfg.sta.ssid, pdata, len); + memset(wifi_cfg.sta.sae_h2e_identifier, 0x0, SAE_H2E_IDENTIFIER_LEN); + wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; + + ret = esp_wifi_sta_disconnect(true); + if (ret != 0) + { + wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); + return ret; + } + + ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + size = strnlen((char *)wifi_cfg.sta.ssid, IW_ESSID_MAX_SIZE); + if (len < size) + { + return -EINVAL; + } + else + { + len = size; + memcpy(pdata, wifi_cfg.sta.ssid, len); + } + } + +#ifdef CONFIG_DEBUG_WIRELESS_INFO + memcpy(buf, pdata, len); + buf[len] = 0; + wlinfo("Wi-Fi station ssid=%s len=%d\n", buf, len); +#endif + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_bssid + * + * Description: + * Set/Get Wi-Fi station BSSID. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_bssid(struct iwreq *iwr, bool set) +{ + int ret; + wifi_config_t wifi_cfg; + struct sockaddr *sockaddr; + char *pdata; + + sockaddr = &iwr->u.ap_addr; + pdata = sockaddr->sa_data; + + esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg); + + if (set) + { + wifi_cfg.sta.bssid_set = true; + memcpy(wifi_cfg.sta.bssid, pdata, MAC_LEN); + + wlinfo("set station BSSID: %02x:%02x:%02x:%02x:%02x:%02x\n", + wifi_cfg.sta.bssid[0], wifi_cfg.sta.bssid[1], wifi_cfg.sta.bssid[2], + wifi_cfg.sta.bssid[3], wifi_cfg.sta.bssid[4], wifi_cfg.sta.bssid[5]); + + ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + ret = esp_wifi_sta_disconnect(true); + if (ret != 0) + { + wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); + return ret; + } + } + else + { + memcpy(pdata, wifi_cfg.sta.bssid, MAC_LEN); + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_mode + * + * Description: + * Set/Get Wi-Fi Station mode code. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_mode(struct iwreq *iwr, bool set) +{ + if (!set) + { + iwr->u.mode = IW_MODE_INFRA; + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_auth_trans + * + * Description: + * Converts authenticate mode values to WEXT authenticate mode. + * + * Input Parameters: + * wifi_auth - ESP Wi-Fi authenticate mode. + * + * Returned Value: + * int - authenticate mode. + * + ****************************************************************************/ + +static int esp_wifi_auth_trans(uint32_t wifi_auth) +{ + int auth_mode = IW_AUTH_WPA_VERSION_DISABLED; + + switch (wifi_auth) + { + case WIFI_AUTH_OPEN: + auth_mode = IW_AUTH_WPA_VERSION_DISABLED; + break; + + case WIFI_AUTH_WPA_PSK: + auth_mode = IW_AUTH_WPA_VERSION_WPA; + break; + + case WIFI_AUTH_WPA2_PSK: + case WIFI_AUTH_WPA_WPA2_PSK: + auth_mode = IW_AUTH_WPA_VERSION_WPA2; + break; + + case WIFI_AUTH_WPA3_PSK: + case WIFI_AUTH_WPA2_WPA3_PSK: + auth_mode = IW_AUTH_WPA_VERSION_WPA3; + break; + + default: + wlerr("Failed to transfer wireless authmode: %ld", wifi_auth); + break; + } + + return auth_mode; +} + +/**************************************************************************** + * Name: esp_wifi_cipher_trans + * + * Description: + * Converts a ESP32-C6 cipher type values to WEXT cipher type values. + * + * Input Parameters: + * wifi_cipher - ESP Wi-Fi cipher type. + * + * Returned Value: + * cipher type. + * + ****************************************************************************/ + +static int esp_wifi_cipher_trans(uint32_t wifi_cipher) +{ + int cipher_mode = IW_AUTH_CIPHER_NONE; + + switch (wifi_cipher) + { + case WIFI_CIPHER_TYPE_NONE: + cipher_mode = IW_AUTH_CIPHER_NONE; + break; + + case WIFI_CIPHER_TYPE_WEP40: + cipher_mode = IW_AUTH_CIPHER_WEP40; + break; + + case WIFI_CIPHER_TYPE_WEP104: + cipher_mode = IW_AUTH_CIPHER_WEP104; + break; + + case WIFI_CIPHER_TYPE_TKIP: + cipher_mode = IW_AUTH_CIPHER_TKIP; + break; + + case WIFI_CIPHER_TYPE_CCMP: + case WIFI_CIPHER_TYPE_TKIP_CCMP: + cipher_mode = IW_AUTH_CIPHER_CCMP; + break; + + case WIFI_CIPHER_TYPE_AES_CMAC128: + cipher_mode = IW_AUTH_CIPHER_AES_CMAC; + break; + + default: + wlerr("Failed to transfer wireless authmode: %ld", + wifi_cipher); + break; + } + + return cipher_mode; +} + +/**************************************************************************** + * Name: esp_wifi_sta_auth + * + * Description: + * Set/Get station authentication mode params. + * Setting will disconnect from the current AP. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success (positive non-zero values are cmd-specific). + * Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_auth(struct iwreq *iwr, bool set) +{ + int ret; + int cmd; + wifi_config_t wifi_cfg; + wifi_ap_record_t ap_info; + + esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg); + + if (set) + { + cmd = iwr->u.param.flags & IW_AUTH_INDEX; + switch (cmd) + { + case IW_AUTH_WPA_VERSION: + { + switch (iwr->u.param.value) + { + case IW_AUTH_WPA_VERSION_DISABLED: + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; + wlinfo("set authmode to WIFI_AUTH_OPEN\n"); + break; + + case IW_AUTH_WPA_VERSION_WPA: + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA_PSK; + wlinfo("set authmode to WIFI_AUTH_WPA_PSK\n"); + break; + + case IW_AUTH_WPA_VERSION_WPA2: + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; + wlinfo("set authmode to WIFI_AUTH_WPA2_PSK\n"); + break; + + case IW_AUTH_WPA_VERSION_WPA3: + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK; + wlinfo("set authmode to WIFI_AUTH_WPA3_PSK\n"); + break; + + default: + wlerr("Invalid wpa version %" PRId32 "\n", + iwr->u.param.value); + return -EINVAL; + } + } + break; + + case IW_AUTH_CIPHER_PAIRWISE: + case IW_AUTH_CIPHER_GROUP: + { + switch (iwr->u.param.value) + { + case IW_AUTH_CIPHER_NONE: + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; + wlinfo("set authmode to WIFI_AUTH_OPEN\n"); + break; + + case IW_AUTH_CIPHER_WEP40: + case IW_AUTH_CIPHER_WEP104: + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP; + wlinfo("set authmode to WIFI_AUTH_WEP\n"); + break; + + case IW_AUTH_CIPHER_TKIP: + case IW_AUTH_CIPHER_CCMP: + case IW_AUTH_CIPHER_AES_CMAC: + break; + + default: + wlerr("Invalid cipher mode %" PRId32 "\n", + iwr->u.param.value); + return -EINVAL; + } + } + break; + + case IW_AUTH_KEY_MGMT: + case IW_AUTH_TKIP_COUNTERMEASURES: + case IW_AUTH_DROP_UNENCRYPTED: + case IW_AUTH_80211_AUTH_ALG: + case IW_AUTH_WPA_ENABLED: + case IW_AUTH_RX_UNENCRYPTED_EAPOL: + case IW_AUTH_ROAMING_CONTROL: + case IW_AUTH_PRIVACY_INVOKED: + default: + wlerr("Unknown cmd %d\n", cmd); + return -EINVAL; + } + + ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + ret = esp_wifi_sta_get_ap_info(&ap_info); + if (ret != 0) + { + wlerr("Failed to get AP record ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + cmd = iwr->u.param.flags & IW_AUTH_INDEX; + switch (cmd) + { + case IW_AUTH_WPA_VERSION: + iwr->u.param.value = esp_wifi_auth_trans(ap_info.authmode); + break; + + case IW_AUTH_CIPHER_PAIRWISE: + iwr->u.param.value = + esp_wifi_cipher_trans(ap_info.pairwise_cipher); + break; + + case IW_AUTH_CIPHER_GROUP: + iwr->u.param.value = + esp_wifi_cipher_trans(ap_info.group_cipher); + break; + + case IW_AUTH_KEY_MGMT: + case IW_AUTH_TKIP_COUNTERMEASURES: + case IW_AUTH_DROP_UNENCRYPTED: + case IW_AUTH_80211_AUTH_ALG: + case IW_AUTH_WPA_ENABLED: + case IW_AUTH_RX_UNENCRYPTED_EAPOL: + case IW_AUTH_ROAMING_CONTROL: + case IW_AUTH_PRIVACY_INVOKED: + default: + wlerr("Unknown cmd %d\n", cmd); + return -ENOSYS; + } + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_freq + * + * Description: + * Set/Get station frequency. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_freq(struct iwreq *iwr, bool set) +{ + int ret; + wifi_config_t wifi_cfg; + + esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg); + + if (set && (iwr->u.freq.flags == IW_FREQ_FIXED)) + { + wifi_cfg.sta.channel = esp_freq_to_channel(iwr->u.freq.m); + + ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + ret = esp_wifi_sta_disconnect(true); + if (ret != 0) + { + wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); + return ret; + } + } + else + { + wifi_ap_record_t ap_info; + + ret = esp_wifi_sta_get_ap_info(&ap_info); + if (ret != 0) + { + wlerr("Failed to get AP record ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + iwr->u.freq.flags = IW_FREQ_FIXED; + iwr->u.freq.e = 0; + iwr->u.freq.m = 2407 + 5 * ap_info.primary; + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_bitrate + * + * Description: + * Get station default bit rate (Mbps). + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set) +{ + int ret; + wifi_ap_record_t ap_info; + + if (set) + { + return -ENOSYS; + } + else + { + ret = esp_wifi_sta_get_ap_info(&ap_info); + if (ret != 0) + { + wlerr("Failed to get AP record ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + + iwr->u.bitrate.fixed = IW_FREQ_FIXED; + if (ap_info.phy_11n) + { + if (ap_info.second) + { + iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT40_BITRATE; + } + else + { + iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT20_BITRATE; + } + } + else if (ap_info.phy_11g) + { + iwr->u.bitrate.value = ESP_WIFI_11G_MAX_BITRATE; + } + else if (ap_info.phy_11b) + { + iwr->u.bitrate.value = ESP_WIFI_11B_MAX_BITRATE; + } + else + { + return -EIO; + } + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_rssi + * + * Description: + * Get Wi-Fi sensitivity (dBm). + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_rssi(struct iwreq *iwr, bool set) +{ + int ret; + wifi_ap_record_t ap_info; + + if (set) + { + wlwarn("WARN: rssi set is not supported\n"); + return -ENOSYS; + } + + ret = esp_wifi_sta_get_ap_info(&ap_info); + if (ret != 0) + { + wlerr("Failed to get AP record ret=%d " + "Make sure you are connected to an AP\n", ret); + return esp_wifi_to_errno(ret); + } + + iwr->u.sens.value = -(ap_info.rssi); + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_sta_txpower + * + * Description: + * Get/Set station transmit power (dBm). + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_txpower(struct iwreq *iwr, bool set) +{ + return esp_wifi_txpower(iwr, set); +} + +#endif /* ESP_WLAN_HAS_STA */ + +/**************************************************************************** + * Name: esp_wifi_sta_channel + * + * Description: + * Get station range of channel parameters. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_channel(struct iwreq *iwr, bool set) +{ + return esp_wifi_channel(iwr, set); +} + +/**************************************************************************** + * Name: esp_wifi_sta_country + * + * Description: + * Configure country info. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_sta_country(struct iwreq *iwr, bool set) +{ + return esp_wifi_country(iwr, set); +} + +/**************************************************************************** + * Wi-Fi SoftAP functions + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP + +/**************************************************************************** + * Name: esp_wifi_softap_send_data + * + * Description: + * Use Wi-Fi SoftAP interface to send 802.3 frame + * + * Input Parameters: + * pbuf - Packet buffer pointer + * len - Packet length + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_send_data(void *pbuf, size_t len) +{ + int ret; + + ret = esp_wifi_internal_tx(WIFI_IF_AP, pbuf, len); + if (ret != 0) + { + wlerr("Failed to send Wi-Fi data ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_softap_read_mac + * + * Description: + * Read softAP interface MAC address from efuse + * + * Input Parameters: + * mac - MAC address buffer pointer + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int esp_wifi_softap_read_mac(uint8_t *mac) +{ + int ret = esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); + return esp_wifi_to_errno(ret); +} + +/**************************************************************************** + * Name: esp_wifi_softap_password + * + * Description: + * Set/Get Wi-Fi SoftAP password + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_password(struct iwreq *iwr, bool set) +{ + int ret; + int size; + wifi_config_t wifi_cfg; + struct iw_encode_ext *ext = iwr->u.encoding.pointer; + uint8_t *pdata; + uint8_t len; +#ifdef CONFIG_DEBUG_WIRELESS_INFO + char buf[PWD_MAX_LEN + 1]; +#endif + + DEBUGASSERT(ext != NULL); + + pdata = ext->key; + len = ext->key_len; + + if (set && len > PWD_MAX_LEN) + { + return -EINVAL; + } + + pdata = ext->key; + len = ext->key_len; + + esp_wifi_get_config(WIFI_IF_AP, &wifi_cfg); + + if (set) + { + /* Clear the password field and copy the user password to it */ + + memset(wifi_cfg.ap.password, 0x0, PWD_MAX_LEN); + + if (ext->alg != IW_ENCODE_ALG_NONE) + { + memcpy(wifi_cfg.ap.password, pdata, len); + } + + ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + size = strnlen((char *)wifi_cfg.ap.password, PWD_MAX_LEN); + if (len < size) + { + return -EINVAL; + } + else + { + len = size; + memcpy(pdata, wifi_cfg.ap.password, len); + } + } + +#ifdef CONFIG_DEBUG_WIRELESS_INFO + memcpy(buf, pdata, len); + buf[len] = 0; + wlinfo("Wi-Fi SoftAP password=%s len=%d\n", buf, len); +#endif + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_softap_essid + * + * Description: + * Set/Get Wi-Fi SoftAP ESSID + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_essid(struct iwreq *iwr, bool set) +{ + int ret; + int size; + wifi_config_t wifi_cfg; + struct iw_point *essid = &iwr->u.essid; + uint8_t *pdata; + uint8_t len; +#ifdef CONFIG_DEBUG_WIRELESS_INFO + char buf[IW_ESSID_MAX_SIZE + 1]; +#endif + + DEBUGASSERT(essid != NULL); + + pdata = essid->pointer; + len = essid->length; + + if (set && len > IW_ESSID_MAX_SIZE) + { + return -EINVAL; + } + + esp_wifi_get_config(WIFI_IF_AP, &wifi_cfg); + + if (set) + { + memset(wifi_cfg.ap.ssid, 0x0, IW_ESSID_MAX_SIZE); + memcpy(wifi_cfg.ap.ssid, pdata, len); + wifi_cfg.ap.ssid_len = len; + ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + size = strnlen((char *)wifi_cfg.ap.ssid, IW_ESSID_MAX_SIZE); + if (len < size) + { + return -EINVAL; + } + else + { + len = size; + memcpy(pdata, wifi_cfg.ap.ssid, len); + } + } + +#ifdef CONFIG_DEBUG_WIRELESS_INFO + memcpy(buf, pdata, len); + buf[len] = 0; + wlinfo("Wi-Fi SoftAP ssid=%s len=%d\n", buf, len); +#endif + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_softap_bssid + * + * Description: + * Set/Get Wi-Fi SoftAP BSSID + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success (positive non-zero values are cmd-specific) + * Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_bssid(struct iwreq *iwr, bool set) +{ + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wifi_softap_connect + * + * Description: + * Trigger Wi-Fi SoftAP accept connection action. + * + * Input Parameters: + * config - The Wi-Fi config to set. + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_connect() +{ + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_softap_disconnect + * + * Description: + * Trigger Wi-Fi SoftAP drop connection action + * + * Input Parameters: + * None + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_disconnect(void) +{ + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_softap_mode + * + * Description: + * Set/Get Wi-Fi SoftAP mode code. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_mode(struct iwreq *iwr, bool set) +{ + if (!set) + { + iwr->u.mode = IW_MODE_MASTER; + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_softap_auth + * + * Description: + * Set/get authentication mode params. + * + * Note: Authmode threshold resets to WPA2 as default if password matches + * WPA2 standards (password len => 8). + * If you want to connect the device to deprecated WEP/WPA networks, set + * the threshold value to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the + * password with length and format matching to WEP/WPA standards. + * + * Note 2: WAPI blocks passwords below 8 characters. WEP is not supported. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success (positive non-zero values are cmd-specific) + * Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_auth(struct iwreq *iwr, bool set) +{ + int ret; + int cmd; + wifi_config_t wifi_cfg; + + esp_wifi_get_config(WIFI_IF_AP, &wifi_cfg); + + if (set) + { + cmd = iwr->u.param.flags & IW_AUTH_INDEX; + switch (cmd) + { + case IW_AUTH_WPA_VERSION: + { + switch (iwr->u.param.value) + { + case IW_AUTH_WPA_VERSION_DISABLED: + wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; + wlinfo("set authmode to WIFI_AUTH_OPEN\n"); + break; + + case IW_AUTH_WPA_VERSION_WPA: + wifi_cfg.ap.authmode = WIFI_AUTH_WPA_PSK; + wlinfo("set authmode to WIFI_AUTH_WPA_PSK\n"); + break; + + case IW_AUTH_WPA_VERSION_WPA2: + wifi_cfg.ap.authmode = WIFI_AUTH_WPA2_PSK; + wlinfo("set authmode to WIFI_AUTH_WPA2_PSK\n"); + break; + + case IW_AUTH_WPA_VERSION_WPA3: + wifi_cfg.ap.pmf_cfg.required = true; + wifi_cfg.ap.pmf_cfg.capable = false; + wifi_cfg.ap.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; + wifi_cfg.ap.authmode = WIFI_AUTH_WPA3_PSK; + wlinfo("set authmode to WIFI_AUTH_WPA3_PSK\n"); + break; + + default: + wlerr("Invalid wpa version %" PRId32 "\n", + iwr->u.param.value); + return -EINVAL; + } + } + break; + + case IW_AUTH_CIPHER_PAIRWISE: + case IW_AUTH_CIPHER_GROUP: + { + switch (iwr->u.param.value) + { + case IW_AUTH_CIPHER_NONE: + wifi_cfg.ap.pairwise_cipher = WIFI_CIPHER_TYPE_NONE; + wlinfo("set pairwise_cipher to " \ + "WIFI_CIPHER_TYPE_NONE\n"); + break; + + case IW_AUTH_CIPHER_WEP40: + wifi_cfg.ap.pairwise_cipher = WIFI_CIPHER_TYPE_WEP40; + wlinfo("set pairwise_cipher to " \ + "WIFI_CIPHER_TYPE_WEP40\n"); + break; + + case IW_AUTH_CIPHER_WEP104: + wifi_cfg.ap.pairwise_cipher = WIFI_CIPHER_TYPE_WEP104; + wlinfo("set pairwise_cipher to " \ + "WIFI_CIPHER_TYPE_WEP104\n"); + break; + + case IW_AUTH_CIPHER_TKIP: + wifi_cfg.ap.pairwise_cipher = WIFI_CIPHER_TYPE_TKIP; + wlinfo("set pairwise_cipher to " \ + "WIFI_CIPHER_TYPE_TKIP\n"); + break; + + case IW_AUTH_CIPHER_CCMP: + wifi_cfg.ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP; + wlinfo("set pairwise_cipher to " \ + "WIFI_CIPHER_TYPE_CCMP\n"); + break; + + case IW_AUTH_CIPHER_AES_CMAC: + wifi_cfg.ap.pairwise_cipher = + WIFI_CIPHER_TYPE_AES_CMAC128; + wlinfo("set pairwise_cipher to " \ + "WIFI_CIPHER_TYPE_AES_CMAC128\n"); + break; + + default: + wlerr("Invalid cipher mode %" PRId32 "\n", + iwr->u.param.value); + return -EINVAL; + } + } + break; + + case IW_AUTH_KEY_MGMT: + case IW_AUTH_TKIP_COUNTERMEASURES: + case IW_AUTH_DROP_UNENCRYPTED: + case IW_AUTH_80211_AUTH_ALG: + case IW_AUTH_WPA_ENABLED: + case IW_AUTH_RX_UNENCRYPTED_EAPOL: + case IW_AUTH_ROAMING_CONTROL: + case IW_AUTH_PRIVACY_INVOKED: + default: + wlerr("Unknown cmd %d\n", cmd); + return -EINVAL; + } + + ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + return -ENOSYS; + } + + return ret; +} + +/**************************************************************************** + * Name: esp_wifi_softap_freq + * + * Description: + * Set/Get SoftAP frequency. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success (positive non-zero values are cmd-specific) + * Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_freq(struct iwreq *iwr, bool set) +{ + int ret; + wifi_config_t wifi_cfg; + + esp_wifi_get_config(WIFI_IF_AP, &wifi_cfg); + + if (set) + { + int channel = esp_freq_to_channel(iwr->u.freq.m); + wifi_cfg.ap.channel = channel; + + ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); + if (ret != 0) + { + wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); + return esp_wifi_to_errno(ret); + } + } + else + { + iwr->u.freq.flags = IW_FREQ_FIXED; + iwr->u.freq.e = 0; + iwr->u.freq.m = 2407 + 5 * wifi_cfg.ap.channel; + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wifi_softap_get_bitrate + * + * Description: + * Get SoftAP default bit rate (Mbps). + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set) +{ + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wifi_softap_txpower + * + * Description: + * Get SoftAP transmit power (dBm). + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_txpower(struct iwreq *iwr, bool set) +{ + return esp_wifi_txpower(iwr, set); +} + +/**************************************************************************** + * Name: esp_wifi_softap_channel + * + * Description: + * Get SoftAP range of channel parameters. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_channel(struct iwreq *iwr, bool set) +{ + return esp_wifi_channel(iwr, set); +} + +/**************************************************************************** + * Name: esp_wifi_softap_country + * + * Description: + * Configure country info. + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_country(struct iwreq *iwr, bool set) +{ + return esp_wifi_country(iwr, set); +} + +/**************************************************************************** + * Name: esp_wifi_softap_rssi + * + * Description: + * Get Wi-Fi sensitivity (dBm). + * + * Input Parameters: + * iwr - The argument of the ioctl cmd + * set - true: set data; false: get data + * + * Returned Value: + * OK on success; Negated errno returned on failure. + * + ****************************************************************************/ + +int esp_wifi_softap_rssi(struct iwreq *iwr, bool set) +{ + return -ENOSYS; +} + +#endif /* ESP_WLAN_HAS_SOFTAP */ diff --git a/arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.h b/arch/xtensa/src/common/espressif/esp_wifi_api.h similarity index 68% rename from arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.h rename to arch/xtensa/src/common/espressif/esp_wifi_api.h index 4d42b77b34..6e57cc340b 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.h +++ b/arch/xtensa/src/common/espressif/esp_wifi_api.h @@ -1,5 +1,7 @@ /**************************************************************************** - * arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.h + * arch/xtensa/src/common/espressif/esp_wifi_api.h + * + * SPDX-License-Identifier: Apache-2.0 * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -18,173 +20,184 @@ * ****************************************************************************/ -#ifndef __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_WIFI_ADAPTER_H -#define __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_WIFI_ADAPTER_H +#ifndef __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WIFI_API_H +#define __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WIFI_API_H /**************************************************************************** * Included Files ****************************************************************************/ -#include +#include +#include +#include + #include -#include - -#ifndef __ASSEMBLY__ - -#undef EXTERN -#if defined(__cplusplus) -#define EXTERN extern "C" -extern "C" -{ -#else -#define EXTERN extern -#endif - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define SSID_MAX_LEN (32) -#define PWD_MAX_LEN (64) - -#define CONFIG_IDF_TARGET_ESP32S2 1 - -/* Define esp_err_t */ - -typedef int esp_err_t; - -/* Wi-Fi event callback function */ - -typedef void (*wifi_evt_cb_t)(void *p); - -/* Wi-Fi TX done callback function */ - -typedef void (*wifi_txdone_cb_t)(uint8_t *data, uint16_t *len, bool status); +#include "esp_wlan_netdev.h" /**************************************************************************** * Public Function Prototypes ****************************************************************************/ /**************************************************************************** - * Name: esp_wifi_adapter_init + * Name: esp_wifi_api_adapter_deinit * * Description: - * Initialize ESP32S2 Wi-Fi adapter + * De-initialize Wi-Fi adapter, freeing all resources allocated by + * esp_wifi_init. Also stops the Wi-Fi task. * * Input Parameters: * None * * Returned Value: - * 0 if success or -1 if fail + * OK on success; Negated errno on failure. * ****************************************************************************/ -int esp_wifi_adapter_init(void); +int esp_wifi_api_adapter_deinit(void); /**************************************************************************** - * Name: esp_wifi_free_eb + * Name: esp_wifi_api_adapter_init * * Description: - * Free Wi-Fi receive callback input eb pointer + * Initialize the Wi-Fi driver, control structure, buffers and Wi-Fi task. * * Input Parameters: - * eb - Wi-Fi receive callback input eb pointer + * None. * * Returned Value: - * None + * OK on success; Negated errno on failure. * ****************************************************************************/ -void esp_wifi_free_eb(void *eb); +int esp_wifi_api_adapter_init(void); /**************************************************************************** - * Name: esp_wifi_sta_start + * Name: esp_wifi_api_start * * Description: - * Start Wi-Fi station. + * Start Wi-Fi station. This will start the proper Wi-Fi mode based on + * the AP/Station configuration. * * Input Parameters: - * None + * start_mode - The Wi-Fi mode to start from + * nuttx/include/nuttx/wireless/wireless.h. * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno on failure. * ****************************************************************************/ -int esp_wifi_sta_start(void); +int esp_wifi_api_start(uint32_t start_mode); /**************************************************************************** - * Name: esp_wifi_sta_stop + * Name: esp_wifi_api_stop * * Description: - * Stop Wi-Fi station. + * Stops Wi-Fi AP, Station or both. + * + * If AP + SoftAP are running, be aware that both will be stopped briefly, + * and then the remaining one will be restarted. * * Input Parameters: - * None + * stop_mode - The Wi-Fi mode to stop from + * nuttx/include/nuttx/wireless/wireless.h. * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno on failure. * ****************************************************************************/ -int esp_wifi_sta_stop(void); +int esp_wifi_api_stop(uint32_t stop_mode); + +/**************************************************************************** + * Name: esp_wifi_api_sta_register_rx_callback + * + * Description: + * Register a callback function for the Wi-Fi station interface. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_sta_register_rx_callback(void *cb); + +/**************************************************************************** + * Name: esp_wifi_api_softap_register_rx_callback + * + * Description: + * Register a callback function for the Wi-Fi softAP interface. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_softap_register_rx_callback(void *cb); + +/**************************************************************************** + * Name: esp_wifi_api_registe_tx_done_callback + * + * Description: + * Register a callback function for transmission done. Valid for + * both station and softAP and needs to be called only once on bringup. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_register_tx_done_callback(void *cb); + +/**************************************************************************** + * Name: esp_wifi_api_free_rx_buffer + * + * Description: + * Free the RX buffer allocated by the Wi-Fi driver. + * + * Input Parameters: + * eb - The event buffer to free + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void esp_wifi_api_free_rx_buffer(void *eb); + +/**************************************************************************** + * Station functions + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA /**************************************************************************** * Name: esp_wifi_sta_send_data * * Description: - * Use Wi-Fi station interface to send 802.3 frame + * Use Wi-Fi station interface to send 802.3 frame. * * Input Parameters: * pbuf - Packet buffer pointer * len - Packet length * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_sta_send_data(void *pbuf, size_t len); -/**************************************************************************** - * Name: esp_wifi_sta_register_recv_cb - * - * Description: - * Regitser Wi-Fi station receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); - -/**************************************************************************** - * Name: esp_wifi_sta_register_txdone_cb - * - * Description: - * Register the station TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb); - /**************************************************************************** * Name: esp_wifi_sta_read_mac * @@ -202,18 +215,17 @@ void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb); int esp_wifi_sta_read_mac(uint8_t *mac); /**************************************************************************** - * Name: esp_wifi_set_password + * Name: esp_wifi_sta_password * * Description: - * Set/Get Wi-Fi station password + * Set/Get Wi-Fi station password. * * Input Parameters: * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data + * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -227,11 +239,10 @@ int esp_wifi_sta_password(struct iwreq *iwr, bool set); * * Input Parameters: * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data + * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -241,15 +252,14 @@ int esp_wifi_sta_essid(struct iwreq *iwr, bool set); * Name: esp_wifi_sta_bssid * * Description: - * Set/Get Wi-Fi station BSSID + * Set/Get Wi-Fi station BSSID. * * Input Parameters: * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data + * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -259,14 +269,13 @@ int esp_wifi_sta_bssid(struct iwreq *iwr, bool set); * Name: esp_wifi_sta_connect * * Description: - * Trigger Wi-Fi station connection action + * Trigger Wi-Fi station connection action. * * Input Parameters: * None * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -276,18 +285,17 @@ int esp_wifi_sta_connect(void); * Name: esp_wifi_sta_disconnect * * Description: - * Trigger Wi-Fi station disconnection action + * Trigger Wi-Fi station disconnection action. * * Input Parameters: - * None + * None. * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ -int esp_wifi_sta_disconnect(void); +int esp_wifi_sta_disconnect(bool allow_reconnect); /**************************************************************************** * Name: esp_wifi_sta_mode @@ -300,8 +308,7 @@ int esp_wifi_sta_disconnect(void); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -329,15 +336,14 @@ int esp_wifi_sta_auth(struct iwreq *iwr, bool set); * Name: esp_wifi_sta_freq * * Description: - * Get station frequency. + * Set/Get station frequency. * * Input Parameters: * iwr - The argument of the ioctl cmd * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -354,33 +360,33 @@ int esp_wifi_sta_freq(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set); +#endif /* ESP_WLAN_HAS_STA */ + /**************************************************************************** - * Name: esp_wifi_sta_get_txpower + * Name: esp_wifi_sta_txpower * * Description: - * Get station transmit power (dBm). + * Get/Set station transmit power (dBm). * * Input Parameters: * iwr - The argument of the ioctl cmd * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_sta_txpower(struct iwreq *iwr, bool set); /**************************************************************************** - * Name: esp_wifi_sta_get_channel_range + * Name: esp_wifi_sta_channel * * Description: * Get station range of channel parameters. @@ -390,8 +396,7 @@ int esp_wifi_sta_txpower(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -408,13 +413,14 @@ int esp_wifi_sta_channel(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_sta_country(struct iwreq *iwr, bool set); +#ifdef ESP_WLAN_HAS_STA + /**************************************************************************** * Name: esp_wifi_sta_rssi * @@ -426,100 +432,37 @@ int esp_wifi_sta_country(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_sta_rssi(struct iwreq *iwr, bool set); -/**************************************************************************** - * Name: esp_wifi_softap_start - * - * Description: - * Start Wi-Fi softAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_start(void); +#endif /* ESP_WLAN_HAS_STA */ /**************************************************************************** - * Name: esp_wifi_softap_stop - * - * Description: - * Stop Wi-Fi softAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * + * SoftAP functions ****************************************************************************/ -int esp_wifi_softap_stop(void); +#ifdef ESP_WLAN_HAS_SOFTAP /**************************************************************************** * Name: esp_wifi_softap_send_data * * Description: - * Use Wi-Fi softAP interface to send 802.3 frame + * Use Wi-Fi SoftAP interface to send 802.3 frame * * Input Parameters: * pbuf - Packet buffer pointer * len - Packet length * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_softap_send_data(void *pbuf, size_t len); -/**************************************************************************** - * Name: esp_wifi_softap_register_recv_cb - * - * Description: - * Regitser Wi-Fi softAP receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); - -/**************************************************************************** - * Name: esp_wifi_softap_register_txdone_cb - * - * Description: - * Register the softAP TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb); - /**************************************************************************** * Name: esp_wifi_softap_read_mac * @@ -544,11 +487,10 @@ int esp_wifi_softap_read_mac(uint8_t *mac); * * Input Parameters: * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data + * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -565,8 +507,7 @@ int esp_wifi_softap_password(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -576,11 +517,11 @@ int esp_wifi_softap_essid(struct iwreq *iwr, bool set); * Name: esp_wifi_softap_bssid * * Description: - * Set/Get Wi-Fi softAP BSSID + * Set/Get Wi-Fi SoftAP BSSID * * Input Parameters: * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data + * set - true: set data; false: get data * * Returned Value: * OK on success (positive non-zero values are cmd-specific) @@ -594,14 +535,13 @@ int esp_wifi_softap_bssid(struct iwreq *iwr, bool set); * Name: esp_wifi_softap_connect * * Description: - * Trigger Wi-Fi softAP accept connection action + * Trigger Wi-Fi SoftAP accept connection action. * * Input Parameters: - * None + * config - The Wi-Fi config to set. * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -611,14 +551,13 @@ int esp_wifi_softap_connect(void); * Name: esp_wifi_softap_disconnect * * Description: - * Trigger Wi-Fi softAP drop connection action + * Trigger Wi-Fi SoftAP drop connection action * * Input Parameters: * None * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -635,8 +574,7 @@ int esp_wifi_softap_disconnect(void); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -646,7 +584,7 @@ int esp_wifi_softap_mode(struct iwreq *iwr, bool set); * Name: esp_wifi_softap_auth * * Description: - * Set/Get authentication mode params. + * Set/get authentication mode params. * * Input Parameters: * iwr - The argument of the ioctl cmd @@ -679,7 +617,7 @@ int esp_wifi_softap_auth(struct iwreq *iwr, bool set); int esp_wifi_softap_freq(struct iwreq *iwr, bool set); /**************************************************************************** - * Name: esp_wifi_softap_get_bitrate + * Name: esp_wifi_softap_bitrate * * Description: * Get SoftAP default bit rate (Mbps). @@ -689,8 +627,7 @@ int esp_wifi_softap_freq(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -707,8 +644,7 @@ int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -725,8 +661,7 @@ int esp_wifi_softap_txpower(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -743,8 +678,7 @@ int esp_wifi_softap_channel(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ @@ -761,33 +695,12 @@ int esp_wifi_softap_country(struct iwreq *iwr, bool set); * set - true: set data; false: get data * * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. + * OK on success; Negated errno returned on failure. * ****************************************************************************/ int esp_wifi_softap_rssi(struct iwreq *iwr, bool set); -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ +#endif /* ESP_WLAN_HAS_SOFTAP */ -void esp_wifi_stop_callback(void); - -#ifdef __cplusplus -} -#endif -#undef EXTERN - -#endif /* __ASSEMBLY__ */ -#endif /* __ARCH_XTENSA_SRC_ESP32S2_ESP32S2_WIFI_ADAPTER_H */ +#endif /* __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WIFI_API_H */ diff --git a/arch/xtensa/src/common/espressif/esp_wifi_event_handler.c b/arch/xtensa/src/common/espressif/esp_wifi_event_handler.c new file mode 100644 index 0000000000..3d2e68812d --- /dev/null +++ b/arch/xtensa/src/common/espressif/esp_wifi_event_handler.c @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/xtensa/src/common/espressif/esp_wifi_event_handler.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include "esp_wifi.h" + +#include "esp_wifi_utils.h" +#include "esp_wifi_api.h" + +#ifndef CONFIG_SCHED_LPWORK +# error "CONFIG_SCHED_LPWORK must be defined" +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* CONFIG_POWER_SAVE_MODEM */ + +#if defined(CONFIG_ESPRESSIF_POWER_SAVE_MIN_MODEM) +# define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM +#elif defined(CONFIG_ESPRESSIF_POWER_SAVE_MAX_MODEM) +# define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM +#elif defined(CONFIG_ESPRESSIF_POWER_SAVE_NONE) +# define DEFAULT_PS_MODE WIFI_PS_NONE +#else +# define DEFAULT_PS_MODE WIFI_PS_NONE +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* Wi-Fi event notification private data */ + +struct wifi_notify +{ + bool assigned; /* Flag indicate if it is used */ + pid_t pid; /* Signal's target thread PID */ + struct sigevent event; /* Signal event private data */ + struct sigwork_s work; /* Signal work private data */ +}; + +/* Wi-Fi event private data */ + +struct evt_adpt +{ + sq_entry_t entry; /* Sequence entry */ + wifi_event_t id; /* Event ID */ + uint8_t buf[0]; /* Event private data */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct wifi_notify g_wifi_notify[WIFI_EVENT_MAX]; +static struct work_s g_wifi_evt_work; +static sq_queue_t g_wifi_evt_queue; +static spinlock_t g_lock; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_reconnect_work_cb + * + * Description: + * Function called by a work queue to reconnect to Wi-Fi in case of + * a disconnection event and WIFI_REASON_ASSOC_LEAVE reason. + * Must check if the failure_retry_cnt is not 0, otherwise it may + * reconnect when not desired, such as when the user has actually + * asked to disconnect from the AP. + * + * Input Parameters: + * arg - Not used. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA +static void esp_reconnect_work_cb(void *arg) +{ + UNUSED(arg); + int ret; + wifi_config_t wifi_config; + + esp_wifi_get_config(WIFI_IF_STA, &wifi_config); + if (wifi_config.sta.failure_retry_cnt == 0) + { + wlinfo("Reconnect to Wi-Fi on callback: failure_retry_cnt is 0\n"); + return; + } + + ret = esp_wifi_sta_connect(); + if (ret < 0) + { + wlerr("Failed to reconnect to Wi-Fi on callback\n"); + } +} +#endif /* ESP_WLAN_HAS_STA */ + +/**************************************************************************** + * Name: esp_evt_work_cb + * + * Description: + * Process Wi-Fi events. + * + * Input Parameters: + * arg - Not used. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static void esp_evt_work_cb(void *arg) +{ + int ret; + irqstate_t flags; + struct evt_adpt *evt_adpt; + struct wifi_notify *notify; + wifi_ps_type_t ps_type = DEFAULT_PS_MODE; + + while (1) + { + flags = spin_lock_irqsave(&g_lock); + evt_adpt = (struct evt_adpt *)sq_remfirst(&g_wifi_evt_queue); + spin_unlock_irqrestore(&g_lock, flags); + if (evt_adpt == NULL) + { + break; + } + + /* Some of the following logic (eg. esp_wlan_sta_set_linkstatus) + * can take net_lock(). To maintain the consistent locking order, + * we take net_lock() here before taking esp_wifi_lock. Note that + * net_lock() is a recursive lock. + */ + + net_lock(); + esp_wifi_lock(true); + + switch (evt_adpt->id) + { +#ifdef ESP_WLAN_DEVS + case WIFI_EVENT_SCAN_DONE: + esp_wifi_scan_event_parse(); + break; +#endif + case WIFI_EVENT_HOME_CHANNEL_CHANGE: + wlinfo("Wi-Fi home channel change\n"); + break; + +#ifdef ESP_WLAN_HAS_STA + case WIFI_EVENT_STA_START: + wlinfo("Wi-Fi sta start\n"); + + ret = esp_wifi_set_ps(ps_type); + if (ret != 0) + { + wlerr("Failed to set power save type\n"); + break; + } + break; + + case WIFI_EVENT_STA_STOP: + wlinfo("Wi-Fi station stopped\n"); + break; + + case WIFI_EVENT_STA_CONNECTED: + wlinfo("Wi-Fi station connected\n"); + esp_wlan_sta_connect_success_hook(); + break; + + case WIFI_EVENT_STA_DISCONNECTED: + wifi_event_sta_disconnected_t *event = + (wifi_event_sta_disconnected_t *)evt_adpt->buf; + wifi_err_reason_t reason = event->reason; + + wlinfo("Wi-Fi station disconnected, reason: %u\n", reason); + esp_wlan_sta_disconnect_hook(); + if (reason == WIFI_REASON_ASSOC_LEAVE) + { + work_queue(LPWORK, &g_wifi_evt_work, esp_reconnect_work_cb, + NULL, 0); + } + + break; + + case WIFI_EVENT_STA_AUTHMODE_CHANGE: + wlinfo("Wi-Fi station auth mode change\n"); + break; +#endif /* ESP_WLAN_HAS_STA */ + +#ifdef ESP_WLAN_HAS_SOFTAP + case WIFI_EVENT_AP_START: + wlinfo("INFO: Wi-Fi softap start\n"); + esp_wlan_softap_connect_success_hook(); + ret = esp_wifi_set_ps(ps_type); + if (ret != 0) + { + wlerr("Failed to set power save type\n"); + break; + } + break; + + case WIFI_EVENT_AP_STOP: + wlinfo("Wi-Fi softap stop\n"); + esp_wlan_softap_disconnect_hook(); + break; + + case WIFI_EVENT_AP_STACONNECTED: + wlinfo("Wi-Fi station joined AP\n"); + break; + + case WIFI_EVENT_AP_STADISCONNECTED: + wlinfo("Wi-Fi station left AP\n"); + break; +#endif /* ESP_WLAN_HAS_SOFTAP */ + default: + break; + } + + notify = &g_wifi_notify[evt_adpt->id]; + if (notify->assigned) + { + notify->event.sigev_value.sival_ptr = evt_adpt->buf; + + ret = nxsig_notification(notify->pid, ¬ify->event, + SI_QUEUE, ¬ify->work); + if (ret < 0) + { + wlwarn("nxsig_notification event ID=%d failed: %d\n", + evt_adpt->id, ret); + } + } + + esp_wifi_lock(false); + net_unlock(); + + kmm_free(evt_adpt); + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_event_post + * + * Description: + * Posts an event to the event loop system. The event is queued in a FIFO + * and processed asynchronously in the low-priority work queue. + * + * Input Parameters: + * event_base - Identifier for the event category (e.g. WIFI_EVENT) + * event_id - Event ID within the event base category + * event_data - Pointer to event data structure + * event_data_size - Size of event data structure + * ticks - Number of ticks to wait (currently unused) + * + * Returned Value: + * 0 on success + * -1 on failure with following error conditions: + * - Invalid event ID + * - Memory allocation failure + * + * Assumptions/Limitations: + * - Event data is copied into a new buffer, so the original can be freed + * - Events are processed in FIFO order in the low priority work queue + * - The function is thread-safe and can be called from interrupt context + * + ****************************************************************************/ + +int esp_event_post(const char *event_base, + int32_t event_id, + void *event_data, + size_t event_data_size, + uint32_t ticks) +{ + size_t size; + int32_t id; + irqstate_t flags; + struct evt_adpt *evt_adpt; + + wlinfo("Event: base=%s id=%ld data=%p data_size=%u ticks=%lu\n", + event_base, event_id, event_data, event_data_size, ticks); + + size = event_data_size + sizeof(struct evt_adpt); + evt_adpt = kmm_malloc(size); + if (evt_adpt == NULL) + { + wlerr("ERROR: Failed to alloc %d memory\n", size); + return -1; + } + + evt_adpt->id = event_id; + memcpy(evt_adpt->buf, event_data, event_data_size); + + flags = enter_critical_section(); + sq_addlast(&evt_adpt->entry, &g_wifi_evt_queue); + leave_critical_section(flags); + + work_queue(LPWORK, &g_wifi_evt_work, esp_evt_work_cb, NULL, 0); + + return 0; +} + +/**************************************************************************** + * Name: esp_evt_work_init + * + * Description: + * Initialize the event work queue + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void esp_evt_work_init(void) +{ + sq_init(&g_wifi_evt_queue); +} diff --git a/arch/xtensa/src/common/espressif/esp_wifi_utils.c b/arch/xtensa/src/common/espressif/esp_wifi_utils.c index 1a9eb08245..37668b51e2 100644 --- a/arch/xtensa/src/common/espressif/esp_wifi_utils.c +++ b/arch/xtensa/src/common/espressif/esp_wifi_utils.c @@ -27,53 +27,21 @@ #include #include +#include #include #include #include #include #include -#include -#include -#ifdef CONFIG_ARCH_CHIP_ESP32 -#include "esp32_wifi_adapter.h" -#endif -#ifdef CONFIG_ARCH_CHIP_ESP32S2 -#include "esp32s2_wifi_adapter.h" -#endif -#ifdef CONFIG_ARCH_CHIP_ESP32S3 -#include "esp32s3_wifi_adapter.h" -#endif - -#ifdef CONFIG_ESPRESSIF_BLE -# ifdef CONFIG_ARCH_CHIP_ESP32 -# include "esp32_ble_adapter.h" -# endif -# ifdef CONFIG_ARCH_CHIP_ESP32S3 -# include "esp32s3_ble_adapter.h" -# endif -# ifdef CONFIG_ESPRESSIF_WIFI_BT_COEXIST -# include "private/esp_coexist_internal.h" -# endif -#endif - -#include "espressif/esp_wlan.h" - -#include "esp_wifi_utils.h" -#include "esp_wireless.h" - -#include "esp_log.h" -#include "esp_mac.h" -#include "esp_private/phy.h" -#include "esp_private/wifi.h" -#include "esp_random.h" #include "esp_timer.h" -#ifdef CONFIG_ESPRESSIF_WIFI -# include "esp_wpa.h" -#endif -#include "rom/ets_sys.h" -#include "soc/soc_caps.h" +#include "esp_wifi_utils.h" +#include "esp_wlan_netdev.h" + +#include "esp_wifi.h" +#include "esp_err.h" +#include "esp_wifi_types_generic.h" /**************************************************************************** * Pre-processor Definitions @@ -96,18 +64,6 @@ #define CHANNEL_MAX_NUM (14) -/* CONFIG_POWER_SAVE_MODEM */ - -#if defined(CONFIG_ESPRESSIF_POWER_SAVE_MIN_MODEM) -# define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM -#elif defined(CONFIG_ESPRESSIF_POWER_SAVE_MAX_MODEM) -# define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM -#elif defined(CONFIG_ESPRESSIF_POWER_SAVE_NONE) -# define DEFAULT_PS_MODE WIFI_PS_NONE -#else -# define DEFAULT_PS_MODE WIFI_PS_NONE -#endif - /**************************************************************************** * Private Types ****************************************************************************/ @@ -129,70 +85,10 @@ struct wifi_scan_result unsigned int scan_result_size; /* Current size of temp buffer */ }; -/* Wi-Fi event ID */ - -enum wifi_adpt_evt_e -{ - WIFI_ADPT_EVT_SCAN_DONE = 0, - WIFI_ADPT_EVT_STA_START, - WIFI_ADPT_EVT_STA_CONNECT, - WIFI_ADPT_EVT_STA_DISCONNECT, - WIFI_ADPT_EVT_STA_AUTHMODE_CHANGE, - WIFI_ADPT_EVT_STA_STOP, - WIFI_ADPT_EVT_AP_START, - WIFI_ADPT_EVT_AP_STOP, - WIFI_ADPT_EVT_AP_STACONNECTED, - WIFI_ADPT_EVT_AP_STADISCONNECTED, - WIFI_ADPT_EVT_MAX, -}; - -/* Wi-Fi event callback function */ - -typedef void (*wifi_evt_cb_t)(void *p); - -/* Wi-Fi event private data */ - -struct evt_adpt -{ - sq_entry_t entry; /* Sequence entry */ - int32_t id; /* Event ID */ - uint8_t buf[0]; /* Event private data */ -}; - -/* Wi-Fi event notification private data */ - -struct wifi_notify -{ - bool assigned; /* Flag indicate if it is used */ - pid_t pid; /* Signal's target thread PID */ - struct sigevent event; /* Signal event private data */ - struct sigwork_s work; /* Signal work private data */ -}; - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -static int esp_event_id_map(int event_id); - /**************************************************************************** * Public Data ****************************************************************************/ -/* Wi-Fi interface configuration */ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Private Data ****************************************************************************/ @@ -204,95 +100,107 @@ static struct wifi_scan_result g_scan_priv = static uint8_t g_channel_num; static uint8_t g_channel_list[CHANNEL_MAX_NUM]; -/* Wi-Fi event private data */ - -static spinlock_t g_lock_event; -static struct work_s g_wifi_evt_work; -static sq_queue_t g_wifi_evt_queue; -static struct wifi_notify g_wifi_notify[WIFI_ADPT_EVT_MAX]; - static mutex_t g_wifiexcl_lock = NXMUTEX_INITIALIZER; -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: esp_event_id_map - * - * Description: - * Transform from esp-idf event ID to Wi-Fi adapter event ID - * - * Input Parameters: - * event_id - esp-idf event ID - * - * Returned Value: - * Wi-Fi adapter event ID - * - ****************************************************************************/ - -static int esp_event_id_map(int event_id) -{ - int id; - - switch (event_id) - { - case WIFI_EVENT_SCAN_DONE: - id = WIFI_ADPT_EVT_SCAN_DONE; - break; - -#ifdef ESPRESSIF_WLAN_HAS_STA - case WIFI_EVENT_STA_START: - id = WIFI_ADPT_EVT_STA_START; - break; - - case WIFI_EVENT_STA_CONNECTED: - id = WIFI_ADPT_EVT_STA_CONNECT; - break; - - case WIFI_EVENT_STA_DISCONNECTED: - id = WIFI_ADPT_EVT_STA_DISCONNECT; - break; - - case WIFI_EVENT_STA_AUTHMODE_CHANGE: - id = WIFI_ADPT_EVT_STA_AUTHMODE_CHANGE; - break; - - case WIFI_EVENT_STA_STOP: - id = WIFI_ADPT_EVT_STA_STOP; - break; -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - case WIFI_EVENT_AP_START: - id = WIFI_ADPT_EVT_AP_START; - break; - - case WIFI_EVENT_AP_STOP: - id = WIFI_ADPT_EVT_AP_STOP; - break; - - case WIFI_EVENT_AP_STACONNECTED: - id = WIFI_ADPT_EVT_AP_STACONNECTED; - break; - - case WIFI_EVENT_AP_STADISCONNECTED: - id = WIFI_ADPT_EVT_AP_STADISCONNECTED; - break; -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - default: - wlerr("ERROR: Unknown event ID: %d\n", event_id); - return -1; - } - - return id; -} - /**************************************************************************** * Public Functions ****************************************************************************/ +#ifdef CONFIG_ESPRESSIF_WIFI + +/**************************************************************************** + * Name: esp_wifi_mode_translate + * + * Description: + * Translate wireless mode constants to ESP Wi-Fi mode constants. + * + * Input Parameters: + * wireless_mode - Wireless mode from wireless.h (IW_MODE_*) + * + * Returned Value: + * ESP Wi-Fi mode (WIFI_MODE_*) on success + * -EINVAL on failure + * + ****************************************************************************/ + +wifi_mode_t esp_wifi_mode_translate(uint32_t wireless_mode) +{ + switch (wireless_mode) + { + case IW_MODE_INFRA: + return WIFI_MODE_STA; + + case IW_MODE_MASTER: + return WIFI_MODE_AP; + + default: + wlerr("Invalid wireless mode=%ld\n", wireless_mode); + return -EINVAL; + } +} + +/**************************************************************************** + * Name: esp_freq_to_channel + * + * Description: + * Converts Wi-Fi frequency to channel. + * + * Input Parameters: + * freq - Wi-Fi frequency + * + * Returned Value: + * Wi-Fi channel + * + ****************************************************************************/ + +int esp_freq_to_channel(uint16_t freq) +{ + int channel = 0; + if (freq >= 2412 && freq <= 2484) + { + if (freq == 2484) + { + channel = 14; + } + else + { + channel = freq - 2407; + if (channel % 5) + { + return 0; + } + + channel /= 5; + } + + return channel; + } + + if (freq >= 5005 && freq < 5900) + { + if (freq % 5) + { + return 0; + } + + channel = (freq - 5000) / 5; + return channel; + } + + if (freq >= 4905 && freq < 5000) + { + if (freq % 5) + { + return 0; + } + + channel = (freq - 4000) / 5; + return channel; + } + + return 0; +} + /**************************************************************************** * Name: esp_wifi_start_scan * @@ -316,8 +224,8 @@ int esp_wifi_start_scan(struct iwreq *iwr) int ret = 0; int i; uint8_t target_mac[MAC_LEN]; - uint8_t target_ssid[SSID_MAX_LEN + 1]; - memset(target_ssid, 0x0, sizeof(SSID_MAX_LEN + 1)); + uint8_t target_ssid[IW_ESSID_MAX_SIZE + 1]; + memset(target_ssid, 0x0, sizeof(IW_ESSID_MAX_SIZE + 1)); if (iwr == NULL) { @@ -395,7 +303,6 @@ int esp_wifi_start_scan(struct iwreq *iwr) config->scan_type = WIFI_SCAN_TYPE_ACTIVE; /* Active scan */ } - esp_wifi_start(); ret = esp_wifi_scan_start(config, false); if (ret != OK) { @@ -440,10 +347,10 @@ int esp_wifi_start_scan(struct iwreq *iwr) * Name: esp_wifi_get_scan_results * * Description: - * Get scan result + * Get Wi-Fi scan results. * * Input Parameters: - * iwr - The argument of the ioctl cmd + * iwr - The argument of the ioctl cmd. * * Returned Value: * OK on success (positive non-zero values are cmd-specific) @@ -544,13 +451,13 @@ exit_failed: * Name: esp_wifi_scan_event_parse * * Description: - * Parse scan information + * Parse scan information Wi-Fi AP scan results. * * Input Parameters: - * None + * None. * * Returned Value: - * None + * None. * ****************************************************************************/ @@ -639,7 +546,8 @@ void esp_wifi_scan_event_parse(void) /* Copy ESSID */ essid_len = MIN(strlen((const char *) - ap_list_buffer[bss_count].ssid), SSID_MAX_LEN); + ap_list_buffer[bss_count].ssid), + IW_ESSID_MAX_SIZE); essid_len_aligned = (essid_len + 3) & -4; if (result_size < ESP_IW_EVENT_SIZE(essid) + essid_len_aligned) { @@ -762,277 +670,85 @@ scan_result_full: priv->scan_status = ESP_SCAN_DONE; nxsem_post(&priv->scan_signal); } +#endif /* CONFIG_ESPRESSIF_WIFI */ /**************************************************************************** - * Name: esp_evt_work_cb + * Name: esp_wifi_to_errno * * Description: - * Process the cached event + * Transform from ESP Wi-Fi error code to NuttX error code. * * Input Parameters: - * arg - No mean + * err - ESP Wi-Fi error code. * * Returned Value: - * None + * NuttX error code defined in errno.h * ****************************************************************************/ -void esp_evt_work_cb(void *arg) +int32_t esp_wifi_to_errno(int err) { int ret; - irqstate_t flags; - struct evt_adpt *evt_adpt; - struct wifi_notify *notify; - wifi_ps_type_t ps_type = DEFAULT_PS_MODE; - while (1) + if (err < ESP_ERR_WIFI_BASE) { - flags = spin_lock_irqsave(&g_lock_event); - evt_adpt = (struct evt_adpt *)sq_remfirst(&g_wifi_evt_queue); - spin_unlock_irqrestore(&g_lock_event, flags); - if (!evt_adpt) + /* Unmask component error bits */ + + ret = err & 0xfff; + + switch (ret) { - break; - } - - /* Some of the following logic (eg. esp32s3_wlan_sta_set_linkstatus) - * can take net_lock(). To maintain the consistent locking order, - * we take net_lock() here before taking esp_wifi_lock. Note that - * net_lock() is a recursive lock. - */ - - net_lock(); - esp_wifi_lock(true); - - switch (evt_adpt->id) - { - case WIFI_ADPT_EVT_SCAN_DONE: - esp_wifi_scan_event_parse(); + case ESP_OK: + ret = OK; + break; + case ESP_ERR_NO_MEM: + ret = -ENOMEM; break; -#ifdef ESPRESSIF_WLAN_HAS_STA - case WIFI_ADPT_EVT_STA_START: - wlinfo("Wi-Fi sta start\n"); - - g_sta_connected = false; - -#ifdef CONFIG_ESPRESSIF_BLE - if (esp_bt_controller_get_status() != - ESP_BT_CONTROLLER_STATUS_IDLE) - { - if (ps_type == WIFI_PS_NONE) - { - ps_type = WIFI_PS_MIN_MODEM; - } - } -#endif - - ret = esp_wifi_set_ps(ps_type); - - if (ret) - { - wlerr("Failed to set power save type\n"); - break; - } - else - { - wlinfo("INFO: Set ps type=%d\n", ps_type); - } - - ret = esp_wifi_get_config(WIFI_IF_STA, &g_sta_wifi_cfg); - if (ret) - { - wlerr("Failed to get Wi-Fi config data ret=%d\n", ret); - } + case ESP_ERR_INVALID_ARG: + ret = -EINVAL; break; - case WIFI_ADPT_EVT_STA_CONNECT: - wlinfo("Wi-Fi sta connect\n"); - g_sta_connected = true; - ret = esp_wlan_sta_set_linkstatus(true); - if (ret < 0) - { - wlerr("ERROR: Failed to set Wi-Fi station link status\n"); - } - + case ESP_ERR_INVALID_STATE: + ret = -EIO; break; - case WIFI_ADPT_EVT_STA_DISCONNECT: - wlinfo("Wi-Fi sta disconnect\n"); - g_sta_connected = false; - ret = esp_wlan_sta_set_linkstatus(false); - if (ret < 0) - { - wlerr("ERROR: Failed to set Wi-Fi station link status\n"); - } - - if (g_sta_reconnect) - { - ret = esp_wifi_connect(); - if (ret) - { - wlerr("Failed to connect AP error=%d\n", ret); - } - } + case ESP_ERR_INVALID_SIZE: + ret = -EINVAL; break; - case WIFI_ADPT_EVT_STA_STOP: - wlinfo("Wi-Fi sta stop\n"); - g_sta_connected = false; - break; -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - case WIFI_ADPT_EVT_AP_START: - wlinfo("INFO: Wi-Fi softap start\n"); - -#ifdef CONFIG_ESPRESSIF_BLE - if (esp_bt_controller_get_status() != - ESP_BT_CONTROLLER_STATUS_IDLE) - { - if (ps_type == WIFI_PS_NONE) - { - ps_type = WIFI_PS_MIN_MODEM; - } - } -#endif - - ret = esp_wifi_set_ps(ps_type); - - if (ret) - { - wlerr("Failed to set power save type\n"); - break; - } - else - { - wlinfo("INFO: Set ps type=%d\n", ps_type); - } - - ret = esp_wifi_get_config(WIFI_IF_AP, &g_softap_wifi_cfg); - if (ret) - { - wlerr("Failed to get Wi-Fi config data ret=%d\n", ret); - } + case ESP_ERR_NOT_FOUND: + ret = -ENOSYS; break; - case WIFI_ADPT_EVT_AP_STOP: - wlinfo("INFO: Wi-Fi softap stop\n"); + case ESP_ERR_NOT_SUPPORTED: + ret = -ENOSYS; break; - case WIFI_ADPT_EVT_AP_STACONNECTED: - wlinfo("INFO: Wi-Fi station join\n"); + case ESP_ERR_TIMEOUT: + ret = -ETIMEDOUT; break; - case WIFI_ADPT_EVT_AP_STADISCONNECTED: - wlinfo("INFO: Wi-Fi station leave\n"); + case ESP_ERR_INVALID_MAC: + ret = -EINVAL; break; -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ + default: + ret = ERROR; break; } - - notify = &g_wifi_notify[evt_adpt->id]; - if (notify->assigned) - { - notify->event.sigev_value.sival_ptr = evt_adpt->buf; - - ret = nxsig_notification(notify->pid, ¬ify->event, - SI_QUEUE, ¬ify->work); - if (ret < 0) - { - wlwarn("nxsig_notification event ID=%" PRId32 " failed: %d\n", - evt_adpt->id, ret); - } - } - - esp_wifi_lock(false); - net_unlock(); - - kmm_free(evt_adpt); } -} - -/**************************************************************************** - * Name: esp_event_post - * - * Description: - * Active work queue and let the work to process the cached event - * - * Input Parameters: - * event_base - Event set name - * event_id - Event ID - * event_data - Event private data - * event_data_size - Event data size - * ticks - Waiting system ticks - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_event_post(esp_event_base_t event_base, - int32_t event_id, - void *event_data, - size_t event_data_size, - uint32_t ticks) -{ - size_t size; - int32_t id; - irqstate_t flags; - struct evt_adpt *evt_adpt; - - wlinfo("Event: base=%s id=%" PRId32 " data=%p data_size=%d " - "ticks=%" PRIu32 "\n", - event_base, event_id, event_data, event_data_size, ticks); - - id = esp_event_id_map(event_id); - if (id < 0) + else { - wlinfo("No process event %" PRId32 "\n", event_id); - return -1; + ret = ERROR; } - size = event_data_size + sizeof(struct evt_adpt); - evt_adpt = kmm_malloc(size); - if (!evt_adpt) + if (ret != OK) { - wlerr("Failed to alloc %d memory\n", size); - return -1; + wlerr("ERROR: %s\n", esp_err_to_name(err)); } - evt_adpt->id = id; - memcpy(evt_adpt->buf, event_data, event_data_size); - - flags = spin_lock_irqsave(&g_lock_event); - sq_addlast(&evt_adpt->entry, &g_wifi_evt_queue); - spin_unlock_irqrestore(&g_lock_event, flags); - - work_queue(LPWORK, &g_wifi_evt_work, esp_evt_work_cb, NULL, 0); - - return 0; -} - -/**************************************************************************** - * Name: esp_init_event_queue - * - * Description: - * Initialize the Wi-Fi event queue that holds pending events to be - * processed. This queue is used to store Wi-Fi events like scan - * completion, station connect/disconnect etc. before they are handled by - * the event work callback. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_init_event_queue(void) -{ - sq_init(&g_wifi_evt_queue); + return ret; } /**************************************************************************** @@ -1072,94 +788,3 @@ int esp_wifi_lock(bool lock) return ret; } - -/**************************************************************************** - * Name: esp_wifi_notify_subscribe - * - * Description: - * Enable event notification - * - * Input Parameters: - * pid - Task PID - * event - Signal event data pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_notify_subscribe(pid_t pid, struct sigevent *event) -{ - int id; - struct wifi_notify *notify; - int ret = -1; - - wlinfo("PID=%d event=%p\n", pid, event); - - esp_wifi_lock(true); - - if (event->sigev_notify == SIGEV_SIGNAL) - { - id = esp_event_id_map(event->sigev_signo); - if (id < 0) - { - wlerr("No process event %d\n", event->sigev_signo); - } - else - { - notify = &g_wifi_notify[id]; - - if (notify->assigned) - { - wlerr("sigev_signo %d has subscribed\n", - event->sigev_signo); - } - else - { - if (pid == 0) - { - pid = nxsched_gettid(); - wlinfo("Actual PID=%d\n", pid); - } - - notify->pid = pid; - notify->event = *event; - notify->assigned = true; - - ret = 0; - } - } - } - else if (event->sigev_notify == SIGEV_NONE) - { - id = esp_event_id_map(event->sigev_signo); - if (id < 0) - { - wlerr("No process event %d\n", event->sigev_signo); - } - else - { - notify = &g_wifi_notify[id]; - - if (!notify->assigned) - { - wlerr("sigev_signo %d has not subscribed\n", - event->sigev_signo); - } - else - { - notify->assigned = false; - - ret = 0; - } - } - } - else - { - wlerr("sigev_notify %d is invalid\n", event->sigev_signo); - } - - esp_wifi_lock(false); - - return ret; -} diff --git a/arch/xtensa/src/common/espressif/esp_wifi_utils.h b/arch/xtensa/src/common/espressif/esp_wifi_utils.h index 4e741a4724..0871ad1b02 100644 --- a/arch/xtensa/src/common/espressif/esp_wifi_utils.h +++ b/arch/xtensa/src/common/espressif/esp_wifi_utils.h @@ -29,13 +29,15 @@ #include #include +#include -#include "esp_private/wifi.h" +#include "esp_wifi_types_generic.h" #ifndef __ASSEMBLY__ -# include -#endif /* __ASSEMBLY__ */ +#include + +#undef EXTERN #if defined(__cplusplus) #define EXTERN extern "C" extern "C" @@ -44,10 +46,71 @@ extern "C" #define EXTERN extern #endif +#define MAC_LEN (6) + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nuttx_err_to_common_err + * + * Description: + * Transform from Nuttx OS error code to low level API error code. + * + * Input Parameters: + * ret - NuttX error code + * + * Returned Value: + * Wi-Fi adapter error code + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_WIFI +static inline int32_t nuttx_err_to_common_err(int ret) +{ + return ret >= 0; +} +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ +#ifdef CONFIG_ESPRESSIF_WIFI + +/**************************************************************************** + * Name: esp_freq_to_channel + * + * Description: + * Converts Wi-Fi frequency to channel. + * + * Input Parameters: + * freq - Wi-Fi frequency + * + * Returned Value: + * Wi-Fi channel + * + ****************************************************************************/ + +int esp_freq_to_channel(uint16_t freq); + +/**************************************************************************** + * Name: esp_evt_work_init + * + * Description: + * Initialize the event work queue + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void esp_evt_work_init(void); + /**************************************************************************** * Name: esp_wifi_start_scan * @@ -72,7 +135,7 @@ int esp_wifi_start_scan(struct iwreq *iwr); * Get scan result * * Input Parameters: - * req The argument of the ioctl cmd + * iwr - The argument of the ioctl cmd * * Returned Value: * OK on success (positive non-zero values are cmd-specific) @@ -86,76 +149,52 @@ int esp_wifi_get_scan_results(struct iwreq *iwr); * Name: esp_wifi_scan_event_parse * * Description: - * Parse scan information + * Parse scan information Wi-Fi AP scan results. * * Input Parameters: - * None + * None. * * Returned Value: - * None + * None. * ****************************************************************************/ void esp_wifi_scan_event_parse(void); +#endif + /**************************************************************************** - * Name: esp_evt_work_cb + * Name: esp_wifi_to_errno * * Description: - * Process the cached event + * Transform from ESP Wi-Fi error code to NuttX error code * * Input Parameters: - * arg - No mean + * err - ESP Wi-Fi error code * * Returned Value: - * None + * NuttX error code defined in errno.h * ****************************************************************************/ -void esp_evt_work_cb(void *arg); +int32_t esp_wifi_to_errno(int err); /**************************************************************************** - * Name: esp_event_post + * Name: esp_wifi_mode_translate * * Description: - * Active work queue and let the work to process the cached event + * Translate wireless mode constants to ESP Wi-Fi mode constants. * * Input Parameters: - * event_base - Event set name - * event_id - Event ID - * event_data - Event private data - * event_data_size - Event data size - * ticks - Waiting system ticks + * wireless_mode - Wireless mode from wireless.h (IW_MODE_*) * * Returned Value: - * 0 if success or -1 if fail + * ESP Wi-Fi mode (WIFI_MODE_*) on success + * -EINVAL on failure * ****************************************************************************/ -int esp_event_post(const char *event_base, - int32_t event_id, - void *event_data, - size_t event_data_size, - uint32_t ticks); - -/**************************************************************************** - * Name: esp_init_event_queue - * - * Description: - * Initialize the Wi-Fi event queue that holds pending events to be - * processed. This queue is used to store Wi-Fi events like scan - * completion, station connect/disconnect etc. before they are handled by - * the event work callback. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_init_event_queue(void); +wifi_mode_t esp_wifi_mode_translate(uint32_t wireless_mode); /**************************************************************************** * Name: esp_wifi_lock @@ -174,25 +213,42 @@ void esp_init_event_queue(void); int esp_wifi_lock(bool lock); /**************************************************************************** - * Name: esp_wifi_notify_subscribe + * Name: esp_event_post * * Description: - * Enable event notification + * Posts an event to the event loop system. The event is queued in a FIFO + * and processed asynchronously in the low-priority work queue. * * Input Parameters: - * pid - Task PID - * event - Signal event data pointer + * event_base - Identifier for the event category (e.g. WIFI_EVENT) + * event_id - Event ID within the event base category + * event_data - Pointer to event data structure + * event_data_size - Size of event data structure + * ticks - Number of ticks to wait (currently unused) * * Returned Value: - * 0 if success or -1 if fail + * 0 on success + * -1 on failure with following error conditions: + * - Invalid event ID + * - Memory allocation failure + * + * Assumptions/Limitations: + * - Event data is copied into a new buffer, so the original can be freed + * - Events are processed in FIFO order in the low priority work queue + * - The function is thread-safe and can be called from interrupt context * ****************************************************************************/ -int esp_wifi_notify_subscribe(pid_t pid, struct sigevent *event); +int esp_event_post(const char *event_base, + int32_t event_id, + void *event_data, + size_t event_data_size, + uint32_t ticks); -#undef EXTERN #ifdef __cplusplus } #endif +#undef EXTERN +#endif /* __ASSEMBLY__ */ #endif /* __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WIFI_UTILS_H */ diff --git a/arch/xtensa/src/common/espressif/esp_wireless.c b/arch/xtensa/src/common/espressif/esp_wireless.c index ac208709cb..4131893546 100644 --- a/arch/xtensa/src/common/espressif/esp_wireless.c +++ b/arch/xtensa/src/common/espressif/esp_wireless.c @@ -72,6 +72,7 @@ #include "phy_init_data.h" #include "esp_wireless.h" +#include "esp_wifi_utils.h" /**************************************************************************** * Pre-processor Definitions @@ -365,87 +366,6 @@ static int esp_swi_irq(int irq, void *context, void *arg) * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: esp_wifi_to_errno - * - * Description: - * Transform from ESP Wi-Fi error code to NuttX error code - * - * Input Parameters: - * err - ESP Wi-Fi error code - * - * Returned Value: - * NuttX error code defined in errno.h - * - ****************************************************************************/ - -#ifndef CONFIG_ARCH_CHIP_ESP32S2 -int32_t esp_wifi_to_errno(int err) -{ - int ret; - - if (err < ESP_ERR_WIFI_BASE) - { - /* Unmask component error bits */ - - ret = err & 0xfff; - - switch (ret) - { - case ESP_OK: - ret = OK; - break; - case ESP_ERR_NO_MEM: - ret = -ENOMEM; - break; - - case ESP_ERR_INVALID_ARG: - ret = -EINVAL; - break; - - case ESP_ERR_INVALID_STATE: - ret = -EIO; - break; - - case ESP_ERR_INVALID_SIZE: - ret = -EINVAL; - break; - - case ESP_ERR_NOT_FOUND: - ret = -ENOSYS; - break; - - case ESP_ERR_NOT_SUPPORTED: - ret = -ENOSYS; - break; - - case ESP_ERR_TIMEOUT: - ret = -ETIMEDOUT; - break; - - case ESP_ERR_INVALID_MAC: - ret = -EINVAL; - break; - - default: - ret = ERROR; - break; - } - } - else - { - ret = ERROR; - } - - if (ret != OK) - { - wlerr("ERROR: %s\n", esp_err_to_name(err)); - } - - return ret; -} -#endif - /**************************************************************************** * Functions needed by libphy.a ****************************************************************************/ @@ -1047,7 +967,7 @@ int esp_timer_create(const esp_timer_create_args_t *create_args, rt_timer_args.callback = create_args->callback; ret = rt_timer_create(&rt_timer_args, &rt_timer); - if (ret) + if (ret != 0) { wlerr("Failed to create rt_timer error=%d\n", ret); return ret; diff --git a/arch/xtensa/src/common/espressif/esp_wlan.c b/arch/xtensa/src/common/espressif/esp_wlan.c deleted file mode 100644 index b24e31501d..0000000000 --- a/arch/xtensa/src/common/espressif/esp_wlan.c +++ /dev/null @@ -1,1902 +0,0 @@ -/**************************************************************************** - * arch/xtensa/src/common/espressif/esp_wlan.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#ifdef CONFIG_ESPRESSIF_WIFI - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(CONFIG_NET_PKT) -# include -#endif - -#include "espressif/esp_wlan.h" -#include "espressif/esp_wifi_utils.h" -#include "espressif/esp_wireless.h" - -#ifdef CONFIG_ARCH_CHIP_ESP32 -# include "esp32_wifi_adapter.h" -# include "esp32_systemreset.h" -#elif CONFIG_ARCH_CHIP_ESP32S2 -# include "esp32s2_wifi_adapter.h" -# include "esp32s2_systemreset.h" -#elif CONFIG_ARCH_CHIP_ESP32S3 -# include "esp32s3_wifi_adapter.h" -# include "esp32s3_systemreset.h" -#endif - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#ifdef CONFIG_ARCH_CHIP_ESP32 -# define esp_register_shutdown_handler esp32_register_shutdown_handler -# define esp_unregister_shutdown_handler esp32_unregister_shutdown_handler -#elif CONFIG_ARCH_CHIP_ESP32S2 -# define esp_register_shutdown_handler esp32s2_register_shutdown_handler -# define esp_unregister_shutdown_handler esp32s2_unregister_shutdown_handler -#elif CONFIG_ARCH_CHIP_ESP32S3 -# define esp_register_shutdown_handler esp32s3_register_shutdown_handler -# define esp_unregister_shutdown_handler esp32s3_unregister_shutdown_handler -#endif - -/* TX timeout = 1 minute */ - -#define WLAN_TXTOUT (60 * CLK_TCK) - -/* Low-priority work queue processes RX/TX */ - -#define WLAN_WORK LPWORK - -/* Ethernet frame: - * Resource address : 6 bytes - * Destination address: 6 bytes - * Type : 2 bytes - * Payload : MAX 1500 - * Checksum : Ignore - * - * Total size : 1514 - */ - -#define WLAN_BUF_SIZE (CONFIG_NET_ETH_PKTSIZE) - -/* WLAN packet buffer number */ - -#define WLAN_PKTBUF_NUM (CONFIG_ESPRESSIF_WLAN_PKTBUF_NUM) - -/* Receive threshold which allows the receive function to trigger a scheduler - * to activate the application if possible. - */ - -#ifdef CONFIG_MM_IOB -# define IOBBUF_SIZE (CONFIG_IOB_NBUFFERS * CONFIG_IOB_BUFSIZE) -# if (IOBBUF_SIZE) > (WLAN_BUF_SIZE + 1) -# define WLAN_RX_THRESHOLD (IOBBUF_SIZE - WLAN_BUF_SIZE + 1) -# endif -#endif - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/* WLAN packet buffer */ - -struct wlan_pktbuf -{ - sq_entry_t entry; /* Queue entry */ - - /* Packet data buffer */ - - uint8_t buffer[WLAN_BUF_SIZE]; - uint16_t len; /* Packet data length */ -}; - -/* WLAN operations */ - -struct wlan_ops -{ - int (*start)(void); - int (*send)(void *pdata, size_t n); - int (*essid)(struct iwreq *iwr, bool set); - int (*bssid)(struct iwreq *iwr, bool set); - int (*passwd)(struct iwreq *iwr, bool set); - int (*mode)(struct iwreq *iwr, bool set); - int (*auth)(struct iwreq *iwr, bool set); - int (*freq)(struct iwreq *iwr, bool set); - int (*bitrate)(struct iwreq *iwr, bool set); - int (*txpower)(struct iwreq *iwr, bool set); - int (*channel)(struct iwreq *iwr, bool set); - int (*country)(struct iwreq *iwr, bool set); - int (*rssi)(struct iwreq *iwr, bool set); - int (*connect)(void); - int (*disconnect)(void); - int (*event)(pid_t pid, struct sigevent *event); - int (*stop)(void); -}; - -/* The wlan_priv_s encapsulates all state information for a single - * hardware interface - */ - -struct wlan_priv_s -{ - int ref; /* Reference count */ - - bool ifup; /* true:ifup false:ifdown */ - - struct wdog_s txtimeout; /* TX timeout timer */ - - struct work_s rxwork; /* Send packet work */ - struct work_s txwork; /* Receive packet work */ - struct work_s pollwork; /* Poll work */ - struct work_s toutwork; /* Send packet timeout work */ - - const struct wlan_ops *ops; /* WLAN operations */ - - /* This holds the information visible to the NuttX network */ - - struct net_driver_s dev; - - /* Packet buffer cache */ - -#ifdef CONFIG_ESPRESSIF_WIFI_WLAN_BUFFER_OPTIMIZATION - struct wlan_pktbuf *pktbuf; -#else - struct wlan_pktbuf pktbuf[WLAN_PKTBUF_NUM]; -#endif - - /* RX packet queue */ - - sq_queue_t rxb; - - /* TX ready packet queue */ - - sq_queue_t txb; - - /* Free packet buffer queue */ - - sq_queue_t freeb; - - /* Device specific lock */ - - spinlock_t lock; -}; - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/* If reconnect automatically */ - -volatile bool g_sta_reconnect; - -/* If Wi-Fi sta starts */ - -volatile bool g_sta_started; - -/* If Wi-Fi sta connected */ - -volatile bool g_sta_connected; - -/* Wi-Fi interface configuration */ - -wifi_config_t g_sta_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/* If Wi-Fi SoftAP starts */ - -volatile bool g_softap_started; - -/* Wi-Fi interface configuration */ - -wifi_config_t g_softap_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/* Reference count of register Wi-Fi handler */ - -static uint8_t g_callback_register_ref; - -static struct wlan_priv_s g_wlan_priv[ESPRESSIF_WLAN_DEVS]; - -#ifdef ESPRESSIF_WLAN_HAS_STA -static const struct wlan_ops g_sta_ops = -{ - .start = esp_wifi_sta_start, - .send = esp_wifi_sta_send_data, - .essid = esp_wifi_sta_essid, - .bssid = esp_wifi_sta_bssid, - .passwd = esp_wifi_sta_password, - .mode = esp_wifi_sta_mode, - .auth = esp_wifi_sta_auth, - .freq = esp_wifi_sta_freq, - .bitrate = esp_wifi_sta_bitrate, - .txpower = esp_wifi_sta_txpower, - .channel = esp_wifi_sta_channel, - .country = esp_wifi_sta_country, - .rssi = esp_wifi_sta_rssi, - .connect = esp_wifi_sta_connect, - .disconnect = esp_wifi_sta_disconnect, - .event = esp_wifi_notify_subscribe, - .stop = esp_wifi_sta_stop -}; -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP -static const struct wlan_ops g_softap_ops = -{ - .start = esp_wifi_softap_start, - .send = esp_wifi_softap_send_data, - .essid = esp_wifi_softap_essid, - .bssid = esp_wifi_softap_bssid, - .passwd = esp_wifi_softap_password, - .mode = esp_wifi_softap_mode, - .auth = esp_wifi_softap_auth, - .freq = esp_wifi_softap_freq, - .bitrate = esp_wifi_softap_bitrate, - .txpower = esp_wifi_softap_txpower, - .channel = esp_wifi_softap_channel, - .country = esp_wifi_softap_country, - .rssi = esp_wifi_softap_rssi, - .connect = esp_wifi_softap_connect, - .disconnect = esp_wifi_softap_disconnect, - .event = esp_wifi_notify_subscribe, - .stop = esp_wifi_softap_stop -}; -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/* Common TX/RX logic */ - -static struct wlan_pktbuf *wlan_recvframe(struct wlan_priv_s *priv); -static struct wlan_pktbuf *wlan_txframe(struct wlan_priv_s *priv); -static void wlan_transmit(struct wlan_priv_s *priv); -static void wlan_rxpoll(void *arg); -static int wlan_txpoll(struct net_driver_s *dev); -static void wlan_dopoll(struct wlan_priv_s *priv); - -/* Watchdog timer expirations */ - -static void wlan_txtimeout_work(void *arg); -static void wlan_txtimeout_expiry(wdparm_t arg); - -/* NuttX callback functions */ - -static int wlan_ifup(struct net_driver_s *dev); -static int wlan_ifdown(struct net_driver_s *dev); - -static void wlan_txavail_work(void *arg); -static int wlan_txavail(struct net_driver_s *dev); - -#if defined(CONFIG_NET_MCASTGROUP) || defined(CONFIG_NET_ICMPv6) -static int wlan_addmac(struct net_driver_s *dev, const uint8_t *mac); -#endif - -#ifdef CONFIG_NET_MCASTGROUP -static int wlan_rmmac(struct net_driver_s *dev, const uint8_t *mac); -#endif - -#ifdef CONFIG_NETDEV_IOCTL -static int wlan_ioctl(struct net_driver_s *dev, int cmd, - unsigned long arg); -#endif - -static inline void wlan_free_buffer(struct wlan_priv_s *priv, - uint8_t *buffer); - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/* Note: - * All TX done/RX done/Error trigger functions are not called from - * interrupts, this is much different from ethernet driver, including: - * * wlan_rx_done - * * wlan_tx_done - * - * These functions are called in a Wi-Fi private thread. So we just use - * mutex/semaphore instead of disable interrupt, if necessary. - */ - -/**************************************************************************** - * Function: wlan_init_buffer - * - * Description: - * Initialize the free buffer list - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline void wlan_init_buffer(struct wlan_priv_s *priv) -{ - irqstate_t flags; - -#ifdef CONFIG_ESPRESSIF_WIFI_WLAN_BUFFER_OPTIMIZATION - flags = spin_lock_irqsave(&priv->lock); - - priv->dev.d_buf = NULL; - priv->dev.d_len = 0; - - sq_init(&priv->rxb); - sq_init(&priv->txb); - - spin_unlock_irqrestore(&priv->lock, flags); -#else - int i; - flags = spin_lock_irqsave(&priv->lock); - - priv->dev.d_buf = NULL; - priv->dev.d_len = 0; - - sq_init(&priv->freeb); - sq_init(&priv->rxb); - sq_init(&priv->txb); - - for (i = 0; i < WLAN_PKTBUF_NUM; i++) - { - sq_addlast(&priv->pktbuf[i].entry, &priv->freeb); - } - - spin_unlock_irqrestore(&priv->lock, flags); -#endif -} - -/**************************************************************************** - * Function: wlan_deinit_buffer - * - * Description: - * De-initialize the buffer list - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None. - * - ****************************************************************************/ - -static inline void wlan_deinit_buffer(struct wlan_priv_s *priv) -{ -#ifdef CONFIG_ESPRESSIF_WIFI_WLAN_BUFFER_OPTIMIZATION - struct wlan_pktbuf *pktbuf; - while ((pktbuf = (struct wlan_pktbuf *)wlan_recvframe(priv)) != NULL) - { - wlan_free_buffer(priv, (void *)pktbuf->buffer); - } - - while ((pktbuf = (struct wlan_pktbuf *)wlan_txframe(priv)) != NULL) - { - wlan_free_buffer(priv, (void *)pktbuf->buffer); - } - - sq_init(&priv->rxb); - sq_init(&priv->txb); -#endif -} - -/**************************************************************************** - * Function: wlan_alloc_buffer - * - * Description: - * Allocate one buffer from the free buffer queue - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * Pointer to the allocated buffer on success; NULL on failure - * - ****************************************************************************/ - -static inline struct wlan_pktbuf *wlan_alloc_buffer(struct wlan_priv_s *priv) -{ - struct wlan_pktbuf *pktbuf = NULL; - -#ifdef CONFIG_ESPRESSIF_WIFI_WLAN_BUFFER_OPTIMIZATION - struct mallinfo info = kmm_mallinfo(); - if (info.fordblks < MINIMUM_HEAP_SIZE) - { - return NULL; - } - - pktbuf = kmm_malloc(sizeof(struct wlan_pktbuf) + WLAN_BUF_SIZE); -#else - sq_entry_t *entry; - irqstate_t flags = spin_lock_irqsave(&priv->lock); - - entry = sq_remfirst(&priv->freeb); - if (entry) - { - pktbuf = container_of(entry, struct wlan_pktbuf, entry); - } - - spin_unlock_irqrestore(&priv->lock, flags); - -#endif - return pktbuf; -} - -/**************************************************************************** - * Function: wlan_free_buffer - * - * Description: - * Insert a free Rx buffer into the free queue - * - * Input Parameters: - * priv - Reference to the driver state structure - * buffer - A pointer to the packet buffer to be freed - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline void wlan_free_buffer(struct wlan_priv_s *priv, - uint8_t *buffer) -{ - struct wlan_pktbuf *pktbuf; - -#ifdef CONFIG_ESPRESSIF_WIFI_WLAN_BUFFER_OPTIMIZATION - pktbuf = container_of(buffer, struct wlan_pktbuf, buffer); - kmm_free(pktbuf); -#else - irqstate_t flags = spin_lock_irqsave(&priv->lock); - - pktbuf = container_of(buffer, struct wlan_pktbuf, buffer); - sq_addlast(&pktbuf->entry, &priv->freeb); - - spin_unlock_irqrestore(&priv->lock, flags); -#endif -} - -/**************************************************************************** - * Function: wlan_cache_txpkt_tail - * - * Description: - * Cache packet from dev->d_buf into tail of TX ready queue. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline void wlan_cache_txpkt_tail(struct wlan_priv_s *priv) -{ - struct wlan_pktbuf *pktbuf; - irqstate_t flags; - struct net_driver_s *dev = &priv->dev; - - pktbuf = container_of(dev->d_buf, struct wlan_pktbuf, buffer); - pktbuf->len = dev->d_len; - - flags = spin_lock_irqsave(&priv->lock); - sq_addlast(&pktbuf->entry, &priv->txb); - spin_unlock_irqrestore(&priv->lock, flags); - - dev->d_buf = NULL; - dev->d_len = 0; -} - -/**************************************************************************** - * Function: wlan_add_txpkt_head - * - * Description: - * Add packet into head of TX ready queue. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline void wlan_add_txpkt_head(struct wlan_priv_s *priv, - struct wlan_pktbuf *pktbuf) -{ - irqstate_t flags; - - flags = spin_lock_irqsave(&priv->lock); - sq_addfirst(&pktbuf->entry, &priv->txb); - spin_unlock_irqrestore(&priv->lock, flags); -} - -/**************************************************************************** - * Function: wlan_recvframe - * - * Description: - * Try to receive RX packet from RX done packet queue. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * RX packet if success or NULl if no packet in queue. - * - ****************************************************************************/ - -static struct wlan_pktbuf *wlan_recvframe(struct wlan_priv_s *priv) -{ - irqstate_t flags; - sq_entry_t *entry; - struct wlan_pktbuf *pktbuf = NULL; - - flags = spin_lock_irqsave(&priv->lock); - - entry = sq_remfirst(&priv->rxb); - if (entry) - { - pktbuf = container_of(entry, struct wlan_pktbuf, entry); - } - - spin_unlock_irqrestore(&priv->lock, flags); - - return pktbuf; -} - -/**************************************************************************** - * Function: wlan_txframe - * - * Description: - * Try to receive TX buffer from TX ready buffer queue. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * TX packets buffer if success or NULL if no packet in queue. - * - ****************************************************************************/ - -static struct wlan_pktbuf *wlan_txframe(struct wlan_priv_s *priv) -{ - irqstate_t flags; - sq_entry_t *entry; - struct wlan_pktbuf *pktbuf = NULL; - - flags = spin_lock_irqsave(&priv->lock); - - entry = sq_remfirst(&priv->txb); - if (entry) - { - pktbuf = container_of(entry, struct wlan_pktbuf, entry); - } - - spin_unlock_irqrestore(&priv->lock, flags); - - return pktbuf; -} - -/**************************************************************************** - * Name: wlan_transmit - * - * Description: - * Try to send all TX packets in TX ready queue to Wi-Fi driver. If this - * sending fails, then breaks loop and returns. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_transmit(struct wlan_priv_s *priv) -{ - struct wlan_pktbuf *pktbuf; - int ret; - - while ((pktbuf = wlan_txframe(priv))) - { - ret = priv->ops->send(pktbuf->buffer, pktbuf->len); - if (ret == -ENOMEM) - { - wlan_add_txpkt_head(priv, pktbuf); - wd_start(&priv->txtimeout, WLAN_TXTOUT, - wlan_txtimeout_expiry, (uint32_t)priv); - break; - } - else - { - if (ret < 0) - { - nwarn("WARN: Failed to send pkt, ret: %d\n", ret); - } - - wlan_free_buffer(priv, pktbuf->buffer); - } - } -} - -/**************************************************************************** - * Name: wlan_tx_done - * - * Description: - * Wi-Fi TX done callback function. If this is called, it means sending - * next packet. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_tx_done(struct wlan_priv_s *priv) -{ - wd_cancel(&priv->txtimeout); - - wlan_txavail(&priv->dev); -} - -/**************************************************************************** - * Function: wlan_rx_done - * - * Description: - * Wi-Fi RX done callback function. If this is called, it means receiving - * packet. - * - * Input Parameters: - * priv - Reference to the driver state structure - * buffer - Wi-Fi received packet buffer - * len - Length of received packet - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * 0 on success or a negated errno on failure - * - ****************************************************************************/ - -static int wlan_rx_done(struct wlan_priv_s *priv, void *buffer, - uint16_t len, void *eb) -{ - struct wlan_pktbuf *pktbuf; - irqstate_t flags; - int ret = 0; - - if (!priv->ifup) - { - goto out; - } - - if (len > WLAN_BUF_SIZE) - { - nwarn("ERROR: Wlan receive %d larger than %d\n", - len, WLAN_BUF_SIZE); - ret = -EINVAL; - goto out; - } - - pktbuf = wlan_alloc_buffer(priv); - if (!pktbuf) - { - ret = -ENOBUFS; - goto out; - } - - memcpy(pktbuf->buffer, buffer, len); - pktbuf->len = len; - - if (eb) - { - esp_wifi_free_eb(eb); - } - - flags = spin_lock_irqsave(&priv->lock); - sq_addlast(&pktbuf->entry, &priv->rxb); - spin_unlock_irqrestore(&priv->lock, flags); - - if (work_available(&priv->rxwork)) - { - work_queue(WLAN_WORK, &priv->rxwork, wlan_rxpoll, priv, 0); - } - - return 0; - -out: - if (eb) - { - esp_wifi_free_eb(eb); - } - - return ret; -} - -/**************************************************************************** - * Function: wlan_rxpoll - * - * Description: - * Try to receive packets from RX done queue and pass packets into IP - * stack and send packets which is from IP stack if necessary. - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_rxpoll(void *arg) -{ - struct wlan_pktbuf *pktbuf; - struct eth_hdr_s *eth_hdr; - struct wlan_priv_s *priv = (struct wlan_priv_s *)arg; - struct net_driver_s *dev = &priv->dev; -#ifdef WLAN_RX_THRESHOLD - uint32_t rbytes = 0; -#endif - - /* Try to send all cached TX packets for TX ack and so on */ - - wlan_transmit(priv); - - /* Loop while while wlan_recvframe() successfully retrieves valid - * Ethernet frames. - */ - - net_lock(); - - while ((pktbuf = wlan_recvframe(priv)) != NULL) - { - dev->d_buf = pktbuf->buffer; - dev->d_len = pktbuf->len; - -#ifdef WLAN_RX_THRESHOLD - rbytes += pktbuf->len; -#endif - -#ifdef CONFIG_NET_PKT - - /* When packet sockets are enabled, - * feed the frame into the packet tap. - */ - - pkt_input(&priv->dev); -#endif - - /* Check if the packet is a valid size for the network - * buffer configuration (this should not happen) - */ - - if (dev->d_len > WLAN_BUF_SIZE) - { - nwarn("WARNING: DROPPED Too big: %d\n", dev->d_len); - - /* Free dropped packet buffer */ - - if (dev->d_buf) - { - wlan_free_buffer(priv, dev->d_buf); - dev->d_buf = NULL; - dev->d_len = 0; - } - - continue; - } - - eth_hdr = (struct eth_hdr_s *)dev->d_buf; - - /* We only accept IP packets of the configured type and ARP packets */ - -#ifdef CONFIG_NET_IPv4 - if (eth_hdr->type == HTONS(ETHTYPE_IP)) - { - ninfo("IPv4 frame\n"); - - /* Receive an IPv4 packet from the network device */ - - ipv4_input(&priv->dev); - - /* If the above function invocation resulted in data - * that should be sent out on the network, - * the field d_len will set to a value > 0. - */ - - if (priv->dev.d_len > 0) - { - /* And send the packet */ - - wlan_cache_txpkt_tail(priv); - } - } - else -#endif -#ifdef CONFIG_NET_IPv6 - if (eth_hdr->type == HTONS(ETHTYPE_IP6)) - { - ninfo("IPv6 frame\n"); - - /* Give the IPv6 packet to the network layer */ - - ipv6_input(&priv->dev); - - /* If the above function invocation resulted in data - * that should be sent out on the network, the field - * d_len will set to a value > 0. - */ - - if (priv->dev.d_len > 0) - { - /* And send the packet */ - - wlan_cache_txpkt_tail(priv); - } - } - else -#endif -#ifdef CONFIG_NET_ARP - if (eth_hdr->type == HTONS(ETHTYPE_ARP)) - { - ninfo("ARP frame\n"); - - /* Handle ARP packet */ - - arp_input(&priv->dev); - - /* If the above function invocation resulted in data - * that should be sent out on the network, the field - * d_len will set to a value > 0. - */ - - if (priv->dev.d_len > 0) - { - wlan_cache_txpkt_tail(priv); - } - } - else -#endif - { - ninfo("INFO: Dropped, Unknown type: %04x\n", eth_hdr->type); - } - - /* We are finished with the RX buffer. NOTE: If the buffer is - * reused for transmission, the dev->d_buf field will have been - * nullified. - */ - - if (dev->d_buf) - { - /* Free the receive packet buffer */ - - wlan_free_buffer(priv, dev->d_buf); - dev->d_buf = NULL; - dev->d_len = 0; - } - -#ifdef WLAN_RX_THRESHOLD - /* If received total bytes is larger than receive threshold, - * then do "unlock" to try to active applicantion to receive - * data from low-level buffer of IP stack. - */ - - if (rbytes >= WLAN_RX_THRESHOLD) - { - net_unlock(); - rbytes = 0; - net_lock(); - } -#endif - } - - /* Try to send all cached TX packets */ - - wlan_transmit(priv); - - net_unlock(); -} - -/**************************************************************************** - * Name: wlan_txpoll - * - * Description: - * The transmitter is available, check if the network has any outgoing - * packets ready to send. This is a callback from devif_poll(). - * devif_poll() may be called: - * - * 1. When the preceding TX packets send times out and the interface is - * reset - * 2. During normal TX polling - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * - * Returned Value: - * OK on success; a negated errno on failure - * - ****************************************************************************/ - -static int wlan_txpoll(struct net_driver_s *dev) -{ - struct wlan_pktbuf *pktbuf; - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - - DEBUGASSERT(dev->d_buf != NULL); - - wlan_cache_txpkt_tail(priv); - - pktbuf = wlan_alloc_buffer(priv); - if (!pktbuf) - { - return -ENOMEM; - } - - dev->d_buf = pktbuf->buffer; - dev->d_len = WLAN_BUF_SIZE; - - /* If zero is returned, the polling will continue until - * all connections have been examined. - */ - - return OK; -} - -/**************************************************************************** - * Function: wlan_dopoll - * - * Description: - * The function is called in order to perform an out-of-sequence TX poll. - * This is done: - * - * 1. When new TX data is available (wlan_txavail) - * 2. After a TX timeout to restart the sending process - * (wlan_txtimeout_expiry). - * - * Input Parameters: - * priv - Reference to the driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_dopoll(struct wlan_priv_s *priv) -{ - struct net_driver_s *dev = &priv->dev; - struct wlan_pktbuf *pktbuf; - uint8_t *txbuf; - int ret; - - pktbuf = wlan_alloc_buffer(priv); - if (!pktbuf) - { - return ; - } - - dev->d_buf = pktbuf->buffer; - dev->d_len = WLAN_BUF_SIZE; - - /* Try to let TCP/IP to send all packets to netcard driver */ - - do - { - txbuf = dev->d_buf; - ret = devif_poll(dev, wlan_txpoll); - } - while ((ret == 0) && - (dev->d_buf != txbuf)); - - if (dev->d_buf) - { - wlan_free_buffer(priv, dev->d_buf); - - dev->d_buf = NULL; - dev->d_len = 0; - } - - /* Try to send all cached TX packets */ - - wlan_transmit(priv); -} - -/**************************************************************************** - * Function: wlan_txtimeout_work - * - * Description: - * Perform TX timeout related work from the worker thread - * - * Input Parameters: - * arg - The argument passed when work_queue() as called. - * - * Returned Value: - * OK on success - * - ****************************************************************************/ - -static void wlan_txtimeout_work(void *arg) -{ - struct wlan_priv_s *priv = (struct wlan_priv_s *)arg; - - /* Try to send all cached TX packets */ - - wlan_transmit(priv); - - net_lock(); - - wlan_ifdown(&priv->dev); - wlan_ifup(&priv->dev); - - /* Then poll for new XMIT data */ - - wlan_dopoll(priv); - - net_unlock(); -} - -/**************************************************************************** - * Function: wlan_txtimeout_expiry - * - * Description: - * Our TX watchdog timed out. Called from the timer callback handler. - * The last TX never completed. Reset the hardware and start again. - * - * Input Parameters: - * argc - The number of available arguments - * arg - The first argument - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_txtimeout_expiry(wdparm_t arg) -{ - struct wlan_priv_s *priv = (struct wlan_priv_s *)arg; - - /* Schedule to perform the TX timeout processing on the worker thread. */ - - if (work_available(&priv->toutwork)) - { - work_queue(WLAN_WORK, &priv->toutwork, wlan_txtimeout_work, priv, 0); - } -} - -/**************************************************************************** - * Name: wlan_txavail_work - * - * Description: - * Perform an out-of-cycle poll on the worker thread. - * - * Input Parameters: - * arg - Reference to the NuttX driver state structure (cast to void*) - * - * Returned Value: - * None - * - * Assumptions: - * Called on the higher priority worker thread. - * - ****************************************************************************/ - -static void wlan_txavail_work(void *arg) -{ - struct wlan_priv_s *priv = (struct wlan_priv_s *)arg; - - /* Try to send all cached TX packets even if net is down */ - - wlan_transmit(priv); - - /* Lock the network and serialize driver operations if necessary. - * NOTE: Serialization is only required in the case where the driver work - * is performed on an LP worker thread and where more than one LP worker - * thread has been configured. - */ - - net_lock(); - - /* Ignore the notification if the interface is not yet up */ - - if (priv->ifup) - { - /* Poll the network for new XMIT data */ - - wlan_dopoll(priv); - } - - net_unlock(); -} - -/**************************************************************************** - * Name: wlan_ifup - * - * Description: - * NuttX Callback: Bring up the Ethernet interface when an IP address is - * provided - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int wlan_ifup(struct net_driver_s *dev) -{ - int ret; - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - -#ifdef CONFIG_NET_IPv4 - ninfo("Bringing up: %u.%u.%u.%u\n", - ip4_addr1(dev->d_ipaddr), ip4_addr2(dev->d_ipaddr), - ip4_addr3(dev->d_ipaddr), ip4_addr4(dev->d_ipaddr)); -#endif -#ifdef CONFIG_NET_IPv6 - ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", - dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2], - dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5], - dev->d_ipv6addr[6], dev->d_ipv6addr[7]); -#endif - - net_lock(); - - if (priv->ifup) - { - net_unlock(); - return OK; - } - - wlan_init_buffer(priv); - ret = priv->ops->start(); - if (ret < 0) - { -#ifdef CONFIG_ARCH_CHIP_ESP32 - wlan_deinit_buffer(priv); -#endif - net_unlock(); - nerr("ERROR: Failed to start Wi-Fi ret=%d\n", ret); - return ret; - } - - priv->ifup = true; - if (g_callback_register_ref == 0) - { - ret = esp_register_shutdown_handler(esp_wifi_stop_callback); - if (ret < 0) - { - nwarn("WARN: Failed to register handler ret=%d\n", ret); - } - } - - ++g_callback_register_ref; - -#ifdef CONFIG_ARCH_CHIP_ESP32 - /* We can make sure that the WLAN TX and RX are not doing, because - * the process is in "net_lock()" - */ - - wlan_deinit_buffer(priv); -#endif - net_unlock(); - - return OK; -} - -/**************************************************************************** - * Name: wlan_ifdown - * - * Description: - * NuttX Callback: Stop the interface. - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int wlan_ifdown(struct net_driver_s *dev) -{ - int ret; - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - - net_lock(); - - if (!priv->ifup) - { - net_unlock(); - return OK; - } - - /* Cancel the TX poll timer and TX timeout timers */ - - wd_cancel(&priv->txtimeout); - - /* Mark the device "down" */ - - priv->ifup = false; - - ret = priv->ops->stop(); - if (ret < 0) - { - nerr("ERROR: Failed to stop Wi-Fi ret=%d\n", ret); - } - - --g_callback_register_ref; - if (g_callback_register_ref == 0) - { - ret = esp_unregister_shutdown_handler(esp_wifi_stop_callback); - if (ret < 0) - { - nwarn("WARN: Failed to unregister handler ret=%d\n", ret); - } - } - - net_unlock(); - - return OK; -} - -/**************************************************************************** - * Name: wlan_txavail - * - * Description: - * Driver callback invoked when new TX data is available. This is a - * stimulus perform an out-of-cycle poll and, thereby, reduce the TX - * latency. - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * - * Returned Value: - * None - * - * Assumptions: - * Called in normal user mode - * - ****************************************************************************/ - -static int wlan_txavail(struct net_driver_s *dev) -{ - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - - if (work_available(&priv->txwork)) - { - /* Schedule to serialize the poll on the worker thread. */ - - work_queue(WLAN_WORK, &priv->txwork, wlan_txavail_work, priv, 0); - } - - return OK; -} - -/**************************************************************************** - * Name: wlan_addmac - * - * Description: - * NuttX Callback: Add the specified MAC address to the hardware multicast - * address filtering - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * mac - The MAC address to be added - * - * Returned Value: - * None - * - ****************************************************************************/ - -#if defined(CONFIG_NET_MCASTGROUP) || defined(CONFIG_NET_ICMPv6) -static int wlan_addmac(struct net_driver_s *dev, const uint8_t *mac) -{ - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - - /* Add the MAC address to the hardware multicast routing table */ - - return OK; -} -#endif - -/**************************************************************************** - * Name: wlan_rmmac - * - * Description: - * NuttX Callback: Remove the specified MAC address from the - * hardware multicast address filtering - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * mac - The MAC address to be removed - * - * Returned Value: - * None - * - ****************************************************************************/ - -#ifdef CONFIG_NET_MCASTGROUP -static int wlan_rmmac(struct net_driver_s *dev, const uint8_t *mac) -{ - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - - /* Add the MAC address to the hardware multicast routing table */ - - return OK; -} -#endif - -/**************************************************************************** - * Name: wlan_ioctl - * - * Description: - * Handle network IOCTL commands directed to this device. - * - * Input Parameters: - * dev - Reference to the NuttX driver state structure - * cmd - The IOCTL command - * arg - The argument for the IOCTL command - * - * Returned Value: - * OK on success; Negated errno on failure. - * - ****************************************************************************/ - -#ifdef CONFIG_NETDEV_IOCTL -static int wlan_ioctl(struct net_driver_s *dev, - int cmd, - unsigned long arg) -{ - int ret; - struct iwreq *iwr = (struct iwreq *)arg; - struct wlan_priv_s *priv = (struct wlan_priv_s *)dev->d_private; - const struct wlan_ops *ops = priv->ops; - - /* Decode and dispatch the driver-specific IOCTL command */ - - switch (cmd) - { -#ifdef CONFIG_NETDEV_PHY_IOCTL -#ifdef CONFIG_ARCH_PHY_INTERRUPT - case SIOCMIINOTIFY: /* Set up for PHY event notifications */ - { - struct mii_ioctl_notify_s *req = (struct mii_ioctl_notify_s *)arg; - ret = ops->event(req->pid, &req->event); - if (ret < 0) - { - nerr("ERROR: Failed to subscribe event\n"); - } - } - break; -#endif -#endif - - case SIOCSIWENCODEEXT: - ret = ops->passwd(iwr, true); - - break; - - case SIOCGIWENCODEEXT: - ret = ops->passwd(iwr, false); - break; - - case SIOCSIWESSID: - if ((iwr->u.essid.flags == IW_ESSID_ON) || - (iwr->u.essid.flags == IW_ESSID_DELAY_ON)) - { - ret = ops->essid(iwr, true); - if (ret < 0) - { - break; - } - - if (iwr->u.essid.flags == IW_ESSID_ON) - { - ret = ops->connect(); - if (ret < 0) - { - nerr("ERROR: Failed to connect\n"); - break; - } - } - } - else - { - ret = ops->disconnect(); - if (ret < 0) - { - nerr("ERROR: Failed to disconnect\n"); - break; - } - } - - break; - - case SIOCGIWESSID: /* Get ESSID */ - ret = ops->essid(iwr, false); - break; - - case SIOCSIWAP: /* Set access point MAC addresses */ - if (iwr->u.ap_addr.sa_data[0] != 0 && - iwr->u.ap_addr.sa_data[1] != 0 && - iwr->u.ap_addr.sa_data[2] != 0) - { - ret = ops->bssid(iwr, true); - if (ret < 0) - { - nerr("ERROR: Failed to set BSSID\n"); - break; - } - - ret = ops->connect(); - if (ret < 0) - { - nerr("ERROR: Failed to connect\n"); - break; - } - } - else - { - ret = ops->disconnect(); - if (ret < 0) - { - nerr("ERROR: Failed to disconnect\n"); - break; - } - } - - break; - - case SIOCGIWAP: /* Get access point MAC addresses */ - ret = ops->bssid(iwr, false); - break; - - case SIOCSIWSCAN: - ret = esp_wifi_start_scan(iwr); - break; - - case SIOCGIWSCAN: - ret = esp_wifi_get_scan_results(iwr); - break; - - case SIOCSIWCOUNTRY: /* Set country code */ - ret = ops->country(iwr, true); - break; - - case SIOCGIWSENS: /* Get sensitivity (dBm) */ - ret = ops->rssi(iwr, false); - break; - - case SIOCSIWMODE: /* Set operation mode */ - ret = ops->mode(iwr, true); - break; - - case SIOCGIWMODE: /* Get operation mode */ - ret = ops->mode(iwr, false); - break; - - case SIOCSIWAUTH: /* Set authentication mode params */ - ret = ops->auth(iwr, true); - break; - - case SIOCGIWAUTH: /* Get authentication mode params */ - ret = ops->auth(iwr, false); - break; - - case SIOCSIWFREQ: /* Set channel/frequency (MHz) */ - ret = ops->freq(iwr, true); - break; - - case SIOCGIWFREQ: /* Get channel/frequency (MHz) */ - ret = ops->freq(iwr, false); - break; - - case SIOCSIWRATE: /* Set default bit rate (Mbps) */ - wlwarn("WARNING: SIOCSIWRATE not implemented\n"); - ret = -ENOSYS; - break; - - case SIOCGIWRATE: /* Get default bit rate (Mbps) */ - ret = ops->bitrate(iwr, false); - break; - - case SIOCSIWTXPOW: /* Set transmit power (dBm) */ - ret = ops->txpower(iwr, true); - break; - - case SIOCGIWTXPOW: /* Get transmit power (dBm) */ - ret = ops->txpower(iwr, false); - break; - - case SIOCGIWRANGE: /* Get range of parameters */ - ret = ops->channel(iwr, false); - break; - - default: - nerr("ERROR: Unrecognized IOCTL command: %d\n", cmd); - ret = -ENOTTY; /* Special return value for this case */ - break; - } - - return ret; -} -#endif /* CONFIG_NETDEV_IOCTL */ - -/**************************************************************************** - * Name: esp_net_initialize - * - * Description: - * Initialize the ESP32-S2 driver - * - * Input Parameters: - * devno - The device number - * mac_addr - MAC address - * - * Returned Value: - * OK on success; Negated errno on failure. - * - ****************************************************************************/ - -static int esp_net_initialize(int devno, uint8_t *mac_addr, - const struct wlan_ops *ops) -{ - int ret; - struct wlan_priv_s *priv; - struct net_driver_s *netdev; - - priv = &g_wlan_priv[devno]; - if (priv->ref) - { - priv->ref++; - return OK; - } - - netdev = &priv->dev; - - /* Initialize the driver structure */ - - memset(priv, 0, sizeof(struct wlan_priv_s)); - - netdev->d_ifup = wlan_ifup; /* I/F down callback */ - netdev->d_ifdown = wlan_ifdown; /* I/F up (new IP address) callback */ - netdev->d_txavail = wlan_txavail; /* New TX data callback */ -#ifdef CONFIG_NET_MCASTGROUP - netdev->d_addmac = wlan_addmac; /* Add multicast MAC address */ - netdev->d_rmmac = wlan_rmmac; /* Remove multicast MAC address */ -#endif -#ifdef CONFIG_NETDEV_IOCTL - netdev->d_ioctl = wlan_ioctl; /* Handle network IOCTL commands */ -#endif - - /* Used to recover private state from dev */ - - netdev->d_private = (void *)priv; - - memcpy(netdev->d_mac.ether.ether_addr_octet, mac_addr, MAC_LEN); - - ret = netdev_register(netdev, NET_LL_IEEE80211); - if (ret < 0) - { - nerr("ERROR: Initialization of IEEE 802.11 block failed: %d\n", ret); - return ret; - } - - priv->ops = ops; - - priv->ref++; - - ninfo("INFO: Initialize Wi-Fi adapter No.%d success\n", devno); - - return OK; -} - -/**************************************************************************** - * Function: wlan_sta_rx_done - * - * Description: - * Wi-Fi station RX done callback function. If this is called, it means - * station receiving packet. - * - * Input Parameters: - * buffer - Wi-Fi received packet buffer - * len - Length of received packet - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * 0 on success or a negated errno on failure - * - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_STA -static int wlan_sta_rx_done(void *buffer, uint16_t len, void *eb) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESPRESSIF_WLAN_STA_DEVNO]; - - return wlan_rx_done(priv, buffer, len, eb); -} - -/**************************************************************************** - * Name: wlan_sta_tx_done - * - * Description: - * Wi-Fi station TX done callback function. If this is called, it means - * station sending next packet. - * - * Input Parameters: - * data - Pointer to the data transmitted. - * len - Length of the data transmitted. - * status - True if data was transmitted successfully or false if failed. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_sta_tx_done(uint8_t *data, uint16_t *len, bool status) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESPRESSIF_WLAN_STA_DEVNO]; - - wlan_tx_done(priv); -} -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Function: wlan_softap_rx_done - * - * Description: - * Wi-Fi softAP RX done callback function. If this is called, it means - * softAP receiving packet. - * - * Input Parameters: - * buffer - Wi-Fi received packet buffer - * len - Length of received packet - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * 0 on success or a negated errno on failure - * - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP -static int wlan_softap_rx_done(void *buffer, uint16_t len, void *eb) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESPRESSIF_WLAN_SOFTAP_DEVNO]; - - return wlan_rx_done(priv, buffer, len, eb); -} - -/**************************************************************************** - * Name: wlan_softap_tx_done - * - * Description: - * Wi-Fi softAP TX done callback function. If this is called, it means - * softAP sending next packet. - * - * Input Parameters: - * ifidx - The interface ID that the TX callback has been triggered from. - * data - Pointer to the data transmitted. - * len - Length of the data transmitted. - * status - True if data was transmitted successfully or false if failed. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void wlan_softap_tx_done(uint8_t *data, uint16_t *len, bool status) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESPRESSIF_WLAN_SOFTAP_DEVNO]; - - wlan_tx_done(priv); -} -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: esp_wlan_sta_set_linkstatus - * - * Description: - * Set Wi-Fi station link status - * - * Parameters: - * linkstatus - true Notifies the networking layer about an available - * carrier, false Notifies the networking layer about an - * disappeared carrier. - * - * Returned Value: - * OK on success; Negated errno on failure. - * - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_STA -int esp_wlan_sta_set_linkstatus(bool linkstatus) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESPRESSIF_WLAN_STA_DEVNO]; - - if (linkstatus) - { - netdev_carrier_on(&priv->dev); - } - else - { - netdev_carrier_off(&priv->dev); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wlan_sta_initialize - * - * Description: - * Initialize the ESP32-S2 WLAN station netcard driver - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success; Negated errno on failure. - * - ****************************************************************************/ - -int esp_wlan_sta_initialize(void) -{ - int ret; - uint8_t eth_mac[6]; - - ret = esp_wifi_adapter_init(); - if (ret < 0) - { - nerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret); - return ret; - } - - ret = esp_wifi_sta_read_mac(eth_mac); - if (ret < 0) - { - nerr("ERROR: Failed to read MAC address\n"); - return ret; - } - - ninfo("Wi-Fi station MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", - eth_mac[0], eth_mac[1], eth_mac[2], - eth_mac[3], eth_mac[4], eth_mac[5]); - - ret = esp_net_initialize(ESPRESSIF_WLAN_STA_DEVNO, - eth_mac, &g_sta_ops); - if (ret < 0) - { - nerr("ERROR: Failed to initialize net\n"); - return ret; - } - - ret = esp_wifi_sta_register_recv_cb(wlan_sta_rx_done); - if (ret < 0) - { - nerr("ERROR: Failed to register RX callback\n"); - return ret; - } - - esp_wifi_sta_register_txdone_cb(wlan_sta_tx_done); - - ninfo("INFO: Initialize Wi-Fi station success net\n"); - - return OK; -} -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_wlan_softap_initialize - * - * Description: - * Initialize the ESP32-S2 WLAN softAP netcard driver - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success; Negated errno on failure. - * - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP -int esp_wlan_softap_initialize(void) -{ - int ret; - uint8_t eth_mac[6]; - - ret = esp_wifi_adapter_init(); - if (ret < 0) - { - nerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret); - return ret; - } - - ret = esp_wifi_softap_read_mac(eth_mac); - if (ret < 0) - { - nerr("ERROR: Failed to read MAC address\n"); - return ret; - } - - ninfo("Wi-Fi softAP MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", - eth_mac[0], eth_mac[1], eth_mac[2], - eth_mac[3], eth_mac[4], eth_mac[5]); - - ret = esp_net_initialize(ESPRESSIF_WLAN_SOFTAP_DEVNO, eth_mac, - &g_softap_ops); - if (ret < 0) - { - nerr("ERROR: Failed to initialize net\n"); - return ret; - } - - ret = esp_wifi_softap_register_recv_cb(wlan_softap_rx_done); - if (ret < 0) - { - nerr("ERROR: Failed to register RX callback\n"); - return ret; - } - - esp_wifi_softap_register_txdone_cb(wlan_softap_tx_done); - - ninfo("INFO: Initialize Wi-Fi softAP net success\n"); - - return OK; -} -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - -#endif /* CONFIG_ESPRESSIF_WIFI */ diff --git a/arch/xtensa/src/common/espressif/esp_wlan_netdev.c b/arch/xtensa/src/common/espressif/esp_wlan_netdev.c new file mode 100644 index 0000000000..60e5c373df --- /dev/null +++ b/arch/xtensa/src/common/espressif/esp_wlan_netdev.c @@ -0,0 +1,1405 @@ +/**************************************************************************** + * arch/xtensa/src/common/espressif/esp_wlan_netdev.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +#include +#include + +#include "esp_attr.h" + +#include "esp_wifi_api.h" +#include "esp_wifi_utils.h" +#include "esp_wlan_netdev.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Only need 1 TX buf because packets are transmitted immediately and we can + * reuse the same buffer for the next packet. + * RX buf should match CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM. + */ + + #define RX_BUF_COUNT CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM + #define TX_BUF_COUNT 1 + + #define CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +static int esp_wlan_ifup(struct netdev_lowerhalf_s *dev); +static int esp_wlan_ifdown(struct netdev_lowerhalf_s *dev); +static int esp_wlan_transmit(struct netdev_lowerhalf_s *dev, netpkt_t *pkt); +static netpkt_t *esp_wlan_receive(struct netdev_lowerhalf_s *dev); +static void esp_wlan_reclaim(struct netdev_lowerhalf_s *dev); +static int esp_wlan_ioctl(struct netdev_lowerhalf_s *dev, int cmd, + unsigned long arg); + +static int esp_wlan_connect(struct netdev_lowerhalf_s *dev); +static int esp_wlan_disconnect(struct netdev_lowerhalf_s *dev); +static int esp_wlan_essid(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_bssid(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_passwd(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_mode(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_auth(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_freq(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_bitrate(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_txpower(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_country(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_sensitivity(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_scan(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set); +static int esp_wlan_range(struct netdev_lowerhalf_s *dev, struct iwreq *iwr); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Common Wi-Fi controller data */ + +struct esp_common_wifi_s +{ + bool driver_init; +}; + +/* Main driver control structure, contains the upper-half data structure + * and lower-half driver information. + */ + +struct esp_wlan_priv_s +{ + /* Upper-half interface */ + + struct netdev_lowerhalf_s dev; + + /* Lower-half data */ + + struct esp_common_wifi_s *common; + uint32_t mode; + + netpkt_queue_t netdev_rx_queue; + uint8_t flatbuf[CONFIG_NET_ETH_PKTSIZE]; +}; + +/* Netdev operations */ + +static const struct netdev_ops_s g_netdev_ops = +{ + .ifup = esp_wlan_ifup, + .ifdown = esp_wlan_ifdown, + .transmit = esp_wlan_transmit, + .receive = esp_wlan_receive, + .ioctl = esp_wlan_ioctl, + .reclaim = esp_wlan_reclaim, +}; + +/* Wireless operations */ + +static const struct wireless_ops_s g_wireless_ops = +{ + .connect = esp_wlan_connect, + .disconnect = esp_wlan_disconnect, + .essid = esp_wlan_essid, + .bssid = esp_wlan_bssid, + .passwd = esp_wlan_passwd, + .mode = esp_wlan_mode, + .auth = esp_wlan_auth, + .freq = esp_wlan_freq, + .bitrate = esp_wlan_bitrate, + .txpower = esp_wlan_txpower, + .country = esp_wlan_country, + .sensitivity = esp_wlan_sensitivity, + .scan = esp_wlan_scan, + .range = esp_wlan_range, +}; + +/* Common Wi-Fi controller data */ + +struct esp_common_wifi_s g_common_wifi = +{ + .driver_init = false, +}; + +/* Station interface control structure */ + +#ifdef ESP_WLAN_HAS_STA +static struct esp_wlan_priv_s g_wlan_sta = +{ + .dev = + { + .ops = &g_netdev_ops, + .iw_ops = &g_wireless_ops, + }, + .common = &g_common_wifi, + .mode = IW_MODE_INFRA, +}; +#endif /* ESP_WLAN_HAS_STA */ + +/* SoftAP interface control structure */ + +#ifdef ESP_WLAN_HAS_SOFTAP +static struct esp_wlan_priv_s g_wlan_softap = +{ + .dev = + { + .ops = &g_netdev_ops, + .iw_ops = &g_wireless_ops, + }, + .common = &g_common_wifi, + .mode = IW_MODE_MASTER, +}; +#endif /* ESP_WLAN_HAS_SOFTAP */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_wlan_ifup + * + * Description: + * Bring up the network device. + * + * Input Parameters: + * dev - The network device. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_ifup(struct netdev_lowerhalf_s *dev) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + struct net_driver_s *netdev = &priv->dev.netdev; + int ret = OK; + +#ifdef CONFIG_NET_IPv4 + wlinfo("Bringing up: %u.%u.%u.%u\n", + ip4_addr1(netdev->d_ipaddr), ip4_addr2(netdev->d_ipaddr), + ip4_addr3(netdev->d_ipaddr), ip4_addr4(netdev->d_ipaddr)); +#endif +#ifdef CONFIG_NET_IPv6 + wlinfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", + netdev->d_ipv6addr[0], netdev->d_ipv6addr[1], netdev->d_ipv6addr[2], + netdev->d_ipv6addr[3], netdev->d_ipv6addr[4], netdev->d_ipv6addr[5], + netdev->d_ipv6addr[6], netdev->d_ipv6addr[7]); +#endif + + /* Clear RX queue */ + + netpkt_free_queue(&priv->netdev_rx_queue); + + /* Start Wi-Fi interface */ + + ret = esp_wifi_api_start(priv->mode); + if (ret != 0) + { + wlerr("ERROR: Failed to start Wi-Fi station\n"); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Name: esp_wlan_ifdown + * + * Description: + * Bring down the network device. + * If using STA + SoftAP, both interfaces are stopped, followed by another + * bringup of the other interface. + * + * If using only STA, only the STA interface is stopped. + * If using only SoftAP, only the SoftAP interface is stopped. + * If using STA + SoftAP and request to stop SoftAP: + * Both are disabled and STA is restarted. + * If using STA + SoftAP and request to stop STA: + * Both are disabled and SoftAP is restarted. + * + * Input Parameters: + * dev - The network device. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_ifdown(struct netdev_lowerhalf_s *dev) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + int ret = OK; + + ret = esp_wifi_api_stop(priv->mode); + if (ret != OK) + { + wlerr("ERROR: Failed to stop Wi-Fi station\n"); + } + + return ret; +} + +/**************************************************************************** + * Name: esp_wlan_transmit + * + * Description: + * Transmit function required by the netdev ops. + * + * Input Parameters: + * dev - The network device. + * pkt - The packet to transmit. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_transmit(struct netdev_lowerhalf_s *dev, + netpkt_t *pkt) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + unsigned int len = netpkt_getdatalen(dev, pkt); + int ret = OK; + + /* Copy data from the packet to the flat buffer, then push it + * to the Wi-Fi driver. + */ + + netpkt_copyout(dev, priv->flatbuf, pkt, len, 0); +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + ret = esp_wifi_sta_send_data(priv->flatbuf, len); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + ret = esp_wifi_softap_send_data(priv->flatbuf, len); + } +#endif + + if (ret < 0) + { + return ret; + } + + /* Free the packet after sending */ + + netpkt_free(dev, pkt, NETPKT_TX); + + return ret; +} + +/**************************************************************************** + * Name: esp_wlan_receive + * + * Description: + * Receive function required by the netdev ops. + * + * Input Parameters: + * dev - The network device. + * + * Returned Value: + * A pointer to the received packet. + * + ****************************************************************************/ + +static netpkt_t *esp_wlan_receive(struct netdev_lowerhalf_s *dev) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + netpkt_t *pkt = netpkt_remove_queue(&priv->netdev_rx_queue); + return pkt; +} + +/**************************************************************************** + * Name: esp_wlan_ioctl + * + * Description: + * Ioctl function required by the netdev ops. + * + * Input Parameters: + * dev - The network device. + * cmd - The command. + * arg - The argument. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_NETDEV_IOCTL +static int esp_wlan_ioctl(struct netdev_lowerhalf_s *dev, int cmd, + unsigned long arg) +{ + /* TODO: Implement ioctl handler */ + + return -ENOSYS; +} +#endif + +/**************************************************************************** + * Name: esp_wlan_reclaim + * + * Description: + * Reclaim function required by the netdev ops. + * + * Input Parameters: + * dev - The network device. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static void esp_wlan_reclaim(struct netdev_lowerhalf_s *dev) +{ + /* TODO: Implement reclaim logic */ +} + +/* Skeleton implementations for wireless_ops_s functions */ + +/**************************************************************************** + * Name: esp_wlan_connect + * + * Description: + * Connect function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_connect(struct netdev_lowerhalf_s *dev) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + int ret; + useconds_t timeout = 1000 * 1000; /* 1 second */ + int timeout_count = CONNECT_TIMEOUT; + volatile bool connection_success = false; + volatile bool timeout_reached = false; + + if (priv->mode == IW_MODE_INFRA) + { + ret = esp_wifi_sta_connect(); + if (ret < 0) + { + wlerr("Failed to connect to Wi-Fi\n"); + return ret; + } + + /* Wait for connection success or timeout. + * + * Note: IFF_RUNNING is set when WIFI_ADPT_EVT_STA_CONNECT event is + * received. + */ + + while (timeout_count >= 0 && !connection_success) + { + if (priv->dev.netdev.d_flags & IFF_RUNNING) + { + connection_success = true; + break; + } + + nxsig_usleep(timeout); + timeout_count--; + } + + if (!connection_success) + { + wlerr("Connection timeout after %d seconds\n", CONNECT_TIMEOUT); + return -ETIMEDOUT; + } + + return ret; + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_connect(); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_disconnect + * + * Description: + * Disconnect function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_disconnect(struct netdev_lowerhalf_s *dev) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_disconnect(false); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_disconnect(); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_essid + * + * Description: + * ESSID function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the ESSID is to be set, false if the ESSID is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_essid(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_essid(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_essid(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_bssid + * + * Description: + * BSSID function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the BSSID is to be set, false if the BSSID is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_bssid(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_bssid(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_bssid(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_passwd + * + * Description: + * Password function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the password is to be set, false if the password is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_passwd(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_password(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_password(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_mode + * + * Description: + * Mode function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if mode is to be set, false if the mode is to be retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_mode(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_mode(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_mode(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_auth + * + * Description: + * Authentication function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if authentication is to be set, false if the authentication + * is to be retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_auth(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_auth(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_auth(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_freq + * + * Description: + * Frequency function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if frequency is to be set, false if the frequency is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_freq(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_freq(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_freq(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_bitrate + * + * Description: + * Bitrate function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the bitrate is to be set, false if the bitrate is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_bitrate(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_bitrate(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_bitrate(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_txpower + * + * Description: + * TX power function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the TX power is to be set, false if the TX power is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_txpower(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_txpower(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_txpower(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_country + * + * Description: + * Country function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the country is to be set, false if the country is to be + * retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_country(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_country(iwr, set); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (priv->mode == IW_MODE_MASTER) + { + return esp_wifi_softap_country(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_sensitivity + * + * Description: + * Sensitivity function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if the sensitivity is to be set, false if the sensitivity is + * to be retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_sensitivity(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ + struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev; + +#ifdef ESP_WLAN_HAS_STA + if (priv->mode == IW_MODE_INFRA) + { + return esp_wifi_sta_rssi(iwr, set); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_scan + * + * Description: + * Scan function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * set - True if scan is to be started, false if the scan results are to + * be retrieved. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_scan(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr, bool set) +{ +#ifdef ESP_WLAN_HAS_STA + if (set) + { + return esp_wifi_start_scan(iwr); + } + else + { + return esp_wifi_get_scan_results(iwr); + } +#endif + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wlan_range + * + * Description: + * Range get function required by the wireless ops. + * + * Input Parameters: + * dev - The network device. + * iwr - The wireless request. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_range(struct netdev_lowerhalf_s *dev, + struct iwreq *iwr) +{ + /* TODO: Implement range get */ + + return -ENOSYS; +} + +/**************************************************************************** + * Name: esp_wifi_tx_done_cb + * + * Description: + * TX done callback. Called when the TX packet is sent. Informs the upper + * layer that the TX packet has been sent. + * + * Input Parameters: + * ifidx - The interface index. + * data - The data of the TX packet. + * len - The length of the TX packet. + * txstatus - The status of the TX packet. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void IRAM_ATTR esp_wifi_tx_done_cb(uint8_t ifidx, + uint8_t *data, + uint16_t *len, + bool txstatus) +{ +#ifdef ESP_WLAN_HAS_STA + if (ifidx == ESP_IF_WIFI_STA) + { + netdev_lower_txdone(&g_wlan_sta.dev); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (ifidx == ESP_IF_WIFI_AP) + { + netdev_lower_txdone(&g_wlan_softap.dev); + } +#endif +} + +/**************************************************************************** + * Name: wlan_rx_done + * + * Description: + * RX done function. Allocates a new packet and copies the received + * data into a packet, which is then added to the receive queue. + * + * When done, informs the upper layer that the packet is ready to be + * processed. + * + * Input Parameters: + * priv - The private data structure for the specified mode. + * buffer - The buffer containing the received packet. + * len - The length of the received packet. + * eb - The event buffer. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int wlan_rx_done(struct esp_wlan_priv_s *priv, + void *buffer, uint16_t len, void *eb) +{ + int ret = OK; + netpkt_t *pkt = NULL; + + pkt = netpkt_alloc(&priv->dev, NETPKT_RX); + if (pkt == NULL) + { + ret = -ENOMEM; + goto out; + } + + ret = netpkt_copyin(&priv->dev, pkt, buffer, len, 0); + if (ret != OK) + { + wlerr("ERROR: Failed to copy packet\n"); + goto out; + } + + ret = netpkt_tryadd_queue(pkt, &priv->netdev_rx_queue); + if (ret != OK) + { + wlerr("ERROR: Failed to add packet to queue\n"); + goto out; + } + +out: + if (eb != NULL) + { + esp_wifi_api_free_rx_buffer(eb); + } + + if (ret != OK && pkt != NULL) + { + netpkt_free(&priv->dev, pkt, NETPKT_RX); + } + + if (ret == OK) + { + netdev_lower_rxready(&priv->dev); + } + + return ret; +} + +/**************************************************************************** + * Name: wlan_sta_rx_done + * + * Description: + * RX done callback for station mode. This call back is specified by + * IDF's API. + * + * Input Parameters: + * buffer - The buffer containing the received packet. + * len - The length of the received packet. + * eb - The event buffer. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA +static int wlan_sta_rx_done(void *buffer, uint16_t len, void *eb) +{ + struct esp_wlan_priv_s *priv = &g_wlan_sta; + + return wlan_rx_done(priv, buffer, len, eb); +} +#endif /* ESP_WLAN_HAS_STA */ + +/**************************************************************************** + * Name: wlan_softap_rx_done + * + * Description: + * RX done callback for SoftAP mode. This call back is specified by + * IDF's API. + * + * Input Parameters: + * buffer - The buffer containing the received packet. + * len - The length of the received packet. + * eb - The event buffer. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP +static int wlan_softap_rx_done(void *buffer, uint16_t len, void *eb) +{ + struct esp_wlan_priv_s *priv = &g_wlan_softap; + + return wlan_rx_done(priv, buffer, len, eb); +} +#endif /* ESP_WLAN_HAS_SOFTAP */ + +/**************************************************************************** + * Name: esp_wifi_initialize + * + * Description: + * Initialize the Wi-Fi controller. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wifi_initialize(void) +{ + int ret; + + wlinfo("INFO: Bring up Wi-Fi adapter\n"); + + if (!g_common_wifi.driver_init) + { + ret = esp_wifi_api_adapter_init(); + if (ret != OK) + { + wlerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret); + return ret; + } + + g_common_wifi.driver_init = true; + } + else + { + wlwarn("WARN: Wi-Fi adapter is already initialized\n"); + } + + return OK; +} + +/**************************************************************************** + * Name: esp_wlan_initialize + * + * Description: + * Initialize the Wi-Fi adapter for the specified mode. + * + * 1. Calls esp_wifi_initialize() to initialize the Wi-Fi controller. This + * should be done only once. + * + * 2. Reads the MAC address from the Wi-Fi controller. + * + * 3. Copies the MAC address to the netdev. + * + * 4. Sets the number of RX and TX buffers. + * + * 5. Registers the TX and RX callback. + * + * 6. Registers the network device at /dev/wlanN. + * + * Input Parameters: + * mode - The Wi-Fi mode to initialize (from wireless.h). + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +static int esp_wlan_initialize(uint32_t mode) +{ + int ret; + uint8_t mac[6]; + struct esp_wlan_priv_s *priv = NULL; + + ret = esp_wifi_initialize(); + if (ret != OK) + { + wlerr("ERROR: Failed to initialize Wi-Fi adapter\n"); + return ret; + } + + switch (mode) + { + case IW_MODE_INFRA: +#ifdef ESP_WLAN_HAS_STA + wlinfo("INFO: Initialize Wi-Fi station\n"); + priv = &g_wlan_sta; + ret = esp_wifi_sta_read_mac(mac); +#endif + break; + case IW_MODE_MASTER: +#ifdef ESP_WLAN_HAS_SOFTAP + wlinfo("INFO: Initialize Wi-Fi SoftAP\n"); + priv = &g_wlan_softap; + ret = esp_wifi_softap_read_mac(mac); +#endif + break; + default: + wlerr("ERROR: Invalid Wi-Fi mode\n"); + return -EINVAL; + } + + if (ret != OK) + { + wlerr("ERROR: Failed to read MAC address\n"); + return ret; + } + + wlinfo("MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n", + mac[0], mac[1], mac[2], + mac[3], mac[4], mac[5]); + + /* Copy the ESP MAC Address to the netdev */ + + memcpy(priv->dev.netdev.d_mac.ether.ether_addr_octet, mac, sizeof(mac)); + + /* Set the number of RX and TX buffers. */ + + priv->dev.quota[NETPKT_RX] = RX_BUF_COUNT; + priv->dev.quota[NETPKT_TX] = TX_BUF_COUNT; + + IOB_QINIT(&priv->netdev_rx_queue); + + /* Register RX done callback. Called when the RX packet is received. */ + +#ifdef ESP_WLAN_HAS_STA + if (mode == IW_MODE_INFRA) + { + ret = esp_wifi_api_sta_register_rx_callback(wlan_sta_rx_done); + } +#endif + +#ifdef ESP_WLAN_HAS_SOFTAP + if (mode == IW_MODE_MASTER) + { + ret = esp_wifi_api_softap_register_rx_callback(wlan_softap_rx_done); + } +#endif + + if (ret != OK) + { + wlerr("ERROR: Failed to register RX callback\n"); + return ret; + } + + /* Register TX done callback. Called when the TX packet is sent. */ + + ret = esp_wifi_api_register_tx_done_callback(esp_wifi_tx_done_cb); + if (ret != OK) + { + wlerr("ERROR: Failed to register TX done callback\n"); + return ret; + } + + /* Registers the network device at /dev/wlanN */ + + ret = netdev_lower_register((FAR struct netdev_lowerhalf_s *) priv, + NET_LL_IEEE80211); + if (ret != OK) + { + wlerr("ERROR: Failed to register IEEE 802.11 netdev: %d\n", ret); + return ret; + } + + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_wlan_sta_connect_success_hook + * + * Description: + * Notify the networking layer that connection has succeeded. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA +void esp_wlan_sta_connect_success_hook(void) +{ + struct esp_wlan_priv_s *priv = &g_wlan_sta; + netdev_lower_carrier_on(&priv->dev); +} +#endif + +/**************************************************************************** + * Name: esp_wlan_sta_disconnect_hook + * + * Description: + * Notify the networking layer that connection has been disconnected. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA +void esp_wlan_sta_disconnect_hook(void) +{ + struct esp_wlan_priv_s *priv = &g_wlan_sta; + netdev_lower_carrier_off(&priv->dev); +} +#endif + +/**************************************************************************** + * Name: esp_wlan_softap_connect_success_hook + * + * Description: + * Notify the networking layer that connection has succeeded. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP +void esp_wlan_softap_connect_success_hook(void) +{ + struct esp_wlan_priv_s *priv = &g_wlan_softap; + netdev_lower_carrier_on(&priv->dev); +} +#endif + +/**************************************************************************** + * Name: esp_wlan_softap_disconnect_hook + * + * Description: + * Notify the networking layer that connection has been disconnected. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP +void esp_wlan_softap_disconnect_hook(void) +{ + struct esp_wlan_priv_s *priv = &g_wlan_softap; + netdev_lower_carrier_off(&priv->dev); +} +#endif + +/**************************************************************************** + * Name: esp_wlan_sta_initialize + * + * Description: + * Initialize the Wi-Fi adapter for station mode. + * Called from board level initialization. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA +int esp_wlan_sta_initialize(void) +{ + return esp_wlan_initialize(IW_MODE_INFRA); +} +#endif + +/**************************************************************************** + * Name: esp_wlan_softap_initialize + * + * Description: + * Initialize the Wi-Fi adapter for SoftAP mode. + * Called from board level initialization. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP +int esp_wlan_softap_initialize(void) +{ + return esp_wlan_initialize(IW_MODE_MASTER); +} +#endif /* ESP_WLAN_HAS_SOFTAP */ diff --git a/arch/xtensa/src/common/espressif/esp_wlan.h b/arch/xtensa/src/common/espressif/esp_wlan_netdev.h similarity index 59% rename from arch/xtensa/src/common/espressif/esp_wlan.h rename to arch/xtensa/src/common/espressif/esp_wlan_netdev.h index 8e16183e96..31ef4b1931 100644 --- a/arch/xtensa/src/common/espressif/esp_wlan.h +++ b/arch/xtensa/src/common/espressif/esp_wlan_netdev.h @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/xtensa/src/common/espressif/esp_wlan.h + * arch/xtensa/src/common/espressif/esp_wlan_netdev.h * * SPDX-License-Identifier: Apache-2.0 * @@ -20,8 +20,8 @@ * ****************************************************************************/ -#ifndef __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WLAN_H -#define __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WLAN_H +#ifndef __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WLAN_NETDEV_H +#define __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WLAN_NETDEV_H /**************************************************************************** * Included Files @@ -29,16 +29,6 @@ #include -#ifdef CONFIG_ARCH_CHIP_ESP32 -# include "esp32_wifi_adapter.h" -#endif -#ifdef CONFIG_ARCH_CHIP_ESP32S2 -# include "esp32s2_wifi_adapter.h" -#endif -#ifdef CONFIG_ARCH_CHIP_ESP32S3 -# include "esp32s3_wifi_adapter.h" -#endif - #ifndef __ASSEMBLY__ #undef EXTERN @@ -50,119 +40,147 @@ extern "C" #define EXTERN extern #endif -#ifdef CONFIG_ESPRESSIF_WIFI - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ #if defined(CONFIG_ESPRESSIF_WIFI_STATION) -# define ESPRESSIF_WLAN_HAS_STA -# define ESPRESSIF_WLAN_STA_DEVNO 0 -# define ESPRESSIF_WLAN_DEVS 1 +# define ESP_WLAN_HAS_STA +# define ESP_WLAN_STA_DEVNO 0 +# define ESP_WLAN_DEVS 1 #elif defined(CONFIG_ESPRESSIF_WIFI_SOFTAP) -# define ESPRESSIF_WLAN_HAS_SOFTAP -# define ESPRESSIF_WLAN_SOFTAP_DEVNO 0 -# define ESPRESSIF_WLAN_DEVS 1 +# define ESP_WLAN_HAS_SOFTAP +# define ESP_WLAN_SOFTAP_DEVNO 0 +# define ESP_WLAN_DEVS 1 #elif defined(CONFIG_ESPRESSIF_WIFI_STATION_SOFTAP) -# define ESPRESSIF_WLAN_HAS_STA -# define ESPRESSIF_WLAN_HAS_SOFTAP -# define ESPRESSIF_WLAN_STA_DEVNO 0 -# define ESPRESSIF_WLAN_SOFTAP_DEVNO 1 -# define ESPRESSIF_WLAN_DEVS 2 +# define ESP_WLAN_HAS_STA +# define ESP_WLAN_HAS_SOFTAP +# define ESP_WLAN_HAS_APSTA +# define ESP_WLAN_STA_DEVNO 0 +# define ESP_WLAN_SOFTAP_DEVNO 1 +# define ESP_WLAN_DEVS 2 #endif /**************************************************************************** * Public Data ****************************************************************************/ -#ifdef ESPRESSIF_WLAN_HAS_STA - -/* If reconnect automatically */ - -extern volatile bool g_sta_reconnect; - -/* If Wi-Fi sta starts */ - -extern volatile bool g_sta_started; - -/* If Wi-Fi sta connected */ - -extern volatile bool g_sta_connected; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/* If Wi-Fi SoftAP starts */ - -extern volatile bool g_softap_started; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ /**************************************************************************** - * Name: esp_wlan_sta_set_linkstatus + * Name: esp_wlan_sta_connect_success_hook * * Description: - * Set Wi-Fi station link status + * Notify the networking layer that connection has succeeded. * - * Parameters: - * linkstatus - true Notifies the networking layer about an available - * carrier, false Notifies the networking layer about an - * disappeared carrier. + * Input Parameters: + * None. * * Returned Value: - * OK on success; Negated errno on failure. + * None. * ****************************************************************************/ -int esp_wlan_sta_set_linkstatus(bool linkstatus); +#ifdef ESP_WLAN_HAS_STA +void esp_wlan_sta_connect_success_hook(void); +#endif + +/**************************************************************************** + * Name: esp_wlan_sta_disconnect_hook + * + * Description: + * Notify the networking layer that connection has been disconnected. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_STA +void esp_wlan_sta_disconnect_hook(void); +#endif + +/**************************************************************************** + * Name: esp_wlan_softap_connect_success_hook + * + * Description: + * Notify the networking layer that connection has succeeded. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP +void esp_wlan_softap_connect_success_hook(void); +#endif + +/**************************************************************************** + * Name: esp_wlan_softap_disconnect_hook + * + * Description: + * Notify the networking layer that connection has been disconnected. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef ESP_WLAN_HAS_SOFTAP +void esp_wlan_softap_disconnect_hook(void); +#endif /**************************************************************************** * Name: esp_wlan_sta_initialize * * Description: - * Initialize the ESP32|S2|S3 WLAN station netcard driver + * Initialize the Wi-Fi adapter for station mode. * * Input Parameters: - * None + * None. * * Returned Value: * OK on success; Negated errno on failure. * ****************************************************************************/ +#ifdef ESP_WLAN_HAS_STA int esp_wlan_sta_initialize(void); +#endif /**************************************************************************** * Name: esp_wlan_softap_initialize * * Description: - * Initialize the ESP32|S2|S3 WLAN softAP netcard driver + * Initialize the Wi-Fi adapter for SoftAP mode. * * Input Parameters: - * None + * None. * * Returned Value: * OK on success; Negated errno on failure. * ****************************************************************************/ +#ifdef ESP_WLAN_HAS_SOFTAP int esp_wlan_softap_initialize(void); +#endif -#endif /* CONFIG_ESPRESSIF_WIFI */ #ifdef __cplusplus } #endif #undef EXTERN #endif /* __ASSEMBLY__ */ -#endif /* __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WLAN_H */ +#endif /* __ARCH_XTENSA_SRC_COMMON_ESPRESSIF_ESP_WLAN_NETDEV_H */ diff --git a/arch/xtensa/src/esp32/Make.defs b/arch/xtensa/src/esp32/Make.defs index fca6f11cb6..47d995bf51 100644 --- a/arch/xtensa/src/esp32/Make.defs +++ b/arch/xtensa/src/esp32/Make.defs @@ -223,7 +223,7 @@ endif ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty ifndef ESP_HAL_3RDPARTY_VERSION - ESP_HAL_3RDPARTY_VERSION = 96185c5348c747d2e15baef639d0b2a842ecd504 + ESP_HAL_3RDPARTY_VERSION = b6fa6c9098318007a61acc7c9f0f180443bb80c2 endif ifndef ESP_HAL_3RDPARTY_URL diff --git a/arch/xtensa/src/esp32/esp32_ble_adapter.c b/arch/xtensa/src/esp32/esp32_ble_adapter.c index a47191e803..fc71656498 100644 --- a/arch/xtensa/src/esp32/esp32_ble_adapter.c +++ b/arch/xtensa/src/esp32/esp32_ble_adapter.c @@ -56,6 +56,7 @@ #include "utils/memory_reserve.h" #include "esp32_rt_timer.h" #include "espressif/esp_wireless.h" +#include "espressif/esp_wifi_utils.h" #include "esp32_irq.h" #include "esp32_spicache.h" diff --git a/arch/xtensa/src/esp32/esp32_wifi_adapter.c b/arch/xtensa/src/esp32/esp32_wifi_adapter.c index 34716f9235..c919bd7cb7 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_adapter.c +++ b/arch/xtensa/src/esp32/esp32_wifi_adapter.c @@ -23,21 +23,13 @@ ****************************************************************************/ #include -#include -#include -#include -#include #include #include #include -#include -#include #include #include #include #include -#include -#include #include #include #include @@ -45,24 +37,23 @@ #include #include #include -#include #include #include -#include #include -#include #include #include "xtensa.h" #include "xtensa_attr.h" #include "hardware/esp32_dport.h" #include "hardware/esp32_emac.h" -#include "esp32_irq.h" #include "espressif/esp_wireless.h" #include "esp32_wifi_adapter.h" #include "esp32_rt_timer.h" + +#include "espressif/esp_wireless.h" #include "espressif/esp_wifi_utils.h" -#include "espressif/esp_wlan.h" + +#include "periph_ctrl.h" #ifdef CONFIG_PM # include "esp32_pm.h" @@ -75,57 +66,18 @@ # endif #endif -#include "esp_log.h" -#include "esp_mac.h" -#include "esp_private/phy.h" -#include "esp_private/wifi.h" -#include "esp_random.h" -#include "esp_timer.h" -#include "esp_wpa.h" -#include "periph_ctrl.h" -#include "rom/ets_sys.h" -#include "soc/soc_caps.h" - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE)) - -#define WIFI_CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT - -#define TIMER_INITIALIZED_VAL (0x5aa5a55a) - -#define ESP_WIFI_11B_MAX_BITRATE 11 -#define ESP_WIFI_11G_MAX_BITRATE 54 -#define ESP_WIFI_11N_MCS7_HT20_BITRATE 72 -#define ESP_WIFI_11N_MCS7_HT40_BITRATE 150 - -#ifndef CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL -#define CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL 3 -#endif - -#define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL - -#define ets_timer _ETSTIMER_ - -#define ESP_MAX_PRIORITIES (25) +#define TIMER_INITIALIZED_VAL 0x5aa5a55a +#define ets_timer _ETSTIMER_ +#define ESP_MAX_PRIORITIES 25 /**************************************************************************** * Private Types ****************************************************************************/ -/* Wi-Fi Station state */ - -enum wifi_sta_state -{ - WIFI_STA_STATE_NULL, - WIFI_STA_STATE_START, - WIFI_STA_STATE_CONNECT, - WIFI_STA_STATE_DISCONNECT, - WIFI_STA_STATE_STOP -}; - /* Wi-Fi interrupt adapter private data */ struct irq_adpt @@ -151,13 +103,6 @@ struct time_adpt suseconds_t usec; /* Micro second value */ }; -/* Wi-Fi NVS private data */ - -struct nvs_adpt -{ - char *index_name; -}; - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -327,26 +272,6 @@ static void * coex_schm_get_phase_by_idx_wrapper(int phase_idx); * Private Data ****************************************************************************/ -/* Wi-Fi adapter reference */ - -static int g_wifi_ref; - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/* Wi-Fi station TX done callback function */ - -static wifi_txdone_cb_t g_sta_txdone_cb; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/* Wi-Fi SoftAP TX done callback function */ - -static wifi_txdone_cb_t g_softap_txdone_cb; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Public Data ****************************************************************************/ @@ -512,114 +437,10 @@ wifi_osi_funcs_t g_wifi_osi_funcs = * Public Data ****************************************************************************/ -/* Wi-Fi interface configuration */ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** - * Private Functions and Public Functions only used by libraries + * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: osi_errno_trans - * - * Description: - * Transform from nuttx Os error code to Wi-Fi adapter error code - * - * Input Parameters: - * ret - NuttX error code - * - * Returned Value: - * Wi-Fi adapter error code - * - ****************************************************************************/ - -static inline int32_t osi_errno_trans(int ret) -{ - if (!ret) - { - return true; - } - else - { - return false; - } -} - -/**************************************************************************** - * Name: wifi_errno_trans - * - * Description: - * Transform from ESP Wi-Fi error code to NuttX error code - * - * Input Parameters: - * ret - ESP Wi-Fi error code - * - * Returned Value: - * NuttX error code - * - ****************************************************************************/ - -static int32_t wifi_errno_trans(int ret) -{ - int wifierr; - - /* Unmask component error bits */ - - wifierr = ret & 0xfff; - - if (wifierr == ESP_OK) - { - return OK; - } - else if (wifierr == ESP_ERR_NO_MEM) - { - return -ENOMEM; - } - else if (wifierr == ESP_ERR_INVALID_ARG) - { - return -EINVAL; - } - else if (wifierr == ESP_ERR_INVALID_STATE) - { - return -EIO; - } - else if (wifierr == ESP_ERR_INVALID_SIZE) - { - return -EINVAL; - } - else if (wifierr == ESP_ERR_NOT_FOUND) - { - return -ENOSYS; - } - else if (wifierr == ESP_ERR_NOT_SUPPORTED) - { - return -ENOSYS; - } - else if (wifierr == ESP_ERR_TIMEOUT) - { - return -ETIMEDOUT; - } - else if (wifierr == ESP_ERR_INVALID_MAC) - { - return -EINVAL; - } - else - { - return ERROR; - } -} - /**************************************************************************** * Name: esp_int_adpt_cb * @@ -1029,7 +850,7 @@ static int32_t esp_semphr_take(void *semphr, uint32_t block_time_tick) block_time_tick, ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1057,7 +878,7 @@ static int32_t esp_semphr_give(void *semphr) wlerr("Failed to post sem error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1254,7 +1075,7 @@ static int32_t esp_mutex_lock(void *mutex_data) wlerr("Failed to lock mutex error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1282,7 +1103,7 @@ static int32_t esp_mutex_unlock(void *mutex_data) wlerr("Failed to unlock mutex error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1420,7 +1241,7 @@ static int32_t esp_queue_send_generic(void *queue, void *item, } } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1727,6 +1548,7 @@ static int32_t esp_task_create_pinned_to_core(void *entry, { *((int *)task_handle) = pid; } + #ifdef CONFIG_SMP if (core_id < CONFIG_SMP_NCPUS) { @@ -1957,7 +1779,7 @@ static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, { *(int *)hptw = 0; - return osi_errno_trans(nxsem_trywait(semphr)); + return nuttx_err_to_common_err(nxsem_trywait(semphr)); } /**************************************************************************** @@ -3782,2464 +3604,10 @@ static unsigned long esp_random_ulong(void) return esp_random(); } -/**************************************************************************** - * Name: esp_wifi_tx_done_cb - * - * Description: - * Wi-Fi TX done callback function. - * - * Input Parameters: - * ifidx - The interface id that the tx callback has been triggered from - * data - Pointer to the data transmitted - * data_len - Length of the data transmitted - * txstatus - True: if the data was transmitted successfully, False: if - * data transmission failed. - * - * Returned Value: - * none - * - ****************************************************************************/ - -static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data, - uint16_t *len, bool txstatus) -{ -#ifdef ESPRESSIF_WLAN_HAS_STA - if (ifidx == ESP_IF_WIFI_STA) - { - if (g_sta_txdone_cb) - { - g_sta_txdone_cb(data, len, txstatus); - } - } - else -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (ifidx == ESP_IF_WIFI_AP) - { - if (g_softap_txdone_cb) - { - g_softap_txdone_cb(data, len, txstatus); - } - } - else -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - { - wlerr("ifidx=%d is error\n", ifidx); - } -} - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_auth_trans - * - * Description: - * Converts a ESP32 authenticate mode values to WEXT authenticate mode. - * - * Input Parameters: - * wifi_auth - ESP32 authenticate mode - * - * Returned Value: - * authenticate mode - * - ****************************************************************************/ - -static int esp_wifi_auth_trans(uint32_t wifi_auth) -{ - int auth_mode = IW_AUTH_WPA_VERSION_DISABLED; - - switch (wifi_auth) - { - case WIFI_AUTH_OPEN: - auth_mode = IW_AUTH_WPA_VERSION_DISABLED; - break; - - case WIFI_AUTH_WPA_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA; - break; - - case WIFI_AUTH_WPA2_PSK: - case WIFI_AUTH_WPA_WPA2_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA2; - break; - - case WIFI_AUTH_WPA3_PSK: - case WIFI_AUTH_WPA2_WPA3_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA3; - break; - - default: - wlerr("Failed to transfer wireless authmode: %" PRIu32 "", - wifi_auth); - break; - } - - return auth_mode; -} - -/**************************************************************************** - * Name: esp_wifi_cipher_trans - * - * Description: - * Converts a ESP32 cipher type values to WEXT cipher type values. - * - * Input Parameters: - * wifi_cipher - ESP32 cipher type - * - * Returned Value: - * cipher type - * - ****************************************************************************/ - -static int esp_wifi_cipher_trans(uint32_t wifi_cipher) -{ - int cipher_mode = IW_AUTH_CIPHER_NONE; - - switch (wifi_cipher) - { - case WIFI_CIPHER_TYPE_NONE: - cipher_mode = IW_AUTH_CIPHER_NONE; - break; - - case WIFI_CIPHER_TYPE_WEP40: - cipher_mode = IW_AUTH_CIPHER_WEP40; - break; - - case WIFI_CIPHER_TYPE_WEP104: - cipher_mode = IW_AUTH_CIPHER_WEP104; - break; - - case WIFI_CIPHER_TYPE_TKIP: - cipher_mode = IW_AUTH_CIPHER_TKIP; - break; - - case WIFI_CIPHER_TYPE_CCMP: - case WIFI_CIPHER_TYPE_TKIP_CCMP: - cipher_mode = IW_AUTH_CIPHER_CCMP; - break; - - case WIFI_CIPHER_TYPE_AES_CMAC128: - cipher_mode = IW_AUTH_CIPHER_AES_CMAC; - break; - - default: - wlerr("Failed to transfer wireless authmode: %" PRIu32 "", - wifi_cipher); - break; - } - - return cipher_mode; -} - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_freq_to_channel - * - * Description: - * Converts Wi-Fi frequency to channel. - * - * Input Parameters: - * freq - Wi-Fi frequency - * - * Returned Value: - * Wi-Fi channel - * - ****************************************************************************/ - -static int esp_freq_to_channel(uint16_t freq) -{ - int channel = 0; - if (freq >= 2412 && freq <= 2484) - { - if (freq == 2484) - { - channel = 14; - } - else - { - channel = freq - 2407; - if (channel % 5) - { - return 0; - } - - channel /= 5; - } - - return channel; - } - - if (freq >= 5005 && freq < 5900) - { - if (freq % 5) - { - return 0; - } - - channel = (freq - 5000) / 5; - return channel; - } - - if (freq >= 4905 && freq < 5000) - { - if (freq % 5) - { - return 0; - } - - channel = (freq - 4000) / 5; - return channel; - } - - return 0; -} - -/**************************************************************************** - * Functions needed by libpp.a - ****************************************************************************/ - -/**************************************************************************** - * Name: pp_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int pp_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Functions needed by libnet80211.a - ****************************************************************************/ - -/**************************************************************************** - * Name: net80211_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int net80211_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Functions needed by libcoexist.a - ****************************************************************************/ - -/**************************************************************************** - * Name: coexist_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int coexist_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - /**************************************************************************** * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: esp_event_send_internal - * - * Description: - * Post event message to queue - * - * Input Parameters: - * event_base - Event set name - * event_id - Event ID - * event_data - Event private data - * event_data_size - Event data size - * ticks_to_wait - Waiting system ticks - * - * Returned Value: - * Task maximum priority - * - ****************************************************************************/ - -int32_t esp_event_send_internal(esp_event_base_t event_base, - int32_t event_id, - void *event_data, - size_t event_data_size, - uint32_t ticks_to_wait) -{ - int32_t ret; - - ret = esp_event_post(event_base, event_id, event_data, event_data_size, - ticks_to_wait); - - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_free_eb - * - * Description: - * Free Wi-Fi receive callback input eb pointer - * - * Input Parameters: - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_free_eb(void *eb) -{ - esp_wifi_internal_free_rx_buffer(eb); -} - -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32 Wi-Fi adapter - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_adapter_init(void) -{ - int ret; - wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); - - esp_wifi_lock(true); - - if (g_wifi_ref) - { - wlinfo("Wi-Fi adapter is already initialized\n"); - g_wifi_ref++; - esp_wifi_lock(false); - return OK; - } - - esp_init_event_queue(); - - wifi_cfg.nvs_enable = 0; - -#ifdef CONFIG_ESPRESSIF_WIFI_TX_AMPDU - wifi_cfg.ampdu_tx_enable = 1; -#else - wifi_cfg.ampdu_tx_enable = 0; -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI_RX_AMPDU - wifi_cfg.ampdu_rx_enable = 1; -#else - wifi_cfg.ampdu_rx_enable = 0; -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI_STA_DISCONNECT_PM - wifi_cfg.sta_disconnected_pm = true; -#else - wifi_cfg.sta_disconnected_pm = false; -#endif - - wifi_cfg.rx_ba_win = CONFIG_ESPRESSIF_WIFI_RXBA_AMPDU_WZ; - wifi_cfg.static_rx_buf_num = CONFIG_ESPRESSIF_WIFI_STATIC_RXBUF_NUM; - wifi_cfg.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RXBUF_NUM; - wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TXBUF_NUM; - - ret = esp_wifi_init(&wifi_cfg); - if (ret) - { - wlerr("Failed to initialize Wi-Fi error=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout_init_wifi; - } - - ret = esp_wifi_set_tx_done_cb(esp_wifi_tx_done_cb); - if (ret) - { - wlerr("Failed to register TX done callback ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout_init_txdone; - } - - g_wifi_ref++; - - wlinfo("OK to initialize Wi-Fi adapter\n"); - - esp_wifi_lock(false); - - return OK; - -errout_init_txdone: - esp_wifi_deinit(); -errout_init_wifi: - esp_wifi_lock(false); - - return ret; -} - -/**************************************************************************** - * Station functions - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_sta_start - * - * Description: - * Start Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_start(void) -{ - int ret; - wifi_mode_t mode; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (g_softap_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - { - mode = WIFI_MODE_STA; - } - - ret = esp_wifi_set_mode(mode); - if (ret) - { - wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - g_sta_started = true; - - wlinfo("OK to start Wi-Fi station\n"); - -errout: - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_stop - * - * Description: - * Stop Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_stop(void) -{ - int ret; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - - g_sta_started = false; - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (g_softap_started) - { - ret = esp_wifi_set_mode(WIFI_MODE_AP); - if (ret) - { - wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi AP ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - } -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - wlinfo("OK to stop Wi-Fi station\n"); - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP -errout: -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_send_data - * - * Description: - * Use Wi-Fi station interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_send_data(void *pbuf, size_t len) -{ - int ret; - - ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_sta_register_recv_cb - * - * Description: - * Register Wi-Fi station receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)) -{ - int ret; - - ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)recv_cb); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_sta_register_txdone_cb - * - * Description: - * Register the station TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb) -{ - g_sta_txdone_cb = cb; -} - -/**************************************************************************** - * Name: esp_wifi_sta_read_mac - * - * Description: - * Read station interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_sta_read_mac(uint8_t *mac) -{ - return esp_read_mac(mac, ESP_MAC_WIFI_STA); -} - -/**************************************************************************** - * Name: esp_wifi_set_password - * - * Description: - * Set/Get Wi-Fi station password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_password(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_encode_ext *ext = iwr->u.encoding.pointer; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[PWD_MAX_LEN + 1]; -#endif - - DEBUGASSERT(ext != NULL); - - pdata = ext->key; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - len = ext->key_len; - if (len > PWD_MAX_LEN) - { - return -EINVAL; - } - - memset(wifi_cfg.sta.password, 0x0, PWD_MAX_LEN); - - if (ext->alg != IW_ENCODE_ALG_NONE) - { - memcpy(wifi_cfg.sta.password, pdata, len); - } - - wifi_cfg.sta.pmf_cfg.capable = true; - wifi_cfg.sta.listen_interval = DEFAULT_LISTEN_INTERVAL; - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - len = iwr->u.encoding.length - sizeof(*ext); - size = strnlen((char *)wifi_cfg.sta.password, PWD_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - ext->key_len = size; - memcpy(pdata, wifi_cfg.sta.password, ext->key_len); - } - - if (g_sta_connected) - { - wifi_ap_record_t ap_info; - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d", ret); - return wifi_errno_trans(ret); - } - - switch (ap_info.pairwise_cipher) - { - case WIFI_CIPHER_TYPE_NONE: - ext->alg = IW_ENCODE_ALG_NONE; - break; - - case WIFI_CIPHER_TYPE_WEP40: - case WIFI_CIPHER_TYPE_WEP104: - ext->alg = IW_ENCODE_ALG_WEP; - break; - - case WIFI_CIPHER_TYPE_TKIP: - ext->alg = IW_ENCODE_ALG_TKIP; - break; - - case WIFI_CIPHER_TYPE_CCMP: - case WIFI_CIPHER_TYPE_TKIP_CCMP: - ext->alg = IW_ENCODE_ALG_CCMP; - break; - - case WIFI_CIPHER_TYPE_AES_CMAC128: - ext->alg = IW_ENCODE_ALG_AES_CMAC; - break; - - default: - wlerr("Failed to transfer wireless authmode: %d", - ap_info.pairwise_cipher); - return -EIO; - } - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi station password=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_essid - * - * Description: - * Set/Get Wi-Fi station ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_essid(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_point *essid = &iwr->u.essid; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[SSID_MAX_LEN + 1]; -#endif - - DEBUGASSERT(essid != NULL); - - pdata = essid->pointer; - len = essid->length; - - if (set && len > SSID_MAX_LEN) - { - return -EINVAL; - } - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - memset(wifi_cfg.sta.ssid, 0x0, SSID_MAX_LEN); - memcpy(wifi_cfg.sta.ssid, pdata, len); - memset(wifi_cfg.sta.sae_h2e_identifier, 0x0, SAE_H2E_IDENTIFIER_LEN); - wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.sta.ssid, SSID_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.sta.ssid, len); - } - - if (g_sta_connected) - { - essid->flags = IW_ESSID_ON; - } - else - { - essid->flags = IW_ESSID_OFF; - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi station ssid=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_bssid - * - * Description: - * Set/Get Wi-Fi station BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bssid(struct iwreq *iwr, bool set) -{ - int ret; - wifi_config_t wifi_cfg; - struct sockaddr *sockaddr; - char *pdata; - - sockaddr = &iwr->u.ap_addr; - pdata = sockaddr->sa_data; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - wifi_cfg.sta.bssid_set = true; - memcpy(wifi_cfg.sta.bssid, pdata, MAC_LEN); - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - memcpy(pdata, wifi_cfg.sta.bssid, MAC_LEN); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_connect - * - * Description: - * Trigger Wi-Fi station connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_connect(void) -{ - int ret; - uint32_t ticks; - - esp_wifi_lock(true); - - if (g_sta_connected) - { - wlinfo("Wi-Fi has connected AP\n"); - esp_wifi_lock(false); - return OK; - } - - g_sta_reconnect = true; - - ret = esp_wifi_set_config(WIFI_IF_STA, &g_sta_wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_connect(); - if (ret) - { - wlerr("Failed to connect ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - esp_wifi_lock(false); - - ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT); - do - { - if (g_sta_connected) - { - break; - } - - esp_task_delay(1); - } - while (ticks--); - - if (!g_sta_connected) - { - g_sta_reconnect = false; - wlinfo("Failed to connect to AP\n"); - return -1; - } - - return OK; - -errout: - g_sta_reconnect = false; - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_disconnect - * - * Description: - * Trigger Wi-Fi station disconnection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_disconnect(void) -{ - int ret; - - esp_wifi_lock(true); - - g_sta_reconnect = false; - - ret = esp_wifi_disconnect(); - if (ret) - { - wlerr("Failed to disconnect ret=%d\n", ret); - ret = wifi_errno_trans(ret); - } - else - { - wlinfo("OK to disconnect Wi-Fi station\n"); - } - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_mode - * - * Description: - * Set/Get Wi-Fi Station mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_mode(struct iwreq *iwr, bool set) -{ - if (set == false) - { - iwr->u.mode = IW_MODE_INFRA; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_auth - * - * Description: - * Set/Get station authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_auth(struct iwreq *iwr, bool set) -{ - int ret; - int cmd; - wifi_config_t wifi_cfg; - wifi_ap_record_t ap_info; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - { - switch (iwr->u.param.value) - { - case IW_AUTH_WPA_VERSION_DISABLED: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_WPA_VERSION_WPA: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA2: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA3: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK; - break; - - default: - wlerr("Invalid wpa version %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_CIPHER_PAIRWISE: - case IW_AUTH_CIPHER_GROUP: - { - switch (iwr->u.param.value) - { - case IW_AUTH_CIPHER_NONE: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_CIPHER_WEP40: - case IW_AUTH_CIPHER_WEP104: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP; - break; - - case IW_AUTH_CIPHER_TKIP: - case IW_AUTH_CIPHER_CCMP: - case IW_AUTH_CIPHER_AES_CMAC: - break; - - default: - wlerr("Invalid cipher mode %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -EINVAL; - } - - size_t password_len = strlen((const char *)wifi_cfg.sta.password); - wifi_auth_mode_t authmode = wifi_cfg.sta.threshold.authmode; - - if (g_sta_connected && - ((password_len > 0 && authmode != WIFI_AUTH_OPEN) || - (password_len == 0 && authmode == WIFI_AUTH_OPEN))) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - if (g_sta_connected == false) - { - return -ENOTCONN; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - iwr->u.param.value = esp_wifi_auth_trans(ap_info.authmode); - break; - - case IW_AUTH_CIPHER_PAIRWISE: - iwr->u.param.value = - esp_wifi_cipher_trans(ap_info.pairwise_cipher); - break; - - case IW_AUTH_CIPHER_GROUP: - iwr->u.param.value = esp_wifi_cipher_trans(ap_info.group_cipher); - break; - - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -ENOSYS; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_freq - * - * Description: - * Set/Get station frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_freq(struct iwreq *iwr, bool set) -{ - int ret; - - if (set && (iwr->u.freq.flags == IW_FREQ_FIXED)) - { - wifi_config_t wifi_cfg = g_sta_wifi_cfg; - - wifi_cfg.sta.channel = esp_freq_to_channel(iwr->u.freq.m); - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - if (g_sta_connected) - { - wifi_ap_record_t ap_info; - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.freq.flags = IW_FREQ_FIXED; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2407 + 5 * ap_info.primary; - } - else - { - iwr->u.freq.flags = IW_FREQ_AUTO; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2412; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_bitrate - * - * Description: - * Get station default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set) -{ - int ret; - wifi_ap_record_t ap_info; - - if (set) - { - return -ENOSYS; - } - else - { - if (g_sta_connected == false) - { - iwr->u.bitrate.fixed = IW_FREQ_AUTO; - return OK; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.bitrate.fixed = IW_FREQ_FIXED; - if (ap_info.phy_11n) - { - if (ap_info.second) - { - iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT40_BITRATE; - } - else - { - iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT20_BITRATE; - } - } - else if (ap_info.phy_11g) - { - iwr->u.bitrate.value = ESP_WIFI_11G_MAX_BITRATE; - } - else if (ap_info.phy_11b) - { - iwr->u.bitrate.value = ESP_WIFI_11B_MAX_BITRATE; - } - else - { - return -EIO; - } - } - - return OK; -} - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_wifi_sta_get_txpower - * - * Description: - * Get station transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_txpower(struct iwreq *iwr, bool set) -{ - int ret; - int8_t power; - double power_dbm; - - if (set) - { - if (iwr->u.txpower.flags == IW_TXPOW_RELATIVE) - { - power = (int8_t)iwr->u.txpower.value; - } - else - { - if (iwr->u.txpower.flags == IW_TXPOW_MWATT) - { - power_dbm = ceil(10 * log10(iwr->u.txpower.value)); - } - else - { - power_dbm = iwr->u.txpower.value; - } - - power = (int8_t)(power_dbm * 4); - } - - /* The value set by this API will be mapped to the max_tx_power - * of the structure wifi_country_t variable. Param power unit is - * 0.25dBm, range is [8, 84] corresponding to 2dBm - 20dBm. - * Relationship between set value and actual value. - * As follows: {set value range, actual value} = - * {{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, - * {[34, 43],34}, {[44, 51],44}, {[52, 55],52}, - * {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, - * {[72, 79],72}, {[80, 84],80}}. - */ - - if (power < 8 || power > 84) - { - wlerr("Failed to set transmit power =%d\n", power); - return -ENOSYS; - } - - esp_wifi_set_max_tx_power(power); - return OK; - } - else - { - ret = esp_wifi_get_max_tx_power(&power); - if (ret) - { - wlerr("Failed to get transmit power ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.txpower.disabled = 0; - iwr->u.txpower.flags = IW_TXPOW_DBM; - iwr->u.txpower.value = power / 4; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_channel - * - * Description: - * Get station range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_channel(struct iwreq *iwr, bool set) -{ - int ret; - int k; - wifi_country_t country; - struct iw_range *range; - - if (set) - { - return -ENOSYS; - } - else - { - ret = esp_wifi_get_country(&country); - if (ret) - { - wlerr("Failed to get country info ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - range = (struct iw_range *)iwr->u.data.pointer; - range->num_frequency = country.nchan; - for (k = 1; k <= range->num_frequency; k++) - { - range->freq[k - 1].i = k; - range->freq[k - 1].e = 0; - range->freq[k - 1].m = 2407 + 5 * k; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_country(struct iwreq *iwr, bool set) -{ - int ret; - char *country_code; - wifi_country_t country; - - if (set) - { - memset(&country, 0x00, sizeof(wifi_country_t)); - country.schan = 1; - country.policy = 0; - - country_code = (char *)iwr->u.data.pointer; - if (strlen(country_code) != 2) - { - wlerr("Invalid input arguments\n"); - return -EINVAL; - } - - if (strncmp(country_code, "US", 3) == 0 || - strncmp(country_code, "CA", 3) == 0) - { - country.nchan = 11; - } - else if(strncmp(country_code, "JP", 3) == 0) - { - country.nchan = 14; - } - else - { - country.nchan = 13; - } - - memcpy(country.cc, country_code, 2); - ret = esp_wifi_set_country(&country); - if (ret) - { - wlerr("Failed to Configure country ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - else - { - return -ENOSYS; - } - - return OK; -} - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_sta_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_rssi(struct iwreq *iwr, bool set) -{ - int ret; - wifi_ap_record_t ap_info; - - if (set) - { - return -ENOSYS; - } - else - { - if (g_sta_connected == false) - { - iwr->u.sens.value = 128; - return OK; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.sens.value = -(ap_info.rssi); - } - - return OK; -} -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * SoftAP functions - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/**************************************************************************** - * Name: esp_wifi_softap_start - * - * Description: - * Start Wi-Fi SoftAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_start(void) -{ - int ret; - wifi_mode_t mode; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - -#ifdef ESPRESSIF_WLAN_HAS_STA - if (g_sta_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESPRESSIF_WLAN_HAS_STA */ - { - mode = WIFI_MODE_AP; - } - - ret = esp_wifi_set_mode(mode); - if (ret) - { - wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - g_softap_started = true; - - wlinfo("OK to start Wi-Fi SoftAP\n"); - -errout: - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_softap_stop - * - * Description: - * Stop Wi-Fi SoftAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_stop(void) -{ - int ret; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - - g_softap_started = false; - -#ifdef ESPRESSIF_WLAN_HAS_STA - if (g_sta_started) - { - ret = esp_wifi_set_mode(WIFI_MODE_STA); - if (ret) - { - wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi STA ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - } -#endif /* ESPRESSIF_WLAN_HAS_STA */ - - wlinfo("OK to stop Wi-Fi SoftAP\n"); - -#ifdef ESPRESSIF_WLAN_HAS_STA -errout: -#endif /* ESPRESSIF_WLAN_HAS_STA */ - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_softap_send_data - * - * Description: - * Use Wi-Fi SoftAP interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_send_data(void *pbuf, size_t len) -{ - int ret; - - ret = esp_wifi_internal_tx(WIFI_IF_AP, pbuf, len); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_softap_register_recv_cb - * - * Description: - * Register Wi-Fi SoftAP receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)) -{ - int ret; - - ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)recv_cb); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_softap_register_txdone_cb - * - * Description: - * Register the SoftAP TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb) -{ - g_softap_txdone_cb = cb; -} - -/**************************************************************************** - * Name: esp_wifi_softap_read_mac - * - * Description: - * Read SoftAP interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_softap_read_mac(uint8_t *mac) -{ - return esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); -} - -/**************************************************************************** - * Name: esp_wifi_softap_password - * - * Description: - * Set/Get Wi-Fi SoftAP password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_password(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_encode_ext *ext = iwr->u.encoding.pointer; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[PWD_MAX_LEN + 1]; -#endif - - DEBUGASSERT(ext != NULL); - - pdata = ext->key; - len = ext->key_len; - - if (set && len > PWD_MAX_LEN) - { - return -EINVAL; - } - - pdata = ext->key; - len = ext->key_len; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - /* Clear the password field and copy the user password to it */ - - memset(wifi_cfg.ap.password, 0x0, PWD_MAX_LEN); - - if (ext->alg != IW_ENCODE_ALG_NONE) - { - memcpy(wifi_cfg.ap.password, pdata, len); - } - - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.ap.password, PWD_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.ap.password, len); - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi SoftAP password=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_essid - * - * Description: - * Set/Get Wi-Fi SoftAP ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_essid(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_point *essid = &iwr->u.essid; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[SSID_MAX_LEN + 1]; -#endif - - DEBUGASSERT(essid != NULL); - - pdata = essid->pointer; - len = essid->length; - - if (set && len > SSID_MAX_LEN) - { - return -EINVAL; - } - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - memset(wifi_cfg.ap.ssid, 0x0, SSID_MAX_LEN); - memcpy(wifi_cfg.ap.ssid, pdata, len); - wifi_cfg.ap.ssid_len = len; - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.ap.ssid, SSID_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.ap.ssid, len); - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi SoftAP ssid=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_bssid - * - * Description: - * Set/Get Wi-Fi softAP BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bssid(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -/**************************************************************************** - * Name: esp_wifi_softap_connect - * - * Description: - * Trigger Wi-Fi SoftAP accept connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_connect(void) -{ - int ret; - - ret = esp_wifi_set_config(WIFI_IF_AP, &g_softap_wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_disconnect - * - * Description: - * Trigger Wi-Fi SoftAP drop connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_disconnect(void) -{ - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_mode - * - * Description: - * Set/Get Wi-Fi SoftAP mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_mode(struct iwreq *iwr, bool set) -{ - if (set == false) - { - iwr->u.mode = IW_MODE_MASTER; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_auth - * - * Description: - * Set/get authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_auth(struct iwreq *iwr, bool set) -{ - int ret; - int cmd; - wifi_config_t wifi_cfg; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - { - switch (iwr->u.param.value) - { - case IW_AUTH_WPA_VERSION_DISABLED: - wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_WPA_VERSION_WPA: - wifi_cfg.ap.authmode = WIFI_AUTH_WPA_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA2: - wifi_cfg.ap.authmode = WIFI_AUTH_WPA2_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA3: - wifi_cfg.ap.pmf_cfg.required = true; - wifi_cfg.ap.pmf_cfg.capable = false; - wifi_cfg.ap.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; - wifi_cfg.ap.authmode = WIFI_AUTH_WPA3_PSK; - break; - - default: - wlerr("Invalid wpa version %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_CIPHER_PAIRWISE: - case IW_AUTH_CIPHER_GROUP: - { - switch (iwr->u.param.value) - { - case IW_AUTH_CIPHER_NONE: - wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_CIPHER_WEP40: - case IW_AUTH_CIPHER_WEP104: - wifi_cfg.ap.authmode = WIFI_AUTH_WEP; - break; - - case IW_AUTH_CIPHER_TKIP: - case IW_AUTH_CIPHER_CCMP: - case IW_AUTH_CIPHER_AES_CMAC: - break; - - default: - wlerr("Invalid cipher mode %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -EINVAL; - } - - size_t password_len = strlen((const char *)wifi_cfg.ap.password); - - if (g_softap_started && - ((password_len > 0 && wifi_cfg.ap.authmode != WIFI_AUTH_OPEN) || - (password_len == 0 && wifi_cfg.ap.authmode == WIFI_AUTH_OPEN))) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - return -ENOSYS; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_freq - * - * Description: - * Set/Get SoftAP frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_freq(struct iwreq *iwr, bool set) -{ - int ret; - wifi_config_t wifi_cfg; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - int channel = esp_freq_to_channel(iwr->u.freq.m); - - wifi_cfg.ap.channel = channel; - - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - iwr->u.freq.flags = IW_FREQ_FIXED; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2407 + 5 * wifi_cfg.ap.channel; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_get_bitrate - * - * Description: - * Get SoftAP default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -/**************************************************************************** - * Name: esp_wifi_softap_txpower - * - * Description: - * Get SoftAP transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_txpower(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_txpower(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_channel - * - * Description: - * Get SoftAP range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_channel(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_channel(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_country(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_country(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_rssi(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Name: esp32_wifi_bt_coexist_init * @@ -6264,32 +3632,3 @@ int esp32_wifi_bt_coexist_init(void) return 0; } #endif /* CONFIG_ESPRESSIF_WIFI_BT_COEXIST */ - -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_stop_callback(void) -{ - wlinfo("INFO: Try to stop Wi-Fi\n"); - - int ret = esp_wifi_stop(); - if (ret) - { - wlerr("ERROR: Failed to stop Wi-Fi ret=%d\n", ret); - } - else - { - nxsig_sleep(1); - } -} diff --git a/arch/xtensa/src/esp32/esp32_wifi_adapter.h b/arch/xtensa/src/esp32/esp32_wifi_adapter.h index b358e8966a..b5b0645231 100644 --- a/arch/xtensa/src/esp32/esp32_wifi_adapter.h +++ b/arch/xtensa/src/esp32/esp32_wifi_adapter.h @@ -45,741 +45,10 @@ extern "C" * Pre-processor Definitions ****************************************************************************/ -#define SSID_MAX_LEN (32) -#define PWD_MAX_LEN (64) - -#define CONFIG_IDF_TARGET_ESP32 1 - -/* Define esp_err_t */ - -typedef int esp_err_t; - -enum coex_log_level_e -{ - COEX_LOG_NONE = 0, - COEX_LOG_ERROR, - COEX_LOG_WARN, - COEX_LOG_INFO, - COEX_LOG_DEBUG, - COEX_LOG_VERBOSE -}; - -/* Wi-Fi event callback function */ - -typedef void (*wifi_evt_cb_t)(void *p); - -/* Wi-Fi TX done callback function */ - -typedef void (*wifi_txdone_cb_t)(uint8_t *data, uint16_t *len, bool status); - -#define COEX_ADAPTER_FUNCS_TIME_BLOCKING 0xffffffff - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32 Wi-Fi adapter - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_adapter_init(void); - -/**************************************************************************** - * Name: esp_wifi_free_eb - * - * Description: - * Free Wi-Fi receive callback input eb pointer - * - * Input Parameters: - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_free_eb(void *eb); - -/**************************************************************************** - * Name: esp_wifi_sta_start - * - * Description: - * Start Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_start(void); - -/**************************************************************************** - * Name: esp_wifi_sta_stop - * - * Description: - * Stop Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_stop(void); - -/**************************************************************************** - * Name: esp_wifi_sta_send_data - * - * Description: - * Use Wi-Fi station interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_send_data(void *pbuf, size_t len); - -/**************************************************************************** - * Name: esp_wifi_sta_register_recv_cb - * - * Description: - * Regitser Wi-Fi station receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); - -/**************************************************************************** - * Name: esp_wifi_sta_register_txdone_cb - * - * Description: - * Register the station TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb); - -/**************************************************************************** - * Name: esp_wifi_sta_read_mac - * - * Description: - * Read station interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_sta_read_mac(uint8_t *mac); - -/**************************************************************************** - * Name: esp_wifi_set_password - * - * Description: - * Set/Get Wi-Fi station password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_password(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_essid - * - * Description: - * Set/Get Wi-Fi station ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_essid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_bssid - * - * Description: - * Set/Get Wi-Fi station BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bssid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_connect - * - * Description: - * Trigger Wi-Fi station connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_connect(void); - -/**************************************************************************** - * Name: esp_wifi_sta_disconnect - * - * Description: - * Trigger Wi-Fi station disconnection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_disconnect(void); - -/**************************************************************************** - * Name: esp_wifi_sta_mode - * - * Description: - * Set/Get Wi-Fi Station mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_mode(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_auth - * - * Description: - * Set/Get station authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_auth(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_freq - * - * Description: - * Get station frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_freq(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_bitrate - * - * Description: - * Get station default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_get_txpower - * - * Description: - * Get station transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_txpower(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_get_channel_range - * - * Description: - * Get station range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_channel(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_country(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_rssi(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_start - * - * Description: - * Start Wi-Fi softAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_start(void); - -/**************************************************************************** - * Name: esp_wifi_softap_stop - * - * Description: - * Stop Wi-Fi softAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_stop(void); - -/**************************************************************************** - * Name: esp_wifi_softap_send_data - * - * Description: - * Use Wi-Fi softAP interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_send_data(void *pbuf, size_t len); - -/**************************************************************************** - * Name: esp_wifi_softap_register_recv_cb - * - * Description: - * Regitser Wi-Fi softAP receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); - -/**************************************************************************** - * Name: esp_wifi_softap_register_txdone_cb - * - * Description: - * Register the softAP TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb); - -/**************************************************************************** - * Name: esp_wifi_softap_read_mac - * - * Description: - * Read softAP interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_softap_read_mac(uint8_t *mac); - -/**************************************************************************** - * Name: esp_wifi_softap_password - * - * Description: - * Set/Get Wi-Fi SoftAP password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_password(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_essid - * - * Description: - * Set/Get Wi-Fi SoftAP ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_essid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_bssid - * - * Description: - * Set/Get Wi-Fi softAP BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bssid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_connect - * - * Description: - * Trigger Wi-Fi softAP accept connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_connect(void); - -/**************************************************************************** - * Name: esp_wifi_softap_disconnect - * - * Description: - * Trigger Wi-Fi softAP drop connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_disconnect(void); - -/**************************************************************************** - * Name: esp_wifi_softap_mode - * - * Description: - * Set/Get Wi-Fi SoftAP mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_mode(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_auth - * - * Description: - * Set/Get authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_auth(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_freq - * - * Description: - * Set/Get SoftAP frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_freq(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_get_bitrate - * - * Description: - * Get SoftAP default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_txpower - * - * Description: - * Get SoftAP transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_txpower(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_channel - * - * Description: - * Get SoftAP range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_channel(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_country(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_rssi(struct iwreq *iwr, bool set); - /**************************************************************************** * Name: esp32_wifi_bt_coexist_init * @@ -797,25 +66,8 @@ int esp_wifi_softap_rssi(struct iwreq *iwr, bool set); #ifdef CONFIG_ESPRESSIF_WIFI_BT_COEXIST int esp32_wifi_bt_coexist_init(void); -void coex_dbg_set_log_level(int level); #endif -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_stop_callback(void); - #ifdef __cplusplus } #endif diff --git a/arch/xtensa/src/esp32/hal.mk b/arch/xtensa/src/esp32/hal.mk index 34b05b03cf..c10a1f3df5 100644 --- a/arch/xtensa/src/esp32/hal.mk +++ b/arch/xtensa/src/esp32/hal.mk @@ -106,6 +106,7 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efus CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_adc$(DELIM)adc_cali.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_adc$(DELIM)$(CHIP_SERIES)$(DELIM)adc_cali_line_fitting.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_app_format$(DELIM)esp_app_desc.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_common$(DELIM)src$(DELIM)esp_err_to_name.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)adc_share_hw_ctrl.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)clk_ctrl_os.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)cpu.c diff --git a/arch/xtensa/src/esp32s2/Make.defs b/arch/xtensa/src/esp32s2/Make.defs index e2b3ffe89b..fb83d2c384 100644 --- a/arch/xtensa/src/esp32s2/Make.defs +++ b/arch/xtensa/src/esp32s2/Make.defs @@ -133,7 +133,7 @@ endif ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty ifndef ESP_HAL_3RDPARTY_VERSION - ESP_HAL_3RDPARTY_VERSION = 96185c5348c747d2e15baef639d0b2a842ecd504 + ESP_HAL_3RDPARTY_VERSION = b6fa6c9098318007a61acc7c9f0f180443bb80c2 endif ifndef ESP_HAL_3RDPARTY_URL diff --git a/arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.c b/arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.c index 42abfccb50..78a1822f62 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.c +++ b/arch/xtensa/src/esp32s2/esp32s2_wifi_adapter.c @@ -23,21 +23,12 @@ ****************************************************************************/ #include -#include -#include -#include -#include #include #include #include -#include -#include #include #include -#include #include -#include -#include #include #include #include @@ -45,80 +36,34 @@ #include #include #include -#include #include #include -#include #include -#include #include #include "xtensa.h" #include "esp_attr.h" #include "hardware/esp32s2_system.h" #include "hardware/esp32s2_rtccntl.h" -#include "hardware/esp32s2_syscon.h" -#include "hardware/esp32s2_soc.h" -#include "esp32s2_irq.h" -#include "espressif/esp_wireless.h" -#include "esp32s2_wifi_adapter.h" -#include "esp32s2_rt_timer.h" -#include "espressif/esp_wifi_utils.h" -#include "espressif/esp_wlan.h" -#include "esp_log.h" -#include "esp_mac.h" -#include "esp_private/phy.h" -#include "esp_private/wifi.h" -#include "esp_random.h" -#include "esp_timer.h" -#include "esp_wpa.h" +#include "espressif/esp_wireless.h" +#include "espressif/esp_wifi_utils.h" + #include "periph_ctrl.h" -#include "rom/ets_sys.h" -#include "soc/soc_caps.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE)) - -#define WIFI_CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT - #define TIMER_INITIALIZED_VAL (0x5aa5a55a) - -#define ESP_WIFI_11B_MAX_BITRATE 11 -#define ESP_WIFI_11G_MAX_BITRATE 54 -#define ESP_WIFI_11N_MCS7_HT20_BITRATE 72 -#define ESP_WIFI_11N_MCS7_HT40_BITRATE 150 - -#ifndef CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL -#define CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL 3 -#endif - -#define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL - -#define RTC_CLK_CAL_FRACT 19 /* Number of fractional bits in values returned by rtc_clk_cal */ - -#define ets_timer _ETSTIMER_ - -#define ESP_MAX_PRIORITIES (25) +#define RTC_CLK_CAL_FRACT 19 /* Number of fractional bits in values returned by rtc_clk_cal */ +#define ets_timer _ETSTIMER_ +#define ESP_MAX_PRIORITIES 25 /**************************************************************************** * Private Types ****************************************************************************/ -/* Wi-Fi Station state */ - -enum wifi_sta_state -{ - WIFI_STA_STATE_NULL, - WIFI_STA_STATE_START, - WIFI_STA_STATE_CONNECT, - WIFI_STA_STATE_DISCONNECT, - WIFI_STA_STATE_STOP -}; - /* Wi-Fi interrupt adapter private data */ struct irq_adpt @@ -144,13 +89,6 @@ struct time_adpt suseconds_t usec; /* Micro second value */ }; -/* Wi-Fi NVS private data */ - -struct nvs_adpt -{ - char *index_name; -}; - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -309,26 +247,6 @@ static void * coex_schm_get_phase_by_idx_wrapper(int phase_idx); * Private Data ****************************************************************************/ -/* Wi-Fi adapter reference */ - -static int g_wifi_ref; - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/* Wi-Fi station TX done callback function */ - -static wifi_txdone_cb_t g_sta_txdone_cb; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/* Wi-Fi SoftAP TX done callback function */ - -static wifi_txdone_cb_t g_softap_txdone_cb; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Public Data ****************************************************************************/ @@ -464,114 +382,10 @@ wifi_osi_funcs_t g_wifi_osi_funcs = * Public Data ****************************************************************************/ -/* Wi-Fi interface configuration */ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** - * Private Functions and Public Functions only used by libraries + * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: osi_errno_trans - * - * Description: - * Transform from nuttx Os error code to Wi-Fi adapter error code - * - * Input Parameters: - * ret - NuttX error code - * - * Returned Value: - * Wi-Fi adapter error code - * - ****************************************************************************/ - -static inline int32_t osi_errno_trans(int ret) -{ - if (!ret) - { - return true; - } - else - { - return false; - } -} - -/**************************************************************************** - * Name: wifi_errno_trans - * - * Description: - * Transform from ESP Wi-Fi error code to NuttX error code - * - * Input Parameters: - * ret - ESP Wi-Fi error code - * - * Returned Value: - * NuttX error code - * - ****************************************************************************/ - -static int32_t wifi_errno_trans(int ret) -{ - int wifierr; - - /* Unmask component error bits */ - - wifierr = ret & 0xfff; - - if (wifierr == ESP_OK) - { - return OK; - } - else if (wifierr == ESP_ERR_NO_MEM) - { - return -ENOMEM; - } - else if (wifierr == ESP_ERR_INVALID_ARG) - { - return -EINVAL; - } - else if (wifierr == ESP_ERR_INVALID_STATE) - { - return -EIO; - } - else if (wifierr == ESP_ERR_INVALID_SIZE) - { - return -EINVAL; - } - else if (wifierr == ESP_ERR_NOT_FOUND) - { - return -ENOSYS; - } - else if (wifierr == ESP_ERR_NOT_SUPPORTED) - { - return -ENOSYS; - } - else if (wifierr == ESP_ERR_TIMEOUT) - { - return -ETIMEDOUT; - } - else if (wifierr == ESP_ERR_INVALID_MAC) - { - return -EINVAL; - } - else - { - return ERROR; - } -} - /**************************************************************************** * Name: esp_int_adpt_cb * @@ -989,7 +803,7 @@ static int32_t esp_semphr_take(void *semphr, uint32_t block_time_tick) block_time_tick, ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1017,7 +831,7 @@ static int32_t esp_semphr_give(void *semphr) wlerr("Failed to post sem error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1214,7 +1028,7 @@ static int32_t esp_mutex_lock(void *mutex_data) wlerr("Failed to lock mutex error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1242,7 +1056,7 @@ static int32_t esp_mutex_unlock(void *mutex_data) wlerr("Failed to unlock mutex error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1380,7 +1194,7 @@ static int32_t esp_queue_send_generic(void *queue, void *item, } } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1668,10 +1482,6 @@ static int32_t esp_task_create_pinned_to_core(void *entry, uint32_t core_id) { int pid; -#ifdef CONFIG_SMP - int ret; - cpu_set_t cpuset; -#endif uint32_t target_prio = prio; if (target_prio < ESP_MAX_PRIORITIES) @@ -1687,19 +1497,6 @@ static int32_t esp_task_create_pinned_to_core(void *entry, { *((int *)task_handle) = pid; } -#ifdef CONFIG_SMP - if (core_id < CONFIG_SMP_NCPUS) - { - CPU_ZERO(&cpuset); - CPU_SET(core_id, &cpuset); - ret = nxsched_set_affinity(pid, sizeof(cpuset), &cpuset); - if (ret) - { - wlerr("Failed to set affinity error=%d\n", ret); - return false; - } - } -#endif } else { @@ -3597,2490 +3394,3 @@ static unsigned long esp_random_ulong(void) { return esp_random(); } - -/**************************************************************************** - * Name: esp_wifi_tx_done_cb - * - * Description: - * Wi-Fi TX done callback function. - * - * Input Parameters: - * ifidx - The interface id that the tx callback has been triggered from - * data - Pointer to the data transmitted - * data_len - Length of the data transmitted - * txstatus - True: if the data was transmitted successfully, False: if - * data transmission failed. - * - * Returned Value: - * none - * - ****************************************************************************/ - -static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data, - uint16_t *len, bool txstatus) -{ -#ifdef ESPRESSIF_WLAN_HAS_STA - if (ifidx == ESP_IF_WIFI_STA) - { - if (g_sta_txdone_cb) - { - g_sta_txdone_cb(data, len, txstatus); - } - } - else -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (ifidx == ESP_IF_WIFI_AP) - { - if (g_softap_txdone_cb) - { - g_softap_txdone_cb(data, len, txstatus); - } - } - else -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - { - wlerr("ifidx=%d is error\n", ifidx); - } -} - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_auth_trans - * - * Description: - * Converts a ESP32-S2 authenticate mode values to WEXT authenticate mode. - * - * Input Parameters: - * wifi_auth - ESP32-S2 authenticate mode - * - * Returned Value: - * authenticate mode - * - ****************************************************************************/ - -static int esp_wifi_auth_trans(uint32_t wifi_auth) -{ - int auth_mode = IW_AUTH_WPA_VERSION_DISABLED; - - switch (wifi_auth) - { - case WIFI_AUTH_OPEN: - auth_mode = IW_AUTH_WPA_VERSION_DISABLED; - break; - - case WIFI_AUTH_WPA_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA; - break; - - case WIFI_AUTH_WPA2_PSK: - case WIFI_AUTH_WPA_WPA2_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA2; - break; - - case WIFI_AUTH_WPA3_PSK: - case WIFI_AUTH_WPA2_WPA3_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA3; - break; - - default: - wlerr("Failed to transfer wireless authmode: %" PRIu32 "", - wifi_auth); - break; - } - - return auth_mode; -} - -/**************************************************************************** - * Name: esp_wifi_cipher_trans - * - * Description: - * Converts a ESP32-S2 cipher type values to WEXT cipher type values. - * - * Input Parameters: - * wifi_cipher - ESP32-S2 cipher type - * - * Returned Value: - * cipher type - * - ****************************************************************************/ - -static int esp_wifi_cipher_trans(uint32_t wifi_cipher) -{ - int cipher_mode = IW_AUTH_CIPHER_NONE; - - switch (wifi_cipher) - { - case WIFI_CIPHER_TYPE_NONE: - cipher_mode = IW_AUTH_CIPHER_NONE; - break; - - case WIFI_CIPHER_TYPE_WEP40: - cipher_mode = IW_AUTH_CIPHER_WEP40; - break; - - case WIFI_CIPHER_TYPE_WEP104: - cipher_mode = IW_AUTH_CIPHER_WEP104; - break; - - case WIFI_CIPHER_TYPE_TKIP: - cipher_mode = IW_AUTH_CIPHER_TKIP; - break; - - case WIFI_CIPHER_TYPE_CCMP: - case WIFI_CIPHER_TYPE_TKIP_CCMP: - cipher_mode = IW_AUTH_CIPHER_CCMP; - break; - - case WIFI_CIPHER_TYPE_AES_CMAC128: - cipher_mode = IW_AUTH_CIPHER_AES_CMAC; - break; - - default: - wlerr("Failed to transfer wireless authmode: %" PRIu32 "", - wifi_cipher); - break; - } - - return cipher_mode; -} - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_freq_to_channel - * - * Description: - * Converts Wi-Fi frequency to channel. - * - * Input Parameters: - * freq - Wi-Fi frequency - * - * Returned Value: - * Wi-Fi channel - * - ****************************************************************************/ - -static int esp_freq_to_channel(uint16_t freq) -{ - int channel = 0; - if (freq >= 2412 && freq <= 2484) - { - if (freq == 2484) - { - channel = 14; - } - else - { - channel = freq - 2407; - if (channel % 5) - { - return 0; - } - - channel /= 5; - } - - return channel; - } - - if (freq >= 5005 && freq < 5900) - { - if (freq % 5) - { - return 0; - } - - channel = (freq - 5000) / 5; - return channel; - } - - if (freq >= 4905 && freq < 5000) - { - if (freq % 5) - { - return 0; - } - - channel = (freq - 4000) / 5; - return channel; - } - - return 0; -} - -/**************************************************************************** - * Functions needed by libpp.a - ****************************************************************************/ - -/**************************************************************************** - * Name: pp_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int pp_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Functions needed by libnet80211.a - ****************************************************************************/ - -/**************************************************************************** - * Name: net80211_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int net80211_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Functions needed by libcoexist.a - ****************************************************************************/ - -/**************************************************************************** - * Name: coexist_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int coexist_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: esp_event_send_internal - * - * Description: - * Post event message to queue - * - * Input Parameters: - * event_base - Event set name - * event_id - Event ID - * event_data - Event private data - * event_data_size - Event data size - * ticks_to_wait - Waiting system ticks - * - * Returned Value: - * Task maximum priority - * - ****************************************************************************/ - -int32_t esp_event_send_internal(esp_event_base_t event_base, - int32_t event_id, - void *event_data, - size_t event_data_size, - uint32_t ticks_to_wait) -{ - int32_t ret; - - ret = esp_event_post(event_base, event_id, event_data, event_data_size, - ticks_to_wait); - - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_free_eb - * - * Description: - * Free Wi-Fi receive callback input eb pointer - * - * Input Parameters: - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_free_eb(void *eb) -{ - esp_wifi_internal_free_rx_buffer(eb); -} - -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32-S2 Wi-Fi adapter - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_adapter_init(void) -{ - int ret; - wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); - - esp_wifi_lock(true); - - if (g_wifi_ref) - { - wlinfo("Wi-Fi adapter is already initialized\n"); - g_wifi_ref++; - esp_wifi_lock(false); - return OK; - } - - esp_init_event_queue(); - - wifi_cfg.nvs_enable = 0; - -#ifdef CONFIG_ESPRESSIF_WIFI_TX_AMPDU - wifi_cfg.ampdu_tx_enable = 1; -#else - wifi_cfg.ampdu_tx_enable = 0; -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI_RX_AMPDU - wifi_cfg.ampdu_rx_enable = 1; -#else - wifi_cfg.ampdu_rx_enable = 0; -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI_STA_DISCONNECT_PM - wifi_cfg.sta_disconnected_pm = true; -#else - wifi_cfg.sta_disconnected_pm = false; -#endif - - wifi_cfg.rx_ba_win = CONFIG_ESPRESSIF_WIFI_RXBA_AMPDU_WZ; - wifi_cfg.static_rx_buf_num = CONFIG_ESPRESSIF_WIFI_STATIC_RXBUF_NUM; - wifi_cfg.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RXBUF_NUM; - wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TXBUF_NUM; - - ret = esp_wifi_init(&wifi_cfg); - if (ret) - { - wlerr("Failed to initialize Wi-Fi error=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout_init_wifi; - } - - ret = esp_wifi_set_tx_done_cb(esp_wifi_tx_done_cb); - if (ret) - { - wlerr("Failed to register TX done callback ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout_init_txdone; - } - - g_wifi_ref++; - - wlinfo("OK to initialize Wi-Fi adapter\n"); - - esp_wifi_lock(false); - - return OK; - -errout_init_txdone: - esp_wifi_deinit(); -errout_init_wifi: - esp_wifi_lock(false); - - return ret; -} - -/**************************************************************************** - * Station functions - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_sta_start - * - * Description: - * Start Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_start(void) -{ - int ret; - wifi_mode_t mode; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (g_softap_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - { - mode = WIFI_MODE_STA; - } - - ret = esp_wifi_set_mode(mode); - if (ret) - { - wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - g_sta_started = true; - - wlinfo("OK to start Wi-Fi station\n"); - -errout: - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_stop - * - * Description: - * Stop Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_stop(void) -{ - int ret; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - - g_sta_started = false; - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (g_softap_started) - { - ret = esp_wifi_set_mode(WIFI_MODE_AP); - if (ret) - { - wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi AP ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - } -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - wlinfo("OK to stop Wi-Fi station\n"); - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP -errout: -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_send_data - * - * Description: - * Use Wi-Fi station interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_send_data(void *pbuf, size_t len) -{ - int ret; - - ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_sta_register_recv_cb - * - * Description: - * Register Wi-Fi station receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)) -{ - int ret; - - ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)recv_cb); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_sta_register_txdone_cb - * - * Description: - * Register the station TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb) -{ - g_sta_txdone_cb = cb; -} - -/**************************************************************************** - * Name: esp_wifi_sta_read_mac - * - * Description: - * Read station interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_sta_read_mac(uint8_t *mac) -{ - return esp_read_mac(mac, ESP_MAC_WIFI_STA); -} - -/**************************************************************************** - * Name: esp_wifi_set_password - * - * Description: - * Set/Get Wi-Fi station password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_password(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_encode_ext *ext = iwr->u.encoding.pointer; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[PWD_MAX_LEN + 1]; -#endif - - DEBUGASSERT(ext != NULL); - - pdata = ext->key; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - len = ext->key_len; - if (len > PWD_MAX_LEN) - { - return -EINVAL; - } - - memset(wifi_cfg.sta.password, 0x0, PWD_MAX_LEN); - - if (ext->alg != IW_ENCODE_ALG_NONE) - { - memcpy(wifi_cfg.sta.password, pdata, len); - } - - wifi_cfg.sta.pmf_cfg.capable = true; - wifi_cfg.sta.listen_interval = DEFAULT_LISTEN_INTERVAL; - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - len = iwr->u.encoding.length - sizeof(*ext); - size = strnlen((char *)wifi_cfg.sta.password, PWD_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - ext->key_len = size; - memcpy(pdata, wifi_cfg.sta.password, ext->key_len); - } - - if (g_sta_connected) - { - wifi_ap_record_t ap_info; - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d", ret); - return wifi_errno_trans(ret); - } - - switch (ap_info.pairwise_cipher) - { - case WIFI_CIPHER_TYPE_NONE: - ext->alg = IW_ENCODE_ALG_NONE; - break; - - case WIFI_CIPHER_TYPE_WEP40: - case WIFI_CIPHER_TYPE_WEP104: - ext->alg = IW_ENCODE_ALG_WEP; - break; - - case WIFI_CIPHER_TYPE_TKIP: - ext->alg = IW_ENCODE_ALG_TKIP; - break; - - case WIFI_CIPHER_TYPE_CCMP: - case WIFI_CIPHER_TYPE_TKIP_CCMP: - ext->alg = IW_ENCODE_ALG_CCMP; - break; - - case WIFI_CIPHER_TYPE_AES_CMAC128: - ext->alg = IW_ENCODE_ALG_AES_CMAC; - break; - - default: - wlerr("Failed to transfer wireless authmode: %d", - ap_info.pairwise_cipher); - return -EIO; - } - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi station password=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_essid - * - * Description: - * Set/Get Wi-Fi station ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_essid(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_point *essid = &iwr->u.essid; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[SSID_MAX_LEN + 1]; -#endif - - DEBUGASSERT(essid != NULL); - - pdata = essid->pointer; - len = essid->length; - - if (set && len > SSID_MAX_LEN) - { - return -EINVAL; - } - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - memset(wifi_cfg.sta.ssid, 0x0, SSID_MAX_LEN); - memcpy(wifi_cfg.sta.ssid, pdata, len); - memset(wifi_cfg.sta.sae_h2e_identifier, 0x0, SAE_H2E_IDENTIFIER_LEN); - wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.sta.ssid, SSID_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.sta.ssid, len); - } - - if (g_sta_connected) - { - essid->flags = IW_ESSID_ON; - } - else - { - essid->flags = IW_ESSID_OFF; - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi station ssid=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_bssid - * - * Description: - * Set/Get Wi-Fi station BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bssid(struct iwreq *iwr, bool set) -{ - int ret; - wifi_config_t wifi_cfg; - struct sockaddr *sockaddr; - char *pdata; - - sockaddr = &iwr->u.ap_addr; - pdata = sockaddr->sa_data; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - wifi_cfg.sta.bssid_set = true; - memcpy(wifi_cfg.sta.bssid, pdata, MAC_LEN); - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - memcpy(pdata, wifi_cfg.sta.bssid, MAC_LEN); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_connect - * - * Description: - * Trigger Wi-Fi station connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_connect(void) -{ - int ret; - uint32_t ticks; - - esp_wifi_lock(true); - - if (g_sta_connected) - { - wlinfo("Wi-Fi has connected AP\n"); - esp_wifi_lock(false); - return OK; - } - - g_sta_reconnect = true; - - ret = esp_wifi_set_config(WIFI_IF_STA, &g_sta_wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_connect(); - if (ret) - { - wlerr("Failed to connect ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - esp_wifi_lock(false); - - ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT); - do - { - if (g_sta_connected) - { - break; - } - - esp_task_delay(1); - } - while (ticks--); - - if (!g_sta_connected) - { - g_sta_reconnect = false; - wlinfo("Failed to connect to AP\n"); - return -1; - } - - return OK; - -errout: - g_sta_reconnect = false; - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_disconnect - * - * Description: - * Trigger Wi-Fi station disconnection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_disconnect(void) -{ - int ret; - - esp_wifi_lock(true); - - g_sta_reconnect = false; - - ret = esp_wifi_disconnect(); - if (ret) - { - wlerr("Failed to disconnect ret=%d\n", ret); - ret = wifi_errno_trans(ret); - } - else - { - wlinfo("OK to disconnect Wi-Fi station\n"); - } - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_mode - * - * Description: - * Set/Get Wi-Fi Station mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_mode(struct iwreq *iwr, bool set) -{ - if (set == false) - { - iwr->u.mode = IW_MODE_INFRA; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_auth - * - * Description: - * Set/Get station authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_auth(struct iwreq *iwr, bool set) -{ - int ret; - int cmd; - wifi_config_t wifi_cfg; - wifi_ap_record_t ap_info; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - { - switch (iwr->u.param.value) - { - case IW_AUTH_WPA_VERSION_DISABLED: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_WPA_VERSION_WPA: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA2: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA3: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK; - break; - - default: - wlerr("Invalid wpa version %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_CIPHER_PAIRWISE: - case IW_AUTH_CIPHER_GROUP: - { - switch (iwr->u.param.value) - { - case IW_AUTH_CIPHER_NONE: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_CIPHER_WEP40: - case IW_AUTH_CIPHER_WEP104: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP; - break; - - case IW_AUTH_CIPHER_TKIP: - case IW_AUTH_CIPHER_CCMP: - case IW_AUTH_CIPHER_AES_CMAC: - break; - - default: - wlerr("Invalid cipher mode %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -EINVAL; - } - - size_t password_len = strlen((const char *)wifi_cfg.sta.password); - wifi_auth_mode_t authmode = wifi_cfg.sta.threshold.authmode; - - if (g_sta_connected && - ((password_len > 0 && authmode != WIFI_AUTH_OPEN) || - (password_len == 0 && authmode == WIFI_AUTH_OPEN))) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - if (g_sta_connected == false) - { - return -ENOTCONN; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - iwr->u.param.value = esp_wifi_auth_trans(ap_info.authmode); - break; - - case IW_AUTH_CIPHER_PAIRWISE: - iwr->u.param.value = - esp_wifi_cipher_trans(ap_info.pairwise_cipher); - break; - - case IW_AUTH_CIPHER_GROUP: - iwr->u.param.value = esp_wifi_cipher_trans(ap_info.group_cipher); - break; - - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -ENOSYS; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_freq - * - * Description: - * Set/Get station frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_freq(struct iwreq *iwr, bool set) -{ - int ret; - - if (set && (iwr->u.freq.flags == IW_FREQ_FIXED)) - { - wifi_config_t wifi_cfg = g_sta_wifi_cfg; - - wifi_cfg.sta.channel = esp_freq_to_channel(iwr->u.freq.m); - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - if (g_sta_connected) - { - wifi_ap_record_t ap_info; - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.freq.flags = IW_FREQ_FIXED; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2407 + 5 * ap_info.primary; - } - else - { - iwr->u.freq.flags = IW_FREQ_AUTO; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2412; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_bitrate - * - * Description: - * Get station default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set) -{ - int ret; - wifi_ap_record_t ap_info; - - if (set) - { - return -ENOSYS; - } - else - { - if (g_sta_connected == false) - { - iwr->u.bitrate.fixed = IW_FREQ_AUTO; - return OK; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.bitrate.fixed = IW_FREQ_FIXED; - if (ap_info.phy_11n) - { - if (ap_info.second) - { - iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT40_BITRATE; - } - else - { - iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT20_BITRATE; - } - } - else if (ap_info.phy_11g) - { - iwr->u.bitrate.value = ESP_WIFI_11G_MAX_BITRATE; - } - else if (ap_info.phy_11b) - { - iwr->u.bitrate.value = ESP_WIFI_11B_MAX_BITRATE; - } - else - { - return -EIO; - } - } - - return OK; -} - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_wifi_sta_get_txpower - * - * Description: - * Get station transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_txpower(struct iwreq *iwr, bool set) -{ - int ret; - int8_t power; - double power_dbm; - - if (set) - { - if (iwr->u.txpower.flags == IW_TXPOW_RELATIVE) - { - power = (int8_t)iwr->u.txpower.value; - } - else - { - if (iwr->u.txpower.flags == IW_TXPOW_MWATT) - { - power_dbm = ceil(10 * log10(iwr->u.txpower.value)); - } - else - { - power_dbm = iwr->u.txpower.value; - } - - power = (int8_t)(power_dbm * 4); - } - - /* The value set by this API will be mapped to the max_tx_power - * of the structure wifi_country_t variable. Param power unit is - * 0.25dBm, range is [8, 84] corresponding to 2dBm - 20dBm. - * Relationship between set value and actual value. - * As follows: {set value range, actual value} = - * {{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, - * {[34, 43],34}, {[44, 51],44}, {[52, 55],52}, - * {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, - * {[72, 79],72}, {[80, 84],80}}. - */ - - if (power < 8 || power > 84) - { - wlerr("Failed to set transmit power =%d\n", power); - return -ENOSYS; - } - - esp_wifi_set_max_tx_power(power); - return OK; - } - else - { - ret = esp_wifi_get_max_tx_power(&power); - if (ret) - { - wlerr("Failed to get transmit power ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.txpower.disabled = 0; - iwr->u.txpower.flags = IW_TXPOW_DBM; - iwr->u.txpower.value = power / 4; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_channel - * - * Description: - * Get station range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_channel(struct iwreq *iwr, bool set) -{ - int ret; - int k; - wifi_country_t country; - struct iw_range *range; - - if (set) - { - return -ENOSYS; - } - else - { - ret = esp_wifi_get_country(&country); - if (ret) - { - wlerr("Failed to get country info ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - range = (struct iw_range *)iwr->u.data.pointer; - range->num_frequency = country.nchan; - for (k = 1; k <= range->num_frequency; k++) - { - range->freq[k - 1].i = k; - range->freq[k - 1].e = 0; - range->freq[k - 1].m = 2407 + 5 * k; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_country(struct iwreq *iwr, bool set) -{ - int ret; - char *country_code; - wifi_country_t country; - - if (set) - { - memset(&country, 0x00, sizeof(wifi_country_t)); - country.schan = 1; - country.policy = 0; - - country_code = (char *)iwr->u.data.pointer; - if (strlen(country_code) != 2) - { - wlerr("Invalid input arguments\n"); - return -EINVAL; - } - - if (strncmp(country_code, "US", 3) == 0 || - strncmp(country_code, "CA", 3) == 0) - { - country.nchan = 11; - } - else if(strncmp(country_code, "JP", 3) == 0) - { - country.nchan = 14; - } - else - { - country.nchan = 13; - } - - memcpy(country.cc, country_code, 2); - ret = esp_wifi_set_country(&country); - if (ret) - { - wlerr("Failed to Configure country ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - else - { - return -ENOSYS; - } - - return OK; -} - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_sta_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_rssi(struct iwreq *iwr, bool set) -{ - int ret; - wifi_ap_record_t ap_info; - - if (set) - { - return -ENOSYS; - } - else - { - if (g_sta_connected == false) - { - iwr->u.sens.value = 128; - return OK; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.sens.value = -(ap_info.rssi); - } - - return OK; -} -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * SoftAP functions - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/**************************************************************************** - * Name: esp_wifi_softap_start - * - * Description: - * Start Wi-Fi SoftAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_start(void) -{ - int ret; - wifi_mode_t mode; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - -#ifdef ESPRESSIF_WLAN_HAS_STA - if (g_sta_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESPRESSIF_WLAN_HAS_STA */ - { - mode = WIFI_MODE_AP; - } - - ret = esp_wifi_set_mode(mode); - if (ret) - { - wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - g_softap_started = true; - - wlinfo("OK to start Wi-Fi SoftAP\n"); - -errout: - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_softap_stop - * - * Description: - * Stop Wi-Fi SoftAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_stop(void) -{ - int ret; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - - g_softap_started = false; - -#ifdef ESPRESSIF_WLAN_HAS_STA - if (g_sta_started) - { - ret = esp_wifi_set_mode(WIFI_MODE_STA); - if (ret) - { - wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi STA ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - } -#endif /* ESPRESSIF_WLAN_HAS_STA */ - - wlinfo("OK to stop Wi-Fi SoftAP\n"); - -#ifdef ESPRESSIF_WLAN_HAS_STA -errout: -#endif /* ESPRESSIF_WLAN_HAS_STA */ - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_softap_send_data - * - * Description: - * Use Wi-Fi SoftAP interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_send_data(void *pbuf, size_t len) -{ - int ret; - - ret = esp_wifi_internal_tx(WIFI_IF_AP, pbuf, len); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_softap_register_recv_cb - * - * Description: - * Register Wi-Fi SoftAP receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)) -{ - int ret; - - ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)recv_cb); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_softap_register_txdone_cb - * - * Description: - * Register the SoftAP TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb) -{ - g_softap_txdone_cb = cb; -} - -/**************************************************************************** - * Name: esp_wifi_softap_read_mac - * - * Description: - * Read SoftAP interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_softap_read_mac(uint8_t *mac) -{ - return esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); -} - -/**************************************************************************** - * Name: esp_wifi_softap_password - * - * Description: - * Set/Get Wi-Fi SoftAP password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_password(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_encode_ext *ext = iwr->u.encoding.pointer; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[PWD_MAX_LEN + 1]; -#endif - - DEBUGASSERT(ext != NULL); - - pdata = ext->key; - len = ext->key_len; - - if (set && len > PWD_MAX_LEN) - { - return -EINVAL; - } - - pdata = ext->key; - len = ext->key_len; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - /* Clear the password field and copy the user password to it */ - - memset(wifi_cfg.ap.password, 0x0, PWD_MAX_LEN); - - if (ext->alg != IW_ENCODE_ALG_NONE) - { - memcpy(wifi_cfg.ap.password, pdata, len); - } - - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.ap.password, PWD_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.ap.password, len); - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi SoftAP password=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_essid - * - * Description: - * Set/Get Wi-Fi SoftAP ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_essid(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_point *essid = &iwr->u.essid; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[SSID_MAX_LEN + 1]; -#endif - - DEBUGASSERT(essid != NULL); - - pdata = essid->pointer; - len = essid->length; - - if (set && len > SSID_MAX_LEN) - { - return -EINVAL; - } - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - memset(wifi_cfg.ap.ssid, 0x0, SSID_MAX_LEN); - memcpy(wifi_cfg.ap.ssid, pdata, len); - wifi_cfg.ap.ssid_len = len; - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.ap.ssid, SSID_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.ap.ssid, len); - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi SoftAP ssid=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_bssid - * - * Description: - * Set/Get Wi-Fi softAP BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bssid(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -/**************************************************************************** - * Name: esp_wifi_softap_connect - * - * Description: - * Trigger Wi-Fi SoftAP accept connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_connect(void) -{ - int ret; - - ret = esp_wifi_set_config(WIFI_IF_AP, &g_softap_wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_disconnect - * - * Description: - * Trigger Wi-Fi SoftAP drop connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_disconnect(void) -{ - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_mode - * - * Description: - * Set/Get Wi-Fi SoftAP mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_mode(struct iwreq *iwr, bool set) -{ - if (set == false) - { - iwr->u.mode = IW_MODE_MASTER; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_auth - * - * Description: - * Set/get authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_auth(struct iwreq *iwr, bool set) -{ - int ret; - int cmd; - wifi_config_t wifi_cfg; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - { - switch (iwr->u.param.value) - { - case IW_AUTH_WPA_VERSION_DISABLED: - wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_WPA_VERSION_WPA: - wifi_cfg.ap.authmode = WIFI_AUTH_WPA_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA2: - wifi_cfg.ap.authmode = WIFI_AUTH_WPA2_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA3: - wifi_cfg.ap.pmf_cfg.required = true; - wifi_cfg.ap.pmf_cfg.capable = false; - wifi_cfg.ap.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; - wifi_cfg.ap.authmode = WIFI_AUTH_WPA3_PSK; - break; - - default: - wlerr("Invalid wpa version %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_CIPHER_PAIRWISE: - case IW_AUTH_CIPHER_GROUP: - { - switch (iwr->u.param.value) - { - case IW_AUTH_CIPHER_NONE: - wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_CIPHER_WEP40: - case IW_AUTH_CIPHER_WEP104: - wifi_cfg.ap.authmode = WIFI_AUTH_WEP; - break; - - case IW_AUTH_CIPHER_TKIP: - case IW_AUTH_CIPHER_CCMP: - case IW_AUTH_CIPHER_AES_CMAC: - break; - - default: - wlerr("Invalid cipher mode %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -EINVAL; - } - - size_t password_len = strlen((const char *)wifi_cfg.ap.password); - - if (g_softap_started && - ((password_len > 0 && wifi_cfg.ap.authmode != WIFI_AUTH_OPEN) || - (password_len == 0 && wifi_cfg.ap.authmode == WIFI_AUTH_OPEN))) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - return -ENOSYS; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_freq - * - * Description: - * Set/Get SoftAP frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_freq(struct iwreq *iwr, bool set) -{ - int ret; - wifi_config_t wifi_cfg; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - int channel = esp_freq_to_channel(iwr->u.freq.m); - - wifi_cfg.ap.channel = channel; - - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - iwr->u.freq.flags = IW_FREQ_FIXED; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2407 + 5 * wifi_cfg.ap.channel; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_get_bitrate - * - * Description: - * Get SoftAP default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -/**************************************************************************** - * Name: esp_wifi_softap_txpower - * - * Description: - * Get SoftAP transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_txpower(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_txpower(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_channel - * - * Description: - * Get SoftAP range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_channel(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_channel(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_country(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_country(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_rssi(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_stop_callback(void) -{ - wlinfo("INFO: Try to stop Wi-Fi\n"); - - int ret = esp_wifi_stop(); - if (ret) - { - wlerr("ERROR: Failed to stop Wi-Fi ret=%d\n", ret); - } - else - { - nxsig_sleep(1); - } -} diff --git a/arch/xtensa/src/esp32s3/Make.defs b/arch/xtensa/src/esp32s3/Make.defs index 90d2c34d43..db630f3ac4 100644 --- a/arch/xtensa/src/esp32s3/Make.defs +++ b/arch/xtensa/src/esp32s3/Make.defs @@ -207,7 +207,7 @@ endif ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty ifndef ESP_HAL_3RDPARTY_VERSION - ESP_HAL_3RDPARTY_VERSION = 96185c5348c747d2e15baef639d0b2a842ecd504 + ESP_HAL_3RDPARTY_VERSION = b6fa6c9098318007a61acc7c9f0f180443bb80c2 endif ifndef ESP_HAL_3RDPARTY_URL diff --git a/arch/xtensa/src/esp32s3/esp32s3_ble_adapter.c b/arch/xtensa/src/esp32s3/esp32s3_ble_adapter.c index 9adb578152..a9840c74fe 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_ble_adapter.c +++ b/arch/xtensa/src/esp32s3/esp32s3_ble_adapter.c @@ -3163,6 +3163,71 @@ uint32_t get_ble_controller_free_heap_size(void) * Other Functions ****************************************************************************/ +int32_t esp_ble_to_errno(int err) +{ + int ret; + + if (err < ESP_ERR_WIFI_BASE) + { + /* Unmask component error bits */ + + ret = err & 0xfff; + + switch (ret) + { + case ESP_OK: + ret = OK; + break; + case ESP_ERR_NO_MEM: + ret = -ENOMEM; + break; + + case ESP_ERR_INVALID_ARG: + ret = -EINVAL; + break; + + case ESP_ERR_INVALID_STATE: + ret = -EIO; + break; + + case ESP_ERR_INVALID_SIZE: + ret = -EINVAL; + break; + + case ESP_ERR_NOT_FOUND: + ret = -ENOSYS; + break; + + case ESP_ERR_NOT_SUPPORTED: + ret = -ENOSYS; + break; + + case ESP_ERR_TIMEOUT: + ret = -ETIMEDOUT; + break; + + case ESP_ERR_INVALID_MAC: + ret = -EINVAL; + break; + + default: + ret = ERROR; + break; + } + } + else + { + ret = ERROR; + } + + if (ret != OK) + { + wlerr("ERROR: %s\n", esp_err_to_name(err)); + } + + return ret; +} + /**************************************************************************** * Name: esp32s3_bt_controller_init * @@ -3318,7 +3383,7 @@ error: bt_controller_deinit_internal (); - return esp_wifi_to_errno(err); + return esp_ble_to_errno(err); } /**************************************************************************** diff --git a/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.c b/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.c index ce49934650..6820e1d73c 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.c +++ b/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.c @@ -23,21 +23,12 @@ ****************************************************************************/ #include -#include -#include -#include -#include #include #include #include -#include -#include #include #include -#include #include -#include -#include #include #include #include @@ -45,26 +36,21 @@ #include #include #include -#include #include #include -#include #include -#include #include #include "xtensa.h" #include "esp_attr.h" #include "hardware/esp32s3_system.h" #include "hardware/esp32s3_rtccntl.h" -#include "hardware/esp32s3_syscon.h" -#include "hardware/esp32s3_soc.h" -#include "esp32s3_irq.h" + #include "espressif/esp_wireless.h" -#include "esp32s3_wifi_adapter.h" -#include "esp32s3_rt_timer.h" #include "espressif/esp_wifi_utils.h" -#include "espressif/esp_wlan.h" + +#include "esp32s3_wifi_adapter.h" +#include "periph_ctrl.h" #ifdef CONFIG_PM # include "esp32s3_pm.h" @@ -77,59 +63,19 @@ # endif #endif -#include "esp_log.h" -#include "esp_mac.h" -#include "esp_private/phy.h" -#include "esp_private/wifi.h" -#include "esp_random.h" -#include "esp_timer.h" -#include "esp_wpa.h" -#include "periph_ctrl.h" -#include "rom/ets_sys.h" -#include "soc/soc_caps.h" - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE)) - -#define WIFI_CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT - #define TIMER_INITIALIZED_VAL (0x5aa5a55a) - -#define ESP_WIFI_11B_MAX_BITRATE 11 -#define ESP_WIFI_11G_MAX_BITRATE 54 -#define ESP_WIFI_11N_MCS7_HT20_BITRATE 72 -#define ESP_WIFI_11N_MCS7_HT40_BITRATE 150 - -#ifndef CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL -#define CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL 3 -#endif - -#define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL - -#define RTC_CLK_CAL_FRACT 19 /* Number of fractional bits in values returned by rtc_clk_cal */ - -#define ets_timer _ETSTIMER_ - -#define ESP_MAX_PRIORITIES (25) +#define RTC_CLK_CAL_FRACT 19 /* Number of fractional bits in values returned by rtc_clk_cal */ +#define ets_timer _ETSTIMER_ +#define ESP_MAX_PRIORITIES 25 /**************************************************************************** * Private Types ****************************************************************************/ -/* Wi-Fi Station state */ - -enum wifi_sta_state -{ - WIFI_STA_STATE_NULL, - WIFI_STA_STATE_START, - WIFI_STA_STATE_CONNECT, - WIFI_STA_STATE_DISCONNECT, - WIFI_STA_STATE_STOP -}; - /* Wi-Fi interrupt adapter private data */ struct irq_adpt @@ -155,13 +101,6 @@ struct time_adpt suseconds_t usec; /* Micro second value */ }; -/* Wi-Fi NVS private data */ - -struct nvs_adpt -{ - char *index_name; -}; - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -329,26 +268,6 @@ static void * coex_schm_get_phase_by_idx_wrapper(int phase_idx); * Private Data ****************************************************************************/ -/* Wi-Fi adapter reference */ - -static int g_wifi_ref; - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/* Wi-Fi station TX done callback function */ - -static wifi_txdone_cb_t g_sta_txdone_cb; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/* Wi-Fi SoftAP TX done callback function */ - -static wifi_txdone_cb_t g_softap_txdone_cb; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Public Data ****************************************************************************/ @@ -509,114 +428,10 @@ wifi_osi_funcs_t g_wifi_osi_funcs = * Public Data ****************************************************************************/ -/* Wi-Fi interface configuration */ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** - * Private Functions and Public Functions only used by libraries + * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: osi_errno_trans - * - * Description: - * Transform from nuttx Os error code to Wi-Fi adapter error code - * - * Input Parameters: - * ret - NuttX error code - * - * Returned Value: - * Wi-Fi adapter error code - * - ****************************************************************************/ - -static inline int32_t osi_errno_trans(int ret) -{ - if (!ret) - { - return true; - } - else - { - return false; - } -} - -/**************************************************************************** - * Name: wifi_errno_trans - * - * Description: - * Transform from ESP Wi-Fi error code to NuttX error code - * - * Input Parameters: - * ret - ESP Wi-Fi error code - * - * Returned Value: - * NuttX error code - * - ****************************************************************************/ - -static int32_t wifi_errno_trans(int ret) -{ - int wifierr; - - /* Unmask component error bits */ - - wifierr = ret & 0xfff; - - if (wifierr == ESP_OK) - { - return OK; - } - else if (wifierr == ESP_ERR_NO_MEM) - { - return -ENOMEM; - } - else if (wifierr == ESP_ERR_INVALID_ARG) - { - return -EINVAL; - } - else if (wifierr == ESP_ERR_INVALID_STATE) - { - return -EIO; - } - else if (wifierr == ESP_ERR_INVALID_SIZE) - { - return -EINVAL; - } - else if (wifierr == ESP_ERR_NOT_FOUND) - { - return -ENOSYS; - } - else if (wifierr == ESP_ERR_NOT_SUPPORTED) - { - return -ENOSYS; - } - else if (wifierr == ESP_ERR_TIMEOUT) - { - return -ETIMEDOUT; - } - else if (wifierr == ESP_ERR_INVALID_MAC) - { - return -EINVAL; - } - else - { - return ERROR; - } -} - /**************************************************************************** * Name: esp_int_adpt_cb * @@ -1033,7 +848,7 @@ static int32_t esp_semphr_take(void *semphr, uint32_t block_time_tick) block_time_tick, ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1061,7 +876,7 @@ static int32_t esp_semphr_give(void *semphr) wlerr("Failed to post sem error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1258,7 +1073,7 @@ static int32_t esp_mutex_lock(void *mutex_data) wlerr("Failed to lock mutex error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1286,7 +1101,7 @@ static int32_t esp_mutex_unlock(void *mutex_data) wlerr("Failed to unlock mutex error=%d\n", ret); } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1424,7 +1239,7 @@ static int32_t esp_queue_send_generic(void *queue, void *item, } } - return osi_errno_trans(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1731,6 +1546,7 @@ static int32_t esp_task_create_pinned_to_core(void *entry, { *((int *)task_handle) = pid; } + #ifdef CONFIG_SMP if (core_id < CONFIG_SMP_NCPUS) { @@ -1961,7 +1777,7 @@ static int32_t IRAM_ATTR semphr_take_from_isr_wrapper(void *semphr, { *(int *)hptw = 0; - return osi_errno_trans(nxsem_trywait(semphr)); + return nuttx_err_to_common_err(nxsem_trywait(semphr)); } /**************************************************************************** @@ -3823,2472 +3639,10 @@ static unsigned long esp_random_ulong(void) return esp_random(); } -/**************************************************************************** - * Name: esp_wifi_tx_done_cb - * - * Description: - * Wi-Fi TX done callback function. - * - * Input Parameters: - * ifidx - The interface id that the tx callback has been triggered from - * data - Pointer to the data transmitted - * data_len - Length of the data transmitted - * txstatus - True: if the data was transmitted successfully, False: if - * data transmission failed. - * - * Returned Value: - * none - * - ****************************************************************************/ - -static IRAM_ATTR void esp_wifi_tx_done_cb(uint8_t ifidx, uint8_t *data, - uint16_t *len, bool txstatus) -{ -#ifdef ESPRESSIF_WLAN_HAS_STA - if (ifidx == ESP_IF_WIFI_STA) - { - if (g_sta_txdone_cb) - { - g_sta_txdone_cb(data, len, txstatus); - } - } - else -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (ifidx == ESP_IF_WIFI_AP) - { - if (g_softap_txdone_cb) - { - g_softap_txdone_cb(data, len, txstatus); - } - } - else -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - { - wlerr("ifidx=%d is error\n", ifidx); - } -} - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_auth_trans - * - * Description: - * Converts a ESP32-S3 authenticate mode values to WEXT authenticate mode. - * - * Input Parameters: - * wifi_auth - ESP32-S3 authenticate mode - * - * Returned Value: - * authenticate mode - * - ****************************************************************************/ - -static int esp_wifi_auth_trans(uint32_t wifi_auth) -{ - int auth_mode = IW_AUTH_WPA_VERSION_DISABLED; - - switch (wifi_auth) - { - case WIFI_AUTH_OPEN: - auth_mode = IW_AUTH_WPA_VERSION_DISABLED; - break; - - case WIFI_AUTH_WPA_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA; - break; - - case WIFI_AUTH_WPA2_PSK: - case WIFI_AUTH_WPA_WPA2_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA2; - break; - - case WIFI_AUTH_WPA3_PSK: - case WIFI_AUTH_WPA2_WPA3_PSK: - auth_mode = IW_AUTH_WPA_VERSION_WPA3; - break; - - default: - wlerr("Failed to transfer wireless authmode: %" PRIu32 "", - wifi_auth); - break; - } - - return auth_mode; -} - -/**************************************************************************** - * Name: esp_wifi_cipher_trans - * - * Description: - * Converts a ESP32-S3 cipher type values to WEXT cipher type values. - * - * Input Parameters: - * wifi_cipher - ESP32-S3 cipher type - * - * Returned Value: - * cipher type - * - ****************************************************************************/ - -static int esp_wifi_cipher_trans(uint32_t wifi_cipher) -{ - int cipher_mode = IW_AUTH_CIPHER_NONE; - - switch (wifi_cipher) - { - case WIFI_CIPHER_TYPE_NONE: - cipher_mode = IW_AUTH_CIPHER_NONE; - break; - - case WIFI_CIPHER_TYPE_WEP40: - cipher_mode = IW_AUTH_CIPHER_WEP40; - break; - - case WIFI_CIPHER_TYPE_WEP104: - cipher_mode = IW_AUTH_CIPHER_WEP104; - break; - - case WIFI_CIPHER_TYPE_TKIP: - cipher_mode = IW_AUTH_CIPHER_TKIP; - break; - - case WIFI_CIPHER_TYPE_CCMP: - case WIFI_CIPHER_TYPE_TKIP_CCMP: - cipher_mode = IW_AUTH_CIPHER_CCMP; - break; - - case WIFI_CIPHER_TYPE_AES_CMAC128: - cipher_mode = IW_AUTH_CIPHER_AES_CMAC; - break; - - default: - wlerr("Failed to transfer wireless authmode: %" PRIu32 "", - wifi_cipher); - break; - } - - return cipher_mode; -} - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_freq_to_channel - * - * Description: - * Converts Wi-Fi frequency to channel. - * - * Input Parameters: - * freq - Wi-Fi frequency - * - * Returned Value: - * Wi-Fi channel - * - ****************************************************************************/ - -static int esp_freq_to_channel(uint16_t freq) -{ - int channel = 0; - if (freq >= 2412 && freq <= 2484) - { - if (freq == 2484) - { - channel = 14; - } - else - { - channel = freq - 2407; - if (channel % 5) - { - return 0; - } - - channel /= 5; - } - - return channel; - } - - if (freq >= 5005 && freq < 5900) - { - if (freq % 5) - { - return 0; - } - - channel = (freq - 5000) / 5; - return channel; - } - - if (freq >= 4905 && freq < 5000) - { - if (freq % 5) - { - return 0; - } - - channel = (freq - 4000) / 5; - return channel; - } - - return 0; -} - -/**************************************************************************** - * Functions needed by libpp.a - ****************************************************************************/ - -/**************************************************************************** - * Name: pp_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int pp_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Functions needed by libnet80211.a - ****************************************************************************/ - -/**************************************************************************** - * Name: net80211_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int net80211_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - -/**************************************************************************** - * Functions needed by libcoexist.a - ****************************************************************************/ - -/**************************************************************************** - * Name: coexist_printf - * - * Description: - * Output format string and its arguments - * - * Input Parameters: - * format - format string - * - * Returned Value: - * Zero (OK) - * - ****************************************************************************/ - -int coexist_printf(const char *format, ...) -{ -#ifdef CONFIG_DEBUG_WIRELESS_INFO - va_list arg; - - va_start(arg, format); - vsyslog(LOG_INFO, format, arg); - va_end(arg); -#endif - - return 0; -} - /**************************************************************************** * Public Functions ****************************************************************************/ -/**************************************************************************** - * Name: esp_event_send_internal - * - * Description: - * Post event message to queue - * - * Input Parameters: - * event_base - Event set name - * event_id - Event ID - * event_data - Event private data - * event_data_size - Event data size - * ticks_to_wait - Waiting system ticks - * - * Returned Value: - * Task maximum priority - * - ****************************************************************************/ - -int32_t esp_event_send_internal(esp_event_base_t event_base, - int32_t event_id, - void *event_data, - size_t event_data_size, - uint32_t ticks_to_wait) -{ - int32_t ret; - - ret = esp_event_post(event_base, event_id, event_data, event_data_size, - ticks_to_wait); - - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_free_eb - * - * Description: - * Free Wi-Fi receive callback input eb pointer - * - * Input Parameters: - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_free_eb(void *eb) -{ - esp_wifi_internal_free_rx_buffer(eb); -} - -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32-S3 Wi-Fi adapter - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_adapter_init(void) -{ - int ret; - wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); - - esp_wifi_lock(true); - - if (g_wifi_ref) - { - wlinfo("Wi-Fi adapter is already initialized\n"); - g_wifi_ref++; - esp_wifi_lock(false); - return OK; - } - - esp_init_event_queue(); - - wifi_cfg.nvs_enable = 0; - -#ifdef CONFIG_ESPRESSIF_WIFI_TX_AMPDU - wifi_cfg.ampdu_tx_enable = 1; -#else - wifi_cfg.ampdu_tx_enable = 0; -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI_RX_AMPDU - wifi_cfg.ampdu_rx_enable = 1; -#else - wifi_cfg.ampdu_rx_enable = 0; -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI_STA_DISCONNECT_PM - wifi_cfg.sta_disconnected_pm = true; -#else - wifi_cfg.sta_disconnected_pm = false; -#endif - - wifi_cfg.rx_ba_win = CONFIG_ESPRESSIF_WIFI_RXBA_AMPDU_WZ; - wifi_cfg.static_rx_buf_num = CONFIG_ESPRESSIF_WIFI_STATIC_RXBUF_NUM; - wifi_cfg.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RXBUF_NUM; - wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TXBUF_NUM; - - ret = esp_wifi_init(&wifi_cfg); - if (ret) - { - wlerr("Failed to initialize Wi-Fi error=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout_init_wifi; - } - - ret = esp_wifi_set_tx_done_cb(esp_wifi_tx_done_cb); - if (ret) - { - wlerr("Failed to register TX done callback ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout_init_txdone; - } - - g_wifi_ref++; - - wlinfo("OK to initialize Wi-Fi adapter\n"); - - esp_wifi_lock(false); - - return OK; - -errout_init_txdone: - esp_wifi_deinit(); -errout_init_wifi: - esp_wifi_lock(false); - - return ret; -} - -/**************************************************************************** - * Station functions - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_sta_start - * - * Description: - * Start Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_start(void) -{ - int ret; - wifi_mode_t mode; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (g_softap_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - { - mode = WIFI_MODE_STA; - } - - ret = esp_wifi_set_mode(mode); - if (ret) - { - wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - g_sta_started = true; - - wlinfo("OK to start Wi-Fi station\n"); - -errout: - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_stop - * - * Description: - * Stop Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_stop(void) -{ - int ret; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - - g_sta_started = false; - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - if (g_softap_started) - { - ret = esp_wifi_set_mode(WIFI_MODE_AP); - if (ret) - { - wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi AP ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - } -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - wlinfo("OK to stop Wi-Fi station\n"); - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP -errout: -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_send_data - * - * Description: - * Use Wi-Fi station interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_send_data(void *pbuf, size_t len) -{ - int ret; - - ret = esp_wifi_internal_tx(WIFI_IF_STA, pbuf, len); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_sta_register_recv_cb - * - * Description: - * Register Wi-Fi station receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)) -{ - int ret; - - ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, (wifi_rxcb_t)recv_cb); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_sta_register_txdone_cb - * - * Description: - * Register the station TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb) -{ - g_sta_txdone_cb = cb; -} - -/**************************************************************************** - * Name: esp_wifi_sta_read_mac - * - * Description: - * Read station interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_sta_read_mac(uint8_t *mac) -{ - return esp_read_mac(mac, ESP_MAC_WIFI_STA); -} - -/**************************************************************************** - * Name: esp_wifi_set_password - * - * Description: - * Set/Get Wi-Fi station password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_password(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_encode_ext *ext = iwr->u.encoding.pointer; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[PWD_MAX_LEN + 1]; -#endif - - DEBUGASSERT(ext != NULL); - - pdata = ext->key; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - len = ext->key_len; - if (len > PWD_MAX_LEN) - { - return -EINVAL; - } - - memset(wifi_cfg.sta.password, 0x0, PWD_MAX_LEN); - - if (ext->alg != IW_ENCODE_ALG_NONE) - { - memcpy(wifi_cfg.sta.password, pdata, len); - } - - wifi_cfg.sta.pmf_cfg.capable = true; - wifi_cfg.sta.listen_interval = DEFAULT_LISTEN_INTERVAL; - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - len = iwr->u.encoding.length - sizeof(*ext); - size = strnlen((char *)wifi_cfg.sta.password, PWD_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - ext->key_len = size; - memcpy(pdata, wifi_cfg.sta.password, ext->key_len); - } - - if (g_sta_connected) - { - wifi_ap_record_t ap_info; - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d", ret); - return wifi_errno_trans(ret); - } - - switch (ap_info.pairwise_cipher) - { - case WIFI_CIPHER_TYPE_NONE: - ext->alg = IW_ENCODE_ALG_NONE; - break; - - case WIFI_CIPHER_TYPE_WEP40: - case WIFI_CIPHER_TYPE_WEP104: - ext->alg = IW_ENCODE_ALG_WEP; - break; - - case WIFI_CIPHER_TYPE_TKIP: - ext->alg = IW_ENCODE_ALG_TKIP; - break; - - case WIFI_CIPHER_TYPE_CCMP: - case WIFI_CIPHER_TYPE_TKIP_CCMP: - ext->alg = IW_ENCODE_ALG_CCMP; - break; - - case WIFI_CIPHER_TYPE_AES_CMAC128: - ext->alg = IW_ENCODE_ALG_AES_CMAC; - break; - - default: - wlerr("Failed to transfer wireless authmode: %d", - ap_info.pairwise_cipher); - return -EIO; - } - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi station password=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_essid - * - * Description: - * Set/Get Wi-Fi station ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_essid(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_point *essid = &iwr->u.essid; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[SSID_MAX_LEN + 1]; -#endif - - DEBUGASSERT(essid != NULL); - - pdata = essid->pointer; - len = essid->length; - - if (set && len > SSID_MAX_LEN) - { - return -EINVAL; - } - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - memset(wifi_cfg.sta.ssid, 0x0, SSID_MAX_LEN); - memcpy(wifi_cfg.sta.ssid, pdata, len); - memset(wifi_cfg.sta.sae_h2e_identifier, 0x0, SAE_H2E_IDENTIFIER_LEN); - wifi_cfg.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.sta.ssid, SSID_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.sta.ssid, len); - } - - if (g_sta_connected) - { - essid->flags = IW_ESSID_ON; - } - else - { - essid->flags = IW_ESSID_OFF; - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi station ssid=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_bssid - * - * Description: - * Set/Get Wi-Fi station BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bssid(struct iwreq *iwr, bool set) -{ - int ret; - wifi_config_t wifi_cfg; - struct sockaddr *sockaddr; - char *pdata; - - sockaddr = &iwr->u.ap_addr; - pdata = sockaddr->sa_data; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - wifi_cfg.sta.bssid_set = true; - memcpy(wifi_cfg.sta.bssid, pdata, MAC_LEN); - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - memcpy(pdata, wifi_cfg.sta.bssid, MAC_LEN); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_connect - * - * Description: - * Trigger Wi-Fi station connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_connect(void) -{ - int ret; - uint32_t ticks; - - esp_wifi_lock(true); - - if (g_sta_connected) - { - wlinfo("Wi-Fi has connected AP\n"); - esp_wifi_lock(false); - return OK; - } - - g_sta_reconnect = true; - - ret = esp_wifi_set_config(WIFI_IF_STA, &g_sta_wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_connect(); - if (ret) - { - wlerr("Failed to connect ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - esp_wifi_lock(false); - - ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT); - do - { - if (g_sta_connected) - { - break; - } - - esp_task_delay(1); - } - while (ticks--); - - if (!g_sta_connected) - { - g_sta_reconnect = false; - wlinfo("Failed to connect to AP\n"); - return -1; - } - - return OK; - -errout: - g_sta_reconnect = false; - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_disconnect - * - * Description: - * Trigger Wi-Fi station disconnection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_disconnect(void) -{ - int ret; - - esp_wifi_lock(true); - - g_sta_reconnect = false; - - ret = esp_wifi_disconnect(); - if (ret) - { - wlerr("Failed to disconnect ret=%d\n", ret); - ret = wifi_errno_trans(ret); - } - else - { - wlinfo("OK to disconnect Wi-Fi station\n"); - } - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_sta_mode - * - * Description: - * Set/Get Wi-Fi Station mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_mode(struct iwreq *iwr, bool set) -{ - if (set == false) - { - iwr->u.mode = IW_MODE_INFRA; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_auth - * - * Description: - * Set/Get station authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_auth(struct iwreq *iwr, bool set) -{ - int ret; - int cmd; - wifi_config_t wifi_cfg; - wifi_ap_record_t ap_info; - - wifi_cfg = g_sta_wifi_cfg; - - if (set) - { - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - { - switch (iwr->u.param.value) - { - case IW_AUTH_WPA_VERSION_DISABLED: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_WPA_VERSION_WPA: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA2: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA3: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA3_PSK; - break; - - default: - wlerr("Invalid wpa version %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_CIPHER_PAIRWISE: - case IW_AUTH_CIPHER_GROUP: - { - switch (iwr->u.param.value) - { - case IW_AUTH_CIPHER_NONE: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_CIPHER_WEP40: - case IW_AUTH_CIPHER_WEP104: - wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WEP; - break; - - case IW_AUTH_CIPHER_TKIP: - case IW_AUTH_CIPHER_CCMP: - case IW_AUTH_CIPHER_AES_CMAC: - break; - - default: - wlerr("Invalid cipher mode %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -EINVAL; - } - - size_t password_len = strlen((const char *)wifi_cfg.sta.password); - wifi_auth_mode_t authmode = wifi_cfg.sta.threshold.authmode; - - if (g_sta_connected && - ((password_len > 0 && authmode != WIFI_AUTH_OPEN) || - (password_len == 0 && authmode == WIFI_AUTH_OPEN))) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - if (g_sta_connected == false) - { - return -ENOTCONN; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - iwr->u.param.value = esp_wifi_auth_trans(ap_info.authmode); - break; - - case IW_AUTH_CIPHER_PAIRWISE: - iwr->u.param.value = - esp_wifi_cipher_trans(ap_info.pairwise_cipher); - break; - - case IW_AUTH_CIPHER_GROUP: - iwr->u.param.value = esp_wifi_cipher_trans(ap_info.group_cipher); - break; - - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -ENOSYS; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_freq - * - * Description: - * Set/Get station frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_freq(struct iwreq *iwr, bool set) -{ - int ret; - - if (set && (iwr->u.freq.flags == IW_FREQ_FIXED)) - { - wifi_config_t wifi_cfg = g_sta_wifi_cfg; - - wifi_cfg.sta.channel = esp_freq_to_channel(iwr->u.freq.m); - - if (g_sta_connected) - { - ret = esp_wifi_sta_disconnect(); - if (ret) - { - wlerr("Failed to disconnect from Wi-Fi AP ret=%d\n", ret); - return ret; - } - - ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - ret = esp_wifi_sta_connect(); - if (ret) - { - wlerr("Failed to connect to Wi-Fi AP ret=%d\n", ret); - return ret; - } - } - - g_sta_wifi_cfg = wifi_cfg; - } - else - { - if (g_sta_connected) - { - wifi_ap_record_t ap_info; - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.freq.flags = IW_FREQ_FIXED; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2407 + 5 * ap_info.primary; - } - else - { - iwr->u.freq.flags = IW_FREQ_AUTO; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2412; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_bitrate - * - * Description: - * Get station default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set) -{ - int ret; - wifi_ap_record_t ap_info; - - if (set) - { - return -ENOSYS; - } - else - { - if (g_sta_connected == false) - { - iwr->u.bitrate.fixed = IW_FREQ_AUTO; - return OK; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.bitrate.fixed = IW_FREQ_FIXED; - if (ap_info.phy_11n) - { - if (ap_info.second) - { - iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT40_BITRATE; - } - else - { - iwr->u.bitrate.value = ESP_WIFI_11N_MCS7_HT20_BITRATE; - } - } - else if (ap_info.phy_11g) - { - iwr->u.bitrate.value = ESP_WIFI_11G_MAX_BITRATE; - } - else if (ap_info.phy_11b) - { - iwr->u.bitrate.value = ESP_WIFI_11B_MAX_BITRATE; - } - else - { - return -EIO; - } - } - - return OK; -} - -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_wifi_sta_get_txpower - * - * Description: - * Get station transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_txpower(struct iwreq *iwr, bool set) -{ - int ret; - int8_t power; - double power_dbm; - - if (set) - { - if (iwr->u.txpower.flags == IW_TXPOW_RELATIVE) - { - power = (int8_t)iwr->u.txpower.value; - } - else - { - if (iwr->u.txpower.flags == IW_TXPOW_MWATT) - { - power_dbm = ceil(10 * log10(iwr->u.txpower.value)); - } - else - { - power_dbm = iwr->u.txpower.value; - } - - power = (int8_t)(power_dbm * 4); - } - - /* The value set by this API will be mapped to the max_tx_power - * of the structure wifi_country_t variable. Param power unit is - * 0.25dBm, range is [8, 84] corresponding to 2dBm - 20dBm. - * Relationship between set value and actual value. - * As follows: {set value range, actual value} = - * {{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, - * {[34, 43],34}, {[44, 51],44}, {[52, 55],52}, - * {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, - * {[72, 79],72}, {[80, 84],80}}. - */ - - if (power < 8 || power > 84) - { - wlerr("Failed to set transmit power =%d\n", power); - return -ENOSYS; - } - - esp_wifi_set_max_tx_power(power); - return OK; - } - else - { - ret = esp_wifi_get_max_tx_power(&power); - if (ret) - { - wlerr("Failed to get transmit power ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.txpower.disabled = 0; - iwr->u.txpower.flags = IW_TXPOW_DBM; - iwr->u.txpower.value = power / 4; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_channel - * - * Description: - * Get station range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_channel(struct iwreq *iwr, bool set) -{ - int ret; - int k; - wifi_country_t country; - struct iw_range *range; - - if (set) - { - return -ENOSYS; - } - else - { - ret = esp_wifi_get_country(&country); - if (ret) - { - wlerr("Failed to get country info ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - range = (struct iw_range *)iwr->u.data.pointer; - range->num_frequency = country.nchan; - for (k = 1; k <= range->num_frequency; k++) - { - range->freq[k - 1].i = k; - range->freq[k - 1].e = 0; - range->freq[k - 1].m = 2407 + 5 * k; - } - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_sta_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_country(struct iwreq *iwr, bool set) -{ - int ret; - char *country_code; - wifi_country_t country; - - if (set) - { - memset(&country, 0x00, sizeof(wifi_country_t)); - country.schan = 1; - country.policy = 0; - - country_code = (char *)iwr->u.data.pointer; - if (strlen(country_code) != 2) - { - wlerr("Invalid input arguments\n"); - return -EINVAL; - } - - if (strncmp(country_code, "US", 3) == 0 || - strncmp(country_code, "CA", 3) == 0) - { - country.nchan = 11; - } - else if(strncmp(country_code, "JP", 3) == 0) - { - country.nchan = 14; - } - else - { - country.nchan = 13; - } - - memcpy(country.cc, country_code, 2); - ret = esp_wifi_set_country(&country); - if (ret) - { - wlerr("Failed to Configure country ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - else - { - memset(&country, 0x00, sizeof(wifi_country_t)); - ret = esp_wifi_get_country(&country); - if (ret) - { - wlerr("Failed to get country info ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - memcpy(iwr->u.data.pointer, country.cc, 2); - } - - return OK; -} - -#ifdef ESPRESSIF_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_sta_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_rssi(struct iwreq *iwr, bool set) -{ - int ret; - wifi_ap_record_t ap_info; - - if (set) - { - return -ENOSYS; - } - else - { - if (g_sta_connected == false) - { - iwr->u.sens.value = 128; - return OK; - } - - ret = esp_wifi_sta_get_ap_info(&ap_info); - if (ret) - { - wlerr("Failed to get AP record ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - iwr->u.sens.value = -(ap_info.rssi); - } - - return OK; -} -#endif /* ESPRESSIF_WLAN_HAS_STA */ - -/**************************************************************************** - * SoftAP functions - ****************************************************************************/ - -#ifdef ESPRESSIF_WLAN_HAS_SOFTAP - -/**************************************************************************** - * Name: esp_wifi_softap_start - * - * Description: - * Start Wi-Fi SoftAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_start(void) -{ - int ret; - wifi_mode_t mode; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - -#ifdef ESPRESSIF_WLAN_HAS_STA - if (g_sta_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESPRESSIF_WLAN_HAS_STA */ - { - mode = WIFI_MODE_AP; - } - - ret = esp_wifi_set_mode(mode); - if (ret) - { - wlerr("Failed to set Wi-Fi mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - g_softap_started = true; - - wlinfo("OK to start Wi-Fi SoftAP\n"); - -errout: - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_softap_stop - * - * Description: - * Stop Wi-Fi SoftAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_stop(void) -{ - int ret; - - esp_wifi_lock(true); - - ret = esp_wifi_stop(); - if (ret) - { - wlinfo("Failed to stop Wi-Fi ret=%d\n", ret); - } - - g_softap_started = false; - -#ifdef ESPRESSIF_WLAN_HAS_STA - if (g_sta_started) - { - ret = esp_wifi_set_mode(WIFI_MODE_STA); - if (ret) - { - wlerr("Failed to set Wi-Fi AP mode ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi STA ret=%d\n", ret); - ret = wifi_errno_trans(ret); - goto errout; - } - } -#endif /* ESPRESSIF_WLAN_HAS_STA */ - - wlinfo("OK to stop Wi-Fi SoftAP\n"); - -#ifdef ESPRESSIF_WLAN_HAS_STA -errout: -#endif /* ESPRESSIF_WLAN_HAS_STA */ - - esp_wifi_lock(false); - return ret; -} - -/**************************************************************************** - * Name: esp_wifi_softap_send_data - * - * Description: - * Use Wi-Fi SoftAP interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_send_data(void *pbuf, size_t len) -{ - int ret; - - ret = esp_wifi_internal_tx(WIFI_IF_AP, pbuf, len); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_softap_register_recv_cb - * - * Description: - * Register Wi-Fi SoftAP receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)) -{ - int ret; - - ret = esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)recv_cb); - - return wifi_errno_trans(ret); -} - -/**************************************************************************** - * Name: esp_wifi_softap_register_txdone_cb - * - * Description: - * Register the SoftAP TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb) -{ - g_softap_txdone_cb = cb; -} - -/**************************************************************************** - * Name: esp_wifi_softap_read_mac - * - * Description: - * Read SoftAP interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_softap_read_mac(uint8_t *mac) -{ - return esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); -} - -/**************************************************************************** - * Name: esp_wifi_softap_password - * - * Description: - * Set/Get Wi-Fi SoftAP password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_password(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_encode_ext *ext = iwr->u.encoding.pointer; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[PWD_MAX_LEN + 1]; -#endif - - DEBUGASSERT(ext != NULL); - - pdata = ext->key; - len = ext->key_len; - - if (set && len > PWD_MAX_LEN) - { - return -EINVAL; - } - - pdata = ext->key; - len = ext->key_len; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - /* Clear the password field and copy the user password to it */ - - memset(wifi_cfg.ap.password, 0x0, PWD_MAX_LEN); - - if (ext->alg != IW_ENCODE_ALG_NONE) - { - memcpy(wifi_cfg.ap.password, pdata, len); - } - - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.ap.password, PWD_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.ap.password, len); - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi SoftAP password=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_essid - * - * Description: - * Set/Get Wi-Fi SoftAP ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_essid(struct iwreq *iwr, bool set) -{ - int ret; - int size; - wifi_config_t wifi_cfg; - struct iw_point *essid = &iwr->u.essid; - uint8_t *pdata; - uint8_t len; -#ifdef CONFIG_DEBUG_WIRELESS_INFO - char buf[SSID_MAX_LEN + 1]; -#endif - - DEBUGASSERT(essid != NULL); - - pdata = essid->pointer; - len = essid->length; - - if (set && len > SSID_MAX_LEN) - { - return -EINVAL; - } - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - memset(wifi_cfg.ap.ssid, 0x0, SSID_MAX_LEN); - memcpy(wifi_cfg.ap.ssid, pdata, len); - wifi_cfg.ap.ssid_len = len; - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - size = strnlen((char *)wifi_cfg.ap.ssid, SSID_MAX_LEN); - if (len < size) - { - return -EINVAL; - } - else - { - len = size; - memcpy(pdata, wifi_cfg.ap.ssid, len); - } - } - -#ifdef CONFIG_DEBUG_WIRELESS_INFO - memcpy(buf, pdata, len); - buf[len] = 0; - wlinfo("Wi-Fi SoftAP ssid=%s len=%d\n", buf, len); -#endif - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_bssid - * - * Description: - * Set/Get Wi-Fi softAP BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bssid(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -/**************************************************************************** - * Name: esp_wifi_softap_connect - * - * Description: - * Trigger Wi-Fi SoftAP accept connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_connect(void) -{ - int ret; - - ret = esp_wifi_set_config(WIFI_IF_AP, &g_softap_wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_disconnect - * - * Description: - * Trigger Wi-Fi SoftAP drop connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_disconnect(void) -{ - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_mode - * - * Description: - * Set/Get Wi-Fi SoftAP mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_mode(struct iwreq *iwr, bool set) -{ - if (set == false) - { - iwr->u.mode = IW_MODE_MASTER; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_auth - * - * Description: - * Set/get authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_auth(struct iwreq *iwr, bool set) -{ - int ret; - int cmd; - wifi_config_t wifi_cfg; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - cmd = iwr->u.param.flags & IW_AUTH_INDEX; - switch (cmd) - { - case IW_AUTH_WPA_VERSION: - { - switch (iwr->u.param.value) - { - case IW_AUTH_WPA_VERSION_DISABLED: - wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_WPA_VERSION_WPA: - wifi_cfg.ap.authmode = WIFI_AUTH_WPA_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA2: - wifi_cfg.ap.authmode = WIFI_AUTH_WPA2_PSK; - break; - - case IW_AUTH_WPA_VERSION_WPA3: - wifi_cfg.ap.pmf_cfg.required = true; - wifi_cfg.ap.pmf_cfg.capable = false; - wifi_cfg.ap.sae_pwe_h2e = WPA3_SAE_PWE_BOTH; - wifi_cfg.ap.authmode = WIFI_AUTH_WPA3_PSK; - break; - - default: - wlerr("Invalid wpa version %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_CIPHER_PAIRWISE: - case IW_AUTH_CIPHER_GROUP: - { - switch (iwr->u.param.value) - { - case IW_AUTH_CIPHER_NONE: - wifi_cfg.ap.authmode = WIFI_AUTH_OPEN; - break; - - case IW_AUTH_CIPHER_WEP40: - case IW_AUTH_CIPHER_WEP104: - wifi_cfg.ap.authmode = WIFI_AUTH_WEP; - break; - - case IW_AUTH_CIPHER_TKIP: - case IW_AUTH_CIPHER_CCMP: - case IW_AUTH_CIPHER_AES_CMAC: - break; - - default: - wlerr("Invalid cipher mode %" PRId32 "\n", - iwr->u.param.value); - return -EINVAL; - } - } - - break; - case IW_AUTH_KEY_MGMT: - case IW_AUTH_TKIP_COUNTERMEASURES: - case IW_AUTH_DROP_UNENCRYPTED: - case IW_AUTH_80211_AUTH_ALG: - case IW_AUTH_WPA_ENABLED: - case IW_AUTH_RX_UNENCRYPTED_EAPOL: - case IW_AUTH_ROAMING_CONTROL: - case IW_AUTH_PRIVACY_INVOKED: - default: - wlerr("Unknown cmd %d\n", cmd); - return -EINVAL; - } - - size_t password_len = strlen((const char *)wifi_cfg.ap.password); - - if (g_softap_started && - ((password_len > 0 && wifi_cfg.ap.authmode != WIFI_AUTH_OPEN) || - (password_len == 0 && wifi_cfg.ap.authmode == WIFI_AUTH_OPEN))) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - return -ENOSYS; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_freq - * - * Description: - * Set/Get SoftAP frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_freq(struct iwreq *iwr, bool set) -{ - int ret; - wifi_config_t wifi_cfg; - - wifi_cfg = g_softap_wifi_cfg; - - if (set) - { - int channel = esp_freq_to_channel(iwr->u.freq.m); - - wifi_cfg.ap.channel = channel; - - if (g_softap_started) - { - ret = esp_wifi_set_config(WIFI_IF_AP, &wifi_cfg); - if (ret) - { - wlerr("Failed to set Wi-Fi config data ret=%d\n", ret); - return wifi_errno_trans(ret); - } - } - - g_softap_wifi_cfg = wifi_cfg; - } - else - { - iwr->u.freq.flags = IW_FREQ_FIXED; - iwr->u.freq.e = 0; - iwr->u.freq.m = 2407 + 5 * wifi_cfg.ap.channel; - } - - return OK; -} - -/**************************************************************************** - * Name: esp_wifi_softap_get_bitrate - * - * Description: - * Get SoftAP default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -/**************************************************************************** - * Name: esp_wifi_softap_txpower - * - * Description: - * Get SoftAP transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_txpower(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_txpower(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_channel - * - * Description: - * Get SoftAP range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_channel(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_channel(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_country(struct iwreq *iwr, bool set) -{ - return esp_wifi_sta_country(iwr, set); -} - -/**************************************************************************** - * Name: esp_wifi_softap_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_rssi(struct iwreq *iwr, bool set) -{ - return -ENOSYS; -} - -#endif /* ESPRESSIF_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Name: esp_wifi_bt_coexist_init * @@ -6313,32 +3667,3 @@ int esp_wifi_bt_coexist_init(void) return 0; } #endif /* CONFIG_ESPRESSIF_WIFI_BT_COEXIST */ - -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_stop_callback(void) -{ - wlinfo("INFO: Try to stop Wi-Fi\n"); - - int ret = esp_wifi_stop(); - if (ret) - { - wlerr("ERROR: Failed to stop Wi-Fi ret=%d\n", ret); - } - else - { - nxsig_sleep(1); - } -} diff --git a/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.h b/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.h index f0db8ba52b..668a209fb5 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.h +++ b/arch/xtensa/src/esp32s3/esp32s3_wifi_adapter.h @@ -26,9 +26,6 @@ ****************************************************************************/ #include -#include - -#include #ifndef __ASSEMBLY__ @@ -45,725 +42,10 @@ extern "C" * Pre-processor Definitions ****************************************************************************/ -#define SSID_MAX_LEN (32) -#define PWD_MAX_LEN (64) - -#define CONFIG_IDF_TARGET_ESP32S3 1 - -/* Define esp_err_t */ - -typedef int esp_err_t; - -/* Wi-Fi TX done callback function */ - -typedef void (*wifi_txdone_cb_t)(uint8_t *data, uint16_t *len, bool status); - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32S3 Wi-Fi adapter - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_adapter_init(void); - -/**************************************************************************** - * Name: esp_wifi_free_eb - * - * Description: - * Free Wi-Fi receive callback input eb pointer - * - * Input Parameters: - * eb - Wi-Fi receive callback input eb pointer - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_free_eb(void *eb); - -/**************************************************************************** - * Name: esp_wifi_sta_start - * - * Description: - * Start Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_start(void); - -/**************************************************************************** - * Name: esp_wifi_sta_stop - * - * Description: - * Stop Wi-Fi station. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_stop(void); - -/**************************************************************************** - * Name: esp_wifi_sta_send_data - * - * Description: - * Use Wi-Fi station interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_send_data(void *pbuf, size_t len); - -/**************************************************************************** - * Name: esp_wifi_sta_register_recv_cb - * - * Description: - * Regitser Wi-Fi station receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); - -/**************************************************************************** - * Name: esp_wifi_sta_register_txdone_cb - * - * Description: - * Register the station TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_sta_register_txdone_cb(wifi_txdone_cb_t cb); - -/**************************************************************************** - * Name: esp_wifi_sta_read_mac - * - * Description: - * Read station interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_sta_read_mac(uint8_t *mac); - -/**************************************************************************** - * Name: esp_wifi_set_password - * - * Description: - * Set/Get Wi-Fi station password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_password(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_essid - * - * Description: - * Set/Get Wi-Fi station ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_essid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_bssid - * - * Description: - * Set/Get Wi-Fi station BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bssid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_connect - * - * Description: - * Trigger Wi-Fi station connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_connect(void); - -/**************************************************************************** - * Name: esp_wifi_sta_disconnect - * - * Description: - * Trigger Wi-Fi station disconnection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_disconnect(void); - -/**************************************************************************** - * Name: esp_wifi_sta_mode - * - * Description: - * Set/Get Wi-Fi Station mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_mode(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_auth - * - * Description: - * Set/Get station authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_auth(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_freq - * - * Description: - * Get station frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_freq(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_bitrate - * - * Description: - * Get station default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_bitrate(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_get_txpower - * - * Description: - * Get station transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_txpower(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_get_channel_range - * - * Description: - * Get station range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_channel(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_country(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_sta_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_sta_rssi(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_start - * - * Description: - * Start Wi-Fi softAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_start(void); - -/**************************************************************************** - * Name: esp_wifi_softap_stop - * - * Description: - * Stop Wi-Fi softAP. - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_stop(void); - -/**************************************************************************** - * Name: esp_wifi_softap_send_data - * - * Description: - * Use Wi-Fi softAP interface to send 802.3 frame - * - * Input Parameters: - * pbuf - Packet buffer pointer - * len - Packet length - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_send_data(void *pbuf, size_t len); - -/**************************************************************************** - * Name: esp_wifi_softap_register_recv_cb - * - * Description: - * Regitser Wi-Fi softAP receive packet callback function - * - * Input Parameters: - * recv_cb - Receive callback function - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); - -/**************************************************************************** - * Name: esp_wifi_softap_register_txdone_cb - * - * Description: - * Register the softAP TX done callback function. - * - * Input Parameters: - * cb - The callback function - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_softap_register_txdone_cb(wifi_txdone_cb_t cb); - -/**************************************************************************** - * Name: esp_wifi_softap_read_mac - * - * Description: - * Read softAP interface MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_softap_read_mac(uint8_t *mac); - -/**************************************************************************** - * Name: esp_wifi_softap_password - * - * Description: - * Set/Get Wi-Fi SoftAP password - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_password(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_essid - * - * Description: - * Set/Get Wi-Fi SoftAP ESSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_essid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_bssid - * - * Description: - * Set/Get Wi-Fi softAP BSSID - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bssid(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_connect - * - * Description: - * Trigger Wi-Fi softAP accept connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_connect(void); - -/**************************************************************************** - * Name: esp_wifi_softap_disconnect - * - * Description: - * Trigger Wi-Fi softAP drop connection action - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_disconnect(void); - -/**************************************************************************** - * Name: esp_wifi_softap_mode - * - * Description: - * Set/Get Wi-Fi SoftAP mode code. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_mode(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_auth - * - * Description: - * Set/Get authentication mode params. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_auth(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_freq - * - * Description: - * Set/Get SoftAP frequency. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_freq(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_get_bitrate - * - * Description: - * Get SoftAP default bit rate (Mbps). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_bitrate(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_txpower - * - * Description: - * Get SoftAP transmit power (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_txpower(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_channel - * - * Description: - * Get SoftAP range of channel parameters. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_channel(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_country - * - * Description: - * Configure country info. - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_country(struct iwreq *iwr, bool set); - -/**************************************************************************** - * Name: esp_wifi_softap_rssi - * - * Description: - * Get Wi-Fi sensitivity (dBm). - * - * Input Parameters: - * iwr - The argument of the ioctl cmd - * set - true: set data; false: get data - * - * Returned Value: - * OK on success (positive non-zero values are cmd-specific) - * Negated errno returned on failure. - * - ****************************************************************************/ - -int esp_wifi_softap_rssi(struct iwreq *iwr, bool set); - /**************************************************************************** * Name: esp_wifi_bt_coexist_init * @@ -783,22 +65,6 @@ int esp_wifi_softap_rssi(struct iwreq *iwr, bool set); int esp_wifi_bt_coexist_init(void); #endif -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_wifi_stop_callback(void); - #ifdef __cplusplus } #endif