Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit cefd9e4

Browse files
committed
Add options to select running core and to enable WDT for Async
1 parent ff5c8b2 commit cefd9e4

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

Kconfig.projbuild

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
menu "AsyncTCP Configuration"
2+
3+
choice ASYNC_TCP_RUNNING_CORE
4+
bool "Core on which AsyncTCP's thread is running"
5+
default ASYNC_TCP_RUN_CORE1
6+
help
7+
Select on which core AsyncTCP is running
8+
9+
config ASYNC_TCP_RUN_CORE0
10+
bool "CORE 0"
11+
config ASYNC_TCP_RUN_CORE1
12+
bool "CORE 1"
13+
config ASYNC_TCP_RUN_NO_AFFINITY
14+
bool "BOTH"
15+
16+
endchoice
17+
18+
config ASYNC_TCP_RUNNING_CORE
19+
int
20+
default 0 if ASYNC_TCP_RUN_CORE0
21+
default 1 if ASYNC_TCP_RUN_CORE1
22+
default -1 if ASYNC_TCP_RUN_NO_AFFINITY
23+
24+
config ASYNC_TCP_USE_WDT
25+
bool "Enable WDT for the AsyncTCP task"
26+
default "y"
27+
help
28+
Enable WDT for the AsyncTCP task, so it will trigger if a handler is locking the thread.
29+
30+
endmenu

src/AsyncTCP.cpp

+23-14
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ extern "C"{
2828
#include "lwip/inet.h"
2929
#include "lwip/dns.h"
3030
}
31-
32-
#if CONFIG_FREERTOS_UNICORE
33-
#define ASYNCTCP_RUNNING_CORE 0
34-
#else
35-
#define ASYNCTCP_RUNNING_CORE 1
36-
#endif
31+
#include "esp_task_wdt.h"
3732

