37
37
38
38
// ESP8266Interface implementation
39
39
ESP8266Interface::ESP8266Interface (PinName tx, PinName rx, bool debug)
40
- : _esp(tx, rx, debug )
40
+ : _esp(tx, rx, callback( this , &ESP8266Interface::signal), false )
41
41
{
42
42
memset (_ids, 0 , sizeof (_ids));
43
43
memset (_cbs, 0 , sizeof (_cbs));
44
+ memset (_incoming_sockets, 0 , sizeof (_incoming_sockets));
44
45
45
46
_esp.attach (this , &ESP8266Interface::event);
46
47
}
@@ -59,19 +60,19 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
59
60
int ESP8266Interface::connect ()
60
61
{
61
62
_esp.setTimeout (ESP8266_CONNECT_TIMEOUT);
62
-
63
+
63
64
if (!_esp.reset ()) {
64
65
return NSAPI_ERROR_DEVICE_ERROR;
65
- }
66
-
66
+ }
67
+
67
68
_esp.setTimeout (ESP8266_MISC_TIMEOUT);
68
-
69
+
69
70
if (_esp.get_firmware_version () != ESP8266_VERSION) {
70
71
debug (" ESP8266: ERROR: Firmware incompatible with this driver.\
71
- \r\n Update to v%d - https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update\r\n " ,ESP8266_VERSION);
72
+ \r\n Update to v%d - https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update\r\n " ,ESP8266_VERSION);
72
73
return NSAPI_ERROR_DEVICE_ERROR;
73
74
}
74
-
75
+
75
76
_esp.setTimeout (ESP8266_CONNECT_TIMEOUT);
76
77
77
78
if (!_esp.startup (3 )) {
@@ -164,24 +165,24 @@ int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
164
165
{
165
166
// Look for an unused socket
166
167
int id = -1 ;
167
-
168
+
168
169
for (int i = 0 ; i < ESP8266_SOCKET_COUNT; i++) {
169
170
if (!_ids[i]) {
170
171
id = i;
171
172
_ids[i] = true ;
172
173
break ;
173
174
}
174
175
}
175
-
176
+
176
177
if (id == -1 ) {
177
178
return NSAPI_ERROR_NO_SOCKET;
178
179
}
179
-
180
+
180
181
struct esp8266_socket *socket = new struct esp8266_socket ;
181
182
if (!socket) {
182
183
return NSAPI_ERROR_NO_SOCKET;
183
184
}
184
-
185
+
185
186
socket->id = id;
186
187
socket->proto = proto;
187
188
socket->connected = false ;
@@ -194,7 +195,7 @@ int ESP8266Interface::socket_close(void *handle)
194
195
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
195
196
int err = 0 ;
196
197
_esp.setTimeout (ESP8266_MISC_TIMEOUT);
197
-
198
+
198
199
if (socket->connected && !_esp.close (socket->id )) {
199
200
err = NSAPI_ERROR_DEVICE_ERROR;
200
201
}
@@ -207,7 +208,7 @@ int ESP8266Interface::socket_close(void *handle)
207
208
208
209
int ESP8266Interface::socket_bind (void *handle, const SocketAddress &address)
209
210
{
210
- return NSAPI_ERROR_UNSUPPORTED ;
211
+ return _esp. bind (address) ;
211
212
}
212
213
213
214
int ESP8266Interface::socket_listen (void *handle, int backlog)
@@ -224,38 +225,62 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
224
225
if (!_esp.open (proto, socket->id , addr.get_ip_address (), addr.get_port ())) {
225
226
return NSAPI_ERROR_DEVICE_ERROR;
226
227
}
227
-
228
+
228
229
socket->connected = true ;
229
230
return 0 ;
230
231
}
231
-
232
+
232
233
int ESP8266Interface::socket_accept (void *server, void **socket, SocketAddress *addr)
233
234
{
234
- return NSAPI_ERROR_UNSUPPORTED;
235
+ printf (" socket_accept\n " );
236
+
237
+ while (1 ) {
238
+ // to get the OOB messages we need to send something to the ESP8266 so the ATParser has time to
239
+ // grab the packets. Send a bogus message to the module...
240
+ _esp.get_firmware_version ();
241
+
242
+ // now go through the _incoming_sockets array and see if there are unattached sockets...
243
+ for (size_t ix = 0 ; ix < ESP8266_SOCKET_COUNT; ix++) {
244
+ // printf("_incoming_sockets[ix] socket=%p connected=%d accepted=%d\n",
245
+ // _incoming_sockets[ix].socket, _incoming_sockets[ix].socket && _incoming_sockets[ix].socket->connected,
246
+ // _incoming_sockets[ix].accepted);
247
+ if (_incoming_sockets[ix].socket && _incoming_sockets[ix].socket ->connected && !_incoming_sockets[ix].accepted ) {
248
+ printf (" accepting socket %d\n " , ix);
249
+
250
+ *socket = _incoming_sockets[ix].socket ;
251
+ // addr is not used here I think...
252
+ _incoming_sockets[ix].accepted = true ;
253
+ return 0 ;
254
+ }
255
+ }
256
+
257
+ wait_ms (20 ); // socket_accept is blocking
258
+ }
259
+ return 0 ;
235
260
}
236
261
237
262
int ESP8266Interface::socket_send (void *handle, const void *data, unsigned size)
238
263
{
239
264
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
240
265
_esp.setTimeout (ESP8266_SEND_TIMEOUT);
241
-
266
+
242
267
if (!_esp.send (socket->id , data, size)) {
243
268
return NSAPI_ERROR_DEVICE_ERROR;
244
269
}
245
-
270
+
246
271
return size;
247
272
}
248
273
249
274
int ESP8266Interface::socket_recv (void *handle, void *data, unsigned size)
250
275
{
251
276
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
252
277
_esp.setTimeout (ESP8266_RECV_TIMEOUT);
253
-
278
+
254
279
int32_t recv = _esp.recv (socket->id , data, size);
255
280
if (recv < 0 ) {
256
281
return NSAPI_ERROR_WOULD_BLOCK;
257
282
}
258
-
283
+
259
284
return recv;
260
285
}
261
286
@@ -278,7 +303,7 @@ int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, con
278
303
}
279
304
socket->addr = addr;
280
305
}
281
-
306
+
282
307
return socket_send (socket, data, size);
283
308
}
284
309
@@ -295,7 +320,7 @@ int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *d
295
320
296
321
void ESP8266Interface::socket_attach (void *handle, void (*callback)(void *), void *data)
297
322
{
298
- struct esp8266_socket *socket = (struct esp8266_socket *)handle;
323
+ struct esp8266_socket *socket = (struct esp8266_socket *)handle;
299
324
_cbs[socket->id ].callback = callback;
300
325
_cbs[socket->id ].data = data;
301
326
}
@@ -307,3 +332,45 @@ void ESP8266Interface::event() {
307
332
}
308
333
}
309
334
}
335
+
336
+ void ESP8266Interface::signal (SignalingAction action, int socket_id) {
337
+ printf (" ESP8266Interface::signal %d %d\n " , action, socket_id);
338
+
339
+ if (action == ESP8266_SOCKET_CONNECT) {
340
+ if (_ids[socket_id]) {
341
+ // this should not be possible...
342
+ printf (" ESP8266_SOCKET_CONNECT for socket that already exists...\n " );
343
+ // return;
344
+ }
345
+
346
+ struct esp8266_socket *socket = new struct esp8266_socket ;
347
+ if (!socket) {
348
+ printf (" ESP8266_SOCKET_CONNECT could not create socket\n " );
349
+ return ;
350
+ }
351
+
352
+ _ids[socket_id] = true ;
353
+
354
+ socket->id = socket_id;
355
+ socket->proto = NSAPI_TCP;
356
+ socket->connected = true ;
357
+
358
+ _incoming_sockets[socket_id].socket = socket;
359
+ _incoming_sockets[socket_id].accepted = false ;
360
+ }
361
+ else if (action == ESP8266_SOCKET_CLOSE) {
362
+ // Q: should we be able to delete the socket here? probably segfaults if held in user code
363
+ struct esp8266_socket *socket = _incoming_sockets[socket_id].socket ;
364
+ if (!socket || !socket->connected ) {
365
+ printf (" ESP8266_SOCKET_CLOSE for socket that does not exist or is already closed\n " );
366
+ return ;
367
+ }
368
+
369
+ socket->connected = false ;
370
+ _incoming_sockets[socket_id].accepted = false ;
371
+ _incoming_sockets[socket_id].socket = NULL ;
372
+ _ids[socket_id] = false ;
373
+
374
+ // delete socket?
375
+ }
376
+ }
0 commit comments