Port of RadioHead version 1.48 to mbed. It is a little messy and only works for SPI at this time.

Committer:
davidr99
Date:
Thu Oct 15 01:27:00 2015 +0000
Revision:
0:ab4e012489ef
Messy start, but a port for RadioHead.; Currently the SPI modulus are the only ones that work.

Who changed what in which revision?

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