diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index 9fc7fe10432..c1bec64ff43 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -184,13 +184,20 @@ void ESP8266Interface::_connect_async() return; } _connect_retval = _esp.connect(ap_ssid, ap_pass); + int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms(); if (_connect_retval == NSAPI_ERROR_OK || _connect_retval == NSAPI_ERROR_AUTH_FAILURE - || _connect_retval == NSAPI_ERROR_NO_SSID) { + || _connect_retval == NSAPI_ERROR_NO_SSID + || ((_if_blocking == true) && (timeleft_ms <= 0))) { _connect_event_id = 0; + _conn_timer.stop(); + if (timeleft_ms <= 0) { + _connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT; + } _if_connected.notify_all(); } else { // Postpone to give other stuff time to run - _connect_event_id = _global_event_queue->call_in(ESP8266_CONNECT_TIMEOUT, callback(this, &ESP8266Interface::_connect_async)); + _connect_event_id = _global_event_queue->call_in(ESP8266_INTERFACE_CONNECT_INTERVAL_MS, + callback(this, &ESP8266Interface::_connect_async)); if (!_connect_event_id) { MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \ "ESP8266Interface::_connect_async(): unable to add event to queue. Increase \"events.shared-eventsize\"\n"); @@ -233,6 +240,9 @@ int ESP8266Interface::connect() _connect_retval = NSAPI_ERROR_NO_CONNECTION; MBED_ASSERT(!_connect_event_id); + _conn_timer.stop(); + _conn_timer.reset(); + _conn_timer.start(); _connect_event_id = _global_event_queue->call(callback(this, &ESP8266Interface::_connect_async)); if (!_connect_event_id) { @@ -315,7 +325,7 @@ int ESP8266Interface::disconnect() _initialized = false; nsapi_error_t status = _conn_status_to_error(); - if (status == NSAPI_ERROR_NO_CONNECTION || !get_ip_address()) { + if (status == NSAPI_ERROR_NO_CONNECTION) { return NSAPI_ERROR_NO_CONNECTION; } diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index 1166f6892e2..1f8dfb11c3d 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.h +++ b/components/wifi/esp8266-driver/ESP8266Interface.h @@ -19,6 +19,7 @@ #if DEVICE_SERIAL && DEVICE_INTERRUPTIN && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT) #include "drivers/DigitalOut.h" +#include "drivers/Timer.h" #include "ESP8266/ESP8266.h" #include "events/EventQueue.h" #include "events/mbed_shared_queues.h" @@ -34,6 +35,9 @@ #define ESP8266_SOCKET_COUNT 5 +#define ESP8266_INTERFACE_CONNECT_INTERVAL_MS (5000) +#define ESP8266_INTERFACE_CONNECT_TIMEOUT_MS (2 * ESP8266_CONNECT_TIMEOUT + ESP8266_INTERFACE_CONNECT_INTERVAL_MS) + #ifdef TARGET_FF_ARDUINO #ifndef MBED_CONF_ESP8266_TX #define MBED_CONF_ESP8266_TX D1 @@ -94,6 +98,9 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface { * * Attempts to connect to a WiFi network. * + * If interface is configured blocking it will timeout after up to + * ESP8266_INTERFACE_CONNECT_TIMEOUT_MS + ESP8266_CONNECT_TIMEOUT ms. + * * @param ssid Name of the network to connect to * @param pass Security passphrase to connect to the network * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) @@ -408,6 +415,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface { // connect status reporting nsapi_error_t _conn_status_to_error(); + mbed::Timer _conn_timer; // Drivers's socket info struct _sock_info {