]> granicus.if.org Git - esp-idf/commitdiff
components/tcpip_adapter: Allow to set different hostname for each interface
authorLiu Han <liuhan@espressif.com>
Wed, 7 Dec 2016 07:37:59 +0000 (15:37 +0800)
committerWu Jian Gang <wujiangang@espressif.com>
Tue, 10 Jan 2017 04:42:14 +0000 (12:42 +0800)
components/lwip/port/netif/ethernetif.c [changed mode: 0755->0644]
components/lwip/port/netif/wlanif.c [changed mode: 0755->0644]
components/tcpip_adapter/include/tcpip_adapter.h
components/tcpip_adapter/tcpip_adapter_lwip.c

old mode 100755 (executable)
new mode 100644 (file)
index 6b1245e..90a5b24
@@ -55,8 +55,6 @@
 #define IFNAME0 'e'
 #define IFNAME1 'n'
 
-static char hostname[16];
-
 /**
  * In this function, the hardware should be initialized.
  * Called from ethernetif_init().
@@ -78,14 +76,13 @@ ethernet_low_level_init(struct netif *netif)
   /* device capabilities */
   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
-  
+
 #if ESP_LWIP
 #if LWIP_IGMP
-
-     netif->flags |= NETIF_FLAG_IGMP;
+  netif->flags |= NETIF_FLAG_IGMP;
 #endif
 #endif
-  /* Do whatever else is needed to initialize interface. */  
+  /* Do whatever else is needed to initialize interface. */
 }
 
 /**
@@ -113,30 +110,28 @@ ethernet_low_level_output(struct netif *netif, struct pbuf *p)
     LWIP_DEBUGF(NETIF_DEBUG,("eth_if=%d netif=%p pbuf=%p len=%d\n", eth_if, netif, p, p->len)); 
 
     return ERR_IF;
-  } 
-  
+  }
+
 #if ESP_LWIP
-    q = p;
-    u16_t pbuf_x_len = 0;
-    pbuf_x_len = q->len;
-    if(q->next !=NULL)
-    {
-        //char cnt = 0;
-        struct pbuf *tmp = q->next;
-        while(tmp != NULL)
-        {
-            memcpy( (u8_t *)( (u8_t *)(q->payload) + pbuf_x_len), (u8_t *)tmp->payload , tmp->len );
-            pbuf_x_len += tmp->len;
-            //cnt++;
-            tmp = tmp->next;
-        }
+  q = p;
+  u16_t pbuf_x_len = 0;
+  pbuf_x_len = q->len;
+  if(q->next !=NULL) {
+    //char cnt = 0;
+    struct pbuf *tmp = q->next;
+    while(tmp != NULL) {
+      memcpy( (u8_t *)( (u8_t *)(q->payload) + pbuf_x_len), (u8_t *)tmp->payload , tmp->len );
+      pbuf_x_len += tmp->len;
+      //cnt++;
+      tmp = tmp->next;
     }
-   
-    return esp_eth_tx(q->payload, pbuf_x_len);
+  }
+
+  return esp_eth_tx(q->payload, pbuf_x_len);
 #else
-    for(q = p; q != NULL; q = q->next) {
-        return esp_emac_tx(q->payload, q->len);
-    }
+  for(q = p; q != NULL; q = q->next) {
+    return esp_emac_tx(q->payload, q->len);
+  }
   return ERR_OK;
 #endif
 }
@@ -154,9 +149,9 @@ void
 ethernetif_input(struct netif *netif, void *buffer, uint16_t len)
 {
   struct pbuf *p;
-  
+
   if(buffer== NULL || netif == NULL)
-       goto _exit;
+    goto _exit;
 #if CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
   p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
   if (p == NULL) {
@@ -178,7 +173,7 @@ if (netif->input(p, netif) != ERR_OK) {
   p->payload = buffer;
   p->user_flag = PBUF_USER_FLAG_OWNER_ETH;
   p->user_buf = buffer;
+
   /* full packet send to tcpip_thread to process */
 if (netif->input(p, netif) != ERR_OK) {
   LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
@@ -187,7 +182,7 @@ if (netif->input(p, netif) != ERR_OK) {
 }
 #endif
 _exit:
-;        
+;
 }
 
 /**
@@ -211,14 +206,11 @@ ethernetif_init(struct netif *netif)
   /* Initialize interface hostname */
 
 #if ESP_LWIP
