Skip to content

Commit fb0411b

Browse files
authored
Adapt for LibreTuya compatibility (#3)
1 parent 7c767c3 commit fb0411b

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"version": "1.2.2",
1414
"license": "LGPL-3.0",
1515
"frameworks": "arduino",
16-
"platforms": "espressif32",
16+
"platforms": ["espressif32", "libretuya"],
1717
"build": {
1818
"libCompatMode": 2
1919
}

src/AsyncTCP.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ extern "C"{
2929
#include "lwip/dns.h"
3030
#include "lwip/err.h"
3131
}
32+
#if CONFIG_ASYNC_TCP_USE_WDT
3233
#include "esp_task_wdt.h"
34+
#endif
3335

3436
/*
3537
* TCP/IP Event Task
@@ -238,7 +240,7 @@ static bool _start_async_task(){
238240
return false;
239241
}
240242
if(!_async_service_task_handle){
241-
customTaskCreateUniversal(_async_service_task, "async_tcp", 8192 * 2, NULL, 3, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE);
243+
customTaskCreateUniversal(_async_service_task, "async_tcp", CONFIG_ASYNC_TCP_STACK_SIZE, NULL, 3, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE);
242244
if(!_async_service_task_handle){
243245
return false;
244246
}
@@ -704,8 +706,12 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
704706
}
705707

706708
ip_addr_t addr;
709+
#if LWIP_IPV4 && LWIP_IPV6
707710
addr.type = IPADDR_TYPE_V4;
708711
addr.u_addr.ip4.addr = ip;
712+
#else
713+
addr.addr = ip;
714+
#endif
709715

710716
tcp_pcb* pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
711717
if (!pcb){
@@ -733,7 +739,11 @@ bool AsyncClient::connect(const char* host, uint16_t port){
733739

734740
err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcp_dns_found, this);
735741
if(err == ERR_OK) {
742+
#if LWIP_IPV4 && LWIP_IPV6
736743
return connect(IPAddress(addr.u_addr.ip4.addr), port);
744+
#else
745+
return connect(IPAddress(addr.addr), port);
746+
#endif
737747
} else if(err == ERR_INPROGRESS) {
738748
_connect_port = port;
739749
return true;
@@ -998,8 +1008,13 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
9981008
}
9991009

10001010
void AsyncClient::_dns_found(struct ip_addr *ipaddr){
1011+
#if LWIP_IPV4 && LWIP_IPV6
10011012
if(ipaddr && ipaddr->u_addr.ip4.addr){
10021013
connect(IPAddress(ipaddr->u_addr.ip4.addr), _connect_port);
1014+
#else
1015+
if (ipaddr && ipaddr->addr){
1016+
connect(IPAddress(ipaddr->addr), _connect_port);
1017+
#endif
10031018
} else {
10041019
if(_error_cb) {
10051020
_error_cb(_error_cb_arg, this, -55);
@@ -1088,7 +1103,11 @@ uint32_t AsyncClient::getRemoteAddress() {
10881103
if(!_pcb) {
10891104
return 0;
10901105
}
1106+
#if LWIP_IPV4 && LWIP_IPV6
10911107
return _pcb->remote_ip.u_addr.ip4.addr;
1108+
#else
1109+
return _pcb->remote_ip.addr;
1110+
#endif
10921111
}
10931112

10941113
uint16_t AsyncClient::getRemotePort() {
@@ -1102,7 +1121,11 @@ uint32_t AsyncClient::getLocalAddress() {
11021121
if(!_pcb) {
11031122
return 0;
11041123
}
1124+
#if LWIP_IPV4 && LWIP_IPV6
11051125
return _pcb->local_ip.u_addr.ip4.addr;
1126+
#else
1127+
return _pcb->local_ip.addr;
1128+
#endif
11061129
}
11071130

11081131
uint16_t AsyncClient::getLocalPort() {
@@ -1298,8 +1321,12 @@ void AsyncServer::begin(){
12981321
}
12991322

13001323
ip_addr_t local_addr;
1324+
#if LWIP_IPV4 && LWIP_IPV6
13011325
local_addr.type = IPADDR_TYPE_V4;
13021326
local_addr.u_addr.ip4.addr = (uint32_t) _addr;
1327+
#else
1328+
local_addr.addr = (uint32_t) _addr;
1329+
#endif
13031330
err = _tcp_bind(_pcb, &local_addr, _port);
13041331

13051332
if (err != ERR_OK) {

src/AsyncTCP.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,33 @@
2323
#define ASYNCTCP_H_
2424

2525
#include "IPAddress.h"
26-
#include "sdkconfig.h"
2726
#include <functional>
27+
28+
#ifndef LIBRETUYA
29+
#include "sdkconfig.h"
2830
extern "C" {
2931
#include "freertos/semphr.h"
3032
#include "lwip/pbuf.h"
3133
}
34+
#else
35+
extern "C" {
36+
#include <semphr.h>
37+
#include <lwip/pbuf.h>
38+
}
39+
#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 //any available core
40+
#define CONFIG_ASYNC_TCP_USE_WDT 0
41+
#endif
3242

3343
//If core is not defined, then we are running in Arduino or PIO
3444
#ifndef CONFIG_ASYNC_TCP_RUNNING_CORE
3545
#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 //any available core
3646
#define CONFIG_ASYNC_TCP_USE_WDT 1 //if enabled, adds between 33us and 200us per event
3747
#endif
3848

49+
#ifndef CONFIG_ASYNC_TCP_STACK_SIZE
50+
#define CONFIG_ASYNC_TCP_STACK_SIZE 8192 * 2
51+
#endif
52+
3953
class AsyncClient;
4054

4155
#define ASYNC_MAX_ACK_TIME 5000

0 commit comments

Comments
 (0)