1
1
// SPDX-License-Identifier: LGPL-3.0-or-later
2
2
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
3
3
4
- #include " Arduino.h"
5
-
6
4
#include " AsyncTCP.h"
7
5
6
+ #include < esp_log.h>
7
+
8
+ #ifdef ARDUINO
9
+ #include < esp32-hal.h>
10
+ #include < esp32-hal-log.h>
11
+ #if (ESP_IDF_VERSION_MAJOR >= 5)
12
+ #include < NetworkInterface.h>
13
+ #endif
14
+ #else
15
+ #include " esp_timer.h"
16
+ #define log_e (...) ESP_LOGE(__FILE__, __VA_ARGS__)
17
+ #define log_w (...) ESP_LOGW(__FILE__, __VA_ARGS__)
18
+ #define log_i (...) ESP_LOGI(__FILE__, __VA_ARGS__)
19
+ #define log_d (...) ESP_LOGD(__FILE__, __VA_ARGS__)
20
+ #define log_v (...) ESP_LOGV(__FILE__, __VA_ARGS__)
21
+ static unsigned long millis () {
22
+ return (unsigned long )(esp_timer_get_time () / 1000ULL );
23
+ }
24
+ #endif
25
+
8
26
extern " C" {
9
27
#include " lwip/dns.h"
10
28
#include " lwip/err.h"
@@ -19,9 +37,6 @@ extern "C" {
19
37
20
38
// Required for:
21
39
// https://github.com/espressif/arduino-esp32/blob/3.0.3/libraries/Network/src/NetworkInterface.cpp#L37-L47
22
- #if ESP_IDF_VERSION_MAJOR >= 5
23
- #include < NetworkInterface.h>
24
- #endif
25
40
26
41
// https://github.com/espressif/arduino-esp32/issues/10526
27
42
#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
@@ -829,7 +844,7 @@ void AsyncClient::onPoll(AcConnectHandler cb, void *arg) {
829
844
* Main Public Methods
830
845
* */
831
846
832
- bool AsyncClient::_connect (ip_addr_t addr, uint16_t port) {
847
+ bool AsyncClient::connect (ip_addr_t addr, uint16_t port) {
833
848
if (_pcb) {
834
849
log_d (" already connected, state %d" , _pcb->state );
835
850
return false ;
@@ -862,6 +877,7 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port) {
862
877
return err == ESP_OK;
863
878
}
864
879
880
+ #ifdef ARDUINO
865
881
bool AsyncClient::connect (const IPAddress &ip, uint16_t port) {
866
882
ip_addr_t addr;
867
883
#if ESP_IDF_VERSION_MAJOR < 5
@@ -871,15 +887,16 @@ bool AsyncClient::connect(const IPAddress &ip, uint16_t port) {
871
887
ip.to_ip_addr_t (&addr);
872
888
#endif
873
889
874
- return _connect (addr, port);
890
+ return connect (addr, port);
875
891
}
892
+ #endif
876
893
877
894
#if LWIP_IPV6 && ESP_IDF_VERSION_MAJOR < 5
878
895
bool AsyncClient::connect (const IPv6Address &ip, uint16_t port) {
879
896
auto ipaddr = static_cast <const uint32_t *>(ip);
880
897
ip_addr_t addr = IPADDR6_INIT (ipaddr[0 ], ipaddr[1 ], ipaddr[2 ], ipaddr[3 ]);
881
898
882
- return _connect (addr, port);
899
+ return connect (addr, port);
883
900
}
884
901
#endif
885
902
@@ -905,7 +922,7 @@ bool AsyncClient::connect(const char *host, uint16_t port) {
905
922
return connect (IPAddress (addr.addr ), port);
906
923
#endif
907
924
#else
908
- return _connect (addr, port);
925
+ return connect (addr, port);
909
926
#endif
910
927
} else if (err == ERR_INPROGRESS) {
911
928
_connect_port = port;
@@ -1073,7 +1090,7 @@ void AsyncClient::_error(int8_t err) {
1073
1090
// In LwIP Thread
1074
1091
int8_t AsyncClient::_lwip_fin (tcp_pcb *pcb, int8_t err) {
1075
1092
if (!_pcb || pcb != _pcb) {
1076
- log_d (" 0x%08x != 0x%08x " , (uint32_t )pcb, (uint32_t )_pcb);
1093
+ log_d (" 0x%08 " PRIx32 " != 0x%08 " PRIx32 , (uint32_t )pcb, (uint32_t )_pcb);
1077
1094
return ERR_OK;
1078
1095
}
1079
1096
tcp_arg (_pcb, NULL );
@@ -1139,7 +1156,7 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
1139
1156
return ERR_OK;
1140
1157
}
1141
1158
if (pcb != _pcb) {
1142
- log_d (" 0x%08x != 0x%08x " , (uint32_t )pcb, (uint32_t )_pcb);
1159
+ log_d (" 0x%08 " PRIx32 " != 0x%08 " PRIx32 , (uint32_t )pcb, (uint32_t )_pcb);
1143
1160
return ERR_OK;
1144
1161
}
1145
1162
@@ -1171,19 +1188,8 @@ int8_t AsyncClient::_poll(tcp_pcb *pcb) {
1171
1188
}
1172
1189
1173
1190
void AsyncClient::_dns_found (struct ip_addr *ipaddr) {
1174
- #if ESP_IDF_VERSION_MAJOR < 5
1175
- if (ipaddr && IP_IS_V4 (ipaddr)) {
1176
- connect (IPAddress (ip_addr_get_ip4_u32 (ipaddr)), _connect_port);
1177
- #if LWIP_IPV6
1178
- } else if (ipaddr && ipaddr->u_addr .ip6 .addr ) {
1179
- connect (IPv6Address (ipaddr->u_addr .ip6 .addr ), _connect_port);
1180
- #endif
1181
- #else
1182
1191
if (ipaddr) {
1183
- IPAddress ip;
1184
- ip.from_ip_addr_t (ipaddr);
1185
- connect (ip, _connect_port);
1186
- #endif
1192
+ connect (*ipaddr, _connect_port);
1187
1193
} else {
1188
1194
if (_error_cb) {
1189
1195
_error_cb (_error_cb_arg, this , -55 );
@@ -1282,22 +1288,25 @@ uint32_t AsyncClient::getRemoteAddress() const {
1282
1288
1283
1289
#if LWIP_IPV6
1284
1290
ip6_addr_t AsyncClient::getRemoteAddress6 () const {
1285
- if (!_pcb) {
1291
+ if (_pcb && _pcb->remote_ip .type == IPADDR_TYPE_V6) {
1292
+ return _pcb->remote_ip .u_addr .ip6 ;
1293
+ } else {
1286
1294
ip6_addr_t nulladdr;
1287
1295
ip6_addr_set_zero (&nulladdr);
1288
1296
return nulladdr;
1289
1297
}
1290
- return _pcb->remote_ip .u_addr .ip6 ;
1291
1298
}
1292
1299
1293
1300
ip6_addr_t AsyncClient::getLocalAddress6 () const {
1294
- if (!_pcb) {
1301
+ if (_pcb && _pcb->local_ip .type == IPADDR_TYPE_V6) {
1302
+ return _pcb->local_ip .u_addr .ip6 ;
1303
+ } else {
1295
1304
ip6_addr_t nulladdr;
1296
1305
ip6_addr_set_zero (&nulladdr);
1297
1306
return nulladdr;
1298
1307
}
1299
- return _pcb->local_ip .u_addr .ip6 ;
1300
1308
}
1309
+ #ifdef ARDUINO
1301
1310
#if ESP_IDF_VERSION_MAJOR < 5
1302
1311
IPv6Address AsyncClient::remoteIP6 () const {
1303
1312
return IPv6Address (getRemoteAddress6 ().addr );
@@ -1326,6 +1335,7 @@ IPAddress AsyncClient::localIP6() const {
1326
1335
}
1327
1336
#endif
1328
1337
#endif
1338
+ #endif
1329
1339
1330
1340
uint16_t AsyncClient::getRemotePort () const {
1331
1341
if (!_pcb) {
@@ -1352,6 +1362,27 @@ uint16_t AsyncClient::getLocalPort() const {
1352
1362
return _pcb->local_port ;
1353
1363
}
1354
1364
1365
+ ip4_addr_t AsyncClient::getRemoteAddress4 () const {
1366
+ if (_pcb && _pcb->remote_ip .type == IPADDR_TYPE_V4) {
1367
+ return _pcb->remote_ip .u_addr .ip4 ;
1368
+ } else {
1369
+ ip4_addr_t nulladdr;
1370
+ ip4_addr_set_zero (&nulladdr);
1371
+ return nulladdr;
1372
+ }
1373
+ }
1374
+
1375
+ ip4_addr_t AsyncClient::getLocalAddress4 () const {
1376
+ if (_pcb && _pcb->local_ip .type == IPADDR_TYPE_V4) {
1377
+ return _pcb->local_ip .u_addr .ip4 ;
1378
+ } else {
1379
+ ip4_addr_t nulladdr;
1380
+ ip4_addr_set_zero (&nulladdr);
1381
+ return nulladdr;
1382
+ }
1383
+ }
1384
+
1385
+ #ifdef ARDUINO
1355
1386
IPAddress AsyncClient::remoteIP () const {
1356
1387
#if ESP_IDF_VERSION_MAJOR < 5
1357
1388
return IPAddress (getRemoteAddress ());
@@ -1365,10 +1396,6 @@ IPAddress AsyncClient::remoteIP() const {
1365
1396
#endif
1366
1397
}
1367
1398
1368
- uint16_t AsyncClient::remotePort () const {
1369
- return getRemotePort ();
1370
- }
1371
-
1372
1399
IPAddress AsyncClient::localIP () const {
1373
1400
#if ESP_IDF_VERSION_MAJOR < 5
1374
1401
return IPAddress (getLocalAddress ());
@@ -1381,10 +1408,7 @@ IPAddress AsyncClient::localIP() const {
1381
1408
return ip;
1382
1409
#endif
1383
1410
}
1384
-
1385
- uint16_t AsyncClient::localPort () const {
1386
- return getLocalPort ();
1387
- }
1411
+ #endif
1388
1412
1389
1413
uint8_t AsyncClient::state () const {
1390
1414
if (!_pcb) {
@@ -1512,32 +1536,30 @@ int8_t AsyncClient::_s_connected(void *arg, struct tcp_pcb *pcb, int8_t err) {
1512
1536
Async TCP Server
1513
1537
*/
1514
1538
1515
- AsyncServer::AsyncServer (IPAddress addr, uint16_t port)
1516
- : _port (port)
1539
+ AsyncServer::AsyncServer (ip_addr_t addr, uint16_t port)
1540
+ : _port (port), _addr (addr), _noDelay (false ), _pcb (nullptr ), _connect_cb (nullptr ), _connect_cb_arg (nullptr ) {}
1541
+
1542
+ #ifdef ARDUINO
1543
+ AsyncServer::AsyncServer (IPAddress addr, uint16_t port) : _port (port), _noDelay (false ), _pcb (0 ), _connect_cb (0 ), _connect_cb_arg (0 ) {
1517
1544
#if ESP_IDF_VERSION_MAJOR < 5
1518
- ,
1519
- _bind4 ( true ), _bind6 ( false )
1545
+ _addr. type = IPADDR_TYPE_V4;
1546
+ _addr. u_addr . ip4 . addr = addr;
1520
1547
#else
1521
- ,
1522
- _bind4 (addr.type () != IPType::IPv6), _bind6 (addr.type () == IPType::IPv6)
1548
+ addr.to_ip_addr_t (&_addr);
1523
1549
#endif
1524
- ,
1525
- _addr (addr), _noDelay (false ), _pcb (0 ), _connect_cb (0 ), _connect_cb_arg (0 ) {
1526
1550
}
1527
-
1528
1551
#if ESP_IDF_VERSION_MAJOR < 5
1529
- AsyncServer::AsyncServer (IPv6Address addr, uint16_t port)
1530
- : _port (port), _bind4 (false ), _bind6 (true ), _addr6 (addr), _noDelay (false ), _pcb (0 ), _connect_cb (0 ), _connect_cb_arg (0 ) {}
1552
+ AsyncServer::AsyncServer (IPv6Address addr, uint16_t port) : _port (port), _noDelay (false ), _pcb (0 ), _connect_cb (0 ), _connect_cb_arg (0 ) {
1553
+ _addr.type = IPADDR_TYPE_V6;
1554
+ auto ipaddr = static_cast <const uint32_t *>(addr);
1555
+ _addr = IPADDR6_INIT (ipaddr[0 ], ipaddr[1 ], ipaddr[2 ], ipaddr[3 ]);
1556
+ }
1531
1557
#endif
1532
-
1533
- AsyncServer::AsyncServer (uint16_t port)
1534
- : _port (port), _bind4 (true ), _bind6 (false ), _addr ((uint32_t )IPADDR_ANY)
1535
- #if ESP_IDF_VERSION_MAJOR < 5
1536
- ,
1537
- _addr6 ()
1538
1558
#endif
1539
- ,
1540
- _noDelay (false ), _pcb (0 ), _connect_cb (0 ), _connect_cb_arg (0 ) {
1559
+
1560
+ AsyncServer::AsyncServer (uint16_t port) : _port (port), _noDelay (false ), _pcb (0 ), _connect_cb (0 ), _connect_cb_arg (0 ) {
1561
+ _addr.type = IPADDR_TYPE_V4;
1562
+ _addr.u_addr .ip4 .addr = INADDR_ANY;
1541
1563
}
1542
1564
1543
1565
AsyncServer::~AsyncServer () {
@@ -1560,26 +1582,14 @@ void AsyncServer::begin() {
1560
1582
}
1561
1583
int8_t err;
1562
1584
TCP_MUTEX_LOCK ();
1563
- _pcb = tcp_new_ip_type (_bind4 && _bind6 ? IPADDR_TYPE_ANY : (_bind6 ? IPADDR_TYPE_V6 : IPADDR_TYPE_V4) );
1585
+ _pcb = tcp_new_ip_type (_addr. type );
1564
1586
TCP_MUTEX_UNLOCK ();
1565
1587
if (!_pcb) {
1566
1588
log_e (" _pcb == NULL" );
1567
1589
return ;
1568
1590
}
1569
1591
1570
- ip_addr_t local_addr;
1571
- #if ESP_IDF_VERSION_MAJOR < 5
1572
- if (_bind6) { // _bind6 && _bind4 both at the same time is not supported on Arduino 2 in this lib API
1573
- local_addr.type = IPADDR_TYPE_V6;
1574
- memcpy (local_addr.u_addr .ip6 .addr , static_cast <const uint32_t *>(_addr6), sizeof (uint32_t ) * 4 );
1575
- } else {
1576
- local_addr.type = IPADDR_TYPE_V4;
1577
- local_addr.u_addr .ip4 .addr = _addr;
1578
- }
1579
- #else
1580
- _addr.to_ip_addr_t (&local_addr);
1581
- #endif
1582
- err = _tcp_bind (_pcb, &local_addr, _port);
1592
+ err = _tcp_bind (_pcb, &_addr, _port);
1583
1593
1584
1594
if (err != ERR_OK) {
1585
1595
_tcp_close (_pcb, -1 );
0 commit comments