From c4d96e6309a3823b10ed6738b5bf4c6b831467f9 Mon Sep 17 00:00:00 2001 From: Veijo Pesonen Date: Mon, 21 Jan 2019 14:37:19 +0200 Subject: [PATCH] ESP8266: provides blocking/non-blocking connect Implements NetworkInterface::set_blocking() and implements the functionality to distinguish between the two in connect() --- .../wifi/esp8266-driver/ESP8266Interface.cpp | 20 ++++++++++++++++++- .../wifi/esp8266-driver/ESP8266Interface.h | 11 ++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index 8a88a74b24..08c9100587 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -55,6 +55,8 @@ ESP8266Interface::ESP8266Interface() : _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS), _rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset _ap_sec(NSAPI_SECURITY_UNKNOWN), + _if_blocking(true), + _if_connected(_cmutex), _initialized(false), _conn_stat(NSAPI_STATUS_DISCONNECTED), _conn_stat_cb(NULL), @@ -84,6 +86,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r : _esp(tx, rx, debug, rts, cts), _rst_pin(rst), _ap_sec(NSAPI_SECURITY_UNKNOWN), + _if_blocking(true), + _if_connected(_cmutex), _initialized(false), _conn_stat(NSAPI_STATUS_DISCONNECTED), _conn_stat_cb(NULL), @@ -194,6 +198,7 @@ void ESP8266Interface::_connect_async() } } else { _connect_event_id = 0; + _if_connected.notify_all(); } _cmutex.unlock(); } @@ -229,14 +234,19 @@ int ESP8266Interface::connect() } _cmutex.lock(); - MBED_ASSERT(!_connect_event_id); + MBED_ASSERT(!_connect_event_id); _connect_event_id = _global_event_queue->call(callback(this, &ESP8266Interface::_connect_async)); if (!_connect_event_id) { MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \ "connect(): unable to add event to queue"); } + + while (_if_blocking && (_conn_status_to_error() != NSAPI_ERROR_IS_CONNECTED)) { + _if_connected.wait(); + } + _cmutex.unlock(); return NSAPI_ERROR_OK; @@ -795,4 +805,12 @@ nsapi_error_t ESP8266Interface::_conn_status_to_error() return ret; } +nsapi_error_t ESP8266Interface::set_blocking(bool blocking) +{ + _if_blocking = blocking; + + return NSAPI_ERROR_OK; +} + + #endif diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index 24c0b5e34a..f123cf27cd 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.h +++ b/components/wifi/esp8266-driver/ESP8266Interface.h @@ -29,6 +29,7 @@ #include "features/netsocket/WiFiAccessPoint.h" #include "features/netsocket/WiFiInterface.h" #include "platform/Callback.h" +#include "rtos/ConditionVariable.h" #include "rtos/Mutex.h" #define ESP8266_SOCKET_COUNT 5 @@ -317,6 +318,13 @@ protected: return this; } + /** Set blocking status of connect() which by default should be blocking. + * + * @param blocking Use true to make connect() blocking. + * @return NSAPI_ERROR_OK on success, negative error code on failure. + */ + virtual nsapi_error_t set_blocking(bool blocking); + private: // AT layer ESP8266 _esp; @@ -342,6 +350,9 @@ private: char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */ nsapi_security_t _ap_sec; + bool _if_blocking; // NetworkInterface, blocking or not + rtos::ConditionVariable _if_connected; + // connect status reporting nsapi_error_t _conn_status_to_error();