mirror of https://github.com/ARMmbed/mbed-os.git
ESP8266 send returns WOULD_BLOCK error when busy
parent
08d1127729
commit
d6e385b330
|
@ -50,6 +50,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
|
|||
_fail(false),
|
||||
_sock_already(false),
|
||||
_closed(false),
|
||||
_busy(false),
|
||||
_conn_status(NSAPI_STATUS_DISCONNECTED)
|
||||
{
|
||||
_serial.set_baud(ESP8266_DEFAULT_BAUD_RATE);
|
||||
|
@ -572,29 +573,33 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
|
|||
return NSAPI_ERROR_PARAMETER;
|
||||
}
|
||||
|
||||
//May take a second try if device is busy
|
||||
for (unsigned i = 0; i < 2; i++) {
|
||||
_smutex.lock();
|
||||
set_timeout(ESP8266_SEND_TIMEOUT);
|
||||
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
|
||||
&& _parser.recv(">")
|
||||
&& _parser.write((char *)data, (int)amount) >= 0
|
||||
&& _parser.recv("SEND OK")) {
|
||||
// No flow control, data overrun is possible
|
||||
if (_serial_rts == NC) {
|
||||
while (_parser.process_oob()); // Drain USART receive register
|
||||
}
|
||||
_smutex.unlock();
|
||||
return NSAPI_ERROR_OK;
|
||||
_smutex.lock();
|
||||
set_timeout(ESP8266_SEND_TIMEOUT);
|
||||
_busy = false;
|
||||
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
|
||||
&& _parser.recv(">")
|
||||
&& _parser.write((char *)data, (int)amount) >= 0
|
||||
&& _parser.recv("SEND OK")) {
|
||||
// No flow control, data overrun is possible
|
||||
if (_serial_rts == NC) {
|
||||
while (_parser.process_oob()); // Drain USART receive register
|
||||
}
|
||||
if (_error) {
|
||||
_error = false;
|
||||
}
|
||||
|
||||
_smutex.unlock();
|
||||
return NSAPI_ERROR_OK;
|
||||
}
|
||||
if (_error) {
|
||||
_error = false;
|
||||
}
|
||||
if (_busy) {
|
||||
set_timeout();
|
||||
_smutex.unlock();
|
||||
tr_debug("returning WOULD_BLOCK");
|
||||
return NSAPI_ERROR_WOULD_BLOCK;
|
||||
}
|
||||
|
||||
set_timeout();
|
||||
_smutex.unlock();
|
||||
|
||||
return NSAPI_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
|
@ -963,6 +968,7 @@ void ESP8266::_oob_busy()
|
|||
"ESP8266::_oob_busy() AT timeout\n");
|
||||
}
|
||||
_busy = true;
|
||||
_parser.abort();
|
||||
}
|
||||
|
||||
void ESP8266::_oob_tcp_data_hdlr()
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "platform/Callback.h"
|
||||
#include "platform/mbed_debug.h"
|
||||
#include "platform/mbed_wait_api.h"
|
||||
#include "Kernel.h"
|
||||
|
||||
#ifndef MBED_CONF_ESP8266_DEBUG
|
||||
#define MBED_CONF_ESP8266_DEBUG false
|
||||
|
@ -518,7 +519,16 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
|
|||
return NSAPI_ERROR_NO_SOCKET;
|
||||
}
|
||||
|
||||
status = _esp.send(socket->id, data, size);
|
||||
unsigned long int sendStartTime = rtos::Kernel::get_ms_count();
|
||||
do {
|
||||
status = _esp.send(socket->id, data, size);
|
||||
} while ((sendStartTime - rtos::Kernel::get_ms_count() < 50)
|
||||
&& (status != NSAPI_ERROR_OK));
|
||||
|
||||
if (status == NSAPI_ERROR_WOULD_BLOCK) {
|
||||
debug("Enqueuing the event call");
|
||||
_global_event_queue->call_in(100, callback(this, &ESP8266Interface::event));
|
||||
}
|
||||
|
||||
return status != NSAPI_ERROR_OK ? status : size;
|
||||
}
|
||||
|
@ -720,4 +730,5 @@ void ESP8266Interface::proc_oob_evnt()
|
|||
{
|
||||
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue