Skip to content

Commit 5a88ff9

Browse files
author
Mika Leppänen
committed
ESP8266 now clears socket buffer packets on reset and close
After a board where the ESP8266 was connected to was resetted, the ESP8266 send incoming packets from previous session to the board until reset AT command (AT+RST) was handled. Added clearing of the socket buffer after reset command. Added clearing of packets for a socket during socket open() and close(). This ensured that when socket ID is reused, packets from previous session are not in the socket buffer.
1 parent 32148e3 commit 5a88ff9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ESP8266/ESP8266.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cstring>
2222

2323
#define ESP8266_DEFAULT_BAUD_RATE 115200
24+
#define ESP8266_ALL_SOCKET_IDS -1
2425

2526
ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
2627
: _serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
@@ -97,6 +98,7 @@ bool ESP8266::reset(void)
9798
if (_parser.send("AT+RST")
9899
&& _parser.recv("OK\n")
99100
&& _parser.recv("ready")) {
101+
_clear_socket_packets(ESP8266_ALL_SOCKET_IDS);
100102
_smutex.unlock();
101103
return true;
102104
}
@@ -305,6 +307,8 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
305307
_socket_open[id] = 1;
306308
}
307309

310+
_clear_socket_packets(id);
311+
308312
_smutex.unlock();
309313

310314
return done ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
@@ -332,6 +336,8 @@ bool ESP8266::open_tcp(int id, const char* addr, int port, int keepalive)
332336
_socket_open[id] = 1;
333337
}
334338

339+
_clear_socket_packets(id);
340+
335341
_smutex.unlock();
336342

337343
return done;
@@ -480,6 +486,27 @@ int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount, uint32_t timeout)
480486
return NSAPI_ERROR_WOULD_BLOCK;
481487
}
482488

489+
void ESP8266::_clear_socket_packets(int id)
490+
{
491+
struct packet **p = &_packets;
492+
493+
while (*p) {
494+
if ((*p)->id == id || id == ESP8266_ALL_SOCKET_IDS) {
495+
struct packet *q = *p;
496+
497+
if (_packets_end == &(*p)->next) {
498+
_packets_end = p; // Set last packet next field/_packets
499+
}
500+
*p = (*p)->next;
501+
502+
free(q);
503+
} else {
504+
// Point to last packet next field
505+
p = &(*p)->next;
506+
}
507+
}
508+
}
509+
483510
bool ESP8266::close(int id)
484511
{
485512
//May take a second try if device is busy
@@ -490,12 +517,14 @@ bool ESP8266::close(int id)
490517
if (_closed) { // UNLINK ERROR
491518
_closed = false;
492519
_socket_open[id] = 0;
520+
_clear_socket_packets(id);
493521
_smutex.unlock();
494522
// ESP8266 has a habit that it might close a socket on its own.
495523
//debug("ESP8266: socket %d already closed when calling close\n", id);
496524
return true;
497525
}
498526
} else {
527+
_clear_socket_packets(id);
499528
_smutex.unlock();
500529
return true;
501530
}

ESP8266/ESP8266.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class ESP8266
291291
void _oob_socket4_closed_handler();
292292
void _connection_status_handler();
293293
void _oob_socket_close_error();
294+
void _clear_socket_packets(int id);
294295

295296
char _ip_buffer[16];
296297
char _gateway_buffer[16];

0 commit comments

Comments
 (0)