3833
/*
3934
* TCP/IP Event Task
@@ -146,15 +141,19 @@ static void _handle_async_event(lwip_event_packet_t * e){
146141
if(e->event == LWIP_TCP_CLEAR){
147142
_remove_events_with_arg(e->arg);
148143
} else if(e->event == LWIP_TCP_RECV){
144+
//ets_printf("-R: 0x%08x\n", e->recv.pcb);
149145
//ets_printf("R: 0x%08x 0x%08x %d\n", e->arg, e->recv.pcb, e->recv.err);
150146
AsyncClient::_s_recv(e->arg, e->recv.pcb, e->recv.pb, e->recv.err);
151147
} else if(e->event == LWIP_TCP_FIN){
148+
//ets_printf("-F: 0x%08x\n", e->fin.pcb);
152149
//ets_printf("F: 0x%08x 0x%08x %d\n", e->arg, e->fin.pcb, e->fin.err);
153150
AsyncClient::_s_fin(e->arg, e->fin.pcb, e->fin.err);
154151
} else if(e->event == LWIP_TCP_SENT){
152+
//ets_printf("-S: 0x%08x\n", e->sent.pcb);
155153
//ets_printf("S: 0x%08x 0x%08x\n", e->arg, e->sent.pcb);
156154
AsyncClient::_s_sent(e->arg, e->sent.pcb, e->sent.len);
157155
} else if(e->event == LWIP_TCP_POLL){
156+
//ets_printf("-P: 0x%08x\n", e->poll.pcb);
158157
//ets_printf("P: 0x%08x 0x%08x\n", e->arg, e->poll.pcb);
159158
AsyncClient::_s_poll(e->arg, e->poll.pcb);
160159
} else if(e->event == LWIP_TCP_ERROR){
@@ -174,7 +173,17 @@ static void _async_service_task(void *pvParameters){
174173
lwip_event_packet_t * packet = NULL;
175174
for (;;) {
176175
if(_get_async_event(&packet)){
176+
#if CONFIG_ASYNC_TCP_USE_WDT
177+
if(esp_task_wdt_add(NULL) != ESP_OK){
178+
log_e("Failed to add async task to WDT");
179+
}
180+
#endif
177181
_handle_async_event(packet);
182+
#if CONFIG_ASYNC_TCP_USE_WDT
183+
if(esp_task_wdt_delete(NULL) != ESP_OK){
184+
log_e("Failed to remove loop task from WDT");
185+
}
186+
#endif
178187
}
179188
}
180189
vTaskDelete(NULL);
@@ -193,7 +202,7 @@ static bool _start_async_task(){
193202
return false;
194203
}
195204
if(!_async_service_task_handle){
196-
xTaskCreatePinnedToCore(_async_service_task, "async_tcp", 8192 * 2, NULL, 3, &_async_service_task_handle, ASYNCTCP_RUNNING_CORE);
205+
xTaskCreateUniversal(_async_service_task, "async_tcp", 8192 * 2, NULL, 3, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE);
197206
if(!_async_service_task_handle){
198207
return false;
199208
}
@@ -216,7 +225,7 @@ static int8_t _tcp_clear_events(void * arg) {
216225
}
217226

218227
static int8_t _tcp_connected(void * arg, tcp_pcb * pcb, int8_t err) {
219-
//ets_printf("C: 0x%08x\n", pcb);
228+
//ets_printf("+C: 0x%08x\n", pcb);
220229
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
221230
e->event = LWIP_TCP_CONNECTED;
222231
e->arg = arg;
@@ -229,7 +238,7 @@ static int8_t _tcp_connected(void * arg, tcp_pcb * pcb, int8_t err) {
229238
}
230239

231240
static int8_t _tcp_poll(void * arg, struct tcp_pcb * pcb) {
232-
//ets_printf("P: 0x%08x\n", pcb);
241+
//ets_printf("+P: 0x%08x\n", pcb);
233242
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
234243
e->event = LWIP_TCP_POLL;
235244
e->arg = arg;
@@ -244,13 +253,13 @@ static int8_t _tcp_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_
244253
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
245254
e->arg = arg;
246255
if(pb){
247-
//ets_printf("R: 0x%08x\n", pcb);
256+
//ets_printf("+R: 0x%08x\n", pcb);
248257
e->event = LWIP_TCP_RECV;
249258
e->recv.pcb = pcb;
250259
e->recv.pb = pb;
251260
e->recv.err = err;
252261
} else {
253-
//ets_printf("D: 0x%08x\n", pcb);
262+
//ets_printf("+F: 0x%08x\n", pcb);
254263
e->event = LWIP_TCP_FIN;
255264
e->fin.pcb = pcb;
256265
e->fin.err = err;
@@ -264,7 +273,7 @@ static int8_t _tcp_recv(void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_
264273
}
265274

266275
static int8_t _tcp_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) {
267-
//ets_printf("S: 0x%08x\n", pcb);
276+
//ets_printf("+S: 0x%08x\n", pcb);
268277
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
269278
e->event = LWIP_TCP_SENT;
270279
e->arg = arg;
@@ -277,7 +286,7 @@ static int8_t _tcp_sent(void * arg, struct tcp_pcb * pcb, uint16_t len) {
277286
}
278287

279288
static void _tcp_error(void * arg, int8_t err) {
280-
//ets_printf("E: 0x%08x\n", arg);
289+
//ets_printf("+E: 0x%08x\n", arg);
281290
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc(sizeof(lwip_event_packet_t));
282291
e->event = LWIP_TCP_ERROR;
283292
e->arg = arg;
@@ -1174,7 +1183,7 @@ int8_t AsyncServer::_s_accepted(void *arg, AsyncClient* client){
11741183

11751184
//runs on LwIP thread
11761185
int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
1177-
//ets_printf("A: 0x%08x\n", pcb);
1186+
//ets_printf("+A: 0x%08x\n", pcb);
11781187
if(_connect_cb){
11791188
if (_noDelay) {
11801189
tcp_nagle_disable(pcb);

src/AsyncTCP.h

+9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@
2323
#define ASYNCTCP_H_
2424

2525
#include "IPAddress.h"
26+
#include "sdkconfig.h"
2627
#include <functional>
2728
extern "C" {
2829
#include "freertos/semphr.h"
2930
#include "lwip/pbuf.h"
3031
}
3132

33+
#ifndef CONFIG_ASYNC_TCP_RUNNING_CORE
34+
#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 //any available core
35+
#endif
36+
37+
#ifndef CONFIG_ASYNC_TCP_USE_WDT
38+
#define CONFIG_ASYNC_TCP_USE_WDT 0 //if enabled, adds between 33us and 200us per event
39+
#endif
40+
3241
class AsyncClient;
3342

3443
#define ASYNC_MAX_ACK_TIME 5000

0 commit comments

Comments
 (0)