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

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

@ -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().
@ -81,7 +79,6 @@ ethernet_low_level_init(struct netif *netif)
#if ESP_LWIP
#if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
#endif
#endif
@ -119,12 +116,10 @@ ethernet_low_level_output(struct netif *netif, struct pbuf *p)
q = p;
u16_t pbuf_x_len = 0;
pbuf_x_len = q->len;
if(q->next !=NULL)
{
if(q->next !=NULL) {
//char cnt = 0;
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;
//cnt++;
@ -211,12 +206,9 @@ 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 */

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

@ -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().
@ -68,10 +66,6 @@ static char hostname[16];
static void
low_level_init(struct netif *netif)
{
/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
@ -85,13 +79,9 @@ low_level_init(struct netif *netif)
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
#if ESP_LWIP
#if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
#endif
#endif
/* Do whatever else is needed to initialize interface. */
@ -208,23 +198,9 @@ 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 */

View File

@ -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

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)
{
#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;
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