mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Revision:
8:4acb22344932
Parent:
6:10e42359e217
--- a/UIPClient.cpp	Tue Apr 26 18:37:14 2016 +0000
+++ b/UIPClient.cpp	Fri Jun 30 19:51:28 2017 +0000
@@ -1,5 +1,5 @@
 /*
- UIPClient.cpp - Arduino implementation of a uIP wrapper class.
+ UIPClient.cpp - Arduino implementation of a UIP wrapper class.
  Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
  All rights reserved.
 
@@ -30,12 +30,9 @@
 #include "UIPClient.h"
 #include "Dns.h"
 
-#ifdef UIPETHERNET_DEBUG_CLIENT
-    #include "HardwareSerial.h"
-#endif
 #define UIP_TCP_PHYH_LEN    UIP_LLH_LEN + UIP_IPTCPH_LEN
 
-uip_userdata_t UIPClient::  all_data[UIP_CONNS];
+uip_userdata_t UIPClient::all_data[UIP_CONNS];
 
 /**
  * @brief
@@ -70,26 +67,23 @@
     uip_ip_addr(ipaddr, ip);
 
     struct uip_conn*    conn = uip_connect(&ipaddr, htons(port));
-    if(conn)
+    if (conn)
     {
 #if UIP_CONNECT_TIMEOUT > 0
         int32_t timeout = millis() + 1000 * UIP_CONNECT_TIMEOUT;
 #endif
-        while((conn->tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
-            UIPEthernet.tick();
-            if((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
+        while ((conn->tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) {
+            uIPEthernet.tick();
+            if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
                 data = (uip_userdata_t*)conn->appstate;
 #ifdef UIPETHERNET_DEBUG_CLIENT
-                Serial.print(F("connected, state: "));
-                Serial.print(data->state);
-                Serial.print(F(", first packet in: "));
-                Serial.println(data->packets_in[0]);
+                printf("connected, state: %d, first packet in: %d\r\n", data->state, data->packets_in[0]);
 #endif
                 return 1;
             }
 
 #if UIP_CONNECT_TIMEOUT > 0
-            if(((int32_t) (millis() - timeout)) > 0) {
+            if (((int32_t) (millis() - timeout)) > 0) {
                 conn->tcpstateflags = UIP_CLOSED;
                 break;
             }
@@ -114,9 +108,9 @@
     DNSClient   dns;
     IPAddress   remote_addr;
 
-    dns.begin(UIPEthernetClass::_dnsServerAddress);
+    dns.begin(UIPEthernet::_dnsServerAddress);
     ret = dns.getHostByName(host, remote_addr);
-    if(ret == 1) {
+    if (ret == 1) {
         return connect(remote_addr, port);
     }
 #endif
@@ -130,14 +124,14 @@
  * @retval
  */
 void UIPClient::stop(void) {
-    if(data && data->state)
+    if (data && data->state)
     {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-        Serial.println(F("before stop(), with data"));
+        printf("before stop(), with data\r\n");
         _dumpAllData();
 #endif
         _flushBlocks(&data->packets_in[0]);
-        if(data->state & UIP_CLIENT_REMOTECLOSED) {
+        if (data->state & UIP_CLIENT_REMOTECLOSED) {
             data->state = 0;
         }
         else {
@@ -145,18 +139,18 @@
         }
 
 #ifdef UIPETHERNET_DEBUG_CLIENT
-        Serial.println(F("after stop()"));
+        printf("after stop()\r\n");
         _dumpAllData();
 #endif
     }
 
 #ifdef UIPETHERNET_DEBUG_CLIENT
     else {
-        Serial.println(F("stop(), data: NULL"));
+        printf("stop(), data: NULL\r\n");
     }
 #endif
     data = NULL;
-    UIPEthernet.tick();
+    uIPEthernet.tick();
 }
 
 /**
@@ -186,7 +180,7 @@
  * @retval
  */
 UIPClient::operator bool(void) {
-    UIPEthernet.tick();
+    uIPEthernet.tick();
     return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in[0] != NOBLOCK);
 }
 
@@ -222,17 +216,17 @@
 #if UIP_ATTEMPTS_ON_WRITE > 0
     uint16_t    attempts = UIP_ATTEMPTS_ON_WRITE;
 #endif
