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.11 2019/09/06 04:40:40 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 {
135 public:
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
153  } RoutedMessage;
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
161  } RouteState;
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. 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  /// Sends a message to the destination node. Initialises the RHRouter message header
226  /// (the SOURCE address is set to the address of this node, HOPS to 0) and calls
227  /// route() which looks up in the routing table the next hop to deliver to and sends the
228  /// message to the next hop. Waits for an acknowledgement from the next hop
229  /// (but not from the destination node (if that is different).
230  /// \param [in] buf The application message data
231  /// \param [in] len Number of octets in the application message data. 0 is permitted
232  /// \param [in] dest The destination node address
233  /// \param [in] flags Optional flags for use by subclasses or application layer,
234  /// delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck().
235  /// \return The result code:
236  /// - RH_ROUTER_ERROR_NONE Message was routed and delivered to the next hop
237  /// (not necessarily to the final dest address)
238  /// - RH_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table
239  /// - RH_ROUTER_ERROR_UNABLE_TO_DELIVER Not able to deliver to the next hop
240  /// (usually because it dod not acknowledge due to being off the air or out of range
241  uint8_t sendtoWait(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t flags = 0);
242 
243  /// Similar to sendtoWait() above, but spoofs the source address.
244  /// For internal use only during routing
245  /// \param [in] buf The application message data.
246  /// \param [in] len Number of octets in the application message data. 0 is permitted.
247  /// \param [in] dest The destination node address.
248  /// \param [in] source The (fake) originating node address.
249  /// \param [in] flags Optional flags for use by subclasses or application layer,
250  /// delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck().
251  /// \return The result code:
252  /// - RH_ROUTER_ERROR_NONE Message was routed and deliverd to the next hop
253  /// (not necessarily to the final dest address)
254  /// - RH_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table
255  /// - RH_ROUTER_ERROR_UNABLE_TO_DELIVER Noyt able to deliver to the next hop
256  /// (usually because it dod not acknowledge due to being off the air or out of range
257  uint8_t sendtoFromSourceWait(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t source, uint8_t flags = 0);
258 
259  /// Starts the receiver if it is not running already.
260  /// If there is a valid message available for this node (or RH_BROADCAST_ADDRESS),
261  /// send an acknowledgement to the last hop
262  /// address (blocking until this is complete), then copy the application message payload data
263  /// to buf and return true
264  /// else return false.
265  /// If a message is copied, *len is set to the length..
266  /// If from is not NULL, the originator SOURCE address is placed in *source.
267  /// If to is not NULL, the DEST address is placed in *dest. This might be this nodes address or
268  /// RH_BROADCAST_ADDRESS.
269  /// This is the preferred function for getting messages addressed to this node.
270  /// If the message is not a broadcast, acknowledge to the sender before returning.
271  /// \param[in] buf Location to copy the received message
272  /// \param[in,out] len Available space in buf. Set to the actual number of octets copied.
273  /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address
274  /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address
275  /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID
276  /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS
277  /// (not just those addressed to this node).
278  /// \return true if a valid message was recvived for this node copied to buf
279  bool recvfromAck(uint8_t* buf, uint8_t* len, uint8_t* source = NULL, uint8_t* dest = NULL, uint8_t* id = NULL, uint8_t* flags = NULL);
280 
281  /// Starts the receiver if it is not running already.
282  /// Similar to recvfromAck(), this will block until either a valid message available for this node
283  /// or the timeout expires.
284  /// \param[in] buf Location to copy the received message
285  /// \param[in,out] len Available space in buf. Set to the actual number of octets copied.
286  /// \param[in] timeout Maximum time to wait in milliseconds
287  /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address
288  /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address
289  /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID
290  /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS
291  /// (not just those addressed to this node).
292  /// \return true if a valid message was copied to buf
293  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);
294 
295 protected:
296 
297  /// Lets sublasses peek at messages going
298  /// past before routing or local delivery.
299  /// Called by recvfromAck() immediately after it gets the message from RHReliableDatagram
300  /// \param [in] message Pointer to the RHRouter message that was received.
301  /// \param [in] messageLen Length of message in octets
302  virtual void peekAtMessage(RoutedMessage* message, uint8_t messageLen);
303 
304  /// Finds the next-hop route and sends the message via RHReliableDatagram::sendtoWait().
305  /// This is virtual, which lets subclasses override or intercept the route() function.
306  /// Called by sendtoWait after the message header has been filled in.
307  /// \param [in] message Pointer to the RHRouter message to be sent.
308  /// \param [in] messageLen Length of message in octets
309  virtual uint8_t route(RoutedMessage* message, uint8_t messageLen);
310 
311  /// Deletes a specific rout entry from therouting table
312  /// \param [in] index The 0 based index of the routing table entry to delete
313  void deleteRoute(uint8_t index);
314 
315  /// The last end-to-end sequence number to be used
316  /// Defaults to 0
318 
319  /// The maximum number of hops permitted in routed messages.
320  /// If a routed message would exceed this number of hops it is dropped and ignored.
321  uint8_t _max_hops;
322 
323  /// Flag to set if packets are forwarded or not
325 
326 private:
327 
328  /// Temporary mesage buffer
329  static RoutedMessage _tmpMessage;
330 
331  /// Local routing table
332  RoutingTableEntry _routes[RH_ROUTING_TABLE_SIZE];
333 };
334 
335 /// @example rf22_router_client.pde
336 /// @example rf22_router_server1.pde
337 /// @example rf22_router_server2.pde
338 /// @example rf22_router_server3.pde
339 #endif
340 
RHRouter::RouteState
RouteState
Values for the possible states for routes.
Definition: RHRouter.h:156
RHRouter::sendtoFromSourceWait
uint8_t sendtoFromSourceWait(uint8_t *buf, uint8_t len, uint8_t dest, uint8_t source, uint8_t flags=0)
Definition: RHRouter.cpp:167
RHDatagram::thisAddress
uint8_t thisAddress()
Definition: RHDatagram.cpp:77
RHRouter::Invalid
@ Invalid
No valid route is known.
Definition: RHRouter.h:158
RHRouter::RoutingTableEntry::state
uint8_t state
State of this route, one of RouteState.
Definition: RHRouter.h:168
RHGenericDriver::maxMessageLength
virtual uint8_t maxMessageLength()=0
RHRouter::setMaxHops
void setMaxHops(uint8_t max_hops)
Definition: RHRouter.cpp:39
RHRouter::printRoutingTable
void printRoutingTable()
Definition: RHRouter.cpp:112
RHReliableDatagram::sendtoWait
bool sendtoWait(uint8_t *buf, uint8_t len, uint8_t address)
Definition: RHReliableDatagram.cpp:48
RHRouter::RoutedMessageHeader::dest
uint8_t dest
Destination node address.
Definition: RHRouter.h:140
RHRouter::RoutedMessage::data
uint8_t data[RH_ROUTER_MAX_MESSAGE_LEN]
Application payload data.
Definition: RHRouter.h:152
RHReliableDatagram::recvfromAck
bool recvfromAck(uint8_t *buf, uint8_t *len, uint8_t *from=NULL, uint8_t *to=NULL, uint8_t *id=NULL, uint8_t *flags=NULL)
Definition: RHReliableDatagram.cpp:128
RHRouter::Valid
@ Valid
Route is valid.
Definition: RHRouter.h:160
RHRouter::peekAtMessage
virtual void peekAtMessage(RoutedMessage *message, uint8_t messageLen)
Definition: RHRouter.cpp:204
RHRouter::RoutingTableEntry::next_hop
uint8_t next_hop
Send via this next hop address.
Definition: RHRouter.h:167
RHRouter::route
virtual uint8_t route(RoutedMessage *message, uint8_t messageLen)
Definition: RHRouter.cpp:184
RHGenericDriver
Abstract base class for a RadioHead driver.
Definition: RHGenericDriver.h:41
RHRouter::RoutedMessageHeader::source
uint8_t source
Originator node address.
Definition: RHRouter.h:141
RHRouter::RoutedMessageHeader
Defines the structure of the RHRouter message header, used to keep track of end-to-end delivery param...
Definition: RHRouter.h:138
RHRouter::recvfromAck
bool recvfromAck(uint8_t *buf, uint8_t *len, uint8_t *source=NULL, uint8_t *dest=NULL, uint8_t *id=NULL, uint8_t *flags=NULL)
Definition: RHRouter.cpp:212
RHRouter::_max_hops
uint8_t _max_hops
Definition: RHRouter.h:321
RHRouter::addRouteTo
void addRouteTo(uint8_t dest, uint8_t next_hop, uint8_t state=Valid)
Definition: RHRouter.cpp:50
RHRouter::deleteRoute
void deleteRoute(uint8_t index)
Definition: RHRouter.cpp:103
RHRouter
RHReliableDatagram subclass for sending addressed, optionally acknowledged datagrams multi-hop routed...
Definition: RHRouter.h:133
RHRouter::Discovering
@ Discovering
Discovering a route (not currently used)
Definition: RHRouter.h:159
RHDatagram::waitAvailableTimeout
bool waitAvailableTimeout(uint16_t timeout)
Definition: RHDatagram.cpp:72
RHRouter::RoutedMessageHeader::id
uint8_t id
Originator sequence number.
Definition: RHRouter.h:143
RHRouter::RoutingTableEntry
Defines an entry in the routing table.
Definition: RHRouter.h:164
RHRouter::RHRouter
RHRouter(RHGenericDriver &driver, uint8_t thisAddress=0)
Definition: RHRouter.cpp:20
RHRouter::sendtoWait
uint8_t sendtoWait(uint8_t *buf, uint8_t len, uint8_t dest, uint8_t flags=0)
Definition: RHRouter.cpp:160
RHRouter::RoutedMessageHeader::flags
uint8_t flags
Originator flags.
Definition: RHRouter.h:144
RHRouter::retireOldestRoute
void retireOldestRoute()
Definition: RHRouter.cpp:145
RHRouter::RoutedMessage::header
RoutedMessageHeader header
end-to-end delivery header
Definition: RHRouter.h:151
RHRouter::getRouteTo
RoutingTableEntry * getRouteTo(uint8_t dest)
Definition: RHRouter.cpp:93
RHReliableDatagram
RHDatagram subclass for sending addressed, acknowledged, retransmitted datagrams.
Definition: RHReliableDatagram.h:93
RHRouter::init
bool init()
Definition: RHRouter.cpp:30
RHRouter::_lastE2ESequenceNumber
uint8_t _lastE2ESequenceNumber
Definition: RHRouter.h:317
RHRouter::setIsaRouter
void setIsaRouter(bool isa_router)
Definition: RHRouter.cpp:45
RHRouter::RoutedMessage
Defines the structure of a RHRouter message.
Definition: RHRouter.h:149
RHDatagram::init
bool init()
Definition: RHDatagram.cpp:17
RHDatagram::_thisAddress
uint8_t _thisAddress
The address of this node.
Definition: RHDatagram.h:159
RHRouter::deleteRouteTo
bool deleteRouteTo(uint8_t dest)
Definition: RHRouter.cpp:130
RHRouter::RoutedMessageHeader::hops
uint8_t hops
Hops traversed so far.
Definition: RHRouter.h:142
RHDatagram::_driver
RHGenericDriver & _driver
The Driver we are to use.
Definition: RHDatagram.h:156
RHRouter::RoutingTableEntry::dest
uint8_t dest
Destination node address.
Definition: RHRouter.h:166
RHRouter::clearRoutingTable
void clearRoutingTable()
Definition: RHRouter.cpp:152
RHRouter::_isa_router
bool _isa_router
Flag to set if packets are forwarded or not.
Definition: RHRouter.h:324
RHRouter::recvfromAckTimeout
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)
Definition: RHRouter.cpp:302