components/tcpip_adapter: Allow to set different hostname for each interface

This commit is contained in:
Liu Han 2016-12-07 15:37:59 +08:00 committed by Wu Jian Gang
parent fb70126bc8
commit 315b3f979f
4 changed files with 82 additions and 110 deletions

68
components/lwip/port/netif/ethernetif.c Executable file → Normal file
View File

@ -55,8 +55,6 @@
#define IFNAME0 'e' #define IFNAME0 'e'
#define IFNAME1 'n' #define IFNAME1 'n'
static char hostname[16];
/** /**
* In this function, the hardware should be initialized. * In this function, the hardware should be initialized.
* Called from ethernetif_init(). * Called from ethernetif_init().
@ -78,14 +76,13 @@ ethernet_low_level_init(struct netif *netif)
/* device capabilities */ /* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ /* 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; netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
#if ESP_LWIP #if ESP_LWIP
#if LWIP_IGMP #if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
netif->flags |= NETIF_FLAG_IGMP;
#endif #endif
#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)); LWIP_DEBUGF(NETIF_DEBUG,("eth_if=%d netif=%p pbuf=%p len=%d\n", eth_if, netif, p, p->len));
return ERR_IF; return ERR_IF;
} }
#if ESP_LWIP #if ESP_LWIP
q = p; q = p;
u16_t pbuf_x_len = 0; u16_t pbuf_x_len = 0;
pbuf_x_len = q->len; pbuf_x_len = q->len;
if(q->next !=NULL) if(q->next !=NULL) {
{ //char cnt = 0;
//char cnt = 0; struct pbuf *tmp = q->next;
struct pbuf *tmp = q->next; while(tmp != NULL) {
while(tmp != NULL) memcpy( (u8_t *)( (u8_t *)(q->payload) + pbuf_x_len), (u8_t *)tmp->payload , tmp->len );
{ pbuf_x_len += tmp->len;
memcpy( (u8_t *)( (u8_t *)(q->payload) + pbuf_x_len), (u8_t *)tmp->payload , tmp->len ); //cnt++;
pbuf_x_len += tmp->len; tmp = tmp->next;
//cnt++;
tmp = tmp->next;
}
} }
}
return esp_eth_tx(q->payload, pbuf_x_len);
return esp_eth_tx(q->payload, pbuf_x_len);
#else #else
for(q = p; q != NULL; q = q->next) { for(q = p; q != NULL; q = q->next) {
return esp_emac_tx(q->payload, q->len); return esp_emac_tx(q->payload, q->len);
} }
return ERR_OK; return ERR_OK;
#endif #endif
} }
@ -154,9 +149,9 @@ void
ethernetif_input(struct netif *netif, void *buffer, uint16_t len) ethernetif_input(struct netif *netif, void *buffer, uint16_t len)
{ {
struct pbuf *p; struct pbuf *p;
if(buffer== NULL || netif == NULL) if(buffer== NULL || netif == NULL)
goto _exit; goto _exit;
#if CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE #if CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
if (p == NULL) { if (p == NULL) {
@ -178,7 +173,7 @@ if (netif->input(p, netif) != ERR_OK) {
p->payload = buffer; p->payload = buffer;
p->user_flag = PBUF_USER_FLAG_OWNER_ETH; p->user_flag = PBUF_USER_FLAG_OWNER_ETH;
p->user_buf = buffer; p->user_buf = buffer;
/* full packet send to tcpip_thread to process */ /* full packet send to tcpip_thread to process */
if (netif->input(p, netif) != ERR_OK) { if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
@ -187,7 +182,7 @@ if (netif->input(p, netif) != ERR_OK) {
} }
#endif #endif
_exit: _exit:
; ;
} }
/** /**
@ -211,14 +206,11 @@ ethernetif_init(struct netif *netif)
/* Initialize interface hostname */ /* Initialize interface hostname */
#if ESP_LWIP #if ESP_LWIP
sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]); netif->hostname = "espressif";
netif->hostname = hostname;
#else #else
sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]); netif->hostname = "lwip";
netif->hostname = hostname;
#endif #endif
#endif /* LWIP_NETIF_HOSTNAME */ #endif /* LWIP_NETIF_HOSTNAME */
/* /*
@ -239,7 +231,7 @@ ethernetif_init(struct netif *netif)
netif->output_ip6 = ethip6_output; netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */ #endif /* LWIP_IPV6 */
netif->linkoutput = ethernet_low_level_output; netif->linkoutput = ethernet_low_level_output;
/* initialize the hardware */ /* initialize the hardware */
ethernet_low_level_init(netif); ethernet_low_level_init(netif);

88
components/lwip/port/netif/wlanif.c Executable file → Normal file
View File

