Skip to content

Commit 8c0814f

Browse files
author
Veijo Pesonen
committed
Adds check for already opened socket
1 parent c753591 commit 8c0814f

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

ESP8266/ESP8266.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
3737
_at_v(-1,-1,-1),
3838
_connect_error(0),
3939
_fail(false),
40+
_sock_already(false),
4041
_closed(false),
4142
_connection_status(NSAPI_STATUS_DISCONNECTED),
4243
_heap_usage(0)
@@ -57,6 +58,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
5758
_parser.oob("4,CLOSED", callback(this, &ESP8266::_oob_socket4_closed_handler));
5859
_parser.oob("WIFI ", callback(this, &ESP8266::_connection_status_handler));
5960
_parser.oob("UNLINK", callback(this, &ESP8266::_oob_socket_close_error));
61+
_parser.oob("ALREADY CONNECTED", callback(this, &ESP8266::_oob_cipstart_already_connected));
6062

6163
for(int i= 0; i < SOCKET_COUNT; i++) {
6264
_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
396398
static const char *type = "UDP";
397399
bool done = false;
398400

399-
if (id >= SOCKET_COUNT) {
401+
if (id >= SOCKET_COUNT || _socket_open[id].id == id) {
400402
return NSAPI_ERROR_PARAMETER;
401-
} else if (_socket_open[id].id == id) {
402-
return NSAPI_ERROR_IS_CONNECTED;
403403
}
404404

405405
_smutex.lock();
406406
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);
409408
} 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);
412410
}
413411

414412
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+
415421
_socket_open[id].id = id;
416422
_socket_open[id].proto = NSAPI_UDP;
417-
}
418-
419-
_clear_socket_packets(id);
420423

424+
_clear_socket_packets(id);
425+
}
421426
_smutex.unlock();
422427

423428
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
424429
}
425430

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)
427432
{
428433
static const char *type = "TCP";
429434
bool done = false;
430435

431436
if (id >= SOCKET_COUNT || _socket_open[id].id == id) {
432-
return false;
437+
return NSAPI_ERROR_PARAMETER;
433438
}
434439

435440
_smutex.lock();
436441
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);
439443
} 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);
442445
}
443446

444447
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+
}
445455
_socket_open[id].id = id;
446456
_socket_open[id].proto = NSAPI_TCP;
447-
}
448-
449-
_clear_socket_packets(id);
450457

458+
_clear_socket_packets(id);
459+
}
451460
_smutex.unlock();
452461

453-
return done;
462+
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
454463
}
455464

456465
bool ESP8266::dns_lookup(const char* name, char* ip)
@@ -787,6 +796,13 @@ void ESP8266::_connect_error_handler()
787796
}
788797
}
789798

799+
800+
void ESP8266::_oob_cipstart_already_connected()
801+
{
802+
_sock_already = true;
803+
_parser.abort();
804+
}
805+
790806
void ESP8266::_oob_socket_close_error()
791807
{
792808
if (_parser.recv("ERROR\n")) {

ESP8266/ESP8266.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class ESP8266
132132
*
133133
* @param ap the name of the AP
134134
* @param passPhrase the password of AP
135-
* @return NSAPI_ERROR_OK only if ESP8266 is connected successfully
135+
* @return NSAPI_ERROR_OK in success, negative error code in failure
136136
*/
137137
nsapi_error_t connect(const char *ap, const char *passPhrase);
138138

@@ -203,7 +203,7 @@ class ESP8266
203203
* @param addr the IP address of the destination
204204
* @param port the port on the destination
205205
* @param local_port UDP socket's local port, zero means any
206-
* @return true only if socket opened successfully
206+
* @return NSAPI_ERROR_OK in success, negative error code in failure
207207
*/
208208
nsapi_error_t open_udp(int id, const char* addr, int port, int local_port = 0);
209209

@@ -216,9 +216,9 @@ class ESP8266
216216
* @param addr the IP address of the destination
217217
* @param port the port on the destination
218218
* @param tcp_keepalive TCP connection's keep alive time, zero means disabled
219-
* @return true only if socket opened successfully
219+
* @return NSAPI_ERROR_OK in success, negative error code in failure
220220
*/
221-
bool open_tcp(int id, const char* addr, int port, int keepalive = 0);
221+
nsapi_error_t open_tcp(int id, const char* addr, int port, int keepalive = 0);
222222

223223
/**
224224
* Sends data to an open socket
@@ -362,6 +362,7 @@ class ESP8266
362362
struct fw_at_version _at_v;
363363
void _packet_handler();
364364
void _connect_error_handler();
365+
void _oob_cipstart_already_connected();
365366
bool recv_ap(nsapi_wifi_ap_t *ap);
366367
void _oob_socket0_closed_handler();
367368
void _oob_socket1_closed_handler();
@@ -385,6 +386,7 @@ class ESP8266
385386

386387
int _connect_error;
387388
bool _fail;
389+
bool _sock_already;
388390
bool _closed;
389391
struct _sock_info _socket_open[SOCKET_COUNT];
390392
nsapi_connection_status_t _connection_status;

ESP8266Interface.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,17 +409,13 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
409409

410410
if (socket->proto == NSAPI_UDP) {
411411
ret = _esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _local_ports[socket->id]);
412-
if (ret != NSAPI_ERROR_OK) {
413-
return ret;
414-
}
415412
} else {
416-
if (!_esp.open_tcp(socket->id, addr.get_ip_address(), addr.get_port(), socket->keepalive)) {
417-
return NSAPI_ERROR_DEVICE_ERROR;
418-
}
413+
ret = _esp.open_tcp(socket->id, addr.get_ip_address(), addr.get_port(), socket->keepalive);
419414
}
420415

421-
socket->connected = true;
422-
return 0;
416+
socket->connected = (ret == NSAPI_ERROR_OK) ? true : false;
417+
418+
return ret;
423419
}
424420

425421
int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr)

0 commit comments

Comments
 (0)