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 {
47 public:
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 available space in buf. Set 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  void waitAvailable();
101 
102  /// Blocks until the transmitter
103  /// is no longer transmitting.
104  bool waitPacketSent();
105 
106  /// Blocks until the transmitter is no longer transmitting.
107  /// or until the timeout occuers, whichever happens first
108  /// \param[in] timeout Maximum time to wait in milliseconds.
109  /// \return true if the radio completed transmission within the timeout period. False if it timed out.
110  bool waitPacketSent(uint16_t timeout);
111 
112  /// Starts the Driver receiver and blocks until a received message is available or a timeout
113  /// \param[in] timeout Maximum time to wait in milliseconds.
114  /// \return true if a message is available
115  bool waitAvailableTimeout(uint16_t timeout);
116 
117  /// Sets the TO header to be sent in all subsequent messages
118  /// \param[in] to The new TO header value
119  void setHeaderTo(uint8_t to);
120 
121  /// Sets the FROM header to be sent in all subsequent messages
122  /// \param[in] from The new FROM header value
123  void setHeaderFrom(uint8_t from);
124 
125  /// Sets the ID header to be sent in all subsequent messages
126  /// \param[in] id The new ID header value
127  void setHeaderId(uint8_t id);
128 
129  /// Sets and clears bits in the FLAGS header to be sent in all subsequent messages
130  /// \param[in] set bitmask of bits to be set
131  /// \param[in] clear bitmask of flags to clear
132  void setHeaderFlags(uint8_t set, uint8_t clear = RH_FLAGS_NONE);
133 
134  /// Returns the TO header of the last received message
135  /// \return The TO header of the most recently received message.
136  uint8_t headerTo();
137 
138  /// Returns the FROM header of the last received message
139  /// \return The FROM header of the most recently received message.
140  uint8_t headerFrom();
141 
142  /// Returns the ID header of the last received message
143  /// \return The ID header of the most recently received message.
144  uint8_t headerId();
145 
146  /// Returns the FLAGS header of the last received message
147  /// \return The FLAGS header of the most recently received message.
148  uint8_t headerFlags();
149 
150  /// Returns the address of this node.
151  /// \return The address of this node
152  uint8_t thisAddress();
153 
154 protected:
155  /// The Driver we are to use
157 
158  /// The address of this node
159  uint8_t _thisAddress;
160 };
161 
162 #endif
RHDatagram::thisAddress
uint8_t thisAddress()
Definition: RHDatagram.cpp:77
RHDatagram::setHeaderFlags
void setHeaderFlags(uint8_t set, uint8_t clear=RH_FLAGS_NONE)
Definition: RHDatagram.cpp:97
RHGenericDriver::available
virtual bool available()=0
RHGenericDriver::waitAvailableTimeout
virtual bool waitAvailableTimeout(uint16_t timeout)
Definition: RHGenericDriver.cpp:38
RHGenericDriver::headerFlags
virtual uint8_t headerFlags()
Definition: RHGenericDriver.cpp:149
RHGenericDriver
Abstract base class for a RadioHead driver.
Definition: RHGenericDriver.h:41
RHDatagram::headerFlags
uint8_t headerFlags()
Definition: RHDatagram.cpp:117
RHDatagram::sendto
bool sendto(uint8_t *buf, uint8_t len, uint8_t address)
Definition: RHDatagram.cpp:33
RHGenericDriver::headerId
virtual uint8_t headerId()
Definition: RHGenericDriver.cpp:144
RHDatagram::recvfrom
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
RHDatagram::setHeaderFrom
void setHeaderFrom(uint8_t from)
Definition: RHDatagram.cpp:87
RHDatagram::headerTo
uint8_t headerTo()
Definition: RHDatagram.cpp:102
RHDatagram::waitAvailableTimeout
bool waitAvailableTimeout(uint16_t timeout)
Definition: RHDatagram.cpp:72
RHDatagram::headerFrom
uint8_t headerFrom()
Definition: RHDatagram.cpp:107
RHGenericDriver::headerFrom
virtual uint8_t headerFrom()
Definition: RHGenericDriver.cpp:139
RHGenericDriver::setThisAddress
virtual void setThisAddress(uint8_t thisAddress)
Definition: RHGenericDriver.cpp:108
RHGenericDriver::setHeaderId
virtual void setHeaderId(uint8_t id)
Definition: RHGenericDriver.cpp:123
RHDatagram::waitAvailable
void waitAvailable()
Definition: RHDatagram.cpp:57
RHDatagram::RHDatagram
RHDatagram(RHGenericDriver &driver, uint8_t thisAddress=0)
Definition: RHDatagram.cpp:8
RHDatagram::headerId
uint8_t headerId()
Definition: RHDatagram.cpp:112
RHGenericDriver::recv
virtual bool recv(uint8_t *buf, uint8_t *len)=0
RHGenericDriver::send
virtual bool send(const uint8_t *data, uint8_t len)=0
RHDatagram::available
bool available()
Definition: RHDatagram.cpp:52
RHDatagram::setHeaderId
void setHeaderId(uint8_t id)
Definition: RHDatagram.cpp:92
RHGenericDriver::setHeaderTo
virtual void setHeaderTo(uint8_t to)
Definition: RHGenericDriver.cpp:113
RHGenericDriver::setHeaderFrom
virtual void setHeaderFrom(uint8_t from)
Definition: RHGenericDriver.cpp:118
RHGenericDriver::init
virtual bool init()
Definition: RHGenericDriver.cpp:23
RHDatagram
Manager class for addressed, unreliable messages.
Definition: RHDatagram.h:45
RHGenericDriver::headerTo
virtual uint8_t headerTo()
Definition: RHGenericDriver.cpp:134
RHDatagram::waitPacketSent
bool waitPacketSent()
Definition: RHDatagram.cpp:62
RHDatagram::init
bool init()
Definition: RHDatagram.cpp:17
RHDatagram::setHeaderTo
void setHeaderTo(uint8_t to)
Definition: RHDatagram.cpp:82
RHDatagram::_thisAddress
uint8_t _thisAddress
The address of this node.
Definition: RHDatagram.h:159
RHGenericDriver::setHeaderFlags
virtual void setHeaderFlags(uint8_t set, uint8_t clear=RH_FLAGS_APPLICATION_SPECIFIC)
Definition: RHGenericDriver.cpp:128
RHDatagram::_driver
RHGenericDriver & _driver
The Driver we are to use.
Definition: RHDatagram.h:156
RHGenericDriver::waitPacketSent
virtual bool waitPacketSent()
Definition: RHGenericDriver.cpp:52
RHDatagram::setThisAddress
void setThisAddress(uint8_t thisAddress)
Definition: RHDatagram.cpp:25
RHGenericDriver::waitAvailable
virtual void waitAvailable()
Definition: RHGenericDriver.cpp:29