Skip to content

Commit 69b9f3f

Browse files
authored
Merge pull request ARMmbed#10377 from michalpasztamobica/esp8266_connect_failures
Fix ESP8266 driver behavior on connection failures
2 parents fc12229 + 3e70df7 commit 69b9f3f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 13 additions & 3 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) {
@@ -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

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)