|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifdef CONFIG_ESP_WIFI_BSS_MAX_IDLE_SUPPORT |
| 8 | + |
| 9 | +#include <string.h> |
| 10 | +#include "unity.h" |
| 11 | +#include "freertos/FreeRTOS.h" |
| 12 | +#include "freertos/task.h" |
| 13 | +#include "esp_wifi.h" |
| 14 | +#include "esp_event.h" |
| 15 | +#include "esp_log.h" |
| 16 | +#include "test_utils.h" |
| 17 | +#include "freertos/event_groups.h" |
| 18 | +#include "unity_test_utils.h" |
| 19 | +#include "esp_private/wifi.h" |
| 20 | + |
| 21 | +#ifdef __CHECKER__ |
| 22 | +#define __force __attribute__((force)) |
| 23 | +#define __bitwise __attribute__((bitwise)) |
| 24 | +#else |
| 25 | +#define __force |
| 26 | +#define __bitwise |
| 27 | +#endif |
| 28 | + |
| 29 | +#if defined(__linux__) || defined(__GLIBC__) |
| 30 | +#include <endian.h> |
| 31 | +#include <byteswap.h> |
| 32 | +#else |
| 33 | +#include <machine/endian.h> |
| 34 | +#define __BYTE_ORDER BYTE_ORDER |
| 35 | +#define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 36 | +#endif /* __linux__ */ |
| 37 | +#ifndef __BYTE_ORDER |
| 38 | +#ifndef __LITTLE_ENDIAN |
| 39 | +#define __LITTLE_ENDIAN 1234 |
| 40 | +#endif |
| 41 | +#endif |
| 42 | + |
| 43 | +typedef uint16_t u16; |
| 44 | +typedef uint8_t u8; |
| 45 | +typedef u16 __bitwise be16; |
| 46 | + |
| 47 | +#define host_to_be16(n) ((__force be16) __builtin_bswap16((n))) |
| 48 | + |
| 49 | +#ifndef TEST_SUFFIX_STR |
| 50 | +#define TEST_SUFFIX_STR "_0000" |
| 51 | +#endif |
| 52 | + |
| 53 | +#define TEST_DEFAULT_SSID "SSID_" CONFIG_IDF_TARGET TEST_SUFFIX_STR |
| 54 | +#define TEST_DEFAULT_PWD "PASS_" CONFIG_IDF_TARGET TEST_SUFFIX_STR |
| 55 | +#define TEST_DEFAULT_CHANNEL (6) |
| 56 | +#define CONNECT_TIMEOUT_MS (8000) |
| 57 | +#define MAXIMUM_RETRY (5) |
| 58 | + |
| 59 | +#define WIFI_DISCONNECT_EVENT (1) |
| 60 | +#define WIFI_STA_CONNECTED (1<<1) |
| 61 | +#define WIFI_AP_STA_CONNECTED (1<<2) |
| 62 | +#define WIFI_FAIL (1<<3) |
| 63 | +#define EMPH_STR(s) "****** "s" ******" |
| 64 | + |
| 65 | +#define MAX_IDLE_PERIOD (5) |
| 66 | +#define ETHTYPE_IP 0x0800 |
| 67 | + |
| 68 | +static const char* TAG = "test_bss_max_idle"; |
| 69 | + |
| 70 | +static bool s_sta_conn_first = false; |
| 71 | +static bool s_keep_alive_received = false; |
| 72 | +static int s_retry_num = 0; |
| 73 | +static EventGroupHandle_t wifi_events; |
| 74 | + |
| 75 | +struct eth_header { |
| 76 | + u8 dest_mac[6]; |
| 77 | + u8 src_mac[6]; |
| 78 | + be16 ethertype; |
| 79 | +}; |
| 80 | + |
| 81 | +static esp_err_t wifi_ap_receive_test(void *buffer, uint16_t len, void *eb) |
| 82 | +{ |
| 83 | + struct eth_header *pac = (struct eth_header *)buffer; |
| 84 | + if ((host_to_be16(pac->ethertype) == ETHTYPE_IP) && (len < 48)) { |
| 85 | + ESP_LOGI(TAG, "KEEP ALIVE RECEIVED"); |
| 86 | + s_keep_alive_received = true; |
| 87 | + esp_wifi_deauth_sta(0); |
| 88 | + } |
| 89 | + esp_wifi_internal_free_rx_buffer(eb); |
| 90 | + return ESP_OK; |
| 91 | +} |
| 92 | + |
| 93 | +static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) |
| 94 | +{ |
| 95 | + ESP_LOGI(TAG, "wifi event handler: %"PRIi32, event_id); |
| 96 | + switch (event_id) { |
| 97 | + case WIFI_EVENT_AP_START: |
| 98 | + ESP_LOGI(TAG, "WIFI_EVENT_AP_START"); |
| 99 | + esp_wifi_internal_reg_rxcb(WIFI_IF_AP, wifi_ap_receive_test); |
| 100 | + break; |
| 101 | + case WIFI_EVENT_STA_START: |
| 102 | + ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); |
| 103 | + break; |
| 104 | + case WIFI_EVENT_AP_STACONNECTED: |
| 105 | + ESP_LOGI(TAG, "Wi-Fi AP got a station connected"); |
| 106 | + break; |
| 107 | + case WIFI_EVENT_STA_CONNECTED: |
| 108 | + ESP_LOGI(TAG, "WIFI_EVENT_STA_CONNECTED"); |
| 109 | + if (wifi_events) { |
| 110 | + xEventGroupSetBits(wifi_events, WIFI_STA_CONNECTED); |
| 111 | + s_sta_conn_first = true; |
| 112 | + } |
| 113 | + break; |
| 114 | + case WIFI_EVENT_STA_DISCONNECTED: |
| 115 | + if ((s_retry_num < MAXIMUM_RETRY) && !s_sta_conn_first) { |
| 116 | + esp_wifi_connect(); |
| 117 | + s_retry_num++; |
| 118 | + ESP_LOGI(TAG, "retry to connect to AP"); |
| 119 | + } else { |
| 120 | + xEventGroupSetBits(wifi_events, WIFI_DISCONNECT_EVENT); |
| 121 | + ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); |
| 122 | + wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; |
| 123 | + ESP_LOGI(TAG, "disconnect reason: %u", event->reason); |
| 124 | + } |
| 125 | + break; |
| 126 | + default: |
| 127 | + break; |
| 128 | + } |
| 129 | + return; |
| 130 | +} |
| 131 | + |
| 132 | +static esp_err_t event_init(void) |
| 133 | +{ |
| 134 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 135 | + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); |
| 136 | + return ESP_OK; |
| 137 | +} |
| 138 | + |
| 139 | +static esp_err_t event_deinit(void) |
| 140 | +{ |
| 141 | + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler)); |
| 142 | + ESP_ERROR_CHECK(esp_event_loop_delete_default()); |
| 143 | + return ESP_OK; |
| 144 | +} |
| 145 | + |
| 146 | +static void start_wifi_as_softap(void) |
| 147 | +{ |
| 148 | + wifi_config_t w_config = { |
| 149 | + .ap.ssid = TEST_DEFAULT_SSID, |
| 150 | + .ap.password = TEST_DEFAULT_PWD, |
| 151 | + .ap.ssid_len = strlen(TEST_DEFAULT_SSID), |
| 152 | + .ap.channel = TEST_DEFAULT_CHANNEL, |
| 153 | + .ap.authmode = WIFI_AUTH_WPA3_PSK, |
| 154 | + .ap.ssid_hidden = false, |
| 155 | + .ap.max_connection = 4, |
| 156 | + .ap.beacon_interval = 100, |
| 157 | + .ap.bss_max_idle_cfg = { |
| 158 | + .period = MAX_IDLE_PERIOD, |
| 159 | + .protected_keep_alive = true, |
| 160 | + }, |
| 161 | + }; |
| 162 | + event_init(); |
| 163 | + if (wifi_events == NULL) { |
| 164 | + wifi_events = xEventGroupCreate(); |
| 165 | + } |
| 166 | + xEventGroupClearBits(wifi_events, 0x00ffffff); |
| 167 | + |
| 168 | + TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_AP)); |
| 169 | + TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_AP, &w_config)); |
| 170 | + TEST_ESP_OK(esp_wifi_start()); |
| 171 | + ESP_LOGI(TAG, "start wifi softap: %s", TEST_DEFAULT_SSID); |
| 172 | +} |
| 173 | + |
| 174 | +static void start_wifi_as_sta(void) |
| 175 | +{ |
| 176 | + event_init(); |
| 177 | + if (wifi_events == NULL) { |
| 178 | + wifi_events = xEventGroupCreate(); |
| 179 | + } |
| 180 | + xEventGroupClearBits(wifi_events, 0x00ffffff); |
| 181 | + TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 182 | + TEST_ESP_OK(esp_wifi_start()); |
| 183 | +} |
| 184 | + |
| 185 | +static void stop_wifi(void) |
| 186 | +{ |
| 187 | + TEST_ESP_OK(esp_wifi_stop()); |
| 188 | + event_deinit(); |
| 189 | + if (wifi_events) { |
| 190 | + vEventGroupDelete(wifi_events); |
| 191 | + wifi_events = NULL; |
| 192 | + } |
| 193 | + vTaskDelay(500 / portTICK_PERIOD_MS); |
| 194 | +} |
| 195 | + |
| 196 | +static void wifi_connect(void) |
| 197 | +{ |
| 198 | + wifi_config_t w_config = { |
| 199 | + .sta.ssid = TEST_DEFAULT_SSID, |
| 200 | + .sta.password = TEST_DEFAULT_PWD, |
| 201 | + .sta.channel = TEST_DEFAULT_CHANNEL, |
| 202 | + }; |
| 203 | + |
| 204 | + TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_STA, &w_config)); |
| 205 | + TEST_ESP_OK(esp_wifi_connect()); |
| 206 | + ESP_LOGI(TAG, "start esp_wifi_connect: %s", TEST_DEFAULT_SSID); |
| 207 | +} |
| 208 | + |
| 209 | +static void test_bss_max_idle_sta(void) |
| 210 | +{ |
| 211 | + start_wifi_as_sta(); |
| 212 | + wifi_connect(); |
| 213 | + // Waiting untill connection est or failed |
| 214 | + EventBits_t bits = xEventGroupWaitBits(wifi_events, |
| 215 | + WIFI_STA_CONNECTED | WIFI_FAIL, |
| 216 | + pdFALSE, |
| 217 | + pdFALSE, |
| 218 | + portMAX_DELAY); |
| 219 | + TEST_ASSERT(bits & WIFI_STA_CONNECTED); |
| 220 | + if (bits != WIFI_FAIL) { |
| 221 | + bits = xEventGroupWaitBits(wifi_events, |
| 222 | + WIFI_DISCONNECT_EVENT, |
| 223 | + pdFALSE, |
| 224 | + pdFALSE, |
| 225 | + portMAX_DELAY); |
| 226 | + } |
| 227 | + stop_wifi(); |
| 228 | +} |
| 229 | + |
| 230 | +static void test_bss_max_idle_softap(void) |
| 231 | +{ |
| 232 | + start_wifi_as_softap(); |
| 233 | + |
| 234 | + vTaskDelay((CONNECT_TIMEOUT_MS + MAX_IDLE_PERIOD * 1000) / portTICK_PERIOD_MS); |
| 235 | + |
| 236 | + TEST_ASSERT(s_keep_alive_received); |
| 237 | + stop_wifi(); |
| 238 | +} |
| 239 | + |
| 240 | +TEST_CASE_MULTIPLE_DEVICES("Connection with bss max idle enabled", "[wifi][bss max idle]", test_bss_max_idle_sta, test_bss_max_idle_softap); |
| 241 | + |
| 242 | +#endif /* CONFIG_ESP_WIFI_BSS_MAX_IDLE_SUPPORT */ |
0 commit comments