RadioLib
Universal wireless communication library for Arduino
LoRaWANNode Class Reference

LoRaWAN-compatible node (class A device). More...

#include <LoRaWAN.h>

Public Member Functions

 LoRaWANNode (PhysicalLayer *phy, const LoRaWANBand_t *band, uint8_t subBand=0)
 Default constructor. More...
 
void wipe ()
 Wipe internal persistent parameters. This will reset all counters and saved variables, so the device will have to rejoin the network.
 
uint8_t * getBufferNonces ()
 Returns the pointer to the internal buffer that holds the LW base parameters. More...
 
int16_t setBufferNonces (uint8_t *persistentBuffer)
 Fill the internal buffer that holds the LW base parameters with a supplied buffer. More...
 
uint8_t * getBufferSession ()
 Returns the pointer to the internal buffer that holds the LW session parameters. More...
 
int16_t setBufferSession (uint8_t *persistentBuffer)
 Fill the internal buffer that holds the LW session parameters with a supplied buffer. More...
 
int16_t restore (uint16_t checkSum, uint16_t lwMode, uint8_t lwClass, uint8_t freqPlan)
 Restore session by loading information from persistent storage. More...
 
int16_t beginOTAA (uint64_t joinEUI, uint64_t devEUI, uint8_t *nwkKey, uint8_t *appKey, bool force=false, uint8_t joinDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED)
 Join network by performing over-the-air activation. By this procedure, the device will perform an exchange with the network server and set all necessary configuration. More...
 
int16_t beginABP (uint32_t addr, uint8_t *nwkSKey, uint8_t *appSKey, uint8_t *fNwkSIntKey=NULL, uint8_t *sNwkSIntKey=NULL, bool force=false, uint8_t initialDr=RADIOLIB_LORAWAN_DATA_RATE_UNUSED)
 Join network by performing activation by personalization. In this procedure, all necessary configuration must be provided by the user. More...
 
bool isJoined ()
 Whether there is an ongoing session active.
 
int16_t saveSession ()
 Save the current state of the session to the session buffer. More...
 
bool sendMacCommandReq (uint8_t cid)
 Add a MAC command to the uplink queue. Only LinkCheck and DeviceTime are available to the user. Other commands are ignored; duplicate MAC commands are discarded. More...
 
int16_t uplink (const char *str, uint8_t port, bool isConfirmed=false, LoRaWANEvent_t *event=NULL)
 Send a message to the server. More...
 
int16_t uplink (uint8_t *data, size_t len, uint8_t port, bool isConfirmed=false, LoRaWANEvent_t *event=NULL)
 Send a message to the server. More...
 
int16_t downlink (uint8_t *data, size_t *len, LoRaWANEvent_t *event=NULL)
 Wait for downlink from the server in either RX1 or RX2 window. More...
 
int16_t downlink (LoRaWANEvent_t *event=NULL)
 Wait for downlink, simplified to allow for simpler sendReceive. More...
 
int16_t sendReceive (const char *strUp, uint8_t port, uint8_t *dataDown, size_t *lenDown, bool isConfirmed=false, LoRaWANEvent_t *eventUp=NULL, LoRaWANEvent_t *eventDown=NULL)
 Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window. More...
 
int16_t sendReceive (uint8_t *dataUp, size_t lenUp, uint8_t port, uint8_t *dataDown, size_t *lenDown, bool isConfirmed=false, LoRaWANEvent_t *eventUp=NULL, LoRaWANEvent_t *eventDown=NULL)
 Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window. More...
 
int16_t sendReceive (uint8_t *dataUp, size_t lenUp, uint8_t port=1, bool isConfirmed=false, LoRaWANEvent_t *eventUp=NULL, LoRaWANEvent_t *eventDown=NULL)
 Send a message to the server and wait for a downlink but don't bother the user with downlink contents. More...
 
void setDeviceStatus (uint8_t battLevel)
 Set device status. More...
 
uint32_t getFcntUp ()
 Returns the last uplink's frame counter; also 0 if no uplink occured yet.
 
uint32_t getNFcntDown ()
 Returns the last network downlink's frame counter; also 0 if no network downlink occured yet.
 
uint32_t getAFcntDown ()
 Returns the last application downlink's frame counter; also 0 if no application downlink occured yet.
 
void resetFcntDown ()
 Reset the downlink frame counters (application and network) This is unsafe and can possibly allow replay attacks using downlinks. It mainly exists as part of the TS009 Specification Verification protocol.
 
