@@ -557,6 +557,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
557
557
558
558
nsapi_error_t ESP8266::send (int id, const void *data, uint32_t amount)
559
559
{
560
+ nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
560
561
// +CIPSEND supports up to 2048 bytes at a time
561
562
// Data stream can be truncated
562
563
if (amount > 2048 && _sock_i[id].proto == NSAPI_TCP) {
@@ -570,31 +571,50 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
570
571
_smutex.lock ();
571
572
set_timeout (ESP8266_SEND_TIMEOUT);
572
573
_busy = false ;
573
- if (_parser.send (" AT+CIPSEND=%d,%lu" , id, amount)
574
- && _parser.recv (" >" )
575
- && _parser.write ((char *)data, (int )amount) >= 0
576
- && _parser.recv (" SEND OK" )) {
577
- // No flow control, data overrun is possible
578
- if (_serial_rts == NC) {
579
- while (_parser.process_oob ()); // Drain USART receive register
580
- }
581
- _smutex.unlock ();
582
- return NSAPI_ERROR_OK;
574
+ _error = false ;
575
+ if (!_parser.send (" AT+CIPSEND=%d,%lu" , id, amount)) {
576
+ tr_debug (" ESP8266::send(): AT+CIPSEND failed" );
577
+ goto END;
583
578
}
584
- if (_error) {
585
- _error = false ;
579
+
580
+ if (!_parser.recv (" >" )) {
581
+ tr_debug (" ESP8266::send(): didn't get \" >\" " );
582
+ ret = NSAPI_ERROR_WOULD_BLOCK;
583
+ goto END;
586
584
}
585
+
586
+ if (_parser.write ((char *)data, (int )amount) >= 0 && _parser.recv (" SEND OK" )) {
587
+ ret = NSAPI_ERROR_OK;
588
+ }
589
+
590
+ END:
591
+ _process_oob (ESP8266_RECV_TIMEOUT, true ); // Drain USART receive register to avoid data overrun
592
+
593
+ // error hierarchy, from low to high
587
594
if (_busy) {
588
- set_timeout ();
589
- _smutex.unlock ();
590
- tr_debug (" returning WOULD_BLOCK" );
591
- return NSAPI_ERROR_WOULD_BLOCK;
595
+ ret = NSAPI_ERROR_WOULD_BLOCK;
596
+ tr_debug (" ESP8266::send(): modem busy" );
597
+ }
598
+
599
+ if (ret == NSAPI_ERROR_DEVICE_ERROR) {
600
+ ret = NSAPI_ERROR_WOULD_BLOCK;
601
+ tr_debug (" ESP8266::send(): send failed" );
602
+ }
603
+
604
+ if (_error) {
605
+ ret = NSAPI_ERROR_CONNECTION_LOST;
606
+ tr_debug (" ESP8266::send(): connection disrupted" );
607
+ }
608
+
609
+ if (!_sock_i[id].open && ret != NSAPI_ERROR_OK) {
610
+ ret = NSAPI_ERROR_CONNECTION_LOST;
611
+ tr_debug (" ESP8266::send(): socket closed abruptly" );
592
612
}
593
613
594
614
set_timeout ();
595
615
_smutex.unlock ();
596
616
597
- return NSAPI_ERROR_DEVICE_ERROR ;
617
+ return ret ;
598
618
}
599
619
600
620
void ESP8266::_oob_packet_hdlr ()
0 commit comments