|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include <inttypes.h> |
| 9 | +#include "esp_system.h" |
| 10 | +#include "esp_log.h" |
| 11 | +#include "esp_system.h" |
| 12 | +#include "esp_event.h" |
| 13 | +#include "freertos/FreeRTOS.h" |
| 14 | +#include "freertos/event_groups.h" |
| 15 | +#include "unity.h" |
| 16 | +#include "utils/common.h" |
| 17 | +#include "esp_wifi_types.h" |
| 18 | +#include "esp_wifi_driver.h" |
| 19 | +#include "memory_checks.h" |
| 20 | +#include "common/ieee802_11_defs.h" |
| 21 | +#include "test_utils.h" |
| 22 | +#include "test_wpa_supplicant_common.h" |
| 23 | +#include "sdkconfig.h" |
| 24 | + |
| 25 | +#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY |
| 26 | +#define WIFI_CONNECTED_BIT BIT0 |
| 27 | +#define WIFI_FAIL_BIT BIT1 |
| 28 | +#define WIFI_AP_STA_CONNECTED BIT2 |
| 29 | +#define CONNECT_TIMEOUT_MS (10000) |
| 30 | + |
| 31 | +#ifndef TEST_SUFFIX_STR |
| 32 | +#define TEST_SUFFIX_STR "_0000" |
| 33 | +#endif |
| 34 | +#define TEST_DEFAULT_SSID "SSID_" CONFIG_IDF_TARGET TEST_SUFFIX_STR |
| 35 | +#define TEST_DEFAULT_PWD "PASS_" CONFIG_IDF_TARGET TEST_SUFFIX_STR |
| 36 | +#define TEST_DEFAULT_CHANNEL (6) |
| 37 | + |
| 38 | +static const char *TAG = "test_ext_bss"; |
| 39 | +static esp_netif_t* s_ap_netif = NULL; |
| 40 | +static esp_netif_t* s_sta_netif = NULL; |
| 41 | +static EventGroupHandle_t wifi_event; |
| 42 | +static int retry_count = 0; |
| 43 | + |
| 44 | +static void wifi_event_handler(void *arg, esp_event_base_t event_base, |
| 45 | + int32_t event_id, void *event_data) |
| 46 | +{ |
| 47 | + switch (event_id) { |
| 48 | + case WIFI_EVENT_STA_START: |
| 49 | + ESP_LOGI(TAG, "WIFI Started"); |
| 50 | + break; |
| 51 | + case WIFI_EVENT_STA_DISCONNECTED: |
| 52 | + if (retry_count < 5) { |
| 53 | + ESP_LOGI(TAG, "retry connection"); |
| 54 | + esp_wifi_connect(); |
| 55 | + retry_count++; |
| 56 | + } else { |
| 57 | + xEventGroupSetBits(wifi_event, WIFI_FAIL_BIT); |
| 58 | + } |
| 59 | + break; |
| 60 | + case WIFI_EVENT_STA_CONNECTED: |
| 61 | + retry_count = 0; |
| 62 | + xEventGroupSetBits(wifi_event, WIFI_CONNECTED_BIT); |
| 63 | + break; |
| 64 | + case WIFI_EVENT_AP_STACONNECTED: |
| 65 | + wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; |
| 66 | + ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", |
| 67 | + MAC2STR(event->mac), event->aid); |
| 68 | + xEventGroupSetBits(wifi_event, WIFI_AP_STA_CONNECTED); |
| 69 | + break; |
| 70 | + case WIFI_EVENT_AP_STADISCONNECTED: |
| 71 | + ESP_LOGI(TAG, "station leave"); |
| 72 | + break; |
| 73 | + default: |
| 74 | + break; |
| 75 | + } |
| 76 | + return; |
| 77 | +} |
| 78 | + |
| 79 | +static esp_err_t event_init(void) |
| 80 | +{ |
| 81 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 82 | + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL)); |
| 83 | + s_sta_netif = esp_netif_create_default_wifi_sta(); |
| 84 | + s_ap_netif = esp_netif_create_default_wifi_ap(); |
| 85 | + |
| 86 | + return ESP_OK; |
| 87 | +} |
| 88 | + |
| 89 | +static esp_err_t event_deinit(void) |
| 90 | +{ |
| 91 | + ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler)); |
| 92 | + esp_netif_destroy_default_wifi(s_sta_netif); |
| 93 | + esp_netif_destroy_default_wifi(s_ap_netif); |
| 94 | + ESP_ERROR_CHECK(esp_event_loop_delete_default()); |
| 95 | + return ESP_OK; |
| 96 | +} |
| 97 | + |
| 98 | +static void stop_wifi(void) |
| 99 | +{ |
| 100 | + TEST_ESP_OK(esp_wifi_stop()); |
| 101 | + event_deinit(); |
| 102 | + if (wifi_event) { |
| 103 | + vEventGroupDelete(wifi_event); |
| 104 | + wifi_event = NULL; |
| 105 | + } |
| 106 | + vTaskDelay(500 / portTICK_PERIOD_MS); |
| 107 | + TEST_ESP_OK(esp_wifi_deinit()); |
| 108 | +} |
| 109 | + |
| 110 | +static void start_wifi_as_sta(void) |
| 111 | +{ |
| 112 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 113 | + cfg.nvs_enable = false; |
| 114 | + set_leak_threshold(2000); |
| 115 | + |
| 116 | + event_init(); |
| 117 | + |
| 118 | + if (wifi_event == NULL) { |
| 119 | + wifi_event = xEventGroupCreate(); |
| 120 | + } else { |
| 121 | + xEventGroupClearBits(wifi_event, 0x00ffffff); |
| 122 | + } |
| 123 | + // Test wifi init |
| 124 | + TEST_ESP_OK(esp_wifi_init(&cfg)); |
| 125 | + |
| 126 | + wifi_config_t wifi_config = { |
| 127 | + .sta = { |
| 128 | + .ssid = TEST_DEFAULT_SSID, |
| 129 | + .password = TEST_DEFAULT_PWD, |
| 130 | + }, |
| 131 | + }; |
| 132 | + |
| 133 | + // Test wifi set mode and wifi start |
| 134 | + TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA)); |
| 135 | + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); |
| 136 | + TEST_ESP_OK(esp_wifi_start()); |
| 137 | +} |
| 138 | + |
| 139 | +static void test_station_connection(void) |
| 140 | +{ |
| 141 | + EventBits_t bits; |
| 142 | + start_wifi_as_sta(); |
| 143 | + |
| 144 | + // wait for softap to start |
| 145 | + vTaskDelay(1000 / portTICK_PERIOD_MS); |
| 146 | + |
| 147 | + TEST_ESP_OK(esp_wifi_connect()); |
| 148 | + ESP_LOGI(TAG, "start esp_wifi_connect: %s", TEST_DEFAULT_SSID); |
| 149 | + |
| 150 | + bits = xEventGroupWaitBits(wifi_event, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, |
| 151 | + pdTRUE, pdFALSE, portMAX_DELAY); |
| 152 | + TEST_ASSERT(bits & WIFI_CONNECTED_BIT); |
| 153 | + stop_wifi(); |
| 154 | +} |
| 155 | + |
| 156 | +static void start_wifi_as_softap(void) |
| 157 | +{ |
| 158 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 159 | + cfg.nvs_enable = false; |
| 160 | + set_leak_threshold(2000); |
| 161 | + |
| 162 | + event_init(); |
| 163 | + |
| 164 | + if (wifi_event == NULL) { |
| 165 | + wifi_event = xEventGroupCreate(); |
| 166 | + } else { |
| 167 | + xEventGroupClearBits(wifi_event, 0x00ffffff); |
| 168 | + } |
| 169 | + // Test wifi init |
| 170 | + TEST_ESP_OK(esp_wifi_init(&cfg)); |
| 171 | + |
| 172 | + wifi_config_t wifi_config = { |
| 173 | + .ap = { |
| 174 | + .ssid = TEST_DEFAULT_SSID, |
| 175 | + .password = TEST_DEFAULT_PWD, |
| 176 | + .channel = TEST_DEFAULT_CHANNEL, |
| 177 | + .authmode = WIFI_AUTH_WPA2_PSK, |
| 178 | + .max_connection = 5, |
| 179 | + }, |
| 180 | + }; |
| 181 | + |
| 182 | + TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_AP)); |
| 183 | + TEST_ESP_OK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); |
| 184 | + TEST_ESP_OK(esp_wifi_start()); |
| 185 | + ESP_LOGI(TAG, "start wifi softap: %s", TEST_DEFAULT_SSID); |
| 186 | +} |
| 187 | + |
| 188 | +static void test_softap_connection(void) |
| 189 | +{ |
| 190 | + EventBits_t bits; |
| 191 | + start_wifi_as_softap(); |
| 192 | + |
| 193 | + // wait for station to be connected |
| 194 | + bits = xEventGroupWaitBits(wifi_event, WIFI_AP_STA_CONNECTED, 1, 0, CONNECT_TIMEOUT_MS / portTICK_PERIOD_MS); |
| 195 | + TEST_ASSERT(bits & WIFI_AP_STA_CONNECTED); |
| 196 | + |
| 197 | + stop_wifi(); |
| 198 | +} |
| 199 | + |
| 200 | +/* Test that wifi starts, scans and stops normally when .bss segment is allowed to move to external memory */ |
| 201 | +TEST_CASE_MULTIPLE_DEVICES("test wifi connection for sta and softap when ESP_ALLOW_BSS_SEG_EXTERNAL_MEMORY is enabled", "[esp_wifi][test_env=wifi_two_dut]", test_station_connection, test_softap_connection); |
| 202 | + |
| 203 | +#endif // CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY |
0 commit comments