Skip to content

Commit

Permalink
ESP8266: Timeout if chip keeps returning errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michalpasztamobica committed Apr 15, 2019
1 parent 8eda11a commit 453e485
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Expand Up @@ -184,13 +184,19 @@ 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(timeleft_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");
Expand Down Expand Up @@ -233,6 +239,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) {
Expand Down
6 changes: 6 additions & 0 deletions components/wifi/esp8266-driver/ESP8266Interface.h
Expand Up @@ -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"
Expand All @@ -34,6 +35,8 @@

#define ESP8266_SOCKET_COUNT 5

#define ESP8266_INTERFACE_CONNECT_TIMEOUT_MS (2 * ESP8266_CONNECT_TIMEOUT + 15000)

#ifdef TARGET_FF_ARDUINO
#ifndef MBED_CONF_ESP8266_TX
#define MBED_CONF_ESP8266_TX D1
Expand Down Expand Up @@ -94,6 +97,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
*
* Attempts to connect to a WiFi network.
*
* If interface is configured blocking it will timeout after ESP8266_INTERFACE_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)
Expand Down Expand Up @@ -408,6 +413,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {

// connect status reporting
nsapi_error_t _conn_status_to_error();
mbed::Timer _conn_timer;

// Drivers's socket info
struct _sock_info {
Expand Down

0 comments on commit 453e485

Please sign in to comment.