Sfoglia il codice sorgente

ESP-NETIF: Add unit test for additional API

David Cermak 6 anni fa
parent
commit
d8da662dec

+ 25 - 1
components/esp_netif/include/esp_netif.h

@@ -255,10 +255,25 @@ void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t eve
 
  * @param[in]  esp_netif Handle to esp-netif instance
  * @param[in]  mac Desired mac address for the related network interface
- * @return     ESP_OK
+ * @return
+ *         - ESP_OK - success
+ *         - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error
+ *         - ESP_ERR_NOT_SUPPORTED - mac not supported on this interface
  */
 esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]);
 
+/**
+ * @brief Get the mac address for the interface instance
+
+ * @param[in]  esp_netif Handle to esp-netif instance
+ * @param[out]  mac Resultant mac address for the related network interface
+ * @return
+ *         - ESP_OK - success
+ *         - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error
+ *         - ESP_ERR_NOT_SUPPORTED - mac not supported on this interface
+ */
+esp_err_t esp_netif_get_mac(esp_netif_t *esp_netif, uint8_t mac[]);
+
 /**
  * @brief  Set the hostname of an interface
  *
@@ -719,6 +734,15 @@ const char *esp_netif_get_ifkey(esp_netif_t *esp_netif);
  */
 const char *esp_netif_get_desc(esp_netif_t *esp_netif);
 
+/**
+ * @brief Returns configured routing priority number
+ *
+ * @param[in]  esp_netif Handle to esp-netif instance
+ *
+ * @return Integer representing the instance's route-prio, or -1 if invalid paramters
+ */
+int esp_netif_get_route_prio(esp_netif_t *esp_netif);
+
 /**
  * @brief Returns configured event for this esp-netif instance and supplied event type
  *

+ 24 - 0
components/esp_netif/lwip/esp_netif_lwip.c

@@ -528,6 +528,22 @@ esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[])
     return ESP_OK;
 }
 
+esp_err_t esp_netif_get_mac(esp_netif_t *esp_netif, uint8_t mac[])
+{
+    if (esp_netif == NULL || esp_netif->lwip_netif == NULL) {
+        return ESP_ERR_ESP_NETIF_IF_NOT_READY;
+    }
+    if (esp_netif->is_ppp_netif) {
+        return ESP_ERR_NOT_SUPPORTED;
+    }
+    if (esp_netif_is_netif_up(esp_netif)) {
+        memcpy(mac, esp_netif->lwip_netif->hwaddr, NETIF_MAX_HWADDR_LEN);
+        return ESP_OK;
+    }
+    memcpy(mac, esp_netif->mac, NETIF_MAX_HWADDR_LEN);
+    return ESP_OK;
+}
+
 
 static void esp_netif_dhcps_cb(u8_t client_ip[4])
 {
@@ -1434,6 +1450,14 @@ const char *esp_netif_get_desc(esp_netif_t *esp_netif)
     return esp_netif->if_desc;
 }
 
+int esp_netif_get_route_prio(esp_netif_t *esp_netif)
+{
+    if (esp_netif == NULL) {
+        return -1;
+    }
+    return esp_netif->route_prio;
+}
+
 int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type)
 {
     switch(event_type) {

+ 38 - 0
components/esp_netif/test/test_esp_netif.c

@@ -3,6 +3,8 @@
 #include "esp_netif.h"
 #include "esp_wifi.h"
 #include "nvs_flash.h"
+#include "esp_wifi_netif.h"
+#include <string.h>
 
 TEST_CASE("esp_netif: init and destroy", "[esp_netif]")
 {
@@ -187,3 +189,39 @@ TEST_CASE("esp_netif: test dhcp state transitions for mesh netifs", "[esp_netif]
     TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
     nvs_flash_deinit();
 }
+
+TEST_CASE("esp_netif: create custom wifi interfaces", "[esp_netif][leaks=0]")
+{
+    esp_netif_t *ap = NULL;
+    esp_netif_t *sta = NULL;
+    uint8_t configured_mac[6] = {1, 2, 3, 4, 5, 6};
+    uint8_t actual_mac[6] = { 0 };
+
+    // create customized station
+    esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
+    esp_netif_config.if_desc = "custom wifi station";
+    esp_netif_config.route_prio = 1;
+    sta = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
+    TEST_ASSERT_NOT_NULL(sta);
+    TEST_ASSERT_EQUAL_STRING("custom wifi station", esp_netif_get_desc(sta));
+    TEST_ASSERT_EQUAL(1, esp_netif_get_route_prio(sta));
+
+    // create customized access point
+    esp_netif_inherent_config_t esp_netif_config2 = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
+    esp_netif_config2.if_desc = "custom wifi ap";
+    esp_netif_config2.route_prio = 10;
+    memcpy(esp_netif_config2.mac, configured_mac, 6);
+
+    ap = esp_netif_create_wifi(WIFI_IF_AP, &esp_netif_config2);
+    TEST_ASSERT_NOT_NULL(ap);
+    TEST_ASSERT_EQUAL_STRING( "custom wifi ap", esp_netif_get_desc(ap));
+    TEST_ASSERT_EQUAL(10, esp_netif_get_route_prio(ap));
+    TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_mac(ap, actual_mac));
+    TEST_ASSERT_EQUAL_HEX8_ARRAY(configured_mac, actual_mac, 6);
+
+    esp_wifi_destroy_if_driver(esp_netif_get_io_driver(ap));
+    esp_wifi_destroy_if_driver(esp_netif_get_io_driver(sta));
+    esp_netif_destroy(ap);
+    esp_netif_destroy(sta);
+}
+