RadioHead
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
RHGenericDriver Class Referenceabstract

Abstract base class for a RadioHead driver. More...

#include <RHGenericDriver.h>

Inheritance diagram for RHGenericDriver:
RHEncryptedDriver RHNRFSPIDriver RHSPIDriver RH_ASK RH_E32 RH_LoRaFileOps RH_NRF51 RH_Serial RH_TCP

Public Types

enum  RHMode {
  RHModeInitialising = 0 , RHModeSleep , RHModeIdle , RHModeTx ,
  RHModeRx , RHModeCad
}
 Defines different operating modes for the transport hardware. More...
 

Public Member Functions

 RHGenericDriver ()
 Constructor.
 
virtual ~RHGenericDriver ()
 Generic destructor to prevent warnings when objects are dynamically allocated.
 
virtual bool init ()
 
virtual bool available ()=0
 
virtual bool recv (uint8_t *buf, uint8_t *len)=0
 
virtual bool send (const uint8_t *data, uint8_t len)=0
 
virtual uint8_t maxMessageLength ()=0
 
virtual void waitAvailable (uint16_t polldelay=0)
 
virtual bool waitPacketSent ()
 
virtual bool waitPacketSent (uint16_t timeout)
 
virtual bool waitAvailableTimeout (uint16_t timeout, uint16_t polldelay=0)
 
virtual bool waitCAD ()
 
void setCADTimeout (unsigned long cad_timeout)
 
virtual bool isChannelActive ()
 
virtual void setThisAddress (uint8_t thisAddress)
 
virtual void setHeaderTo (uint8_t to)
 
virtual void setHeaderFrom (uint8_t from)
 
virtual void setHeaderId (uint8_t id)
 
virtual void setHeaderFlags (uint8_t set, uint8_t clear=RH_FLAGS_APPLICATION_SPECIFIC)
 
virtual void setPromiscuous (bool promiscuous)
 
virtual uint8_t headerTo ()
 
virtual uint8_t headerFrom ()
 
virtual uint8_t headerId ()
 
virtual uint8_t headerFlags ()
 
virtual int16_t lastRssi ()
 
virtual RHMode mode ()
 
virtual void setMode (RHMode mode)
 Sets the operating mode of the transport. More...
 
virtual bool sleep ()
 
virtual uint16_t rxBad ()
 
virtual uint16_t rxGood ()
 
virtual uint16_t txGood ()
 

Static Public Member Functions

static void printBuffer (const char *prompt, const uint8_t *buf, uint8_t len)
 

Protected Attributes

volatile RHMode _mode
 The current transport operating mode.
 
uint8_t _thisAddress
 This node id.
 
bool _promiscuous
 Whether the transport is in promiscuous mode.
 
volatile uint8_t _rxHeaderTo
 TO header in the last received mesasge.
 
volatile uint8_t _rxHeaderFrom
 FROM header in the last received mesasge.
 
volatile uint8_t _rxHeaderId
 ID header in the last received mesasge.
 
volatile uint8_t _rxHeaderFlags
 FLAGS header in the last received mesasge.
 
uint8_t _txHeaderTo
 TO header to send in all messages.
 
uint8_t _txHeaderFrom
 FROM header to send in all messages.
 
uint8_t _txHeaderId
 ID header to send in all messages.
 
uint8_t _txHeaderFlags
 FLAGS header to send in all messages.
 
volatile int16_t _lastRssi
 The value of the last received RSSI value, in some transport specific units.
 
volatile uint16_t _rxBad
 Count of the number of bad messages (eg bad checksum etc) received.
 
volatile uint16_t _rxGood
 Count of the number of successfully transmitted messaged.
 
volatile uint16_t _txGood
 Count of the number of bad messages (correct checksum etc) received.
 
volatile bool _cad
 Channel activity detected.
 
unsigned int _cad_timeout
 Channel activity timeout in ms.
 

Detailed Description

Abstract base class for a RadioHead driver.

This class defines the functions that must be provided by any RadioHead driver. Different types of driver will implement all the abstract functions, and will perhaps override other functions in this subclass, or perhaps add new functions specifically required by that driver. Do not directly instantiate this class: it is only to be subclassed by driver classes.

