Skip to content

Commit e0ea42c

Browse files
Merge pull request esphome#6 from mathieucarbou/arduino-3
Arduino 3 / ESP IDF 5 compatibility
2 parents ab88e23 + c4cb765 commit e0ea42c

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/AsyncTCP.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,16 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port){
722722

723723
bool AsyncClient::connect(IPAddress ip, uint16_t port){
724724
ip_addr_t addr;
725+
#if ESP_IDF_VERSION_MAJOR < 5
725726
ip_addr_set_ip4_u32(&addr, ip);
727+
#else
728+
ip.to_ip_addr_t(&addr);
729+
#endif
726730

727731
return _connect(addr, port);
728732
}
729733

730-
#if LWIP_IPV6
734+
#if LWIP_IPV6 && ESP_IDF_VERSION_MAJOR < 5
731735
bool AsyncClient::connect(IPv6Address ip, uint16_t port){
732736
ip_addr_t addr;
733737
addr.type = IPADDR_TYPE_V6;
@@ -747,13 +751,17 @@ bool AsyncClient::connect(const char* host, uint16_t port){
747751

748752
err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcp_dns_found, this);
749753
if(err == ERR_OK) {
754+
#if ESP_IDF_VERSION_MAJOR < 5
750755
#if LWIP_IPV6
751756
if(addr.type == IPADDR_TYPE_V6) {
752757
return connect(IPv6Address(addr.u_addr.ip6.addr), port);
753758
}
754759
return connect(IPAddress(addr.u_addr.ip4.addr), port);
755760
#else
756761
return connect(IPAddress(addr.addr), port);
762+
#endif
763+
#else
764+
return _connect(addr, port);
757765
#endif
758766
} else if(err == ERR_INPROGRESS) {
759767
_connect_port = port;
@@ -1019,11 +1027,16 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
10191027
}
10201028

10211029
void AsyncClient::_dns_found(struct ip_addr *ipaddr){
1030+
#if ESP_IDF_VERSION_MAJOR < 5
10221031
if(ipaddr && IP_IS_V4(ipaddr)){
10231032
connect(IPAddress(ip_addr_get_ip4_u32(ipaddr)), _connect_port);
10241033
#if LWIP_IPV6
10251034
} else if(ipaddr && ipaddr->u_addr.ip6.addr){
10261035
connect(IPv6Address(ipaddr->u_addr.ip6.addr), _connect_port);
1036+
#endif
1037+
#else
1038+
if(ipaddr) {
1039+
connect(IPAddress(ipaddr), _connect_port);
10271040
#endif
10281041
} else {
10291042
if(_error_cb) {
@@ -1138,14 +1151,23 @@ ip6_addr_t AsyncClient::getLocalAddress6() {
11381151
}
11391152
return _pcb->local_ip.u_addr.ip6;
11401153
}
1141-
1154+
#if ESP_IDF_VERSION_MAJOR < 5
11421155
IPv6Address AsyncClient::remoteIP6() {
11431156
return IPv6Address(getRemoteAddress6().addr);
11441157
}
11451158

11461159
IPv6Address AsyncClient::localIP6() {
11471160
return IPv6Address(getLocalAddress6().addr);
11481161
}
1162+
#else
1163+
IPAddress AsyncClient::remoteIP6() {
1164+
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->remote_ip)) : IPAddress(IPType::IPv6);
1165+
}
1166+
1167+
IPAddress AsyncClient::localIP6() {
1168+
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->local_ip)) : IPAddress(IPType::IPv6);
1169+
}
1170+
#endif
11491171
#endif
11501172

11511173
uint16_t AsyncClient::getRemotePort() {
@@ -1174,15 +1196,23 @@ uint16_t AsyncClient::getLocalPort() {
11741196
}
11751197

11761198
IPAddress AsyncClient::remoteIP() {
1199+
#if ESP_IDF_VERSION_MAJOR < 5
11771200
return IPAddress(getRemoteAddress());
1201+
#else
1202+
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->remote_ip)) : IPAddress();
1203+
#endif
11781204
}
11791205

11801206
uint16_t AsyncClient::remotePort() {
11811207
return getRemotePort();
11821208
}
11831209

