@@ -184,13 +184,20 @@ void ESP8266Interface::_connect_async()
184184 return ;
185185 }
186186 _connect_retval = _esp.connect (ap_ssid, ap_pass);
187+ int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
187188 if (_connect_retval == NSAPI_ERROR_OK || _connect_retval == NSAPI_ERROR_AUTH_FAILURE
188- || _connect_retval == NSAPI_ERROR_NO_SSID) {
189+ || _connect_retval == NSAPI_ERROR_NO_SSID
190+ || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
189191 _connect_event_id = 0 ;
192+ _conn_timer.stop ();
193+ if (timeleft_ms <= 0 ) {
194+ _connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
195+ }
190196 _if_connected.notify_all ();
191197 } else {
192198 // Postpone to give other stuff time to run
193- _connect_event_id = _global_event_queue->call_in (ESP8266_CONNECT_TIMEOUT, callback (this , &ESP8266Interface::_connect_async));
199+ _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL_MS,
200+ callback (this , &ESP8266Interface::_connect_async));
194201 if (!_connect_event_id) {
195202 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
196203 " ESP8266Interface::_connect_async(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
@@ -233,6 +240,9 @@ int ESP8266Interface::connect()
233240
234241 _connect_retval = NSAPI_ERROR_NO_CONNECTION;
235242 MBED_ASSERT (!_connect_event_id);
243+ _conn_timer.stop ();
244+ _conn_timer.reset ();
245+ _conn_timer.start ();
236246 _connect_event_id = _global_event_queue->call (callback (this , &ESP8266Interface::_connect_async));
237247
238248 if (!_connect_event_id) {
@@ -315,7 +325,7 @@ int ESP8266Interface::disconnect()
315325 _initialized = false ;
316326
317327 nsapi_error_t status = _conn_status_to_error ();
318- if (status == NSAPI_ERROR_NO_CONNECTION || ! get_ip_address () ) {
328+ if (status == NSAPI_ERROR_NO_CONNECTION) {
319329 return NSAPI_ERROR_NO_CONNECTION;
320330 }
321331
0 commit comments