-
Notifications
You must be signed in to change notification settings - Fork 51
/
RF24Ethernet.h
300 lines (252 loc) · 8.92 KB
/
RF24Ethernet.h
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
/*
RF24Ethernet by TMRh20 2014-2015
https://github.com/TMRh20
RF24Ethernet.h - Arduino implementation of a uIP wrapper class.
Copyright (c) 2014 [email protected], github.com/TMRh20
Copyright (c) 2013 Norbert Truchsess <[email protected]>
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/>.
*/
#ifndef RF24Ethernet_h
#define RF24Ethernet_h
/**
* @file RF24Ethernet.h
*
* Class declaration for RF24Ethernet
*/
#include <Arduino.h>
extern "C" {
#include "uip-conf.h"
#include "utility/uip.h"
#include "utility/uiptimer.h"
#include "utility/uip_arp.h"
}
#include "RF24Ethernet_config.h"
#if defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52833)
#include <nrf_to_nrf.h>
#endif
#include <RF24.h>
#include <RF24Network.h>
#if !defined(RF24_TAP) // Using RF24Mesh
#include <RF24Mesh.h>
#endif
#include "ethernet_comp.h"
#include "IPAddress.h"
#include "RF24Client.h"
#include "RF24Server.h"
#if UIP_CONF_UDP > 0
#include "RF24Udp.h"
#include "Dns.h"
#endif
#define UIPETHERNET_FREEPACKET 1
#define UIPETHERNET_SENDPACKET 2
//#define TUN // Use only the tcp protocol, no ethernet headers or arps
#define TAP // Include ethernet headers
#if defined(TAP)
#define BUF ((struct uip_eth_hdr*)&uip_buf[0])
#endif
//#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define uip_seteth_addr(eaddr) \
do { \
uip_ethaddr.addr[0] = eaddr[0]; \
uip_ethaddr.addr[1] = eaddr[1]; \
uip_ethaddr.addr[2] = eaddr[2]; \
uip_ethaddr.addr[3] = eaddr[3]; \
uip_ethaddr.addr[4] = eaddr[4]; \
uip_ethaddr.addr[5] = eaddr[5]; \
} while (0)
#define uip_ip_addr(addr, ip) memcpy(addr, &ip[0], 4)
#define ip_addr_uip(a) IPAddress(a[0] & 0xFF, a[0] >> 8, a[1] & 0xFF, a[1] >> 8) // TODO this is not IPV6 capable
#define uip_seteth_addr(eaddr) \
do { \
uip_ethaddr.addr[0] = eaddr[0]; \
uip_ethaddr.addr[1] = eaddr[1]; \
uip_ethaddr.addr[2] = eaddr[2]; \
uip_ethaddr.addr[3] = eaddr[3]; \
uip_ethaddr.addr[4] = eaddr[4]; \
uip_ethaddr.addr[5] = eaddr[5]; \
} while (0)
/**
* @warning <b>This is used internally. Use IPAddress instead. </b>
*/
typedef struct
{
int a, b, c, d;
} IP_ADDR;
class RF24;
template<class radio_t>
class ESB_Network;
class RF24EthernetClass
{ //: public Print {
public:
/**
* Constructor to set up the Ethernet layer. Requires the radio and network to be configured by the user
* this allows users to set custom settings at the radio or network level
*/
#if !defined(RF24_TAP) // Using RF24Mesh
RF24EthernetClass(RF24& _radio, RF24Network& _network, RF24Mesh& _mesh);
#else
RF24EthernetClass(RF24& _radio, RF24Network& _network);
#endif
#if defined NRF52_RADIO_LIBRARY
#if !defined(RF24_TAP)
RF24EthernetClass(nrf_to_nrf& _radio, RF52Network& _network, RF52Mesh& _mesh);
#else
RF24EthernetClass(nrf_to_nrf& _radio, RF52Network& _network);
#endif
#endif
/** Basic constructor */
RF24EthernetClass();
/**
* @note Deprecated, maintained for backwards compatibility with old examples
*
* This function is no longer needed, and does nothing
*/
void use_device();
/**
* Configure the IP address and subnet mask of the node. This is independent of the RF24Network layer, so the IP
* and subnet only have to conform to standard IP routing rules within your network
*/
void begin(IP_ADDR myIP, IP_ADDR subnet);
/**
* Configure the IP address and subnet mask of the node. This is independent of the RF24Network layer, so the IP
* and subnet only have to conform to standard IP routing rules within your network
*/
void begin(IPAddress ip);
void begin(IPAddress ip, IPAddress dns);
void begin(IPAddress ip, IPAddress dns, IPAddress gateway);
void begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
/**
* Configure the gateway IP address. This is generally going to be your master node with RF24Network address 00.
*/
void set_gateway(IPAddress gwIP);
/**
* Listen to a specified port - This will likely be changed to closer match the Arduino Ethernet API with server.begin();
*/
void listen(uint16_t port);
/**
* Sets the MAC address of the RF24 module, which is an RF24Network address
* Specify an Octal address to assign to this node, which will be used as the Ethernet mac address
* If setting up only a few nodes, use 01 to 05
* Please reference the RF24Network documentation for information on setting up a static network
* RF24Mesh will be integrated to provide this automatically
*/
void setMac(uint16_t address);
/** Sets the Radio channel/frequency to use (0-127) */
void setChannel(uint8_t channel);
/** Indicates whether data is available. */
int available();
/** Returns the local IP address */
IPAddress localIP();
/** Returns the subnet mask */
IPAddress subnetMask();
/** Returns the gateway IP address */
IPAddress gatewayIP();
/** Returns the DNS server IP address */
IPAddress dnsServerIP();
/** Keeps the TCP/IP stack running & processing incoming data */
void update();
// uint8_t *key;
private:
#if defined NRF52_RADIO_LIBRARY
nrf_to_nrf& radio;
#else
RF24& radio;
#endif
#if !defined NRF52_RADIO_LIBRARY
RF24Network& network;
#if !defined(RF24_TAP) // Using RF24Mesh
RF24Mesh& mesh;
#endif
#else
RF52Network& network;
#if !defined(RF24_TAP) // Using RF24Mesh
RF52Mesh& mesh;
#endif
#endif
static IPAddress _dnsServerAddress;
void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
// tick() must be called at regular intervals to process the incoming serial
// data and issue IP events to the sketch. It does not return until all IP
// events have been processed.
static void tick();
static void network_send();
uint8_t RF24_Channel;
struct timer periodic_timer;
#if defined RF24_TAP
struct timer arp_timer;
#endif
friend class RF24Server;
friend class RF24Client;
friend class RF24UDP;
};
extern RF24EthernetClass RF24Ethernet;
typedef RF24EthernetClass RF52EthernetClass;
/**
* @example Getting_Started_SimpleServer_Mesh.ino
* <b>Updated: TMRh20 2014 </b><br>
*
* This is an example of how to use the RF24Ethernet class to create a web server which will allow you to connect via
* any device with a web-browser. The url is http://your_chosen_IP:1000
*/
/**
* @example Getting_Started_SimpleClient_Mesh.ino
*
* This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP.
*/
/**
* @example Getting_Started_SimpleClient_Mesh_DNS.ino
*
* This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP,
* using DNS lookups instead of IP address.
*/
/**
* @example SimpleClient_Mesh.ino
*
* This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP.
* It also demonstrates how to read from the HTTP header to read the Content-Length before reading the data.
*/
/**
* @example InteractiveServer_Mesh.ino
*
* This is an example of how to create a more advanced interactive web server.<br>
* This example uses [HTML.h](InteractiveServer__Mesh_2HTML_8h.html) from the
* example's directory.
*/
/**
* @example mqtt_basic.ino
*
* This is the example taken from the PubSub library (https://github.com/knolleary/pubsubclient) & slightly modified to include RF24Ethernet/RF24Mesh.
*/
/**
* @example mqtt_basic_no_blk.ino
*
* This is similar to the mqtt_basic example, but uses a non-blocking connect function.
*/
/**
* @example mqtt_basic_2.ino
*
* A copy of the initial MQTT example using MQTT library https://github.com/256dpi/arduino-mqtt/ slightly modified to include RF24Ethernet/RF24Mesh.
*/
/**
* @example SLIP_Gateway.ino
*
* This example demonstrates how to use an Arduino as a gateway to a SLIP enabled device.
*/
/**
* @example SLIP_InteractiveServer.ino
*
* This example demonstrates how to use RF24Mesh with RF24Ethernet when working with a SLIP or TUN interface.
* <br>This example uses [HTML.h](SLIP__InteractiveServer_2HTML_8h.html) from the
* example's directory.
*/
#endif // RF24Ethernet_h