-  sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]);
-  netif->hostname = hostname;
-  
+  netif->hostname = "espressif";
 #else
-  sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]);
-  netif->hostname = hostname;
+  netif->hostname = "lwip";
 #endif
-  
+
 #endif /* LWIP_NETIF_HOSTNAME */
 
   /*
@@ -239,7 +231,7 @@ ethernetif_init(struct netif *netif)
   netif->output_ip6 = ethip6_output;
 #endif /* LWIP_IPV6 */
   netif->linkoutput = ethernet_low_level_output;
-  
+
   /* initialize the hardware */
   ethernet_low_level_init(netif);
 
old mode 100755 (executable)
new mode 100644 (file)
index e114105..f9def49
@@ -56,8 +56,6 @@
 #define IFNAME0 'e'
 #define IFNAME1 'n'
 
-static char hostname[16];
-
 /**
  * In this function, the hardware should be initialized.
  * Called from ethernetif_init().
@@ -67,11 +65,7 @@ static char hostname[16];
  */
 static void
 low_level_init(struct netif *netif)
-{ 
-
-
-
-
+{
   /* set MAC hardware address length */
   netif->hwaddr_len = ETHARP_HWADDR_LEN;
 
@@ -83,18 +77,14 @@ low_level_init(struct netif *netif)
   /* device capabilities */
   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
-  
-#if ESP_LWIP
 
+#if ESP_LWIP
 #if LWIP_IGMP
-
-     netif->flags |= NETIF_FLAG_IGMP;
+  netif->flags |= NETIF_FLAG_IGMP;
+#endif
 #endif
 
-
- #endif
-  /* Do whatever else is needed to initialize interface. */  
+  /* Do whatever else is needed to initialize interface. */
 }
 
 /**
@@ -115,29 +105,29 @@ low_level_init(struct netif *netif)
 static err_t
 low_level_output(struct netif *netif, struct pbuf *p)
 {
-    wifi_interface_t wifi_if = tcpip_adapter_get_esp_if(netif);
-    struct pbuf *q = p;
-    err_t ret;
+  wifi_interface_t wifi_if = tcpip_adapter_get_esp_if(netif);
+  struct pbuf *q = p;
+  err_t ret;
 
-    if (wifi_if >= ESP_IF_MAX) {
-        return ERR_IF;
-    }
+  if (wifi_if >= ESP_IF_MAX) {
+    return ERR_IF;
+  }
 
-    if(q->next == NULL) {
-        ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len);
+  if(q->next == NULL) {
+    ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len);
+  } else {
+    LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
+    q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
+    if (q != NULL) {
+      pbuf_copy(q, p);
     } else {
-        LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug"));
-        q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM);
-        if (q != NULL) {
-            pbuf_copy(q, p);
-        } else {
-            return ERR_MEM;
-        }
-        ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len);
-        pbuf_free(q);
+      return ERR_MEM;
     }
-  
-    return ret; 
+    ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len);
+    pbuf_free(q);
+  }
+
+  return ret;
 }
 
 /**
@@ -153,9 +143,9 @@ void
 wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
 {
   struct pbuf *p;
-  
+
   if(!buffer || !netif)
-       goto _exit;
+    goto _exit;
 
 #if (ESP_L2_TO_L3_COPY == 1)
   p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
@@ -182,9 +172,9 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
     pbuf_free(p);
   }
-  
+
 _exit:
-;        
+;
 }
 
 /**
@@ -208,25 +198,11 @@ wlanif_init(struct netif *netif)
   /* Initialize interface hostname */
 
 #if ESP_LWIP
-//TO_DO
-/*
-  if ((struct netif *)wifi_get_netif(STATION_IF) == netif) {
-      if (default_hostname == 1) {
-          wifi_station_set_default_hostname(netif->hwaddr);
-      }
-      netif->hostname = hostname;
-  } else {
-      netif->hostname = NULL;
-  }
-*/
-  sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]);
-  netif->hostname = hostname;
-  
+  netif->hostname = "espressif";
 #else
-  sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]);
-  netif->hostname = hostname;
+  netif->hostname = "lwip";
 #endif
