From 1f7c3a32e54833d0fb3e5817983ce50602b418bb Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Thu, 21 Aug 2025 16:10:14 -0300 Subject: [PATCH] arch/risc-v: refactor Wi-Fi driver for ESP32-C3|C6 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/risc-v/src/common/espressif/Kconfig | 123 +- arch/risc-v/src/common/espressif/Make.defs | 7 +- .../src/common/espressif/esp_wifi_api.c | 2126 ++++++++++++++++ .../espressif/esp_wifi_api.h} | 416 +-- .../common/espressif/esp_wifi_event_handler.c | 357 +++ .../src/common/espressif/esp_wifi_init.c | 467 ---- .../src/common/espressif/esp_wifi_utils.c | 562 +--- .../src/common/espressif/esp_wifi_utils.h | 141 +- arch/risc-v/src/common/espressif/esp_wlan.c | 1772 ------------- .../src/common/espressif/esp_wlan_netdev.c | 1402 ++++++++++ .../{esp_wlan.h => esp_wlan_netdev.h} | 169 +- arch/risc-v/src/esp32c3/esp_coex_adapter.c | 13 +- arch/risc-v/src/esp32c3/esp_coex_adapter.h | 82 + arch/risc-v/src/esp32c3/esp_wifi_adapter.c | 2264 +---------------- arch/risc-v/src/esp32c6/esp_coex_adapter.c | 8 +- arch/risc-v/src/esp32c6/esp_wifi_adapter.c | 2224 +--------------- arch/risc-v/src/esp32c6/esp_wifi_adapter.h | 617 ----- 17 files changed, 4591 insertions(+), 8159 deletions(-) create mode 100644 arch/risc-v/src/common/espressif/esp_wifi_api.c rename arch/risc-v/src/{esp32c3/esp_wifi_adapter.h => common/espressif/esp_wifi_api.h} (68%) create mode 100644 arch/risc-v/src/common/espressif/esp_wifi_event_handler.c delete mode 100644 arch/risc-v/src/common/espressif/esp_wifi_init.c delete mode 100644 arch/risc-v/src/common/espressif/esp_wlan.c create mode 100644 arch/risc-v/src/common/espressif/esp_wlan_netdev.c rename arch/risc-v/src/common/espressif/{esp_wlan.h => esp_wlan_netdev.h} (67%) create mode 100644 arch/risc-v/src/esp32c3/esp_coex_adapter.h diff --git a/arch/risc-v/src/common/espressif/Kconfig b/arch/risc-v/src/common/espressif/Kconfig index b0d9c15c62..7b4349e102 100644 --- a/arch/risc-v/src/common/espressif/Kconfig +++ b/arch/risc-v/src/common/espressif/Kconfig @@ -1051,6 +1051,35 @@ config ESPRESSIF_WIFI_STATION_SOFTAP 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 @@ -1088,6 +1117,29 @@ config ESPRESSIF_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. +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 if !ESPRESSIF_ESP32C6 @@ -1120,6 +1172,53 @@ config ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM 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 @@ -1133,6 +1232,14 @@ config ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM 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 @@ -1197,10 +1304,10 @@ config ESPRESSIF_WIFI_STA_DISCONNECT_PM Select this option to enable power management for station when disconnected. Chip will do modem-sleep when RF module is not in use anymore. -choice ESP_POWER_SAVE_MODE +choice ESPRESSIF_POWER_SAVE_MODE prompt "Wi-Fi Power save mode" - default ESP_POWER_SAVE_MIN_MODEM if ESPRESSIF_WIFI_BT_COEXIST - default ESP_POWER_SAVE_NONE + default ESPRESSIF_POWER_SAVE_MIN_MODEM if ESPRESSIF_WIFI_BT_COEXIST + default ESPRESSIF_POWER_SAVE_NONE ---help--- Wi-Fi supports the Modem-sleep mode which refers to the legacy power-saving mode in the IEEE 802.11 protocol. Modem-sleep mode works in station-only mode and the station must connect to the AP first. If the Modem-sleep @@ -1218,18 +1325,18 @@ choice ESP_POWER_SAVE_MODE state at DTIM time. If listen interval is longer, more power is saved, but broadcast data is more easy to lose. Listen interval can be configured by setting ESPRESSIF_WIFI_LISTEN_INTERVAL. - ESP_POWER_SAVE_NONE disables Modem-sleep mode entirely. Disabling it increases power consumption, but + ESPRESSIF_POWER_SAVE_NONE disables Modem-sleep mode entirely. Disabling it increases power consumption, but minimizes the delay in receiving Wi-Fi data in real time. When Modem-sleep mode is enabled, the delay in receiving Wi-Fi data may be the same as the DTIM cycle (minimum power-saving mode) or the listening interval - (maximum power-saving mode). Setting ESP_POWER_SAVE_NONE is suitable when high throughput is required. + (maximum power-saving mode). Setting ESPRESSIF_POWER_SAVE_NONE is suitable when high throughput is required. -config ESP_POWER_SAVE_NONE +config ESPRESSIF_POWER_SAVE_NONE bool "No power save" -config ESP_POWER_SAVE_MIN_MODEM +config ESPRESSIF_POWER_SAVE_MIN_MODEM bool "Minimum modem power saving." -config ESP_POWER_SAVE_MAX_MODEM +config ESPRESSIF_POWER_SAVE_MAX_MODEM bool "Maximum modem power saving" endchoice # ESP_POWER_SAVE_MODE diff --git a/arch/risc-v/src/common/espressif/Make.defs b/arch/risc-v/src/common/espressif/Make.defs index aa6ba9055e..7f637b54be 100644 --- a/arch/risc-v/src/common/espressif/Make.defs +++ b/arch/risc-v/src/common/espressif/Make.defs @@ -152,8 +152,9 @@ endif ifeq ($(CONFIG_ESP_WIRELESS),y) ifeq ($(CONFIG_ESPRESSIF_WIFI),y) - CHIP_CSRCS += esp_wifi_init.c - CHIP_CSRCS += esp_wlan.c + CHIP_CSRCS += esp_wifi_event_handler.c + CHIP_CSRCS += esp_wifi_api.c + CHIP_CSRCS += esp_wlan_netdev.c endif CHIP_CSRCS += esp_wifi_utils.c endif @@ -186,7 +187,7 @@ endif ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty ifndef ESP_HAL_3RDPARTY_VERSION - ESP_HAL_3RDPARTY_VERSION = 98dad3d9b4607df319eca4a7baf50545cda0b1a2 + ESP_HAL_3RDPARTY_VERSION = 8d7f4177afafdb7941182d8997d3589b1152aec0 endif ifndef ESP_HAL_3RDPARTY_URL diff --git a/arch/risc-v/src/common/espressif/esp_wifi_api.c b/arch/risc-v/src/common/espressif/esp_wifi_api.c new file mode 100644 index 0000000000..24e8887074 --- /dev/null +++ b/arch/risc-v/src/common/espressif/esp_wifi_api.c @@ -0,0 +1,2126 @@ +/**************************************************************************** + * arch/risc-v/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) + { + 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) + { + 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) + { + 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. + * + * See possible wifi_mode_t values in esp_wifi.h. + * + * Input Parameters: + * start_mode - The Wi-Fi mode to start from 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) + { + 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) + { + 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) + { + 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) + { + 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. + * + * Input Parameters: + * stop_mode - The Wi-Fi mode to stop (from 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) + { + 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) + { + 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) + { + 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) + { + 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_restore + * + * Description: + * Restore WiFi stack persistent settings to default values. + * Make sure to call esp_wifi_set_config, mode after this. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_restore(void) +{ + int ret; + + ret = esp_wifi_restore(); + if (ret) + { + wlerr("Failed to restore Wi-Fi ret=%d\n", ret); + ret = esp_wifi_to_errno(ret); + return ret; + } + + 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); +} + +/**************************************************************************** + * Wi-FiStation 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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; +} + +#endif /* ESP_WLAN_HAS_STA */ + +/**************************************************************************** + * Name: esp_wifi_sta_get_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) +{ + 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 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_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) +{ + 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 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_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) +{ + 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 esp_wifi_to_errno(ret); + } + } + else + { + return -ENOSYS; + } + + return OK; +} + +/**************************************************************************** + * 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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_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; 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; 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; 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/risc-v/src/esp32c3/esp_wifi_adapter.h b/arch/risc-v/src/common/espressif/esp_wifi_api.h similarity index 68% rename from arch/risc-v/src/esp32c3/esp_wifi_adapter.h rename to arch/risc-v/src/common/espressif/esp_wifi_api.h index eaa6bea7ac..ceb482670b 100644 --- a/arch/risc-v/src/esp32c3/esp_wifi_adapter.h +++ b/arch/risc-v/src/common/espressif/esp_wifi_api.h @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/risc-v/src/esp32c3/esp_wifi_adapter.h + * arch/risc-v/src/common/espressif/esp_wifi_api.h * * SPDX-License-Identifier: Apache-2.0 * @@ -20,144 +20,230 @@ * ****************************************************************************/ -#ifndef __ARCH_RISCV_SRC_ESP32C3_ESP_WIFI_ADAPTER_H -#define __ARCH_RISCV_SRC_ESP32C3_ESP_WIFI_ADAPTER_H +#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WIFI_API_H +#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WIFI_API_H /**************************************************************************** * Included Files ****************************************************************************/ -#include +#include +#include +#include + #include -#include - -#include "esp_wlan.h" - -#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_ESP32C3 1 +#include "esp_wlan_netdev.h" /**************************************************************************** * Public Function Prototypes ****************************************************************************/ /**************************************************************************** - * Name: esp_wifi_adapter_init + * Name: esp_wifi_api_adapter_deinit * * Description: - * Initialize ESP32S3 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_notify_subscribe + * Name: esp_wifi_api_adapter_init * * Description: - * Enable event notification + * Initialize the Wi-Fi driver, control structure, buffers and Wi-Fi task. * * Input Parameters: - * pid - Task PID - * event - Signal event data pointer + * None. * * Returned Value: - * 0 if success or -1 if fail + * OK on success; Negated errno on failure. * ****************************************************************************/ -int esp_wifi_notify_subscribe(pid_t pid, struct sigevent *event); +int esp_wifi_api_adapter_init(void); + +/**************************************************************************** + * Name: esp_wifi_api_start + * + * Description: + * Start Wi-Fi station. This will start the proper Wi-Fi mode based on + * the AP/Station configuration. + * + * Warning: On NuttX, WAPI ifup cannot bring up only one of the modes if + * both are enabled on Kconfig. This is a hard limitation. + * + * Input Parameters: + * start_mode - The Wi-Fi mode to start (from wireless.h). + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_start(uint32_t start_mode); + +/**************************************************************************** + * 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. + * + * Input Parameters: + * stop_mode - The Wi-Fi mode to stop (from wireless.h). + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_stop(uint32_t stop_mode); + +/**************************************************************************** + * Name: esp_wifi_api_restore + * + * Description: + * Restore WiFi stack persistent settings to default values. + * Make sure to call esp_wifi_set_config, mode after this. + * + * Input Parameters: + * None. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ****************************************************************************/ + +int esp_wifi_api_restore(void); + +/**************************************************************************** + * 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_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 + * 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_set_password + * Name: esp_wifi_sta_read_mac * * Description: - * Set/Get Wi-Fi station password + * 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_sta_password + * + * Description: + * 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. * ****************************************************************************/ @@ -171,11 +257,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. * ****************************************************************************/ @@ -185,15 +270,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. * ****************************************************************************/ @@ -203,14 +287,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. * ****************************************************************************/ @@ -220,18 +303,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 @@ -244,8 +326,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. * ****************************************************************************/ @@ -273,15 +354,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. * ****************************************************************************/ @@ -298,33 +378,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. @@ -334,8 +414,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. * ****************************************************************************/ @@ -352,13 +431,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 * @@ -370,68 +450,53 @@ 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); + #endif /* ESP_WLAN_HAS_STA */ +/**************************************************************************** + * SoftAP functions + ****************************************************************************/ + #ifdef ESP_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); - -/**************************************************************************** - * 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 + * 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_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 * @@ -440,11 +505,10 @@ int esp_wifi_softap_send_data(void *pbuf, size_t len); * * 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. * ****************************************************************************/ @@ -461,8 +525,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. * ****************************************************************************/ @@ -472,7 +535,7 @@ 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 @@ -490,14 +553,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. * ****************************************************************************/ @@ -507,14 +569,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. * ****************************************************************************/ @@ -531,8 +592,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. * ****************************************************************************/ @@ -542,7 +602,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 @@ -575,7 +635,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). @@ -585,8 +645,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. * ****************************************************************************/ @@ -603,8 +662,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. * ****************************************************************************/ @@ -621,8 +679,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. * ****************************************************************************/ @@ -639,8 +696,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. * ****************************************************************************/ @@ -657,18 +713,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); + #endif /* ESP_WLAN_HAS_SOFTAP */ -#ifdef __cplusplus -} -#endif -#undef EXTERN - -#endif /* __ASSEMBLY__ */ -#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP_WIFI_ADAPTER_H */ +#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WIFI_API_H */ diff --git a/arch/risc-v/src/common/espressif/esp_wifi_event_handler.c b/arch/risc-v/src/common/espressif/esp_wifi_event_handler.c new file mode 100644 index 0000000000..fd04de4560 --- /dev/null +++ b/arch/risc-v/src/common/espressif/esp_wifi_event_handler.c @@ -0,0 +1,357 @@ +/**************************************************************************** + * arch/risc-v/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: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +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"); + } +} + +/**************************************************************************** + * 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) + { + 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) + { + 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) + { + 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) + { + 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/risc-v/src/common/espressif/esp_wifi_init.c b/arch/risc-v/src/common/espressif/esp_wifi_init.c deleted file mode 100644 index 315522f135..0000000000 --- a/arch/risc-v/src/common/espressif/esp_wifi_init.c +++ /dev/null @@ -1,467 +0,0 @@ -/**************************************************************************** - * arch/risc-v/src/common/espressif/esp_wifi_init.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 args 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 "esp_log.h" -#include "esp_private/wifi.h" -#include "esp_private/adc_share_hw_ctrl.h" -#include "esp_private/sleep_modem.h" -#include "esp_sleep.h" -#include "esp_private/esp_clk.h" -#include "esp_wpa.h" -#include "private/esp_coexist_internal.h" -#include "esp_phy_init.h" -#include "esp_private/phy.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#if (CONFIG_ESPRESSIF_WIFI_RX_BA_WIN > CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM) -#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!" -#endif - -#if (CONFIG_ESPRESSIF_WIFI_RX_BA_WIN > (CONFIG_ESPRESSIF_WIFI_STATIC_RX_BUFFER_NUM << 1)) -#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than double of the WIFI_STATIC_RX_BUFFER_NUM!" -#endif - -#if SOC_PM_SUPPORT_PMU_MODEM_STATE -# define WIFI_BEACON_MONITOR_CONFIG_DEFAULT(ena) { \ - .enable = (ena), \ - .loss_timeout = CONFIG_ESP_WIFI_SLP_BEACON_LOST_TIMEOUT, \ - .loss_threshold = CONFIG_ESP_WIFI_SLP_BEACON_LOST_THRESHOLD, \ - .delta_intr_early = 0, \ - .delta_loss_timeout = 0, \ - .beacon_abort = 1, \ - .broadcast_wakeup = 1, \ - .tsf_time_sync_deviation = 5, \ - .modem_state_consecutive = 10, \ - .rf_ctrl_wait_cycle = 20 \ -} -#else -# define WIFI_BEACON_MONITOR_CONFIG_DEFAULT(ena) { \ - .enable = (ena), \ - .loss_timeout = CONFIG_ESP_WIFI_SLP_BEACON_LOST_TIMEOUT, \ - .loss_threshold = CONFIG_ESP_WIFI_SLP_BEACON_LOST_THRESHOLD, \ - .delta_intr_early = CONFIG_ESP_WIFI_SLP_PHY_ON_DELTA_EARLY_TIME, \ - .delta_loss_timeout = CONFIG_ESP_WIFI_SLP_PHY_OFF_DELTA_TIMEOUT_TIME \ -} -#endif - -/* Set additional WiFi features and capabilities */ - -uint64_t g_wifi_feature_caps = -#if CONFIG_ESP_WIFI_ENABLE_WPA3_SAE - CONFIG_FEATURE_WPA3_SAE_BIT | -#endif -#if CONFIG_SPIRAM - CONFIG_FEATURE_CACHE_TX_BUF_BIT | -#endif -#if CONFIG_ESP_WIFI_FTM_INITIATOR_SUPPORT - CONFIG_FEATURE_FTM_INITIATOR_BIT | -#endif -#if CONFIG_ESP_WIFI_FTM_RESPONDER_SUPPORT - CONFIG_FEATURE_FTM_RESPONDER_BIT | -#endif - 0; - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -static void esp_wifi_set_log_level(void); -static void esp_wifi_config_info(void); - -extern uint8_t esp_wifi_get_user_init_flag_internal(void); - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -ESP_EVENT_DEFINE_BASE(WIFI_EVENT); - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: esp_wifi_set_log_level - * - * Description: - * Sets the log level for the ESP32 WiFi module based on preprocessor - * definitions. The log level can be verbose, warning, or error. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void esp_wifi_set_log_level(void) -{ - wifi_log_level_t wifi_log_level = WIFI_LOG_NONE; - - /* set WiFi log level */ - -#if defined(CONFIG_DEBUG_WIRELESS_INFO) - wifi_log_level = WIFI_LOG_VERBOSE; -#elif defined(CONFIG_DEBUG_WIRELESS_WARN) - wifi_log_level = WIFI_LOG_WARNING; -#elif defined(CONFIG_LOG_MAXIMUM_LEVEL) - wifi_log_level = WIFI_LOG_ERROR; -#endif - - esp_wifi_internal_set_log_level(wifi_log_level); -} - -/**************************************************************************** - * Name: esp_wifi_config_info - * - * Description: - * This function logs the current configuration settings for the Wi-Fi - * module. It checks for various configuration options and logs if they - * are enabled. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static void esp_wifi_config_info(void) -{ -#ifdef CONFIG_ESPRESSIF_WIFI_RX_BA_WIN - wlinfo("rx ba win: %d", CONFIG_ESPRESSIF_WIFI_RX_BA_WIN); -#endif - -#ifdef CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP - wlinfo("WiFi/LWIP prefer SPIRAM"); -#endif - -#ifdef CONFIG_ESP_WIFI_IRAM_OPT - wlinfo("WiFi IRAM OP enabled"); -#endif - -#ifdef CONFIG_ESP_WIFI_RX_IRAM_OPT - wlinfo("WiFi RX IRAM OP enabled"); -#endif - -#ifdef CONFIG_ESP_WIFI_SLP_IRAM_OPT - wlinfo("WiFi SLP IRAM OP enabled"); -#endif - -#ifdef CONFIG_LWIP_IRAM_OPTIMIZATION - wlinfo("LWIP IRAM OP enabled"); -#endif -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -#ifndef CONFIG_ESP_WIFI_FTM_ENABLE - -/**************************************************************************** - * Name: ieee80211_ftm_attach - * - * Description: - * This function initializes and attaches the Fine Timing Measurement (FTM) - * capabilities to the IEEE 802.11 Wi-Fi driver. FTM is used for precise - * distance measurements between Wi-Fi devices. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void ieee80211_ftm_attach(void) -{ - /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */ -} -#endif - -#ifndef CONFIG_ESP_WIFI_SOFTAP_SUPPORT - -/**************************************************************************** - * Name: net80211_softap_funcs_init - * - * Description: - * This function is a placeholder for initializing the SoftAP (Software - * Access Point) functionalities of the IEEE 802.11 Wi-Fi driver. It is - * only compiled if the CONFIG_ESP_WIFI_SOFTAP_SUPPORT configuration - * option is not set. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void net80211_softap_funcs_init(void) -{ -} -#endif - -#ifndef CONFIG_ESP_WIFI_NAN_ENABLE - -/**************************************************************************** - * Name: nan_start - * - * Description: - * This function is a stub to overwrite a weak link in the Wi-Fi library. - * It is used to start the NAN (Neighbor Awareness Networking) service. - * - * Input Parameters: - * None - * - * Returned Value: - * Always returns ESP_OK. - * - ****************************************************************************/ - -esp_err_t nan_start(void) -{ - /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */ - - return ESP_OK; -} - -/**************************************************************************** - * Name: nan_stop - * - * Description: - * This function is a stub to overwrite a weak link in the Wi-Fi library. - * It is used to stop the NAN (Neighbor Awareness Networking) service. - * - * Input Parameters: - * None - * - * Returned Value: - * Always returns ESP_OK. - * - ****************************************************************************/ - -esp_err_t nan_stop(void) -{ - /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */ - - return ESP_OK; -} - -/**************************************************************************** - * Name: nan_input - * - * Description: - * This function is a stub to overwrite a weak link in the Wi-Fi library. - * It is used to handle input for the NAN (Neighbor Awareness Networking) - * service. - * - * Input Parameters: - * p1 - First parameter for the input function. - * p2 - Second parameter for the input function. - * p3 - Third parameter for the input function. - * - * Returned Value: - * Always returns 0. - * - ****************************************************************************/ - -int nan_input(void *p1, int p2, int p3) -{ - /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */ - - return 0; -} - -/**************************************************************************** - * Name: nan_sm_handle_event - * - * Description: - * This function is a stub to overwrite a weak link in the Wi-Fi library. - * It is used to handle events for the NAN (Neighbor Awareness Networking) - * service state machine. - * - * Input Parameters: - * p1 - First parameter for the event handler. - * p2 - Second parameter for the event handler. - * - * Returned Value: - * None - * - ****************************************************************************/ - -void nan_sm_handle_event(void *p1, int p2) -{ - /* Do not remove, stub to overwrite weak link in Wi-Fi Lib */ -} -#endif - -/**************************************************************************** - * Name: esp_wifi_deinit - * - * Description: - * Deinitialize Wi-Fi and free resource - * - * Input Parameters: - * None - * - * Returned Values: esp_err_t - * Zero (OK) is returned or a negative error. - * - ****************************************************************************/ - -esp_err_t esp_wifi_deinit(void) -{ - esp_err_t err = ESP_OK; -#ifdef CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT - wifi_beacon_monitor_config_t monitor_config; -#endif - - if (esp_wifi_get_user_init_flag_internal()) - { - wlerr("Wi-Fi not stop"); - return ESP_ERR_WIFI_NOT_STOPPED; - } - - if (esp_wifi_internal_reg_rxcb(WIFI_IF_STA, NULL) != ESP_OK || - esp_wifi_internal_reg_rxcb(WIFI_IF_AP, NULL) != ESP_OK) - { - wlerr("Failed to unregister Rx callbacks"); - } - - esp_supplicant_deinit(); - err = esp_wifi_deinit_internal(); - if (err != ESP_OK) - { - wlerr("Failed to deinit Wi-Fi driver (0x%x)", err); - return err; - } - -#ifdef CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT - monitor_config = WIFI_BEACON_MONITOR_CONFIG_DEFAULT(false); - esp_wifi_beacon_monitor_configure(&monitor_config); -#endif - -#if CONFIG_MAC_BB_PD - esp_unregister_mac_bb_pd_callback(pm_mac_sleep); - esp_unregister_mac_bb_pu_callback(pm_mac_wakeup); -#endif - esp_wifi_power_domain_off(); -#if CONFIG_MAC_BB_PD - esp_wifi_internal_set_mac_sleep(false); - esp_mac_bb_pd_mem_deinit(); -#endif - esp_phy_modem_deinit(); - - return err; -} - -/**************************************************************************** - * Name: esp_wifi_init - * - * Description: - * Initialize Wi-Fi - * - * Input Parameters: - * config - Initialization config parameters - * - * Returned Values: esp_err_t - * Zero (OK) is returned or a negative error. - * - ****************************************************************************/ - -esp_err_t esp_wifi_init(const wifi_init_config_t *config) -{ - esp_err_t result = ESP_OK; -#ifdef CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT - wifi_beacon_monitor_config_t monitor_config; -#endif - - if ((config->feature_caps & CONFIG_FEATURE_CACHE_TX_BUF_BIT) && - (WIFI_CACHE_TX_BUFFER_NUM == 0)) - { - wlerr("Number of WiFi cache TX buffers should not equal 0 when" - "enable SPIRAM"); - return ESP_ERR_NOT_SUPPORTED; - } - - esp_wifi_power_domain_on(); - -#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE - coex_init(); -#endif - esp_wifi_set_log_level(); - - result = esp_wifi_init_internal(config); - if (result == ESP_OK) - { -#if CONFIG_MAC_BB_PD - esp_mac_bb_pd_mem_init(); - esp_wifi_internal_set_mac_sleep(true); -#endif - esp_phy_modem_init(); - - result = esp_supplicant_init(); - if (result != ESP_OK) - { - esp_err_t deinit_ret; - - wlerr("Failed to init supplicant (0x%x)", result); - - deinit_ret = esp_wifi_deinit(); - if (deinit_ret != ESP_OK) - { - wlerr("Failed to deinit Wi-Fi (0x%x)", deinit_ret); - } - - return result; - } - } - -#if CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT - monitor_config = WIFI_BEACON_MONITOR_CONFIG_DEFAULT(true); - esp_wifi_beacon_monitor_configure(&monitor_config); -#endif - adc2_cal_include(); /* This enables the ADC2 calibration constructor at start up */ - - esp_wifi_config_info(); - - return result; -} diff --git a/arch/risc-v/src/common/espressif/esp_wifi_utils.c b/arch/risc-v/src/common/espressif/esp_wifi_utils.c index f24a448e8d..c635a59a94 100644 --- a/arch/risc-v/src/common/espressif/esp_wifi_utils.c +++ b/arch/risc-v/src/common/espressif/esp_wifi_utils.c @@ -34,24 +34,13 @@ #include #include -#include -#ifdef CONFIG_ESPRESSIF_WIFI -#include "esp_wifi_adapter.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 "rom/ets_sys.h" -#include "soc/soc_caps.h" -#endif /* CONFIG_ESPRESSIF_WIFI */ - -#include "esp_err.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" /**************************************************************************** @@ -75,18 +64,6 @@ #define CHANNEL_MAX_NUM (14) -/* CONFIG_POWER_SAVE_MODEM */ - -#if defined(CONFIG_ESP_POWER_SAVE_MIN_MODEM) -# define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM -#elif defined(CONFIG_ESP_POWER_SAVE_MAX_MODEM) -# define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM -#elif defined(CONFIG_ESP_POWER_SAVE_NONE) -# define DEFAULT_PS_MODE WIFI_PS_NONE -#else -# define DEFAULT_PS_MODE WIFI_PS_NONE -#endif - /**************************************************************************** * Private Types ****************************************************************************/ @@ -112,20 +89,6 @@ struct wifi_scan_result * Public Data ****************************************************************************/ -/* Wi-Fi interface configuration */ - -#ifdef ESP_WLAN_HAS_STA - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_WLAN_HAS_SOFTAP - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESP_WLAN_HAS_SOFTAP */ - /**************************************************************************** * Private Data ****************************************************************************/ @@ -137,11 +100,7 @@ static struct wifi_scan_result g_scan_priv = static uint8_t g_channel_num; static uint8_t g_channel_list[CHANNEL_MAX_NUM]; -static struct wifi_notify g_wifi_notify[WIFI_ADPT_EVT_MAX]; -static struct work_s g_wifi_evt_work; -static sq_queue_t g_wifi_evt_queue; static mutex_t g_wifiexcl_lock = NXMUTEX_INITIALIZER; -static spinlock_t g_lock; /**************************************************************************** * Public Functions @@ -149,6 +108,99 @@ static spinlock_t g_lock; #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 * @@ -172,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) { @@ -251,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) { @@ -296,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) @@ -400,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. * ****************************************************************************/ @@ -495,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) { @@ -624,10 +676,10 @@ scan_result_full: * Name: esp_wifi_to_errno * * Description: - * Transform from ESP Wi-Fi error code to NuttX error code + * Transform from ESP Wi-Fi error code to NuttX error code. * * Input Parameters: - * err - ESP Wi-Fi error code + * err - ESP Wi-Fi error code. * * Returned Value: * NuttX error code defined in errno.h @@ -699,77 +751,6 @@ int32_t esp_wifi_to_errno(int err) return ret; } -/**************************************************************************** - * 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 - * - ****************************************************************************/ - -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 ESP_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 /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_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 /* ESP_WLAN_HAS_SOFTAP */ - - default: - return -1; - } - - return id; -} - /**************************************************************************** * Name: esp_wifi_lock * @@ -807,330 +788,3 @@ int esp_wifi_lock(bool lock) return ret; } - -/**************************************************************************** - * Name: esp_evt_work_cb - * - * Description: - * Process the cached event - * - * Input Parameters: - * arg - No mean - * - * Returned Value: - * None - * - ****************************************************************************/ - -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) - { - 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) - { - case WIFI_ADPT_EVT_SCAN_DONE: -#ifdef CONFIG_ESPRESSIF_WIFI - esp_wifi_scan_event_parse(); -#endif - break; - -#ifdef ESP_WLAN_HAS_STA - case WIFI_ADPT_EVT_STA_START: - wlinfo("Wi-Fi sta start\n"); - - g_sta_connected = false; - - 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); - } - 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"); - } - - 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); - } - } - break; - - case WIFI_ADPT_EVT_STA_STOP: - wlinfo("Wi-Fi sta stop\n"); - g_sta_connected = false; - break; -#endif /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_WLAN_HAS_SOFTAP - case WIFI_ADPT_EVT_AP_START: - wlinfo("INFO: Wi-Fi softap start\n"); - - 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); - } - break; - - case WIFI_ADPT_EVT_AP_STOP: - wlinfo("INFO: Wi-Fi softap stop\n"); - break; - - case WIFI_ADPT_EVT_AP_STACONNECTED: - wlinfo("INFO: Wi-Fi station join\n"); - break; - - case WIFI_ADPT_EVT_AP_STADISCONNECTED: - wlinfo("INFO: Wi-Fi station leave\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=%ld failed: %d\n", - evt_adpt->id, ret); - } - } - - esp_wifi_lock(false); - net_unlock(); - - kmm_free(evt_adpt); - } -} - -/**************************************************************************** - * 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); - - id = esp_event_id_map(event_id); - if (id < 0) - { - wlerr("ERROR: No process event %ld\n", event_id); - return -1; - } - - size = event_data_size + sizeof(struct evt_adpt); - evt_adpt = kmm_malloc(size); - if (!evt_adpt) - { - wlerr("ERROR: Failed to alloc %d memory\n", size); - return -1; - } - - evt_adpt->id = 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_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/risc-v/src/common/espressif/esp_wifi_utils.h b/arch/risc-v/src/common/espressif/esp_wifi_utils.h index 7a93b55a94..9363f492da 100644 --- a/arch/risc-v/src/common/espressif/esp_wifi_utils.h +++ b/arch/risc-v/src/common/espressif/esp_wifi_utils.h @@ -31,6 +31,8 @@ #include #include +#include "esp_wifi_types_generic.h" + #ifndef __ASSEMBLY__ #include @@ -44,41 +46,32 @@ extern "C" #define EXTERN extern #endif -/* Wi-Fi event ID */ +#define MAC_LEN (6) -enum wifi_adpt_evt_e +/**************************************************************************** + * 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) { - 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 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 */ -}; + return ret >= 0; +} +#endif /**************************************************************************** * Public Function Prototypes @@ -86,6 +79,38 @@ struct wifi_notify #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 * @@ -124,13 +149,13 @@ 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. * ****************************************************************************/ @@ -155,20 +180,21 @@ void esp_wifi_scan_event_parse(void); int32_t esp_wifi_to_errno(int err); /**************************************************************************** - * Name: esp_event_id_map + * Name: esp_wifi_mode_translate * * Description: - * Transform from esp-idf event ID to Wi-Fi adapter event ID + * Translate wireless mode constants to ESP Wi-Fi mode constants. * * Input Parameters: - * event_id - esp-idf event ID + * wireless_mode - Wireless mode from wireless.h (IW_MODE_*) * * Returned Value: - * Wi-Fi adapter event ID + * ESP Wi-Fi mode (WIFI_MODE_*) on success + * -EINVAL on failure * ****************************************************************************/ -int esp_event_id_map(int event_id); +wifi_mode_t esp_wifi_mode_translate(uint32_t wireless_mode); /**************************************************************************** * Name: esp_wifi_lock @@ -186,22 +212,6 @@ int esp_event_id_map(int event_id); int esp_wifi_lock(bool lock); -/**************************************************************************** - * Name: esp_evt_work_cb - * - * Description: - * Process the cached event - * - * Input Parameters: - * arg - No mean - * - * Returned Value: - * None - * - ****************************************************************************/ - -void esp_evt_work_cb(void *arg); - /**************************************************************************** * Name: esp_event_post * @@ -235,23 +245,6 @@ int esp_event_post(const char *event_base, size_t event_data_size, uint32_t ticks); -/**************************************************************************** - * 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); - #ifdef __cplusplus } #endif diff --git a/arch/risc-v/src/common/espressif/esp_wlan.c b/arch/risc-v/src/common/espressif/esp_wlan.c deleted file mode 100644 index b62bcf5bd3..0000000000 --- a/arch/risc-v/src/common/espressif/esp_wlan.c +++ /dev/null @@ -1,1772 +0,0 @@ -/**************************************************************************** - * arch/risc-v/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 -#include - -#if defined(CONFIG_NET_PKT) -# include -#endif - -#include "esp_systemreset.h" -#include "esp_wlan.h" -#include "esp_wifi_utils.h" -#include "esp_wifi_adapter.h" - -#include "esp_attr.h" -#include "esp_mac.h" -#include "esp_private/wifi.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* 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 + \ - CONFIG_NET_LL_GUARDSIZE + \ - CONFIG_NET_GUARDSIZE) - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/* 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; - - /* RX packet queue */ - - struct iob_queue_s rxb; - - /* TX ready packet queue */ - - struct iob_queue_s txb; - - /* Flat buffer swap */ - - uint8_t flatbuf[WLAN_BUF_SIZE]; - - spinlock_t lock; -}; - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -#ifdef ESP_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 /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_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 /* ESP_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[ESP_WLAN_DEVS]; - -#ifdef ESP_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 /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_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 /* ESP_WLAN_HAS_SOFTAP */ - -/* Wi-Fi station TX done callback function */ - -static wifi_tx_done_cb_t g_sta_txdone_cb; - -/* Wi-Fi SoftAP TX done callback function */ - -static wifi_tx_done_cb_t g_softap_txdone_cb; - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/* Common TX logic */ - -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 void esp_wifi_stop_callback(void); -static void esp_wifi_free_eb(void *eb); -#ifdef ESP_WLAN_HAS_STA -static int esp_wifi_sta_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); -static void esp_wifi_sta_register_txdone_cb(wifi_tx_done_cb_t cb); -#endif /* ESP_WLAN_HAS_STA */ -#ifdef ESP_WLAN_HAS_SOFTAP -static int esp_wifi_softap_register_recv_cb(int (*recv_cb)(void *buffer, - uint16_t len, - void *eb)); -static void esp_wifi_softap_register_txdone_cb(wifi_tx_done_cb_t cb); -#endif /* ESP_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * 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_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) -{ - if (priv->dev.d_iob) - { - iob_tryadd_queue(priv->dev.d_iob, &priv->txb); - } - - netdev_iob_clear(&priv->dev); -} - -/**************************************************************************** - * 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 iob_s *wlan_recvframe(struct wlan_priv_s *priv) -{ - struct iob_s *iob; - irqstate_t flags; - - flags = spin_lock_irqsave(&priv->lock); - iob = iob_remove_queue(&priv->rxb); - spin_unlock_irqrestore(&priv->lock, flags); - - return iob; -} - -/**************************************************************************** - * 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) -{ - uint16_t llhdrlen = NET_LL_HDRLEN(&priv->dev); - unsigned int offset = CONFIG_NET_LL_GUARDSIZE - llhdrlen; - struct iob_s *iob; - int ret; - - while ((iob = iob_peek_queue(&priv->txb)) != NULL) - { - iob_copyout(priv->flatbuf + llhdrlen, iob, iob->io_pktlen, 0); - memcpy(priv->flatbuf, iob->io_data + offset, llhdrlen); - - ret = priv->ops->send(priv->flatbuf, iob->io_pktlen + llhdrlen); - if (ret == -ENOMEM) - { - 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); - } - - iob_remove_queue(&priv->txb); - - /* And free the I/O buffer chain */ - - iob_free_chain(iob); - } - } -} - -/**************************************************************************** - * 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 net_driver_s *dev = &priv->dev; - struct iob_s *iob = NULL; - 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; - } - - if (len > iob_navail(false) * CONFIG_IOB_BUFSIZE) - { - ret = -ENOBUFS; - goto out; - } - - iob = iob_tryalloc(false); - if (iob == NULL) - { - ret = -ENOBUFS; - goto out; - } - - iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE - NET_LL_HDRLEN(dev)); - - ret = iob_trycopyin(iob, buffer, len, 0, false); - if (ret != len) - { - ret = -ENOBUFS; - goto out; - } - - flags = spin_lock_irqsave(&priv->lock); - ret = iob_tryadd_queue(iob, &priv->rxb); - spin_unlock_irqrestore(&priv->lock, flags); - - if (ret < 0) - { - ret = -ENOBUFS; - goto out; - } - -out: - - if (eb != NULL) - { - esp_wifi_free_eb(eb); - } - - if (ret != OK && iob != NULL) - { - iob_free_chain(iob); - } - - if (work_available(&priv->rxwork)) - { - work_queue(WLAN_WORK, &priv->rxwork, wlan_rxpoll, priv, 0); - } - - wlan_txavail(&priv->dev); - - 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_priv_s *priv = (struct wlan_priv_s *)arg; - struct net_driver_s *dev = &priv->dev; - struct eth_hdr_s *eth_hdr; - struct iob_s *iob; - - /* Try to send all cached TX packets for TX ack and so on */ - - wlan_transmit(priv); - - /* Loop while while iob_remove_queue() successfully retrieves valid - * Ethernet frames. - */ - - net_lock(); - - while ((iob = wlan_recvframe(priv)) != NULL) - { - dev->d_iob = iob; - dev->d_len = iob->io_pktlen; - - iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE); - -#ifdef CONFIG_NET_PKT - - /* When packet sockets are enabled, - * feed the frame into the packet tap. - */ - - pkt_input(&priv->dev); -#endif - - eth_hdr = (struct eth_hdr_s *) - &dev->d_iob->io_data[CONFIG_NET_LL_GUARDSIZE - - NET_LL_HDRLEN(dev)]; - - /* 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); - } - - netdev_iob_release(&priv->dev); - } - - /* 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_priv_s *priv = dev->d_private; - - wlan_cache_txpkt_tail(priv); - wlan_transmit(priv); - - 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; - - /* Try to let TCP/IP to send all packets to netcard driver */ - - while (devif_poll(dev, wlan_txpoll)); - - /* 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: - * arg - The reference of the private driver structure - * - * 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; - } - - ret = priv->ops->start(); - if (ret < 0) - { - net_unlock(); - nerr("ERROR: Failed to start Wi-Fi ret=%d\n", ret); - return ret; - } - - IOB_QINIT(&priv->rxb); - IOB_QINIT(&priv->txb); - - priv->dev.d_buf = NULL; - priv->dev.d_len = 0; - - 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; - 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; - - iob_free_queue(&priv->rxb); - iob_free_queue(&priv->txb); - - 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 network driver. - * - * Input Parameters: - * devno - The device number. - * mac_addr - MAC address. - * ops - A pointer to the structure containing the WLAN operations - * functions. - * - * 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 ESP_WLAN_HAS_STA -static int wlan_sta_rx_done(void *buffer, uint16_t len, void *eb) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESP_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: - * 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_sta_tx_done(uint8_t ifidx, - uint8_t *data, - uint16_t *len, - bool status) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_STA_DEVNO]; - - wlan_tx_done(priv); -} -#endif /* ESP_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 ESP_WLAN_HAS_SOFTAP -static int wlan_softap_rx_done(void *buffer, uint16_t len, void *eb) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESP_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 ifidx, - uint8_t *data, - uint16_t *len, - bool status) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESP_WLAN_SOFTAP_DEVNO]; - - wlan_tx_done(priv); -} -#endif /* ESP_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * Name: esp_wifi_stop_callback - * - * Description: - * Callback to stop Wi-Fi. - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -static 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); - DEBUGPANIC(); - } -} - -/**************************************************************************** - * 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 - * - ****************************************************************************/ - -static void esp_wifi_free_eb(void *eb) -{ - esp_wifi_internal_free_rx_buffer(eb); -} - -/**************************************************************************** - * 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. - * - ****************************************************************************/ - -#ifdef ESP_WLAN_HAS_STA -static 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 esp_wifi_to_errno(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 - * - ****************************************************************************/ - -static void esp_wifi_sta_register_txdone_cb(wifi_tx_done_cb_t cb) -{ - g_sta_txdone_cb = cb; -} -#endif /* ESP_WLAN_HAS_STA */ - -/**************************************************************************** - * 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. - * - ****************************************************************************/ - -#ifdef ESP_WLAN_HAS_SOFTAP -static 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 esp_wifi_to_errno(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_tx_done_cb_t cb) -{ - g_softap_txdone_cb = cb; -} -#endif /* ESP_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 ESP_WLAN_HAS_STA -int esp_wlan_sta_set_linkstatus(bool linkstatus) -{ - struct wlan_priv_s *priv = &g_wlan_priv[ESP_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 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 mac[6]; - - ret = esp_wifi_adapter_init(); - if (ret < 0) - { - nerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret); - return ret; - } - - ret = esp_read_mac(mac, ESP_MAC_WIFI_STA); - 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", - mac[0], mac[1], mac[2], - mac[3], mac[4], mac[5]); - - ret = esp_net_initialize(ESP_WLAN_STA_DEVNO, 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 /* ESP_WLAN_HAS_STA */ - -/**************************************************************************** - * Name: esp_wlan_softap_initialize - * - * Description: - * Initialize the ESP32-S3 WLAN softAP netcard driver - * - * Input Parameters: - * None - * - * Returned Value: - * OK on success; Negated errno on failure. - * - ****************************************************************************/ - -#ifdef ESP_WLAN_HAS_SOFTAP -int esp_wlan_softap_initialize(void) -{ - int ret; - uint8_t mac[6]; - - ret = esp_wifi_adapter_init(); - if (ret < 0) - { - nerr("ERROR: Initialize Wi-Fi adapter error: %d\n", ret); - return ret; - } - - ret = esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP); - 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", - mac[0], mac[1], mac[2], - mac[3], mac[4], mac[5]); - - ret = esp_net_initialize(ESP_WLAN_SOFTAP_DEVNO, 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 /* ESP_WLAN_HAS_SOFTAP */ - -/**************************************************************************** - * 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 - * - ****************************************************************************/ - -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) - { - if (g_sta_txdone_cb) - { - g_sta_txdone_cb(ifidx, data, len, txstatus); - } - } - else -#endif /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_WLAN_HAS_SOFTAP - if (ifidx == ESP_IF_WIFI_AP) - { - if (g_softap_txdone_cb) - { - g_softap_txdone_cb(ifidx, data, len, txstatus); - } - } - else -#endif /* ESP_WLAN_HAS_SOFTAP */ - { - wlerr("ifidx=%d is error\n", ifidx); - } -} - -#endif /* CONFIG_ESPRESSIF_WIFI */ diff --git a/arch/risc-v/src/common/espressif/esp_wlan_netdev.c b/arch/risc-v/src/common/espressif/esp_wlan_netdev.c new file mode 100644 index 0000000000..4e35c64eeb --- /dev/null +++ b/arch/risc-v/src/common/espressif/esp_wlan_netdev.c @@ -0,0 +1,1402 @@ +/**************************************************************************** + * arch/risc-v/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) + { + 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) + { + 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 < 0) + { + wlerr("ERROR: Failed to copy packet\n"); + goto out; + } + + ret = netpkt_tryadd_queue(pkt, &priv->netdev_rx_queue); + if (ret < 0) + { + 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); + } + + 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 < 0) + { + 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 < 0) + { + 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 < 0) + { + 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 < 0) + { + 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 < 0) + { + 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 < 0) + { + 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/risc-v/src/common/espressif/esp_wlan.h b/arch/risc-v/src/common/espressif/esp_wlan_netdev.h similarity index 67% rename from arch/risc-v/src/common/espressif/esp_wlan.h rename to arch/risc-v/src/common/espressif/esp_wlan_netdev.h index 1cd84bc77b..1990ae8404 100644 --- a/arch/risc-v/src/common/espressif/esp_wlan.h +++ b/arch/risc-v/src/common/espressif/esp_wlan_netdev.h @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/risc-v/src/common/espressif/esp_wlan.h + * arch/risc-v/src/common/espressif/esp_wlan_netdev.h * * SPDX-License-Identifier: Apache-2.0 * @@ -20,8 +20,8 @@ * ****************************************************************************/ -#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_H -#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_H +#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_NETDEV_H +#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_NETDEV_H /**************************************************************************** * Included Files @@ -55,115 +55,118 @@ extern "C" #elif defined(CONFIG_ESPRESSIF_WIFI_STATION_SOFTAP) # 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 -#define MAC_LEN (6) - /**************************************************************************** * Public Data ****************************************************************************/ -#ifdef ESP_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 - -#ifdef ESP_WLAN_HAS_SOFTAP - -/* If Wi-Fi SoftAP starts */ - -extern volatile bool g_softap_started; - -#endif - -#ifdef CONFIG_ESPRESSIF_WIFI - -/**************************************************************************** - * Inline Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: nuttx_err_to_freertos - * - * Description: - * Transform from Nuttx OS error code to FreeRTOS's pdTRUE or pdFALSE. - * - * Input Parameters: - * ret - NuttX error code - * - * Returned Value: - * Wi-Fi adapter error code - * - ****************************************************************************/ - -static inline int32_t nuttx_err_to_freertos(int ret) -{ - return ret >= 0; -} - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ -#ifdef ESP_WLAN_HAS_STA - /**************************************************************************** - * 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-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 /* ESP_WLAN_HAS_STA */ +#endif /**************************************************************************** * Name: esp_wlan_softap_initialize * * Description: - * Initialize the ESP32-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. @@ -172,36 +175,12 @@ int esp_wlan_sta_initialize(void); #ifdef ESP_WLAN_HAS_SOFTAP int esp_wlan_softap_initialize(void); -#endif /* ESP_WLAN_HAS_SOFTAP */ +#endif -/**************************************************************************** - * 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 - * - ****************************************************************************/ - -void esp_wifi_tx_done_cb(uint8_t ifidx, - uint8_t *data, - uint16_t *len, - bool txstatus); - -#endif /* CONFIG_ESPRESSIF_WIFI */ #ifdef __cplusplus } #endif #undef EXTERN #endif /* __ASSEMBLY__ */ -#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_H */ +#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_WLAN_NETDEV_H */ diff --git a/arch/risc-v/src/esp32c3/esp_coex_adapter.c b/arch/risc-v/src/esp32c3/esp_coex_adapter.c index 36434859cb..b0f3bc72d7 100644 --- a/arch/risc-v/src/esp32c3/esp_coex_adapter.c +++ b/arch/risc-v/src/esp32c3/esp_coex_adapter.c @@ -37,7 +37,6 @@ #include #include "esp_hr_timer.h" -#include "esp_wlan.h" #include "esp_attr.h" #include "esp_timer.h" @@ -49,6 +48,10 @@ #include "soc/system_reg.h" #include "private/esp_modem_wrapper.h" +#include "espressif/esp_wifi_utils.h" + +#include "esp_coex_adapter.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -59,7 +62,7 @@ * Private Function Prototypes ****************************************************************************/ -static int64_t esp_coex_esp_timer_get_time_wrapper(void); +static IRAM_ATTR int64_t esp_coex_esp_timer_get_time_wrapper(void); static int32_t esp_coex_semphr_take_from_isr_wrapper(void *semphr, void *hptw); static int32_t esp_coex_semphr_give_from_isr_wrapper(void *semphr, @@ -137,7 +140,7 @@ static int32_t IRAM_ATTR esp_coex_semphr_take_from_isr_wrapper(void *semphr, { *(int *)hptw = 0; - return nuttx_err_to_freertos(nxsem_trywait(semphr)); + return nuttx_err_to_common_err(nxsem_trywait(semphr)); } /**************************************************************************** @@ -418,7 +421,7 @@ int32_t esp_coex_common_semphr_take_wrapper(void *semphr, block_time_tick, ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -449,7 +452,7 @@ int32_t esp_coex_common_semphr_give_wrapper(void *semphr) wlerr("Failed to post sem error=%d\n", ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** diff --git a/arch/risc-v/src/esp32c3/esp_coex_adapter.h b/arch/risc-v/src/esp32c3/esp_coex_adapter.h new file mode 100644 index 0000000000..4f67b33c6e --- /dev/null +++ b/arch/risc-v/src/esp32c3/esp_coex_adapter.h @@ -0,0 +1,82 @@ +/**************************************************************************** + * arch/risc-v/src/esp32c3/esp_coex_adapter.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 + * 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. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_ESP32C3_ESP_COEX_ADAPTER_H +#define __ARCH_RISCV_SRC_ESP32C3_ESP_COEX_ADAPTER_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include "esp_attr.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void); +void *esp_coex_common_spin_lock_create_wrapper(void); +uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux); +void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux, + uint32_t tmp); +void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void); +void *esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init); +void esp_coex_common_semphr_delete_wrapper(void *semphr); +int32_t esp_coex_common_semphr_take_wrapper(void *semphr, + uint32_t block_time_tick); +int32_t esp_coex_common_semphr_give_wrapper(void *semphr); +void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer); +void esp_coex_common_timer_done_wrapper(void *ptimer); +void esp_coex_common_timer_setfn_wrapper(void *ptimer, + void *pfunction, + void *parg); +void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, + uint32_t us, + bool repeat); +IRAM_ATTR void *esp_coex_common_malloc_internal_wrapper(size_t size); +uint32_t esp_coex_common_clk_slowclk_cal_get_wrapper(void); + +#ifdef __cplusplus +} +#endif +#undef EXTERN + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP_COEX_ADAPTER_H */ diff --git a/arch/risc-v/src/esp32c3/esp_wifi_adapter.c b/arch/risc-v/src/esp32c3/esp_wifi_adapter.c index 8e176446a7..f1a1286ac7 100644 --- a/arch/risc-v/src/esp32c3/esp_wifi_adapter.c +++ b/arch/risc-v/src/esp32c3/esp_wifi_adapter.c @@ -25,88 +25,37 @@ ****************************************************************************/ #include -#include -#include -#include -#include -#include #include #include -#include -#include -#include #include -#include #include -#include -#include #include #include #include -#include #include -#include #include -#include -#include -#include -#include -#include -#include #include #include "esp_irq.h" #include "esp_hr_timer.h" -#include "esp_types.h" -#include "esp_random.h" -#include "esp_mac.h" -#include "esp_intr_alloc.h" -#include "esp_attr.h" #include "esp_log.h" -#include "esp_event.h" -#include "esp_timer.h" +#include "esp_mac.h" +#include "esp_random.h" #include "esp_private/wifi_os_adapter.h" -#include "esp_private/wifi.h" #include "esp_phy_init.h" -#include "soc/rtc_cntl_reg.h" -#include "soc/syscon_reg.h" -#include "soc/system_reg.h" #include "esp_private/periph_ctrl.h" -#include "esp_private/esp_clk.h" #include "os.h" -#include "esp_smartconfig.h" #include "private/esp_coexist_internal.h" #include "rom/ets_sys.h" -#include "private/esp_modem_wrapper.h" -#include "esp_wlan.h" -#include "esp_wifi_adapter.h" -#include "esp_wifi_utils.h" +#include "espressif/esp_wifi_utils.h" +#include "esp_coex_adapter.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -#define MHZ (1000000) - -#define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE)) - -#define WIFI_CONNECT_TIMEOUT CONFIG_ESPRESSIF_WIFI_CONNECT_TIMEOUT - -#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) @@ -115,17 +64,6 @@ * 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 @@ -143,25 +81,6 @@ struct mq_adpt char name[16]; /* Message queue name */ }; -/* Wi-Fi time private data */ - -struct time_adpt -{ - time_t sec; /* Second value */ - suseconds_t usec; /* Micro second value */ -}; - -/* Wi-Fi NVS private data */ - -struct nvs_adpt -{ - char *index_name; -}; - -/* Wi-Fi event callback function */ - -typedef void (*wifi_evt_cb_t)(void *p); - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -292,7 +211,6 @@ static void esp_empty_wrapper(void); * of functions or software adapters for the Wi-Fi driver */ -static int esp_freq_to_channel(uint16_t freq); static uint32_t esp_get_free_heap_size(void); static void *event_group_create_wrapper(void); static void event_group_delete_wrapper(void *event); @@ -323,8 +241,6 @@ static int esp_nvs_get_u16(uint32_t handle, static int esp_nvs_set_u16(uint32_t handle, const char *key, uint16_t value); static void esp_nvs_close(uint32_t handle); static void esp_update_time(struct timespec *timespec, uint32_t ticks); -static int esp_wifi_auth_trans(uint32_t wifi_auth); -static int esp_wifi_cipher_trans(uint32_t wifi_cipher); static uint32_t queue_msg_waiting_wrapper(void *queue); static void task_delay_wrapper(uint32_t tick); static void task_delete_wrapper(void *task_handle); @@ -347,34 +263,6 @@ extern void wifi_apb80m_release(void); * Private Data ****************************************************************************/ -/* Wi-Fi event private data */ - -static sq_queue_t g_wifi_evt_queue; - -/* Wi-Fi adapter reference */ - -static int g_wifi_ref; - -#ifdef ESP_WLAN_HAS_STA - -/* Wi-Fi interface configuration */ - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_WLAN_HAS_SOFTAP - -/* Wi-Fi interface configuration */ - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESP_WLAN_HAS_SOFTAP */ - -/* Device specific lock */ - -static spinlock_t g_lock; - /**************************************************************************** * Public Data ****************************************************************************/ @@ -1027,7 +915,7 @@ static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex) wlerr("Failed to lock mutex error=%d\n", ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1055,7 +943,7 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex) wlerr("Failed to unlock mutex error=%d\n", ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -2370,73 +2258,6 @@ static void IRAM_ATTR esp_empty_wrapper(void) { } -/* Second block of functions - * These functions are auxiliary functions that are used by the first block - * of functions or software adapters for the Wi-Fi driver - */ - -/**************************************************************************** - * 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; -} - /**************************************************************************** * Name: esp_get_free_heap_size * @@ -2825,110 +2646,6 @@ static void esp_update_time(struct timespec *timespec, uint32_t ticks) clock_timespec_add(timespec, &ts, timespec); } -#ifdef ESP_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_auth_trans - * - * Description: - * Converts an ESP32-C3 authenticate mode values to WEXT authenticate mode. - * - * Input Parameters: - * wifi_auth - ESP32-C3 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: %ld", wifi_auth); - break; - } - - return auth_mode; -} - -/**************************************************************************** - * Name: esp_wifi_cipher_trans - * - * Description: - * Converts a ESP32-C3 cipher type values to WEXT cipher type values. - * - * Input Parameters: - * wifi_cipher - ESP32-C3 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; -} - -#endif /* ESP_WLAN_HAS_STA */ - /**************************************************************************** * Name: queue_msg_waiting_wrapper * @@ -3184,7 +2901,7 @@ static int32_t xqueue_send_adapter(void *queue, } } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -3289,1970 +3006,3 @@ IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { return calloc(n, size); } - -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32-C3 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; - } - - sq_init(&g_wifi_evt_queue); - - 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.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM; - wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM; - - ret = esp_wifi_init(&wifi_cfg); - if (ret) - { - wlerr("Failed to initialize Wi-Fi error=%d\n", ret); - ret = esp_wifi_to_errno(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 = esp_wifi_to_errno(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 ESP_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 ESP_WLAN_HAS_SOFTAP - if (g_softap_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = esp_wifi_to_errno(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 ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi AP ret=%d\n", ret); - ret = esp_wifi_to_errno(ret); - goto errout; - } - } -#endif /* ESP_WLAN_HAS_SOFTAP */ - - wlinfo("OK to stop Wi-Fi station\n"); - -#ifdef ESP_WLAN_HAS_SOFTAP -errout: -#endif /* ESP_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 esp_wifi_to_errno(ret); -} - -/**************************************************************************** - * 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; - - 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 esp_wifi_to_errno(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 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 (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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(ret); - } - - ret = esp_wifi_connect(); - if (ret) - { - wlerr("Failed to connect ret=%d\n", ret); - ret = esp_wifi_to_errno(ret); - goto errout; - } - - esp_wifi_lock(false); - - ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT); - do - { - if (g_sta_connected) - { - break; - } - - task_delay_wrapper(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 = esp_wifi_to_errno(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 esp_wifi_to_errno(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 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 (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 esp_wifi_to_errno(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 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; - } - 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 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; -} - -#endif /* ESP_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 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_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 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_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 esp_wifi_to_errno(ret); - } - } - else - { - return -ENOSYS; - } - - return OK; -} - -#ifdef ESP_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 esp_wifi_to_errno(ret); - } - - iwr->u.sens.value = -(ap_info.rssi); - } - - return OK; -} -#endif /* ESP_WLAN_HAS_STA */ - -/**************************************************************************** - * SoftAP functions - ****************************************************************************/ - -#ifdef ESP_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 ESP_WLAN_HAS_STA - if (g_sta_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = esp_wifi_to_errno(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 ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi STA ret=%d\n", ret); - ret = esp_wifi_to_errno(ret); - goto errout; - } - } -#endif /* ESP_WLAN_HAS_STA */ - - wlinfo("OK to stop Wi-Fi SoftAP\n"); - -#ifdef ESP_WLAN_HAS_STA -errout: -#endif /* ESP_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 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 (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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 /* ESP_WLAN_HAS_SOFTAP */ diff --git a/arch/risc-v/src/esp32c6/esp_coex_adapter.c b/arch/risc-v/src/esp32c6/esp_coex_adapter.c index e5d97b50ee..a512256299 100644 --- a/arch/risc-v/src/esp32c6/esp_coex_adapter.c +++ b/arch/risc-v/src/esp32c6/esp_coex_adapter.c @@ -37,7 +37,7 @@ #include #include "esp_hr_timer.h" -#include "esp_wlan.h" +#include "esp_wifi_utils.h" #include "esp_attr.h" #include "esp_timer.h" @@ -137,7 +137,7 @@ static int32_t IRAM_ATTR esp_coex_semphr_take_from_isr_wrapper(void *semphr, { *(int *)hptw = 0; - return nuttx_err_to_freertos(nxsem_trywait(semphr)); + return nuttx_err_to_common_err(nxsem_trywait(semphr)); } /**************************************************************************** @@ -418,7 +418,7 @@ int32_t esp_coex_common_semphr_take_wrapper(void *semphr, block_time_tick, ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -449,7 +449,7 @@ int32_t esp_coex_common_semphr_give_wrapper(void *semphr) wlerr("Failed to post sem error=%d\n", ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** diff --git a/arch/risc-v/src/esp32c6/esp_wifi_adapter.c b/arch/risc-v/src/esp32c6/esp_wifi_adapter.c index d6f8f884a2..5110274a19 100644 --- a/arch/risc-v/src/esp32c6/esp_wifi_adapter.c +++ b/arch/risc-v/src/esp32c6/esp_wifi_adapter.c @@ -84,7 +84,7 @@ #include "esp_private/sleep_retention.h" #endif -#include "esp_wlan.h" +#include "esp_wlan_netdev.h" #include "esp_wifi_adapter.h" #include "esp_wifi_utils.h" @@ -92,23 +92,6 @@ * 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 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) @@ -117,17 +100,6 @@ * 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 @@ -145,25 +117,6 @@ struct mq_adpt char name[16]; /* Message queue name */ }; -/* Wi-Fi time private data */ - -struct time_adpt -{ - time_t sec; /* Second value */ - suseconds_t usec; /* Micro second value */ -}; - -/* Wi-Fi NVS private data */ - -struct nvs_adpt -{ - char *index_name; -}; - -/* Wi-Fi event callback function */ - -typedef void (*wifi_evt_cb_t)(void *p); - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -292,7 +245,6 @@ static void esp_empty_wrapper(void); * of functions or software adapters for the Wi-Fi driver */ -static int esp_freq_to_channel(uint16_t freq); static uint32_t esp_get_free_heap_size(void); static void *event_group_create_wrapper(void); static void event_group_delete_wrapper(void *event); @@ -323,8 +275,6 @@ static int esp_nvs_get_u16(uint32_t handle, static int esp_nvs_set_u16(uint32_t handle, const char *key, uint16_t value); static void esp_nvs_close(uint32_t handle); static void esp_update_time(struct timespec *timespec, uint32_t ticks); -static int esp_wifi_auth_trans(uint32_t wifi_auth); -static int esp_wifi_cipher_trans(uint32_t wifi_cipher); static uint32_t queue_msg_waiting_wrapper(void *queue); static void task_delay_wrapper(uint32_t tick); static void task_delete_wrapper(void *task_handle); @@ -347,34 +297,6 @@ extern void wifi_apb80m_release(void); * Private Data ****************************************************************************/ -/* Wi-Fi event private data */ - -static sq_queue_t g_wifi_evt_queue; - -/* Wi-Fi adapter reference */ - -static int g_wifi_ref; - -#ifdef ESP_WLAN_HAS_STA - -/* Wi-Fi interface configuration */ - -extern wifi_config_t g_sta_wifi_cfg; - -#endif /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_WLAN_HAS_SOFTAP - -/* Wi-Fi interface configuration */ - -extern wifi_config_t g_softap_wifi_cfg; - -#endif /* ESP_WLAN_HAS_SOFTAP */ - -/* Device specific lock */ - -static spinlock_t g_lock; - /**************************************************************************** * Public Data ****************************************************************************/ @@ -1027,7 +949,7 @@ static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex) wlerr("Failed to lock mutex error=%d\n", ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -1055,7 +977,7 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex) wlerr("Failed to unlock mutex error=%d\n", ret); } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -2324,73 +2246,6 @@ static void IRAM_ATTR esp_empty_wrapper(void) { } -/* Second block of functions - * These functions are auxiliary functions that are used by the first block - * of functions or software adapters for the Wi-Fi driver - */ - -/**************************************************************************** - * 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; -} - /**************************************************************************** * Name: esp_get_free_heap_size * @@ -2779,110 +2634,6 @@ static void esp_update_time(struct timespec *timespec, uint32_t ticks) clock_timespec_add(timespec, &ts, timespec); } -#ifdef ESP_WLAN_HAS_STA - -/**************************************************************************** - * Name: esp_wifi_auth_trans - * - * Description: - * Converts a ESP32-C6 authenticate mode values to WEXT authenticate mode. - * - * Input Parameters: - * wifi_auth - ESP32-C6 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: %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 - ESP32-C6 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; -} - -#endif /* ESP_WLAN_HAS_STA */ - /**************************************************************************** * Name: queue_msg_waiting_wrapper * @@ -3138,7 +2889,7 @@ static int32_t xqueue_send_adapter(void *queue, } } - return nuttx_err_to_freertos(ret); + return nuttx_err_to_common_err(ret); } /**************************************************************************** @@ -3243,1970 +2994,3 @@ IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { return calloc(n, size); } - -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32-C6 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; - } - - sq_init(&g_wifi_evt_queue); - - 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.dynamic_rx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_RX_BUFFER_NUM; - wifi_cfg.dynamic_tx_buf_num = CONFIG_ESPRESSIF_WIFI_DYNAMIC_TX_BUFFER_NUM; - - ret = esp_wifi_init(&wifi_cfg); - if (ret) - { - wlerr("Failed to initialize Wi-Fi error=%d\n", ret); - ret = esp_wifi_to_errno(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 = esp_wifi_to_errno(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 ESP_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 ESP_WLAN_HAS_SOFTAP - if (g_softap_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = esp_wifi_to_errno(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 ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi AP ret=%d\n", ret); - ret = esp_wifi_to_errno(ret); - goto errout; - } - } -#endif /* ESP_WLAN_HAS_SOFTAP */ - - wlinfo("OK to stop Wi-Fi station\n"); - -#ifdef ESP_WLAN_HAS_SOFTAP -errout: -#endif /* ESP_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 esp_wifi_to_errno(ret); -} - -/**************************************************************************** - * 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; - - 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 esp_wifi_to_errno(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 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 (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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(ret); - } - - ret = esp_wifi_connect(); - if (ret) - { - wlerr("Failed to connect ret=%d\n", ret); - ret = esp_wifi_to_errno(ret); - goto errout; - } - - esp_wifi_lock(false); - - ticks = SEC2TICK(WIFI_CONNECT_TIMEOUT); - do - { - if (g_sta_connected) - { - break; - } - - task_delay_wrapper(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 = esp_wifi_to_errno(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 esp_wifi_to_errno(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 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 (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 esp_wifi_to_errno(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 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; - } - 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 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; -} - -#endif /* ESP_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 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_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 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_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 esp_wifi_to_errno(ret); - } - } - else - { - return -ENOSYS; - } - - return OK; -} - -#ifdef ESP_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 esp_wifi_to_errno(ret); - } - - iwr->u.sens.value = -(ap_info.rssi); - } - - return OK; -} -#endif /* ESP_WLAN_HAS_STA */ - -/**************************************************************************** - * SoftAP functions - ****************************************************************************/ - -#ifdef ESP_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 ESP_WLAN_HAS_STA - if (g_sta_started) - { - mode = WIFI_MODE_APSTA; - } - else -#endif /* ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi with mode=%d ret=%d\n", mode, ret); - ret = esp_wifi_to_errno(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 ESP_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 = esp_wifi_to_errno(ret); - goto errout; - } - - ret = esp_wifi_start(); - if (ret) - { - wlerr("Failed to start Wi-Fi STA ret=%d\n", ret); - ret = esp_wifi_to_errno(ret); - goto errout; - } - } -#endif /* ESP_WLAN_HAS_STA */ - - wlinfo("OK to stop Wi-Fi SoftAP\n"); - -#ifdef ESP_WLAN_HAS_STA -errout: -#endif /* ESP_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 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 (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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 esp_wifi_to_errno(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 /* ESP_WLAN_HAS_SOFTAP */ diff --git a/arch/risc-v/src/esp32c6/esp_wifi_adapter.h b/arch/risc-v/src/esp32c6/esp_wifi_adapter.h index cc69495df8..3f0e19ed7f 100644 --- a/arch/risc-v/src/esp32c6/esp_wifi_adapter.h +++ b/arch/risc-v/src/esp32c6/esp_wifi_adapter.h @@ -28,11 +28,6 @@ ****************************************************************************/ #include -#include - -#include - -#include "esp_wlan.h" #ifndef __ASSEMBLY__ @@ -49,622 +44,10 @@ extern "C" * Pre-processor Definitions ****************************************************************************/ -#define SSID_MAX_LEN (32) -#define PWD_MAX_LEN (64) - -#define CONFIG_IDF_TARGET_ESP32C6 1 - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ -/**************************************************************************** - * Name: esp_wifi_adapter_init - * - * Description: - * Initialize ESP32C6 Wi-Fi adapter - * - * Input Parameters: - * None - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -int esp_wifi_adapter_init(void); - -/**************************************************************************** - * 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); - -#ifdef ESP_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); - -/**************************************************************************** - * 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_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); -#endif /* ESP_WLAN_HAS_STA */ - -#ifdef ESP_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); - -/**************************************************************************** - * 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_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); -#endif /* ESP_WLAN_HAS_SOFTAP */ - #ifdef __cplusplus } #endif