Skip to content

Commit 3135124

Browse files
ESP8266: Improve error handling and partial sends
1 parent 02050c5 commit 3135124

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
612612
return done;
613613
}
614614

615-
nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
615+
nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
616616
{
617617
nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
618618
unsigned int send_ack_retries = 3;
@@ -651,15 +651,21 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
651651
goto RETRY;
652652
}
653653
}
654+
// This means ESP8266 hasn't even started to receive data
654655
tr_debug("send(): Didn't get \">\"");
655-
ret = NSAPI_ERROR_WOULD_BLOCK;
656+
if (_sock_i[id].proto == NSAPI_TCP) {
657+
ret = NSAPI_ERROR_WOULD_BLOCK;
658+
} else if (_sock_i[id].proto == NSAPI_UDP) {
659+
ret = NSAPI_ERROR_NO_MEMORY;
660+
}
656661
goto END;
657662
}
658663
_ok_received = false;
659664
_parser.remove_oob("OK");
660665

661666
if (_parser.write((char *)data, (int)amount) < 0) {
662-
tr_debug("Failed to write data");
667+
tr_debug("Failed to write serial data");
668+
// Serial is not working, serious error, reset needed.
663669
ret = NSAPI_ERROR_DEVICE_ERROR;
664670
goto END;
665671
}
@@ -669,6 +675,11 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
669675
if (!_parser.recv("Recv %d bytes", &bytes_confirmed)) {
670676
tr_debug("Bytes not confirmed.");
671677
ret = NSAPI_ERROR_DEVICE_ERROR;
678+
if (_sock_i[id].proto == NSAPI_TCP) {
679+
ret = NSAPI_ERROR_WOULD_BLOCK;
680+
} else if (_sock_i[id].proto == NSAPI_UDP) {
681+
ret = NSAPI_ERROR_NO_MEMORY;
682+
}
672683
goto END;
673684
} else if (bytes_confirmed != amount) {
674685
tr_debug("Error: confirmed %d bytes, but expected %d.", bytes_confirmed, amount);
@@ -689,7 +700,7 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
689700
goto RETRY_SEND_ACK;
690701
}
691702
} else {
692-
ret = NSAPI_ERROR_OK;
703+
ret = amount;
693704
}
694705

695706
END:
@@ -711,9 +722,9 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
711722
tr_debug("send(): Connection disrupted.");
712723
}
713724

714-
if (!_sock_i[id].open && ret != NSAPI_ERROR_OK) {
725+
if (!_sock_i[id].open && ret < NSAPI_ERROR_OK) {
715726
ret = NSAPI_ERROR_CONNECTION_LOST;
716-
tr_debug("send(): Socket closed abruptly.");
727+
tr_debug("send(): Socket %d closed abruptly.", id);
717728
}
718729

719730
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)