Skip to content

Commit

Permalink
Move Ethernet socket level stuff to utility/socket.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Aug 1, 2014
1 parent abb37e2 commit 53924e9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions libraries/Ethernet/src/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libraries/Ethernet/src/EthernetUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
12 changes: 12 additions & 0 deletions libraries/Ethernet/src/utility/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
*
Expand Down
2 changes: 2 additions & 0 deletions libraries/Ethernet/src/utility/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 53924e9

Please sign in to comment.