2
2
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
3
3
4
4
#include " AsyncTCP.h"
5
+ #include " AsyncTCPLogging.h"
5
6
#include " AsyncTCPSimpleIntrusiveList.h"
6
7
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)
30
12
#include < Arduino.h>
31
13
// LibreTiny does not support IDF - disable code that expects it to be available
32
14
#define ESP_IDF_VERSION_MAJOR (0 )
@@ -35,7 +17,27 @@ static unsigned long millis() {
35
17
// ESP watchdog is not available
36
18
#undef CONFIG_ASYNC_TCP_USE_WDT
37
19
#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
39
41
40
42
extern " C" {
41
43
#include " lwip/dns.h"
@@ -231,7 +233,7 @@ static inline lwip_tcp_event_packet_t *_get_async_event() {
231
233
next_pkt = _async_queue.begin ()) {
232
234
// if the next event that will come is a poll event for the same connection, we can discard it and continue
233
235
_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 ( " merging polls, network congestion or async callbacks might be too slow!" );
235
237
}
236
238
237
239
/*
@@ -245,7 +247,7 @@ static inline lwip_tcp_event_packet_t *_get_async_event() {
245
247
*/
246
248
if (_async_queue.size () > (rand () % CONFIG_ASYNC_TCP_QUEUE_SIZE / 4 + CONFIG_ASYNC_TCP_QUEUE_SIZE * 3 / 4 )) {
247
249
_free_event (e);
248
- log_d (" discarding poll due to queue congestion" );
250
+ async_tcp_log_d (" discarding poll due to queue congestion" );
249
251
continue ;
250
252
}
251
253
@@ -308,7 +310,7 @@ void AsyncTCP_detail::handle_async_event(lwip_tcp_event_packet_t *e) {
308
310
static void _async_service_task (void *pvParameters) {
309
311
#if CONFIG_ASYNC_TCP_USE_WDT
310
312
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" );
312
314
}
313
315
#endif
314
316
for (;;) {
@@ -404,7 +406,7 @@ static int8_t _tcp_connected(void *arg, tcp_pcb *pcb, int8_t err) {
404
406
AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
405
407
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_CONNECTED, client};
406
408
if (!e) {
407
- log_e (" Failed to allocate event packet" );
409
+ async_tcp_log_e (" Failed to allocate event packet" );
408
410
return ERR_MEM;
409
411
}
410
412
e->connected .pcb = pcb;
@@ -418,9 +420,9 @@ int8_t AsyncTCP_detail::tcp_poll(void *arg, struct tcp_pcb *pcb) {
418
420
// throttle polling events queueing when event queue is getting filled up, let it handle _onack's
419
421
{
420
422
queue_mutex_guard guard;
421
- // log_d ("qs:%u", _async_queue.size());
423
+ // async_tcp_log_d ("qs:%u", _async_queue.size());
422
424
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" );
424
426
return ERR_OK;
425
427
}
426
428
}
@@ -429,7 +431,7 @@ int8_t AsyncTCP_detail::tcp_poll(void *arg, struct tcp_pcb *pcb) {
429
431
AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
430
432
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_POLL, client};
431
433
if (!e) {
432
- log_e (" Failed to allocate event packet" );
434
+ async_tcp_log_e (" Failed to allocate event packet" );
433
435
return ERR_MEM;
434
436
}
435
437
e->poll .pcb = pcb;
@@ -443,7 +445,7 @@ int8_t AsyncTCP_detail::tcp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb
443
445
AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
444
446
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_RECV, client};
445
447
if (!e) {
446
- log_e (" Failed to allocate event packet" );
448
+ async_tcp_log_e (" Failed to allocate event packet" );
447
449
return ERR_MEM;
448
450
}
449
451
if (pb) {
@@ -470,7 +472,7 @@ int8_t AsyncTCP_detail::tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
470
472
AsyncClient *client = reinterpret_cast <AsyncClient *>(arg);
471
473
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_SENT, client};
472
474
if (!e) {
473
- log_e (" Failed to allocate event packet" );
475
+ async_tcp_log_e (" Failed to allocate event packet" );
474
476
return ERR_MEM;
475
477
}
476
478
e->sent .pcb = pcb;
@@ -493,7 +495,7 @@ void AsyncTCP_detail::tcp_error(void *arg, int8_t err) {
493
495
// enqueue event to be processed in the async task for the user callback
494
496
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_ERROR, client};
495
497
if (!e) {
496
- log_e (" Failed to allocate event packet" );
498
+ async_tcp_log_e (" Failed to allocate event packet" );
497
499
return ;
498
500
}
499
501
e->error .err = err;
@@ -508,7 +510,7 @@ static void _tcp_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) {
508
510
509
511
lwip_tcp_event_packet_t *e = new (std::nothrow) lwip_tcp_event_packet_t {LWIP_TCP_DNS, client};
510
512
if (!e) {
511
- log_e (" Failed to allocate event packet" );
513
+ async_tcp_log_e (" Failed to allocate event packet" );
512
514
return ;
513
515
}
514
516
@@ -819,11 +821,11 @@ void AsyncClient::onPoll(AcConnectHandler cb, void *arg) {
819
821
820
822
bool AsyncClient::connect (ip_addr_t addr, uint16_t port) {
821
823
if (_pcb) {
822
- log_d (" already connected, state %d" , _pcb->state );
824
+ async_tcp_log_d (" already connected, state %d" , _pcb->state );
823
825
return false ;
824
826
}
825
827
if (!_start_async_task ()) {
826
- log_e (" failed to start task" );
828
+ async_tcp_log_e (" failed to start task" );
827
829
return false ;
828
830
}
829
831
@@ -836,7 +838,7 @@ bool AsyncClient::connect(ip_addr_t addr, uint16_t port) {
836
838
pcb = tcp_new_ip_type (IPADDR_TYPE_V4);
837
839
#endif
838
840
if (!pcb) {
839
- log_e (" pcb == NULL" );
841
+ async_tcp_log_e (" pcb == NULL" );
840
842
return false ;
841
843
}
842
844
_bind_tcp_callbacks (pcb, this );
@@ -878,7 +880,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
878
880
ip_addr_t addr;
879
881
880
882
if (!_start_async_task ()) {
881
- log_e (" failed to start task" );
883
+ async_tcp_log_e (" failed to start task" );
882
884
return false ;
883
885
}
884
886
@@ -905,7 +907,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
905
907
_connect_port = port;
906
908
return true ;
907
909
}
908
- log_d (" error: %d" , err);
910
+ async_tcp_log_d (" error: %d" , err);
909
911
return false ;
910
912
}
911
913
@@ -1018,7 +1020,7 @@ void AsyncClient::_error(int8_t err) {
1018
1020
// In LwIP Thread
1019
1021
int8_t AsyncClient::_lwip_fin (tcp_pcb *pcb, int8_t err) {
1020
1022
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);
1022
1024
return ERR_OK;
1023
1025
}
1024
1026
_reset_tcp_callbacks (_pcb, this );
@@ -1072,11 +1074,11 @@ int8_t AsyncClient::_recv(tcp_pcb *pcb, pbuf *pb, int8_t err) {
1072
1074
1073
1075
int8_t AsyncClient::_poll (tcp_pcb *pcb) {
1074
1076
if (!_pcb) {
1075
- // log_d ("pcb is NULL");
1077
+ // async_tcp_log_d ("pcb is NULL");
1076
1078
return ERR_OK;
1077
1079
}
1078
1080
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);
1080
1082
return ERR_OK;
1081
1083
}
1082
1084
@@ -1087,7 +1089,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
1087
1089
const uint32_t one_day = 86400000 ;
1088
1090
bool last_tx_is_after_last_ack = (_rx_last_ack - _tx_last_packet + one_day) < one_day;
1089
1091
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 );
1091
1093
if (_timeout_cb) {
1092
1094
_timeout_cb (_timeout_cb_arg, this , (now - _tx_last_packet));
1093
1095
}
@@ -1096,7 +1098,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
1096
1098
}
1097
1099
// RX Timeout
1098
1100
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 );
1100
1102
_close ();
1101
1103
return ERR_OK;
1102
1104
}
@@ -1485,7 +1487,7 @@ void AsyncServer::begin() {
1485
1487
}
1486
1488
1487
1489
if (!_start_async_task ()) {
1488
- log_e (" failed to start task" );
1490
+ async_tcp_log_e (" failed to start task" );
1489
1491
return ;
1490
1492
}
1491
1493
int8_t err;
@@ -1498,22 +1500,22 @@ void AsyncServer::begin() {
1498
1500
#endif
1499
1501
}
1500
1502
if (!_pcb) {
1501
- log_e (" _pcb == NULL" );
1503
+ async_tcp_log_e (" _pcb == NULL" );
1502
1504
return ;
1503
1505
}
1504
1506
1505
1507
err = _tcp_bind (&_pcb, &_addr, _port);
1506
1508
1507
1509
if (err != ERR_OK) {
1508
1510
// pcb was closed by _tcp_bind
1509
- log_e (" bind error: %d" , err);
1511
+ async_tcp_log_e (" bind error: %d" , err);
1510
1512
return ;
1511
1513
}
1512
1514
1513
1515
static uint8_t backlog = 5 ;
1514
1516
_pcb = _tcp_listen_with_backlog (_pcb, backlog);
1515
1517
if (!_pcb) {
1516
- log_e (" listen_pcb == NULL" );
1518
+ async_tcp_log_e (" listen_pcb == NULL" );
1517
1519
return ;
1518
1520
}
1519
1521
tcp_core_guard tcg;
@@ -1536,7 +1538,7 @@ void AsyncServer::end() {
1536
1538
// runs on LwIP thread
1537
1539
int8_t AsyncTCP_detail::tcp_accept (void *arg, tcp_pcb *pcb, int8_t err) {
1538
1540
if (!pcb) {
1539
- log_e (" _accept failed: pcb is NULL" );
1541
+ async_tcp_log_e (" _accept failed: pcb is NULL" );
1540
1542
return ERR_ABRT;
1541
1543
}
1542
1544
auto server = reinterpret_cast <AsyncServer *>(arg);
@@ -1558,20 +1560,20 @@ int8_t AsyncTCP_detail::tcp_accept(void *arg, tcp_pcb *pcb, int8_t err) {
1558
1560
// 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
1559
1561
c->_pcb = nullptr ;
1560
1562
tcp_abort (pcb);
1561
- log_e (" _accept failed: couldn't accept client" );
1563
+ async_tcp_log_e (" _accept failed: couldn't accept client" );
1562
1564
return ERR_ABRT;
1563
1565
}
1564
1566
if (c) {
1565
1567
// Couldn't complete setup
1566
1568
// pcb has already been aborted
1567
1569
delete c;
1568
1570
pcb = nullptr ;
1569
- log_e (" _accept failed: couldn't complete setup" );
1571
+ async_tcp_log_e (" _accept failed: couldn't complete setup" );
1570
1572
return ERR_ABRT;
1571
1573
}
1572
- log_e (" _accept failed: couldn't allocate client" );
1574
+ async_tcp_log_e (" _accept failed: couldn't allocate client" );
1573
1575
} else {
1574
- log_e (" _accept failed: no onConnect callback" );
1576
+ async_tcp_log_e (" _accept failed: no onConnect callback" );
1575
1577
}
1576
1578
tcp_abort (pcb);
1577
1579
return ERR_OK;
0 commit comments