@@ -59,7 +59,8 @@ ESP8266Interface::ESP8266Interface()
59
59
_conn_stat(NSAPI_STATUS_DISCONNECTED),
60
60
_conn_stat_cb(NULL ),
61
61
_global_event_queue(NULL ),
62
- _oob_event_id(0 )
62
+ _oob_event_id(0 ),
63
+ _connect_event_id(0 )
63
64
{
64
65
memset (_cbs, 0 , sizeof (_cbs));
65
66
memset (ap_ssid, 0 , sizeof (ap_ssid));
@@ -87,7 +88,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
87
88
_conn_stat(NSAPI_STATUS_DISCONNECTED),
88
89
_conn_stat_cb(NULL ),
89
90
_global_event_queue(NULL ),
90
- _oob_event_id(0 )
91
+ _oob_event_id(0 ),
92
+ _connect_event_id(0 )
91
93
{
92
94
memset (_cbs, 0 , sizeof (_cbs));
93
95
memset (ap_ssid, 0 , sizeof (ap_ssid));
@@ -111,6 +113,12 @@ ESP8266Interface::~ESP8266Interface()
111
113
_global_event_queue->cancel (_oob_event_id);
112
114
}
113
115
116
+ _cmutex.lock ();
117
+ if (_connect_event_id) {
118
+ _global_event_queue->cancel (_connect_event_id);
119
+ }
120
+ _cmutex.unlock ();
121
+
114
122
// Power down the modem
115
123
_rst_pin.rst_assert ();
116
124
}
@@ -167,6 +175,29 @@ void ESP8266Interface::_oob2global_event_queue()
167
175
}
168
176
}
169
177
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
+
170
201
int ESP8266Interface::connect ()
171
202
{
172
203
nsapi_error_t status = _conn_status_to_error ();
@@ -197,7 +228,18 @@ int ESP8266Interface::connect()
197
228
return NSAPI_ERROR_DHCP_FAILURE;
198
229
}
199
230
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;
201
243
}
202
244
203
245
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)
252
294
253
295
int ESP8266Interface::disconnect ()
254
296
{
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 ();
255
303
_initialized = false ;
256
304
257
305
nsapi_error_t status = _conn_status_to_error ();
0 commit comments