Skip to content

Commit 71c705a

Browse files
committed
Implement remoteIP and remotePort in WiFiUDP
1 parent ea90d3c commit 71c705a

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎libraries/ESP8266WiFi/src/WiFiUdp.cpp‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C"
3636
#include "lwip/udp.h"
3737
#include "lwip/inet.h"
3838
#include "lwip/igmp.h"
39+
#include "lwip/mem.h"
3940
#include "include/UdpContext.h"
4041

4142
/* Constructor */
@@ -209,23 +210,19 @@ void WiFiUDP::flush()
209210
_ctx->flush();
210211
}
211212

212-
IPAddress WiFiUDP::remoteIP()
213+
IPAddress WiFiUDP::remoteIP()
213214
{
214-
uint8_t _remoteIp[4] = {0};
215-
uint8_t _remotePort[2] = {0};
215+
if (!_ctx)
216+
return IPAddress(0U);
216217

217-
// WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
218-
IPAddress ip(_remoteIp);
219-
return ip;
218+
return IPAddress(_ctx->getRemoteAddress());
220219
}
221220

222-
uint16_t WiFiUDP::remotePort()
221+
uint16_t WiFiUDP::remotePort()
223222
{
224-
uint8_t _remoteIp[4] = {0};
225-
uint8_t _remotePort[2] = {0};
223+
if (!_ctx)
224+
return 0;
226225

227-
// WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
228-
uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
229-
return port;
226+
return _ctx->getRemotePort();
230227
}
231228

‎libraries/ESP8266WiFi/src/include/UdpContext.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ class UdpContext
102102
return _rx_buf->len - _rx_buf_offset;
103103
}
104104

105+
uint32_t getRemoteAddress()
106+
{
107+
if (!_rx_buf)
108+
return 0;
109+
110+
struct ip_hdr* iphdr = (struct ip_hdr*) (((uint8_t*)_rx_buf->payload) - UDP_HLEN - IP_HLEN);
111+
return iphdr->src.addr;
112+
}
113+
114+
uint16_t getRemotePort()
115+
{
116+
if (!_rx_buf)
117+
return 0;
118+
119+
struct udp_hdr* udphdr = (struct udp_hdr*) (((uint8_t*)_rx_buf->payload) - UDP_HLEN);
120+
return ntohs(udphdr->src);
121+
}
122+
105123
bool next()
106124
{
107125
if (!_rx_buf)

‎libraries/ESP8266WiFi/src/include/lwipopts.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#define __LWIPOPTS_H__
4040

4141

42+
#define EBUF_LWIP 1
43+
#define LWIP_ESP 1
44+
#define EP_OFFSET 36
4245
/*
4346
-----------------------------------------------
4447
---------- Platform specific locking ----------
@@ -242,7 +245,7 @@
242245
* (requires the LWIP_TCP option)
243246
*/
244247
#ifndef MEMP_NUM_TCP_PCB
245-
#define MEMP_NUM_TCP_PCB 5
248+
#define MEMP_NUM_TCP_PCB (*((volatile uint32*)0x600011FC))
246249
#endif
247250

248251
/**

0 commit comments

Comments
 (0)