]> granicus.if.org Git - esp-idf/commitdiff
tcpip_adapter: add set hostname interface
authorliuhan <liuhan@espressif.com>
Thu, 3 Nov 2016 05:30:32 +0000 (13:30 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 15 Nov 2016 03:46:52 +0000 (11:46 +0800)
components/tcpip_adapter/include/tcpip_adapter.h
components/tcpip_adapter/tcpip_adapter_lwip.c

index e84701688433d46cc1bd6a34dac1893ec08aa418..45d6ade305cc4191346ddd45faa6d1544b347ffa 100644 (file)
@@ -372,6 +372,19 @@ wifi_interface_t tcpip_adapter_get_wifi_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
+/**
+ * @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
+ *
+ * @return ESP_OK:success
+ *         ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:interface status error
+ *         ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS:parameter error
+ */
+esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname);
+
 #ifdef __cplusplus
 }
 #endif
index 9b6e9d94fa1b9454a1e37a961c8f30709e76266f..3edc90509effee8b6de0cd8adfa1c1b94016a88f 100644 (file)
@@ -607,4 +607,32 @@ esp_err_t tcpip_adapter_get_sta_list(wifi_sta_list_t *wifi_sta_list, tcpip_adapt
     return ESP_OK;
 }
 
+esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname)
+{
+    struct netif *p_netif;
+    static char hostinfo[TCPIP_HOSTNAME_MAX_SIZE + 1];
+
+    if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
+        return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
+    }
+
+    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;
+        }
+    } else {
+        return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
+    }
+}
+
 #endif