22// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
33
44#include " AsyncTCP.h"
5+ #include " AsyncTCPLogging.h"
56#include " AsyncTCPSimpleIntrusiveList.h"
67
7- #ifndef LIBRETINY
8- #include < esp_log.h>
9-
10- #ifdef ARDUINO
11- #include < esp32-hal.h>
12- #include < esp32-hal-log.h>
13- #if (ESP_IDF_VERSION_MAJOR >= 5)
14- #include < NetworkInterface.h>
15- #endif
16- #else
17- #include " esp_timer.h"
18- #define log_e (...) ESP_LOGE(__FILE__, __VA_ARGS__)
19- #define log_w (...) ESP_LOGW(__FILE__, __VA_ARGS__)
20- #define log_i (...) ESP_LOGI(__FILE__, __VA_ARGS__)
21- #define log_d (...) ESP_LOGD(__FILE__, __VA_ARGS__)
22- #define log_v (...) ESP_LOGV(__FILE__, __VA_ARGS__)
23- static unsigned long millis () {
24- return (unsigned long )(esp_timer_get_time () / 1000ULL );
25- }
26- #endif
27- #endif
28-
29- #ifdef LIBRETINY
8+ /* *
9+ * LibreTiny specific configurations
10+ */
11+ #if defined(LIBRETINY)
3012#include < Arduino.h>
3113// LibreTiny does not support IDF - disable code that expects it to be available
3214#define ESP_IDF_VERSION_MAJOR (0 )
@@ -35,7 +17,27 @@ static unsigned long millis() {
3517// ESP watchdog is not available
3618#undef CONFIG_ASYNC_TCP_USE_WDT
3719#define CONFIG_ASYNC_TCP_USE_WDT 0
38- #endif
20+ #endif // LIBRETINY
21+
22+ /* *
23+ * Arduino specific configurations
24+ */
25+ #if defined(ARDUINO) && !defined(LIBRETINY)
26+ #include < esp_idf_version.h>
27+ #if (ESP_IDF_VERSION_MAJOR >= 5)
28+ #include < NetworkInterface.h>
29+ #endif // ESP_IDF_VERSION_MAJOR
30+ #endif // ARDUINO
31+
32+ /* *
33+ * ESP-IDF specific configurations
34+ */
35+ #if !defined(LIBRETINY) && !defined(ARDUINO)
36+ #include " esp_timer.h"
37+ static unsigned long millis () {
38+ return (unsigned long )(esp_timer_get_time () / 1000ULL );
39+ }
40+ #endif // !LIBRETINY && !ARDUINO
3941
4042extern " C" {
4143#include " lwip/dns.h"
@@ -146,7 +148,7 @@ struct lwip_tcp_event_packet_t {
146148 } dns;
147149 };
148150
149- inline lwip_tcp_event_packet_t (lwip_tcp_event_t _event, AsyncClient *_client) : next(nullptr ), event(_event), client(_client){};
151+ inline lwip_tcp_event_packet_t (lwip_tcp_event_t _event, AsyncClient *_client) : next(nullptr ), event(_event), client(_client) {};
150152};
151153
152154// Detail class for interacting with AsyncClient internals, but without exposing the API
@@ -172,7 +174,7 @@ class queue_mutex_guard {
172174 bool holds_mutex;
173175
174176public:
175- inline queue_mutex_guard () : holds_mutex(xSemaphoreTake(_async_queue_mutex, portMAX_DELAY)){};
177+ inline queue_mutex_guard () : holds_mutex(xSemaphoreTake(_async_queue_mutex, portMAX_DELAY)) {};
176178 inline ~queue_mutex_guard () {
177179 if (holds_mutex) {
178180 xSemaphoreGive (_async_queue_mutex);
@@ -231,7 +233,7 @@ static inline lwip_tcp_event_packet_t *_get_async_event() {
231233 next_pkt = _async_queue.begin ()) {
232234 // if the next event that will come is a poll event for the same connection, we can discard it and continue
233235 _free_event (_async_queue.pop_front ());
234- log_d (" coalescing polls, network congestion or async callbacks might be too slow!" );
236+ async_tcp_log_d (" coalescing polls, network congestion or async callbacks might be too slow!" );
235237 }
236238
237239 /*
@@ -245,7 +247,7 @@ static inline lwip_tcp_event_packet_t *_get_async_event() {
245247 */
246248 if (_async_queue.size () > (rand () % CONFIG_ASYNC_TCP_QUEUE_SIZE / 4 + CONFIG_ASYNC_TCP_QUEUE_SIZE * 3 / 4 )) {
247249 _free_event (e);
248- log_d (" discarding poll due to queue congestion" );
250+ async_tcp_log_d (" discarding poll due to queue congestion" );
249251 continue ;
250252 }
251253
@@ -308,7 +310,7 @@ void AsyncTCP_detail::handle_async_event(lwip_tcp_event_packet_t *e) {
308310static void _async_service_task (void *pvParameters) {
309311#if CONFIG_ASYNC_TCP_USE_WDT
310312 if (esp_task_wdt_add (NULL ) != ESP_OK) {
311- log_w (" Failed to add async task to WDT" );
313+ async_tcp_log_w (" Failed to add async task to WDT" );
312314 }
313315#endif
314316 for (;;) {
@@ -404,7 +406,7 @@ static int8_t _tcp_connected(void *arg, tcp_pcb *pcb, int8_t err) {
404406 AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
405407 lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_CONNECTED, client};
406408 if (!e) {
407- log_e (" Failed to allocate event packet" );
409+ async_tcp_log_e (" Failed to allocate event packet" );
408410 return ERR_MEM;
409411 }
410412 e->connected .pcb = pcb;
@@ -418,9 +420,9 @@ int8_t AsyncTCP_detail::tcp_poll(void *arg, struct tcp_pcb *pcb) {
418420 // throttle polling events queueing when event queue is getting filled up, let it handle _onack's
419421 {
420422 queue_mutex_guard guard;
421- // log_d ("qs:%u", _async_queue.size());
423+ // async_tcp_log_d ("qs:%u", _async_queue.size());
422424 if (_async_queue.size () > (rand () % CONFIG_ASYNC_TCP_QUEUE_SIZE / 2 + CONFIG_ASYNC_TCP_QUEUE_SIZE / 4 )) {
423- log_d (" throttling" );
425+ async_tcp_log_d (" throttling" );
424426 return ERR_OK;
425427 }
426428 }
@@ -429,7 +431,7 @@ int8_t AsyncTCP_detail::tcp_poll(void *arg, struct tcp_pcb *pcb) {
429431 AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
430432 lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_POLL, client};
431433 if (!e) {
432- log_e (" Failed to allocate event packet" );
434+ async_tcp_log_e (" Failed to allocate event packet" );
433435 return ERR_MEM;
434436 }
435437 e->poll .pcb = pcb;
@@ -443,7 +445,7 @@ int8_t AsyncTCP_detail::tcp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb
443445 AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
444446 lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_RECV, client};
445447 if (!e) {
446- log_e (" Failed to allocate event packet" );
448+ async_tcp_log_e (" Failed to allocate event packet" );
447449 return ERR_MEM;
448450 }
449451 if (pb) {
@@ -470,7 +472,7 @@ int8_t AsyncTCP_detail::tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
470472 AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
471473 lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_SENT, client};
472474 if (!e) {
473- log_e (" Failed to allocate event packet" );
475+ async_tcp_log_e (" Failed to allocate event packet" );
474476 return ERR_MEM;
475477 }
476478 e->sent .pcb = pcb;
@@ -493,7 +495,7 @@ void AsyncTCP_detail::tcp_error(void *arg, int8_t err) {
493495 // enqueue event to be processed in the async task for the user callback
494496 lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_ERROR, client};
495497 if (!e) {
496- log_e (" Failed to allocate event packet" );
498+ async_tcp_log_e (" Failed to allocate event packet" );
497499 return ;
498500 }
499501 e->error .err = err;
@@ -508,7 +510,7 @@ static void _tcp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
508510
509511 lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_DNS, client};
510512 if (!e) {
511- log_e (" Failed to allocate event packet" );
513+ async_tcp_log_e (" Failed to allocate event packet" );
512514 return ;
513515 }
514516
@@ -819,11 +821,11 @@ void AsyncClient::onPoll(AcConnectHandler cb, void *arg) {
819821
820822bool AsyncClient::connect (ip_addr_t addr, uint16_t port) {
821823 if (_pcb) {
822- log_d (" already connected, state %d" , _pcb->state );
824+ async_tcp_log_d (" already connected, state %d" , _pcb->state );
823825 return false ;
824826 }
825827 if (!_start_async_task ()) {
826- log_e (" failed to start task" );
828+ async_tcp_log_e (" failed to start task" );
827829 return false ;
828830 }
829831
@@ -836,7 +838,7 @@ bool AsyncClient::connect(ip_addr_t addr, uint16_t port) {
836838 pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
837839#endif
838840 if (!pcb) {
839- log_e (" pcb == NULL" );
841+ async_tcp_log_e (" pcb == NULL" );
840842 return false ;
841843 }
842844 _bind_tcp_callbacks (pcb, this );
@@ -878,7 +880,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
878880 ip_addr_t addr;
879881
880882 if (!_start_async_task ()) {
881- log_e (" failed to start task" );
883+ async_tcp_log_e (" failed to start task" );
882884 return false ;
883885 }
884886
@@ -905,7 +907,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
905907 _connect_port = port;
906908 return true ;
907909 }
908- log_d (" error: %d" , err);
910+ async_tcp_log_d (" error: %d" , err);
909911 return false ;
910912}
911913
@@ -1018,7 +1020,7 @@ void AsyncClient::_error(int8_t err) {
10181020// In LwIP Thread
10191021int8_t AsyncClient::_lwip_fin (tcp_pcb *pcb, int8_t err) {
10201022 if (!_pcb || pcb != _pcb) {
1021- log_d (" 0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t )pcb, (uint32_t )_pcb);
1023+ async_tcp_log_d (" 0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t )pcb, (uint32_t )_pcb);
10221024 return ERR_OK;
10231025 }
10241026 _reset_tcp_callbacks (_pcb, this );
@@ -1072,11 +1074,11 @@ int8_t AsyncClient::_recv(tcp_pcb *pcb, pbuf *pb, int8_t err) {
10721074
10731075int8_t AsyncClient::_poll (tcp_pcb *pcb) {
10741076 if (!_pcb) {
1075- // log_d ("pcb is NULL");
1077+ // async_tcp_log_d ("pcb is NULL");
10761078 return ERR_OK;
10771079 }
10781080 if (pcb != _pcb) {
1079- log_d (" 0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t )pcb, (uint32_t )_pcb);
1081+ async_tcp_log_d (" 0x%08" PRIx32 " != 0x%08" PRIx32, (uint32_t )pcb, (uint32_t )_pcb);
10801082 return ERR_OK;
10811083 }
10821084
@@ -1087,7 +1089,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
10871089 const uint32_t one_day = 86400000 ;
10881090 bool last_tx_is_after_last_ack = (_rx_last_ack - _tx_last_packet + one_day) < one_day;
10891091 if (last_tx_is_after_last_ack && (now - _tx_last_packet) >= _ack_timeout) {
1090- log_d (" ack timeout %d" , pcb->state );
1092+ async_tcp_log_d (" ack timeout %d" , pcb->state );
10911093 if (_timeout_cb) {
10921094 _timeout_cb (_timeout_cb_arg, this , (now - _tx_last_packet));
10931095 }
@@ -1096,7 +1098,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
10961098 }
10971099 // RX Timeout
10981100 if (_rx_timeout && (now - _rx_last_packet) >= (_rx_timeout * 1000 )) {
1099- log_d (" rx timeout %d" , pcb->state );
1101+ async_tcp_log_d (" rx timeout %d" , pcb->state );
11001102 _close ();
11011103 return ERR_OK;
11021104 }
@@ -1485,7 +1487,7 @@ void AsyncServer::begin() {
14851487 }
14861488
14871489 if (!_start_async_task ()) {
1488- log_e (" failed to start task" );
1490+ async_tcp_log_e (" failed to start task" );
14891491 return ;
14901492 }
14911493 int8_t err;
@@ -1498,22 +1500,22 @@ void AsyncServer::begin() {
14981500#endif
14991501 }
15001502 if (!_pcb) {
1501- log_e (" _pcb == NULL" );
1503+ async_tcp_log_e (" _pcb == NULL" );
15021504 return ;
15031505 }
15041506
15051507 err = _tcp_bind (&_pcb, &_addr, _port);
15061508
15071509 if (err != ERR_OK) {
15081510 // pcb was closed by _tcp_bind
1509- log_e (" bind error: %d" , err);
1511+ async_tcp_log_e (" bind error: %d" , err);
15101512 return ;
15111513 }
15121514
15131515 static uint8_t backlog = 5 ;
15141516 _pcb = _tcp_listen_with_backlog (_pcb, backlog);
15151517 if (!_pcb) {
1516- log_e (" listen_pcb == NULL" );
1518+ async_tcp_log_e (" listen_pcb == NULL" );
15171519 return ;
15181520 }
15191521 tcp_core_guard tcg;
@@ -1536,7 +1538,7 @@ void AsyncServer::end() {
15361538// runs on LwIP thread
15371539int8_t AsyncTCP_detail::tcp_accept (void *arg, tcp_pcb *pcb, int8_t err) {
15381540 if (!pcb) {
1539- log_e (" _accept failed: pcb is NULL" );
1541+ async_tcp_log_e (" _accept failed: pcb is NULL" );
15401542 return ERR_ABRT;
15411543 }
15421544 auto server = reinterpret_cast <AsyncServer *>(arg);
@@ -1558,20 +1560,20 @@ int8_t AsyncTCP_detail::tcp_accept(void *arg, tcp_pcb *pcb, int8_t err) {
15581560 // We can't let the client object call in to close, as we're on the LWIP thread; it could deadlock trying to RPC to itself
15591561 c->_pcb = nullptr ;
15601562 tcp_abort (pcb);
1561- log_e (" _accept failed: couldn't accept client" );
1563+ async_tcp_log_e (" _accept failed: couldn't accept client" );
15621564 return ERR_ABRT;
15631565 }
15641566 if (c) {
15651567 // Couldn't complete setup
15661568 // pcb has already been aborted
15671569 delete c;
15681570 pcb = nullptr ;
1569- log_e (" _accept failed: couldn't complete setup" );
1571+ async_tcp_log_e (" _accept failed: couldn't complete setup" );
15701572 return ERR_ABRT;
15711573 }
1572- log_e (" _accept failed: couldn't allocate client" );
1574+ async_tcp_log_e (" _accept failed: couldn't allocate client" );
15731575 } else {
1574- log_e (" _accept failed: no onConnect callback" );
1576+ async_tcp_log_e (" _accept failed: no onConnect callback" );
15751577 }
15761578 tcp_abort (pcb);
15771579 return ERR_OK;
0 commit comments