@ -56,8 +56,6 @@
#define IFNAME0 'e' #define IFNAME0 'e'
#define IFNAME1 'n' #define IFNAME1 'n'
static char hostname[16];
/** /**
* In this function, the hardware should be initialized. * In this function, the hardware should be initialized.
* Called from ethernetif_init(). * Called from ethernetif_init().
@ -67,11 +65,7 @@ static char hostname[16];
*/ */
static void static void
low_level_init(struct netif *netif) low_level_init(struct netif *netif)
{ {
/* set MAC hardware address length */ /* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN; netif->hwaddr_len = ETHARP_HWADDR_LEN;
@ -83,18 +77,14 @@ low_level_init(struct netif *netif)
/* device capabilities */ /* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ /* 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; netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
#if ESP_LWIP #if ESP_LWIP
#if LWIP_IGMP #if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
netif->flags |= NETIF_FLAG_IGMP; #endif
#endif #endif
/* Do whatever else is needed to initialize interface. */
#endif
/* Do whatever else is needed to initialize interface. */
} }
/** /**
@ -115,29 +105,29 @@ low_level_init(struct netif *netif)
static err_t static err_t
low_level_output(struct netif *netif, struct pbuf *p) low_level_output(struct netif *netif, struct pbuf *p)
{ {
wifi_interface_t wifi_if = tcpip_adapter_get_esp_if(netif); wifi_interface_t wifi_if = tcpip_adapter_get_esp_if(netif);
struct pbuf *q = p; struct pbuf *q = p;
err_t ret; err_t ret;
if (wifi_if >= ESP_IF_MAX) { if (wifi_if >= ESP_IF_MAX) {
return ERR_IF; return ERR_IF;
} }
if(q->next == NULL) { if(q->next == NULL) {
ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len); 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 { } else {
LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug")); return ERR_MEM;
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);
} }
ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len);
return ret; pbuf_free(q);
}
return ret;
} }
/** /**
@ -153,9 +143,9 @@ void
wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb)
{ {
struct pbuf *p; struct pbuf *p;
if(!buffer || !netif) if(!buffer || !netif)
goto _exit; goto _exit;
#if (ESP_L2_TO_L3_COPY == 1) #if (ESP_L2_TO_L3_COPY == 1)
p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); 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")); LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p); pbuf_free(p);
} }
_exit: _exit:
; ;
} }
/** /**
@ -208,25 +198,11 @@ wlanif_init(struct netif *netif)
/* Initialize interface hostname */ /* Initialize interface hostname */
#if ESP_LWIP #if ESP_LWIP
//TO_DO netif->hostname = "espressif";
/*
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;
#else #else
sprintf(hostname, "ESP_%02X%02X%02X", netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]); netif->hostname = "lwip";
netif->hostname = hostname;
#endif #endif
#endif /* LWIP_NETIF_HOSTNAME */ #endif /* LWIP_NETIF_HOSTNAME */
/* /*
@ -247,7 +223,7 @@ wlanif_init(struct netif *netif)
netif->output_ip6 = ethip6_output; netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */ #endif /* LWIP_IPV6 */
netif->linkoutput = low_level_output; netif->linkoutput = low_level_output;
/* initialize the hardware */ /* initialize the hardware */
low_level_init(netif); low_level_init(netif);

View File

@ -98,7 +98,7 @@ typedef struct {
typedef enum { typedef enum {
TCPIP_ADAPTER_IF_STA = 0, /**< ESP32 station interface */ TCPIP_ADAPTER_IF_STA = 0, /**< ESP32 station interface */
TCPIP_ADAPTER_IF_AP, /**< ESP32 soft-AP 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_MAX
} tcpip_adapter_if_t; } tcpip_adapter_if_t;
@ -126,7 +126,7 @@ typedef enum{
} tcpip_adapter_option_id_t; } tcpip_adapter_option_id_t;
/** /**
* @brief Initialize tcpip adpater * @brief Initialize tcpip adapter
* *
* This will initialize TCPIP stack inside. * 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); 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 * @brief Set the hostname to the interface
* *
* @param[in] tcpip_if: the interface which we will set the hostname * @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 * @return ESP_OK:success
* ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:interface status error * 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 * @brief Get the hostname from the interface
* *
* @param[in] tcpip_if: the interface which we will get the hostname * @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 * @return ESP_OK:success
* ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:interface status error * ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY:interface status error

View 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) esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname)
{ {
#if LWIP_NETIF_HOSTNAME
struct netif *p_netif; 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) { if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; 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; return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
} }
p_netif = esp_netif[tcpip_if]; p_netif = esp_netif[tcpip_if];
if (p_netif != NULL) { if (p_netif != NULL) {
if (netif_is_up(p_netif)) { memset(hostinfo[tcpip_if], 0, sizeof(hostinfo[tcpip_if]));
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY; memcpy(hostinfo[tcpip_if], hostname, strlen(hostname));
} else { p_netif->hostname = hostinfo[tcpip_if];
memset(hostinfo, 0, sizeof(hostinfo)); return ESP_OK;
memcpy(hostinfo, hostname, strlen(hostname));
p_netif->hostname = hostinfo;
return ESP_OK;
}
} else { } 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) 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; struct netif *p_netif = NULL;
if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) { if (tcpip_if >= TCPIP_ADAPTER_IF_MAX || hostname == NULL) {
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; 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 { } else {
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
} }
#else
return ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY;
#endif
} }
#endif #endif