mbed IP library over Ethernet K64F static IP FIXED

Dependencies:   Socket lwip-eth lwip-sys lwip

Fork of EthernetInterface by mbed official

Files at this revision

API Documentation at this revision

Comitter:
pinfred
Date:
Mon Oct 24 16:25:18 2016 +0000
Parent:
54:183490eb1b4a
Commit message:
fix static IP issue

Changed in this revision

EthernetInterface.cpp Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.h Show annotated file Show diff for this revision Revisions of this file
diff -r 183490eb1b4a -r ab0511be5528 EthernetInterface.cpp
--- 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);
diff -r 183490eb1b4a -r ab0511be5528 EthernetInterface.h
--- a/EthernetInterface.h	Wed May 11 22:18:04 2016 +0000
+++ b/EthernetInterface.h	Mon Oct 24 16:25:18 2016 +0000
@@ -45,7 +45,7 @@
   * \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);
 
   /** Connect
   * Bring the interface up, start DHCP if needed.