Skip to content

ESP8266 send returns WOULD_BLOCK error when busy #9051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions components/wifi/esp8266-driver/ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
if (_error) {
_error = false;
_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
}

_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;
}

Expand Down Expand Up @@ -963,6 +968,7 @@ void ESP8266::_oob_busy()
"ESP8266::_oob_busy() AT timeout\n");
}
_busy = true;
_parser.abort();
}

void ESP8266::_oob_tcp_data_hdlr()
Expand Down
13 changes: 12 additions & 1 deletion components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -720,4 +730,5 @@ void ESP8266Interface::proc_oob_evnt()
{
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
}

#endif