Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Threaded_LoRa_Modem
RHRouter.h
00001 // RHRouter.h 00002 // 00003 // Author: Mike McCauley (mikem@airspayce.com) 00004 // Copyright (C) 2011 Mike McCauley 00005 // $Id: RHRouter.h,v 1.9 2014/08/10 20:55:17 mikem Exp $ 00006 00007 #ifndef RHRouter_h 00008 #define RHRouter_h 00009 00010 #include <RHReliableDatagram.h> 00011 00012 // Default max number of hops we will route 00013 #define RH_DEFAULT_MAX_HOPS 30 00014 00015 // The default size of the routing table we keep 00016 #define RH_ROUTING_TABLE_SIZE 10 00017 00018 // Error codes 00019 #define RH_ROUTER_ERROR_NONE 0 00020 #define RH_ROUTER_ERROR_INVALID_LENGTH 1 00021 #define RH_ROUTER_ERROR_NO_ROUTE 2 00022 #define RH_ROUTER_ERROR_TIMEOUT 3 00023 #define RH_ROUTER_ERROR_NO_REPLY 4 00024 #define RH_ROUTER_ERROR_UNABLE_TO_DELIVER 5 00025 00026 // This size of RH_ROUTER_MAX_MESSAGE_LEN is OK for Arduino Mega, but too big for 00027 // Duemilanova. Size of 50 works with the sample router programs on Duemilanova. 00028 #define RH_ROUTER_MAX_MESSAGE_LEN (RH_MAX_MESSAGE_LEN - sizeof(RHRouter::RoutedMessageHeader)) 00029 //#define RH_ROUTER_MAX_MESSAGE_LEN 50 00030 00031 // These allow us to define a simulated network topology for testing purposes 00032 // See RHRouter.cpp for details 00033 //#define RH_TEST_NETWORK 1 00034 //#define RH_TEST_NETWORK 2 00035 //#define RH_TEST_NETWORK 3 00036 //#define RH_TEST_NETWORK 4 00037 00038 ///////////////////////////////////////////////////////////////////// 00039 /// \class RHRouter RHRouter.h <RHRouter.h> 00040 /// \brief RHReliableDatagram subclass for sending addressed, optionally acknowledged datagrams 00041 /// multi-hop routed across a network. 00042 /// 00043 /// Manager class that extends RHReliableDatagram to define addressed messages 00044 /// That are reliably transmitted and routed across a network. Each message is transmitted reliably 00045 /// between each hop in order to get from the source node to the destination node. 00046 /// 00047 /// With RHRouter, routes are hard wired. This means that each node must have programmed 00048 /// in it how to reach each of the other nodes it will be trying to communicate with. 00049 /// This means you must specify the next-hop node address for each of the destination nodes, 00050 /// using the addRouteTo() function. 00051 /// 00052 /// When sendtoWait() is called with a new message to deliver, and the destination address, 00053 /// RHRouter looks up the next hop node for the destination node. It then uses 00054 /// RHReliableDatagram to (reliably) deliver the message to the next hop 00055 /// (which is expected also to be running an RHRouter). If that next-hop node is not 00056 /// the final destination, it will also look up the next hop for the destination node and 00057 /// (reliably) deliver the message to the next hop. By this method, messages can be delivered 00058 /// across a network of nodes, even if each node cannot hear all of the others in the network. 00059 /// Each time a message is received for another node and retransmitted to the next hop, 00060 /// the HOPS filed in teh header is incremented. If a message is received for routing to another node 00061 /// which has exceed the routers max_hops, the message wioll be dropped and ignored. 00062 /// This helps prevent infinite routing loops. 00063 /// 00064 /// RHRouter supports messages with a dest of RH_BROADCAST_ADDRESS. Such messages are not routed, 00065 /// and are broadcast (once) to all nodes within range. 00066 /// 00067 /// The recvfromAck() function is responsible not just for receiving and delivering 00068 /// messages addressed to this node (or RH_BROADCAST_ADDRESS), but 00069 /// it is also responsible for routing other message to their next hop. This means that it is important to 00070 /// call recvfromAck() or recvfromAckTimeout() frequently in your main loop. recvfromAck() will return 00071 /// false if it receives a message but it is not for this node. 00072 /// 00073 /// RHRouter does not provide reliable end-to-end delivery, but uses reliable hop-to-hop delivery. 00074 /// If a message is unable to be delivered to an end node during to a delivery failure between 2 hops, 00075 /// the source node will not be told about it. 00076 /// 00077 /// Note: This class is most useful for networks of nodes that are essentially static 00078 /// (i.e. the nodes dont move around), and for which the 00079 /// routing never changes. If that is not the case for your proposed network, see RHMesh instead. 00080 /// 00081 /// \par The Routing Table 00082 /// 00083 /// The routing table is a local table in RHRouter that holds the information about the next hop node 00084 /// address for each destination address you may want to send a message to. It is your responsibility 00085 /// to make sure every node in an RHRouter network has been configured with a unique address and the 00086 /// routing information so that messages are correctly routed across the network from source node to 00087 /// destination node. This is usually done once in setup() by calling addRouteTo(). 00088 /// The hardwired routing will in general be different on each node, and will depend on the physical 00089 /// topololgy of the network. 00090 /// You can also use addRouteTo() to change a route and 00091 /// deleteRouteTo() to delete a route at run time. Youcan also clear the entire routing table 00092 /// 00093 /// The Routing Table has limited capacity for entries (defined by RH_ROUTING_TABLE_SIZE, which is 10) 00094 /// if more than RH_ROUTING_TABLE_SIZE are added, the oldest (first) one will be removed by calling 00095 /// retireOldestRoute() 00096 /// 00097 /// \par Message Format 00098 /// 00099 /// RHRouter add to the lower level RHReliableDatagram (and even lower level RH) class message formats. 00100 /// In those lower level classes, the hop-to-hop message headers are in the RH message headers, 00101 /// and are handled automcatically by tyhe RH hardware. 00102 /// RHRouter and its subclasses add an end-to-end addressing header in the payload of the RH message, 00103 /// and before the RHRouter application data. 00104 /// - 1 octet DEST, the destination node address (ie the address of the final 00105 /// destination node for this message) 00106 /// - 1 octet SOURCE, the source node address (ie the address of the originating node that first sent 00107 /// the message). 00108 /// - 1 octet HOPS, the number of hops this message has traversed so far. 00109 /// - 1 octet ID, an incrementing message ID for end-to-end message tracking for use by subclasses. 00110 /// Not used by RHRouter. 00111 /// - 1 octet FLAGS, a bitmask for use by subclasses. Not used by RHRouter. 00112 /// - 0 or more octets DATA, the application payload data. The length of this data is implicit 00113 /// in the length of the entire message. 00114 /// 00115 /// You should be careful to note that there are ID and FLAGS fields in the low level per-hop 00116 /// message header too. These are used only for hop-to-hop, and in general will be different to 00117 /// the ones at the RHRouter level. 00118 /// 00119 /// \par Testing 00120 /// 00121 /// Bench testing of such networks is notoriously difficult, especially simulating limited radio 00122 /// connectivity between some nodes. 00123 /// To assist testing (both during RH development and for your own networks) 00124 /// RHRouter.cpp has the ability to 00125 /// simulate a number of different small network topologies. Each simulated network supports 4 nodes with 00126 /// addresses 1 to 4. It operates by pretending to not hear RH messages from certain other nodes. 00127 /// You can enable testing with a \#define TEST_NETWORK in RHRouter.h 00128 /// The sample programs rf22_mesh_* rely on this feature. 00129 /// 00130 /// Part of the Arduino RH library for operating with HopeRF RH compatible transceivers 00131 /// (see http://www.hoperf.com) 00132 class RHRouter : public RHReliableDatagram 00133 { 00134 public: 00135 00136 /// Defines the structure of the RHRouter message header, used to keep track of end-to-end delivery parameters 00137 typedef struct 00138 { 00139 uint8_t dest; ///< Destination node address 00140 uint8_t source; ///< Originator node address 00141 uint8_t hops; ///< Hops traversed so far 00142 uint8_t id; ///< Originator sequence number 00143 uint8_t flags; ///< Originator flags 00144 // Data follows, Length is implicit in the overall message length 00145 } RoutedMessageHeader; 00146 00147 /// Defines the structure of a RHRouter message 00148 typedef struct 00149 { 00150 RoutedMessageHeader header; ///< end-to-end delivery header 00151 uint8_t data[RH_ROUTER_MAX_MESSAGE_LEN]; ///< Application payload data 00152 } RoutedMessage; 00153 00154 /// Values for the possible states for routes 00155 typedef enum 00156 { 00157 Invalid = 0, ///< No valid route is known 00158 Discovering, ///< Discovering a route (not currently used) 00159 Valid ///< Route is valid 00160 } RouteState; 00161 00162 /// Defines an entry in the routing table 00163 typedef struct 00164 { 00165 uint8_t dest; ///< Destination node address 00166 uint8_t next_hop; ///< Send via this next hop address 00167 uint8_t state; ///< State of this route, one of RouteState 00168 } RoutingTableEntry; 00169 00170 /// Constructor. 00171 /// \param[in] driver The RadioHead driver to use to transport messages. 00172 /// \param[in] thisAddress The address to assign to this node. Defaults to 0 00173 RHRouter(RHGenericDriver& driver, uint8_t thisAddress = 0); 00174 00175 /// Initialises this instance and the radio module connected to it. 00176 /// Overrides the init() function in RH. 00177 /// Sets max_hops to the default of RH_DEFAULT_MAX_HOPS (30) 00178 bool init(); 00179 00180 /// Sets the max_hops to the given value 00181 /// This controls the maximum number of hops allowed between source and destination nodes 00182 /// Messages that are not delivered by the time their HOPS field exceeds max_hops on a 00183 /// routing node will be dropped and ignored. 00184 /// \param [in] max_hops The new value for max_hops 00185 void setMaxHops(uint8_t max_hops); 00186 00187 /// Adds a route to the local routing table, or updates it if already present. 00188 /// If there is not enough room the oldest (first) route will be deleted by calling retireOldestRoute(). 00189 /// \param [in] dest The destination node address. RH_BROADCAST_ADDRESS is permitted. 00190 /// \param [in] next_hop The address of the next hop to send messages destined for dest 00191 /// \param [in] state The satte of the route. Defaults to Valid 00192 void addRouteTo(uint8_t dest, uint8_t next_hop, uint8_t state = Valid); 00193 00194 /// Finds and returns a RoutingTableEntry for the given destination node 00195 /// \param [in] dest The desired destination node address. 00196 /// \return pointer to a RoutingTableEntry for dest 00197 RoutingTableEntry* getRouteTo(uint8_t dest); 00198 00199 /// Deletes from the local routing table any route for the destination node. 00200 /// \param [in] dest The destination node address 00201 /// \return true if the route was present 00202 bool deleteRouteTo(uint8_t dest); 00203 00204 /// Deletes the oldest (first) route from the 00205 /// local routing table 00206 void retireOldestRoute(); 00207 00208 /// Clears all entries from the 00209 /// local routing table 00210 void clearRoutingTable(); 00211 00212 /// If RH_HAVE_SERIAL is defined, this will print out the contents of the local 00213 /// routing table using Serial 00214 void printRoutingTable(); 00215 00216 /// Sends a message to the destination node. Initialises the RHRouter message header 00217 /// (the SOURCE address is set to the address of this node, HOPS to 0) and calls 00218 /// route() which looks up in the routing table the next hop to deliver to and sends the 00219 /// message to the next hop. Waits for an acknowledgement from the next hop 00220 /// (but not from the destination node (if that is different). 00221 /// \param [in] buf The application message data 00222 /// \param [in] len Number of octets in the application message data. 0 is permitted 00223 /// \param [in] dest The destination node address 00224 /// \param [in] flags Optional flags for use by subclasses or application layer, 00225 /// delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck(). 00226 /// \return The result code: 00227 /// - RH_ROUTER_ERROR_NONE Message was routed and delivered to the next hop 00228 /// (not necessarily to the final dest address) 00229 /// - RH_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table 00230 /// - RH_ROUTER_ERROR_UNABLE_TO_DELIVER Not able to deliver to the next hop 00231 /// (usually because it dod not acknowledge due to being off the air or out of range 00232 uint8_t sendtoWait(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t flags = 0); 00233 00234 /// Similar to sendtoWait() above, but spoofs the source address. 00235 /// For internal use only during routing 00236 /// \param [in] buf The application message data. 00237 /// \param [in] len Number of octets in the application message data. 0 is permitted. 00238 /// \param [in] dest The destination node address. 00239 /// \param [in] source The (fake) originating node address. 00240 /// \param [in] flags Optional flags for use by subclasses or application layer, 00241 /// delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck(). 00242 /// \return The result code: 00243 /// - RH_ROUTER_ERROR_NONE Message was routed and deliverd to the next hop 00244 /// (not necessarily to the final dest address) 00245 /// - RH_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table 00246 /// - RH_ROUTER_ERROR_UNABLE_TO_DELIVER Noyt able to deliver to the next hop 00247 /// (usually because it dod not acknowledge due to being off the air or out of range 00248 uint8_t sendtoFromSourceWait(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t source, uint8_t flags = 0); 00249 00250 /// Starts the receiver if it is not running already. 00251 /// If there is a valid message available for this node (or RH_BROADCAST_ADDRESS), 00252 /// send an acknowledgement to the last hop 00253 /// address (blocking until this is complete), then copy the application message payload data 00254 /// to buf and return true 00255 /// else return false. 00256 /// If a message is copied, *len is set to the length.. 00257 /// If from is not NULL, the originator SOURCE address is placed in *source. 00258 /// If to is not NULL, the DEST address is placed in *dest. This might be this nodes address or 00259 /// RH_BROADCAST_ADDRESS. 00260 /// This is the preferred function for getting messages addressed to this node. 00261 /// If the message is not a broadcast, acknowledge to the sender before returning. 00262 /// \param[in] buf Location to copy the received message 00263 /// \param[in,out] len Available space in buf. Set to the actual number of octets copied. 00264 /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address 00265 /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address 00266 /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID 00267 /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS 00268 /// (not just those addressed to this node). 00269 /// \return true if a valid message was recvived for this node copied to buf 00270 bool recvfromAck(uint8_t* buf, uint8_t* len, uint8_t* source = NULL, uint8_t* dest = NULL, uint8_t* id = NULL, uint8_t* flags = NULL); 00271 00272 /// Starts the receiver if it is not running already. 00273 /// Similar to recvfromAck(), this will block until either a valid message available for this node 00274 /// or the timeout expires. 00275 /// \param[in] buf Location to copy the received message 00276 /// \param[in,out] len Available space in buf. Set to the actual number of octets copied. 00277 /// \param[in] timeout Maximum time to wait in milliseconds 00278 /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address 00279 /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address 00280 /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID 00281 /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS 00282 /// (not just those addressed to this node). 00283 /// \return true if a valid message was copied to buf 00284 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); 00285 00286 protected: 00287 00288 /// Lets sublasses peek at messages going 00289 /// past before routing or local delivery. 00290 /// Called by recvfromAck() immediately after it gets the message from RHReliableDatagram 00291 /// \param [in] message Pointer to the RHRouter message that was received. 00292 /// \param [in] messageLen Length of message in octets 00293 virtual void peekAtMessage(RoutedMessage* message, uint8_t messageLen); 00294 00295 /// Finds the next-hop route and sends the message via RHReliableDatagram::sendtoWait(). 00296 /// This is virtual, which lets subclasses override or intercept the route() function. 00297 /// Called by sendtoWait after the message header has been filled in. 00298 /// \param [in] message Pointer to the RHRouter message to be sent. 00299 /// \param [in] messageLen Length of message in octets 00300 virtual uint8_t route(RoutedMessage* message, uint8_t messageLen); 00301 00302 /// Deletes a specific rout entry from therouting table 00303 /// \param [in] index The 0 based index of the routing table entry to delete 00304 void deleteRoute(uint8_t index); 00305 00306 /// The last end-to-end sequence number to be used 00307 /// Defaults to 0 00308 uint8_t _lastE2ESequenceNumber; 00309 00310 /// The maximum number of hops permitted in routed messages. 00311 /// If a routed message would exceed this number of hops it is dropped and ignored. 00312 uint8_t _max_hops; 00313 00314 private: 00315 00316 /// Temporary mesage buffer 00317 static RoutedMessage _tmpMessage; 00318 00319 /// Local routing table 00320 RoutingTableEntry _routes[RH_ROUTING_TABLE_SIZE]; 00321 }; 00322 00323 /// @example rf22_router_client.pde 00324 /// @example rf22_router_server1.pde 00325 /// @example rf22_router_server2.pde 00326 /// @example rf22_router_server3.pde 00327 #endif 00328
Generated on Thu Jul 14 2022 12:14:50 by
1.7.2