24 #ifdef RF24ETHERNET_DEBUG_UDP
25 #include "HardwareSerial.h"
29 #define UIP_ARPHDRSIZE 42
30 #define UDPBUF ((struct uip_udpip_hdr*)&uip_buf[UIP_LLH_LEN])
35RF24UDP::RF24UDP() : _uip_udp_conn(NULL)
37 memset(&appdata, 0,
sizeof(appdata));
43uint8_t RF24UDP::begin(uint16_t port)
47 _uip_udp_conn = uip_udp_new(NULL, 0);
51 uip_udp_bind(_uip_udp_conn, htons(port));
52 _uip_udp_conn->appstate = &appdata;
65 uip_udp_remove(_uip_udp_conn);
66 _uip_udp_conn->appstate = NULL;
68 appdata.packet_in = 0;
69 appdata.packet_next = 0;
70 appdata.packet_out = 0;
72 memset(&appdata, 0,
sizeof(appdata));
82int RF24UDP::beginPacket(IPAddress ip, uint16_t port)
84 RF24EthernetClass::tick();
93 _uip_udp_conn->rport = htons(port);
94 uip_ipaddr_copy(_uip_udp_conn->ripaddr, &ripaddr);
98 _uip_udp_conn = uip_udp_new(&ripaddr, htons(port));
102 _uip_udp_conn->appstate = &appdata;
110 IF_RF24ETHERNET_DEBUG_UDP(Serial.print(F(
"rip: ")); Serial.print(ip); Serial.print(F(
", port: ")); Serial.println(port););
115 if (appdata.packet_out == 0)
117 appdata.packet_out = 1;
119 if (appdata.packet_out != 0)
140int RF24UDP::beginPacket(
const char* host, uint16_t port)
145 IPAddress remote_addr;
148 ret = dns.getHostByName(host, remote_addr);
151 return beginPacket(remote_addr, port);
163int RF24UDP::endPacket()
165 if (_uip_udp_conn && appdata.packet_out != 0)
169 uip_udp_periodic_conn(_uip_udp_conn);
182size_t RF24UDP::write(uint8_t c)
190size_t RF24UDP::write(
const uint8_t* buffer,
size_t size)
192 if (appdata.packet_out != 0)
194 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(
""););
197 appdata.out_pos += ret;
207int RF24UDP::parsePacket()
210 RF24EthernetClass::tick();
211 int size = appdata.packet_in_size;
213 IF_RF24ETHERNET_DEBUG_UDP(
if (appdata.packet_in != 0) { Serial.print(F(
"RF24UDP udp parsePacket freeing previous packet: ")); Serial.println(appdata.packet_in); });
220 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););
228int RF24UDP::available()
230 RF24EthernetClass::tick();
231 return appdata.packet_in_size;
251int RF24UDP::read(
unsigned char* buffer,
size_t len)
254 RF24EthernetClass::tick();
256 if (appdata.packet_in != 0)
259 appdata.in_pos += len;
260 appdata.packet_in_size -= len;
262 if (appdata.packet_in_size < 1)
264 appdata.packet_in = 0;
277 RF24EthernetClass::tick();
279 if (appdata.packet_in != 0)
291 appdata.packet_in = 0;
292 appdata.packet_in_size = 0;
293 RF24EthernetClass::tick();
299IPAddress RF24UDP::remoteIP()
301 return _uip_udp_conn ?
ip_addr_uip(_uip_udp_conn->ripaddr) : IPAddress();
307uint16_t RF24UDP::remotePort()
309 return _uip_udp_conn ? ntohs(_uip_udp_conn->rport) : 0;
318 if (uip_udp_userdata_t* data = (uip_udp_userdata_t*)(uip_udp_conn->appstate))
322 if (data->packet_next == 0)
324 uip_udp_conn->rport = UDPBUF->srcport;
325 uip_ipaddr_copy(uip_udp_conn->ripaddr, UDPBUF->srcipaddr);
329 data->packet_in_size += uip_len;
332 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(););
335 if (uip_poll() && data->send)
338 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););
341 uip_udp_send(data->out_pos);
348void RF24UDP::_send(uip_udp_userdata_t* data)
350 #if defined(RF24_TAP)
353 if (uip_len == UIP_ARPHDRSIZE)
359 RF24EthernetClass::network_send();
365 data->packet_out = 0;
367 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(
""););
369 RF24NetworkHeader headerOut(00, EXTERNAL_DATA_TYPE);
370 RF24Ethernet.network.write(headerOut, uip_buf, data->out_pos + UIP_UDP_PHYH_LEN);
#define uip_ip_addr(addr, ip)
RF24EthernetClass RF24Ethernet
static uip_userdata_t all_data[UIP_CONNS]
#define IF_RF24ETHERNET_DEBUG_UDP(x)
void uipudp_appcall(void)