RadioHead
RHRouter.h
1// RHRouter.h
2//
3// Author: Mike McCauley (mikem@airspayce.com)
4// Copyright (C) 2011 Mike McCauley
5// $Id: RHRouter.h,v 1.13 2020/08/04 09:02:14 mikem Exp $
6
7#ifndef RHRouter_h
8#define RHRouter_h
9
10#include <RHReliableDatagram.h>
11
12// Default max number of hops we will route
13#define RH_DEFAULT_MAX_HOPS 30
14
15// The default size of the routing table we keep
16#define RH_ROUTING_TABLE_SIZE 10
17
18// Error codes
19#define RH_ROUTER_ERROR_NONE 0
20#define RH_ROUTER_ERROR_INVALID_LENGTH 1
21#define RH_ROUTER_ERROR_NO_ROUTE 2
22#define RH_ROUTER_ERROR_TIMEOUT 3
23#define RH_ROUTER_ERROR_NO_REPLY 4
24#define RH_ROUTER_ERROR_UNABLE_TO_DELIVER 5
25
26// This size of RH_ROUTER_MAX_MESSAGE_LEN is OK for Arduino Mega, but too big for
27// Duemilanove. Size of 50 works with the sample router programs on Duemilanove.
28#define RH_ROUTER_MAX_MESSAGE_LEN (RH_MAX_MESSAGE_LEN - sizeof(RHRouter::RoutedMessageHeader))
29//#define RH_ROUTER_MAX_MESSAGE_LEN 50
30
31// These allow us to define a simulated network topology for testing purposes
32// See RHRouter.cpp for details
33//#define RH_TEST_NETWORK 1
34//#define RH_TEST_NETWORK 2
35//#define RH_TEST_NETWORK 3
36//#define RH_TEST_NETWORK 4
37
38/////////////////////////////////////////////////////////////////////
39/// \class RHRouter RHRouter.h <RHRouter.h>
40/// \brief RHReliableDatagram subclass for sending addressed, optionally acknowledged datagrams
41/// multi-hop routed across a network.
42///
43/// This is a Manager class that extends RHReliableDatagram to handle addressed messages
44/// that are reliably transmitted and routed across a network of multiple RHRouter nodes.
45/// Each message is transmitted reliably
46/// between each hop in order to get from the source node to the destination node.
47///
48/// With RHRouter, routes are hard wired. This means that each node must have programmed
49/// in it how to reach each of the other nodes it will be trying to communicate with.
50/// This means you must specify the next-hop node address for each of the destination nodes,
51/// using the addRouteTo() function.
52///
53/// When sendtoWait() is called with a new message to deliver, and the destination address,
54/// RHRouter looks up the next hop node for the destination node. It then uses
55/// RHReliableDatagram to (reliably) deliver the message to the next hop
56/// (which is expected also to be running an RHRouter). If that next-hop node is not
57/// the final destination, it will also look up the next hop for the destination node and
58/// (reliably) deliver the message to the next hop. By this method, messages can be delivered
59/// across a network of nodes, even if each node cannot hear all of the others in the network.
60/// Each time a message is received for another node and retransmitted to the next hop,
61/// the HOPS field in the header is incremented. If a message is received for routing to another node
62/// which has exceed the routers max_hops, the message will be dropped and ignored.
63/// This helps prevent infinite routing loops.
64///
65/// RHRouter supports messages with a dest of RH_BROADCAST_ADDRESS. Such messages are not routed,
66/// and are broadcast (once) to all nodes within range.
67///
68/// The recvfromAck() function is responsible not just for receiving and delivering
69/// messages addressed to this node (or RH_BROADCAST_ADDRESS), but
70/// it is also responsible for routing other message to their next hop. This means that it is important to
71/// call recvfromAck() or recvfromAckTimeout() frequently in your main loop. recvfromAck() will return
72/// false if it receives a message but it is not for this node.
73///
74/// RHRouter does not provide reliable end-to-end delivery, but uses reliable hop-to-hop delivery.
75/// If a message is unable to be delivered to an end node during to a delivery failure between 2 hops,
76/// the source node will not be told about it.
77///
78/// Note: This class is most useful for networks of nodes that are essentially static
79/// (i.e. the nodes dont move around), and for which the
80/// routing never changes. If that is not the case for your proposed network, see RHMesh instead.
81///
82/// \par The Routing Table
83///
84/// The routing table is a local table in RHRouter that holds the information about the next hop node
85/// address for each destination address you may want to send a message to. It is your responsibility
86/// to make sure every node in an RHRouter network has been configured with a unique address and the
87/// routing information so that messages are correctly routed across the network from source node to
88/// destination node. This is usually done once in setup() by calling addRouteTo().
89/// The hardwired routing will in general be different on each node, and will depend on the physical
90/// topololgy of the network.
91/// You can also use addRouteTo() to change a route and
92/// deleteRouteTo() to delete a route at run time. Youcan also clear the entire routing table
93///
94/// The Routing Table has limited capacity for entries (defined by RH_ROUTING_TABLE_SIZE, which is 10)
95/// if more than RH_ROUTING_TABLE_SIZE are added, the oldest (first) one will be removed by calling
96/// retireOldestRoute()
97///
98/// \par Message Format
99///
100/// RHRouter add to the lower level RHReliableDatagram (and even lower level RH) class message formats.
101/// In those lower level classes, the hop-to-hop message headers are in the RH message headers,
102/// and are handled automcatically by tyhe RH hardware.
103/// RHRouter and its subclasses add an end-to-end addressing header in the payload of the RH message,
104/// and before the RHRouter application data.
105/// - 1 octet DEST, the destination node address (ie the address of the final
106/// destination node for this message)
107/// - 1 octet SOURCE, the source node address (ie the address of the originating node that first sent
108/// the message).
109/// - 1 octet HOPS, the number of hops this message has traversed so far.
110/// - 1 octet ID, an incrementing message ID for end-to-end message tracking for use by subclasses.
111/// Not used by RHRouter.
112/// - 1 octet FLAGS, a bitmask for use by subclasses. Not used by RHRouter.
113/// - 0 or more octets DATA, the application payload data. The length of this data is implicit
114/// in the length of the entire message.
115///
116/// You should be careful to note that there are ID and FLAGS fields in the low level per-hop
117/// message header too. These are used only for hop-to-hop, and in general will be different to
118/// the ones at the RHRouter level.
119///
120/// \par Testing
121///
122/// Bench testing of such networks is notoriously difficult, especially simulating limited radio
123/// connectivity between some nodes.
124/// To assist testing (both during RH development and for your own networks)
125/// RHRouter.cpp has the ability to
126/// simulate a number of different small network topologies. Each simulated network supports 4 nodes with
127/// addresses 1 to 4. It operates by pretending to not hear RH messages from certain other nodes.
128/// You can enable testing with a \#define TEST_NETWORK in RHRouter.h
129/// The sample programs rf22_mesh_* rely on this feature.
130///
131/// Part of the Arduino RH library for operating with HopeRF RH compatible transceivers
132/// (see http://www.hoperf.com)
134{
135public:
136
137 /// Defines the structure of the RHRouter message header, used to keep track of end-to-end delivery parameters
138 typedef struct
139 {
140 uint8_t dest; ///< Destination node address
141 uint8_t source; ///< Originator node address
142 uint8_t hops; ///< Hops traversed so far
143 uint8_t id; ///< Originator sequence number
144 uint8_t flags; ///< Originator flags
145 // Data follows, Length is implicit in the overall message length
147
148 /// Defines the structure of a RHRouter message
149 typedef struct
150 {
151 RoutedMessageHeader header; ///< end-to-end delivery header
152 uint8_t data[RH_ROUTER_MAX_MESSAGE_LEN]; ///< Application payload data
154
155 /// Values for the possible states for routes
156 typedef enum
157 {
158 Invalid = 0, ///< No valid route is known
159 Discovering, ///< Discovering a route (not currently used)
160 Valid ///< Route is valid
162
163 /// Defines an entry in the routing table
164 typedef struct
165 {
166 uint8_t dest; ///< Destination node address
167 uint8_t next_hop; ///< Send via this next hop address
168 uint8_t state; ///< State of this route, one of RouteState
170
171 /// Constructor.
172 /// \param[in] driver The RadioHead driver to use to transport messages.
173 /// \param[in] thisAddress The address to assign to this node. Defaults to 0
174 RHRouter(RHGenericDriver& driver, uint8_t thisAddress = 0);
175
176 /// Initialises this instance and the radio module connected to it.
177 /// Overrides the init() function in RH.
178 /// Sets max_hops to the default of RH_DEFAULT_MAX_HOPS (30)
179 bool init();
180
181
182 /// Sets the flag determining if the node will participate in routing.
183 /// if isa_router is true, the node will be a full participant. If false the node
184 /// will only respond to
185 /// packets directed to its address, and act only as a leaf node in the network. The default is true.
186 /// \param[in] isa_router true or false
187 void setIsaRouter(bool isa_router);
188
189 /// Sets the max_hops to the given value
190 /// This controls the maximum number of hops allowed between source and destination nodes
191 /// Messages that are not delivered by the time their HOPS field exceeds max_hops on a
192 /// routing node will be dropped and ignored.
193 /// \param [in] max_hops The new value for max_hops
194 void setMaxHops(uint8_t max_hops);
195
196 /// Adds a route to the local routing table, or updates it if already present.
197 /// If there is not enough room the oldest (first) route will be deleted by calling retireOldestRoute().
198 /// \param [in] dest The destination node address. RH_BROADCAST_ADDRESS is permitted.
199 /// \param [in] next_hop The address of the next hop to send messages destined for dest
200 /// \param [in] state The satte of the route. Defaults to Valid
201 void addRouteTo(uint8_t dest, uint8_t next_hop, uint8_t state = Valid);
202
203 /// Finds and returns a RoutingTableEntry for the given destination node
204 /// \param [in] dest The desired destination node address.
205 /// \return pointer to a RoutingTableEntry for dest
206 RoutingTableEntry* getRouteTo(uint8_t dest);
207
208 /// Deletes from the local routing table any route for the destination node.
209 /// \param [in] dest The destination node address
210 /// \return true if the route was present
211 bool deleteRouteTo(uint8_t dest);
212
213 /// Deletes the oldest (first) route from the
214 /// local routing table
215 void retireOldestRoute();
216
217 /// Clears all entries from the
218 /// local routing table
219 void clearRoutingTable();
220
221 /// If RH_HAVE_SERIAL is defined, this will print out the contents of the local
222 /// routing table using Serial
223 void printRoutingTable();
224
225 /// Method for iterating through the current routing table
226 /// \param [inout] RTE_p If a valid entry is found, the entry is copied to this structure
227 /// caller is responsible for alloocating and deallocating the structure.
228 /// \param [inout] lastIndex_p points to the index to start searching from. Set to the
229 /// index of the next valid route found. Set this to -1 to start the search.
230 /// \return false if next entry is valid, true if finished with table.
231 bool getNextValidRoutingTableEntry(RoutingTableEntry *RTE_p, int *lastIndex_p); //blase 7/27/20
232
233
234 /// Sends a message to the destination node. Initialises the RHRouter message header
235 /// (the SOURCE address is set to the address of this node, HOPS to 0) and calls
236 /// route() which looks up in the routing table the next hop to deliver to and sends the
237 /// message to the next hop. Waits for an acknowledgement from the next hop
238 /// (but not from the destination node (if that is different).
239 /// \param [in] buf The application message data
240 /// \param [in] len Number of octets in the application message data. 0 is permitted
241 /// \param [in] dest The destination node address
242 /// \param [in] flags Optional flags for use by subclasses or application layer,
243 /// delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck().
244 /// \return The result code:
245 /// - RH_ROUTER_ERROR_NONE Message was routed and delivered to the next hop
246 /// (not necessarily to the final dest address)
247 /// - RH_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table
248 /// - RH_ROUTER_ERROR_UNABLE_TO_DELIVER Not able to deliver to the next hop
249 /// (usually because it dod not acknowledge due to being off the air or out of range
250 uint8_t sendtoWait(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t flags = 0);
251
252 /// Similar to sendtoWait() above, but spoofs the source address.
253 /// For internal use only during routing
254 /// \param [in] buf The application message data.
255 /// \param [in] len Number of octets in the application message data. 0 is permitted.
256 /// \param [in] dest The destination node address.
257 /// \param [in] source The (fake) originating node address.
258 /// \param [in] flags Optional flags for use by subclasses or application layer,
259 /// delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck().
260 /// \return The result code:
261 /// - RH_ROUTER_ERROR_NONE Message was routed and deliverd to the next hop
262 /// (not necessarily to the final dest address)
263 /// - RH_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table
264 /// - RH_ROUTER_ERROR_UNABLE_TO_DELIVER Noyt able to deliver to the next hop
265 /// (usually because it dod not acknowledge due to being off the air or out of range
266 uint8_t sendtoFromSourceWait(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t source, uint8_t flags = 0);
267
268 /// Starts the receiver if it is not running already.
269 /// If there is a valid message available for this node (or RH_BROADCAST_ADDRESS),
270 /// send an acknowledgement to the last hop
271 /// address (blocking until this is complete), then copy the application message payload data
272 /// to buf and return true
273 /// else return false.
274 /// If a message is copied, *len is set to the length..
275 /// If from is not NULL, the originator SOURCE address is placed in *source.
276 /// If to is not NULL, the DEST address is placed in *dest. This might be this nodes address or
277 /// RH_BROADCAST_ADDRESS.
278 /// This is the preferred function for getting messages addressed to this node.
279 /// If the message is not a broadcast, acknowledge to the sender before returning.
280 /// \param[in] buf Location to copy the received message
281 /// \param[in,out] len Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied.
282 /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address
283 /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address
284 /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID
285 /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS
286 /// \param[in] hops If present and not NULL, the referenced uint8_t will be set to the HOPS
287 /// (not just those addressed to this node).
288 /// \return true if a valid message was recvived for this node copied to buf
289 bool recvfromAck(uint8_t* buf, uint8_t* len, uint8_t* source = NULL, uint8_t* dest = NULL, uint8_t* id = NULL, uint8_t* flags = NULL, uint8_t* hops = NULL);
290
291 /// Starts the receiver if it is not running already.
292 /// Similar to recvfromAck(), this will block until either a valid message available for this node
293 /// or the timeout expires.
294 /// \param[in] buf Location to copy the received message
295 /// \param[in,out] len Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied.
296 /// \param[in] timeout Maximum time to wait in milliseconds
297 /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address
298 /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address
299 /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID
300 /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS
301 /// \param[in] hops If present and not NULL, the referenced uint8_t will be set to the HOPS
302 /// (not just those addressed to this node).
303 /// \return true if a valid message was copied to buf
304 bool recvfromAckTimeout(uint8_t* buf, uint8_t* len, uint16_t timeout, uint8_t* source = NULL, uint8_t* dest = NULL, uint8_t* id = NULL, uint8_t* flags = NULL, uint8_t* hops = NULL);
305
306protected:
307
308 /// Lets sublasses peek at messages going
309 /// past before routing or local delivery.
310 /// Called by recvfromAck() immediately after it gets the message from RHReliableDatagram
311 /// \param [in] message Pointer to the RHRouter message that was received.
312 /// \param [in] messageLen Length of message in octets
313 virtual void peekAtMessage(RoutedMessage* message, uint8_t messageLen);
314
315 /// Finds the next-hop route and sends the message via RHReliableDatagram::sendtoWait().
316 /// This is virtual, which lets subclasses override or intercept the route() function.
317 /// Called by sendtoWait after the message header has been filled in.
318 /// \param [in] message Pointer to the RHRouter message to be sent.
319 /// \param [in] messageLen Length of message in octets
320 virtual uint8_t route(RoutedMessage* message, uint8_t messageLen);
321
322 /// Deletes a specific rout entry from therouting table
323 /// \param [in] index The 0 based index of the routing table entry to delete
324 void deleteRoute(uint8_t index);
325
326 /// The last end-to-end sequence number to be used
327 /// Defaults to 0
329
330 /// The maximum number of hops permitted in routed messages.
331 /// If a routed message would exceed this number of hops it is dropped and ignored.
332 uint8_t _max_hops;
333
334 /// Flag to set if packets are forwarded or not
336
337private:
338
339 /// Temporary mesage buffer
340 static RoutedMessage _tmpMessage;
341
342 /// Local routing table
343 RoutingTableEntry _routes[RH_ROUTING_TABLE_SIZE];
344};
345
346/// @example rf22_router_client.ino
347/// @example rf22_router_server1.ino
348/// @example rf22_router_server2.ino
349/// @example rf22_router_server3.ino
350#endif
351
uint8_t thisAddress()
Definition RHDatagram.cpp:77
Abstract base class for a RadioHead driver.
Definition RHGenericDriver.h:42
RHDatagram subclass for sending addressed, acknowledged, retransmitted datagrams.
Definition RHReliableDatagram.h:96
RHReliableDatagram subclass for sending addressed, optionally acknowledged datagrams multi-hop routed...
Definition RHRouter.h:134
uint8_t sendtoFromSourceWait(uint8_t *buf, uint8_t len, uint8_t dest, uint8_t source, uint8_t flags=0)
Definition RHRouter.cpp:208
void printRoutingTable()
Definition RHRouter.cpp:153
uint8_t _lastE2ESequenceNumber
Definition RHRouter.h:328
void clearRoutingTable()
Definition RHRouter.cpp:193
void setIsaRouter(bool isa_router)
Definition RHRouter.cpp:45
bool recvfromAckTimeout(uint8_t *buf, uint8_t *len, uint16_t timeout, uint8_t *source=NULL, uint8_t *dest=NULL, uint8_t *id=NULL, uint8_t *flags=NULL, uint8_t *hops=NULL)
Definition RHRouter.cpp:344
uint8_t _max_hops
Definition RHRouter.h:332
bool deleteRouteTo(uint8_t dest)
Definition RHRouter.cpp:171
bool recvfromAck(uint8_t *buf, uint8_t *len, uint8_t *source=NULL, uint8_t *dest=NULL, uint8_t *id=NULL, uint8_t *flags=NULL, uint8_t *hops=NULL)
Definition RHRouter.cpp:253
uint8_t sendtoWait(uint8_t *buf, uint8_t len, uint8_t dest, uint8_t flags=0)
Definition RHRouter.cpp:201
void setMaxHops(uint8_t max_hops)
Definition RHRouter.cpp:39
void addRouteTo(uint8_t dest, uint8_t next_hop, uint8_t state=Valid)
Definition RHRouter.cpp:50
void retireOldestRoute()
Definition RHRouter.cpp:186
virtual uint8_t route(RoutedMessage *message, uint8_t messageLen)
Definition RHRouter.cpp:225
void deleteRoute(uint8_t index)
Definition RHRouter.cpp:144
RouteState
Values for the possible states for routes.
Definition RHRouter.h:157
@ Discovering
Discovering a route (not currently used)
Definition RHRouter.h:159
@ Valid
Route is valid.
Definition RHRouter.h:160
@ Invalid
No valid route is known.
Definition RHRouter.h:158
virtual void peekAtMessage(RoutedMessage *message, uint8_t messageLen)
Definition RHRouter.cpp:245
bool getNextValidRoutingTableEntry(RoutingTableEntry *RTE_p, int *lastIndex_p)
Definition RHRouter.cpp:105
bool _isa_router
Flag to set if packets are forwarded or not.
Definition RHRouter.h:335
RoutingTableEntry * getRouteTo(uint8_t dest)
Definition RHRouter.cpp:93
bool init()
Definition RHRouter.cpp:30
Defines the structure of the RHRouter message header, used to keep track of end-to-end delivery param...
Definition RHRouter.h:139
uint8_t hops
Hops traversed so far.
Definition RHRouter.h:142
uint8_t dest
Destination node address.
Definition RHRouter.h:140
uint8_t source
Originator node address.
Definition RHRouter.h:141
uint8_t id
Originator sequence number.
Definition RHRouter.h:143
uint8_t flags
Originator flags.
Definition RHRouter.h:144
Defines the structure of a RHRouter message.
Definition RHRouter.h:150
RoutedMessageHeader header
end-to-end delivery header
Definition RHRouter.h:151
Defines an entry in the routing table.
Definition RHRouter.h:165
uint8_t next_hop
Send via this next hop address.
Definition RHRouter.h:167
uint8_t state
State of this route, one of RouteState.
Definition RHRouter.h:168
uint8_t dest
Destination node address.
Definition RHRouter.h:166