-    repeat : UIPEthernet.tick();
-    if(u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) {
+    repeat : uIPEthernet.tick();
+    if (u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) {
         uint8_t p = _currentBlock(&u->packets_out[0]);
-        if(u->packets_out[p] == NOBLOCK)
+        if (u->packets_out[p] == NOBLOCK)
         {
 newpacket:
-            u->packets_out[p] = UIPEthernet.network.allocBlock(UIP_SOCKET_DATALEN);
-            if(u->packets_out[p] == NOBLOCK)
+            u->packets_out[p] = uIPEthernet.network.allocBlock(UIP_SOCKET_DATALEN);
+            if (u->packets_out[p] == NOBLOCK)
             {
 #if UIP_ATTEMPTS_ON_WRITE > 0
-                if((--attempts) > 0)
+                if ((--attempts) > 0)
 #endif
 #if UIP_ATTEMPTS_ON_WRITE != 0
                     goto repeat;
@@ -244,19 +238,9 @@
         }
 
 #ifdef UIPETHERNET_DEBUG_CLIENT
-        Serial.print(F("UIPClient.write: writePacket("));
-        Serial.print(u->packets_out[p]);
-        Serial.print(F(") pos: "));
-        Serial.print(u->out_pos);
-        Serial.print(F(", buf["));
-        Serial.print(size - remain);
-        Serial.print(F("-"));
-        Serial.print(remain);
-        Serial.print(F("]: '"));
-        Serial.write((uint8_t*)buf + size - remain, remain);
-        Serial.println(F("'"));
+        printf("UIPClient.write: writePacket(%d) pos: %d, buf[%d-%d]\r\n", u->packets_out[p], u->out_pos, size - remain, remain);
 #endif
-        written = UIPEthernet.network.writePacket
+        written = uIPEthernet.network.writePacket
             (
                 u->packets_out[p],
                 u->out_pos,
@@ -265,11 +249,11 @@
             );
         remain -= written;
         u->out_pos += written;
-        if(remain > 0) {
-            if(p == UIP_SOCKET_NUMPACKETS - 1)
+        if (remain > 0) {
+            if (p == UIP_SOCKET_NUMPACKETS - 1)
             {
 #if UIP_ATTEMPTS_ON_WRITE > 0
-                if((--attempts) > 0)
+                if ((--attempts) > 0)
 #endif
 #if UIP_ATTEMPTS_ON_WRITE != 0
                     goto repeat;
@@ -298,7 +282,7 @@
  * @retval
  */
 int UIPClient::available(void) {
-    if(*this)
+    if (*this)
         return _available(data);
     return 0;
 }
@@ -311,8 +295,8 @@
  */
 int UIPClient::_available(uip_userdata_t* u) {
     int len = 0;
-    for(uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
-        len += UIPEthernet.network.blockSize(u->packets_in[i]);
+    for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
+        len += uIPEthernet.network.blockSize(u->packets_in[i]);
     }
 
     return len;
@@ -325,16 +309,15 @@
  * @retval
  */
 int UIPClient::read(uint8_t* buf, size_t size) {
-    if(*this) {
+    if (*this) {
         uint16_t    remain = size;
-        if(data->packets_in[0] == NOBLOCK)
+        if (data->packets_in[0] == NOBLOCK)
             return 0;
 
         uint16_t    read;
-        do
-        {
-            read = UIPEthernet.network.readPacket(data->packets_in[0], 0, buf + size - remain, remain);
-            if(read == UIPEthernet.network.blockSize(data->packets_in[0])) {
+        do {
+            read = uIPEthernet.network.readPacket(data->packets_in[0], 0, buf + size - remain, remain);
+            if (read == uIPEthernet.network.blockSize(data->packets_in[0])) {
                 remain -= read;
                 _eatBlock(&data->packets_in[0]);
                 if
@@ -342,8 +325,8 @@
                     uip_stopped(&uip_conns[data->state & UIP_CLIENT_SOCKETS])
                 &&  !(data->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))
                 ) data->state |= UIP_CLIENT_RESTART;
-                if(data->packets_in[0] == NOBLOCK) {
-                    if(data->state & UIP_CLIENT_REMOTECLOSED) {
+                if (data->packets_in[0] == NOBLOCK) {
+                    if (data->state & UIP_CLIENT_REMOTECLOSED) {
                         data->state = 0;
                         data = NULL;
                     }
@@ -352,10 +335,10 @@
                 }
             }
             else {
-                UIPEthernet.network.resizeBlock(data->packets_in[0], read);
+                uIPEthernet.network.resizeBlock(data->packets_in[0], read);
                 break;
             }
-        } while(remain > 0);
+        } while (remain > 0);
         return size;
     }
 
@@ -369,8 +352,8 @@
  * @retval
  */
 int UIPClient::read(void) {
-    static uint8_t c;
-    if(read(&c, 1) < 0)
+    static uint8_t  c;
+    if (read(&c, 1) < 0)
         return -1;
     return c;
 }
@@ -382,10 +365,10 @@
  * @retval
  */
 int UIPClient::peek(void) {
-    static uint8_t c;
-    if(*this) {
-        if(data->packets_in[0] != NOBLOCK) {
-            UIPEthernet.network.readPacket(data->packets_in[0], 0, &c, 1);
+    static uint8_t  c;
+    if (*this) {
+        if (data->packets_in[0] != NOBLOCK) {
+            uIPEthernet.network.readPacket(data->packets_in[0], 0, &c, 1);
             return c;
         }
     }
@@ -400,7 +383,7 @@
  * @retval
  */
 void UIPClient::flush(void) {
-    if(*this) {
+    if (*this) {
         _flushBlocks(&data->packets_in[0]);
     }
 }
@@ -414,76 +397,74 @@
 void uipclient_appcall(void) {
     uint16_t            send_len = 0;
     uip_userdata_t*     u = (uip_userdata_t*)uip_conn->appstate;
-    if(!u && uip_connected())
+    if (!u && uip_connected())
     {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-        Serial.println(F("UIPClient uip_connected"));
+        printf("UIPClient uip_connected\r\n");
         UIPClient::_dumpAllData();
 #endif
         u = (uip_userdata_t*)UIPClient::_allocateData();
-        if(u) {
+        if (u) {
             uip_conn->appstate = u;
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            Serial.print(F("UIPClient allocated state: "));
-            Serial.println(u->state, BIN);
+            printf("UIPClient allocated state: %d", u->state);
 #endif
         }
 
 #ifdef UIPETHERNET_DEBUG_CLIENT
         else
-            Serial.println(F("UIPClient allocation failed"));
+            printf("UIPClient allocation failed\r\n");
 #endif
     }
 
-    if(u) {
-        if(uip_newdata())
+    if (u) {
+        if (uip_newdata())
         {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            Serial.print(F("UIPClient uip_newdata, uip_len:"));
-            Serial.println(uip_len);
+            printf("UIPClient uip_newdata, uip_len: %d\r\n", uip_len);
 #endif
-            if(uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) {
-                for(uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
-                    if(u->packets_in[i] == NOBLOCK) {
-                        u->packets_in[i] = UIPEthernet.network.allocBlock(uip_len);
-                        if(u->packets_in[i] != NOBLOCK) {
-                            UIPEthernet.network.copyPacket
+            if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) {
+                for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
+                    if (u->packets_in[i] == NOBLOCK) {
+                        u->packets_in[i] = uIPEthernet.network.allocBlock(uip_len);
+                        if (u->packets_in[i] != NOBLOCK) {
+                            uIPEthernet.network.copyPacket
                                 (
                                     u->packets_in[i],
                                     0,
-                                    UIPEthernetClass::in_packet,
+                                    UIPEthernet::in_packet,
                                     ((uint8_t*)uip_appdata) - uip_buf,
                                     uip_len
                                 );
-                            if(i == UIP_SOCKET_NUMPACKETS - 1)
+                            if (i == UIP_SOCKET_NUMPACKETS - 1)
                                 uip_stop();
                             goto finish_newdata;
                         }
                     }
                 }
 
-                UIPEthernetClass::packetstate &= ~UIPETHERNET_FREEPACKET;
+                UIPEthernet::packetstate &= ~UIPETHERNET_FREEPACKET;
                 uip_stop();
             }
         }
 
 finish_newdata:
-        if(u->state & UIP_CLIENT_RESTART) {
+        if (u->state & UIP_CLIENT_RESTART) {
             u->state &= ~UIP_CLIENT_RESTART;
             uip_restart();
         }
 
         // If the connection has been closed, save received but unread data.
-        if(uip_closed() || uip_timedout())
+        if (uip_closed() || uip_timedout())
         {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            Serial.println(F("UIPClient uip_closed"));
+            printf("UIPClient uip_closed\r\n");
             UIPClient::_dumpAllData();
 #endif
             // drop outgoing packets not sent yet:
 
             UIPClient::_flushBlocks(&u->packets_out[0]);
-            if(u->packets_in[0] != NOBLOCK) {
+            if (u->packets_in[0] != NOBLOCK) {
                 ((uip_userdata_closed_t*)u)->lport = uip_conn->lport;
                 u->state |= UIP_CLIENT_REMOTECLOSED;
             }
@@ -492,48 +473,48 @@
 
             // disassociate appdata.
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            Serial.println(F("after UIPClient uip_closed"));
+            printf("after UIPClient uip_closed\r\n");
             UIPClient::_dumpAllData();
 #endif
             uip_conn->appstate = NULL;
             goto finish;
         }
 
-        if(uip_acked())
+        if (uip_acked())
         {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            Serial.println(F("UIPClient uip_acked"));
+            printf("UIPClient uip_acked\r\n");
 #endif
             UIPClient::_eatBlock(&u->packets_out[0]);
         }
 
-        if(uip_poll() || uip_rexmit())
+        if (uip_poll() || uip_rexmit())
         {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            //Serial.println(F("UIPClient uip_poll"));
+            //printf("UIPClient uip_poll\r\n");
 #endif
-            if(u->packets_out[0] != NOBLOCK) {
-                if(u->packets_out[1] == NOBLOCK) {
+            if (u->packets_out[0] != NOBLOCK) {
+                if (u->packets_out[1] == NOBLOCK) {
                     send_len = u->out_pos;
-                    if(send_len > 0) {
-                        UIPEthernet.network.resizeBlock(u->packets_out[0], 0, send_len);
+                    if (send_len > 0) {
+                        uIPEthernet.network.resizeBlock(u->packets_out[0], 0, send_len);
                     }
                 }
                 else
-                    send_len = UIPEthernet.network.blockSize(u->packets_out[0]);
-                if(send_len > 0) {
-                    UIPEthernetClass::uip_hdrlen = ((uint8_t*)uip_appdata) - uip_buf;
-                    UIPEthernetClass::uip_packet = UIPEthernet.network.allocBlock(UIPEthernetClass::uip_hdrlen + send_len);
-                    if(UIPEthernetClass::uip_packet != NOBLOCK) {
-                        UIPEthernet.network.copyPacket
+                    send_len = uIPEthernet.network.blockSize(u->packets_out[0]);
+                if (send_len > 0) {
+                    UIPEthernet::uip_hdrlen = ((uint8_t*)uip_appdata) - uip_buf;
+                    UIPEthernet::uip_packet = uIPEthernet.network.allocBlock(UIPEthernet::uip_hdrlen + send_len);
+                    if (UIPEthernet::uip_packet != NOBLOCK) {
+                        uIPEthernet.network.copyPacket
                             (
-                                UIPEthernetClass::uip_packet,
-                                UIPEthernetClass::uip_hdrlen,
+                                UIPEthernet::uip_packet,
+                                UIPEthernet::uip_hdrlen,
                                 u->packets_out[0],
                                 0,
                                 send_len
                             );
-                        UIPEthernetClass::packetstate |= UIPETHERNET_SENDPACKET;
+                        UIPEthernet::packetstate |= UIPETHERNET_SENDPACKET;
                     }
                 }
 
@@ -542,25 +523,25 @@
         }
 
         // don't close connection unless all outgoing packets are sent
-        if(u->state & UIP_CLIENT_CLOSE)
+        if (u->state & UIP_CLIENT_CLOSE)
         {
 #ifdef UIPETHERNET_DEBUG_CLIENT
-            Serial.println(F("UIPClient state UIP_CLIENT_CLOSE"));
+            printf("UIPClient state UIP_CLIENT_CLOSE\r\n");
             UIPClient::_dumpAllData();
 #endif
-            if(u->packets_out[0] == NOBLOCK) {
+            if (u->packets_out[0] == NOBLOCK) {
                 u->state = 0;
                 uip_conn->appstate = NULL;
                 uip_close();
 #ifdef UIPETHERNET_DEBUG_CLIENT
-                Serial.println(F("no blocks out -> free userdata"));
+                printf("no blocks out -> free userdata\r\n");
                 UIPClient::_dumpAllData();
 #endif
             }
             else {
                 uip_stop();
 #ifdef UIPETHERNET_DEBUG_CLIENT
-                Serial.println(F("blocks outstanding transfer -> uip_stop()"));
+                printf("blocks outstanding transfer -> uip_stop()\r\n");
 #endif
             }
         }
@@ -578,9 +559,9 @@
  * @retval
  */
 uip_userdata_t* UIPClient::_allocateData(void) {
-    for(uint8_t sock = 0; sock < UIP_CONNS; sock++) {
+    for (uint8_t sock = 0; sock < UIP_CONNS; sock++) {
         uip_userdata_t*     data = &UIPClient::all_data[sock];
-        if(!data->state) {
+        if (!data->state) {
             data->state = sock | UIP_CLIENT_CONNECTED;
             memset(&data->packets_in[0], 0, sizeof(uip_userdata_t) - sizeof(data->state));
             return data;
@@ -597,8 +578,8 @@
  * @retval
  */
 uint8_t UIPClient::_currentBlock(memhandle* block) {
-    for(uint8_t i = 1; i < UIP_SOCKET_NUMPACKETS; i++) {
-        if(block[i] == NOBLOCK)
+    for (uint8_t i = 1; i < UIP_SOCKET_NUMPACKETS; i++) {
+        if (block[i] == NOBLOCK)
             return i - 1;
     }
 
@@ -615,29 +596,25 @@
 {
 #ifdef UIPETHERNET_DEBUG_CLIENT
     memhandle*  start = block;
-    Serial.print(F("eatblock("));
-    Serial.print(*block);
-    Serial.print(F("): "));
-    for(uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
-        Serial.print(start[i]);
-        Serial.print(F(" "));
+    printf("eatblock(%d): ", *block);
+    for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
+        printf("%d ", start[i]);
     }
 
-    Serial.print(F("-> "));
+    printf("-> ");
 #endif
-    UIPEthernet.network.freeBlock(block[0]);
-    for(uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS - 1; i++) {
+    uIPEthernet.network.freeBlock(block[0]);
+    for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS - 1; i++) {
         block[i] = block[i + 1];
     }
 
     block[UIP_SOCKET_NUMPACKETS - 1] = NOBLOCK;
 #ifdef UIPETHERNET_DEBUG_CLIENT
-    for(uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
-        Serial.print(start[i]);
-        Serial.print(F(" "));
+    for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
+        printf("%d ", start[i]);
     }
 
-    Serial.println();
+    printf("\r\n");
 #endif
 }
 
@@ -648,8 +625,8 @@
  * @retval
  */
 void UIPClient::_flushBlocks(memhandle* block) {
-    for(uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
-        UIPEthernet.network.freeBlock(block[i]);
+    for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) {
+        uIPEthernet.network.freeBlock(block[i]);
         block[i] = NOBLOCK;
     }
 }
@@ -663,34 +640,27 @@
  * @retval
  */
 void UIPClient::_dumpAllData(void) {
-    for(uint8_t i = 0; i < UIP_CONNS; i++) {
-        Serial.print(F("UIPClient::all_data["));
-        Serial.print(i);
-        Serial.print(F("], state:"));
-        Serial.println(all_data[i].state, BIN);
-        Serial.print(F("packets_in: "));
-        for(uint8_t j = 0; j < UIP_SOCKET_NUMPACKETS; j++) {
-            Serial.print(all_data[i].packets_in[j]);
-            Serial.print(F(" "));
+    for (uint8_t i = 0; i < UIP_CONNS; i++) {
+        printf("UIPClient::all_data[%d], state:%d packets_in: ", i, all_data[i].state);
+        for (uint8_t j = 0; j < UIP_SOCKET_NUMPACKETS; j++) {
+            printf("%d ", all_data[i].packets_in[j]);
         }
 
-        Serial.println();
-        if(all_data[i].state & UIP_CLIENT_REMOTECLOSED) {
-            Serial.print(F("state remote closed, local port: "));
-            Serial.println(htons(((uip_userdata_closed_t *) (&all_data[i]))->lport));
+        printf("\r\n");
+        if (all_data[i].state & UIP_CLIENT_REMOTECLOSED) {
+            printf("state remote closed, local port: %d", htons(((uip_userdata_closed_t *) (&all_data[i]))->lport));
+            printf("\r\n");
         }
         else {
-            Serial.print(F("packets_out: "));
-            for(uint8_t j = 0; j < UIP_SOCKET_NUMPACKETS; j++) {
-                Serial.print(all_data[i].packets_out[j]);
-                Serial.print(F(" "));
+            printf("packets_out: ");
+            for (uint8_t j = 0; j < UIP_SOCKET_NUMPACKETS; j++) {
+                printf("%d ", all_data[i].packets_out[j]);
             }
 
-            Serial.println();
-            Serial.print(F("out_pos: "));
-            Serial.println(all_data[i].out_pos);
+            printf("\r\n");
+            printf("out_pos: %d\r\n", all_data[i].out_pos);
         }
     }
 }
 #endif
-
+