@@ -37,6 +37,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
37
37
_at_v(-1 ,-1 ,-1 ),
38
38
_connect_error(0 ),
39
39
_fail(false ),
40
+ _sock_already(false ),
40
41
_closed(false ),
41
42
_connection_status(NSAPI_STATUS_DISCONNECTED),
42
43
_heap_usage(0 )
@@ -57,6 +58,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
57
58
_parser.oob (" 4,CLOSED" , callback (this , &ESP8266::_oob_socket4_closed_handler));
58
59
_parser.oob (" WIFI " , callback (this , &ESP8266::_connection_status_handler));
59
60
_parser.oob (" UNLINK" , callback (this , &ESP8266::_oob_socket_close_error));
61
+ _parser.oob (" ALREADY CONNECTED" , callback (this , &ESP8266::_oob_cipstart_already_connected));
60
62
61
63
for (int i= 0 ; i < SOCKET_COUNT; i++) {
62
64
_socket_open[i].id = -1 ;
@@ -396,61 +398,68 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
396
398
static const char *type = " UDP" ;
397
399
bool done = false ;
398
400
399
- if (id >= SOCKET_COUNT) {
401
+ if (id >= SOCKET_COUNT || _socket_open[id]. id == id ) {
400
402
return NSAPI_ERROR_PARAMETER;
401
- } else if (_socket_open[id].id == id) {
402
- return NSAPI_ERROR_IS_CONNECTED;
403
403
}
404
404
405
405
_smutex.lock ();
406
406
if (local_port) {
407
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, local_port)
408
- && _parser.recv (" OK\n " );
407
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, local_port);
409
408
} else {
410
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port)
411
- && _parser.recv (" OK\n " );
409
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port);
412
410
}
413
411
414
412
if (done) {
413
+ if (!_parser.recv (" OK\n " )) {
414
+ if (_sock_already) {
415
+ _sock_already = false ; // To be raised again by OOB msg
416
+ _smutex.unlock ();
417
+ return NSAPI_ERROR_IS_CONNECTED;
418
+ }
419
+ }
420
+
415
421
_socket_open[id].id = id;
416
422
_socket_open[id].proto = NSAPI_UDP;
417
- }
418
-
419
- _clear_socket_packets (id);
420
423
424
+ _clear_socket_packets (id);
425
+ }
421
426
_smutex.unlock ();
422
427
423
428
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
424
429
}
425
430
426
- bool ESP8266::open_tcp (int id, const char * addr, int port, int keepalive)
431
+ nsapi_error_t ESP8266::open_tcp (int id, const char * addr, int port, int keepalive)
427
432
{
428
433
static const char *type = " TCP" ;
429
434
bool done = false ;
430
435
431
436
if (id >= SOCKET_COUNT || _socket_open[id].id == id) {
432
- return false ;
437
+ return NSAPI_ERROR_PARAMETER ;
433
438
}
434
439
435
440
_smutex.lock ();
436
441
if (keepalive) {
437
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, keepalive)
438
- && _parser.recv (" OK\n " );
442
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, keepalive);
439
443
} else {
440
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port)
441
- && _parser.recv (" OK\n " );
444
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port);
442
445
}
443
446
444
447
if (done) {
448
+ if (!_parser.recv (" OK\n " )) {
449
+ if (_sock_already) {
450
+ _sock_already = false ; // To be raised again by OOB msg
451
+ _smutex.unlock ();
452
+ return NSAPI_ERROR_IS_CONNECTED;
453
+ }
454
+ }
445
455
_socket_open[id].id = id;
446
456
_socket_open[id].proto = NSAPI_TCP;
447
- }
448
-
449
- _clear_socket_packets (id);
450
457
458
+ _clear_socket_packets (id);
459
+ }
451
460
_smutex.unlock ();
452
461
453
- return done;
462
+ return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR ;
454
463
}
455
464
456
465
bool ESP8266::dns_lookup (const char * name, char * ip)
@@ -787,6 +796,13 @@ void ESP8266::_connect_error_handler()
787
796
}
788
797
}
789
798
799
+
800
+ void ESP8266::_oob_cipstart_already_connected ()
801
+ {
802
+ _sock_already = true ;
803
+ _parser.abort ();
804
+ }
805
+
790
806
void ESP8266::_oob_socket_close_error ()
791
807
{
792
808
if (_parser.recv (" ERROR\n " )) {
0 commit comments