Skip to content

Commit 605c383

Browse files
author
Veijo Pesonen
committed
Sanitizes internal bookkeeping of sockets
1 parent 3bb5207 commit 605c383

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

ESP8266/ESP8266.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
6161
_parser.oob("ALREADY CONNECTED", callback(this, &ESP8266::_oob_cipstart_already_connected));
6262

6363
for(int i= 0; i < SOCKET_COUNT; i++) {
64-
_socket_open[i].id = -1;
65-
_socket_open[i].proto = NSAPI_UDP;
64+
_sinfo[i].open = false;
65+
_sinfo[i].proto = NSAPI_UDP;
6666
}
6767
}
6868

@@ -398,7 +398,7 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
398398
static const char *type = "UDP";
399399
bool done = false;
400400

401-
if (id >= SOCKET_COUNT || _socket_open[id].id == id) {
401+
if (id >= SOCKET_COUNT || _sinfo[id].open) {
402402
return NSAPI_ERROR_PARAMETER;
403403
}
404404

@@ -423,8 +423,8 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
423423
}
424424
continue;
425425
}
426-
_socket_open[id].id = id;
427-
_socket_open[id].proto = NSAPI_UDP;
426+
_sinfo[id].open = true;
427+
_sinfo[id].proto = NSAPI_UDP;
428428
break;
429429
}
430430
}
@@ -440,7 +440,7 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
440440
static const char *type = "TCP";
441441
bool done = false;
442442

443-
if (id >= SOCKET_COUNT || _socket_open[id].id == id) {
443+
if (id >= SOCKET_COUNT || _sinfo[id].open) {
444444
return NSAPI_ERROR_PARAMETER;
445445
}
446446

@@ -465,8 +465,8 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
465465
}
466466
continue;
467467
}
468-
_socket_open[id].id = id;
469-
_socket_open[id].proto = NSAPI_TCP;
468+
_sinfo[id].open = true;
469+
_sinfo[id].proto = NSAPI_TCP;
470470
break;
471471
}
472472
}
@@ -521,8 +521,8 @@ void ESP8266::_packet_handler()
521521
}
522522
// In passive mode amount not used...
523523
if(_tcp_passive
524-
&& _socket_open[id].id == id
525-
&& _socket_open[id].proto == NSAPI_TCP) {
524+
&& _sinfo[id].open == true
525+
&& _sinfo[id].proto == NSAPI_TCP) {
526526
if (!_parser.recv("%d\n", &amount)) {
527527
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENODATA), \
528528
"ESP8266::_packet_handler(): Data length missing");
@@ -592,7 +592,7 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
592592
}
593593

594594
// Socket closed, doesn't mean there couldn't be data left
595-
if (_socket_open[id].id != id) {
595+
if (!_sinfo[id].open) {
596596
done = _parser.send("AT+CIPRECVDATA=%d,%lu", id, amount)
597597
&& _parser.recv("+CIPRECVDATA,%ld:", &len)
598598
&& _parser.read((char*)data, len)
@@ -649,7 +649,7 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
649649
}
650650
}
651651
}
652-
if(_socket_open[id].id < 0) {
652+
if(!_sinfo[id].open) {
653653
_smutex.unlock();
654654
return 0;
655655
}
@@ -738,7 +738,7 @@ bool ESP8266::close(int id)
738738
if (!_parser.recv("OK\n")) {
739739
if (_closed) { // UNLINK ERROR
740740
_closed = false;
741-
_socket_open[id].id = -1;
741+
_sinfo[id].open = false;
742742
_clear_socket_packets(id);
743743
_smutex.unlock();
744744
// ESP8266 has a habit that it might close a socket on its own.
@@ -829,27 +829,27 @@ void ESP8266::_oob_socket_close_error()
829829

830830
void ESP8266::_oob_socket0_closed_handler()
831831
{
832-
_socket_open[0].id = -1;
832+
_sinfo[0].open = false;
833833
}
834834

835835
void ESP8266::_oob_socket1_closed_handler()
836836
{
837-
_socket_open[1].id = -1;
837+
_sinfo[1].open = false;
838838
}
839839

840840
void ESP8266::_oob_socket2_closed_handler()
841841
{
842-
_socket_open[2].id = -1;
842+
_sinfo[2].open = false;
843843
}
844844

845845
void ESP8266::_oob_socket3_closed_handler()
846846
{
847-
_socket_open[3].id = -1;
847+
_sinfo[3].open = false;
848848
}
849849

850850
void ESP8266::_oob_socket4_closed_handler()
851851
{
852-
_socket_open[4].id = -1;
852+
_sinfo[4].open = false;
853853
}
854854

855855
void ESP8266::_connection_status_handler()

ESP8266/ESP8266.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,15 @@ class ESP8266
380380
char _netmask_buffer[16];
381381
char _mac_buffer[18];
382382
struct _sock_info {
383-
int id;
383+
bool open;
384384
nsapi_protocol_t proto;
385385
};
386386

387387
int _connect_error;
388388
bool _fail;
389389
bool _sock_already;
390390
bool _closed;
391-
struct _sock_info _socket_open[SOCKET_COUNT];
391+
struct _sock_info _sinfo[SOCKET_COUNT];
392392
nsapi_connection_status_t _connection_status;
393393
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
394394
size_t _heap_usage;

0 commit comments

Comments
 (0)