Skip to content

Add LibreTiny support #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
56 changes: 53 additions & 3 deletions src/AsyncTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "AsyncTCP.h"

#ifndef LIBRETINY
#include <esp_log.h>

#ifdef ARDUINO
Expand All @@ -22,6 +23,18 @@ static unsigned long millis() {
return (unsigned long)(esp_timer_get_time() / 1000ULL);
}
#endif
#endif

#ifdef LIBRETINY
#include <Arduino.h>
// 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" {
#include "lwip/dns.h"
Expand Down Expand Up @@ -837,7 +850,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;
Expand All @@ -857,8 +874,13 @@ 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
// 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
addr.addr = ip;
#endif
#else
ip.to_ip_addr_t(&addr);
#endif
Expand Down Expand Up @@ -1343,19 +1365,33 @@ 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;
}
}

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;
Expand Down Expand Up @@ -1486,24 +1522,34 @@ 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(<IPv6Address.h>) && 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<const uint32_t *>(addr);
_addr = IPADDR6_INIT(ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
}
#endif
#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 = INADDR_ANY;
#endif
}

AsyncServer::~AsyncServer() {
Expand All @@ -1527,7 +1573,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_ANY);
#endif
}
if (!_pcb) {
log_e("_pcb == NULL");
Expand Down
4 changes: 3 additions & 1 deletion src/AsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "AsyncTCPVersion.h"
#define ASYNCTCP_FORK_ESP32Async

#ifndef LIBRETINY
#include <esp_idf_version.h>
#endif

#ifdef ARDUINO
#include "IPAddress.h"
Expand All @@ -29,9 +31,9 @@ extern "C" {
#else
extern "C" {
#include <lwip/pbuf.h>
#include <FreeRTOS.h>
#include <semphr.h>
}
#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 // any available core
#endif

// If core is not defined, then we are running in Arduino or PIO
Expand Down
Loading