Subclasses are expected to implement a half-duplex, unreliable, error checked, unaddressed packet transport. They are expected to carry a message payload with an appropriate maximum length for the transport hardware and to also carry unaltered 4 message headers: TO, FROM, ID, FLAGS

Headers

Each message sent and received by a RadioHead driver includes 4 headers: -TO The node address that the message is being sent to (broadcast RH_BROADCAST_ADDRESS (255) is permitted) -FROM The node address of the sending node -ID A message ID, distinct (over short time scales) for each message sent by a particilar node -FLAGS A bitmask of flags. The most significant 4 bits are reserved for use by RadioHead. The least significant 4 bits are reserved for applications.

Member Enumeration Documentation

◆ RHMode

Defines different operating modes for the transport hardware.

These are the different values that can be adopted by the _mode variable and returned by the mode() member function,

Enumerator
RHModeInitialising 

Transport is initialising. Initial default value until init() is called..

RHModeSleep 

Transport hardware is in low power sleep mode (if supported)

RHModeIdle 

Transport is idle.

RHModeTx 

Transport is in the process of transmitting a message.

RHModeRx 

Transport is in the process of receiving a message.

RHModeCad 

Transport is in the process of detecting channel activity (if supported)

Member Function Documentation

◆ available()

virtual bool RHGenericDriver::available ( )
pure virtual

Tests whether a new message is available from the Driver. On most drivers, if there is an uncollected received message, and there is no message currently bing transmitted, this will also put the Driver into RHModeRx mode until a message is actually received by the transport, when it will be returned to RHModeIdle. This can be called multiple times in a timeout loop.

Returns
true if a new, complete, error-free uncollected message is available to be retreived by recv().

Implemented in RH_ASK, RH_CC110, RH_E32, RH_LoRaFileOps, RH_MRF89, RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, RH_Serial, RH_SX126x, RH_TCP, and RHEncryptedDriver.

Referenced by RHDatagram::available(), RHEncryptedDriver::available(), waitAvailable(), and waitAvailableTimeout().

◆ headerFlags()

uint8_t RHGenericDriver::headerFlags ( )
virtual

Returns the FLAGS header of the last received message

Returns
The FLAGS header

Reimplemented in RHEncryptedDriver.

References _rxHeaderFlags.

Referenced by RHDatagram::headerFlags(), and RHEncryptedDriver::headerFlags().

◆ headerFrom()

uint8_t RHGenericDriver::headerFrom ( )
virtual

Returns the FROM header of the last received message

Returns
The FROM header

Reimplemented in RHEncryptedDriver.

References _rxHeaderFrom.

Referenced by RHDatagram::headerFrom(), and RHEncryptedDriver::headerFrom().

◆ headerId()

uint8_t RHGenericDriver::headerId ( )
virtual

Returns the ID header of the last received message

Returns
The ID header

Reimplemented in RHEncryptedDriver.

References _rxHeaderId.

Referenced by RHDatagram::headerId(), and RHEncryptedDriver::headerId().

◆ headerTo()

uint8_t RHGenericDriver::headerTo ( )
virtual

Returns the TO header of the last received message

Returns
The TO header

Reimplemented in RHEncryptedDriver.

References _rxHeaderTo.

Referenced by RHDatagram::headerTo(), and RHEncryptedDriver::headerTo().

◆ init()

bool RHGenericDriver::init ( )
virtual

Initialise the Driver transport hardware and software. Make sure the Driver is properly configured before calling init().

Returns
true if initialisation succeeded.

Reimplemented in RH_ABZ, RH_ASK, RH_CC110, RH_E32, RH_LoRaFileOps, RH_MRF89, RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, RH_Serial, RH_STM32WLx, RH_SX126x, RH_TCP, RHEncryptedDriver, RHNRFSPIDriver, and RHSPIDriver.

Referenced by RH_ASK::init(), RHDatagram::init(), and RHEncryptedDriver::init().

◆ isChannelActive()

bool RHGenericDriver::isChannelActive ( )
virtual

Determine if the currently selected radio channel is active. This is expected to be subclassed by specific radios to implement their Channel Activity Detection if supported. If the radio does not support CAD, returns true immediately. If a RadioHead radio supports isChannelActive() it will be documented in the radio specific documentation. This is called automatically by waitCAD().

