Skip to content

Commit c4d96e6

Browse files
author
Veijo Pesonen
committed
ESP8266: provides blocking/non-blocking connect
Implements NetworkInterface::set_blocking() and implements the functionality to distinguish between the two in connect()
1 parent 58c1a88 commit c4d96e6

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ ESP8266Interface::ESP8266Interface()
5555
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
5656
_rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset
5757
_ap_sec(NSAPI_SECURITY_UNKNOWN),
58+
_if_blocking(true),
59+
_if_connected(_cmutex),
5860
_initialized(false),
5961
_conn_stat(NSAPI_STATUS_DISCONNECTED),
6062
_conn_stat_cb(NULL),
@@ -84,6 +86,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
8486
: _esp(tx, rx, debug, rts, cts),
8587
_rst_pin(rst),
8688
_ap_sec(NSAPI_SECURITY_UNKNOWN),
89+
_if_blocking(true),
90+
_if_connected(_cmutex),
8791
_initialized(false),
8892
_conn_stat(NSAPI_STATUS_DISCONNECTED),
8993
_conn_stat_cb(NULL),
@@ -194,6 +198,7 @@ void ESP8266Interface::_connect_async()
194198
}
195199
} else {
196200
_connect_event_id = 0;
201+
_if_connected.notify_all();
197202
}
198203
_cmutex.unlock();
199204
}
@@ -229,14 +234,19 @@ int ESP8266Interface::connect()
229234
}
230235

231236
_cmutex.lock();
232-
MBED_ASSERT(!_connect_event_id);
233237

238+
MBED_ASSERT(!_connect_event_id);
234239
_connect_event_id = _global_event_queue->call(callback(this, &ESP8266Interface::_connect_async));
235240

236241
if (!_connect_event_id) {
237242
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
238243
"connect(): unable to add event to queue");
239244
}
245+
246+
while (_if_blocking && (_conn_status_to_error() != NSAPI_ERROR_IS_CONNECTED)) {
247+
_if_connected.wait();
248+
}
249+
240250
_cmutex.unlock();
241251

242252
return NSAPI_ERROR_OK;
@@ -795,4 +805,12 @@ nsapi_error_t ESP8266Interface::_conn_status_to_error()
795805
return ret;
796806
}
797807

808+
nsapi_error_t ESP8266Interface::set_blocking(bool blocking)
809+
{
810+
_if_blocking = blocking;
811+
812+
return NSAPI_ERROR_OK;
813+
}
814+
815+
798816
#endif

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "features/netsocket/WiFiAccessPoint.h"
3030
#include "features/netsocket/WiFiInterface.h"
3131
#include "platform/Callback.h"
32+
#include "rtos/ConditionVariable.h"
3233
#include "rtos/Mutex.h"
3334

3435
#define ESP8266_SOCKET_COUNT 5
@@ -317,6 +318,13 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
317318
return this;
318319
}
319320

321+
/** Set blocking status of connect() which by default should be blocking.
322+
*
323+
* @param blocking Use true to make connect() blocking.
324+
* @return NSAPI_ERROR_OK on success, negative error code on failure.
325+
*/
326+
virtual nsapi_error_t set_blocking(bool blocking);
327+
320328
private:
321329
// AT layer
322330
ESP8266 _esp;
@@ -342,6 +350,9 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
342350
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */
343351
nsapi_security_t _ap_sec;
344352

353+
bool _if_blocking; // NetworkInterface, blocking or not
354+
rtos::ConditionVariable _if_connected;
355+
345356
// connect status reporting
346357
nsapi_error_t _conn_status_to_error();
347358

0 commit comments

Comments
 (0)