Skip to content

Commit 3e70df7

Browse files
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.
1 parent 8eda11a commit 3e70df7

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#if DEVICE_SERIAL && DEVICE_INTERRUPTIN && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT)
2121
#include "drivers/DigitalOut.h"
22+
#include "drivers/Timer.h"
2223
#include "ESP8266/ESP8266.h"
2324
#include "events/EventQueue.h"
2425
#include "events/mbed_shared_queues.h"
@@ -34,6 +35,9 @@
3435

3536
#define ESP8266_SOCKET_COUNT 5
3637

38+
#define ESP8266_INTERFACE_CONNECT_INTERVAL_MS (5000)
39+
#define ESP8266_INTERFACE_CONNECT_TIMEOUT_MS (2 * ESP8266_CONNECT_TIMEOUT + ESP8266_INTERFACE_CONNECT_INTERVAL_MS)
40+
3741
#ifdef TARGET_FF_ARDUINO
3842
#ifndef MBED_CONF_ESP8266_TX
3943
#define MBED_CONF_ESP8266_TX D1
@@ -94,6 +98,9 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
9498
*
9599
* Attempts to connect to a WiFi network.
96100
*
101+
* If interface is configured blocking it will timeout after up to
102+
* ESP8266_INTERFACE_CONNECT_TIMEOUT_MS + ESP8266_CONNECT_TIMEOUT ms.
103+
*
97104
* @param ssid Name of the network to connect to
98105
* @param pass Security passphrase to connect to the network
99106
* @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
@@ -408,6 +415,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
408415

409416
// connect status reporting
410417
nsapi_error_t _conn_status_to_error();
418+
mbed::Timer _conn_timer;
411419

412420
// Drivers's socket info
413421
struct _sock_info {

0 commit comments

Comments
 (0)