Returns
true if the radio-specific CAD (as returned by override of isChannelActive()) shows the current radio channel as active, else false. If there is no radio-specific CAD, returns false.

Reimplemented in RH_RF95, RH_SX126x, and RHEncryptedDriver.

Referenced by RHEncryptedDriver::isChannelActive(), and waitCAD().

◆ lastRssi()

int16_t RHGenericDriver::lastRssi ( )
virtual

Returns the most recent RSSI (Receiver Signal Strength Indicator). Usually it is the RSSI of the last received message, which is measured when the preamble is received. If you called readRssi() more recently, it will return that more recent value.

Returns
The most recent RSSI measurement in dBm.

Reimplemented in RHEncryptedDriver.

References _lastRssi.

Referenced by RHEncryptedDriver::lastRssi().

◆ maxMessageLength()

virtual uint8_t RHGenericDriver::maxMessageLength ( )
pure virtual

Returns the maximum message length available in this Driver.

Returns
The maximum legal message length

Implemented in RH_ASK, RH_CC110, RH_E32, RH_LoRaFileOps, RH_MRF89, RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, RH_Serial, RH_SX126x, RH_TCP, and RHEncryptedDriver.

Referenced by RHRouter::sendtoFromSourceWait().

◆ mode()

RHGenericDriver::RHMode RHGenericDriver::mode ( )
virtual

◆ printBuffer()

void RHGenericDriver::printBuffer ( const char *  prompt,
const uint8_t *  buf,
uint8_t  len 
)
static

Prints a data buffer in HEX. For diagnostic use

Parameters
[in]promptstring to preface the print
[in]bufLocation of the buffer to print
[in]lenLength of the buffer in octets.
Examples
lorafileops_server.cpp.

◆ recv()

virtual bool RHGenericDriver::recv ( uint8_t *  buf,
uint8_t *  len 
)
pure virtual

Turns the receiver on if it not already on. If there is a valid message available, copy it to buf and return true else return false. If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). You should be sure to call this function frequently enough to not miss any messages It is recommended that you call it in your main loop.

Parameters
[in]bufLocation to copy the received message
[in,out]lenPointer to available space in buf. Set to the actual number of octets copied.
Returns
true if a valid message was copied to buf

Implemented in RH_ASK, RH_CC110, RH_E32, RH_LoRaFileOps, RH_MRF89, RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, RH_Serial, RH_SX126x, RH_TCP, and RHEncryptedDriver.

Referenced by RHDatagram::recvfrom().

◆ rxBad()

uint16_t RHGenericDriver::rxBad ( )
virtual

Returns the count of the number of bad received packets (ie packets with bad lengths, checksum etc) which were rejected and not delivered to the application. Caution: not all drivers can correctly report this count. Some underlying hardware only report good packets.

Returns
The number of bad packets received.

Reimplemented in RHEncryptedDriver.

References _rxBad.

Referenced by RHEncryptedDriver::rxBad().

◆ rxGood()

uint16_t RHGenericDriver::rxGood ( )
virtual

Returns the count of the number of good received packets

Returns
The number of good packets received.

Reimplemented in RHEncryptedDriver.

References _rxGood.

Referenced by RHEncryptedDriver::rxGood().

◆ send()

virtual bool RHGenericDriver::send ( const uint8_t *  data,
uint8_t  len 
)
pure virtual

Waits until any previous transmit packet is finished being transmitted with waitPacketSent(). Then optionally waits for Channel Activity Detection (CAD) to show the channnel is clear (if the radio supports CAD) by calling waitCAD(). Then loads a message into the transmitter and starts the transmitter. Note that a message length of 0 is NOT permitted. If the message is too long for the underlying radio technology, send() will return false and will not send the message.

Parameters
[in]dataArray of data to be sent
[in]lenNumber of bytes of data to send (> 0) specify the maximum time in ms to wait. If 0 (the default) do not wait for CAD before transmitting.
Returns
true if the message length was valid and it was correctly queued for transmit. Return false if CAD was requested and the CAD timeout timed out before clear channel was detected.