-  
+
 #endif /* LWIP_NETIF_HOSTNAME */
 
   /*
@@ -247,7 +223,7 @@ wlanif_init(struct netif *netif)
   netif->output_ip6 = ethip6_output;
 #endif /* LWIP_IPV6 */
   netif->linkoutput = low_level_output;
-  
+
   /* initialize the hardware */
   low_level_init(netif);
 
index 861f7ccb8da81165e63645a1733ef5b2738fb59f..07bdc12f913203a495ac7ffc06551385a23c038f 100644 (file)
@@ -98,7 +98,7 @@ typedef struct {
 typedef enum {
     TCPIP_ADAPTER_IF_STA = 0,     /**< ESP32 station interface */
     TCPIP_ADAPTER_IF_AP,          /**< ESP32 soft-AP interface */
-    TCPIP_ADAPTER_IF_ETH,     /**< ESP32 ethernet interface */
+    TCPIP_ADAPTER_IF_ETH,         /**< ESP32 ethernet interface */
     TCPIP_ADAPTER_IF_MAX
 } tcpip_adapter_if_t;
 
@@ -126,7 +126,7 @@ typedef enum{
 } tcpip_adapter_option_id_t;
 
 /**
- * @brief  Initialize tcpip adpater
+ * @brief  Initialize tcpip adapter
  *
  * This will initialize TCPIP stack inside.
  */
@@ -411,12 +411,12 @@ esp_interface_t tcpip_adapter_get_esp_if(void *dev);
  */
 esp_err_t tcpip_adapter_get_sta_list(wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list);
 
-#define TCPIP_HOSTNAME_MAX_SIZE    31
+#define TCPIP_HOSTNAME_MAX_SIZE    32
 /**
  * @brief  Set the hostname to the interface
  *
  * @param[in]   tcpip_if: the interface which we will set the hostname
- * @param[in]   hostname: the host name for set the interfce
+ * @param[in]   hostname: the host name for set the interface, the max length of hostname is 32 bytes
  *
  * @return ESP_OK:success
  *         ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:interface status error
@@ -428,7 +428,7 @@ esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *ho
  * @brief  Get the hostname from the interface
  *
  * @param[in]   tcpip_if: the interface which we will get the hostname
- * @param[in]   hostname: the host name from the interfce
+ * @param[in]   hostname: the host name from the interface
  *
  * @return ESP_OK:success
  *         ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:interface status error
index 726434b5fa07f19f671ac3bf5234c8b66163a946..03f364fe67353f65943e528d47e34e9b8795603e 100644 (file)
@@ -706,34 +706,35 @@ esp_err_t tcpip_adapter_get_sta_list(wifi_sta_list_t *wifi_sta_list, tcpip_adapt
 
 esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname)
 {
+#if LWIP_NETIF_HOSTNAME
     struct netif *p_netif;
-    static char hostinfo[TCPIP_HOSTNAME_MAX_SIZE + 1];
+    static char hostinfo[TCPIP_HOSTNAME_MAX_SIZE + 1][TCPIP_ADAPTER_IF_MAX];
 
     if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
         return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
     }
 
-    if (strlen(hostname) >= TCPIP_HOSTNAME_MAX_SIZE) {
+    if (strlen(hostname) > TCPIP_HOSTNAME_MAX_SIZE) {
         return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
     }
 
     p_netif = esp_netif[tcpip_if];
     if (p_netif != NULL) {
-        if (netif_is_up(p_netif)) {
-            return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
-        } else {
-            memset(hostinfo, 0, sizeof(hostinfo));
-            memcpy(hostinfo, hostname, strlen(hostname));
-            p_netif->hostname = hostinfo;
-            return ESP_OK;
-        }
+        memset(hostinfo[tcpip_if], 0, sizeof(hostinfo[tcpip_if]));
+        memcpy(hostinfo[tcpip_if], hostname, strlen(hostname));
+        p_netif->hostname = hostinfo[tcpip_if];
+        return ESP_OK;
     } else {
-        return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
+        return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
     }
+#else
+    return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
+#endif
 }
 
 esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname)
 {
+#if LWIP_NETIF_HOSTNAME
     struct netif *p_netif = NULL;
     if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
         return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
@@ -746,6 +747,9 @@ esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **h
     } else {
         return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
     }
+#else
+    return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
+#endif
 }
 
 #endif