RF24Ethernet - TCP/IP over RF24Network v2.2.1
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
RF24Udp.h
Go to the documentation of this file.
1/*
2 RF24Udp.h - Arduino implementation of a uIP wrapper class.
3 Copyright (c) 2014 tmrh20@gmail.com, github.com/TMRh20
4 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
5 All rights reserved.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 Updated for RF24Ethernet, TMRh20 2014-2015
21 */
22
23#ifndef UIPUDP_H
24#define UIPUDP_H
25
26#include "RF24Ethernet.h"
27
28#if UIP_CONF_UDP > 0 || RF24ETHERNET_USE_UDP > 0
29
30 #include <Udp.h>
31
32 #define UIP_UDP_MAXDATALEN 1500
33 #define UIP_UDP_PHYH_LEN UIP_LLH_LEN + UIP_IPUDPH_LEN
34 #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN + UIP_UDP_PHYH_LEN
35
36 #if UIP_CONF_UDP > 0
37typedef struct
38{
39 uint16_t out_pos;
40 uint8_t packet_next;
41 uint8_t packet_in;
42 uint8_t packet_out;
43 int packet_in_size;
44 uint16_t in_pos;
45 boolean send;
46} uip_udp_userdata_t;
47
48 #elif RF24ETHERNET_USE_UDP
49
51{
52 volatile bool dataReceived = false;
53};
54
56
57 #endif
58
59class RF24UDP : public UDP
60{
61private:
62
63 #if UIP_CONF_UDP > 0
64 struct uip_udp_conn* _uip_udp_conn;
65
66 uip_udp_userdata_t appdata;
67 #elif RF24ETHERNET_USE_UDP && USE_LWIP == 1
68 static struct udp_pcb* udpPcb;
69
70 static int8_t udpDataIn[MAX_PAYLOAD_SIZE - 14];
71 static int32_t dataInPos;
72 static int8_t udpDataOut[MAX_PAYLOAD_SIZE - 14];
73 static int32_t dataOutPos;
74
75 struct udp_send_ctx
76 {
77 struct udp_pcb* pcb;
78 struct pbuf* p;
79 ip_addr_t ip;
80 u16_t port;
81 };
82
83 #elif USE_LWIP == 2
84 int udpSocketOut;
85 int udpSocketIn;
86 struct sockaddr_in udpDestination;
87 struct sockaddr_in udpBindAddr;
88 struct sockaddr_in udpClientAddr;
89
90 uint8_t udpDataIn[MAX_PAYLOAD_SIZE - 14];
91 int32_t dataInPos;
92 uint8_t udpDataOut[MAX_PAYLOAD_SIZE - 14];
93 int32_t dataOutPos;
94 #endif
95
96public:
97 /** @brief Constructor */
98 RF24UDP();
99
100 /**
101 * @brief initialize, start listening on specified port.
102 * @returns 1 if successful, 0 if there are no sockets available to use
103 */
104 uint8_t begin(uint16_t);
105
106 /** @brief Finish with the UDP socket */
107 void stop();
108
109 /**
110 * @brief Sending UDP packets
111 *
112 * Start building up a packet to send to the remote host specific in ip and port
113 * @return 1 if successful, 0 if there was a problem with the supplied IP address or port
114 */
115 int beginPacket(IPAddress ip, uint16_t port);
116
117 /**
118 * Start building up a packet to send to the remote host specific in host and port
119 * @return 1 if successful, 0 if there was a problem resolving the hostname or port
120 */
121 int beginPacket(const char* host, uint16_t port);
122
123 /**
124 * @brief Finish off this packet and send it
125 * @return 1 if the packet was sent successfully, 0 if there was an error
126 */
127 int endPacket();
128
129 /** @brief Write a single byte into the packet */
130 size_t write(uint8_t);
131
132 /** @brief Write @p size bytes from @p buffer into the packet */
133 size_t write(const uint8_t* buffer, size_t size);
134
135 using Print::write;
136
137 /**
138 * Start processing the next available incoming packet
139 * @return The size of the packet in bytes, or 0 if no packets are available
140 */
141 int parsePacket();
142
143 /** Number of bytes remaining in the current packet */
144 int available();
145
146 /** Read a single byte from the current packet */
147 int read();
148
149 /**
150 * Read up to @p len bytes from the current packet and place them into @p buffer
151 * @return The number of bytes read, or 0 if none are available
152 */
153 int read(unsigned char* buffer, size_t len);
154
155 /**
156 * Read up to @p len characters from the current packet and place them into @p buffer
157 * @return The number of characters read, or 0 if none are available
158 */
159 int read(char* buffer, size_t len)
160 {
161 return read((unsigned char*)buffer, len);
162 };
163
164 /** @return The next byte from the current packet without moving on to the next byte */
165 int peek();
166
167 /** < Finish reading the current packet */
168 void flush();
169
170 /** Return the IP address of the host who sent the current incoming packet */
171 IPAddress remoteIP();
172
173 /** Return the port of the host who sent the current incoming packet */
174 uint16_t remotePort();
175
176private:
177 friend void uipudp_appcall(void);
178
179 friend class RF24EthernetClass;
180 friend class RF24Client;
181 #if RF24ETHERNET_USE_UDP != 1
182 static void _send(uip_udp_userdata_t* data);
183 #elif USE_LWIP == 1
184 static void _send();
185 static void receiveUdp(void* arg, struct udp_pcb* pcb, struct pbuf* p, const ip_addr_t* addr, u16_t port);
186 static void sendUdp(void* arg);
187 #elif USE_LWIP == 2
188 static void _send();
189 static void sendUdp(void* arg);
190 IPAddress udpRemoteIP;
191 int udpRemotePort;
192 #endif
193};
194
195#endif // UDP Enabled
196#endif // UIPUDP_H
static UDPState * udpState
Definition RF24Udp.h:55
int endPacket()
Finish off this packet and send it.
Definition RF24Udp.cpp:326
void flush()
Definition RF24Udp.cpp:543
int read()
Definition RF24Udp.cpp:469
friend class RF24EthernetClass
Definition RF24Udp.h:179
int parsePacket()
Definition RF24Udp.cpp:397
size_t write(uint8_t)
Write a single byte into the packet.
Definition RF24Udp.cpp:363
uint16_t remotePort()
Definition RF24Udp.cpp:583
IPAddress remoteIP()
Definition RF24Udp.cpp:559
RF24UDP()
Constructor.
Definition RF24Udp.cpp:63
void stop()
Finish with the UDP socket.
Definition RF24Udp.cpp:140
int read(char *buffer, size_t len)
Definition RF24Udp.h:159
int peek()
Definition RF24Udp.cpp:520
int beginPacket(IPAddress ip, uint16_t port)
Sending UDP packets.
Definition RF24Udp.cpp:179
friend void uipudp_appcall(void)
Definition RF24Udp.cpp:603
friend class RF24Client
Definition RF24Udp.h:180
int available()
Definition RF24Udp.cpp:426
uint8_t begin(uint16_t)
initialize, start listening on specified port.
Definition RF24Udp.cpp:78
volatile bool dataReceived
Definition RF24Udp.h:52
uint16_t u16_t
16 bit datatype
Definition uip-conf.h:245