11841210
IPAddress AsyncClient::localIP() {
1211+
#if ESP_IDF_VERSION_MAJOR < 5
11851212
return IPAddress(getLocalAddress());
1213+
#else
1214+
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->local_ip)) : IPAddress();
1215+
#endif
11861216
}
11871217

11881218

@@ -1318,14 +1348,20 @@ int8_t AsyncClient::_s_connected(void * arg, void * pcb, int8_t err){
13181348

13191349
AsyncServer::AsyncServer(IPAddress addr, uint16_t port)
13201350
: _port(port)
1351+
#if ESP_IDF_VERSION_MAJOR < 5
13211352
, _bind4(true)
1353+
#else
1354+
, _bind4(addr.type() != IPType::IPv6)
1355+
, _bind6(addr.type() == IPType::IPv6)
1356+
#endif
13221357
, _addr(addr)
13231358
, _noDelay(false)
13241359
, _pcb(0)
13251360
, _connect_cb(0)
13261361
, _connect_cb_arg(0)
13271362
{}
13281363

1364+
#if ESP_IDF_VERSION_MAJOR < 5
13291365
AsyncServer::AsyncServer(IPv6Address addr, uint16_t port)
13301366
: _port(port)
13311367
, _bind6(true)
@@ -1335,13 +1371,16 @@ AsyncServer::AsyncServer(IPv6Address addr, uint16_t port)
13351371
, _connect_cb(0)
13361372
, _connect_cb_arg(0)
13371373
{}
1374+
#endif
13381375

13391376
AsyncServer::AsyncServer(uint16_t port)
13401377
: _port(port)
13411378
, _bind4(true)
13421379
, _bind6(true)
13431380
, _addr((uint32_t) IPADDR_ANY)
1381+
#if ESP_IDF_VERSION_MAJOR < 5
13441382
, _addr6()
1383+
#endif
13451384
, _noDelay(false)
13461385
, _pcb(0)
13471386
, _connect_cb(0)

src/AsyncTCP.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#define ASYNCTCP_H_
2424

2525
#include "IPAddress.h"
26+
#if ESP_IDF_VERSION_MAJOR < 5
2627
#include "IPv6Address.h"
28+
#endif
2729
#include <functional>
2830
#include "lwip/ip_addr.h"
2931
#include "lwip/ip6_addr.h"
@@ -83,7 +85,9 @@ class AsyncClient {
8385
return !(*this == other);
8486
}
8587
bool connect(IPAddress ip, uint16_t port);
88+
#if ESP_IDF_VERSION_MAJOR < 5
8689
bool connect(IPv6Address ip, uint16_t port);
90+
#endif
8791
bool connect(const char *host, uint16_t port);
8892
void close(bool now = false);
8993
void stop();
@@ -124,8 +128,13 @@ class AsyncClient {
124128
#if LWIP_IPV6
125129
ip6_addr_t getRemoteAddress6();
126130
ip6_addr_t getLocalAddress6();
131+
#if ESP_IDF_VERSION_MAJOR < 5
127132
IPv6Address remoteIP6();
128133
IPv6Address localIP6();
134+
#else
135+
IPAddress remoteIP6();
136+
IPAddress localIP6();
137+
#endif
129138
#endif
130139

131140
//compatibility
@@ -214,7 +223,9 @@ class AsyncClient {
214223
class AsyncServer {
215224
public:
216225
AsyncServer(IPAddress addr, uint16_t port);
226+
#if ESP_IDF_VERSION_MAJOR < 5
217227
AsyncServer(IPv6Address addr, uint16_t port);
228+
#endif
218229
AsyncServer(uint16_t port);
219230
~AsyncServer();
220231
void onClient(AcConnectHandler cb, void* arg);
@@ -233,7 +244,9 @@ class AsyncServer {
233244
bool _bind4 = false;
234245
bool _bind6 = false;
235246
IPAddress _addr;
247+
#if ESP_IDF_VERSION_MAJOR < 5
236248
IPv6Address _addr6;
249+
#endif
237250
bool _noDelay;
238251
tcp_pcb* _pcb;
239252
AcConnectHandler _connect_cb;

0 commit comments

Comments
 (0)