Skip to content

Commit

Permalink
Arduino 3 / ESP-IDF 5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Apr 20, 2024
1 parent ab88e23 commit 226a581
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
61 changes: 55 additions & 6 deletions src/AsyncTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typedef struct {
};
} lwip_event_packet_t;

static xQueueHandle _async_queue;
static QueueHandle_t _async_queue;
static TaskHandle_t _async_service_task_handle = NULL;


Expand Down Expand Up @@ -722,12 +722,19 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port){

bool AsyncClient::connect(IPAddress ip, uint16_t port){
ip_addr_t addr;
ip_addr_set_ip4_u32(&addr, ip);
#if ESP_IDF_VERSION_MAJOR < 5
// ip_addr_set_ip4_u32(&addr, ip);
addr.u_addr.ip4.addr = ip;
addr.type = IPADDR_TYPE_V4;
ip_clear_no4(&addr);
#else
ip.to_ip_addr_t(&addr);
#endif

return _connect(addr, port);
}

#if LWIP_IPV6
#if LWIP_IPV6 && ESP_IDF_VERSION_MAJOR < 5
bool AsyncClient::connect(IPv6Address ip, uint16_t port){
ip_addr_t addr;
addr.type = IPADDR_TYPE_V6;
Expand All @@ -747,13 +754,17 @@ bool AsyncClient::connect(const char* host, uint16_t port){

err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcp_dns_found, this);
if(err == ERR_OK) {
#if ESP_IDF_VERSION_MAJOR < 5
#if LWIP_IPV6
if(addr.type == IPADDR_TYPE_V6) {
return connect(IPv6Address(addr.u_addr.ip6.addr), port);
}
return connect(IPAddress(addr.u_addr.ip4.addr), port);
#else
return connect(IPAddress(addr.addr), port);
#endif
#else
return _connect(addr, port);
#endif
} else if(err == ERR_INPROGRESS) {
_connect_port = port;
Expand Down Expand Up @@ -1019,11 +1030,16 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
}

void AsyncClient::_dns_found(struct ip_addr *ipaddr){
#if ESP_IDF_VERSION_MAJOR < 5
if(ipaddr && IP_IS_V4(ipaddr)){
connect(IPAddress(ip_addr_get_ip4_u32(ipaddr)), _connect_port);
#if LWIP_IPV6
} else if(ipaddr && ipaddr->u_addr.ip6.addr){
connect(IPv6Address(ipaddr->u_addr.ip6.addr), _connect_port);
#endif
#else
if(ipaddr) {
connect(IPAddress(ipaddr), _connect_port);
#endif
} else {
if(_error_cb) {
Expand Down Expand Up @@ -1138,14 +1154,23 @@ ip6_addr_t AsyncClient::getLocalAddress6() {
}
return _pcb->local_ip.u_addr.ip6;
}

#if ESP_IDF_VERSION_MAJOR < 5
IPv6Address AsyncClient::remoteIP6() {
return IPv6Address(getRemoteAddress6().addr);
}

IPv6Address AsyncClient::localIP6() {
return IPv6Address(getLocalAddress6().addr);
}
#else
IPAddress AsyncClient::remoteIP6() {
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->remote_ip)) : IPAddress(IPType::IPv6);
}

IPAddress AsyncClient::localIP6() {
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->local_ip)) : IPAddress(IPType::IPv6);
}
#endif
#endif

uint16_t AsyncClient::getRemotePort() {
Expand Down Expand Up @@ -1174,15 +1199,23 @@ uint16_t AsyncClient::getLocalPort() {
}

IPAddress AsyncClient::remoteIP() {
#if ESP_IDF_VERSION_MAJOR < 5
return IPAddress(getRemoteAddress());
#else
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->remote_ip)) : IPAddress();
#endif
}

uint16_t AsyncClient::remotePort() {
return getRemotePort();
}

IPAddress AsyncClient::localIP() {
#if ESP_IDF_VERSION_MAJOR < 5
return IPAddress(getLocalAddress());
#else
return _pcb ? IPAddress(dynamic_cast<const ip_addr_t*>(&_pcb->local_ip)) : IPAddress();
#endif
}