int16_t setDatarate (uint8_t drUp)
 Set uplink datarate. This should not be used when ADR is enabled. More...
 
void setADR (bool enable=true)
 Toggle ADR to on or off. More...
 
void setDutyCycle (bool enable=true, uint32_t msPerHour=0)
 Toggle adherence to dutyCycle limits to on or off. More...
 
uint32_t dutyCycleInterval (uint32_t msPerHour, uint32_t airtime)
 Calculate the minimum interval to adhere to a certain dutyCycle. This interval is based on the ToA of one uplink and does not actually keep track of total airtime. More...
 
uint32_t timeUntilUplink ()
 Returns time in milliseconds until next uplink is available under dutyCycle limits.
 
void setDwellTime (bool enable, uint32_t msPerUplink=0)
 Toggle adherence to dwellTime limits to on or off. More...
 
uint8_t maxPayloadDwellTime ()
 Returns the maximum payload given the currently present dwell time limits. WARNING: the addition of MAC commands may cause uplink errors; if you want to be sure that your payload fits within dwell time limits, subtract 16 from the result!
 
int16_t setTxPower (int8_t txPower)
 Configure TX power of the radio module. More...
 
int16_t getMacLinkCheckAns (uint8_t *margin, uint8_t *gwCnt)
 Returns the quality of connectivity after requesting a LinkCheck MAC command. Returns 'true' if a network response was successfully parsed. Returns 'false' if there was no network response / parsing failed. More...
 
int16_t getMacDeviceTimeAns (uint32_t *gpsEpoch, uint8_t *fraction, bool returnUnix=true)
 Returns the network time after requesting a DeviceTime MAC command. Returns 'true' if a network response was successfully parsed. Returns 'false' if there was no network response / parsing failed. More...
 
uint64_t getDevAddr ()
 Returns the DevAddr of the device, regardless of OTAA or ABP mode. More...
 

Public Attributes

uint8_t rx1DrOffset = 0
 Offset between TX and RX1 (such that RX1 has equal or lower DR)
 
LoRaWANChannel_t rx2
 RX2 channel properties - may be changed by MAC command.
 

Detailed Description

LoRaWAN-compatible node (class A device).

Constructor & Destructor Documentation

◆ LoRaWANNode()

LoRaWANNode::LoRaWANNode ( PhysicalLayer phy,
const LoRaWANBand_t band,
uint8_t  subBand = 0 
)

Default constructor.

Parameters
phyPointer to the PhysicalLayer radio module.
bandPointer to the LoRaWAN band to use.
subBandThe sub-band to be used (starting from 1!)

Member Function Documentation

◆ beginABP()

int16_t LoRaWANNode::beginABP ( uint32_t  addr,
uint8_t *  nwkSKey,
uint8_t *  appSKey,
uint8_t *  fNwkSIntKey = NULL,
uint8_t *  sNwkSIntKey = NULL,
bool  force = false,
uint8_t  initialDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED 
)

Join network by performing activation by personalization. In this procedure, all necessary configuration must be provided by the user.

Parameters
addrDevice address.
nwkSKeyPointer to the network session AES-128 key (LoRaWAN 1.0) or MAC command network session key (LoRaWAN 1.1).
appSKeyPointer to the application session AES-128 key.
fNwkSIntKeyPointer to the Forwarding network session (LoRaWAN 1.1), unused for LoRaWAN 1.0.
sNwkSIntKeyPointer to the Serving network session (LoRaWAN 1.1), unused for LoRaWAN 1.0.
forceSet to true to force a new session, even if one exists.
initialDrThe datarate at which to send the first uplink and any subsequent uplinks (unless ADR is enabled)
Returns
Status Codes

◆ beginOTAA()

int16_t LoRaWANNode::beginOTAA ( uint64_t  joinEUI,
uint64_t  devEUI,
uint8_t *  nwkKey,
uint8_t *  appKey,
bool  force = false,
uint8_t  joinDr = RADIOLIB_LORAWAN_DATA_RATE_UNUSED 
)

Join network by performing over-the-air activation. By this procedure, the device will perform an exchange with the network server and set all necessary configuration.

Parameters
joinEUI8-byte application identifier.
devEUI8-byte device identifier.
nwkKeyPointer to the network AES-128 key.
appKeyPointer to the application AES-128 key.
forceSet to true to force joining even if previously joined.
joinDrThe datarate at which to send the join-request and any subsequent uplinks (unless ADR is enabled)
Returns
Status Codes

◆ downlink() [1/2]

