RF24Ethernet - TCP/IP over RF24Network v1.6.14
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
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
36typedef struct
37{
38 uint16_t out_pos;
39 uint8_t packet_next;
40 uint8_t packet_in;
41 uint8_t packet_out;
42 int packet_in_size;
43 uint16_t in_pos;
44 boolean send;
45} uip_udp_userdata_t;
46
47class RF24UDP : public UDP
48{
49private:
50 struct uip_udp_conn* _uip_udp_conn;
51
52 uip_udp_userdata_t appdata;
53
54public:
55 /** @brief Constructor */
56 RF24UDP();
57
58 /**
59 * @brief initialize, start listening on specified port.
60 * @returns 1 if successful, 0 if there are no sockets available to use
61 */
62 uint8_t begin(uint16_t);
63
64 /** @brief Finish with the UDP socket */
65 void stop();
66
67 /**
68 * @brief Sending UDP packets
69 *
70 * Start building up a packet to send to the remote host specific in ip and port
71 * @return 1 if successful, 0 if there was a problem with the supplied IP address or port
72 */
73 int beginPacket(IPAddress ip, uint16_t port);
74
75 /**
76 * Start building up a packet to send to the remote host specific in host and port
77 * @return 1 if successful, 0 if there was a problem resolving the hostname or port
78 */
79 int beginPacket(const char* host, uint16_t port);
80
81 /**
82 * @brief Finish off this packet and send it
83 * @return 1 if the packet was sent successfully, 0 if there was an error
84 */
85 int endPacket();
86
87 /** @brief Write a single byte into the packet */
88 size_t write(uint8_t);
89
90 /** @brief Write @p size bytes from @p buffer into the packet */
91 size_t write(const uint8_t* buffer, size_t size);
92
93 using Print::write;
94
95 /**
96 * Start processing the next available incoming packet
97 * @return The size of the packet in bytes, or 0 if no packets are available
98 */
99 int parsePacket();
100
101 /** Number of bytes remaining in the current packet */
102 int available();
103
104 /** Read a single byte from the current packet */
105 int read();
106
107 /**
108 * Read up to @p len bytes from the current packet and place them into @p buffer
109 * @return The number of bytes read, or 0 if none are available
110 */
111 int read(unsigned char* buffer, size_t len);
112
113 /**
114 * Read up to @p len characters from the current packet and place them into @p buffer
115 * @return The number of characters read, or 0 if none are available
116 */
117 int read(char* buffer, size_t len)
118 {
119 return read((unsigned char*)buffer, len);
120 };
121
122 /** @return The next byte from the current packet without moving on to the next byte */
123 int peek();
124
125 /** < Finish reading the current packet */
126 void flush();
127
128 /** Return the IP address of the host who sent the current incoming packet */
129 IPAddress remoteIP();
130
131 /** Return the port of the host who sent the current incoming packet */
132 uint16_t remotePort();
133
134private:
135 friend void uipudp_appcall(void);
136
137 friend class RF24EthernetClass;
138 friend class RF24Client;
139 static void _send(uip_udp_userdata_t* data);
140};
141
142#endif // UDP Enabled
143#endif // UIPUDP_H
void uipudp_appcall(void)