@@ -403,26 +403,33 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
403
403
}
404
404
405
405
_smutex.lock ();
406
- if (local_port) {
407
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, local_port);
408
- } else {
409
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port);
410
- }
411
406
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
- }
407
+ for (int i = 0 ; i < 2 ; i++) {
408
+ if (local_port) {
409
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, local_port);
410
+ } else {
411
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port);
419
412
}
420
413
421
- _socket_open[id].id = id;
422
- _socket_open[id].proto = NSAPI_UDP;
423
-
424
- _clear_socket_packets (id);
414
+ if (done) {
415
+ if (!_parser.recv (" OK\n " )) {
416
+ if (_sock_already) {
417
+ _sock_already = false ; // To be raised again by OOB msg
418
+ done = close (id);
419
+ if (!done) {
420
+ MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CLOSE_FAILED), \
421
+ " ESP8266::_open_udp: device refused to close socket" );
422
+ }
423
+ }
424
+ continue ;
425
+ }
426
+ _socket_open[id].id = id;
427
+ _socket_open[id].proto = NSAPI_UDP;
428
+ break ;
429
+ }
425
430
}
431
+ _clear_socket_packets (id);
432
+
426
433
_smutex.unlock ();
427
434
428
435
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
@@ -438,25 +445,33 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
438
445
}
439
446
440
447
_smutex.lock ();
441
- if (keepalive) {
442
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, keepalive);
443
- } else {
444
- done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port);
445
- }
446
448
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
- }
449
+ for (int i = 0 ; i < 2 ; i++) {
450
+ if (keepalive) {
451
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d,%d" , id, type, addr, port, keepalive);
452
+ } else {
453
+ done = _parser.send (" AT+CIPSTART=%d,\" %s\" ,\" %s\" ,%d" , id, type, addr, port);
454
454
}
455
- _socket_open[id].id = id;
456
- _socket_open[id].proto = NSAPI_TCP;
457
455
458
- _clear_socket_packets (id);
456
+ if (done) {
457
+ if (!_parser.recv (" OK\n " )) {
458
+ if (_sock_already) {
459
+ _sock_already = false ; // To be raised again by OOB msg
460
+ done = close (id);
461
+ if (!done) {
462
+ MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CLOSE_FAILED), \
463
+ " ESP8266::_open_tcp: device refused to close socket" );
464
+ }
465
+ }
466
+ continue ;
467
+ }
468
+ _socket_open[id].id = id;
469
+ _socket_open[id].proto = NSAPI_TCP;
470
+ break ;
471
+ }
459
472
}
473
+ _clear_socket_packets (id);
474
+
460
475
_smutex.unlock ();
461
476
462
477
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
0 commit comments