int16_t LoRaWANNode::downlink ( LoRaWANEvent_t event = NULL)

Wait for downlink, simplified to allow for simpler sendReceive.

Parameters
eventPointer to a structure to store extra information about the event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

◆ downlink() [2/2]

int16_t LoRaWANNode::downlink ( uint8_t *  data,
size_t *  len,
LoRaWANEvent_t event = NULL 
)

Wait for downlink from the server in either RX1 or RX2 window.

Parameters
dataBuffer to save received data into.
lenPointer to variable that will be used to save the number of received bytes.
eventPointer to a structure to store extra information about the event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

◆ dutyCycleInterval()

uint32_t LoRaWANNode::dutyCycleInterval ( uint32_t  msPerHour,
uint32_t  airtime 
)

Calculate the minimum interval to adhere to a certain dutyCycle. This interval is based on the ToA of one uplink and does not actually keep track of total airtime.

Parameters
msPerHourThe maximum allowed duty cycle (in milliseconds per hour).
airtimeThe airtime of the uplink.
Returns
Required interval (delay) in milliseconds between consecutive uplinks.

◆ getBufferNonces()

uint8_t * LoRaWANNode::getBufferNonces ( )

Returns the pointer to the internal buffer that holds the LW base parameters.

Returns
Pointer to uint8_t array of size RADIOLIB_LORAWAN_NONCES_BUF_SIZE

◆ getBufferSession()

uint8_t * LoRaWANNode::getBufferSession ( )

Returns the pointer to the internal buffer that holds the LW session parameters.

Returns
Pointer to uint8_t array of size RADIOLIB_LORAWAN_SESSION_BUF_SIZE

◆ getDevAddr()

uint64_t LoRaWANNode::getDevAddr ( )

Returns the DevAddr of the device, regardless of OTAA or ABP mode.

Returns
8-byte DevAddr

◆ getMacDeviceTimeAns()

int16_t LoRaWANNode::getMacDeviceTimeAns ( uint32_t *  gpsEpoch,
uint8_t *  fraction,
bool  returnUnix = true 
)

Returns the network time after requesting a DeviceTime MAC command. Returns 'true' if a network response was successfully parsed. Returns 'false' if there was no network response / parsing failed.

Parameters
gpsEpochNumber of seconds since GPS epoch (Jan. 6th 1980)
fractionFractional-second, in 1/256-second steps
returnUnixIf true, returns Unix timestamp instead of GPS (default true)
Returns
Status Codes

◆ getMacLinkCheckAns()

int16_t LoRaWANNode::getMacLinkCheckAns ( uint8_t *  margin,
uint8_t *  gwCnt 
)

Returns the quality of connectivity after requesting a LinkCheck MAC command. Returns 'true' if a network response was successfully parsed. Returns 'false' if there was no network response / parsing failed.

Parameters
marginLink margin in dB of LinkCheckReq demodulation at gateway side.
gwCntNumber of gateways that received the LinkCheckReq.
Returns
Status Codes

◆ restore()

int16_t LoRaWANNode::restore ( uint16_t  checkSum,
uint16_t  lwMode,
uint8_t  lwClass,
uint8_t  freqPlan 
)

Restore session by loading information from persistent storage.

Returns
Status Codes

◆ saveSession()

int16_t LoRaWANNode::saveSession ( )

Save the current state of the session to the session buffer.

Returns
Status Codes

◆ sendMacCommandReq()

bool LoRaWANNode::sendMacCommandReq ( uint8_t  cid)

Add a MAC command to the uplink queue. Only LinkCheck and DeviceTime are available to the user. Other commands are ignored; duplicate MAC commands are discarded.

Parameters
cidID of the MAC command
Returns
Whether or not the MAC command was added to the queue.

◆ sendReceive() [1/3]

int16_t LoRaWANNode::sendReceive ( const char *  strUp,
uint8_t  port,
uint8_t *  dataDown,
size_t *  lenDown,
bool  isConfirmed = false,
LoRaWANEvent_t eventUp = NULL,
LoRaWANEvent_t eventDown = NULL 
)

Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window.

Parameters
strUpC-string that will be transmitted.
portPort number to send the message to.
dataDownBuffer to save received data into.
lenDownPointer to variable that will be used to save the number of received bytes.
isConfirmedWhether to send a confirmed uplink or not.
eventUpPointer to a structure to store extra information about the uplink event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
eventDownPointer to a structure to store extra information about the downlink event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

◆ sendReceive() [2/3]

