RF24Ethernet - TCP/IP over RF24Network v2.0.3
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
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 #endif
84
85public:
86 /** @brief Constructor */
87 RF24UDP();
88
89 /**
90 * @brief initialize, start listening on specified port.
91 * @returns 1 if successful, 0 if there are no sockets available to use
92 */
93 uint8_t begin(uint16_t);
94
95 /** @brief Finish with the UDP socket */
96 void stop();
97
98 /**
99 * @brief Sending UDP packets
100 *
101 * Start building up a packet to send to the remote host specific in ip and port
102 * @return 1 if successful, 0 if there was a problem with the supplied IP address or port
103 */
104 int beginPacket(IPAddress ip, uint16_t port);
105
106 /**
107 * Start building up a packet to send to the remote host specific in host and port
108 * @return 1 if successful, 0 if there was a problem resolving the hostname or port
109 */
110 int beginPacket(const char* host, uint16_t port);
111
112 /**
113 * @brief Finish off this packet and send it
114 * @return 1 if the packet was sent successfully, 0 if there was an error
115 */
116 int endPacket();
117
118 /** @brief Write a single byte into the packet */
119 size_t write(uint8_t);
120
121 /** @brief Write @p size bytes from @p buffer into the packet */
122 size_t write(const uint8_t* buffer, size_t size);
123
124 using Print::write;
125
126 /**
127 * Start processing the next available incoming packet
128 * @return The size of the packet in bytes, or 0 if no packets are available
129 */
130 int parsePacket();
131
132 /** Number of bytes remaining in the current packet */
133 int available();
134
135 /** Read a single byte from the current packet */
136 int read();
137
138 /**
139 * Read up to @p len bytes from the current packet and place them into @p buffer
140 * @return The number of bytes read, or 0 if none are available
141 */
142 int read(unsigned char* buffer, size_t len);
143
144 /**
145 * Read up to @p len characters from the current packet and place them into @p buffer
146 * @return The number of characters read, or 0 if none are available
147 */
148 int read(char* buffer, size_t len)
149 {
150 return read((unsigned char*)buffer, len);
151 };
152
153 /** @return The next byte from the current packet without moving on to the next byte */
154 int peek();
155
156 /** < Finish reading the current packet */
157 void flush();
158
159 /** Return the IP address of the host who sent the current incoming packet */
160 IPAddress remoteIP();
161
162 /** Return the port of the host who sent the current incoming packet */
163 uint16_t remotePort();
164
165private:
166 friend void uipudp_appcall(void);
167
168 friend class RF24EthernetClass;
169 friend class RF24Client;
170 #ifndef RF24ETHERNET_USE_UDP
171 static void _send(uip_udp_userdata_t* data);
172 #else
173 static void _send();
174 static void receiveUdp(void* arg, struct udp_pcb* pcb, struct pbuf* p, const ip_addr_t* addr, u16_t port);
175 static void sendUdp(void* arg);
176 #endif
177};
178
179#endif // UDP Enabled
180#endif // UIPUDP_H
static UDPState * udpState
Definition RF24Udp.h:55
int endPacket()
Finish off this packet and send it.
Definition RF24Udp.cpp:245
void flush()
Definition RF24Udp.cpp:419
int read()
Definition RF24Udp.cpp:346
friend class RF24EthernetClass
Definition RF24Udp.h:168
int parsePacket()
Definition RF24Udp.cpp:306
size_t write(uint8_t)
Write a single byte into the packet.
Definition RF24Udp.cpp:273
uint16_t remotePort()
Definition RF24Udp.cpp:456
IPAddress remoteIP()
Definition RF24Udp.cpp:435
RF24UDP()
Constructor.
Definition RF24Udp.cpp:58
void stop()
Finish with the UDP socket.
Definition RF24Udp.cpp:96
int read(char *buffer, size_t len)
Definition RF24Udp.h:148
int peek()
Definition RF24Udp.cpp:397
int beginPacket(IPAddress ip, uint16_t port)
Sending UDP packets.
Definition RF24Udp.cpp:124
friend void uipudp_appcall(void)
Definition RF24Udp.cpp:473
friend class RF24Client
Definition RF24Udp.h:169
int available()
Definition RF24Udp.cpp:333
uint8_t begin(uint16_t)
initialize, start listening on specified port.
Definition RF24Udp.cpp:65
volatile bool dataReceived
Definition RF24Udp.h:52
uint16_t u16_t
16 bit datatype
Definition uip-conf.h:245