my fork for nimqtt

Dependencies:   Socket lwip-eth lwip-sys lwip

Fork of EthernetInterface by Guilhem Saurel

Revision:
26:dd9794ce1d64
Parent:
22:2a797ba9babe
Child:
27:2124eae946f3
--- a/EthernetInterface.cpp	Mon Aug 13 09:38:08 2012 +0000
+++ b/EthernetInterface.cpp	Fri Feb 15 17:01:20 2013 +0000
@@ -31,21 +31,30 @@
 /* TCP/IP and Network Interface Initialisation */
 static struct netif lpcNetif;
 
-static Semaphore tcpip_inited(0);
-static Semaphore netif_inited(0);
+static char ip_addr[16] = "\0";
+static bool use_dhcp = false;
 
-static char ip_addr[16];
-static bool connected;
-static bool use_dhcp = false;
+static Semaphore tcpip_inited(0);
+static Semaphore netif_linked(0);
+static Semaphore netif_up(0);
 
 static void tcpip_init_done(void *arg) {
     tcpip_inited.release();
 }
+
+static void netif_link_callback(struct netif *netif) {
+    if (netif_is_link_up(netif)) {
+        netif_linked.release();
+    }
+}
+
 static void netif_status_callback(struct netif *netif) {
-    strcpy(ip_addr, inet_ntoa(netif->ip_addr));
-    connected = netif_is_up(netif) ? true : false;
-    netif_inited.release();
+    if (netif_is_up(netif)) {
+        strcpy(ip_addr, inet_ntoa(netif->ip_addr));
+        netif_up.release();
+    }
 }
+
 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
     tcpip_init(tcpip_init_done, NULL);
     tcpip_inited.wait();
@@ -53,6 +62,8 @@
     memset((void*) &lpcNetif, 0, sizeof(lpcNetif));
     netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, tcpip_input);
     netif_set_default(&lpcNetif);
+    
+    netif_set_link_callback  (&lpcNetif, netif_link_callback);
     netif_set_status_callback(&lpcNetif, netif_status_callback);
 }
 
@@ -64,11 +75,14 @@
 
 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
     use_dhcp = false;
+    strcpy(ip_addr, ip);
+    
     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;
 }
 
@@ -76,14 +90,20 @@
     NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
     NVIC_EnableIRQ(ENET_IRQn);
     
+    int inited;
     if (use_dhcp) {
         dhcp_start(&lpcNetif);
+        
+        // Wait for an IP
+        // -1: error, 0: timeout
+        inited = netif_up.wait(timeout_ms);
     } else {
         netif_set_up(&lpcNetif);
+        
+        // Wait for the link up
+        inited = netif_linked.wait(timeout_ms);
     }
     
-    // -1: error, 0: timeout
-    int inited = netif_inited.wait(timeout_ms);
     return (inited > 0) ? (0) : (-1);
 }
 
@@ -101,5 +121,5 @@
 }
 
 char* EthernetInterface::getIPAddress() {
-    return (connected) ? (ip_addr) : (NULL);
+    return ip_addr;
 }