Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 45 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,11 @@ static unsigned long millis() {
return (unsigned long)(esp_timer_get_time() / 1000ULL);
}
#endif
#endif

#ifdef LIBRETINY
#include <Arduino.h>
#endif

extern "C" {
#include "lwip/dns.h"
Expand Down Expand Up @@ -837,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;
Expand All @@ -857,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
Expand Down Expand Up @@ -1343,19 +1357,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 +1514,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 = IPADDR_TYPE_ANY;
#endif
}

AsyncServer::~AsyncServer() {
Expand All @@ -1527,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");
Expand Down
6 changes: 6 additions & 0 deletions src/AsyncTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#include "AsyncTCPVersion.h"
#define ASYNCTCP_FORK_ESP32Async

#ifdef LIBRETINY
#define ESP_IDF_VERSION_MAJOR (0)
#else
#include <esp_idf_version.h>
#endif

#ifdef ARDUINO
#include "IPAddress.h"
Expand All @@ -29,9 +33,11 @@ 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
#define CONFIG_FREERTOS_UNICORE 1
#endif

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