int16_t LoRaWANNode::sendReceive ( uint8_t *  dataUp,
size_t  lenUp,
uint8_t  port,
uint8_t *  dataDown,
size_t *  lenDown,
bool  isConfirmed = false,
LoRaWANEvent_t eventUp = NULL,
LoRaWANEvent_t eventDown = NULL 
)

Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window.

Parameters
dataUpData to send.
lenUpLength of the data.
portPort number to send the message to.
dataDownBuffer to save received data into.
lenDownPointer to variable that will be used to save the number of received bytes.
isConfirmedWhether to send a confirmed uplink or not.
eventUpPointer to a structure to store extra information about the uplink event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
eventDownPointer to a structure to store extra information about the downlink event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

◆ sendReceive() [3/3]

int16_t LoRaWANNode::sendReceive ( uint8_t *  dataUp,
size_t  lenUp,
uint8_t  port = 1,
bool  isConfirmed = false,
LoRaWANEvent_t eventUp = NULL,
LoRaWANEvent_t eventDown = NULL 
)

Send a message to the server and wait for a downlink but don't bother the user with downlink contents.

Parameters
dataUpData to send.
lenUpLength of the data.
portPort number to send the message to.
isConfirmedWhether to send a confirmed uplink or not.
eventUpPointer to a structure to store extra information about the uplink event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
eventDownPointer to a structure to store extra information about the downlink event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

◆ setADR()

void LoRaWANNode::setADR ( bool  enable = true)

Toggle ADR to on or off.

Parameters
enableWhether to disable ADR or not.

◆ setBufferNonces()

int16_t LoRaWANNode::setBufferNonces ( uint8_t *  persistentBuffer)

Fill the internal buffer that holds the LW base parameters with a supplied buffer.

Parameters
persistentBufferBuffer that should match the internal format (previously extracted using getBufferNonces)
Returns
Status Codes

◆ setBufferSession()

int16_t LoRaWANNode::setBufferSession ( uint8_t *  persistentBuffer)

Fill the internal buffer that holds the LW session parameters with a supplied buffer.

Parameters
persistentBufferBuffer that should match the internal format (previously extracted using getBufferSession)
Returns
Status Codes

◆ setDatarate()

int16_t LoRaWANNode::setDatarate ( uint8_t  drUp)

Set uplink datarate. This should not be used when ADR is enabled.

Parameters
drUpDatarate to use for uplinks.
Returns
Status Codes

◆ setDeviceStatus()

void LoRaWANNode::setDeviceStatus ( uint8_t  battLevel)

Set device status.

Parameters
battLevelBattery level to set. 0 for external power source, 1 for lowest battery, 254 for highest battery, 255 for unable to measure.

◆ setDutyCycle()

void LoRaWANNode::setDutyCycle ( bool  enable = true,
uint32_t  msPerHour = 0 
)

Toggle adherence to dutyCycle limits to on or off.

Parameters
enableWhether to adhere to dutyCycle limits or not (default true).
msPerHourThe maximum allowed Time-on-Air per hour in milliseconds (default 0 = maximum allowed for configured band).

◆ setDwellTime()

void LoRaWANNode::setDwellTime ( bool  enable,
uint32_t  msPerUplink = 0 
)

Toggle adherence to dwellTime limits to on or off.

Parameters
enableWhether to adhere to dwellTime limits or not (default true).
msPerUplinkThe maximum allowed Time-on-Air per uplink in milliseconds (default 0 = maximum allowed for configured band).

◆ setTxPower()

int16_t LoRaWANNode::setTxPower ( int8_t  txPower)

Configure TX power of the radio module.

Parameters
txPowerOutput power during TX mode to be set in dBm.
Returns
Status Codes

◆ uplink() [1/2]

int16_t LoRaWANNode::uplink ( const char *  str,
uint8_t  port,
bool  isConfirmed = false,
LoRaWANEvent_t event = NULL 
)

Send a message to the server.

Parameters
strC-string that will be transmitted.
portPort number to send the message to.
isConfirmedWhether to send a confirmed uplink or not.
eventPointer to a structure to store extra information about the event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

◆ uplink() [2/2]

int16_t LoRaWANNode::uplink ( uint8_t *  data,
size_t  len,
uint8_t  port,
bool  isConfirmed = false,
LoRaWANEvent_t event = NULL 
)

Send a message to the server.

Parameters
dataData to send.
lenLength of the data.
portPort number to send the message to.
isConfirmedWhether to send a confirmed uplink or not.
eventPointer to a structure to store extra information about the event (port, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
Returns
Status Codes

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