Skip to content

Commit 1dcf4b4

Browse files
author
Veijo Pesonen
committed
Re-establishes socket on ESP8266 side if already unexpectedly open
1 parent 8c0814f commit 1dcf4b4

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

ESP8266/ESP8266.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -403,26 +403,33 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
403403
}
404404

405405
_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-
}
411406

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);
419412
}
420413

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+
}
425430
}
431+
_clear_socket_packets(id);
432+
426433
_smutex.unlock();
427434

428435
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
438445
}
439446

440447
_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-
}
446448

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);
454454
}
455-
_socket_open[id].id = id;
456-
_socket_open[id].proto = NSAPI_TCP;
457455

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+
}
459472
}
473+
_clear_socket_packets(id);
474+
460475
_smutex.unlock();
461476

462477
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;

0 commit comments

Comments
 (0)