Expand Down Expand Up @@ -1318,14 +1351,20 @@ int8_t AsyncClient::_s_connected(void * arg, void * pcb, int8_t err){

AsyncServer::AsyncServer(IPAddress addr, uint16_t port)
: _port(port)
#if ESP_IDF_VERSION_MAJOR < 5
, _bind4(true)
#else
, _bind4(addr.type() != IPType::IPv6)
, _bind6(addr.type() == IPType::IPv6)
#endif
, _addr(addr)
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
, _connect_cb_arg(0)
{}

#if ESP_IDF_VERSION_MAJOR < 5
AsyncServer::AsyncServer(IPv6Address addr, uint16_t port)
: _port(port)
, _bind6(true)
Expand All @@ -1335,13 +1374,16 @@ AsyncServer::AsyncServer(IPv6Address addr, uint16_t port)
, _connect_cb(0)
, _connect_cb_arg(0)
{}
#endif

AsyncServer::AsyncServer(uint16_t port)
: _port(port)
, _bind4(true)
, _bind6(true)
, _addr((uint32_t) IPADDR_ANY)
#if ESP_IDF_VERSION_MAJOR < 5
, _addr6()
#endif
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
Expand Down Expand Up @@ -1383,10 +1425,17 @@ void AsyncServer::begin(){
}

ip_addr_t local_addr;
ip_addr_set_ip4_u32(&local_addr, _addr);
/* local_addr.type = bind_type;
#if ESP_IDF_VERSION_MAJOR < 5
// ip_addr_set_ip4_u32(&local_addr, _addr);
local_addr.u_addr.ip4.addr = _addr;
local_addr.type = IPADDR_TYPE_V4;
ip_clear_no4(&local_addr);
/* local_addr.type = bind_type;
local_addr.u_addr.ip4.addr = (uint32_t) _addr;
memcpy(local_addr.u_addr.ip6.addr, static_cast<const uint32_t*>(_addr6), sizeof(uint32_t) * 4); */
#else
_addr.to_ip_addr_t(&local_addr);
#endif
err = _tcp_bind(_pcb, &local_addr, _port);

if (err != ERR_OK) {
Expand Down
13 changes: 13 additions & 0 deletions src/AsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#define ASYNCTCP_H_

#include "IPAddress.h"
#if ESP_IDF_VERSION_MAJOR < 5
#include "IPv6Address.h"
#endif
#include <functional>
#include "lwip/ip_addr.h"
#include "lwip/ip6_addr.h"
Expand Down Expand Up @@ -83,7 +85,9 @@ class AsyncClient {
return !(*this == other);
}
bool connect(IPAddress ip, uint16_t port);
#if ESP_IDF_VERSION_MAJOR < 5
bool connect(IPv6Address ip, uint16_t port);
#endif
bool connect(const char *host, uint16_t port);
void close(bool now = false);
void stop();
Expand Down Expand Up @@ -124,8 +128,13 @@ class AsyncClient {
#if LWIP_IPV6
ip6_addr_t getRemoteAddress6();
ip6_addr_t getLocalAddress6();
#if ESP_IDF_VERSION_MAJOR < 5
IPv6Address remoteIP6();
IPv6Address localIP6();
#else
IPAddress remoteIP6();
IPAddress localIP6();
#endif
#endif

//compatibility
Expand Down Expand Up @@ -214,7 +223,9 @@ class AsyncClient {
class AsyncServer {
public:
AsyncServer(IPAddress addr, uint16_t port);
#if ESP_IDF_VERSION_MAJOR < 5
AsyncServer(IPv6Address addr, uint16_t port);
#endif
AsyncServer(uint16_t port);
~AsyncServer();
void onClient(AcConnectHandler cb, void* arg);
Expand All @@ -233,7 +244,9 @@ class AsyncServer {
bool _bind4 = false;
bool _bind6 = false;
IPAddress _addr;
#if ESP_IDF_VERSION_MAJOR < 5
IPv6Address _addr6;
#endif
bool _noDelay;
tcp_pcb* _pcb;
AcConnectHandler _connect_cb;
Expand Down

0 comments on commit 226a581

Please sign in to comment.