Skip to content

Commit 7e7dc94

Browse files
author
Veijo Pesonen
committed
Fix handling of UDP datagrams and TCP streams
1 parent c2b7e15 commit 7e7dc94

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

ESP8266/ESP8266.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ void ESP8266::_packet_handler()
373373
_packets_end = &packet->next;
374374
}
375375

376-
int32_t ESP8266::recv(int id, void *data, uint32_t amount)
376+
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount)
377377
{
378378
_smutex.lock();
379379
// Poll for inbound packets
@@ -415,7 +415,39 @@ int32_t ESP8266::recv(int id, void *data, uint32_t amount)
415415

416416
_smutex.unlock();
417417

418-
return -1;
418+
return NSAPI_ERROR_WOULD_BLOCK;
419+
}
420+
421+
int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount)
422+
{
423+
_smutex.lock();
424+
// Poll for inbound packets
425+
while (_parser.process_oob()) {
426+
}
427+
428+
// check if any packets are ready for us
429+
for (struct packet **p = &_packets; *p; p = &(*p)->next) {
430+
if ((*p)->id == id) {
431+
struct packet *q = *p;
432+
433+
// Return and remove packet (truncated if necessary)
434+
uint32_t len = q->len < amount ? q->len : amount;
435+
memcpy(data, q+1, len);
436+
437+
if (_packets_end == &(*p)->next) {
438+
_packets_end = p;
439+
}
440+
*p = (*p)->next;
441+
_smutex.unlock();
442+
443+
free(q);
444+
return len;
445+
}
446+
}
447+
448+
_smutex.unlock();
449+
450+
return NSAPI_ERROR_WOULD_BLOCK;
419451
}
420452

421453
bool ESP8266::close(int id)

ESP8266/ESP8266.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,24 @@ class ESP8266
164164
nsapi_error_t send(int id, const void *data, uint32_t amount);
165165

166166
/**
167-
* Receives data from an open socket
167+
* Receives datagram from an open UDP socket
168168
*
169169
* @param id id to receive from
170170
* @param data placeholder for returned information
171171
* @param amount number of bytes to be received
172172
* @return the number of bytes received
173173
*/
174-
int32_t recv(int id, void *data, uint32_t amount);
174+
int32_t recv_udp(int id, void *data, uint32_t amount);
175+
176+
/**
177+
* Receives stream data from an open TCP socket
178+
*
179+
* @param id id to receive from
180+
* @param data placeholder for returned information
181+
* @param amount number of bytes to be received
182+
* @return the number of bytes received
183+
*/
184+
int32_t recv_tcp(int id, void *data, uint32_t amount);
175185

176186
/**
177187
* Closes a socket

ESP8266Interface.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,14 @@ int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
424424
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
425425
_esp.setTimeout(ESP8266_RECV_TIMEOUT);
426426

427-
int32_t recv = _esp.recv(socket->id, data, size);
428-
if(recv == 0) {
429-
socket->connected = false; // No more data, ESP has closed the socket
430-
}
431-
if (recv < 0) {
432-
return NSAPI_ERROR_WOULD_BLOCK;
427+
int32_t recv;
428+
if (socket->proto == NSAPI_TCP) {
429+
recv = _esp.recv_tcp(socket->id, data, size);
430+
if (recv <= 0) {
431+
socket->connected = false;
432+
}
433+
} else {
434+
recv = _esp.recv_udp(socket->id, data, size);
433435
}
434436

435437
return recv;

0 commit comments

Comments
 (0)