Skip to content

Commit 58c1a88

Browse files
author
Veijo Pesonen
committed
ESP8266: turns connect fully to non-blocking mode
Blocking mode connect is not supported anymore when this patch is applied
1 parent c60c524 commit 58c1a88

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ ESP8266Interface::ESP8266Interface()
5959
_conn_stat(NSAPI_STATUS_DISCONNECTED),
6060
_conn_stat_cb(NULL),
6161
_global_event_queue(NULL),
62-
_oob_event_id(0)
62+
_oob_event_id(0),
63+
_connect_event_id(0)
6364
{
6465
memset(_cbs, 0, sizeof(_cbs));
6566
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -87,7 +88,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
8788
_conn_stat(NSAPI_STATUS_DISCONNECTED),
8889
_conn_stat_cb(NULL),
8990
_global_event_queue(NULL),
90-
_oob_event_id(0)
91+
_oob_event_id(0),
92+
_connect_event_id(0)
9193
{
9294
memset(_cbs, 0, sizeof(_cbs));
9395
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -111,6 +113,12 @@ ESP8266Interface::~ESP8266Interface()
111113
_global_event_queue->cancel(_oob_event_id);
112114
}
113115

116+
_cmutex.lock();
117+
if (_connect_event_id) {
118+
_global_event_queue->cancel(_connect_event_id);
119+
}
120+
_cmutex.unlock();
121+
114122
// Power down the modem
115123
_rst_pin.rst_assert();
116124
}
@@ -167,6 +175,29 @@ void ESP8266Interface::_oob2global_event_queue()
167175
}
168176
}
169177

178+
void ESP8266Interface::_connect_async()
179+
{
180+
_cmutex.lock();
181+
if (!_connect_event_id) {
182+
tr_debug("_connect_async(): cancelled");
183+
_cmutex.unlock();
184+
return;
185+
}
186+
187+
if (_esp.connect(ap_ssid, ap_pass) != NSAPI_ERROR_OK) {
188+
// Postpone to give other stuff time to run
189+
_connect_event_id = _global_event_queue->call_in(ESP8266_CONNECT_TIMEOUT, callback(this, &ESP8266Interface::_connect_async));
190+
191+
if (!_connect_event_id) {
192+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
193+
"_connect_async(): unable to add event to queue");
194+
}
195+
} else {
196+
_connect_event_id = 0;
197+
}
198+
_cmutex.unlock();
199+
}
200+
170201
int ESP8266Interface::connect()
171202
{
172203
nsapi_error_t status = _conn_status_to_error();
@@ -197,7 +228,18 @@ int ESP8266Interface::connect()
197228
return NSAPI_ERROR_DHCP_FAILURE;
198229
}
199230

200-
return _esp.connect(ap_ssid, ap_pass);
231+
_cmutex.lock();
232+
MBED_ASSERT(!_connect_event_id);
233+
234+
_connect_event_id = _global_event_queue->call(callback(this, &ESP8266Interface::_connect_async));
235+
236+
if (!_connect_event_id) {
237+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
238+
"connect(): unable to add event to queue");
239+
}
240+
_cmutex.unlock();
241+
242+
return NSAPI_ERROR_OK;
201243
}
202244

203245
int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
@@ -252,6 +294,12 @@ int ESP8266Interface::set_channel(uint8_t channel)
252294

253295
int ESP8266Interface::disconnect()
254296
{
297+
_cmutex.lock();
298+
if (_connect_event_id) {
299+
_global_event_queue->cancel(_connect_event_id);
300+
_connect_event_id = 0; // cancel asynchronous connection attempt if one is ongoing
301+
}
302+
_cmutex.unlock();
255303
_initialized = false;
256304

257305
nsapi_error_t status = _conn_status_to_error();

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 5 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/Mutex.h"
3233

3334
#define ESP8266_SOCKET_COUNT 5
3435

@@ -372,8 +373,12 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
372373
// Use global EventQueue
373374
events::EventQueue *_global_event_queue;
374375
int _oob_event_id;
376+
int _connect_event_id;
375377
void proc_oob_evnt();
376378
void _oob2global_event_queue();
379+
void _connect_async();
380+
rtos::Mutex _cmutex; // Protect asynchronous connection logic
381+
377382
};
378383
#endif
379384
#endif

0 commit comments

Comments
 (0)