Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ESP8266 driver behavior on connection failures #10377

Merged
merged 2 commits into from Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -315,7 +325,7 @@ int ESP8266Interface::disconnect()
_initialized = false;

nsapi_error_t status = _conn_status_to_error();
if (status == NSAPI_ERROR_NO_CONNECTION || !get_ip_address()) {
if (status == NSAPI_ERROR_NO_CONNECTION) {
return NSAPI_ERROR_NO_CONNECTION;
}

Expand Down
8 changes: 8 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,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
Expand Down Expand Up @@ -94,6 +98,9 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
*
* 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.
michalpasztamobica marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 +415,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