-
Notifications
You must be signed in to change notification settings - Fork 3k
Fixing Static EthernetInterface IP #1645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
#include "lwip/netif.h" | ||
#include "netif/etharp.h" | ||
#include "lwip/dhcp.h" | ||
#include "lwip/dns.h" | ||
#include "eth_arch.h" | ||
#include "lwip/tcpip.h" | ||
|
||
|
@@ -33,8 +34,10 @@ static struct netif netif; | |
static char mac_addr[19]; | ||
static char ip_addr[17] = "\0"; | ||
static char gateway[17] = "\0"; | ||
static char dns_addr[17] = "\0"; | ||
static char networkmask[17] = "\0"; | ||
static bool use_dhcp = false; | ||
static bool set_dns = false; | ||
|
||
static Semaphore tcpip_inited(0); | ||
static Semaphore netif_linked(0); | ||
|
@@ -59,16 +62,19 @@ static void netif_status_callback(struct netif *netif) { | |
} | ||
} | ||
|
||
static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { | ||
static int init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { | ||
tcpip_init(tcpip_init_done, NULL); | ||
tcpip_inited.wait(); | ||
|
||
memset((void*) &netif, 0, sizeof(netif)); | ||
netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input); | ||
struct netif * ret; | ||
ret = netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input); | ||
if( ret == NULL) { | ||
return -1; | ||
} | ||
netif_set_default(&netif); | ||
|
||
netif_set_link_callback (&netif, netif_link_callback); | ||
netif_set_status_callback(&netif, netif_status_callback); | ||
return 0; | ||
} | ||
|
||
static void set_mac_address(void) { | ||
|
@@ -85,23 +91,23 @@ static void set_mac_address(void) { | |
int EthernetInterface::init() { | ||
use_dhcp = true; | ||
set_mac_address(); | ||
init_netif(NULL, NULL, NULL); | ||
return 0; | ||
return init_netif(NULL, NULL, NULL); | ||
} | ||
|
||
int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) { | ||
int EthernetInterface::init(const char* ip, const char* mask, const char* gateway, const char * dns) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This API change appears unrelated to the issue being discussed. IMHO it would be better as a separate PR. |
||
use_dhcp = false; | ||
|
||
set_mac_address(); | ||
strcpy(ip_addr, ip); | ||
|
||
if (dns != NULL) { | ||
set_dns = true; | ||
strcpy(dns_addr, dns); | ||
} | ||
ip_addr_t ip_n, mask_n, gateway_n; | ||
inet_aton(ip, &ip_n); | ||
inet_aton(mask, &mask_n); | ||
inet_aton(gateway, &gateway_n); | ||
init_netif(&ip_n, &mask_n, &gateway_n); | ||
|
||
return 0; | ||
return init_netif(&ip_n, &mask_n, &gateway_n); | ||
} | ||
|
||
int EthernetInterface::connect(unsigned int timeout_ms) { | ||
|
@@ -116,11 +122,22 @@ int EthernetInterface::connect(unsigned int timeout_ms) { | |
inited = netif_up.wait(timeout_ms); | ||
} else { | ||
netif_set_up(&netif); | ||
|
||
// Wait for the link up | ||
inited = netif_linked.wait(timeout_ms); | ||
Timer timeout; | ||
timeout.start(); | ||
while(timeout.read_ms() < timeout_ms) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a busy-wait the best way to do this? I would suggest something like
|
||
if (netif.flags & NETIF_FLAG_LINK_UP) { | ||
inited = 1; | ||
break; | ||
} | ||
} | ||
if (set_dns) { | ||
ip_addr_t dns_n; | ||
inet_aton(dns_addr, &dns_n); | ||
dns_setserver(0, &dns_n); | ||
} | ||
} | ||
|
||
return (inited > 0) ? (0) : (-1); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ class EthernetInterface { | |
* \param gateway the gateway to use | ||
* \return 0 on success, a negative number on failure | ||
*/ | ||
static int init(const char* ip, const char* mask, const char* gateway); | ||
static int init(const char* ip, const char* mask, const char* gateway, const char * dns = NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This API change appears orthogonal to the issue being discussed - maybe it should be a separate PR?. However, it seems like a good idea to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, normally lwip gets its dns servers from the dhcp request, so if this class allows static addresses, the dns server does need to be provided manually. This style does match the existing style, though may prove to not be scalable. That being said, this is unrelated to the subject in the pr, maybe it should be separate before we change the EthernetInterface api? |
||
|
||
/** Connect | ||
* Bring the interface up, start DHCP if needed. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please fix a space ->
if(ret == NULL)
, use rebase or I'll do it when merging