Dependents:   New

Committer:
SangSTBK
Date:
Mon Jul 02 01:29:58 2012 +0000
Revision:
0:e16ffa7cb900
RF

Who changed what in which revision?

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