RadioHead
RHDatagram.h
1// RHDatagram.h
2// Author: Mike McCauley (mikem@airspayce.com)
3// Copyright (C) 2011 Mike McCauley
4// $Id: RHDatagram.h,v 1.14 2015/08/12 23:18:51 mikem Exp $
5
6#ifndef RHDatagram_h
7#define RHDatagram_h
8
9#include <RHGenericDriver.h>
10
11// This is the maximum possible message size for radios supported by RadioHead.
12// Not all radios support this length, and many are much smaller
13#define RH_MAX_MESSAGE_LEN 255
14
15/////////////////////////////////////////////////////////////////////
16/// \class RHDatagram RHDatagram.h <RHDatagram.h>
17/// \brief Manager class for addressed, unreliable messages
18///
19/// Every RHDatagram node has an 8 bit address (defaults to 0).
20/// Addresses (DEST and SRC) are 8 bit integers with an address of RH_BROADCAST_ADDRESS (0xff)
21/// reserved for broadcast.
22///
23/// \par Media Access Strategy
24///
25/// RHDatagram and the underlying drivers always transmit as soon as sendto() is called.
26///
27/// \par Message Lengths
28///
29/// Not all Radio drivers supported by RadioHead can handle the same message lengths. Some radios can handle
30/// up to 255 octets, and some as few as 28. If you attempt to send a message that is too long for
31/// the underlying driver, sendTo() will return false and will not transmit the message.
32/// It is the programmers responsibility to make
33/// sure that messages passed to sendto() do not exceed the capability of the radio. You can use the
34/// *_MAX_MESSAGE_LENGTH definitions or driver->maxMessageLength() to help.
35///
36/// \par Headers
37///
38/// Each message sent and received by a RadioHead driver includes 4 headers:<br>
39/// \b TO The node address that the message is being sent to (broadcast RH_BROADCAST_ADDRESS (255) is permitted)<br>
40/// \b FROM The node address of the sending node<br>
41/// \b ID A message ID, distinct (over short time scales) for each message sent by a particilar node<br>
42/// \b FLAGS A bitmask of flags. The most significant 4 bits are reserved for use by RadioHead. The least
43/// significant 4 bits are reserved for applications.<br>
44///
46{
47public:
48 /// Constructor.
49 /// \param[in] driver The RadioHead driver to use to transport messages.
50 /// \param[in] thisAddress The address to assign to this node. Defaults to 0
51 RHDatagram(RHGenericDriver& driver, uint8_t thisAddress = 0);
52
53 /// Initialise this instance and the
54 /// driver connected to it.
55 bool init();
56
57 /// Sets the address of this node. Defaults to 0.
58 /// This will be used to set the FROM address of all messages sent by this node.
59 /// In a conventional multinode system, all nodes will have a unique address
60 /// (which you could store in EEPROM).
61 /// \param[in] thisAddress The address of this node
62 void setThisAddress(uint8_t thisAddress);
63
64 /// Sends a message to the node(s) with the given address
65 /// RH_BROADCAST_ADDRESS is a valid address which will cause the message
66 /// to be accepted by all RHDatagram nodes within range.
67 /// \param[in] buf Pointer to the binary message to send
68 /// \param[in] len Number of octets to send (> 0)
69 /// \param[in] address The address to send the message to.
70 /// \return true if the message not too loing fot eh driver, and the message was transmitted.
71 bool sendto(uint8_t* buf, uint8_t len, uint8_t address);
72
73 /// Turns the receiver on if it not already on.
74 /// If there is a valid message available for this node, copy it to buf and return true
75 /// The SRC address is placed in *from if present and not NULL.
76 /// The DEST address is placed in *to if present and not NULL.
77 /// If a message is copied, *len is set to the length.
78 /// You should be sure to call this function frequently enough to not miss any messages
79 /// It is recommended that you call it in your main loop.
80 /// \param[in] buf Location to copy the received message
81 /// \param[in,out] len Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied.
82 /// \param[in] from If present and not NULL, the referenced uint8_t will be set to the FROM address
83 /// \param[in] to If present and not NULL, the referenced uint8_t will be set to the TO address
84 /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID
85 /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS
86 /// (not just those addressed to this node).
87 /// \return true if a valid message was copied to buf
88 bool recvfrom(uint8_t* buf, uint8_t* len, uint8_t* from = NULL, uint8_t* to = NULL, uint8_t* id = NULL, uint8_t* flags = NULL);
89
90 /// Tests whether a new message is available
91 /// from the Driver.
92 /// On most drivers, this will also put the Driver into RHModeRx mode until
93 /// a message is actually received bythe transport, when it will be returned to RHModeIdle.
94 /// This can be called multiple times in a timeout loop.
95 /// \return true if a new, complete, error-free uncollected message is available to be retreived by recv()
96 bool available();
97
98 /// Starts the Driver receiver and blocks until a valid received
99 /// message is available.
100 /// \param[in] polldelay Time between polling available() in milliseconds. This can be useful
101 /// in multitaking environment like Linux to prevent waitAvailableTimeout
102 /// using all the CPU while polling for receiver activity
103 void waitAvailable(uint16_t polldelay = 0);
104
105 /// Blocks until the transmitter
106 /// is no longer transmitting.
107 bool waitPacketSent();
108
109 /// Blocks until the transmitter is no longer transmitting.
110 /// or until the timeout occuers, whichever happens first
111 /// \param[in] timeout Maximum time to wait in milliseconds.
112 /// \return true if the radio completed transmission within the timeout period. False if it timed out.
113 bool waitPacketSent(uint16_t timeout);
114
115 /// Starts the Driver receiver and blocks until a received message is available or a timeout
116 /// \param[in] timeout Maximum time to wait in milliseconds.
117 /// \param[in] polldelay Time between polling available() in milliseconds. This can be useful
118 /// in multitaking environment like Linux to prevent waitAvailableTimeout
119 /// using all the CPU while polling for receiver activity
120 /// \return true if a message is available
121 bool waitAvailableTimeout(uint16_t timeout, uint16_t polldelay = 0);
122
123 /// Sets the TO header to be sent in all subsequent messages
124 /// \param[in] to The new TO header value
125 void setHeaderTo(uint8_t to);
126
127 /// Sets the FROM header to be sent in all subsequent messages
128 /// \param[in] from The new FROM header value
129 void setHeaderFrom(uint8_t from);
130
131 /// Sets the ID header to be sent in all subsequent messages
132 /// \param[in] id The new ID header value
133 void setHeaderId(uint8_t id);
134
135 /// Sets and clears bits in the FLAGS header to be sent in all subsequent messages
136 /// \param[in] set bitmask of bits to be set
137 /// \param[in] clear bitmask of flags to clear
138 void setHeaderFlags(uint8_t set, uint8_t clear = RH_FLAGS_NONE);
139
140 /// Returns the TO header of the last received message
141 /// \return The TO header of the most recently received message.
142 uint8_t headerTo();
143
144 /// Returns the FROM header of the last received message
145 /// \return The FROM header of the most recently received message.
146 uint8_t headerFrom();
147
148 /// Returns the ID header of the last received message
149 /// \return The ID header of the most recently received message.
150 uint8_t headerId();
151
152 /// Returns the FLAGS header of the last received message
153 /// \return The FLAGS header of the most recently received message.
154 uint8_t headerFlags();
155
156 /// Returns the address of this node.
157 /// \return The address of this node
158 uint8_t thisAddress();
159
160protected:
161 /// The Driver we are to use
163
164 /// The address of this node
166};
167
168#endif
Manager class for addressed, unreliable messages.
Definition RHDatagram.h:46
void setHeaderFrom(uint8_t from)
Definition RHDatagram.cpp:87
void setHeaderFlags(uint8_t set, uint8_t clear=RH_FLAGS_NONE)
Definition RHDatagram.cpp:97
bool available()
Definition RHDatagram.cpp:52
uint8_t headerId()
Definition RHDatagram.cpp:112
void setThisAddress(uint8_t thisAddress)
Definition RHDatagram.cpp:25
uint8_t headerFrom()
Definition RHDatagram.cpp:107
bool waitPacketSent()
Definition RHDatagram.cpp:62
RHGenericDriver & _driver
The Driver we are to use.
Definition RHDatagram.h:162
uint8_t thisAddress()
Definition RHDatagram.cpp:77
void setHeaderId(uint8_t id)
Definition RHDatagram.cpp:92
bool recvfrom(uint8_t *buf, uint8_t *len, uint8_t *from=NULL, uint8_t *to=NULL, uint8_t *id=NULL, uint8_t *flags=NULL)
Definition RHDatagram.cpp:39
uint8_t headerFlags()
Definition RHDatagram.cpp:117
void waitAvailable(uint16_t polldelay=0)
Definition RHDatagram.cpp:57
bool sendto(uint8_t *buf, uint8_t len, uint8_t address)
Definition RHDatagram.cpp:33
void setHeaderTo(uint8_t to)
Definition RHDatagram.cpp:82
bool init()
Definition RHDatagram.cpp:17
uint8_t headerTo()
Definition RHDatagram.cpp:102
uint8_t _thisAddress
The address of this node.
Definition RHDatagram.h:165
bool waitAvailableTimeout(uint16_t timeout, uint16_t polldelay=0)
Definition RHDatagram.cpp:72
Abstract base class for a RadioHead driver.
Definition RHGenericDriver.h:42