|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
5 | 5 | */ |
|
18 | 18 | #include "memory_checks.h" |
19 | 19 | #include "lwip/netif.h" |
20 | 20 | #include "esp_netif_test.h" |
| 21 | +#include "esp_event.h" |
| 22 | +#include "freertos/semphr.h" |
21 | 23 |
|
22 | 24 | TEST_GROUP(esp_netif); |
23 | 25 |
|
@@ -147,6 +149,87 @@ TEST(esp_netif, find_netifs) |
147 | 149 | } |
148 | 150 | } |
149 | 151 |
|
| 152 | +static esp_err_t dummy_transmit(void* hd, void *buf, size_t length) |
| 153 | +{ |
| 154 | + return ESP_OK; |
| 155 | +} |
| 156 | + |
| 157 | +static SemaphoreHandle_t s_netif_up_sem; |
| 158 | +static SemaphoreHandle_t s_netif_down_sem; |
| 159 | +static volatile int s_netif_up_count; |
| 160 | +static volatile int s_netif_down_count; |
| 161 | + |
| 162 | +static void netif_status_evt_handler(void *arg, esp_event_base_t base, int32_t id, void *data) |
| 163 | +{ |
| 164 | + (void)arg; (void)base; (void)data; |
| 165 | + if (id == IP_EVENT_NETIF_UP) { |
| 166 | + s_netif_up_count++; |
| 167 | + if (s_netif_up_sem) { |
| 168 | + xSemaphoreGive(s_netif_up_sem); |
| 169 | + } |
| 170 | + } else if (id == IP_EVENT_NETIF_DOWN) { |
| 171 | + s_netif_down_count++; |
| 172 | + if (s_netif_down_sem) { |
| 173 | + xSemaphoreGive(s_netif_down_sem); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +TEST(esp_netif, unified_netif_status_event) |
| 179 | +{ |
| 180 | + test_case_uses_tcpip(); |
| 181 | + TEST_ESP_OK(esp_event_loop_create_default()); |
| 182 | + |
| 183 | + s_netif_up_sem = xSemaphoreCreateBinary(); |
| 184 | + s_netif_down_sem = xSemaphoreCreateBinary(); |
| 185 | + TEST_ASSERT_NOT_NULL(s_netif_up_sem); |
| 186 | + TEST_ASSERT_NOT_NULL(s_netif_down_sem); |
| 187 | + s_netif_up_count = 0; |
| 188 | + s_netif_down_count = 0; |
| 189 | + |
| 190 | + TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_NETIF_UP, &netif_status_evt_handler, NULL)); |
| 191 | + TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_NETIF_DOWN, &netif_status_evt_handler, NULL)); |
| 192 | + |
| 193 | + // Create a simple netif (no real driver needed) |
| 194 | + esp_netif_driver_ifconfig_t driver_config = { .handle = (void*)1, .transmit = dummy_transmit }; |
| 195 | + esp_netif_inherent_config_t base_netif_config = { .if_key = "if_status", .if_desc = "if_status" }; |
| 196 | + esp_netif_config_t cfg = { .base = &base_netif_config, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, .driver = &driver_config }; |
| 197 | + esp_netif_t *netif = esp_netif_new(&cfg); |
| 198 | + TEST_ASSERT_NOT_NULL(netif); |
| 199 | + |
| 200 | + // Bring interface up (should emit exactly one NETIF_UP) |
| 201 | + esp_netif_action_start(netif, NULL, 0, NULL); |
| 202 | + esp_netif_action_connected(netif, NULL, 0, NULL); |
| 203 | + TEST_ASSERT_EQUAL(pdTRUE, xQueueSemaphoreTake(s_netif_up_sem, pdMS_TO_TICKS(1000))); |
| 204 | + vTaskDelay(pdMS_TO_TICKS(50)); |
| 205 | + TEST_ASSERT_EQUAL(1, s_netif_up_count); |
| 206 | + TEST_ASSERT_EQUAL(0, s_netif_down_count); |
| 207 | + |
| 208 | + // Bring interface down (should emit exactly one NETIF_DOWN) |
| 209 | + esp_netif_action_disconnected(netif, NULL, 0, NULL); |
| 210 | + TEST_ASSERT_EQUAL(pdTRUE, xQueueSemaphoreTake(s_netif_down_sem, pdMS_TO_TICKS(1000))); |
| 211 | + vTaskDelay(pdMS_TO_TICKS(50)); |
| 212 | + TEST_ASSERT_EQUAL(1, s_netif_down_count); |
| 213 | + TEST_ASSERT_EQUAL(1, s_netif_up_count); |
| 214 | + |
| 215 | + // Bring up again (should increment up count to 2) |
| 216 | + esp_netif_action_connected(netif, NULL, 0, NULL); |
| 217 | + TEST_ASSERT_EQUAL(pdTRUE, xQueueSemaphoreTake(s_netif_up_sem, pdMS_TO_TICKS(1000))); |
| 218 | + vTaskDelay(pdMS_TO_TICKS(50)); |
| 219 | + TEST_ASSERT_EQUAL(2, s_netif_up_count); |
| 220 | + TEST_ASSERT_EQUAL(1, s_netif_down_count); |
| 221 | + |
| 222 | + // Cleanup |
| 223 | + TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_NETIF_UP, &netif_status_evt_handler)); |
| 224 | + TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_NETIF_DOWN, &netif_status_evt_handler)); |
| 225 | + vSemaphoreDelete(s_netif_up_sem); |
| 226 | + vSemaphoreDelete(s_netif_down_sem); |
| 227 | + s_netif_up_sem = s_netif_down_sem = NULL; |
| 228 | + |
| 229 | + esp_netif_destroy(netif); |
| 230 | + TEST_ESP_OK(esp_event_loop_delete_default()); |
| 231 | +} |
| 232 | + |
150 | 233 | #ifdef CONFIG_ESP_WIFI_ENABLED |
151 | 234 | /* |
152 | 235 | * This test creates a default WiFi station and checks all possible transitions |
@@ -438,11 +521,6 @@ TEST(esp_netif, get_set_hostname) |
438 | 521 | } |
439 | 522 | #endif // CONFIG_ESP_WIFI_ENABLED |
440 | 523 |
|
441 | | -static esp_err_t dummy_transmit(void* hd, void *buf, size_t length) |
442 | | -{ |
443 | | - return ESP_OK; |
444 | | -} |
445 | | - |
446 | 524 | /* |
447 | 525 | * This test validates the route priority of multiple netifs. It checks that the default route (default netif) |
448 | 526 | * is set correctly for the netifs according to their `route_prio` value and `link_up` state. |
@@ -610,6 +688,7 @@ TEST_GROUP_RUNNER(esp_netif) |
610 | 688 | #endif |
611 | 689 | RUN_TEST_CASE(esp_netif, route_priority) |
612 | 690 | RUN_TEST_CASE(esp_netif, set_get_dnsserver) |
| 691 | + RUN_TEST_CASE(esp_netif, unified_netif_status_event) |
613 | 692 | } |
614 | 693 |
|
615 | 694 | void app_main(void) |
|
0 commit comments