mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Revision:
8:4acb22344932
Parent:
5:59a504be7998
--- a/UIPUdp.cpp	Tue Apr 26 18:37:14 2016 +0000
+++ b/UIPUdp.cpp	Fri Jun 30 19:51:28 2017 +0000
@@ -1,5 +1,5 @@
 /*
- UIPUdp.cpp - Arduino implementation of a uIP wrapper class.
+ UIPUdp.cpp - Arduino implementation of a UIP wrapper class.
  Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
  All rights reserved.
 
@@ -20,9 +20,6 @@
 #include "UIPUdp.h"
 #include "Dns.h"
 
-#ifdef UIPETHERNET_DEBUG_UDP
-    #include "HardwareSerial.h"
-#endif
 extern "C"
 {
 #include "utility/uip-conf.h"
@@ -30,8 +27,8 @@
 #include "utility/uip_arp.h"
 }
 #if UIP_UDP
-    #define UIP_ARPHDRSIZE  42
-    #define UDPBUF          ((struct uip_udpip_hdr*) &uip_buf[UIP_LLH_LEN])
+#define UIP_ARPHDRSIZE  42
+#define UDPBUF          ((struct uip_udpip_hdr*) &uip_buf[UIP_LLH_LEN])
 
 // Constructor
 UIPUDP::UIPUDP(void) :
@@ -41,11 +38,11 @@
 
 // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
 uint8_t UIPUDP::begin(uint16_t port) {
-    if(!_uip_udp_conn) {
+    if (!_uip_udp_conn) {
         _uip_udp_conn = uip_udp_new(NULL, 0);
     }
 
-    if(_uip_udp_conn) {
+    if (_uip_udp_conn) {
         uip_udp_bind(_uip_udp_conn, htons(port));
         _uip_udp_conn->appstate = &appdata;
         return 1;
@@ -56,13 +53,13 @@
 
 // Finish with the UDP socket
 void UIPUDP::stop(void) {
-    if(_uip_udp_conn) {
+    if (_uip_udp_conn) {
         uip_udp_remove(_uip_udp_conn);
         _uip_udp_conn->appstate = NULL;
         _uip_udp_conn = NULL;
-        UIPEthernet.network.freeBlock(appdata.packet_in);
-        UIPEthernet.network.freeBlock(appdata.packet_next);
-        UIPEthernet.network.freeBlock(appdata.packet_out);
+        uIPEthernet.network.freeBlock(appdata.packet_in);
+        uIPEthernet.network.freeBlock(appdata.packet_next);
+        uIPEthernet.network.freeBlock(appdata.packet_out);
         memset(&appdata, 0, sizeof(appdata));
     }
 }
@@ -72,59 +69,56 @@
 
 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
 int UIPUDP::beginPacket(IPAddress ip, uint16_t port) {
-    UIPEthernet.tick();
-    if(ip && port) {
+    uIPEthernet.tick();
+    if (ip && port) {
         uip_ipaddr_t    ripaddr;
         uip_ip_addr(&ripaddr, ip);
-    #ifdef UIPETHERNET_DEBUG_UDP
-        Serial.print(F("udp beginPacket, "));
-    #endif
-        if(_uip_udp_conn) {
+#ifdef UIPETHERNET_DEBUG_UDP
+        printf("udp beginPacket, ");
+#endif
+        if (_uip_udp_conn) {
             _uip_udp_conn->rport = htons(port);
             uip_ipaddr_copy(_uip_udp_conn->ripaddr, &ripaddr);
         }
         else {
             _uip_udp_conn = uip_udp_new(&ripaddr, htons(port));
-            if(_uip_udp_conn)
+            if (_uip_udp_conn)
             {
-    #ifdef UIPETHERNET_DEBUG_UDP
-                Serial.print(F("new connection, "));
-    #endif
+#ifdef UIPETHERNET_DEBUG_UDP
+                printf("new connection, ");
+#endif
                 _uip_udp_conn->appstate = &appdata;
             }
             else
             {
-    #ifdef UIPETHERNET_DEBUG_UDP
-                Serial.println(F("failed to allocate new connection"));
-    #endif
+#ifdef UIPETHERNET_DEBUG_UDP
+                printf("failed to allocate new connection\r\n");
+#endif
                 return 0;
             }
         }
 
-    #ifdef UIPETHERNET_DEBUG_UDP
-        Serial.print(F("rip: "));
-        Serial.print(ip);
-        Serial.print(F(", port: "));
-        Serial.println(port);
-    #endif
+#ifdef UIPETHERNET_DEBUG_UDP
+        printf("rip: %s, port: %d\r\n", ip.asString(), port);
+#endif
     }
 
-    if(_uip_udp_conn) {
-        if(appdata.packet_out == NOBLOCK) {
-            appdata.packet_out = UIPEthernet.network.allocBlock(UIP_UDP_MAXPACKETSIZE);
+    if (_uip_udp_conn) {
+        if (appdata.packet_out == NOBLOCK) {
+            appdata.packet_out = uIPEthernet.network.allocBlock(UIP_UDP_MAXPACKETSIZE);
             appdata.out_pos = UIP_UDP_PHYH_LEN;
-            if(appdata.packet_out != NOBLOCK)
+            if (appdata.packet_out != NOBLOCK)
                 return 1;
-    #ifdef UIPETHERNET_DEBUG_UDP
+#ifdef UIPETHERNET_DEBUG_UDP
             else
-                Serial.println(F("failed to allocate memory for packet"));
-    #endif
+                printf("failed to allocate memory for packet\r\n");
+#endif
         }
 
-    #ifdef UIPETHERNET_DEBUG_UDP
+#ifdef UIPETHERNET_DEBUG_UDP
         else
-            Serial.println(F("previous packet on that connection not sent yet"));
-    #endif
+            printf("previous packet on that connection not sent yet\r\n");
+#endif
     }
 
     return 0;
@@ -140,9 +134,9 @@
     DNSClient   dns;
     IPAddress   remote_addr;
 
-    dns.begin(UIPEthernet.dnsServerIP());
+    dns.begin(uIPEthernet.dnsServerIP());
     ret = dns.getHostByName(host, remote_addr);
-    if(ret == 1) {
+    if (ret == 1) {
         return beginPacket(remote_addr, port);
     }
     else {
@@ -154,11 +148,11 @@
 
 // Returns 1 if the packet was sent successfully, 0 if there was an error
 int UIPUDP::endPacket(void) {
-    if(_uip_udp_conn && appdata.packet_out != NOBLOCK) {
+    if (_uip_udp_conn && appdata.packet_out != NOBLOCK) {
         appdata.send = true;
-        UIPEthernet.network.resizeBlock(appdata.packet_out, 0, appdata.out_pos);
+        uIPEthernet.network.resizeBlock(appdata.packet_out, 0, appdata.out_pos);
         uip_udp_periodic_conn(_uip_udp_conn);
-        if(uip_len > 0) {
+        if (uip_len > 0) {
             _send(&appdata);
             return 1;
         }
@@ -174,8 +168,8 @@
 
 // Write size bytes from buffer into the packet
 size_t UIPUDP::write(const uint8_t* buffer, size_t size) {
-    if(appdata.packet_out != NOBLOCK) {
-        size_t  ret = UIPEthernet.network.writePacket(appdata.packet_out, appdata.out_pos, (uint8_t*)buffer, size);
+    if (appdata.packet_out != NOBLOCK) {
+        size_t  ret = uIPEthernet.network.writePacket(appdata.packet_out, appdata.out_pos, (uint8_t*)buffer, size);
         appdata.out_pos += ret;
         return ret;
     }
@@ -187,45 +181,42 @@
 
 // Returns the size of the packet in bytes, or 0 if no packets are available
 int UIPUDP::parsePacket(void) {
-    UIPEthernet.tick();
-    #ifdef UIPETHERNET_DEBUG_UDP
-    if(appdata.packet_in != NOBLOCK) {
-        Serial.print(F("udp parsePacket freeing previous packet: "));
-        Serial.println(appdata.packet_in);
+    uIPEthernet.tick();
+#ifdef UIPETHERNET_DEBUG_UDP
+    if (appdata.packet_in != NOBLOCK) {
+        printf("udp parsePacket freeing previous packet: %d\r\n", appdata.packet_in);
     }
-    #endif
-    UIPEthernet.network.freeBlock(appdata.packet_in);
+#endif
+    uIPEthernet.network.freeBlock(appdata.packet_in);
 
     appdata.packet_in = appdata.packet_next;
     appdata.packet_next = NOBLOCK;
 
-    #ifdef UIPETHERNET_DEBUG_UDP
-    if(appdata.packet_in != NOBLOCK) {
-        Serial.print(F("udp parsePacket received packet: "));
-        Serial.print(appdata.packet_in);
+#ifdef UIPETHERNET_DEBUG_UDP
+    if (appdata.packet_in != NOBLOCK) {
+        printf("udp parsePacket received packet: %d", appdata.packet_in);
     }
-    #endif
+#endif
 
-    int size = UIPEthernet.network.blockSize(appdata.packet_in);
-    #ifdef UIPETHERNET_DEBUG_UDP
-    if(appdata.packet_in != NOBLOCK) {
-        Serial.print(F(", size: "));
-        Serial.println(size);
+    int size = uIPEthernet.network.blockSize(appdata.packet_in);
+#ifdef UIPETHERNET_DEBUG_UDP
+    if (appdata.packet_in != NOBLOCK) {
+        printf(", size: %d\r\n", size);
     }
-    #endif
+#endif
     return size;
 }
 
 // Number of bytes remaining in the current packet
 int UIPUDP::available(void) {
-    UIPEthernet.tick();
-    return UIPEthernet.network.blockSize(appdata.packet_in);
+    uIPEthernet.tick();
+    return uIPEthernet.network.blockSize(appdata.packet_in);
 }
 
 // Read a single byte from the current packet
 int UIPUDP::read(void) {
-    static unsigned char   c;
-    if(read(&c, 1) > 0) {
+    static unsigned char    c;
+    if (read(&c, 1) > 0) {
         return c;
     }
 
@@ -236,15 +227,15 @@
 
 // Returns the number of bytes read, or 0 if none are available
 int UIPUDP::read(unsigned char* buffer, size_t len) {
-    UIPEthernet.tick();
-    if(appdata.packet_in != NOBLOCK) {
-        memaddress  read = UIPEthernet.network.readPacket(appdata.packet_in, 0, buffer, len);
-        if(read == UIPEthernet.network.blockSize(appdata.packet_in)) {
-            UIPEthernet.network.freeBlock(appdata.packet_in);
+    uIPEthernet.tick();
+    if (appdata.packet_in != NOBLOCK) {
+        memaddress  read = uIPEthernet.network.readPacket(appdata.packet_in, 0, buffer, len);
+        if (read == uIPEthernet.network.blockSize(appdata.packet_in)) {
+            uIPEthernet.network.freeBlock(appdata.packet_in);
             appdata.packet_in = NOBLOCK;
         }
         else
-            UIPEthernet.network.resizeBlock(appdata.packet_in, read);
+            uIPEthernet.network.resizeBlock(appdata.packet_in, read);
         return read;
     }
 
@@ -253,10 +244,10 @@
 
 // Return the next byte from the current packet without moving on to the next byte
 int UIPUDP::peek(void) {
-    UIPEthernet.tick();
-    if(appdata.packet_in != NOBLOCK) {
+    uIPEthernet.tick();
+    if (appdata.packet_in != NOBLOCK) {
         unsigned char   c;
-        if(UIPEthernet.network.readPacket(appdata.packet_in, 0, &c, 1) == 1)
+        if (uIPEthernet.network.readPacket(appdata.packet_in, 0, &c, 1) == 1)
             return c;
     }
 
@@ -265,8 +256,8 @@
 
 // Finish reading the current packet
 void UIPUDP::flush(void) {
-    UIPEthernet.tick();
-    UIPEthernet.network.freeBlock(appdata.packet_in);
+    uIPEthernet.tick();
+    uIPEthernet.network.freeBlock(appdata.packet_in);
     appdata.packet_in = NOBLOCK;
 }
 
@@ -280,48 +271,52 @@
     return _uip_udp_conn ? ntohs(_uip_udp_conn->rport) : 0;
 }
 
-// uIP callback function
+// UIP callback function
 void uipudp_appcall(void) {
-    if(uip_udp_userdata_t * data = (uip_udp_userdata_t *) (uip_udp_conn->appstate)) {
-        if(uip_newdata()) {
-            if(data->packet_next == NOBLOCK) {
+    if (uip_udp_userdata_t * data = (uip_udp_userdata_t *) (uip_udp_conn->appstate)) {
+        if (uip_newdata()) {
+            if (data->packet_next == NOBLOCK) {
                 uip_udp_conn->rport = UDPBUF->srcport;
                 uip_ipaddr_copy(uip_udp_conn->ripaddr, UDPBUF->srcipaddr);
-                data->packet_next = UIPEthernet.network.allocBlock(ntohs(UDPBUF->udplen) - UIP_UDPH_LEN);
+                data->packet_next = uIPEthernet.network.allocBlock(ntohs(UDPBUF->udplen) - UIP_UDPH_LEN);
 
                 //if we are unable to allocate memory the packet is dropped. udp doesn't guarantee packet delivery
-                if(data->packet_next != NOBLOCK) {
+                if (data->packet_next != NOBLOCK) {
 
                     //discard Linklevel and IP and udp-header and any trailing bytes:
-                    UIPEthernet.network.copyPacket
+                    uIPEthernet.network.copyPacket
                         (
                             data->packet_next,
                             0,
-                            UIPEthernetClass::in_packet,
+                            UIPEthernet::in_packet,
                             UIP_UDP_PHYH_LEN,
-                            UIPEthernet.network.blockSize(data->packet_next)
+                            uIPEthernet.network.blockSize(data->packet_next)
                         );
-    #ifdef UIPETHERNET_DEBUG_UDP
-                    Serial.print(F("udp, uip_newdata received packet: "));
-                    Serial.print(data->packet_next);
-                    Serial.print(F(", size: "));
-                    Serial.println(UIPEthernet.network.blockSize(data->packet_next));
-    #endif
+#ifdef UIPETHERNET_DEBUG_UDP
+                    printf
+                    (
+                        "udp, uip_newdata received packet: %d, size: %d\r\n",
+                        data->packet_next,
+                        UIPEthernet.network.blockSize(data->packet_next)
+                    );
+#endif
                 }
             }
         }
 
-        if(uip_poll() && data->send)
+        if (uip_poll() && data->send)
         {
             //set uip_slen (uip private) by calling uip_udp_send
-    #ifdef UIPETHERNET_DEBUG_UDP
-            Serial.print(F("udp, uip_poll preparing packet to send: "));
-            Serial.print(data->packet_out);
-            Serial.print(F(", size: "));
-            Serial.println(UIPEthernet.network.blockSize(data->packet_out));
-    #endif
-            UIPEthernetClass::uip_packet = data->packet_out;
-            UIPEthernetClass::uip_hdrlen = UIP_UDP_PHYH_LEN;
+#ifdef UIPETHERNET_DEBUG_UDP
+            printf
+            (
+                "udp, uip_poll preparing packet to send: %d, size: %d\r\n",
+                data->packet_out,
+                UIPEthernet.network.blockSize(data->packet_out)
+            );
+#endif
+            UIPEthernet::uip_packet = data->packet_out;
+            UIPEthernet::uip_hdrlen = UIP_UDP_PHYH_LEN;
             uip_udp_send(data->out_pos - (UIP_UDP_PHYH_LEN));
         }
     }
@@ -335,25 +330,25 @@
  */
 void UIPUDP::_send(uip_udp_userdata_t* data) {
     uip_arp_out();  //add arp
-    if(uip_len == UIP_ARPHDRSIZE) {
-        UIPEthernetClass::uip_packet = NOBLOCK;
-        UIPEthernetClass::packetstate &= ~UIPETHERNET_SENDPACKET;
-    #ifdef UIPETHERNET_DEBUG_UDP
-        Serial.println(F("udp, uip_poll results in ARP-packet"));
-    #endif
+    if (uip_len == UIP_ARPHDRSIZE) {
+        UIPEthernet::uip_packet = NOBLOCK;
+        UIPEthernet::packetstate &= ~UIPETHERNET_SENDPACKET;
+#ifdef UIPETHERNET_DEBUG_UDP
+        printf("udp, uip_poll results in ARP-packet\r\n");
+#endif
     }
     else {
 
-    //arp found ethaddr for ip (otherwise packet is replaced by arp-request)
+        //arp found ethaddr for ip (otherwise packet is replaced by arp-request)
         data->send = false;
         data->packet_out = NOBLOCK;
-        UIPEthernetClass::packetstate |= UIPETHERNET_SENDPACKET;
-    #ifdef UIPETHERNET_DEBUG_UDP
-        Serial.print(F("udp, uip_packet to send: "));
-        Serial.println(UIPEthernetClass::uip_packet);
-    #endif
+        UIPEthernet::packetstate |= UIPETHERNET_SENDPACKET;
+#ifdef UIPETHERNET_DEBUG_UDP
+        printf("udp, uip_packet to send: %d\r\n", UIPEthernet::uip_packet);
+#endif
     }
 
-    UIPEthernet.network_send();
+    uIPEthernet.network_send();
 }
 #endif
+