From 53924e9d58456ebf77cf3e7fd5529b29886b0bd1 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Fri, 1 Aug 2014 06:03:38 -0700 Subject: [PATCH] Move Ethernet socket level stuff to utility/socket.cpp --- libraries/Ethernet/src/EthernetClient.cpp | 6 +++--- libraries/Ethernet/src/EthernetUdp.cpp | 4 ++-- libraries/Ethernet/src/utility/socket.cpp | 12 ++++++++++++ libraries/Ethernet/src/utility/socket.h | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/libraries/Ethernet/src/EthernetClient.cpp b/libraries/Ethernet/src/EthernetClient.cpp index 44a21dcc8..7a36a3288 100644 --- a/libraries/Ethernet/src/EthernetClient.cpp +++ b/libraries/Ethernet/src/EthernetClient.cpp @@ -40,7 +40,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) { return 0; for (int i = 0; i < MAX_SOCK_NUM; i++) { - uint8_t s = W5100.readSnSR(i); + uint8_t s = socketStatus(i); if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) { _sock = i; break; @@ -88,7 +88,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) { int EthernetClient::available() { if (_sock != MAX_SOCK_NUM) - return W5100.getRXReceivedSize(_sock); + return recvAvailable(_sock); return 0; } @@ -153,7 +153,7 @@ uint8_t EthernetClient::connected() { uint8_t EthernetClient::status() { if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; - return W5100.readSnSR(_sock); + return socketStatus(_sock); } // the next function allows us to use the client returned by diff --git a/libraries/Ethernet/src/EthernetUdp.cpp b/libraries/Ethernet/src/EthernetUdp.cpp index b0df74ba9..b5dcb78cc 100644 --- a/libraries/Ethernet/src/EthernetUdp.cpp +++ b/libraries/Ethernet/src/EthernetUdp.cpp @@ -41,7 +41,7 @@ uint8_t EthernetUDP::begin(uint16_t port) { return 0; for (int i = 0; i < MAX_SOCK_NUM; i++) { - uint8_t s = W5100.readSnSR(i); + uint8_t s = socketStatus(i); if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { _sock = i; break; @@ -120,7 +120,7 @@ int EthernetUDP::parsePacket() // discard any remaining bytes in the last packet flush(); - if (W5100.getRXReceivedSize(_sock) > 0) + if (recvAvailable(_sock) > 0) { //HACK - hand-parse the UDP packet using TCP recv method uint8_t tmpBuf[8]; diff --git a/libraries/Ethernet/src/utility/socket.cpp b/libraries/Ethernet/src/utility/socket.cpp index b6d76f1a8..ff2ec3d78 100644 --- a/libraries/Ethernet/src/utility/socket.cpp +++ b/libraries/Ethernet/src/utility/socket.cpp @@ -30,6 +30,12 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag) } +uint8_t socketStatus(SOCKET s) +{ + return W5100.readSnSR(s); +} + + /** * @brief This function close the socket and parameter is "s" which represent the socket number */ @@ -176,6 +182,12 @@ int16_t recv(SOCKET s, uint8_t *buf, int16_t len) } +int16_t recvAvailable(SOCKET s) +{ + return W5100.getRXReceivedSize(s); +} + + /** * @brief Returns the first byte in the receive queue (no checking) * diff --git a/libraries/Ethernet/src/utility/socket.h b/libraries/Ethernet/src/utility/socket.h index 682e84709..37ba85424 100644 --- a/libraries/Ethernet/src/utility/socket.h +++ b/libraries/Ethernet/src/utility/socket.h @@ -4,12 +4,14 @@ #include "utility/w5100.h" extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode) +extern uint8_t socketStatus(SOCKET s); extern void close(SOCKET s); // Close socket extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection) extern void disconnect(SOCKET s); // disconnect the connection extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection) extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP) extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP) +extern int16_t recvAvailable(SOCKET s); extern uint16_t peek(SOCKET s, uint8_t *buf); extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW) extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW)