Skip to content

Fix handling of UDP datagrams and TCP streams #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ void ESP8266::_packet_handler()
_packets_end = &packet->next;
}

int32_t ESP8266::recv(int id, void *data, uint32_t amount)
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount)
{
_smutex.lock();
// Poll for inbound packets
Expand Down Expand Up @@ -415,7 +415,39 @@ int32_t ESP8266::recv(int id, void *data, uint32_t amount)

_smutex.unlock();

return -1;
return NSAPI_ERROR_WOULD_BLOCK;
}

int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount)
{
_smutex.lock();
// Poll for inbound packets
while (_parser.process_oob()) {
}

// check if any packets are ready for us
for (struct packet **p = &_packets; *p; p = &(*p)->next) {
if ((*p)->id == id) {
struct packet *q = *p;

// Return and remove packet (truncated if necessary)
uint32_t len = q->len < amount ? q->len : amount;
memcpy(data, q+1, len);

if (_packets_end == &(*p)->next) {
_packets_end = p;
}
*p = (*p)->next;
_smutex.unlock();

free(q);
return len;
}
}

_smutex.unlock();

return NSAPI_ERROR_WOULD_BLOCK;
}

bool ESP8266::close(int id)
Expand Down
14 changes: 12 additions & 2 deletions ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,24 @@ class ESP8266
nsapi_error_t send(int id, const void *data, uint32_t amount);

/**
* Receives data from an open socket
* Receives datagram from an open UDP socket
*
* @param id id to receive from
* @param data placeholder for returned information
* @param amount number of bytes to be received
* @return the number of bytes received
*/
int32_t recv(int id, void *data, uint32_t amount);
int32_t recv_udp(int id, void *data, uint32_t amount);

/**
* Receives stream data from an open TCP socket
*
* @param id id to receive from
* @param data placeholder for returned information
* @param amount number of bytes to be received
* @return the number of bytes received
*/
int32_t recv_tcp(int id, void *data, uint32_t amount);

/**
* Closes a socket
Expand Down
14 changes: 8 additions & 6 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,14 @@ int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
_esp.setTimeout(ESP8266_RECV_TIMEOUT);

int32_t recv = _esp.recv(socket->id, data, size);
if(recv == 0) {
socket->connected = false; // No more data, ESP has closed the socket
}
if (recv < 0) {
return NSAPI_ERROR_WOULD_BLOCK;
int32_t recv;
if (socket->proto == NSAPI_TCP) {
recv = _esp.recv_tcp(socket->id, data, size);
if (recv <= 0) {
socket->connected = false;
}
} else {
recv = _esp.recv_udp(socket->id, data, size);
}

return recv;
Expand Down