From 3e70df7f0ac75a78e6bd45794fe044bc9cd69dca Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Thu, 11 Apr 2019 17:14:13 +0300 Subject: [PATCH] ESP8266: Timeout if chip keeps returning errors The ESP chip returns timeout, but keeps trying to connect, so we want to keep track of time ourselves, instead of relying on the chip's timeout. This fixes failing WIFI-CONNECT-SECURE-FAIL test. --- .../wifi/esp8266-driver/ESP8266Interface.cpp | 14 ++++++++++++-- components/wifi/esp8266-driver/ESP8266Interface.h | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index f976b3a49e..c1bec64ff4 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) { diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index 1166f6892e..1f8dfb11c3 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 @@ 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) @@ -408,6 +415,7 @@ private: // connect status reporting nsapi_error_t _conn_status_to_error(); + mbed::Timer _conn_timer; // Drivers's socket info struct _sock_info {