Просмотр исходного кода

esp_netif: Extend unit test to validate route_prio

Also updates the auto routing loop after a netif is destroyed
David Cermak 4 лет назад
Родитель
Сommit
fabad2a13a

+ 7 - 11
components/esp_netif/lwip/esp_netif_lwip.c

@@ -104,7 +104,7 @@ static const char *TAG = "esp_netif_lwip";
 
 static bool tcpip_initialized = false;
 static esp_netif_t *s_last_default_esp_netif = NULL;
-static bool s_manual_last_default_esp_netif = false;
+static bool s_is_last_default_esp_netif_overridden = false;
 
 #if !LWIP_TCPIP_CORE_LOCKING
 static sys_sem_t api_sync_sem = NULL;
@@ -142,7 +142,7 @@ static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t
             .api_fn = fn
     };
 #if !LWIP_TCPIP_CORE_LOCKING
-    if (g_lwip_task != xTaskGetCurrentTaskHandle()) {
+    if (tcpip_initialized && g_lwip_task != xTaskGetCurrentTaskHandle()) {
         ESP_LOGD(TAG, "check: remote, if=%p fn=%p\n", netif, fn);
         sys_arch_sem_wait(&api_lock_sem, 0);
         tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, &msg, &api_sync_sem);
@@ -201,19 +201,19 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
 
     ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
 
-    if (s_manual_last_default_esp_netif && action != ESP_NETIF_SET_DEFAULT) {
+    if (s_is_last_default_esp_netif_overridden && action != ESP_NETIF_SET_DEFAULT) {
         // check if manually configured default interface hasn't been destroyed
         s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
-        if (esp_netif_is_active(s_last_default_esp_netif) != NULL) {
+        if (s_last_default_esp_netif != NULL) {
             return ESP_OK; // still valid -> don't update default netif
         }
         // invalid -> reset the manual override and perform auto update
-        s_manual_last_default_esp_netif = false;
+        s_is_last_default_esp_netif_overridden = false;
     }
     switch (action) {
         case ESP_NETIF_SET_DEFAULT:
             s_last_default_esp_netif = esp_netif;
-            s_manual_last_default_esp_netif = true;
+            s_is_last_default_esp_netif_overridden = true;
             esp_netif_set_default_netif_internal(s_last_default_esp_netif);
         break;
         case ESP_NETIF_STARTED:
@@ -626,11 +626,7 @@ void esp_netif_destroy(esp_netif_t *esp_netif)
 #if CONFIG_ESP_NETIF_L2_TAP
         vSemaphoreDelete(esp_netif->transmit_mutex);
 #endif // CONFIG_ESP_NETIF_L2_TAP
-        if (s_last_default_esp_netif == esp_netif) {
-            // clear last default netif if it happens to be this just destroyed interface
-            s_last_default_esp_netif = NULL;
-            s_manual_last_default_esp_netif = false;
-        }
+        esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED);
         free(esp_netif);
     }
 }

+ 61 - 1
components/esp_netif/test/test_esp_netif.c

@@ -1,10 +1,12 @@
+#include <string.h>
 #include "unity.h"
 #include "test_utils.h"
 #include "esp_netif.h"
 #include "esp_wifi.h"
 #include "nvs_flash.h"
 #include "esp_wifi_netif.h"
-#include <string.h>
+#include "lwip/netif.h"
+#include "esp_netif_net_stack.h"
 
 
 TEST_CASE("esp_netif: init and destroy", "[esp_netif]")
@@ -353,3 +355,61 @@ TEST_CASE("esp_netif: create and destroy default wifi interfaces", "[esp_netif][
     TEST_ASSERT_NOT_NULL(sta);
     esp_netif_destroy_default_wifi(sta);
 }
+
+static esp_err_t dummy_transmit(void* hd, void *buf, size_t length)
+{
+    return ESP_OK;
+}
+
+TEST_CASE("esp_netif: test routing priority", "[esp_netif]")
+{
+    test_case_uses_tcpip();
+    // interface key has to be a unique identifier
+    const char *if_keys[] = {"if0", "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9"};
+    const int nr_of_netifs = sizeof(if_keys) / sizeof(char *);
+    esp_netif_t *netifs[nr_of_netifs];
+    esp_netif_driver_ifconfig_t driver_config = { .handle =  (void*)1, .transmit = dummy_transmit };
+    // create 10 netifs with different route prio
+    int max_prio_i = nr_of_netifs / 2;  // index of netif with maximum route-prio
+    for (int i = 0; i < nr_of_netifs; ++i) {
+        esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i],
+                                                          .route_prio = i > max_prio_i ? 0 : i };
+        esp_netif_config_t cfg = {  .base = &base_netif_config,
+                                    .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA,
+                                    .driver = &driver_config };
+        netifs[i] = esp_netif_new(&cfg);
+        TEST_ASSERT_NOT_NULL(netifs[i]);
+        // set the interface up and connected -- to enable the default netif based on route_prio
+        esp_netif_action_start(netifs[i], 0, 0, 0);
+        esp_netif_action_connected(netifs[i], 0, 0, 0);
+    }
+    // route_prio increases with index until max_prio_i -> check this is the default netif
+    TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i]), netif_default);
+    // now we stop the max_prio netif and check the default is on the previous index (max_prio-1)
+    esp_netif_action_stop(netifs[max_prio_i], 0, 0, 0);
+    TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i - 1]), netif_default);
+
+    // now we override the default netif with API (which has route_prio == 0)
+    int override_prio_i = nr_of_netifs - 1;  // last netif to be set-default manually
+    esp_netif_set_default_netif(netifs[override_prio_i]);
+    // check the configured netif is default
+    TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[override_prio_i]), netif_default);
+    // try to start/connect the previously stopped netif with max_prio
+    esp_netif_action_start(netifs[max_prio_i], 0, 0, 0);
+    esp_netif_action_connected(netifs[max_prio_i], 0, 0, 0);
+    // and check the configured netif is still the default
+    TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[override_prio_i]), netif_default);
+    // we destroy the configured default netif
+    esp_netif_destroy(netifs[override_prio_i]);
+    // ...and check the max-prio netif is default now
+    TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i]), netif_default);
+    // stop the max_prio netif, to see the auto-default still works
+    esp_netif_action_stop(netifs[max_prio_i], 0, 0, 0);
+    // ...so the current default is on (max_prio-1)
+    TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i - 1]), netif_default);
+    // destroy one by one and check it's been removed
+    for (int i=0; i < override_prio_i; ++i) {
+        esp_netif_destroy(netifs[i]);
+        TEST_ASSERT_FALSE(esp_netif_is_netif_listed(netifs[i]));
+    }
+}