RadioHead
|
RHReliableDatagram subclass for sending addressed, optionally acknowledged datagrams multi-hop routed across a network. More...
#include <RHRouter.h>
Classes | |
struct | RoutedMessage |
Defines the structure of a RHRouter message. More... | |
struct | RoutedMessageHeader |
Defines the structure of the RHRouter message header, used to keep track of end-to-end delivery parameters. More... | |
struct | RoutingTableEntry |
Defines an entry in the routing table. More... | |
Public Types | |
enum | RouteState { Invalid = 0 , Discovering , Valid } |
Values for the possible states for routes. More... | |
Public Member Functions | |
RHRouter (RHGenericDriver &driver, uint8_t thisAddress=0) | |
bool | init () |
void | setIsaRouter (bool isa_router) |
void | setMaxHops (uint8_t max_hops) |
void | addRouteTo (uint8_t dest, uint8_t next_hop, uint8_t state=Valid) |
RoutingTableEntry * | getRouteTo (uint8_t dest) |
bool | deleteRouteTo (uint8_t dest) |
void | retireOldestRoute () |
void | clearRoutingTable () |
void | printRoutingTable () |
bool | getNextValidRoutingTableEntry (RoutingTableEntry *RTE_p, int *lastIndex_p) |
uint8_t | sendtoWait (uint8_t *buf, uint8_t len, uint8_t dest, uint8_t flags=0) |
uint8_t | sendtoFromSourceWait (uint8_t *buf, uint8_t len, uint8_t dest, uint8_t source, uint8_t flags=0) |
bool | recvfromAck (uint8_t *buf, uint8_t *len, uint8_t *source=NULL, uint8_t *dest=NULL, uint8_t *id=NULL, uint8_t *flags=NULL, uint8_t *hops=NULL) |
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, uint8_t *hops=NULL) |
Public Member Functions inherited from RHReliableDatagram | |
RHReliableDatagram (RHGenericDriver &driver, uint8_t thisAddress=0) | |
void | setTimeout (uint16_t timeout) |
void | setRetries (uint8_t retries) |
uint8_t | retries () |
bool | sendtoWait (uint8_t *buf, uint8_t len, uint8_t address) |
bool | recvfromAck (uint8_t *buf, uint8_t *len, uint8_t *from=NULL, uint8_t *to=NULL, uint8_t *id=NULL, uint8_t *flags=NULL) |
bool | recvfromAckTimeout (uint8_t *buf, uint8_t *len, uint16_t timeout, uint8_t *from=NULL, uint8_t *to=NULL, uint8_t *id=NULL, uint8_t *flags=NULL) |
uint32_t | retransmissions () |
void | resetRetransmissions () |
Public Member Functions inherited from RHDatagram | |
RHDatagram (RHGenericDriver &driver, uint8_t thisAddress=0) | |
bool | init () |
void | setThisAddress (uint8_t thisAddress) |
bool | sendto (uint8_t *buf, uint8_t len, uint8_t address) |
bool | recvfrom (uint8_t *buf, uint8_t *len, uint8_t *from=NULL, uint8_t *to=NULL, uint8_t *id=NULL, uint8_t *flags=NULL) |
bool | available () |
void | waitAvailable (uint16_t polldelay=0) |
bool | waitPacketSent () |
bool | waitPacketSent (uint16_t timeout) |
bool | waitAvailableTimeout (uint16_t timeout, uint16_t polldelay=0) |
void | setHeaderTo (uint8_t to) |
void | setHeaderFrom (uint8_t from) |
void | setHeaderId (uint8_t id) |
void | setHeaderFlags (uint8_t set, uint8_t clear=RH_FLAGS_NONE) |
uint8_t | headerTo () |
uint8_t | headerFrom () |
uint8_t | headerId () |
uint8_t | headerFlags () |
uint8_t | thisAddress () |
Protected Member Functions | |
virtual void | peekAtMessage (RoutedMessage *message, uint8_t messageLen) |
virtual uint8_t | route (RoutedMessage *message, uint8_t messageLen) |
void | deleteRoute (uint8_t index) |
Protected Member Functions inherited from RHReliableDatagram | |
void | acknowledge (uint8_t id, uint8_t from) |
bool | haveNewMessage () |
Protected Attributes | |
uint8_t | _lastE2ESequenceNumber |
uint8_t | _max_hops |
bool | _isa_router |
Flag to set if packets are forwarded or not. | |
Protected Attributes inherited from RHDatagram | |
RHGenericDriver & | _driver |
The Driver we are to use. | |
uint8_t | _thisAddress |
The address of this node. | |
RHReliableDatagram subclass for sending addressed, optionally acknowledged datagrams multi-hop routed across a network.
This is a Manager class that extends RHReliableDatagram to handle addressed messages that are reliably transmitted and routed across a network of multiple RHRouter nodes. Each message is transmitted reliably between each hop in order to get from the source node to the destination node.
With RHRouter, routes are hard wired. This means that each node must have programmed in it how to reach each of the other nodes it will be trying to communicate with. This means you must specify the next-hop node address for each of the destination nodes, using the addRouteTo() function.
When sendtoWait() is called with a new message to deliver, and the destination address, RHRouter looks up the next hop node for the destination node. It then uses RHReliableDatagram to (reliably) deliver the message to the next hop (which is expected also to be running an RHRouter). If that next-hop node is not the final destination, it will also look up the next hop for the destination node and (reliably) deliver the message to the next hop. By this method, messages can be delivered across a network of nodes, even if each node cannot hear all of the others in the network. Each time a message is received for another node and retransmitted to the next hop, the HOPS field in the header is incremented. If a message is received for routing to another node which has exceed the routers max_hops, the message will be dropped and ignored. This helps prevent infinite routing loops.
RHRouter supports messages with a dest of RH_BROADCAST_ADDRESS. Such messages are not routed, and are broadcast (once) to all nodes within range.
The recvfromAck() function is responsible not just for receiving and delivering messages addressed to this node (or RH_BROADCAST_ADDRESS), but it is also responsible for routing other message to their next hop. This means that it is important to call recvfromAck() or recvfromAckTimeout() frequently in your main loop. recvfromAck() will return false if it receives a message but it is not for this node.
RHRouter does not provide reliable end-to-end delivery, but uses reliable hop-to-hop delivery. If a message is unable to be delivered to an end node during to a delivery failure between 2 hops, the source node will not be told about it.
Note: This class is most useful for networks of nodes that are essentially static (i.e. the nodes dont move around), and for which the routing never changes. If that is not the case for your proposed network, see RHMesh instead.
The routing table is a local table in RHRouter that holds the information about the next hop node address for each destination address you may want to send a message to. It is your responsibility to make sure every node in an RHRouter network has been configured with a unique address and the routing information so that messages are correctly routed across the network from source node to destination node. This is usually done once in setup() by calling addRouteTo(). The hardwired routing will in general be different on each node, and will depend on the physical topololgy of the network. You can also use addRouteTo() to change a route and deleteRouteTo() to delete a route at run time. Youcan also clear the entire routing table
The Routing Table has limited capacity for entries (defined by RH_ROUTING_TABLE_SIZE, which is 10) if more than RH_ROUTING_TABLE_SIZE are added, the oldest (first) one will be removed by calling retireOldestRoute()
RHRouter add to the lower level RHReliableDatagram (and even lower level RH) class message formats. In those lower level classes, the hop-to-hop message headers are in the RH message headers, and are handled automcatically by tyhe RH hardware. RHRouter and its subclasses add an end-to-end addressing header in the payload of the RH message, and before the RHRouter application data.
You should be careful to note that there are ID and FLAGS fields in the low level per-hop message header too. These are used only for hop-to-hop, and in general will be different to the ones at the RHRouter level.
Bench testing of such networks is notoriously difficult, especially simulating limited radio connectivity between some nodes. To assist testing (both during RH development and for your own networks) RHRouter.cpp has the ability to simulate a number of different small network topologies. Each simulated network supports 4 nodes with addresses 1 to 4. It operates by pretending to not hear RH messages from certain other nodes. You can enable testing with a #define TEST_NETWORK in RHRouter.h The sample programs rf22_mesh_* rely on this feature.
Part of the Arduino RH library for operating with HopeRF RH compatible transceivers (see http://www.hoperf.com)
enum RHRouter::RouteState |
RHRouter::RHRouter | ( | RHGenericDriver & | driver, |
uint8_t | thisAddress = 0 |
||
) |
Constructor.
[in] | driver | The RadioHead driver to use to transport messages. |
[in] | thisAddress | The address to assign to this node. Defaults to 0 |
References _isa_router, _max_hops, and clearRoutingTable().
void RHRouter::addRouteTo | ( | uint8_t | dest, |
uint8_t | next_hop, | ||
uint8_t | state = Valid |
||
) |
Adds a route to the local routing table, or updates it if already present. If there is not enough room the oldest (first) route will be deleted by calling retireOldestRoute().
[in] | dest | The destination node address. RH_BROADCAST_ADDRESS is permitted. |
[in] | next_hop | The address of the next hop to send messages destined for dest |
[in] | state | The satte of the route. Defaults to Valid |
References RHRouter::RoutingTableEntry::dest, Invalid, RHRouter::RoutingTableEntry::next_hop, retireOldestRoute(), and RHRouter::RoutingTableEntry::state.
Referenced by RHMesh::doArp(), RHMesh::peekAtMessage(), RHMesh::recvfromAck(), and RHMesh::route().
void RHRouter::clearRoutingTable | ( | ) |
|
protected |
Deletes a specific rout entry from therouting table
[in] | index | The 0 based index of the routing table entry to delete |
References Invalid, and RHRouter::RoutingTableEntry::state.
Referenced by deleteRouteTo(), and retireOldestRoute().
bool RHRouter::deleteRouteTo | ( | uint8_t | dest | ) |
Deletes from the local routing table any route for the destination node.
[in] | dest | The destination node address |
References deleteRoute().
Referenced by RHMesh::peekAtMessage(), and RHMesh::route().
bool RHRouter::getNextValidRoutingTableEntry | ( | RoutingTableEntry * | RTE_p, |
int * | lastIndex_p | ||
) |
Method for iterating through the current routing table
[in,out] | RTE_p | If a valid entry is found, the entry is copied to this structure caller is responsible for alloocating and deallocating the structure. |
[in,out] | lastIndex_p | points to the index to start searching from. Set to the index of the next valid route found. Set this to -1 to start the search. |
References Valid.
RHRouter::RoutingTableEntry * RHRouter::getRouteTo | ( | uint8_t | dest | ) |
Finds and returns a RoutingTableEntry for the given destination node
[in] | dest | The desired destination node address. |
References Invalid.
Referenced by route(), and RHMesh::sendtoWait().
bool RHRouter::init | ( | ) |
Initialises this instance and the radio module connected to it. Overrides the init() function in RH. Sets max_hops to the default of RH_DEFAULT_MAX_HOPS (30)
References _max_hops, and RHDatagram::init().
|
protectedvirtual |
Lets sublasses peek at messages going past before routing or local delivery. Called by recvfromAck() immediately after it gets the message from RHReliableDatagram
[in] | message | Pointer to the RHRouter message that was received. |
[in] | messageLen | Length of message in octets |
Reimplemented in RHMesh.
Referenced by recvfromAck().
void RHRouter::printRoutingTable | ( | ) |
If RH_HAVE_SERIAL is defined, this will print out the contents of the local routing table using Serial
bool RHRouter::recvfromAck | ( | uint8_t * | buf, |
uint8_t * | len, | ||
uint8_t * | source = NULL , |
||
uint8_t * | dest = NULL , |
||
uint8_t * | id = NULL , |
||
uint8_t * | flags = NULL , |
||
uint8_t * | hops = NULL |
||
) |
Starts the receiver if it is not running already. If there is a valid message available for this node (or RH_BROADCAST_ADDRESS), send an acknowledgement to the last hop address (blocking until this is complete), then copy the application message payload data to buf and return true else return false. If a message is copied, *len is set to the length.. If from is not NULL, the originator SOURCE address is placed in *source. If to is not NULL, the DEST address is placed in *dest. This might be this nodes address or RH_BROADCAST_ADDRESS. This is the preferred function for getting messages addressed to this node. If the message is not a broadcast, acknowledge to the sender before returning.
[in] | buf | Location to copy the received message |
[in,out] | len | Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied. |
[in] | source | If present and not NULL, the referenced uint8_t will be set to the SOURCE address |
[in] | dest | If present and not NULL, the referenced uint8_t will be set to the DEST address |
[in] | id | If present and not NULL, the referenced uint8_t will be set to the ID |
[in] | flags | If present and not NULL, the referenced uint8_t will be set to the FLAGS |
[in] | hops | If present and not NULL, the referenced uint8_t will be set to the HOPS (not just those addressed to this node). |
References _isa_router, _max_hops, RHDatagram::_thisAddress, peekAtMessage(), RHReliableDatagram::recvfromAck(), and route().
Referenced by RHMesh::doArp(), RHMesh::recvfromAck(), and recvfromAckTimeout().
bool RHRouter::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 , |
||
uint8_t * | hops = NULL |
||
) |
Starts the receiver if it is not running already. Similar to recvfromAck(), this will block until either a valid message available for this node or the timeout expires.
[in] | buf | Location to copy the received message |
[in,out] | len | Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied. |
[in] | timeout | Maximum time to wait in milliseconds |
[in] | source | If present and not NULL, the referenced uint8_t will be set to the SOURCE address |
[in] | dest | If present and not NULL, the referenced uint8_t will be set to the DEST address |
[in] | id | If present and not NULL, the referenced uint8_t will be set to the ID |
[in] | flags | If present and not NULL, the referenced uint8_t will be set to the FLAGS |
[in] | hops | If present and not NULL, the referenced uint8_t will be set to the HOPS (not just those addressed to this node). |
References recvfromAck(), and RHDatagram::waitAvailableTimeout().
void RHRouter::retireOldestRoute | ( | ) |
Deletes the oldest (first) route from the local routing table
References deleteRoute().
Referenced by addRouteTo().
|
protectedvirtual |
Finds the next-hop route and sends the message via RHReliableDatagram::sendtoWait(). This is virtual, which lets subclasses override or intercept the route() function. Called by sendtoWait after the message header has been filled in.
[in] | message | Pointer to the RHRouter message to be sent. |
[in] | messageLen | Length of message in octets |
Reimplemented in RHMesh.
References RHRouter::RoutedMessageHeader::dest, getRouteTo(), RHRouter::RoutedMessage::header, route(), and RHReliableDatagram::sendtoWait().
Referenced by recvfromAck(), RHMesh::route(), route(), and sendtoFromSourceWait().
uint8_t RHRouter::sendtoFromSourceWait | ( | uint8_t * | buf, |
uint8_t | len, | ||
uint8_t | dest, | ||
uint8_t | source, | ||
uint8_t | flags = 0 |
||
) |
Similar to sendtoWait() above, but spoofs the source address. For internal use only during routing
[in] | buf | The application message data. |
[in] | len | Number of octets in the application message data. 0 is permitted. |
[in] | dest | The destination node address. |
[in] | source | The (fake) originating node address. |
[in] | flags | Optional flags for use by subclasses or application layer, delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck(). |
References RHDatagram::_driver, _lastE2ESequenceNumber, RHRouter::RoutedMessage::data, RHRouter::RoutedMessageHeader::dest, RHRouter::RoutedMessageHeader::flags, RHRouter::RoutedMessage::header, RHRouter::RoutedMessageHeader::hops, RHRouter::RoutedMessageHeader::id, RHGenericDriver::maxMessageLength(), route(), and RHRouter::RoutedMessageHeader::source.
Referenced by RHMesh::recvfromAck(), and sendtoWait().
uint8_t RHRouter::sendtoWait | ( | uint8_t * | buf, |
uint8_t | len, | ||
uint8_t | dest, | ||
uint8_t | flags = 0 |
||
) |
Sends a message to the destination node. Initialises the RHRouter message header (the SOURCE address is set to the address of this node, HOPS to 0) and calls route() which looks up in the routing table the next hop to deliver to and sends the message to the next hop. Waits for an acknowledgement from the next hop (but not from the destination node (if that is different).
[in] | buf | The application message data |
[in] | len | Number of octets in the application message data. 0 is permitted |
[in] | dest | The destination node address |
[in] | flags | Optional flags for use by subclasses or application layer, delivered end-to-end to the dest address. The receiver can recover the flags with recvFromAck(). |
References RHDatagram::_thisAddress, and sendtoFromSourceWait().
Referenced by RHMesh::doArp(), RHMesh::recvfromAck(), RHMesh::route(), and RHMesh::sendtoWait().
void RHRouter::setIsaRouter | ( | bool | isa_router | ) |
Sets the flag determining if the node will participate in routing. if isa_router is true, the node will be a full participant. If false the node will only respond to packets directed to its address, and act only as a leaf node in the network. The default is true.
[in] | isa_router | true or false |
References _isa_router.
void RHRouter::setMaxHops | ( | uint8_t | max_hops | ) |
Sets the max_hops to the given value This controls the maximum number of hops allowed between source and destination nodes Messages that are not delivered by the time their HOPS field exceeds max_hops on a routing node will be dropped and ignored.
[in] | max_hops | The new value for max_hops |
References _max_hops.
|
protected |
The last end-to-end sequence number to be used Defaults to 0
Referenced by sendtoFromSourceWait().
|
protected |
The maximum number of hops permitted in routed messages. If a routed message would exceed this number of hops it is dropped and ignored.
Referenced by init(), RHMesh::recvfromAck(), recvfromAck(), RHRouter(), and setMaxHops().