Skip to content

Commit ae2bc12

Browse files
committed
some UdpContext fixes, add rx callback support
Set udp destination address, port, and multicast TTL only on send. Fix read method to return -1 if buffer is empty.
1 parent 674ddbe commit ae2bc12

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiUdp.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
*/
2222

2323
#define LWIP_INTERNAL
24-
24+
#include <functional>
25+
2526
extern "C"
2627
{
2728
#include "include/wl_definitions.h"

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/include/UdpContext.h

+45-12
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ extern "C" void esp_schedule();
2828

2929
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
3030
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
31+
3132
class UdpContext
3233
{
3334
public:
35+
36+
typedef std::function<void(void)> rxhandler_t;
37+
3438
UdpContext()
3539
: _pcb(0)
3640
, _rx_buf(0)
@@ -40,8 +44,11 @@ class UdpContext
4044
, _tx_buf_head(0)
4145
, _tx_buf_cur(0)
4246
, _tx_buf_offset(0)
47+
, _multicast_ttl(1)
48+
, _dest_port(0)
4349
{
4450
_pcb = udp_new();
51+
_dest_addr.addr = 0;
4552
}
4653

4754
~UdpContext()
@@ -79,8 +86,9 @@ class UdpContext
7986

8087
bool connect(ip_addr_t addr, uint16_t port)
8188
{
82-
err_t err = udp_connect(_pcb, &addr, port);
83-
return err == ERR_OK;
89+
_dest_addr = addr;
90+
_dest_port = port;
91+
return true;
8492
}
8593

8694
bool listen(ip_addr_t addr, uint16_t port)
@@ -107,7 +115,13 @@ class UdpContext
107115
// newer versions of lwip have an additional field (mcast_ttl) for this purpose
108116
// and a macro to set it instead of direct field access
109117
// udp_set_multicast_ttl(_pcb, ttl);
110-
_pcb->ttl = ttl;
118+
_multicast_ttl = ttl;
119+
}
120+
121+
// warning: handler is called from tcp stack context
122+
// esp_yield and non-reentrant functions which depend on it will fail
123+
void onRx(rxhandler_t handler) {
124+
_on_rx = handler;
111125
}
112126

113127
size_t getSize() const
@@ -173,10 +187,10 @@ class UdpContext
173187
return _rx_buf != 0;
174188
}
175189

176-
char read()
190+
int read()
177191
{
178192
if (!_rx_buf || _rx_buf->len == _rx_buf_offset)
179-
return 0;
193+
return -1;
180194

181195
char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
182196
_consume(1);
@@ -190,7 +204,7 @@ class UdpContext
190204

191205
size_t max_size = _rx_buf->len - _rx_buf_offset;
192206
size = (size < max_size) ? size : max_size;
193-
DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf_offset);
207+
DEBUGV(":urd %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf_offset);
194208

195209
os_memcpy(dst, reinterpret_cast<char*>(_rx_buf->payload) + _rx_buf_offset, size);
196210
_consume(size);
@@ -257,10 +271,19 @@ class UdpContext
257271
}
258272
}
259273

260-
if (addr)
261-
udp_sendto(_pcb, _tx_buf_head, addr, port);
262-
else
263-
udp_send(_pcb, _tx_buf_head);
274+
if (!addr) {
275+
addr = &_dest_addr;
276+
port = _dest_port;
277+
}
278+
279+
uint16_t old_ttl = _pcb->ttl;
280+
if (ip_addr_ismulticast(addr)) {
281+
_pcb->ttl = _multicast_ttl;
282+
}
283+
284+
udp_sendto(_pcb, _tx_buf_head, addr, port);
285+
286+
_pcb->ttl = old_ttl;
264287

265288
for (pbuf* p = _tx_buf_head; p; p = p->next)
266289
{
@@ -317,16 +340,19 @@ class UdpContext
317340
{
318341
// there is some unread data
319342
// chain the new pbuf to the existing one
320-
DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
343+
DEBUGV(":urch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
321344
pbuf_cat(_rx_buf, pb);
322345
}
323346
else
324347
{
325-
DEBUGV(":rn %d\r\n", pb->tot_len);
348+
DEBUGV(":urn %d\r\n", pb->tot_len);
326349
_first_buf_taken = false;
327350
_rx_buf = pb;
328351
_rx_buf_offset = 0;
329352
}
353+
if (_on_rx) {
354+
_on_rx();
355+
}
330356
}
331357

332358

@@ -341,13 +367,20 @@ class UdpContext
341367
int _refcnt;
342368
udp_pcb* _pcb;
343369

370+
ip_addr_t _dest_addr;
371+
uint16_t _dest_port;
372+
373+
uint16_t _multicast_ttl;
374+
344375
bool _first_buf_taken;
345376
pbuf* _rx_buf;
346377
size_t _rx_buf_offset;
347378

348379
pbuf* _tx_buf_head;
349380
pbuf* _tx_buf_cur;
350381
size_t _tx_buf_offset;
382+
383+
rxhandler_t _on_rx;
351384
};
352385

353386

0 commit comments

Comments
 (0)