fix(wifi): fix some wifi bugs and support btwt (v5.2)

This commit is contained in:
xuxiao 2024-07-31 10:51:01 +08:00
parent dead39c55d
commit 8971a3c724
6 changed files with 169 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -36,7 +36,7 @@ extern "C" {
* - ESP_ERR_WIFI_TWT_FULL: no available flow id * - ESP_ERR_WIFI_TWT_FULL: no available flow id
* - ESP_ERR_INVALID_ARG: invalid argument * - ESP_ERR_INVALID_ARG: invalid argument
*/ */
esp_err_t esp_wifi_sta_itwt_setup(wifi_twt_setup_config_t *setup_config); esp_err_t esp_wifi_sta_itwt_setup(wifi_itwt_setup_config_t *setup_config);
/** /**
* @brief Tear down individual TWT agreements * @brief Tear down individual TWT agreements
@ -143,6 +143,79 @@ esp_err_t esp_wifi_enable_rx_statistics(bool rx_stats, bool rx_mu_stats);
*/ */
esp_err_t esp_wifi_enable_tx_statistics(esp_wifi_aci_t aci, bool tx_stats); esp_err_t esp_wifi_enable_tx_statistics(esp_wifi_aci_t aci, bool tx_stats);
/**
* @brief Set up an broadcast TWT agreement (NegotiationType=3) or change TWT parameters of the existing TWT agreement
* - TWT Wake Interval = TWT Wake Interval Mantissa * (2 ^ TWT Wake Interval Exponent), unit: us
* - e.g. TWT Wake Interval Mantissa = 512, TWT Wake Interval Exponent = 12, then TWT Wake Interval is 2097.152 ms
* Nominal Minimum Wake Duration = 255, then TWT Wake Duration is 65.28 ms
*
* @attention Support at most 32 TWT agreements, otherwise ESP_ERR_WIFI_TWT_FULL will be returned.
* Support sleep time up to (1 << 35) us.
*
* @param[in,out] config pointer to btwt setup config structure.
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
* - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
* - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
* - ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
* - ESP_ERR_WIFI_TWT_FULL: no available flow id
* - ESP_ERR_INVALID_ARG: invalid argument
*/
esp_err_t esp_wifi_sta_btwt_setup(wifi_btwt_setup_config_t *config);
/**
* @brief Tear down broadcast TWT agreements
*
* @param[in] btwt_id The value range is [0, 31].
* BTWT_ID_ALL indicates tear down all broadcast TWT agreements.
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
* - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
* - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
* - ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
* - ESP_ERR_INVALID_ARG: invalid argument
*/
esp_err_t esp_wifi_sta_btwt_teardown(uint8_t btwt_id);
/**
* @brief Get number of broadcast TWTs supported by the connected AP
*
* @param[out] btwt_number store number of btwts supported by the connected AP
*
* @return
* - ESP_OK: succeed
* - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
* - ESP_ERR_WIFI_NOT_STARTED: WiFi is not started by esp_wifi_start
* - ESP_ERR_INVALID_ARG: invalid argument
*/
esp_err_t esp_wifi_sta_get_btwt_num(uint8_t *btwt_number);
/**
* @brief Get broadcast TWT information
*
* @param[in] btwt_number As input param, it stores max btwt number AP supported.
* @param[in] btwt_info array to hold the btwt information supported by AP, and the array size must be at least as large as the BTWT number.
*
* @return
* - ESP_OK: succeed
* - ESP_FAIL: fail
* - ESP_ERR_WIFI_CONN: WiFi internal error, station or soft-AP control block wrong
*/
esp_err_t esp_wifi_sta_btwt_get_info(uint8_t btwt_number, esp_wifi_btwt_info_t *btwt_info);
/**
* @brief Set WiFi TWT config
*
* @param[in] config pointer to the WiFi TWT configure structure.
*
* @return
* - ESP_OK: succeed
*/
esp_err_t esp_wifi_sta_twt_config(wifi_twt_config_t *config);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -15,6 +15,7 @@ extern "C" {
#endif #endif
#define FLOW_ID_ALL (8) #define FLOW_ID_ALL (8)
#define BTWT_ID_ALL (32)
#define BSS_MAX_COLOR (63) #define BSS_MAX_COLOR (63)
/** /**
@ -93,25 +94,38 @@ typedef enum {
} wifi_twt_setup_cmds_t; } wifi_twt_setup_cmds_t;
/** /**
* @brief TWT setup config * @brief broadcast TWT setup config
*/
typedef struct {
wifi_twt_setup_cmds_t setup_cmd; /**< Indicates the type of TWT command*/
uint8_t btwt_id; /**< When set up an broadcast TWT agreement, the broadcast twt id will be assigned by AP after a successful agreement setup.
broadcast twt id could be specified to a value in the range of [1, 31], but it might be change by AP in the response.
When change TWT parameters of the existing TWT agreement, broadcast twt id should be an existing one. The value range is [1, 31].*/
uint16_t timeout_time_ms; /**< Timeout times of receiving setup action frame response, default 5s*/
} wifi_btwt_setup_config_t;
/**
* @brief Individual TWT setup config
*/ */
typedef struct typedef struct
{ {
wifi_twt_setup_cmds_t setup_cmd; /**< Indicates the type of TWT command */ wifi_twt_setup_cmds_t setup_cmd; /**< Indicates the type of TWT command */
uint16_t trigger :1; /**< 1: a trigger-enabled TWT, 0: a non-trigger-enabled TWT */ uint16_t trigger : 1; /**< 1: a trigger-enabled individual TWT, 0: a non-trigger-enabled individual TWT */
uint16_t flow_type :1; /**< 0: an announced TWT, 1: an unannounced TWT */ uint16_t flow_type : 1; /**< 0: an announced individual TWT, 1: an unannounced individual TWT */
uint16_t flow_id :3; /**< When set up an individual TWT agreement, the flow id will be assigned by AP after a successful agreement setup. uint16_t flow_id : 3; /**< When set up an individual TWT agreement, the flow id will be assigned by AP after a successful agreement setup.
flow_id could be specified to a value in the range of [0, 7], but it might be changed by AP in the response. flow_id could be specified to a value in the range of [0, 7], but it might be changed by AP in the response.
When change TWT parameters of the existing TWT agreement, flow_id should be an existing one. The value range is [0, 7]. */ When change TWT parameters of the existing TWT agreement, flow_id should be an existing one. The value range is [0, 7]. */
uint16_t wake_invl_expn :5; /**< TWT Wake Interval Exponent. The value range is [0, 31]. */ uint16_t wake_invl_expn : 5; /**< Individual TWT Wake Interval Exponent. The value range is [0, 31]. */
uint16_t wake_duration_unit :1; /**< TWT Wake duration unit, 0: 256us 1: TU (TU = 1024us)*/ uint16_t wake_duration_unit : 1; /**< Individual TWT Wake duration unit, 0: 256us 1: TU (TU = 1024us)*/
uint16_t reserved :5; /**< bit: 11.15 reserved */ uint16_t reserved : 5; /**< bit: 11.15 reserved */
uint8_t min_wake_dura; /**< Nominal Minimum Wake Duration, indicates the minimum amount of time, in unit of 256 us, that the TWT requesting STA expects that it needs to be awake. The value range is [1, 255]. */ uint8_t min_wake_dura; /**< Nominal Minimum Wake Duration, indicates the minimum amount of time, in unit of 256 us, that the individual TWT requesting STA expects that it needs to be awake. The value range is [1, 255]. */
uint16_t wake_invl_mant; /**< TWT Wake Interval Mantissa. The value range is [1, 65535]. */ uint16_t wake_invl_mant; /**< Individual TWT Wake Interval Mantissa. The value range is [1, 65535]. */
uint16_t twt_id; /**< TWT connection id, the value range is [0, 32767]. */ uint16_t twt_id; /**< Individual TWT connection id, the value range is [0, 32767]. */
uint16_t timeout_time_ms; /**< Timeout times of receiving setup action frame response, default 5s*/ uint16_t timeout_time_ms; /**< Timeout times of receiving setup action frame response, default 5s*/
} wifi_twt_setup_config_t; } wifi_twt_setup_config_t;
typedef wifi_twt_setup_config_t wifi_itwt_setup_config_t;
/** /**
* @brief HE SU GI and LTF types * @brief HE SU GI and LTF types
*/ */
@ -203,19 +217,68 @@ typedef struct {
unsigned : 24; /**< reserved */ unsigned : 24; /**< reserved */
} __attribute__((packed)) esp_wifi_rxctrl_t; } __attribute__((packed)) esp_wifi_rxctrl_t;
/**
* @brief bTWT setup status
*/
typedef enum {
BTWT_SETUP_TXFAIL, /**< station sends btwt setup request frame fail */
BTWT_SETUP_SUCCESS, /**< station receives btwt setup response frame and setup btwt sucessfully */
BTWT_SETUP_TIMEOUT, /**< timeout of receiving btwt setup response frame */
BTWT_SETUP_FULL, /**< indicate there is no available btwt id */
BTWT_SETUP_INVALID_ARG, /**< indicate invalid argument to setup btwt */
BTWT_SETUP_INTERNAL_ERR, /**< indicate internal error to setup btwt */
} wifi_btwt_setup_status_t;
/** Argument structure for WIFI_EVENT_TWT_SET_UP event */ /** Argument structure for WIFI_EVENT_TWT_SET_UP event */
typedef struct { typedef struct {
wifi_twt_setup_config_t config; /**< itwt setup config, this value is determined by the AP */ wifi_itwt_setup_config_t config; /**< itwt setup config, this value is determined by the AP */
esp_err_t status; /**< itwt setup status, 1: indicate setup success, others : indicate setup fail */ esp_err_t status; /**< itwt setup status, 1: indicate setup success, others : indicate setup fail */
uint8_t reason; /**< itwt setup frame tx fail reason */ uint8_t reason; /**< itwt setup frame tx fail reason */
uint64_t target_wake_time; /**< TWT SP start time */ uint64_t target_wake_time; /**< TWT SP start time */
} wifi_event_sta_itwt_setup_t; } wifi_event_sta_itwt_setup_t;
/**
* @brief iTWT teardown status
*/
typedef enum {
ITWT_TEARDOWN_FAIL, /**< station sends teardown frame fail */
ITWT_TEARDOWN_SUCCESS, /**< 1) station successfully sends teardown frame to AP; 2) station receives teardown frame from AP */
} wifi_itwt_teardown_status_t;
/** Argument structure for WIFI_EVENT_TWT_TEARDOWN event */ /** Argument structure for WIFI_EVENT_TWT_TEARDOWN event */
typedef struct { typedef struct {
uint8_t flow_id; /**< flow id */ uint8_t flow_id; /**< flow id */
wifi_itwt_teardown_status_t status; /**< itwt teardown status */
} wifi_event_sta_itwt_teardown_t; } wifi_event_sta_itwt_teardown_t;
/** Argument structure for WIFI_EVENT_BTWT_SET_UP event */
typedef struct {
wifi_btwt_setup_status_t status; /**< indicate btwt setup status */
wifi_twt_setup_cmds_t setup_cmd; /**< indicate the type of TWT command */
uint8_t btwt_id; /**< indicate btwt id */
uint8_t min_wake_dura; /**< Nominal Minimum Wake Duration, indicates the minimum amount of time, in unit of 256 us, that the TWT requesting STA expects that it needs to be awake. The value range is [1, 255]. */
uint8_t wake_invl_expn; /**< TWT Wake Interval Exponent. The value range is [0, 31]. */
uint16_t wake_invl_mant; /**< TWT Wake Interval Mantissa. The value range is [1, 65535]. */
bool trigger; /**< 1: a trigger-enabled TWT, 0: a non-trigger-enabled TWT */
uint8_t flow_type; /**< 0: an announced TWT, 1: an unannounced TWT */
uint8_t reason; /**< btwt setup frame tx fail reason */
uint64_t target_wake_time; /**< TWT SP start time */
} wifi_event_sta_btwt_setup_t;
/**
* @brief BTWT teardown status
*/
typedef enum {
BTWT_TEARDOWN_FAIL, /**< station sends teardown frame fail */
BTWT_TEARDOWN_SUCCESS, /**< 1) station successfully sends teardown frame to AP; 2) station receives teardown frame from AP */
} wifi_btwt_teardown_status_t;
/** Argument structure for WIFI_EVENT_TWT_TEARDOWN event */
typedef struct {
uint8_t btwt_id; /**< btwt id */
wifi_btwt_teardown_status_t status; /**< btwt teardown status */
} wifi_event_sta_btwt_teardown_t;
/** /**
* @brief iTWT probe status * @brief iTWT probe status
*/ */
@ -260,6 +323,21 @@ typedef struct {
uint8_t flow_id; /**< flow id */ uint8_t flow_id; /**< flow id */
} wifi_event_sta_twt_wakeup_t; } wifi_event_sta_twt_wakeup_t;
/** Argument structure for twt information */
typedef struct {
bool btwt_id_in_use; /**< indicate whether the btwt id is in use or not */
uint16_t btwt_trigger : 1; /**< 1: a trigger-enabled TWT, 0: a non-trigger-enabled TWT */
uint16_t btwt_flow_type : 1; /**< 0: an announced TWT, 1: an unannounced TWT */
uint16_t btwt_recommendation : 3; /**< indicate recommendations on the types of frames. 0: no constraints, [1, 3], [4, 7] reserved */
uint16_t btwt_wake_interval_exponent : 5; /**< TWT Wake Interval Exponent. The value range is [0, 31]. */
uint16_t btwt_rsvd : 6; /**< reserved */
uint8_t btwt_wake_duration; /**< TWT Wake duration unit, 0: 256us 1: TU (TU = 1024us) */
uint16_t btwt_wake_interval_mantissa; /**< TWT Wake Interval Mantissa. The value range is [1, 65535]. */
uint16_t btwt_info_id : 5; /**< btwt id */
uint16_t btwt_info_persistence : 8; /**< indicate the number of TBTTs during which the Broadcast TWT SPs corresponding to this broadcast TWT parameters set are present */
uint16_t btwt_info_rsvd : 3; /**< reserved */
} esp_wifi_btwt_info_t;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -901,6 +901,8 @@ typedef enum {
WIFI_EVENT_ITWT_PROBE, /**< iTWT probe */ WIFI_EVENT_ITWT_PROBE, /**< iTWT probe */
WIFI_EVENT_ITWT_SUSPEND, /**< iTWT suspend */ WIFI_EVENT_ITWT_SUSPEND, /**< iTWT suspend */
WIFI_EVENT_TWT_WAKEUP, /**< TWT wakeup */ WIFI_EVENT_TWT_WAKEUP, /**< TWT wakeup */
WIFI_EVENT_BTWT_SETUP, /**< bTWT setup */
WIFI_EVENT_BTWT_TEARDOWN, /**< bTWT teardown*/
WIFI_EVENT_NAN_STARTED, /**< NAN Discovery has started */ WIFI_EVENT_NAN_STARTED, /**< NAN Discovery has started */
WIFI_EVENT_NAN_STOPPED, /**< NAN Discovery has stopped */ WIFI_EVENT_NAN_STOPPED, /**< NAN Discovery has stopped */

@ -1 +1 @@
Subproject commit 873b5170214d034283b1a612825e04901d45b878 Subproject commit 596bdcf09fc57a2b043a9fe3201e2aa38050ae55

View File

@ -110,7 +110,7 @@ static int wifi_cmd_itwt(int argc, char **argv)
return 1; return 1;
} }
} }
wifi_twt_setup_config_t setup_config = { wifi_itwt_setup_config_t setup_config = {
.setup_cmd = (itwt_args.setup->ival[0] <= TWT_DEMAND) ? itwt_args.setup->ival[0] : TWT_REQUEST, .setup_cmd = (itwt_args.setup->ival[0] <= TWT_DEMAND) ? itwt_args.setup->ival[0] : TWT_REQUEST,
.flow_id = 0, .flow_id = 0,
.twt_id = itwt_args.twtid->count ? itwt_args.twtid->ival[0] : 0, .twt_id = itwt_args.twtid->count ? itwt_args.twtid->ival[0] : 0,

View File

@ -118,7 +118,7 @@ static void got_ip_handler(void *arg, esp_event_base_t event_base,
esp_wifi_sta_get_negotiated_phymode(&phymode); esp_wifi_sta_get_negotiated_phymode(&phymode);
if (phymode == WIFI_PHY_MODE_HE20) { if (phymode == WIFI_PHY_MODE_HE20) {
esp_err_t err = ESP_OK; esp_err_t err = ESP_OK;
wifi_twt_setup_config_t setup_config = { wifi_itwt_setup_config_t setup_config = {
.setup_cmd = TWT_REQUEST, .setup_cmd = TWT_REQUEST,
.flow_id = 0, .flow_id = 0,
.twt_id = CONFIG_EXAMPLE_ITWT_ID, .twt_id = CONFIG_EXAMPLE_ITWT_ID,