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