Implemented in RH_ASK, RH_CC110, RH_E32, RH_LoRaFileOps, RH_MRF89, RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, RH_Serial, RH_SX126x, RH_TCP, and RHEncryptedDriver.

Referenced by RHDatagram::sendto().

◆ setCADTimeout()

void RHGenericDriver::setCADTimeout ( unsigned long  cad_timeout)

Sets the Channel Activity Detection timeout in milliseconds to be used by waitCAD(). The default is 0, which means do not wait for CAD detection. CAD detection depends on support for isChannelActive() by your particular radio.

References _cad_timeout.

Referenced by RHEncryptedDriver::setCADTimeout().

◆ setHeaderFlags()

void RHGenericDriver::setHeaderFlags ( uint8_t  set,
uint8_t  clear = RH_FLAGS_APPLICATION_SPECIFIC 
)
virtual

Sets and clears bits in the FLAGS header to be sent in all subsequent messages First it clears he FLAGS according to the clear argument, then sets the flags according to the set argument. The default for clear always clears the application specific flags.

Parameters
[in]setbitmask of bits to be set. Flags are cleared with the clear mask before being set.
[in]clearbitmask of flags to clear. Defaults to RH_FLAGS_APPLICATION_SPECIFIC which clears the application specific flags, resulting in new application specific flags identical to the set.

Reimplemented in RHEncryptedDriver.

References _txHeaderFlags.

Referenced by RHEncryptedDriver::setHeaderFlags(), and RHDatagram::setHeaderFlags().

◆ setHeaderFrom()

void RHGenericDriver::setHeaderFrom ( uint8_t  from)
virtual

Sets the FROM header to be sent in all subsequent messages

Parameters
[in]fromThe new FROM header value

Reimplemented in RHEncryptedDriver.

References _txHeaderFrom.

Referenced by RHDatagram::setHeaderFrom(), and RHEncryptedDriver::setHeaderFrom().

◆ setHeaderId()

void RHGenericDriver::setHeaderId ( uint8_t  id)
virtual

Sets the ID header to be sent in all subsequent messages

Parameters
[in]idThe new ID header value

Reimplemented in RHEncryptedDriver.

References _txHeaderId.

Referenced by RHDatagram::setHeaderId(), and RHEncryptedDriver::setHeaderId().

◆ setHeaderTo()

void RHGenericDriver::setHeaderTo ( uint8_t  to)
virtual

Sets the TO header to be sent in all subsequent messages

Parameters
[in]toThe new TO header value

Reimplemented in RHEncryptedDriver.

References _txHeaderTo.

Referenced by RHDatagram::setHeaderTo(), and RHEncryptedDriver::setHeaderTo().

◆ setMode()

void RHGenericDriver::setMode ( RHMode  mode)
virtual

Sets the operating mode of the transport.

Reimplemented in RHEncryptedDriver.

References _mode, and mode().

Referenced by RHEncryptedDriver::setMode().

◆ setPromiscuous()

void RHGenericDriver::setPromiscuous ( bool  promiscuous)
virtual

Tells the receiver to accept messages with any TO address, not just messages addressed to thisAddress or the broadcast address

Parameters
[in]promiscuoustrue if you wish to receive messages with any TO address

Reimplemented in RH_RF22, and RHEncryptedDriver.

References _promiscuous.

Referenced by RH_RF22::setPromiscuous(), and RHEncryptedDriver::setPromiscuous().

◆ setThisAddress()

void RHGenericDriver::setThisAddress ( uint8_t  thisAddress)
virtual

Sets the address of this node. Defaults to 0xFF. Subclasses or the user may want to change this. This will be used to test the adddress in incoming messages. In non-promiscuous mode, only messages with a TO header the same as thisAddress or the broadcast addess (0xFF) will be accepted. In promiscuous mode, all messages will be accepted regardless of the TO header. In a conventional multinode system, all nodes will have a unique address (which you could store in EEPROM). You would normally set the header FROM address to be the same as thisAddress (though you dont have to, allowing the possibilty of address spoofing).

Parameters
[in]thisAddressThe address of this node.

Reimplemented in RH_TCP, RH_RF22, and RHEncryptedDriver.

References _thisAddress.

