Skip to content

Commit 1656fed

Browse files
authored
Merge pull request me-no-dev#32 from me-no-dev/idf-update
Idf update
2 parents 952b7eb + 0dfd5b6 commit 1656fed

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

src/AsyncTCP.cpp

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static void _tcp_error(void * arg, int8_t err) {
256256
#include "lwip/priv/tcpip_priv.h"
257257

258258
typedef struct {
259-
struct tcpip_api_call call;
259+
struct tcpip_api_call_data call;
260260
tcp_pcb * pcb;
261261
int8_t err;
262262
union {
@@ -279,20 +279,24 @@ typedef struct {
279279
};
280280
} tcp_api_call_t;
281281

282-
static err_t _tcp_output_api(struct tcpip_api_call *api_call_msg){
282+
static err_t _tcp_output_api(struct tcpip_api_call_data *api_call_msg){
283283
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
284-
msg->err = tcp_output(msg->pcb);
284+
if(msg->pcb){
285+
msg->err = tcp_output(msg->pcb);
286+
} else {
287+
msg->err = 0;
288+
}
285289
return msg->err;
286290
}
287291

288292
static esp_err_t _tcp_output(tcp_pcb * pcb) {
289293
tcp_api_call_t msg;
290294
msg.pcb = pcb;
291-
tcpip_api_call(_tcp_output_api, (struct tcpip_api_call*)&msg);
295+
tcpip_api_call(_tcp_output_api, (struct tcpip_api_call_data*)&msg);
292296
return msg.err;
293297
}
294298

295-
static err_t _tcp_write_api(struct tcpip_api_call *api_call_msg){
299+
static err_t _tcp_write_api(struct tcpip_api_call_data *api_call_msg){
296300
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
297301
msg->err = tcp_write(msg->pcb, msg->write.data, msg->write.size, msg->write.apiflags);
298302
return msg->err;
@@ -304,11 +308,11 @@ static esp_err_t _tcp_write(tcp_pcb * pcb, const char* data, size_t size, uint8_
304308
msg.write.data = data;
305309
msg.write.size = size;
306310
msg.write.apiflags = apiflags;
307-
tcpip_api_call(_tcp_write_api, (struct tcpip_api_call*)&msg);
311+
tcpip_api_call(_tcp_write_api, (struct tcpip_api_call_data*)&msg);
308312
return msg.err;
309313
}
310314

311-
static err_t _tcp_recved_api(struct tcpip_api_call *api_call_msg){
315+
static err_t _tcp_recved_api(struct tcpip_api_call_data *api_call_msg){
312316
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
313317
msg->err = 0;
314318
tcp_recved(msg->pcb, msg->received);
@@ -319,11 +323,11 @@ static esp_err_t _tcp_recved(tcp_pcb * pcb, size_t len) {
319323
tcp_api_call_t msg;
320324
msg.pcb = pcb;
321325
msg.received = len;
322-
tcpip_api_call(_tcp_recved_api, (struct tcpip_api_call*)&msg);
326+
tcpip_api_call(_tcp_recved_api, (struct tcpip_api_call_data*)&msg);
323327
return msg.err;
324328
}
325329

326-
static err_t _tcp_connect_api(struct tcpip_api_call *api_call_msg){
330+
static err_t _tcp_connect_api(struct tcpip_api_call_data *api_call_msg){
327331
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
328332
msg->err = tcp_connect(msg->pcb, msg->connect.addr, msg->connect.port, msg->connect.cb);
329333
return msg->err;
@@ -335,11 +339,11 @@ static esp_err_t _tcp_connect(tcp_pcb * pcb, ip_addr_t * addr, uint16_t port, tc
335339
msg.connect.addr = addr;
336340
msg.connect.port = port;
337341
msg.connect.cb = cb;
338-
tcpip_api_call(_tcp_connect_api, (struct tcpip_api_call*)&msg);
342+
tcpip_api_call(_tcp_connect_api, (struct tcpip_api_call_data*)&msg);
339343
return msg.err;
340344
}
341345

342-
static err_t _tcp_close_api(struct tcpip_api_call *api_call_msg){
346+
static err_t _tcp_close_api(struct tcpip_api_call_data *api_call_msg){
343347
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
344348
msg->err = tcp_close(msg->pcb);
345349
return msg->err;
@@ -349,11 +353,11 @@ static esp_err_t _tcp_close(tcp_pcb * pcb) {
349353
tcp_api_call_t msg;
350354
msg.pcb = pcb;
351355
//ets_printf("close 0x%08x\n", (uint32_t)pcb);
352-
tcpip_api_call(_tcp_close_api, (struct tcpip_api_call*)&msg);
356+
tcpip_api_call(_tcp_close_api, (struct tcpip_api_call_data*)&msg);
353357
return msg.err;
354358
}
355359

356-
static err_t _tcp_abort_api(struct tcpip_api_call *api_call_msg){
360+
static err_t _tcp_abort_api(struct tcpip_api_call_data *api_call_msg){
357361
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
358362
msg->err = 0;
359363
tcp_abort(msg->pcb);
@@ -364,11 +368,11 @@ static esp_err_t _tcp_abort(tcp_pcb * pcb) {
364368
tcp_api_call_t msg;
365369
msg.pcb = pcb;
366370
//ets_printf("abort 0x%08x\n", (uint32_t)pcb);
367-
tcpip_api_call(_tcp_abort_api, (struct tcpip_api_call*)&msg);
371+
tcpip_api_call(_tcp_abort_api, (struct tcpip_api_call_data*)&msg);
368372
return msg.err;
369373
}
370374

371-
static err_t _tcp_bind_api(struct tcpip_api_call *api_call_msg){
375+
static err_t _tcp_bind_api(struct tcpip_api_call_data *api_call_msg){
372376
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
373377
msg->err = tcp_bind(msg->pcb, msg->bind.addr, msg->bind.port);
374378
return msg->err;
@@ -379,11 +383,11 @@ static esp_err_t _tcp_bind(tcp_pcb * pcb, ip_addr_t * addr, uint16_t port) {
379383
msg.pcb = pcb;
380384
msg.bind.addr = addr;
381385
msg.bind.port = port;
382-
tcpip_api_call(_tcp_bind_api, (struct tcpip_api_call*)&msg);
386+
tcpip_api_call(_tcp_bind_api, (struct tcpip_api_call_data*)&msg);
383387
return msg.err;
384388
}
385389

386-
static err_t _tcp_listen_api(struct tcpip_api_call *api_call_msg){
390+
static err_t _tcp_listen_api(struct tcpip_api_call_data *api_call_msg){
387391
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
388392
msg->err = 0;
389393
msg->pcb = tcp_listen_with_backlog(msg->pcb, msg->backlog);
@@ -394,7 +398,7 @@ static tcp_pcb * _tcp_listen_with_backlog(tcp_pcb * pcb, uint8_t backlog) {
394398
tcp_api_call_t msg;
395399
msg.pcb = pcb;
396400
msg.backlog = backlog?backlog:0xFF;
397-
tcpip_api_call(_tcp_listen_api, (struct tcpip_api_call*)&msg);
401+
tcpip_api_call(_tcp_listen_api, (struct tcpip_api_call_data*)&msg);
398402
return msg.pcb;
399403
}
400404
#define _tcp_listen(p) _tcp_listen_with_backlog(p, 0xFF);
@@ -432,6 +436,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
432436
, next(NULL)
433437
, _in_lwip_thread(false)
434438
{
439+
//ets_printf("+: 0x%08x\n", (uint32_t)this);
440+
435441
_pcb = pcb;
436442
if(_pcb){
437443
_rx_last_packet = millis();
@@ -447,6 +453,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
447453
AsyncClient::~AsyncClient(){
448454
if(_pcb)
449455
_close();
456+
457+
//ets_printf("-: 0x%08x\n", (uint32_t)this);
450458
}
451459

452460
bool AsyncClient::connect(IPAddress ip, uint16_t port){
@@ -512,6 +520,7 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
512520
}
513521

514522
int8_t AsyncClient::_close(){
523+
//ets_printf("X: 0x%08x\n", (uint32_t)this);
515524
int8_t err = ERR_OK;
516525
if(_pcb) {
517526
//log_i("");
@@ -520,6 +529,7 @@ int8_t AsyncClient::_close(){
520529
tcp_recv(_pcb, NULL);
521530
tcp_err(_pcb, NULL);
522531
tcp_poll(_pcb, NULL, 0);
532+
_tcp_clear_events(this);
523533
if(_in_lwip_thread){
524534
err = tcp_close(_pcb);
525535
} else {
@@ -529,7 +539,6 @@ int8_t AsyncClient::_close(){
529539
err = abort();
530540
}
531541
_pcb = NULL;
532-
_tcp_clear_events(this);
533542
if(_discard_cb)
534543
_discard_cb(_discard_cb_arg, this);
535544
}
@@ -562,6 +571,13 @@ int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
562571
}
563572

564573
int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
574+
if(!_pcb || pcb != _pcb){
575+
log_e("0x%08x != 0x%08x", (uint32_t)pcb, (uint32_t)_pcb);
576+
if(pb){
577+
pbuf_free(pb);
578+
}
579+
return ERR_OK;
580+
}
565581
_in_lwip_thread = false;
566582
if(pb == NULL){
567583
return _close();
@@ -621,7 +637,7 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
621637
return ERR_OK;
622638
}
623639

624-
void AsyncClient::_dns_found(ip_addr_t *ipaddr){
640+
void AsyncClient::_dns_found(struct ip_addr *ipaddr){
625641
_in_lwip_thread = true;
626642
if(ipaddr){
627643
connect(IPAddress(ipaddr->u_addr.ip4.addr), _connect_port);
@@ -943,31 +959,58 @@ void AsyncClient::onPoll(AcConnectHandler cb, void* arg){
943959
}
944960

945961

946-
void AsyncClient::_s_dns_found(const char * name, ip_addr_t * ipaddr, void * arg){
947-
reinterpret_cast<AsyncClient*>(arg)->_dns_found(ipaddr);
962+
void AsyncClient::_s_dns_found(const char * name, struct ip_addr * ipaddr, void * arg){
963+
if(arg){
964+
reinterpret_cast<AsyncClient*>(arg)->_dns_found(ipaddr);
965+
} else {
966+
log_e("Bad Arg: 0x%08x", arg);
967+
}
948968
}
949969

950970
int8_t AsyncClient::_s_poll(void * arg, struct tcp_pcb * pcb) {
951-
reinterpret_cast<AsyncClient*>(arg)->_poll(pcb);
971+
if(arg && pcb){
972+
reinterpret_cast<AsyncClient*>(arg)->_poll(pcb);
973+
} else {
974+
log_e("Bad Args: 0x%08x 0x%08x", arg, pcb);
975+
}
952976
return ERR_OK;
953977
}
954978

955979
int8_t AsyncClient::_s_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_t err) {
956-
reinterpret_cast<AsyncClient*>(arg)->_recv(pcb, pb, err);
980+
if(arg && pcb){
981+
reinterpret_cast<AsyncClient*>(arg)->_recv(pcb, pb, err);
982+
} else {
983+
if(pb){
984+
pbuf_free(pb);
985+
}
986+
log_e("Bad Args: 0x%08x 0x%08x", arg, pcb);
987+
}
957988
return ERR_OK;
958989
}
959990

960991
int8_t AsyncClient::_s_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) {
961-
reinterpret_cast<AsyncClient*>(arg)->_sent(pcb, len);
992+
if(arg && pcb){
993+
reinterpret_cast<AsyncClient*>(arg)->_sent(pcb, len);
994+
} else {
995+
log_e("Bad Args: 0x%08x 0x%08x", arg, pcb);
996+
}
962997
return ERR_OK;
963998
}
964999

9651000
void AsyncClient::_s_error(void * arg, int8_t err) {
966-
reinterpret_cast<AsyncClient*>(arg)->_error(err);
1001+
if(arg){
1002+
reinterpret_cast<AsyncClient*>(arg)->_error(err);
1003+
} else {
1004+
log_e("Bad Arg: 0x%08x", arg);
1005+
}
9671006
}
9681007

9691008
int8_t AsyncClient::_s_connected(void * arg, void * pcb, int8_t err){
970-
reinterpret_cast<AsyncClient*>(arg)->_connected(pcb, err);
1009+
if(arg && pcb){
1010+
reinterpret_cast<AsyncClient*>(arg)->_connected(pcb, err);
1011+
} else {
1012+
log_e("Bad Args: 0x%08x 0x%08x", arg, pcb);
1013+
}
9711014
return ERR_OK;
9721015
}
9731016

@@ -1122,9 +1165,9 @@ void AsyncServer::end(){
11221165
tcp_arg(_pcb, NULL);
11231166
tcp_accept(_pcb, NULL);
11241167
if(_in_lwip_thread){
1125-
tcp_abort(_pcb);
1168+
tcp_close(_pcb);
11261169
} else {
1127-
_tcp_abort(_pcb);
1170+
_tcp_close(_pcb);
11281171
}
11291172
_pcb = NULL;
11301173
}

src/AsyncTCP.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef std::function<void(void*, AsyncClient*, struct pbuf *pb)> AcPacketHandle
4343
typedef std::function<void(void*, AsyncClient*, uint32_t time)> AcTimeoutHandler;
4444

4545
struct tcp_pcb;
46-
struct _ip_addr;
46+
struct ip_addr;
4747

4848
class AsyncClient {
4949
protected:
@@ -81,7 +81,7 @@ class AsyncClient {
8181
void _error(int8_t err);
8282
int8_t _poll(tcp_pcb* pcb);
8383
int8_t _sent(tcp_pcb* pcb, uint16_t len);
84-
void _dns_found(struct _ip_addr *ipaddr);
84+
void _dns_found(struct ip_addr *ipaddr);
8585

8686

8787
public:
@@ -108,13 +108,13 @@ class AsyncClient {
108108

109109
bool canSend();//ack is not pending
110110
size_t space();
111-
size_t add(const char* data, size_t size, uint8_t apiflags=0);//add for sending
111+
size_t add(const char* data, size_t size, uint8_t apiflags=ASYNC_WRITE_FLAG_COPY);//add for sending
112112
bool send();//send all data added with the method above
113113
size_t ack(size_t len); //ack data that you have not acked using the method below
114114
void ackLater(){ _ack_pcb = false; } //will not ack the current packet. Call from onData
115115

116116
size_t write(const char* data);
117-
size_t write(const char* data, size_t size, uint8_t apiflags=0); //only when canSend() == true
117+
size_t write(const char* data, size_t size, uint8_t apiflags=ASYNC_WRITE_FLAG_COPY); //only when canSend() == true
118118

119119
uint8_t state();
120120
bool connecting();
@@ -161,7 +161,7 @@ class AsyncClient {
161161
static void _s_error(void *arg, int8_t err);
162162
static int8_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len);
163163
static int8_t _s_connected(void* arg, void* tpcb, int8_t err);
164-
static void _s_dns_found(const char *name, struct _ip_addr *ipaddr, void *arg);
164+
static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg);
165165

166166
bool _in_lwip_thread;
167167
};

0 commit comments

Comments
 (0)