Skip to content

Commit 902fedd

Browse files
ESP8266: Improve error handling and partial sends
1 parent de2896c commit 902fedd

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
614614
return done;
615615
}
616616

617-
nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
617+
nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
618618
{
619619
if (_prev_send_ok_pending && _sock_i[id].proto == NSAPI_TCP) {
620620
tr_debug("send(): Previous packet was not ACK-ed with SEND OK.");
@@ -649,7 +649,7 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
649649
// This means ESP8266 hasn't even started to receive data
650650
tr_debug("send(): Didn't get \">\"");
651651
if (_sock_i[id].proto == NSAPI_TCP) {
652-
ret = NSAPI_ERROR_WOULD_BLOCK; // Not neccesarily critical error.
652+
ret = NSAPI_ERROR_WOULD_BLOCK; // Not necessarily critical error.
653653
} else if (_sock_i[id].proto == NSAPI_UDP) {
654654
ret = NSAPI_ERROR_NO_MEMORY;
655655
}
@@ -667,6 +667,11 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
667667
if (!_parser.recv("Recv %d bytes", &bytes_confirmed)) {
668668
tr_debug("send(): Bytes not confirmed.");
669669
ret = NSAPI_ERROR_DEVICE_ERROR;
670+
if (_sock_i[id].proto == NSAPI_TCP) {
671+
ret = NSAPI_ERROR_WOULD_BLOCK;
672+
} else if (_sock_i[id].proto == NSAPI_UDP) {
673+
ret = NSAPI_ERROR_NO_MEMORY;
674+
}
670675
goto END;
671676
} else if (bytes_confirmed != amount) {
672677
tr_debug("send(): Error: confirmed %d bytes, but expected %d.", bytes_confirmed, amount);
@@ -724,7 +729,7 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
724729

725730
if (!_sock_i[id].open && ret < 0) {
726731
ret = NSAPI_ERROR_CONNECTION_LOST;
727-
tr_debug("send(): Socket closed abruptly.");
732+
tr_debug("send(): Socket %d closed abruptly.", id);
728733
}
729734

730735
set_timeout();

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ class ESP8266 {
255255
*
256256
* @param id id of socket to send to
257257
* @param data data to be sent
258-
* @param amount amount of data to be sent - max 1024
259-
* @return NSAPI_ERROR_OK in success, negative error code in failure
258+
* @param amount amount of data to be sent - max 2048
259+
* @return number of bytes on success, negative error code in failure
260260
*/
261-
nsapi_error_t send(int id, const void *data, uint32_t amount);
261+
nsapi_size_or_error_t send(int id, const void *data, uint32_t amount);
262262

263263
/**
264264
* Receives datagram from an open UDP socket

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *
849849

850850
int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
851851
{
852-
nsapi_error_t status;
852+
nsapi_size_or_error_t status;
853853
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
854854
uint8_t expect_false = false;
855855

@@ -881,7 +881,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
881881
status = NSAPI_ERROR_DEVICE_ERROR;
882882
}
883883

884-
return status != NSAPI_ERROR_OK ? status : size;
884+
return status;
885885
}
886886

887887
int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)

0 commit comments

Comments
 (0)