Karl Zweimüller / RF22

Description: Library for HopeRF RFM22 / RFM22B transceiver module ported to mbed. Original Software from Mike McCauley (mikem@open.com.au) . See http://www.open.com.au/mikem/arduino/RF22/

Committer:
user Karl Zweimüller
Date:
Tue Feb 14 19:39:36 2012 +0000
Revision:
0:79c6d0071c4c
Child:
5:0386600f3408
Initial Port - Quick and Dirty !!!

Who changed what in which revision?

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