WIZNet W5500 with additional enhancements

Fork of WIZnetInterface by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
Helmut Tschemernjak
Date:
Mon Oct 09 19:58:19 2017 +0200
Parent:
33:879cfe51e66e
Child:
35:fe3028eda085
Commit message:
Added support for manual DNS server config or DHCP DNS config.
Now the DNS 8.8.8.8 is only a fallback if no DNS server is specified.
Replaced error() messages with printf() to avoid hanging code in error()

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
Socket/DNSClient.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/DNSClient.h Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
arch/ext/W5500.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.cpp	Sat Oct 07 22:10:01 2017 +0200
+++ b/EthernetInterface.cpp	Mon Oct 09 19:58:19 2017 +0200
@@ -41,7 +41,7 @@
     return 0;
 }
 
-int EthernetInterface::init(uint8_t * mac)
+int EthernetInterface::init(uint8_t *mac)
 {
     dhcp = true;
     //
@@ -52,7 +52,7 @@
     return 0;
 }
 
-int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
+int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway, const char* dnsServer)
 {
     dhcp = false;
     //
@@ -63,6 +63,8 @@
     ip_set = true;
     this->netmask = str_to_ip(mask);
     this->gateway = str_to_ip(gateway);
+    if (dnsServer)
+    	this->dnsaddr = str_to_ip(dnsServer);
     reset();
 
     // @Jul. 8. 2014 add code. should be called to write chip.
@@ -131,12 +133,12 @@
 char* EthernetInterface::getDNSServer()
 {
     uint32_t ip = getDNSAddr();
-    snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d",
+    snprintf(dns_string, sizeof(gw_string), "%d.%d.%d.%d",
              (uint8_t)((ip>>24)&0xff),
              (uint8_t)((ip>>16)&0xff),
              (uint8_t)((ip>>8)&0xff),
              (uint8_t)(ip&0xff));
-    return gw_string;
+    return dns_string;
 }
 
 char* EthernetInterface::getMACAddress()
--- a/EthernetInterface.h	Sat Oct 07 22:10:01 2017 +0200
+++ b/EthernetInterface.h	Mon Oct 09 19:58:19 2017 +0200
@@ -51,9 +51,10 @@
   * \param ip the IP address to use
   * \param mask the IP address mask
   * \param gateway the gateway to use
+  * \param dnsServer the DNS server to use
   * \return 0 on success, a negative number on failure
   */
-  int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
+  int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway, const char* dnsServer = NULL);
 
   /** Connect
   * Bring the interface up, start DHCP if needed.
@@ -83,6 +84,7 @@
     char ip_string[20];
     char mask_string[20];
     char gw_string[20];
+    char dns_string[20];
     char mac_string[20];
     bool ip_set;
 };
--- a/Socket/DNSClient.cpp	Sat Oct 07 22:10:01 2017 +0200
+++ b/Socket/DNSClient.cpp	Mon Oct 09 19:58:19 2017 +0200
@@ -113,7 +113,12 @@
     m_udp->init();
     m_udp->set_blocking(false);
     Endpoint server;
-    server.set_address("8.8.8.8", 53); // DNS
+    
+    if (m_dnsServer) {
+        server.set_address(m_dnsServer, 53); // DNS
+    } else {
+        server.set_address("8.8.8.8", 53); // DNS
+    }
     m_udp->bind(rand()&0x7fff);
     uint8_t buf[256];                
     int size = query(buf, sizeof(buf), hostname);
@@ -159,8 +164,9 @@
     }
 }
 
-bool DNSClient::lookup(const char* hostname) {
+bool DNSClient::lookup(const char* hostname, uint32_t dnsServer) {
     m_hostname = hostname;
+    m_dnsServer = dnsServer;
     m_state = MYNETDNS_START;
     while(1) {
         poll();
--- a/Socket/DNSClient.h	Sat Oct 07 22:10:01 2017 +0200
+++ b/Socket/DNSClient.h	Mon Oct 09 19:58:19 2017 +0200
@@ -8,7 +8,7 @@
     DNSClient(const char* hostname = NULL);
     DNSClient(Endpoint* pHost);
     virtual ~DNSClient();
-    bool lookup(const char* hostname = NULL);
+    bool lookup(const char* hostname = NULL, uint32_t dnsServer = 0);
     uint32_t ip;
 protected:
     void poll();
@@ -19,7 +19,6 @@
     uint8_t m_id[2];
     Timer m_interval;
     int m_retry;
-    const char* m_hostname;
 private:
     enum MyNetDnsState
     {
@@ -31,5 +30,7 @@
     };
     MyNetDnsState m_state;
     UDPSocket *m_udp;
+    const char* m_hostname;
+    uint32_t m_dnsServer;
 };
 
--- a/Socket/Endpoint.cpp	Sat Oct 07 22:10:01 2017 +0200
+++ b/Socket/Endpoint.cpp	Mon Oct 09 19:58:19 2017 +0200
@@ -36,12 +36,12 @@
     //Resolve DNS address or populate hard-coded IP address
     WIZnet_Chip* eth = WIZnet_Chip::getInstance();
     if (eth == NULL) {
-        error("Endpoint constructor error: no WIZnet chip instance available!\r\n");
+        printf("Endpoint constructor error: no WIZnet chip instance available!\r\n");
         return -1;
     }
     uint32_t addr;
     if (!eth->gethostbyname(host, &addr)) {
-        error("DNS error : Cannot get url from DNS server\r\n");
+        printf("DNS error : Cannot get url from DNS server\r\n");
         return -1;
     }
     snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (int)(addr>>24)&0xff, (int)(addr>>16)&0xff, (int)(addr>>8)&0xff, (int)addr&0xff);
@@ -49,6 +49,12 @@
     return 0;
 }
 
+int Endpoint::set_address(const uint32_t addr, const int port)
+{
+    snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (int)(addr>>24)&0xff, (int)(addr>>16)&0xff, (int)(addr>>8)&0xff, (int)addr&0xff);
+    _port = port;
+}
+
 char* Endpoint::get_address()
 {
     return _ipAddress;
--- a/Socket/Endpoint.h	Sat Oct 07 22:10:01 2017 +0200
+++ b/Socket/Endpoint.h	Mon Oct 09 19:58:19 2017 +0200
@@ -45,7 +45,8 @@
     \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
      */
     int  set_address(const char* host, const int port);
