mbed IP library over Ethernet K64F static IP FIXED

Dependencies:   Socket lwip-eth lwip-sys lwip

Fork of EthernetInterface by mbed official

Revision:
55:ab0511be5528
Parent:
37:926eb6517318
--- a/EthernetInterface.cpp	Wed May 11 22:18:04 2016 +0000
+++ b/EthernetInterface.cpp	Mon Oct 24 16:25:18 2016 +0000
@@ -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 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);
@@ -66,7 +69,6 @@
     memset((void*) &netif, 0, sizeof(netif));
     netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input);
     netif_set_default(&netif);
-    
     netif_set_link_callback  (&netif, netif_link_callback);
     netif_set_status_callback(&netif, netif_status_callback);
 }
@@ -89,12 +91,17 @@
     return 0;
 }
 
-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) {
     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);
@@ -118,7 +125,19 @@
         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) {
+            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);