Referenced by RH_RF22::setThisAddress(), RHDatagram::setThisAddress(), and RHEncryptedDriver::setThisAddress().

◆ sleep()

bool RHGenericDriver::sleep ( )
virtual

Sets the transport hardware into low-power sleep mode (if supported). May be overridden by specific drivers to initialte sleep mode. If successful, the transport will stay in sleep mode until woken by changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc)

Returns
true if sleep mode is supported by transport hardware and the RadioHead driver, and if sleep mode was successfully entered. If sleep mode is not suported, return false.

Reimplemented in RH_CC110, RH_MRF89, RH_NRF24, RH_RF22, RH_RF24, RH_RF69, RH_RF95, RH_SX126x, and RHEncryptedDriver.

Referenced by RHEncryptedDriver::sleep().

◆ txGood()

uint16_t RHGenericDriver::txGood ( )
virtual

Returns the count of the number of packets successfully transmitted (though not necessarily received by the destination)

Returns
The number of packets successfully transmitted

Reimplemented in RHEncryptedDriver.

References _txGood.

Referenced by RHEncryptedDriver::txGood().

◆ waitAvailable()

void RHGenericDriver::waitAvailable ( uint16_t  polldelay = 0)
virtual

Starts the receiver and blocks until a valid received message is available. Default implementation calls available() repeatedly until it returns true;

Parameters
[in]polldelayTime between polling available() in milliseconds. This can be useful in multitaking environment like Linux to prevent waitAvailableTimeout using all the CPU while polling for receiver activity

Reimplemented in RH_Serial, and RH_TCP.

References available().

Referenced by RHDatagram::waitAvailable().

◆ waitAvailableTimeout()

bool RHGenericDriver::waitAvailableTimeout ( uint16_t  timeout,
uint16_t  polldelay = 0 
)
virtual

Starts the receiver and blocks until a received message is available or a timeout. Default implementation calls available() repeatedly until it returns true;

Parameters
[in]timeoutMaximum time to wait in milliseconds.
[in]polldelayTime between polling available() in milliseconds. This can be useful in multitaking environment like Linux to prevent waitAvailableTimeout using all the CPU while polling for receiver activity
Returns
true if a message is available

Reimplemented in RH_Serial, and RH_TCP.

References available().

Referenced by RHEncryptedDriver::waitAvailableTimeout(), and RHDatagram::waitAvailableTimeout().

◆ waitCAD()

bool RHGenericDriver::waitCAD ( )
virtual

Channel Activity Detection (CAD). Blocks until channel activity is finished or CAD timeout occurs. Uses the radio's CAD function (if supported) to detect channel activity. Implements random delays of 100 to 1000ms while activity is detected and until timeout. Caution: the random() function is not seeded. If you want non-deterministic behaviour, consider using something like randomSeed(analogRead(A0)); in your sketch. Permits the implementation of listen-before-talk mechanism (Collision Avoidance). Calls the isChannelActive() member function for the radio (if supported) to determine if the channel is active. If the radio does not support isChannelActive(), always returns true immediately

Returns
true if the radio-specific CAD (as returned by isChannelActive()) shows the channel is clear within the timeout period (or the timeout period is 0), else returns false.

Reimplemented in RHEncryptedDriver.

References _cad_timeout, and isChannelActive().

Referenced by RH_ASK::send(), RH_CC110::send(), RH_MRF89::send(), RH_NRF24::send(), RH_NRF905::send(), RH_RF22::send(), RH_RF24::send(), RH_RF69::send(), RH_RF95::send(), RH_SX126x::send(), and RHEncryptedDriver::waitCAD().

◆ waitPacketSent() [1/2]

bool RHGenericDriver::waitPacketSent ( )
virtual

◆ waitPacketSent() [2/2]

bool RHGenericDriver::waitPacketSent ( uint16_t  timeout)
virtual

Blocks until the transmitter is no longer transmitting. or until the timeout occuers, whichever happens first

Parameters
[in]timeoutMaximum time to wait in milliseconds.
Returns
true if the radio completed transmission within the timeout period. False if it timed out.

Reimplemented in RHEncryptedDriver.

References _mode, and RHModeTx.


The documentation for this class was generated from the following files: