Skip to content

Commit 2ae3787

Browse files
authored
Fixes libretiny (esphome#10)
1 parent d852ef9 commit 2ae3787

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

src/AsyncTCP.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port){
705705
return false;
706706
}
707707

708-
tcp_pcb* pcb = tcp_new_ip_type(addr.type);
708+
tcp_pcb* pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
709709
if (!pcb){
710710
log_e("pcb == NULL");
711711
return false;
@@ -722,19 +722,20 @@ bool AsyncClient::_connect(ip_addr_t addr, uint16_t port){
722722

723723
bool AsyncClient::connect(IPAddress ip, uint16_t port){
724724
ip_addr_t addr;
725-
addr.type = IPADDR_TYPE_V4;
726-
addr.u_addr.ip4.addr = ip;
725+
ip_addr_set_ip4_u32(&addr, ip);
727726

728727
return _connect(addr, port);
729728
}
730729

730+
#if LWIP_IPV6
731731
bool AsyncClient::connect(IPv6Address ip, uint16_t port){
732732
ip_addr_t addr;
733733
addr.type = IPADDR_TYPE_V6;
734734
memcpy(addr.u_addr.ip6.addr, static_cast<const uint32_t*>(ip), sizeof(uint32_t) * 4);
735735

736736
return _connect(addr, port);
737737
}
738+
#endif
738739

739740
bool AsyncClient::connect(const char* host, uint16_t port){
740741
ip_addr_t addr;
@@ -746,10 +747,10 @@ bool AsyncClient::connect(const char* host, uint16_t port){
746747

747748
err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcp_dns_found, this);
748749
if(err == ERR_OK) {
750+
#if LWIP_IPV6
749751
if(addr.type == IPADDR_TYPE_V6) {
750752
return connect(IPv6Address(addr.u_addr.ip6.addr), port);
751753
}
752-
#if LWIP_IPV6
753754
return connect(IPAddress(addr.u_addr.ip4.addr), port);
754755
#else
755756
return connect(IPAddress(addr.addr), port);
@@ -1018,10 +1019,12 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
10181019
}
10191020

10201021
void AsyncClient::_dns_found(struct ip_addr *ipaddr){
1021-
if(ipaddr && ipaddr->u_addr.ip4.addr){
1022-
connect(IPAddress(ipaddr->u_addr.ip4.addr), _connect_port);
1022+
if(ipaddr && IP_IS_V4(ipaddr)){
1023+
connect(IPAddress(ip_addr_get_ip4_u32(ipaddr)), _connect_port);
1024+
#if LWIP_IPV6
10231025
} else if(ipaddr && ipaddr->u_addr.ip6.addr){
10241026
connect(IPv6Address(ipaddr->u_addr.ip6.addr), _connect_port);
1027+
#endif
10251028
} else {
10261029
if(_error_cb) {
10271030
_error_cb(_error_cb_arg, this, -55);
@@ -1117,6 +1120,7 @@ uint32_t AsyncClient::getRemoteAddress() {
11171120
#endif
11181121
}
11191122

1123+
#if LWIP_IPV6
11201124
ip6_addr_t AsyncClient::getRemoteAddress6() {
11211125
if(!_pcb) {
11221126
ip6_addr_t nulladdr;
@@ -1126,6 +1130,24 @@ ip6_addr_t AsyncClient::getRemoteAddress6() {
11261130
return _pcb->remote_ip.u_addr.ip6;
11271131
}
11281132

1133+
ip6_addr_t AsyncClient::getLocalAddress6() {
1134+
if(!_pcb) {
1135+
ip6_addr_t nulladdr;
1136+
ip6_addr_set_zero(&nulladdr);
1137+
return nulladdr;
1138+
}
1139+
return _pcb->local_ip.u_addr.ip6;
1140+
}
1141+
1142+
IPv6Address AsyncClient::remoteIP6() {
1143+
return IPv6Address(getRemoteAddress6().addr);
1144+
}
1145+
1146+
IPv6Address AsyncClient::localIP6() {
1147+
return IPv6Address(getLocalAddress6().addr);
1148+
}
1149+
#endif
1150+
11291151
uint16_t AsyncClient::getRemotePort() {
11301152
if(!_pcb) {
11311153
return 0;
@@ -1144,15 +1166,6 @@ uint32_t AsyncClient::getLocalAddress() {
11441166
#endif
11451167
}
11461168

1147-
ip6_addr_t AsyncClient::getLocalAddress6() {
1148-
if(!_pcb) {
1149-
ip6_addr_t nulladdr;
1150-
ip6_addr_set_zero(&nulladdr);
1151-
return nulladdr;
1152-
}
1153-
return _pcb->local_ip.u_addr.ip6;
1154-
}
1155-
11561169
uint16_t AsyncClient::getLocalPort() {
11571170
if(!_pcb) {
11581171
return 0;
@@ -1164,10 +1177,6 @@ IPAddress AsyncClient::remoteIP() {
11641177
return IPAddress(getRemoteAddress());
11651178
}
11661179

1167-
IPv6Address AsyncClient::remoteIP6() {
1168-
return IPv6Address(getRemoteAddress6().addr);
1169-
}
1170-
11711180
uint16_t AsyncClient::remotePort() {
11721181
return getRemotePort();
11731182
}
@@ -1176,9 +1185,6 @@ IPAddress AsyncClient::localIP() {
11761185
return IPAddress(getLocalAddress());
11771186
}
11781187

1179-
IPv6Address AsyncClient::localIP6() {
1180-
return IPv6Address(getLocalAddress6().addr);
1181-
}
11821188

11831189
uint16_t AsyncClient::localPort() {
11841190
return getLocalPort();
@@ -1377,9 +1383,10 @@ void AsyncServer::begin(){
13771383
}
13781384

13791385
ip_addr_t local_addr;
1380-
local_addr.type = bind_type;
1386+
ip_addr_set_ip4_u32(&local_addr, _addr);
1387+
/* local_addr.type = bind_type;
13811388
local_addr.u_addr.ip4.addr = (uint32_t) _addr;
1382-
memcpy(local_addr.u_addr.ip6.addr, static_cast<const uint32_t*>(_addr6), sizeof(uint32_t) * 4);
1389+
memcpy(local_addr.u_addr.ip6.addr, static_cast<const uint32_t*>(_addr6), sizeof(uint32_t) * 4); */
13831390
err = _tcp_bind(_pcb, &local_addr, _port);
13841391

13851392
if (err != ERR_OK) {

src/AsyncTCP.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
#include "IPAddress.h"
2626
#include "IPv6Address.h"
2727
#include <functional>
28+
#include "lwip/ip_addr.h"
29+
#include "lwip/ip6_addr.h"
2830

2931
#ifndef LIBRETINY
3032
#include "sdkconfig.h"
3133
extern "C" {
3234
#include "freertos/semphr.h"
3335
#include "lwip/pbuf.h"
34-
#include "lwip/ip_addr.h"
35-
#include "lwip/ip6_addr.h"
3636
}
3737
#else
3838
extern "C" {
@@ -118,18 +118,20 @@ class AsyncClient {
118118
bool getNoDelay();
119119

120120
uint32_t getRemoteAddress();
121-
ip6_addr_t getRemoteAddress6();
122121
uint16_t getRemotePort();
123122
uint32_t getLocalAddress();
124-
ip6_addr_t getLocalAddress6();
125123
uint16_t getLocalPort();
124+
#if LWIP_IPV6
125+
ip6_addr_t getRemoteAddress6();
126+
ip6_addr_t getLocalAddress6();
127+
IPv6Address remoteIP6();
128+
IPv6Address localIP6();
129+
#endif
126130

127131
//compatibility
128132
IPAddress remoteIP();
129-
IPv6Address remoteIP6();
130133
uint16_t remotePort();
131134
IPAddress localIP();
132-
IPv6Address localIP6();
133135
uint16_t localPort();
134136

135137
void onConnect(AcConnectHandler cb, void* arg = 0); //on successful connect

0 commit comments

Comments
 (0)