diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index c9fdebbab3..09b4036948 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -174,13 +174,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"); @@ -223,6 +230,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) { diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index 4abde4455b..7fbb2a7b01 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 @@ -82,6 +86,9 @@ public: * * 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) @@ -355,6 +362,7 @@ private: // connect status reporting nsapi_error_t _conn_status_to_error(); + mbed::Timer _conn_timer; // Drivers's socket info struct _sock_info {