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