Repository navigation
Expand file tree
/
Copy pathRF24Udp.cpp
More file actions
741 lines (622 loc) · 20.8 KB
/
Copy pathRF24Udp.cpp
File metadata and controls
741 lines (622 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
RF24UDP.cpp - Arduino implementation of a uIP wrapper class.
Copyright (c) 2014 tmrh20@gmail.com, github.com/TMRh20
Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RF24Ethernet.h"
#if UIP_CONF_UDP > 0 || RF24ETHERNET_USE_UDP > 0
#if RF24ETHERNET_USE_UDP && USE_LWIP == 1
struct udp_pcb* RF24UDP::udpPcb;
int8_t RF24UDP::udpDataIn[MAX_PAYLOAD_SIZE - 14];
int8_t RF24UDP::udpDataOut[MAX_PAYLOAD_SIZE - 14];
int32_t RF24UDP::dataInPos;
int32_t RF24UDP::dataOutPos;
#if !defined ETHERNET_USING_LWIP_ARDUINO
#include "lwip/udp.h"
#include "lwip/tcpip.h"
#else
#include "lwip/include/lwip/udp.h"
#include "lwip/include/lwip/tcpip.h"
#endif
#endif
#if RF24ETHERNET_USE_UDP && USE_LWIP == 2
#include <zephyr/kernel.h>
#include <zephyr/net/socket.h>
#include <fcntl.h>
#endif
#ifdef RF24ETHERNET_DEBUG_UDP
#include "HardwareSerial.h"
#endif
#if UIP_UDP || RF24ETHERNET_USE_UDP
#define UIP_ARPHDRSIZE 42
#define UDPBUF ((struct uip_udpip_hdr*)&uip_buf[UIP_LLH_LEN])
/*******************************************************/
#ifndef RF24ETHERNET_USE_UDP
// Constructor
RF24UDP::RF24UDP() : _uip_udp_conn(NULL)
{
memset(&appdata, 0, sizeof(appdata));
}
#elif defined RF24ETHERNET_USE_UDP && USE_LWIP == 1
RF24UDP::RF24UDP()
{
}
#elif defined RF24ETHERNET_USE_UDP && USE_LWIP == 2
RF24UDP::RF24UDP()
{
udpSocketOut = -1;
udpSocketIn = -1;
dataInPos = 0;
dataOutPos = 0;
}
#endif
/*******************************************************/
// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
uint8_t RF24UDP::begin(uint16_t port)
{
#ifndef RF24ETHERNET_USE_UDP
if (!_uip_udp_conn)
{
_uip_udp_conn = uip_udp_new(NULL, 0);
}
if (_uip_udp_conn)
{
uip_udp_bind(_uip_udp_conn, htons(port));
_uip_udp_conn->appstate = &appdata;
return 1;
}
return 0;
#elif USE_LWIP == 1
if (udpPcb == nullptr) {
udpPcb = udp_new();
}
err_t err = udp_bind(udpPcb, IP_ANY_TYPE, port);
if (err == ERR_OK) {
return 1;
}
return 0;
#elif USE_LWIP == 2
if (udpSocketIn > -1) {
zsock_close(udpSocketIn);
udpSocketIn = -1;
}
udpSocketIn = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpSocketIn < 0) {
return 0;
}
dataInPos = 0;
memset(&udpBindAddr, 0, sizeof(udpBindAddr));
udpBindAddr.sin_family = AF_INET;
udpBindAddr.sin_port = htons(port);
udpBindAddr.sin_addr.s_addr = INADDR_ANY;
int optval = 1;
zsock_setsockopt(udpSocketIn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (zsock_bind(udpSocketIn, (struct sockaddr*)&udpBindAddr, sizeof(udpBindAddr)) < 0) {
zsock_close(udpSocketIn);
udpSocketIn = -1;
return 0;
}
int flags = zsock_fcntl(udpSocketIn, F_GETFL, 0);
zsock_fcntl(udpSocketIn, F_SETFL, flags | O_NONBLOCK);
return 1;
#endif
}
/*******************************************************/
// Finish with the UDP socket
void RF24UDP::stop()
{
#ifndef RF24ETHERNET_USE_UDP
if (_uip_udp_conn)
{
uip_udp_remove(_uip_udp_conn);
_uip_udp_conn->appstate = NULL;
_uip_udp_conn = NULL;
appdata.packet_in = 0;
appdata.packet_next = 0;
appdata.packet_out = 0;
memset(&appdata, 0, sizeof(appdata));
}
#elif USE_LWIP == 1
if (udpPcb != nullptr) {
udp_disconnect(udpPcb);
}
#elif USE_LWIP == 2
if (udpSocketIn > -1) {
zsock_close(udpSocketIn);
}
dataInPos = 0;
if (udpSocketOut > -1) {
zsock_close(udpSocketOut);
udpSocketOut = -1;
}
dataOutPos = 0;
#endif
}
/*******************************************************/
// Sending UDP packets
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
int RF24UDP::beginPacket(IPAddress ip, uint16_t port)
{
#ifndef RF24ETHERNET_USE_UDP
RF24EthernetClass::tick();
if (ip && port)
{
uip_ipaddr_t ripaddr;
uip_ip_addr(&ripaddr, ip);
IF_RF24ETHERNET_DEBUG_UDP(Serial.print(F("RF24UDP udp beginPacket, ")););
if (_uip_udp_conn)
{
_uip_udp_conn->rport = htons(port);
uip_ipaddr_copy(_uip_udp_conn->ripaddr, &ripaddr);
}
else
{
_uip_udp_conn = uip_udp_new(&ripaddr, htons(port));
if (_uip_udp_conn)
{
IF_RF24ETHERNET_DEBUG_UDP(Serial.print(F("RF24UDP New connection, ")););
_uip_udp_conn->appstate = &appdata;
}
else
{
IF_RF24ETHERNET_DEBUG_UDP(Serial.println(F("RF24UDP Failed to allocate new connection")););
return 0;
}
}
IF_RF24ETHERNET_DEBUG_UDP(Serial.print(F("rip: ")); Serial.print(ip); Serial.print(F(", port: ")); Serial.println(port););
}
if (_uip_udp_conn)
{
if (appdata.packet_out == 0)
{
appdata.packet_out = 1;
appdata.out_pos = 0; // UIP_UDP_PHYH_LEN;
if (appdata.packet_out != 0)
{
return 1;
}
else
{
IF_RF24ETHERNET_DEBUG_UDP(Serial.println(F("RF24UDP Failed to allocate memory for packet")););
}
}
else
{
IF_RF24ETHERNET_DEBUG_UDP(Serial.println(F("RF24UDP Previous packet on that connection not sent yet")););
}
}
return 0;
#elif USE_LWIP == 1
err_t err = ERR_OK;
ip4_addr_t myIp;
#if defined ARDUINO_ARCH_ESP32 || defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_RP2040 || defined ARDUINO_ARCH_RP2350
IP4_ADDR(&myIp, ip[0], ip[1], ip[2], ip[3]);
ip_addr_t generic_addr;
ip_addr_copy_from_ip4(generic_addr, myIp);
err = udp_connect(udpPcb, &generic_addr, port);
#else
IP4_ADDR(&myIp, ip[0], ip[1], ip[2], ip[3]);
// Start non-blocking connection
err = udp_connect(udpPcb, &myIp, port);
#endif
if (err == ERR_OK) {
return 1;
}
return 0;
#elif USE_LWIP == 2
if (udpSocketOut >= 0) {
zsock_close(udpSocketOut);
udpSocketOut = -1;
}
udpSocketOut = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udpSocketOut < 0) {
return 0;
}
memset(&udpDestination, 0, sizeof(udpDestination));
udpDestination.sin_family = AF_INET;
udpDestination.sin_port = htons(port);
udpDestination.sin_addr.s_addr = (uint32_t)ip;
if (zsock_inet_pton(AF_INET, ip.toString().c_str(), &udpDestination.sin_addr) != 1) {
zsock_close(udpSocketOut);
udpSocketOut = -1;
return 0;
}
int flags = zsock_fcntl(udpSocketOut, F_GETFL, 0);
zsock_fcntl(udpSocketOut, F_SETFL, flags | O_NONBLOCK);
return 1;
#endif
}
/*******************************************************/
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
int RF24UDP::beginPacket(const char* host, uint16_t port)
{
#ifndef RF24ETHERNET_USE_UDP
// Look up the host first
int ret = 0;
DNSClient dns;
IPAddress remote_addr;
dns.begin(RF24Ethernet.dnsServerIP());
ret = dns.getHostByName(host, remote_addr);
if (ret == 1)
{
return beginPacket(remote_addr, port);
}
else
{
return ret;
}
#else
// Look up the host first
int ret = 0;
DNSClient dns;
IPAddress remote_addr;
dns.begin(RF24Ethernet.dnsServerIP());
ret = dns.getHostByName(host, remote_addr);
if (ret == 1)
{
return beginPacket(remote_addr, port);
}
else
{
return ret;
}
#endif
}
/*******************************************************/
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
int RF24UDP::endPacket()
{
#ifndef RF24ETHERNET_USE_UDP
if (_uip_udp_conn && appdata.packet_out != 0)
{
appdata.send = true;
IF_RF24ETHERNET_DEBUG_UDP(Serial.println(F("RF24UDP endpacket")););
uip_udp_periodic_conn(_uip_udp_conn);
if (uip_len > 0)
{
_send(&appdata);
return 1;
}
}
return 0;
#elif USE_LWIP == 1
if (dataOutPos > 0) {
_send();
return 1;
}
return 0;
#elif USE_LWIP == 2
ssize_t ret = zsock_sendto(udpSocketOut, udpDataOut, dataOutPos, 0, (struct sockaddr*)&udpDestination, sizeof(udpDestination));
if (ret >= 0) {
return 1;
}
else {
return 0;
}
#endif
}
/*******************************************************/
// Write a single byte into the packet
size_t RF24UDP::write(uint8_t c)
{
return write(&c, 1);
}
/*******************************************************/
// Write size bytes from buffer into the packet
size_t RF24UDP::write(const uint8_t* buffer, size_t size)
{
#ifndef RF24ETHERNET_USE_UDP
if (appdata.packet_out != 0)
{
IF_RF24ETHERNET_DEBUG_UDP(Serial.println("RF24UDP Write: "); Serial.println(size); for (int i = 0; i < size; i++) { Serial.print((char)buffer[i]); Serial.print(" "); } Serial.println(""););
size_t ret = size;
memcpy(RF24Client::all_data[0].myData + appdata.out_pos, buffer, size);
appdata.out_pos += ret;
return ret;
}
return 0;
#else
memcpy(&udpDataOut[dataOutPos], buffer, size);
dataOutPos += size;
return size;
#endif
}
/*******************************************************/
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
int RF24UDP::parsePacket()
{
RF24EthernetClass::tick();
#ifndef RF24ETHERNET_USE_UDP
int size = appdata.packet_in_size;
IF_RF24ETHERNET_DEBUG_UDP(if (appdata.packet_in != 0) { Serial.print(F("RF24UDP udp parsePacket freeing previous packet: ")); Serial.println(appdata.packet_in); });
// appdata.packet_in_size = 0;
// appdata.packet_in = appdata.packet_next;
// appdata.packet_next = 0;
IF_RF24ETHERNET_DEBUG_UDP(if (appdata.packet_in != 0) { Serial.print(F("RF24UDP udp parsePacket received packet: ")); Serial.print(appdata.packet_in); } Serial.print(F(", size: ")); Serial.println(size););
return size;
#elif USE_LWIP == 1
return dataInPos;
#elif USE_LWIP == 2
return available();
#endif
}
/*******************************************************/
// Number of bytes remaining in the current packet
int RF24UDP::available()
{
RF24EthernetClass::tick();
#ifndef RF24ETHERNET_USE_UDP
return appdata.packet_in_size;
#elif USE_LWIP == 1
return dataInPos;
#elif USE_LWIP == 2
if (udpSocketOut >= 0) {
socklen_t client_addr_len = sizeof(udpClientAddr);
ssize_t received = zsock_recvfrom(udpSocketOut, udpDataIn, sizeof(udpDataIn) - 1, 0, (struct sockaddr*)&udpClientAddr, &client_addr_len);
if (received >= 0) {
dataInPos += received;
uint32_t rawIP = udpClientAddr.sin_addr.s_addr;
IPAddress remIP(rawIP);
udpRemoteIP = remIP;
udpRemotePort = ntohs(udpClientAddr.sin_port);
return dataInPos;
}
}
if (udpSocketIn >= 0) {
socklen_t client_addr_len = sizeof(udpClientAddr);
ssize_t received = zsock_recvfrom(udpSocketIn, udpDataIn, sizeof(udpDataIn) - 1, 0, (struct sockaddr*)&udpClientAddr, &client_addr_len);
if (received >= 0) {
dataInPos += received;
uint32_t rawIP = udpClientAddr.sin_addr.s_addr;
IPAddress remIP(rawIP);
udpRemoteIP = remIP;
udpRemotePort = ntohs(udpClientAddr.sin_port);
return dataInPos;
}
}
return dataInPos;
#endif
}
/*******************************************************/
// Read a single byte from the current packet
int RF24UDP::read()
{
unsigned char c;
if (read(&c, 1) > 0)
{
return c;
}
return -1;
}
/*******************************************************/
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
int RF24UDP::read(unsigned char* buffer, size_t len)
{
//RF24EthernetClass::tick();
#ifndef RF24ETHERNET_USE_UDP
if (appdata.packet_in != 0)
{
// Never return more than the packet actually holds, or in_pos walks off
// myData and packet_in_size (an int) underflows to a large positive value
if (appdata.packet_in_size < 0) {
appdata.packet_in_size = 0;
}
if (len > (size_t)appdata.packet_in_size) {
len = (size_t)appdata.packet_in_size;
}
memcpy(buffer, RF24Client::all_data[0].myData + appdata.in_pos, len);
appdata.in_pos += len;
appdata.packet_in_size -= len;
if (appdata.packet_in_size < 1)
{
appdata.packet_in = 0;
}
return len;
}
#elif USE_LWIP == 1 || USE_LWIP == 2
if (dataInPos >= len) {
size_t remainder = dataInPos - len;
memcpy(&buffer[0], &udpDataIn[0], len);
if (remainder > 0) {
memmove(udpDataIn, &udpDataIn[len], remainder);
}
dataInPos = rf24_max(0, remainder);
return len;
}
#endif
return 0;
}
/*******************************************************/
// Return the next byte from the current packet without moving on to the next byte
int RF24UDP::peek()
{
#ifndef RF24ETHERNET_USE_UDP
RF24EthernetClass::tick();
if (appdata.packet_in != 0)
{
return RF24Client::all_data[0].myData[appdata.in_pos];
}
return -1;
#elif USE_LWIP == 1 || USE_LWIP == 1
if (dataInPos > 0) {
return udpDataIn[0];
}
return -1;
#endif
}
/*******************************************************/
// Finish reading the current packet
void RF24UDP::flush()
{
#ifndef RF24ETHERNET_USE_UDP
appdata.packet_in = 0;
appdata.packet_in_size = 0;
#else
uint8_t c = 0;
while (read() > 0) {
};
#endif
//RF24EthernetClass::tick();
}
/*******************************************************/
// Return the IP address of the host who sent the current incoming packet
IPAddress RF24UDP::remoteIP()
{
#ifndef RF24ETHERNET_USE_UDP
return _uip_udp_conn ? ip_addr_uip(_uip_udp_conn->ripaddr) : IPAddress();
#elif USE_LWIP == 1
if (udpPcb != nullptr) {
const ip4_addr_t* addr = ip_2_ip4(&udpPcb->remote_ip);
return IPAddress(
ip4_addr_get_byte(addr, 0),
ip4_addr_get_byte(addr, 1),
ip4_addr_get_byte(addr, 2),
ip4_addr_get_byte(addr, 3));
}
return IPAddress {0, 0, 0, 0};
#elif USE_LWIP == 2
return udpRemoteIP;
#endif
}
/*******************************************************/
// Return the port of the host who sent the current incoming packet
uint16_t RF24UDP::remotePort()
{
#ifndef RF24ETHERNET_USE_UDP
return _uip_udp_conn ? ntohs(_uip_udp_conn->rport) : 0;
#elif USE_LWIP == 1
if (udpPcb != nullptr) {
return udpPcb->remote_port;
}
return 0;
#elif USE_LWIP == 2
return udpRemotePort;
#endif
}
/*******************************************************/
// uIP callback function
void uipudp_appcall(void)
{
#ifndef RF24ETHERNET_USE_UDP
if (uip_udp_userdata_t* data = (uip_udp_userdata_t*)(uip_udp_conn->appstate))
{
if (uip_newdata())
{
if (data->packet_next == 0)
{
uip_udp_conn->rport = UDPBUF->srcport;
uip_ipaddr_copy(uip_udp_conn->ripaddr, UDPBUF->srcipaddr);
// discard Linklevel and IP and udp-header and any trailing bytes:
// uip_len is the UDP payload length here; myData is only OUTPUT_BUFFER_SIZE bytes
uint16_t udpLen = uip_len;
if (udpLen > OUTPUT_BUFFER_SIZE) {
udpLen = OUTPUT_BUFFER_SIZE;
}
memcpy(RF24Client::all_data[0].myData, uip_appdata, udpLen);
data->packet_in_size += udpLen;
data->packet_in = 1;
IF_RF24ETHERNET_DEBUG_UDP(Serial.print(F("RF24UDP udp, uip_newdata received packet: ")); Serial.print(data->packet_next); Serial.print(F(", size: ")); Serial.println(data->packet_in_size); for (int i = 0; i < data->packet_in_size; i++) { Serial.print(RF24Client::all_data[0].myData[i],HEX); Serial.print(F(" : ")); } Serial.println(););
}
}
if (uip_poll() && data->send)
{
// set uip_slen (uip private) by calling uip_udp_send
IF_RF24ETHERNET_DEBUG_UDP(Serial.print(F("udp, uip_poll preparing packet to send: ")); Serial.print(data->packet_out); Serial.print(F(", size: ")); Serial.println(data->out_pos););
memcpy(uip_appdata, RF24Client::all_data[0].myData, data->out_pos);
uip_udp_send(data->out_pos);
}
}
#else
#endif
}
#if RF24ETHERNET_USE_UDP && USE_LWIP == 1
void RF24UDP::receiveUdp(void* arg, struct udp_pcb* pcb, struct pbuf* p, const ip_addr_t* addr, u16_t port)
{
if (p != NULL) {
// p->len is attacker/peer controlled; udpDataIn is a fixed static buffer.
// Buffer a single datagram's worth, clamped to the buffer size.
uint16_t copyLen = p->len;
if (copyLen > sizeof(udpDataIn)) {
copyLen = sizeof(udpDataIn);
}
memcpy(&udpDataIn[0], p->payload, copyLen);
dataInPos = copyLen;
pbuf_free(p);
if (udpState != nullptr) {
udpState->dataReceived = true;
}
}
}
#endif
#if RF24ETHERNET_USE_UDP && USE_LWIP == 1
void RF24UDP::sendUdp(void* arg)
{
struct udp_send_ctx* data = (struct udp_send_ctx*)arg;
udp_send(data->pcb, data->p);
}
#endif
/*******************************************************/
#ifndef RF24ETHERNET_USE_UDP
void RF24UDP::_send(uip_udp_userdata_t* data)
#else
void RF24UDP::_send()
#endif
{
#ifndef RF24ETHERNET_USE_UDP
#if defined(RF24_TAP)
uip_arp_out(); // add arp
#endif
if (uip_len == UIP_ARPHDRSIZE)
{
// RF24EthernetClass::uip_packet = 0;
// RF24EthernetClass::packetstate &= ~UIPETHERNET_SENDPACKET;
IF_RF24ETHERNET_DEBUG_UDP(Serial.println(F("udp, uip_poll results in ARP-packet")););
RF24EthernetClass::network_send();
}
else
{
// arp found ethaddr for ip (otherwise packet is replaced by arp-request)
data->send = false;
data->packet_out = 0;
// RF24EthernetClass::packetstate |= UIPETHERNET_SENDPACKET;
IF_RF24ETHERNET_DEBUG_UDP(Serial.println(data->out_pos); Serial.print(F("udp, uip_packet to send: ")); for (int i = 0; i < data->out_pos; i++) { Serial.print((char)RF24Client::all_data[0].myData[i]); } Serial.println(""););
RF24NetworkHeader headerOut(00, EXTERNAL_DATA_TYPE);
RF24Ethernet.network.write(headerOut, uip_buf, data->out_pos + UIP_UDP_PHYH_LEN);
}
#elif USE_LWIP == 1
if (udpState == nullptr) {
udpState = new UDPState;
}
udpState->dataReceived = false;
udp_recv(udpPcb, receiveUdp, udpState);
pbuf* p = pbuf_alloc(PBUF_RAW, dataOutPos, PBUF_RAM);
if (p) {
memcpy(reinterpret_cast<uint8_t*>(p->payload), &udpDataOut[0], dataOutPos);
p->len = dataOutPos;
udp_send(udpPcb, p);
dataOutPos = 0;
Ethernet.update();
pbuf_free(p);
}
#elif USE_LWIP == 2
#endif
}
/*******************************************************/
#endif // UIP_UDP
#endif // UDP Enabled