From 2cd2e47ae6c94e502459c8a594b90a1422e31c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 28 May 2025 11:12:43 +0200 Subject: [PATCH 1/6] Add LibreTiny support macros --- src/AsyncTCP.cpp | 6 ++++++ src/AsyncTCP.h | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 011318ae..7eb62cbc 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -3,6 +3,7 @@ #include "AsyncTCP.h" +#ifdef ESP32 #include #ifdef ARDUINO @@ -22,6 +23,11 @@ static unsigned long millis() { return (unsigned long)(esp_timer_get_time() / 1000ULL); } #endif +#endif + +#ifdef LIBRETINY +#include +#endif extern "C" { #include "lwip/dns.h" diff --git a/src/AsyncTCP.h b/src/AsyncTCP.h index 0bc7b9df..aa495e30 100644 --- a/src/AsyncTCP.h +++ b/src/AsyncTCP.h @@ -7,7 +7,11 @@ #include "AsyncTCPVersion.h" #define ASYNCTCP_FORK_ESP32Async +#ifdef ESP32 #include +#else +#define ESP_IDF_VERSION_MAJOR (0) +#endif #ifdef ARDUINO #include "IPAddress.h" @@ -29,9 +33,11 @@ extern "C" { #else extern "C" { #include +#include #include } #define CONFIG_ASYNC_TCP_RUNNING_CORE -1 // any available core +#define CONFIG_FREERTOS_UNICORE 1 #endif // If core is not defined, then we are running in Arduino or PIO From 50b244d8891bc245021b03ad59a7e8a431024457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 28 May 2025 11:13:48 +0200 Subject: [PATCH 2/6] Support non-IPv6 lwIP --- src/AsyncTCP.cpp | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 7eb62cbc..792a2b63 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -843,7 +843,11 @@ bool AsyncClient::connect(ip_addr_t addr, uint16_t port) { tcp_pcb *pcb; { tcp_core_guard tcg; +#if LWIP_IPV4 && LWIP_IPV6 pcb = tcp_new_ip_type(addr.type); +#else + pcb = tcp_new_ip_type(IPADDR_TYPE_V4); +#endif if (!pcb) { log_e("pcb == NULL"); return false; @@ -863,8 +867,12 @@ bool AsyncClient::connect(ip_addr_t addr, uint16_t port) { bool AsyncClient::connect(const IPAddress &ip, uint16_t port) { ip_addr_t addr; #if ESP_IDF_VERSION_MAJOR < 5 +#if LWIP_IPV4 && LWIP_IPV6 addr.u_addr.ip4.addr = ip; addr.type = IPADDR_TYPE_V4; +#else + addr.addr = ip; +#endif #else ip.to_ip_addr_t(&addr); #endif @@ -1349,9 +1357,16 @@ uint16_t AsyncClient::getLocalPort() const { } ip4_addr_t AsyncClient::getRemoteAddress4() const { +#if LWIP_IPV4 && LWIP_IPV6 if (_pcb && _pcb->remote_ip.type == IPADDR_TYPE_V4) { return _pcb->remote_ip.u_addr.ip4; - } else { + } +#else + if (_pcb) { + return _pcb->remote_ip; + } +#endif + else { ip4_addr_t nulladdr; ip4_addr_set_zero(&nulladdr); return nulladdr; @@ -1359,9 +1374,16 @@ ip4_addr_t AsyncClient::getRemoteAddress4() const { } ip4_addr_t AsyncClient::getLocalAddress4() const { +#if LWIP_IPV4 && LWIP_IPV6 if (_pcb && _pcb->local_ip.type == IPADDR_TYPE_V4) { return _pcb->local_ip.u_addr.ip4; - } else { + } +#else + if (_pcb) { + return _pcb->local_ip; + } +#endif + else { ip4_addr_t nulladdr; ip4_addr_set_zero(&nulladdr); return nulladdr; @@ -1492,15 +1514,21 @@ AsyncServer::AsyncServer(ip_addr_t addr, uint16_t port) #ifdef ARDUINO AsyncServer::AsyncServer(IPAddress addr, uint16_t port) : _port(port), _noDelay(false), _pcb(0), _connect_cb(0), _connect_cb_arg(0) { #if ESP_IDF_VERSION_MAJOR < 5 +#if LWIP_IPV4 && LWIP_IPV6 _addr.type = IPADDR_TYPE_V4; _addr.u_addr.ip4.addr = addr; +#else + _addr.addr = addr; +#endif #else addr.to_ip_addr_t(&_addr); #endif } -#if ESP_IDF_VERSION_MAJOR < 5 +#if ESP_IDF_VERSION_MAJOR < 5 && __has_include() && LWIP_IPV6 AsyncServer::AsyncServer(IPv6Address addr, uint16_t port) : _port(port), _noDelay(false), _pcb(0), _connect_cb(0), _connect_cb_arg(0) { +#if LWIP_IPV4 && LWIP_IPV6 _addr.type = IPADDR_TYPE_V6; +#endif auto ipaddr = static_cast(addr); _addr = IPADDR6_INIT(ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); } @@ -1508,8 +1536,12 @@ AsyncServer::AsyncServer(IPv6Address addr, uint16_t port) : _port(port), _noDela #endif AsyncServer::AsyncServer(uint16_t port) : _port(port), _noDelay(false), _pcb(0), _connect_cb(0), _connect_cb_arg(0) { +#if LWIP_IPV4 && LWIP_IPV6 _addr.type = IPADDR_TYPE_ANY; _addr.u_addr.ip4.addr = INADDR_ANY; +#else + _addr.addr = IPADDR_TYPE_ANY; +#endif } AsyncServer::~AsyncServer() { @@ -1533,7 +1565,11 @@ void AsyncServer::begin() { int8_t err; { tcp_core_guard tcg; +#if LWIP_IPV4 && LWIP_IPV6 _pcb = tcp_new_ip_type(_addr.type); +#else + _pcb = tcp_new_ip_type(IPADDR_TYPE_V4); +#endif } if (!_pcb) { log_e("_pcb == NULL"); From 6639a95f84d9f881bc2a57bc74fcb09c31cdbfaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 28 May 2025 11:39:27 +0200 Subject: [PATCH 3/6] Replace ESP32 ifdef --- src/AsyncTCP.cpp | 2 +- src/AsyncTCP.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 792a2b63..58b605fe 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -3,7 +3,7 @@ #include "AsyncTCP.h" -#ifdef ESP32 +#ifndef LIBRETINY #include #ifdef ARDUINO diff --git a/src/AsyncTCP.h b/src/AsyncTCP.h index aa495e30..1e34144b 100644 --- a/src/AsyncTCP.h +++ b/src/AsyncTCP.h @@ -7,10 +7,10 @@ #include "AsyncTCPVersion.h" #define ASYNCTCP_FORK_ESP32Async -#ifdef ESP32 -#include -#else +#ifdef LIBRETINY #define ESP_IDF_VERSION_MAJOR (0) +#else +#include #endif #ifdef ARDUINO From 2ca19ecfaa2393d230844f6b782d2b9a54beafac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 28 May 2025 12:44:16 +0200 Subject: [PATCH 4/6] Move LibreTiny macros to .cpp --- src/AsyncTCP.cpp | 8 ++++++++ src/AsyncTCP.h | 5 +---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 58b605fe..4fa07843 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -27,6 +27,13 @@ static unsigned long millis() { #ifdef LIBRETINY #include +// LibreTiny does not support IDF - disable code that expects it to be available +#define ESP_IDF_VERSION_MAJOR (0) +// xTaskCreatePinnedToCore is not available, force single-core operation +#define CONFIG_FREERTOS_UNICORE 1 +// ESP watchdog is not available +#undef CONFIG_ASYNC_TCP_USE_WDT +#define CONFIG_ASYNC_TCP_USE_WDT 0 #endif extern "C" { @@ -868,6 +875,7 @@ bool AsyncClient::connect(const IPAddress &ip, uint16_t port) { ip_addr_t addr; #if ESP_IDF_VERSION_MAJOR < 5 #if LWIP_IPV4 && LWIP_IPV6 + // if both IPv4 and IPv6 are enabled, ip_addr_t has a union field and the address type addr.u_addr.ip4.addr = ip; addr.type = IPADDR_TYPE_V4; #else diff --git a/src/AsyncTCP.h b/src/AsyncTCP.h index 1e34144b..aabc7e88 100644 --- a/src/AsyncTCP.h +++ b/src/AsyncTCP.h @@ -7,9 +7,7 @@ #include "AsyncTCPVersion.h" #define ASYNCTCP_FORK_ESP32Async -#ifdef LIBRETINY -#define ESP_IDF_VERSION_MAJOR (0) -#else +#ifndef LIBRETINY #include #endif @@ -37,7 +35,6 @@ extern "C" { #include } #define CONFIG_ASYNC_TCP_RUNNING_CORE -1 // any available core -#define CONFIG_FREERTOS_UNICORE 1 #endif // If core is not defined, then we are running in Arduino or PIO From 23b6e27ffaab44086143b7f27b57e30f181696c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 28 May 2025 12:59:22 +0200 Subject: [PATCH 5/6] Add LibreTiny to CI --- .github/workflows/ci.yml | 5 +++++ platformio.ini | 9 +++++++++ src/AsyncTCP.h | 1 - 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dcc560e3..f86d7f7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,6 +78,11 @@ jobs: - env: ci-arduino-3-latest board: esp32-c6-devkitc-1 + - env: ci-arduino-libretiny + board: generic-bk7231n-qfn32-tuya + - env: ci-arduino-libretiny + board: generic-rtl8710bn-2mb-788k + steps: - name: Checkout uses: actions/checkout@v4 diff --git a/platformio.ini b/platformio.ini index 5ae31c8b..689e6a0f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -43,3 +43,12 @@ board = ${sysenv.PIO_BOARD} [env:ci-arduino-3-latest] platform = https://github.com/pioarduino/platform-espressif32/releases/download/54.03.20-rc2/platform-espressif32.zip board = ${sysenv.PIO_BOARD} + +[env:ci-arduino-libretiny] +platform = libretiny +board = ${sysenv.PIO_BOARD} +build_flags = + ${env.build_flags} + -Wno-unused-parameter + -Wno-unused-variable + -Wno-missing-field-initializers diff --git a/src/AsyncTCP.h b/src/AsyncTCP.h index aabc7e88..9d098734 100644 --- a/src/AsyncTCP.h +++ b/src/AsyncTCP.h @@ -34,7 +34,6 @@ extern "C" { #include #include } -#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 // any available core #endif // If core is not defined, then we are running in Arduino or PIO From 33c5bcb3d28614e53483432a3517c3e8681ac199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 28 May 2025 15:28:49 +0200 Subject: [PATCH 6/6] Fix INADDR_ANY typo --- src/AsyncTCP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 4fa07843..e9839e72 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -1548,7 +1548,7 @@ AsyncServer::AsyncServer(uint16_t port) : _port(port), _noDelay(false), _pcb(0), _addr.type = IPADDR_TYPE_ANY; _addr.u_addr.ip4.addr = INADDR_ANY; #else - _addr.addr = IPADDR_TYPE_ANY; + _addr.addr = INADDR_ANY; #endif } @@ -1576,7 +1576,7 @@ void AsyncServer::begin() { #if LWIP_IPV4 && LWIP_IPV6 _pcb = tcp_new_ip_type(_addr.type); #else - _pcb = tcp_new_ip_type(IPADDR_TYPE_V4); + _pcb = tcp_new_ip_type(IPADDR_TYPE_ANY); #endif } if (!_pcb) {