-    
+    int  set_address(const uint32_t addr, const int port);
+
     /** Get the IP address of this endpoint
     \return The IP address of this endpoint.
      */
--- a/Socket/Socket.cpp	Sat Oct 07 22:10:01 2017 +0200
+++ b/Socket/Socket.cpp	Mon Oct 09 19:58:19 2017 +0200
@@ -22,7 +22,7 @@
 {
     eth = WIZnet_Chip::getInstance();
     if (eth == NULL) {
-        error("Socket constructor error: no W7500 instance available!\r\n");
+        printf("Socket constructor error: no W7500 instance available!\r\n");
     }
 }
 
--- a/Socket/TCPSocketServer.cpp	Sat Oct 07 22:10:01 2017 +0200
+++ b/Socket/TCPSocketServer.cpp	Mon Oct 09 19:58:19 2017 +0200
@@ -83,13 +83,13 @@
     _sock_fd = -1; // want to assign new available _sock_fd.
     if(bind(listen_port) < 0) {
         // modified by Patrick Pollet
-        error("No more socket for listening, bind error");
+        printf("No more socket for listening, bind error");
         return -1;
     } else {
         //return -1;
         if(listen(1) < 0) {
             // modified by Patrick Pollet
-            error("No more socket for listening, listen error");
+            printf("No more socket for listening, listen error");
             return -1;
         }
     }
--- a/arch/ext/W5500.cpp	Sat Oct 07 22:10:01 2017 +0200
+++ b/arch/ext/W5500.cpp	Mon Oct 09 19:58:19 2017 +0200
@@ -54,6 +54,7 @@
     cs = 1;
     reset_pin = 1;
     inst = this;
+    dnsaddr = 0;
     sock_any_port = SOCK_ANY_PORT_NUM;
 }
 
@@ -125,7 +126,7 @@
         return true;
     }
     DNSClient client;
-    if(client.lookup(host)) {
+    if(client.lookup(host, dnsaddr)) {
         *ip = client.ip;
         return true;
     }