Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef enum _phy_status
/*! @brief Defines the ENET timeout.*/
typedef enum _phy_timeout
{
kPhyTimeout = 0x10000, /*!< ENET resets timeout.*/
kPhyTimeout = 0x800, /*!< ENET resets timeout: 0x800 represents around 2 seconds */
} phy_timeout_t;

/*! @brief Defines the PHY register.*/
Expand Down
45 changes: 31 additions & 14 deletions libraries/net/eth/EthernetInterface/EthernetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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);
Expand All @@ -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) {
Copy link
Contributor

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

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) {
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Expand All @@ -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) {
Copy link
Contributor

@infinnovation-dev infinnovation-dev Jul 7, 2016

Choose a reason for hiding this comment

The 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_is_link_up(&netif)) {
        inited = 1;
    } else {
        inited = netif_linked.wait(timeout_ms);
    }

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);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/net/eth/EthernetInterface/EthernetInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@geky geky Aug 26, 2016

Choose a reason for hiding this comment

The 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.
Expand Down