Bleeding edge development version of the xDot library for mbed 5. This version of the library is not guaranteed to be stable or well tested and should not be used in production or deployment scenarios.

Dependents:   Dot-Examples Dot-AT-Firmware Dot-Examples TEST_FF1705 ... more

The Dot library provides a LoRaWan certified stack for LoRa communication using MultiTech mDot and xDot devices. The stack is compatible with mbed 5.

Dot Library Version 3 Updates

Dot Library versions 3.x.x require a channel plan to be injected into the stack. Channel plans are included with the 3.x.x Dot Library releases. The following code snippet demonstrates how to create a channel plan and inject it into the stack.

#include "mDot.h"
#include "channel_plans.h"

int main() {
    ChannelPlan* plan = new lora::ChannelPlan_US915();
    assert(plan);
    mDot* dot = mDot::getInstance(plan);
    assert(dot);

    // ...
}

Dot devices must not be deployed with software using a different channel plan than the Dot's default plan! This functionality is for development and testing only!

Multicast Sessions

Multicast sessions and packet rx events in library. When in Class C mode Multicast downlinks can be received. Recieved packets should be filtered on address, counter value will be maintained in the session or can be set explicitly depending on Application support to share Multicast Address, Keys and Counters.

mDot.h

        /**
         * Add a multicast session address and keys
         * Downlink counter is set to 0
         * Up to 3 MULTICAST_SESSIONS can be set
         */
        int32_t setMulticastSession(uint8_t index, uint32_t addr, const uint8_t* nsk, const uint8_t* dsk);
 
        /**
         * Set a multicast session counter
         * Up to 3 MULTICAST_SESSIONS can be set
         */
        int32_t setMulticastDownlinkCounter(uint8_t index, uint32_t count);

mDotEvent.h

The address field was added to PacketRx event.

        virtual void PacketRx(uint8_t port, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot, uint8_t retries, uint32_t address);

The name of the repository can be used to determine which device the stack was compiled for and if it's a development or production-ready build:

A changelog for the Dot library can be found here.

The Dot library version and the version of mbed-os it was compiled against can both be found in the commit message for that revision of the Dot library. Building your application with the same version of mbed-os as what was used to build the Dot library is highly recommended!

The Dot-Examples repository demonstrates how to use the Dot library in a custom application.

The mDot and xDot platform pages have lots of platform specific information and document potential issues, gotchas, etc, and provide instructions for getting started with development. Please take a look at the platform page before starting development as they should answer many questions you will have.

Files at this revision

API Documentation at this revision

Comitter:
Jenkins@KEILDM1.dc.multitech.prv
Date:
Tue Sep 18 08:25:41 2018 -0500
Parent:
126:c17213d3156d
Child:
128:ff9ca3779136
Commit message:
xdot-library revision 3.1.0-28-g84dbee8 and mbed-os revision mbed-os-5.9.6

Changed in this revision

ChannelPlan.h Show annotated file Show diff for this revision Revisions of this file
MacEvents.h Show annotated file Show diff for this revision Revisions of this file
SxRadio.h Show annotated file Show diff for this revision Revisions of this file
libxDot-ARMCC.ar Show annotated file Show diff for this revision Revisions of this file
libxDot-GCC_ARM.a Show annotated file Show diff for this revision Revisions of this file
mDot.h Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_AS923.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_AU915.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_EU868.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_IN865.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_KR920.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_RU864.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_RU864.h Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlan_US915.cpp Show annotated file Show diff for this revision Revisions of this file
plans/ChannelPlans.h Show annotated file Show diff for this revision Revisions of this file
--- a/ChannelPlan.h	Wed Sep 12 15:04:54 2018 -0500
+++ b/ChannelPlan.h	Tue Sep 18 08:25:41 2018 -0500
@@ -61,6 +61,7 @@
                 AS923 = DYNAMIC | 0x02,
                 KR920 = DYNAMIC | 0x03,
                 AS923_JAPAN = DYNAMIC | 0x04,
+                RU864 = DYNAMIC | 0x05,
 
                 NONE = 0xFF,
             };
--- a/MacEvents.h	Wed Sep 12 15:04:54 2018 -0500
+++ b/MacEvents.h	Tue Sep 18 08:25:41 2018 -0500
@@ -36,10 +36,10 @@
 
             virtual void BeaconRx(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) = 0;
             virtual void BeaconLost() = 0;
+            virtual void ServerTime(uint32_t seconds, uint8_t sub_seconds) = 0;
 
             virtual void Pong(int16_t m_rssi, int8_t m_snr, int16_t s_rssi, int8_t s_snr) = 0;
             virtual void NetworkLinkCheck(int16_t m_rssi, int8_t m_snr, int8_t s_snr, uint8_t s_gateways) = 0;
-            virtual void ServerTime(uint32_t seconds, uint8_t sub_seconds) = 0;
 
             virtual void RxTimeout(uint8_t slot) = 0;
             virtual void RxError(uint8_t slot) = 0;
--- a/SxRadio.h	Wed Sep 12 15:04:54 2018 -0500
+++ b/SxRadio.h	Tue Sep 18 08:25:41 2018 -0500
@@ -45,7 +45,7 @@
         RF_CAD,
     }RadioState_t;
 
-    SxRadio(uint32_t WakeupTime) : WakeupTime(WakeupTime), State(RF_IDLE), Modem(MODEM_LORA) { }
+    SxRadio(uint32_t WakeupTime) : WakeupTime(WakeupTime), freq_offset(0), State(RF_IDLE), Modem(MODEM_LORA) { }
     virtual ~SxRadio() {};
 
     /*!
@@ -186,6 +186,8 @@
                               bool fixLen, bool crcOn, bool FreqHopOn,
                               uint8_t HopPeriod, bool iqInverted, uint32_t timeout ) = 0;
 
+    virtual void SetTxPower(int8_t power) = 0;
+
     virtual void SetTxContinuous(bool enable) = 0;
 
     /*!
@@ -278,9 +280,15 @@
     void GrabMutex(void) { mutex.lock(); }
     void ReleaseMutex(void) { mutex.unlock(); }
 
+    int32_t GetFrequencyOffset() { return freq_offset; }
+    void SetFrequencyOffset(int32_t offset) { freq_offset = offset; }
+
     const uint32_t WakeupTime;
 
+
 protected:
+    int32_t freq_offset;
+
     RadioState_t State;
 
     RadioModems_t Modem;
@@ -289,6 +297,7 @@
      * Access protection
      */
     Mutex mutex;
+
 };
 
 #endif // __SXRADIO_H__
Binary file libxDot-ARMCC.ar has changed
Binary file libxDot-GCC_ARM.a has changed
--- a/mDot.h	Wed Sep 12 15:04:54 2018 -0500
+++ b/mDot.h	Tue Sep 18 08:25:41 2018 -0500
@@ -1407,6 +1407,20 @@
          */
         int8_t getLbtThreshold();
 
+        /**
+         * Get Radio Frequency Offset
+         * Used for fine calibration of radio frequencies
+         * @returns frequency offset in MHz
+         */
+        int32_t getFrequencyOffset();
+        /**
+         * Get Radio Frequency Offset
+         * Used for fine calibration of radio frequencies
+         * @param offset frequency offset in MHz
+         */
+        void setFrequencyOffset(int32_t offset);
+
+
 #if defined(TARGET_MTS_MDOT_F411RE)
         ///////////////////////////////////////////////////////////////////
         // Filesystem (Non Volatile Memory) Operation Functions for mDot //
@@ -1647,7 +1661,7 @@
 
         void openRxWindow(uint32_t timeout, uint8_t bandwidth = 0);
         void closeRxWindow();
-        void sendContinuous(bool enable=true);
+        void sendContinuous(bool enable=true, uint32_t timeout=0, uint32_t frequency=0, int8_t txpower=-1);
         int32_t setDeviceId(const std::vector<uint8_t>& id);
         int32_t setProtectedAppEUI(const std::vector<uint8_t>& appEUI);
         int32_t setProtectedAppKey(const std::vector<uint8_t>& appKey);
--- a/plans/ChannelPlan_AS923.cpp	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlan_AS923.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -642,9 +642,11 @@
     }
 
     if (GetSettings()->Network.ADREnabled) {
-        GetSettings()->Session.TxDatarate = datarate;
-        GetSettings()->Session.TxPower = GetSettings()->Session.Max_EIRP - (power * 2);
-        GetSettings()->Session.Redundancy = nbRep;
+        if (status == 0x07) {
+            GetSettings()->Session.TxDatarate = datarate;
+            GetSettings()->Session.TxPower = GetSettings()->Session.Max_EIRP - (power * 2);
+            GetSettings()->Session.Redundancy = nbRep;
+        }
     } else {
         logDebug("ADR is disabled, DR and Power not changed.");
         status &= 0xFB; // TxPower KO
--- a/plans/ChannelPlan_AU915.cpp	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlan_AU915.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -160,8 +160,11 @@
 }
 
 uint8_t ChannelPlan_AU915::HandleJoinAccept(const uint8_t* buffer, uint8_t size) {
-    if (size > 17) {
-        // TODO: Handle future channel mask settings
+
+    if (size > 17 && buffer[28] == 0x01) {
+        for (int i = 13; i < size - 5; i += 2) {
+            SetChannelMask((i-13)/2, buffer[i+1] << 8 | buffer[i]);
+        }
     }
 
     return LORA_OK;
@@ -668,9 +671,11 @@
     }
 
     if (GetSettings()->Network.ADREnabled) {
-        GetSettings()->Session.TxDatarate = datarate;
-        GetSettings()->Session.TxPower = TX_POWERS[power];
-        GetSettings()->Session.Redundancy = nbRep;
+        if (status == 0x07) {
+            GetSettings()->Session.TxDatarate = datarate;
+            GetSettings()->Session.TxPower = TX_POWERS[power];
+            GetSettings()->Session.Redundancy = nbRep;
+        }
     } else {
         logDebug("ADR is disabled, DR and Power not changed.");
         status &= 0xFB; // TxPower KO
--- a/plans/ChannelPlan_EU868.cpp	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlan_EU868.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -649,9 +649,11 @@
     }
 
     if (GetSettings()->Network.ADREnabled) {
-        GetSettings()->Session.TxDatarate = datarate;
-        GetSettings()->Session.TxPower = TX_POWERS[power];
-        GetSettings()->Session.Redundancy = nbRep;
+        if (status == 0x07) {
+            GetSettings()->Session.TxDatarate = datarate;
+            GetSettings()->Session.TxPower = TX_POWERS[power];
+            GetSettings()->Session.Redundancy = nbRep;
+        }
     } else {
         logDebug("ADR is disabled, DR and Power not changed.");
         status &= 0xFB; // TxPower KO
--- a/plans/ChannelPlan_IN865.cpp	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlan_IN865.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -628,9 +628,11 @@
     }
 
     if (GetSettings()->Network.ADREnabled) {
-        GetSettings()->Session.TxDatarate = datarate;
-        GetSettings()->Session.TxPower = TX_POWERS[power];
-        GetSettings()->Session.Redundancy = nbRep;
+        if (status == 0x07) {
+            GetSettings()->Session.TxDatarate = datarate;
+            GetSettings()->Session.TxPower = TX_POWERS[power];
+            GetSettings()->Session.Redundancy = nbRep;
+        }
     } else {
         logDebug("ADR is disabled, DR and Power not changed.");
         status &= 0xFB; // TxPower KO
--- a/plans/ChannelPlan_KR920.cpp	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlan_KR920.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -611,9 +611,11 @@
     }
 
     if (GetSettings()->Network.ADREnabled) {
-        GetSettings()->Session.TxDatarate = datarate;
-        GetSettings()->Session.TxPower = TX_POWERS[power];
-        GetSettings()->Session.Redundancy = nbRep;
+        if (status == 0x07) {
+            GetSettings()->Session.TxDatarate = datarate;
+            GetSettings()->Session.TxPower = TX_POWERS[power];
+            GetSettings()->Session.Redundancy = nbRep;
+        }
     } else {
         logDebug("ADR is disabled, DR and Power not changed.");
         status &= 0xFB; // TxPower KO
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plans/ChannelPlan_RU864.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -0,0 +1,1065 @@
+/**********************************************************************
+* COPYRIGHT 2016 MULTI-TECH SYSTEMS, INC.
+*
+* ALL RIGHTS RESERVED BY AND FOR THE EXCLUSIVE BENEFIT OF
+* MULTI-TECH SYSTEMS, INC.
+*
+* MULTI-TECH SYSTEMS, INC. - CONFIDENTIAL AND PROPRIETARY
+* INFORMATION AND/OR TRADE SECRET.
+*
+* NOTICE: ALL CODE, PROGRAM, INFORMATION, SCRIPT, INSTRUCTION,
+* DATA, AND COMMENT HEREIN IS AND SHALL REMAIN THE CONFIDENTIAL
+* INFORMATION AND PROPERTY OF MULTI-TECH SYSTEMS, INC.
+* USE AND DISCLOSURE THEREOF, EXCEPT AS STRICTLY AUTHORIZED IN A
+* WRITTEN AGREEMENT SIGNED BY MULTI-TECH SYSTEMS, INC. IS PROHIBITED.
+*
+***********************************************************************/
+
+#include "ChannelPlan_RU864.h"
+#include "limits.h"
+
+using namespace lora;
+
+// MWF - changed RU864_TX_POWERS to match final 1.0.2 regional spec
+const uint8_t ChannelPlan_RU864::RU864_TX_POWERS[] = { 16, 14, 12, 10, 8, 6, 4, 2 };
+const uint8_t ChannelPlan_RU864::RU864_RADIO_POWERS[] = { 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 19, 20 };
+const uint8_t ChannelPlan_RU864::RU864_MAX_PAYLOAD_SIZE[] = { 51, 51, 51, 115, 242, 242, 242, 242, 0, 0, 0, 0, 0, 0, 0, 0 };
+const uint8_t ChannelPlan_RU864::RU864_MAX_PAYLOAD_SIZE_REPEATER[] = { 51, 51, 51, 115, 222, 222, 222, 222, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ChannelPlan_RU864::ChannelPlan_RU864()
+:
+    ChannelPlan(NULL, NULL)
+{
+
+}
+
+ChannelPlan_RU864::ChannelPlan_RU864(Settings* settings)
+:
+    ChannelPlan(NULL, settings)
+{
+
+}
+
+ChannelPlan_RU864::ChannelPlan_RU864(SxRadio* radio, Settings* settings)
+:
+    ChannelPlan(radio, settings)
+{
+
+}
+
+ChannelPlan_RU864::~ChannelPlan_RU864() {
+
+}
+
+void ChannelPlan_RU864::Init() {
+
+    _datarates.clear();
+    _channels.clear();
+    _dutyBands.clear();
+
+    DutyBand band;
+
+    band.Index = 0;
+    band.DutyCycle = 0;
+
+    Datarate dr;
+
+    _plan = RU864;
+    _planName = "RU864";
+    _maxTxPower = 27;
+    _minTxPower = 0;
+
+    _minFrequency = RU864_FREQ_MIN;
+    _maxFrequency = RU864_FREQ_MAX;
+
+    TX_POWERS = RU864_TX_POWERS;
+    RADIO_POWERS = RU864_RADIO_POWERS;
+    MAX_PAYLOAD_SIZE = RU864_MAX_PAYLOAD_SIZE;
+    MAX_PAYLOAD_SIZE_REPEATER = RU864_MAX_PAYLOAD_SIZE_REPEATER;
+
+    _minDatarate = RU864_MIN_DATARATE;
+    _maxDatarate = RU864_MAX_DATARATE;
+
+    _minRx2Datarate = DR_0;
+    _maxRx2Datarate = DR_7;
+
+    _minDatarateOffset = RU864_MIN_DATARATE_OFFSET;
+    _maxDatarateOffset = RU864_MAX_DATARATE_OFFSET;
+
+    _numChans125k = RU864_125K_NUM_CHANS;
+    _numChans500k = 0;
+
+    GetSettings()->Session.Rx2Frequency = RU864_RX2_FREQ;
+    GetSettings()->Session.Rx2DatarateIndex = DR_0;
+
+    GetSettings()->Session.BeaconFrequency = RU864_BEACON_FREQ;
+    GetSettings()->Session.BeaconFreqHop = false;
+    GetSettings()->Session.PingSlotFrequency = RU864_PING_SLOT_FREQ;
+    GetSettings()->Session.PingSlotDatarateIndex = RU864_BEACON_DR;
+    GetSettings()->Session.PingSlotFreqHop = false;
+
+    logInfo("Initialize datarates...");
+
+    dr.SpreadingFactor = SF_12;
+
+    // Add DR0-5
+    while (dr.SpreadingFactor >= SF_7) {
+        AddDatarate(-1, dr);
+        dr.SpreadingFactor--;
+        dr.Index++;
+    }
+
+    // Add DR6
+    dr.SpreadingFactor = SF_7;
+    dr.Bandwidth = BW_250;
+    AddDatarate(-1, dr);
+    dr.Index++;
+
+    // Add DR7
+    dr.SpreadingFactor = SF_FSK;
+    dr.Bandwidth = BW_FSK;
+    dr.PreambleLength = 10;
+    dr.Coderate = 0;
+    AddDatarate(-1, dr);
+    dr.Index++;
+
+    _maxDatarate = DR_7;
+
+    // Skip DR8-15 RFU
+    dr.SpreadingFactor = SF_INVALID;
+    while (dr.Index++ <= DR_15) {
+        AddDatarate(-1, dr);
+    }
+
+    GetSettings()->Session.TxDatarate = 0;
+
+    logInfo("Initialize channels...");
+
+    Channel chan;
+    chan.DrRange.Fields.Min = DR_0;
+    chan.DrRange.Fields.Max = DR_5;
+    chan.Index = 0;
+    chan.Frequency = RU864_125K_FREQ_BASE;
+    SetNumberOfChannels(RU864_125K_NUM_CHANS);
+
+    for (uint8_t i = 0; i < RU864_DEFAULT_NUM_CHANS; i++) {
+        chan.DrRange.Fields.Max = DR_5;
+
+        AddChannel(i, chan);
+        chan.Index++;
+        chan.Frequency += RU864_125K_FREQ_STEP;
+    }
+
+    chan.DrRange.Value = 0;
+    chan.Frequency = 0;
+
+    for (uint8_t i = RU864_DEFAULT_NUM_CHANS; i < RU864_125K_NUM_CHANS; i++) {
+        AddChannel(i, chan);
+        chan.Index++;
+    }
+
+    // Add downlink channel defaults
+    chan.Index = 0;
+    _dlChannels.resize(16);
+    for (uint8_t i = 0; i < 16; i++) {
+        AddDownlinkChannel(i, chan);
+        chan.Index++;
+    }
+
+    SetChannelMask(0, 0x07);
+
+    band.Index = 0;
+    band.FrequencyMin = RU864_MILLI_FREQ_MIN;
+    band.FrequencyMax = RU864_MILLI_FREQ_MAX;
+    band.PowerMax = 14;
+    band.TimeOffEnd = 0;
+
+    band.DutyCycle = 1000;
+
+    AddDutyBand(-1, band);
+
+    band.Index++;
+    band.FrequencyMin = RU864_MILLI_FREQ_MAX;
+    band.FrequencyMax = RU864_FREQ_MAX;
+    band.PowerMax = 14;
+    band.TimeOffEnd = 0;
+    band.DutyCycle = 0;
+
+    AddDutyBand(-1, band);
+
+    GetSettings()->Session.TxPower = GetSettings()->Network.TxPower;
+}
+
+uint8_t ChannelPlan_RU864::AddChannel(int8_t index, Channel channel) {
+    logTrace("Add Channel %d : %lu : %02x %d", index, channel.Frequency, channel.DrRange.Value, _channels.size());
+
+    assert(index < (int) _channels.size());
+
+    if (index >= 0) {
+        _channels[index] = channel;
+    } else {
+        _channels.push_back(channel);
+    }
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::HandleJoinAccept(const uint8_t* buffer, uint8_t size) {
+
+    if (size == 33) {
+        Channel ch;
+        int index = 3;
+        for (int i = 13; i < size - 5; i += 3) {
+
+            ch.Frequency = ((buffer[i]) | (buffer[i + 1] << 8) | (buffer[i + 2] << 16)) * 100u;
+
+            if (ch.Frequency > 0) {
+                ch.Index = index;
+                ch.DrRange.Fields.Min = static_cast<int8_t>(DR_0);
+                ch.DrRange.Fields.Max = static_cast<int8_t>(DR_5);
+                AddChannel(index, ch);
+
+                if (GetDutyBand(ch.Frequency) > -1)
+                    _channelMask[0] |= (1 << index);
+                else
+                    _channelMask[0] |= ~(1 << index);
+
+                index += 1;
+            }
+        }
+    }
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::SetTxConfig() {
+
+    logInfo("Configure radio for TX");
+
+    uint8_t band = GetDutyBand(GetChannel(_txChannel).Frequency);
+    Datarate txDr = GetDatarate(GetSettings()->Session.TxDatarate);
+    int8_t max_pwr = _dutyBands[band].PowerMax;
+
+    int8_t pwr = 0;
+
+    pwr = std::min < int8_t > (GetSettings()->Session.TxPower, max_pwr);
+    pwr -= GetSettings()->Network.AntennaGain;
+    
+    for (int i = 20; i >= 0; i--) {
+        if (RADIO_POWERS[i] <= pwr) {
+            pwr = i;
+            break;
+        }
+        if (i == 0) {
+            pwr = i;
+        }
+    }
+
+    logDebug("Session pwr: %d ant: %d max: %d", GetSettings()->Session.TxPower, GetSettings()->Network.AntennaGain, max_pwr);
+    logDebug("Radio Power index: %d output: %d total: %d", pwr, RADIO_POWERS[pwr], RADIO_POWERS[pwr] + GetSettings()->Network.AntennaGain);
+
+    uint32_t bw = txDr.Bandwidth;
+    uint32_t sf = txDr.SpreadingFactor;
+    uint8_t cr = txDr.Coderate;
+    uint8_t pl = txDr.PreambleLength;
+    uint16_t fdev = 0;
+    bool crc = txDr.Crc;
+    bool iq = txDr.TxIQ;
+
+    if (GetSettings()->Network.DisableCRC == true)
+        crc = false;
+
+    SxRadio::RadioModems_t modem = SxRadio::MODEM_LORA;
+
+    if (sf == SF_FSK) {
+        modem = SxRadio::MODEM_FSK;
+        sf = 50e3;
+        fdev = 25e3;
+        bw = 0;
+    }
+
+    GetRadio()->SetTxConfig(modem, pwr, fdev, bw, sf, cr, pl, false, crc, false, 0, iq, 3e3);
+
+    logDebug("TX PWR: %u DR: %u SF: %u BW: %u CR: %u PL: %u CRC: %d IQ: %d", pwr, txDr.Index, sf, bw, cr, pl, crc, iq);
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::SetRxConfig(uint8_t window, bool continuous, uint16_t wnd_growth) {
+
+    RxWindow rxw = GetRxWindow(window);
+
+    if (_dlChannels[_txChannel].Frequency != 0 && window == 1)
+        GetRadio()->SetChannel(_dlChannels[_txChannel].Frequency);
+    else
+        GetRadio()->SetChannel(rxw.Frequency);
+
+    Datarate rxDr = GetDatarate(rxw.DatarateIndex);
+    uint32_t bw = rxDr.Bandwidth;
+    uint32_t sf = rxDr.SpreadingFactor;
+    uint8_t cr = rxDr.Coderate;
+    uint8_t pl = rxDr.PreambleLength;
+    uint16_t sto = rxDr.SymbolTimeout() * wnd_growth;
+    uint32_t afc = 0;
+    bool fixLen = false;
+    uint8_t payloadLen = 0U;
+    bool crc = false; // downlink does not use CRC according to LORAWAN
+
+    if (GetSettings()->Network.DisableCRC == true)
+        crc = false;
+
+    Datarate txDr = GetDatarate(GetSettings()->Session.TxDatarate);
+    bool iq = txDr.RxIQ;
+
+    if (P2PEnabled()) {
+        iq = txDr.TxIQ;
+    }
+
+    // Beacon modifications - no I/Q inversion, fixed length rx, preamble
+    if (window == RX_BEACON) {
+        iq = txDr.TxIQ;
+        fixLen = true;
+        payloadLen = sizeof(BCNPayload);
+        pl = BEACON_PREAMBLE_LENGTH;
+    }
+
+    SxRadio::RadioModems_t modem = SxRadio::MODEM_LORA;
+
+    if (sf == SF_FSK) {
+        modem = SxRadio::MODEM_FSK;
+        sf = 50e3;
+        cr = 0;
+        bw = 0;
+        afc = 83333;
+        iq = false;
+        crc = true;  // FSK must use CRC
+    }
+
+    // Disable printf's to actually receive packets, printing to debug may mess up the timing
+    // logTrace("Configure radio for RX%d on freq: %lu", window, rxw.Frequency);
+    // logTrace("RX SF: %u BW: %u CR: %u PL: %u STO: %u CRC: %d IQ: %d", sf, bw, cr, pl, sto, crc, iq);
+
+    GetRadio()->SetRxConfig(modem, bw, sf, cr, afc, pl, sto, fixLen, payloadLen, crc, false, 0, iq, continuous);
+
+    return LORA_OK;
+}
+
+Channel ChannelPlan_RU864::GetChannel(int8_t index) {
+    Channel chan;
+    memset(&chan, 0, sizeof(Channel));
+
+    chan = _channels[index];
+
+    return chan;
+}
+
+uint8_t ChannelPlan_RU864::SetFrequencySubBand(uint8_t sub_band) {
+    return LORA_OK;
+}
+
+void ChannelPlan_RU864::LogRxWindow(uint8_t wnd) {
+
+    RxWindow rxw = GetRxWindow(wnd);
+    Datarate rxDr = GetDatarate(rxw.DatarateIndex);
+    uint8_t bw = rxDr.Bandwidth;
+    uint8_t sf = rxDr.SpreadingFactor;
+    uint8_t cr = rxDr.Coderate;
+    uint8_t pl = rxDr.PreambleLength;
+    uint16_t sto = rxDr.SymbolTimeout();
+    bool crc = false; // downlink does not use CRC according to LORAWAN
+    bool iq = GetTxDatarate().RxIQ;
+    uint32_t freq = rxw.Frequency;
+
+    if (wnd == 1 && _dlChannels[_txChannel].Frequency != 0)
+        freq = _dlChannels[_txChannel].Frequency;
+
+    logTrace("RX%d on freq: %lu", wnd, freq);
+    logTrace("RX DR: %u SF: %u BW: %u CR: %u PL: %u STO: %u CRC: %d IQ: %d", rxDr.Index, sf, bw, cr, pl, sto, crc, iq);
+}
+
+RxWindow ChannelPlan_RU864::GetRxWindow(uint8_t window) {
+    RxWindow rxw;
+    int index = 0;
+
+    if (P2PEnabled()) {
+        rxw.Frequency = GetSettings()->Network.TxFrequency;
+        index = GetSettings()->Session.TxDatarate;
+    } else {
+        switch (window) {
+        case RX_1:
+            rxw.Frequency = _channels[_txChannel].Frequency;
+
+            if (GetSettings()->Session.TxDatarate > GetSettings()->Session.Rx1DatarateOffset) {
+                index = GetSettings()->Session.TxDatarate - GetSettings()->Session.Rx1DatarateOffset;
+            } else {
+                index = 0;
+            }
+
+            break;
+
+        case RX_BEACON:
+            rxw.Frequency = GetSettings()->Session.BeaconFrequency;
+            index = RU864_BEACON_DR;
+            break;
+
+        case RX_SLOT:
+            rxw.Frequency = GetSettings()->Session.PingSlotFrequency;
+            index = GetSettings()->Session.PingSlotDatarateIndex;
+            break;
+
+        // RX2, RXC, RX_TEST, etc..
+        default:
+            rxw.Frequency = GetSettings()->Session.Rx2Frequency;
+            index = GetSettings()->Session.Rx2DatarateIndex;
+        }
+    }
+
+    rxw.DatarateIndex = index;
+
+    return rxw;
+}
+
+uint8_t ChannelPlan_RU864::HandleRxParamSetup(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) {
+    status = 0x07;
+    int8_t datarate = 0;
+    int8_t drOffset = 0;
+    uint32_t freq = 0;
+
+    drOffset = payload[index++];
+    datarate = drOffset & 0x0F;
+    drOffset = (drOffset >> 4) & 0x07;
+
+    freq = payload[index++];
+    freq |= payload[index++] << 8;
+    freq |= payload[index++] << 16;
+    freq *= 100;
+
+    if (!CheckRfFrequency(freq)) {
+        logInfo("Freq KO");
+        status &= 0xFE; // Channel frequency KO
+    }
+
+    if (datarate < _minRx2Datarate || datarate > _maxRx2Datarate) {
+        logInfo("DR KO");
+        status &= 0xFD; // Datarate KO
+    }
+
+    if (drOffset < 0 || drOffset > _maxDatarateOffset) {
+        logInfo("DR Offset KO");
+        status &= 0xFB; // Rx1DrOffset range KO
+    }
+
+    if ((status & 0x07) == 0x07) {
+        logInfo("RxParamSetup accepted Rx2DR: %d Rx2Freq: %d Rx1Offset: %d", datarate, freq, drOffset);
+        SetRx2DatarateIndex(datarate);
+        SetRx2Frequency(freq);
+        SetRx1Offset(drOffset);
+    } else {
+        logInfo("RxParamSetup rejected Rx2DR: %d Rx2Freq: %d Rx1Offset: %d", datarate, freq, drOffset);
+    }
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::HandleNewChannel(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) {
+
+    status = 0x03;
+    uint8_t channelIndex = 0;
+    Channel chParam;
+
+    channelIndex = payload[index++];
+    lora::CopyFreqtoInt(payload + index, chParam.Frequency);
+    index += 3;
+    chParam.DrRange.Value = payload[index++];
+
+    if (channelIndex < 3 || channelIndex > _channels.size() - 1) {
+        logError("New Channel index KO");
+        status &= 0xFE; // Channel index KO
+    }
+
+    if (chParam.Frequency == 0) {
+        chParam.DrRange.Value = 0;
+    } else if (chParam.Frequency < _minFrequency || chParam.Frequency > _maxFrequency) {
+        logError("New Channel frequency KO");
+        status &= 0xFE; // Channel frequency KO
+    }
+
+    if (chParam.DrRange.Fields.Min > chParam.DrRange.Fields.Max && chParam.Frequency != 0) {
+        logError("New Channel datarate min/max KO");
+        status &= 0xFD; // Datarate range KO
+    } else if ((chParam.DrRange.Fields.Min < _minDatarate || chParam.DrRange.Fields.Min > _maxDatarate) &&
+               chParam.Frequency != 0) {
+        logError("New Channel datarate min KO");
+        status &= 0xFD; // Datarate range KO
+    } else if ((chParam.DrRange.Fields.Max < _minDatarate || chParam.DrRange.Fields.Max > _maxDatarate) &&
+               chParam.Frequency != 0) {
+        logError("New Channel datarate max KO");
+        status &= 0xFD; // Datarate range KO
+    }
+
+    if ((status & 0x03) == 0x03) {
+        logInfo("New Channel accepted index: %d freq: %lu drRange: %02x", channelIndex, chParam.Frequency, chParam.DrRange.Value);
+        AddChannel(channelIndex, chParam);
+        SetChannelMask(0, _channelMask[0] | 1 << (channelIndex));
+    }
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::HandlePingSlotChannelReq(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) {
+    uint8_t datarate = 0;
+    uint32_t freq = 0;
+
+    status = 0x03;
+
+    freq = payload[index++];
+    freq |= payload[index++] << 8;
+    freq |= payload[index++] << 16;
+    freq *= 100;
+
+    datarate = payload[index] & 0x0F;
+
+    if (freq == 0U) {
+        logInfo("Received request to reset ping slot frequency to default");
+        freq = RU864_PING_SLOT_FREQ;
+    } else if (!CheckRfFrequency(freq)) {
+        logInfo("Freq KO");
+        status &= 0xFE; // Channel frequency KO
+    }
+
+    if (datarate < _minRx2Datarate || datarate > _maxRx2Datarate) {
+        logInfo("DR KO");
+        status &= 0xFD; // Datarate KO
+    }
+
+    if ((status & 0x03) == 0x03) {
+        logInfo("PingSlotChannelReq accepted DR: %d Freq: %d", datarate, freq);
+        GetSettings()->Session.PingSlotFrequency = freq;
+        GetSettings()->Session.PingSlotDatarateIndex = datarate;
+    } else {
+        logInfo("PingSlotChannelReq rejected DR: %d Freq: %d", datarate, freq);
+    }
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::HandleBeaconFrequencyReq(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status)
+{
+    uint32_t freq = 0;
+
+    status = 0x01;
+
+    freq = payload[index++];
+    freq |= payload[index++] << 8;
+    freq |= payload[index] << 16;
+    freq *= 100;
+
+    if (freq == 0U) {
+        logInfo("Received request to reset beacon frequency to default");
+        freq = RU864_BEACON_FREQ;
+    } else if (!CheckRfFrequency(freq)) {
+        logInfo("Freq KO");
+        status &= 0xFE; // Channel frequency KO
+    }
+
+    if (status & 0x01) {
+        logInfo("BeaconFrequencyReq accepted Freq: %d", freq);
+        GetSettings()->Session.BeaconFrequency = freq;
+    } else {
+        logInfo("BeaconFrequencyReq rejected Freq: %d", freq);
+    }
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::HandleAdrCommand(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status) {
+
+    uint8_t power = 0;
+    uint8_t datarate = 0;
+    uint16_t mask = 0;
+    uint16_t new_mask = 0;
+    uint8_t ctrl = 0;
+    uint8_t nbRep = 0;
+
+    status = 0x07;
+    datarate = payload[index++];
+    power = datarate & 0x0F;
+    datarate = (datarate >> 4) & 0x0F;
+
+    mask = payload[index++];
+    mask |= payload[index++] << 8;
+
+    nbRep = payload[index++];
+    ctrl = (nbRep >> 4) & 0x07;
+    nbRep &= 0x0F;
+
+    if (nbRep == 0) {
+        nbRep = 1;
+    }
+
+    if (datarate > _maxDatarate) {
+        status &= 0xFD; // Datarate KO
+    }
+    //
+    // Remark MaxTxPower = 0 and MinTxPower = 7
+    //
+    if (power > 7) {
+        status &= 0xFB; // TxPower KO
+    }
+
+    switch (ctrl) {
+        case 0:
+            SetChannelMask(0, mask);
+            break;
+
+        case 6:
+            // enable all currently defined channels
+            // set bits 0 - N of a number by (2<<N)-1
+            new_mask = (1 << _channels.size()) - 1;
+            SetChannelMask(0, new_mask);
+            break;
+
+        default:
+            logWarning("rejecting RFU or unknown control value %d", ctrl);
+            status &= 0xFE; // ChannelMask KO
+            return LORA_ERROR;
+    }
+
+    if (GetSettings()->Network.ADREnabled) {
+        GetSettings()->Session.TxDatarate = datarate;
+        GetSettings()->Session.TxPower = TX_POWERS[power];
+        GetSettings()->Session.Redundancy = nbRep;
+    } else {
+        logDebug("ADR is disabled, DR and Power not changed.");
+        status &= 0xFB; // TxPower KO
+        status &= 0xFD; // Datarate KO
+    }
+
+    logDebug("ADR DR: %u PWR: %u Ctrl: %02x Mask: %04x NbRep: %u Stat: %02x", datarate, power, ctrl, mask, nbRep, status);
+
+    return LORA_OK;
+}
+
+uint8_t ChannelPlan_RU864::ValidateAdrConfiguration() {
+    uint8_t status = 0x07;
+    uint8_t datarate = GetSettings()->Session.TxDatarate;
+    uint8_t power = GetSettings()->Session.TxPower;
+
+    if (!GetSettings()->Network.ADREnabled) {
+        logDebug("ADR disabled - no applied changes to validate");
+        return status;
+    }
+
+    if (datarate > _maxDatarate) {
+        logWarning("ADR Datarate KO - outside allowed range");
+        status &= 0xFD; // Datarate KO
+    }
+    if (power < _minTxPower || power > _maxTxPower) {
+        logWarning("ADR TX Power KO - outside allowed range");
+        status &= 0xFB; // TxPower KO
+    }
+
+    // mask must not contain any undefined channels
+    for (int i = 3; i < 16; i++) {
+        if ((_channelMask[0] & (1 << i)) && (_channels[i].Frequency == 0)) {
+            logWarning("ADR Channel Mask KO - cannot enable undefined channel");
+            status &= 0xFE; // ChannelMask KO
+            break;
+        }
+    }
+
+    return status;
+}
+
+uint8_t ChannelPlan_RU864::HandleAckTimeout() {
+
+    if (!GetSettings()->Network.ADREnabled) {
+        return LORA_ADR_OFF;
+    }
+
+    if ((++(GetSettings()->Session.AckCounter) % 2) == 0) {
+        if (GetSettings()->Session.TxPower < GetSettings()->Network.TxPowerMax) {
+            logTrace("ADR Setting power to maximum");
+            GetSettings()->Session.TxPower = GetSettings()->Network.TxPowerMax;
+        } else if (GetSettings()->Session.TxDatarate > 0) {
+            logTrace("ADR Lowering datarate");
+            (GetSettings()->Session.TxDatarate)--;
+        }
+    }
+
+    return LORA_OK;
+}
+
+
+uint32_t ChannelPlan_RU864::GetTimeOffAir()
+{
+    if (GetSettings()->Test.DisableDutyCycle == lora::ON || _LBT_TimeUs > 0)
+        return 0;
+
+    uint32_t min = 0;
+    uint32_t now = _dutyCycleTimer.read_ms();
+
+
+    min = UINT_MAX;
+    int8_t band = 0;
+
+    if (P2PEnabled()) {
+        int8_t band = GetDutyBand(GetSettings()->Network.TxFrequency);
+        if (_dutyBands[band].TimeOffEnd > now) {
+            min = _dutyBands[band].TimeOffEnd - now;
+        } else {
+            min = 0;
+        }
+    } else {
+        for (size_t i = 0; i < _channels.size(); i++) {
+            if (IsChannelEnabled(i) && GetChannel(i).Frequency != 0 &&
+                !(GetSettings()->Session.TxDatarate < GetChannel(i).DrRange.Fields.Min ||
+                  GetSettings()->Session.TxDatarate > GetChannel(i).DrRange.Fields.Max)) {
+
+                band = GetDutyBand(GetChannel(i).Frequency);
+                if (band != -1) {
+                    // logDebug("band: %d time-off: %d now: %d", band, _dutyBands[band].TimeOffEnd, now);
+                    if (_dutyBands[band].TimeOffEnd > now) {
+                        min = std::min < uint32_t > (min, _dutyBands[band].TimeOffEnd - now);
+                    } else {
+                        min = 0;
+                        break;
+                    }
+                }
+            }
+        }
+    }
+
+
+    if (GetSettings()->Session.AggregatedTimeOffEnd > 0 && GetSettings()->Session.AggregatedTimeOffEnd > now) {
+        min = std::max < uint32_t > (min, GetSettings()->Session.AggregatedTimeOffEnd - now);
+    }
+
+    now = time(NULL);
+    uint32_t join_time = 0;
+
+    if (GetSettings()->Session.JoinFirstAttempt != 0 && now < GetSettings()->Session.JoinTimeOffEnd) {
+        join_time = (GetSettings()->Session.JoinTimeOffEnd - now) * 1000;
+    }
+
+    min = std::max < uint32_t > (join_time, min);
+
+    return min;
+}
+
+
+void ChannelPlan_RU864::UpdateDutyCycle(uint32_t freq, uint32_t time_on_air_ms) {
+    if (GetSettings()->Test.DisableDutyCycle == lora::ON || _LBT_TimeUs > 0) {
+        _dutyCycleTimer.stop();
+        for (size_t i = 0; i < _dutyBands.size(); i++) {
+            _dutyBands[i].TimeOffEnd = 0;
+        }
+        return;
+    }
+
+    _dutyCycleTimer.start();
+
+    if (GetSettings()->Session.MaxDutyCycle > 0 && GetSettings()->Session.MaxDutyCycle <= 15) {
+        GetSettings()->Session.AggregatedTimeOffEnd = _dutyCycleTimer.read_ms() + time_on_air_ms * GetSettings()->Session.AggregateDutyCycle;
+        logDebug("Updated Aggregate DCycle Time-off: %lu DC: %f%%", GetSettings()->Session.AggregatedTimeOffEnd, 1 / float(GetSettings()->Session.AggregateDutyCycle));
+    } else {
+        GetSettings()->Session.AggregatedTimeOffEnd = 0;
+    }
+
+
+    uint32_t time_off_air = 0;
+    uint32_t now = _dutyCycleTimer.read_ms();
+
+    for (size_t i = 0; i < _dutyBands.size(); i++) {
+        if (_dutyBands[i].TimeOffEnd < now) {
+            _dutyBands[i].TimeOffEnd = 0;
+        } else {
+            _dutyBands[i].TimeOffEnd -= now;
+        }
+
+        if (freq >= _dutyBands[i].FrequencyMin && freq <= _dutyBands[i].FrequencyMax) {
+            logDebug("update TOE: freq: %d i:%d toa: %d DC:%d", freq, i, time_on_air_ms, _dutyBands[i].DutyCycle);
+            time_off_air = time_on_air_ms * _dutyBands[i].DutyCycle;
+            _dutyBands[i].TimeOffEnd = time_off_air;
+        }
+    }
+
+    ResetDutyCycleTimer();
+}
+
+std::vector<uint32_t> lora::ChannelPlan_RU864::GetChannels() {
+    std::vector < uint32_t > chans;
+
+    for (int8_t i = 0; i < (int) _channels.size(); i++) {
+        chans.push_back(_channels[i].Frequency);
+    }
+    chans.push_back(GetRxWindow(2).Frequency);
+
+    return chans;
+}
+
+std::vector<uint8_t> lora::ChannelPlan_RU864::GetChannelRanges() {
+    std::vector < uint8_t > ranges;
+
+    for (int8_t i = 0; i < (int) _channels.size(); i++) {
+        ranges.push_back(_channels[i].DrRange.Value);
+    }
+
+    ranges.push_back(GetRxWindow(2).DatarateIndex);
+
+    return ranges;
+
+}
+
+void lora::ChannelPlan_RU864::EnableDefaultChannels() {
+    _channelMask[0] |= 0x0007;
+}
+
+uint8_t ChannelPlan_RU864::GetNextChannel()
+{
+    if (GetSettings()->Session.AggregatedTimeOffEnd != 0) {
+        return LORA_AGGREGATED_DUTY_CYCLE;
+    }
+
+    if (P2PEnabled() || GetSettings()->Network.TxFrequency != 0) {
+        logDebug("Using frequency %d", GetSettings()->Network.TxFrequency);
+
+        if (GetSettings()->Test.DisableDutyCycle != lora::ON && _LBT_TimeUs == 0) {
+            int8_t band = GetDutyBand(GetSettings()->Network.TxFrequency);
+            logDebug("band: %d freq: %d", band, GetSettings()->Network.TxFrequency);
+            if (band != -1 && _dutyBands[band].TimeOffEnd != 0) {
+                return LORA_NO_CHANS_ENABLED;
+            }
+        }
+
+        GetRadio()->SetChannel(GetSettings()->Network.TxFrequency);
+        return LORA_OK;
+    }
+
+    uint8_t start = 0;
+    uint8_t maxChannels = _numChans125k;
+    uint8_t nbEnabledChannels = 0;
+    uint8_t *enabledChannels = new uint8_t[maxChannels];
+
+    if (GetTxDatarate().Bandwidth == BW_500) {
+        maxChannels = _numChans500k;
+        start = _numChans125k;
+    }
+
+// Search how many channels are enabled
+    DatarateRange range;
+    uint8_t dr_index = GetSettings()->Session.TxDatarate;
+    uint32_t now = _dutyCycleTimer.read_ms();
+
+    for (size_t i = 0; i < _dutyBands.size(); i++) {
+        if (_dutyBands[i].TimeOffEnd < now || GetSettings()->Test.DisableDutyCycle == lora::ON || _LBT_TimeUs > 0) {
+            _dutyBands[i].TimeOffEnd = 0;
+        }
+    }
+
+    for (uint8_t i = start; i < start + maxChannels; i++) {
+        range = GetChannel(i).DrRange;
+        // logDebug("chan: %d freq: %d range:%02x", i, GetChannel(i).Frequency, range.Value);
+
+        if (IsChannelEnabled(i) && (dr_index >= range.Fields.Min && dr_index <= range.Fields.Max)) {
+            int8_t band = GetDutyBand(GetChannel(i).Frequency);
+            // logDebug("band: %d freq: %d", band, _channels[i].Frequency);
+            if (band != -1 && _dutyBands[band].TimeOffEnd == 0) {
+                enabledChannels[nbEnabledChannels++] = i;
+            }
+        }
+    }
+
+    logTrace("Number of available channels: %d", nbEnabledChannels);
+
+    uint32_t freq = 0;
+    uint8_t sf = GetTxDatarate().SpreadingFactor;
+    uint8_t bw = GetTxDatarate().Bandwidth;
+    int16_t thres = DEFAULT_FREE_CHAN_RSSI_THRESHOLD;
+
+    if (nbEnabledChannels == 0) {
+        delete [] enabledChannels;
+        return LORA_NO_CHANS_ENABLED;
+    }
+
+
+    if (GetSettings()->Network.CADEnabled) {
+        // Search for free channel with ms timeout
+        int16_t timeout = 10000;
+        Timer tmr;
+        tmr.start();
+
+        for (uint8_t j = rand_r(0, nbEnabledChannels - 1); tmr.read_ms() < timeout; j++) {
+            freq = GetChannel(enabledChannels[j]).Frequency;
+
+            if (GetRadio()->IsChannelFree(SxRadio::MODEM_LORA, freq, sf, thres, bw)) {
+                _txChannel = enabledChannels[j];
+                break;
+            }
+        }
+    } else {
+        uint8_t j = rand_r(0, nbEnabledChannels - 1);
+        _txChannel = enabledChannels[j];
+        freq = GetChannel(_txChannel).Frequency;
+    }
+
+    assert(freq != 0);
+
+    logDebug("Using channel %d : %d", _txChannel, freq);
+    GetRadio()->SetChannel(freq);
+
+
+    delete [] enabledChannels;
+    return LORA_OK;
+}
+
+
+uint8_t lora::ChannelPlan_RU864::GetJoinDatarate() {
+    uint8_t dr = GetSettings()->Session.TxDatarate;
+    static uint8_t cnt = 0;
+
+    if (GetSettings()->Test.DisableRandomJoinDatarate == lora::OFF) {
+        if ((cnt++ % 20) == 0) {
+            dr = lora::DR_0;
+        } else if ((cnt % 16) == 0) {
+            dr = lora::DR_1;
+        } else if ((cnt % 12) == 0) {
+            dr = lora::DR_2;
+        } else if ((cnt % 8) == 0) {
+            dr = lora::DR_3;
+        } else if ((cnt % 4) == 0) {
+            dr = lora::DR_4;
+        } else {
+            dr = lora::DR_5;
+        }
+    }
+
+    return dr;
+}
+
+uint8_t ChannelPlan_RU864::CalculateJoinBackoff(uint8_t size) {
+
+    time_t now = time(NULL);
+    uint32_t time_on_max = 0;
+    static uint32_t time_off_max = 15;
+    uint32_t rand_time_off = 0;
+
+    // TODO: calc time-off-max based on RTC time from JoinFirstAttempt, time-off-max is lost over sleep
+
+    if ((time_t)GetSettings()->Session.JoinTimeOffEnd > now) {
+        return LORA_JOIN_BACKOFF;
+    }
+
+    uint32_t secs_since_first_attempt = (now - GetSettings()->Session.JoinFirstAttempt);
+    uint16_t hours_since_first_attempt = secs_since_first_attempt / (60 * 60);
+
+    static uint8_t join_cnt = 0;
+
+    join_cnt = (join_cnt+1) % 8;
+
+    if (GetSettings()->Session.JoinFirstAttempt == 0) {
+        /* 1 % duty-cycle for first hour
+         * 0.1 % next 10 hours
+         * 0.01 % upto 24 hours         */
+        GetSettings()->Session.JoinFirstAttempt = now;
+        GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size);
+        GetSettings()->Session.JoinTimeOffEnd = now + (GetTimeOnAir(size) / 10);
+    } else if (join_cnt == 0) {
+        if (hours_since_first_attempt < 1) {
+            time_on_max = 36000;
+            rand_time_off = rand_r(time_off_max - 1, time_off_max + 1);
+            // time off max 1 hour
+            time_off_max = std::min < uint32_t > (time_off_max * 2, 60 * 60);
+
+            if (GetSettings()->Session.JoinTimeOnAir < time_on_max) {
+                GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size);
+                GetSettings()->Session.JoinTimeOffEnd = now + rand_time_off;
+            } else {
+                logWarning("Max time-on-air limit met for current join backoff period");
+                GetSettings()->Session.JoinTimeOffEnd = GetSettings()->Session.JoinFirstAttempt + 60 * 60;
+            }
+        } else if (hours_since_first_attempt < 11) {
+            if (GetSettings()->Session.JoinTimeOnAir < 36000) {
+                GetSettings()->Session.JoinTimeOnAir = 36000;
+            }
+            time_on_max = 72000;
+            rand_time_off = rand_r(time_off_max - 1, time_off_max + 1);
+            // time off max 1 hour
+            time_off_max = std::min < uint32_t > (time_off_max * 2, 60 * 60);
+
+            if (GetSettings()->Session.JoinTimeOnAir < time_on_max) {
+                GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size);
+                GetSettings()->Session.JoinTimeOffEnd = now + rand_time_off;
+            } else {
+                logWarning("Max time-on-air limit met for current join backoff period");
+                GetSettings()->Session.JoinTimeOffEnd = GetSettings()->Session.JoinFirstAttempt + 11 * 60 * 60;
+            }
+        } else {
+            if (GetSettings()->Session.JoinTimeOnAir < 72000) {
+                GetSettings()->Session.JoinTimeOnAir = 72000;
+            }
+            uint32_t join_time = 1200;
+
+            time_on_max = 80700;
+            time_off_max = 1 * 60 * 60; // 1 hour
+            rand_time_off = rand_r(time_off_max - 1, time_off_max + 1);
+
+            // allow one final join attempt as long as it doesn't start past the max time on air
+            if (GetSettings()->Session.JoinTimeOnAir < time_on_max - join_time) {
+                GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size);
+                GetSettings()->Session.JoinTimeOffEnd = now + rand_time_off;
+            } else {
+                logWarning("Max time-on-air limit met for current join backoff period");
+                // Reset the join time on air and set end of restriction to the next 24 hour period
+                GetSettings()->Session.JoinTimeOnAir = 72000;
+                uint16_t days = (now - GetSettings()->Session.JoinFirstAttempt) / (24 * 60 * 60) + 1;
+                logWarning("days : %d", days);
+                GetSettings()->Session.JoinTimeOffEnd = GetSettings()->Session.JoinFirstAttempt + ((days * 24) + 11) * 60 * 60;
+            }
+        }
+
+        logWarning("JoinBackoff: %lu seconds  Time On Air: %lu / %lu", GetSettings()->Session.JoinTimeOffEnd - now, GetSettings()->Session.JoinTimeOnAir, time_on_max);
+    } else {
+        GetSettings()->Session.JoinTimeOnAir += GetTimeOnAir(size);
+        GetSettings()->Session.JoinTimeOffEnd = now + (GetTimeOnAir(size) / 10);
+    }
+
+    return LORA_OK;
+}
+
+bool ChannelPlan_RU864::DecodeBeacon(const uint8_t* payload, size_t size, BeaconData_t& data) {
+    uint16_t crc1, crc1_rx, crc2, crc2_rx;
+    const BCNPayload* beacon = (const BCNPayload*)payload;
+
+    // First check the size of the packet
+    if (size != sizeof(BCNPayload))
+        return false;
+
+    // Next we verify the CRCs are correct
+    crc1 = CRC16(beacon->RFU, sizeof(beacon->RFU) + sizeof(beacon->Time));
+    memcpy((uint8_t*)&crc1_rx, beacon->CRC1, sizeof(uint16_t));
+
+    if (crc1 != crc1_rx)
+        return false;
+
+    crc2 = CRC16(beacon->GwSpecific, sizeof(beacon->GwSpecific));
+    memcpy((uint8_t*)&crc2_rx, beacon->CRC2, sizeof(uint16_t));
+
+    if (crc2 != crc2_rx)
+        return false;
+
+    // Now that we have confirmed this packet is a beacon, parse and complete the output struct
+    memcpy(&data.Time, beacon->Time, sizeof(beacon->Time));
+    data.InfoDesc = beacon->GwSpecific[0];
+
+    // Update the GPS fields if we have a gps info descriptor
+    if (data.InfoDesc == GPS_FIRST_ANTENNA ||
+        data.InfoDesc == GPS_SECOND_ANTENNA ||
+        data.InfoDesc == GPS_THIRD_ANTENNA) {
+        // Latitude and Longitude 3 bytes in length
+        memcpy(&data.Latitude, &beacon->GwSpecific[1], 3);
+        memcpy(&data.Longitude, &beacon->GwSpecific[4], 3);
+    }
+
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plans/ChannelPlan_RU864.h	Tue Sep 18 08:25:41 2018 -0500
@@ -0,0 +1,270 @@
+/**   __  ___     ____  _    ______        __     ____         __                  ____
+ *   /  |/  /_ __/ / /_(_)__/_  __/__ ____/ /    / __/_ _____ / /____ __ _  ___   /  _/__  ____
+ *  / /|_/ / // / / __/ /___// / / -_) __/ _ \  _\ \/ // (_-</ __/ -_)  ' \(_-<  _/ // _ \/ __/ __
+ * /_/  /_/\_,_/_/\__/_/    /_/  \__/\__/_//_/ /___/\_, /___/\__/\__/_/_/_/___/ /___/_//_/\__/ /_/
+ * Copyright (C) 2015 by Multi-Tech Systems        /___/
+ *
+ *
+ * @author Jason Reiss
+ * @date   10-31-2015
+ * @brief  lora::ChannelPlan provides an interface for LoRaWAN channel schemes
+ *
+ * @details
+ *
+ */
+
+#ifndef __CHANNEL_PLAN_RU864_H__
+#define __CHANNEL_PLAN_RU864_H__
+
+#include "Lora.h"
+#include "SxRadio.h"
+#include <vector>
+#include "ChannelPlan.h"
+
+namespace lora {
+
+    const uint8_t RU864_125K_NUM_CHANS = 16;                    //!< Number of 125k channels in RU864 channel plan
+    const uint8_t RU864_DEFAULT_NUM_CHANS = 2;                  //!< Number of defualt channels in RU864 channel plan
+    const uint32_t RU864_125K_FREQ_BASE = 868900000;            //!< Frequency base for 125k RU864 uplink channels
+    const uint32_t RU864_125K_FREQ_STEP = 200000;               //!< Frequency step for 125k RU864 uplink channels
+    const uint32_t RU864_RX2_FREQ = 869100000;                  //!< Frequency default for second rx window in RU864
+
+    const uint8_t RU864_TX_POWER_MAX = 16;                      //!< Max power for RU864 channel plan
+
+    // 0.1% duty cycle 864-866
+    const uint32_t RU864_MILLI_FREQ_MIN = 864000000;
+    const uint32_t RU864_MILLI_FREQ_MAX = 865000000;
+
+
+    const uint32_t RU864_FREQ_MIN = 864000000;
+    const uint32_t RU864_FREQ_MAX = 870000000;
+
+    const uint8_t RU864_MIN_DATARATE = (uint8_t) DR_0;           //!< Minimum transmit datarate for RU864
+    const uint8_t RU864_MAX_DATARATE = (uint8_t) DR_7;           //!< Maximum transmit datarate for RU864
+
+    const uint8_t RU864_MIN_DATARATE_OFFSET = (uint8_t) 0;       //!< Minimum transmit datarate for US915
+    const uint8_t RU864_MAX_DATARATE_OFFSET = (uint8_t) 5;       //!< Maximum transmit datarate for US915
+
+    const uint8_t  RU864_BEACON_DR = DR_3;                       //!< Default beacon datarate
+    const uint32_t RU864_BEACON_FREQ = 869100000U;               //!< Default beacon broadcast frequency
+    const uint32_t RU864_PING_SLOT_FREQ = 868900000U;            //!< Default ping slot frequency
+
+    class ChannelPlan_RU864 : public lora::ChannelPlan {
+        public:
+            /**
+             * ChannelPlan constructor
+             * @param radio SxRadio object used to set Tx/Rx config
+             * @param settings Settings object
+             */
+            ChannelPlan_RU864();
+            ChannelPlan_RU864(Settings* settings);
+            ChannelPlan_RU864(SxRadio* radio, Settings* settings);
+
+            /**
+             * ChannelPlan destructor
+             */
+            virtual ~ChannelPlan_RU864();
+
+            /**
+             * Initialize channels, datarates and duty cycle bands according to current channel plan in settings
+             */
+            virtual void Init();
+
+            /**
+             * Get the next channel to use to transmit
+             * @return LORA_OK if channel was found
+             * @return LORA_NO_CHANS_ENABLED
+             */
+            virtual uint8_t GetNextChannel();
+
+            /**
+             * Add a channel to the ChannelPlan
+             * @param index of channel, use -1 to add to end
+             * @param channel settings to add
+             */
+            virtual uint8_t AddChannel(int8_t index, Channel channel);
+
+            /**
+             * Get channel at index
+             * @return Channel
+             */
+            virtual Channel GetChannel(int8_t index);
+
+            /**
+             * Get rx window settings for requested window
+             * RX_1, RX_2, RX_BEACON, RX_SLOT
+             * @param window
+             * @return RxWindow
+             */
+            virtual RxWindow GetRxWindow(uint8_t window);
+
+            /**
+             * Get datarate to use on the join request
+             * @return datarate index
+             */
+            virtual uint8_t GetJoinDatarate();
+
+            /**
+             * Calculate the next time a join request is possible
+             * @param size of join frame
+             * @returns LORA_OK
+             */
+            virtual uint8_t CalculateJoinBackoff(uint8_t size);
+
+            /**
+             * Get next channel and set the SxRadio tx config with current settings
+             * @return LORA_OK
+             */
+            virtual uint8_t SetTxConfig();
+
+            /**
+             * Set the SxRadio rx config provided window
+             * @param window to be opened
+             * @param continuous keep window open
+             * @param wnd_growth factor to increase the rx window by
+             * @return LORA_OK
+             */
+            virtual uint8_t SetRxConfig(uint8_t window, bool continuous, uint16_t wnd_growth);
+
+            /**
+             * Set frequency sub band if supported by plan
+             * @param sub_band
+             * @return LORA_OK
+             */
+            virtual uint8_t SetFrequencySubBand(uint8_t sub_band);
+
+            /**
+             * Callback for ACK timeout event
+             * @return LORA_OK
+             */
+            virtual uint8_t HandleAckTimeout();
+
+            /**
+             * Callback for Join Accept packet to load optional channels
+             * @return LORA_OK
+             */
+            virtual uint8_t HandleJoinAccept(const uint8_t* buffer, uint8_t size);
+
+            /**
+             * Callback to for rx parameter setup ServerCommand
+             * @param payload packet data
+             * @param index of start of command buffer
+             * @param size number of bytes in command buffer
+             * @param[out] status to be returned in MoteCommand answer
+             * @return LORA_OK
+             */
+            virtual uint8_t HandleRxParamSetup(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status);
+
+            /**
+             * Callback to for new channel ServerCommand
+             * @param payload packet data
+             * @param index of start of command buffer
+             * @param size number of bytes in command buffer
+             * @param[out] status to be returned in MoteCommand answer
+             * @return LORA_OK
+             */
+            virtual uint8_t HandleNewChannel(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status);
+
+            /**
+             * Callback to for ping slot channel request ServerCommand
+             * @param payload packet data
+             * @param index of start of command buffer
+             * @param size number of bytes in command buffer
+             * @param[out] status to be returned in MoteCommand answer
+             * @return LORA_OK
+             */
+            virtual uint8_t HandlePingSlotChannelReq(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status);
+
+            /**
+             * Callback to for beacon frequency request ServerCommand
+             * @param payload packet data
+             * @param index of start of command buffer
+             * @param size number of bytes in command buffer
+             * @param[out] status to be returned in MoteCommand answer
+             * @return LORA_OK
+             */
+            virtual uint8_t HandleBeaconFrequencyReq(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status);
+
+            /**
+             * Callback to for adaptive datarate ServerCommand
+             * @param payload packet data
+             * @param index of start of command buffer
+             * @param size number of bytes in command buffer
+             * @param[out] status to be returned in MoteCommand answer
+             * @return LORA_OK
+             */
+            virtual uint8_t HandleAdrCommand(const uint8_t* payload, uint8_t index, uint8_t size, uint8_t& status);
+
+            /**
+             * Validate the configuration after multiple ADR commands have been applied
+             * @return status to be returned in MoteCommand answer
+             */
+            virtual uint8_t ValidateAdrConfiguration();
+
+            /**
+             * Update duty cycle with at given frequency and time on air
+             * @param freq frequency
+             * @param time_on_air_ms tx time on air
+             */
+            virtual void UpdateDutyCycle(uint32_t freq, uint32_t time_on_air_ms);
+
+            /**
+             * Get the time the radio must be off air to comply with regulations
+             * Time to wait may be dependent on duty-cycle restrictions per channel
+             * Or duty-cycle of join requests if OTAA is being attempted
+             * @return ms of time to wait for next tx opportunity
+             */
+            virtual uint32_t GetTimeOffAir();
+
+            /**
+             * Get the channels in use by current channel plan
+             * @return channel frequencies
+             */
+            virtual std::vector<uint32_t> GetChannels();
+
+            /**
+             * Get the channel datarate ranges in use by current channel plan
+             * @return channel datarate ranges
+             */
+            virtual std::vector<uint8_t> GetChannelRanges();
+
+            /**
+             * Print log message for given rx window
+             * @param wnd 1 or 2
+             */
+            virtual void LogRxWindow(uint8_t wnd);
+
+            /**
+             * Enable the default channels of the channel plan
+             */
+            virtual void EnableDefaultChannels();
+
+            /**
+             * Check if this packet is a beacon and if so extract parameters needed
+             * @param payload of potential beacon
+             * @param size of the packet
+             * @param [out] data extracted from the beacon if this packet was indeed a beacon
+             * @return true if this packet is beacon, false if not
+             */
+            virtual bool DecodeBeacon(const uint8_t* payload,
+                                      size_t size,
+                                      BeaconData_t& data);
+
+        protected:
+
+            static const uint8_t RU864_TX_POWERS[8];                    //!< List of available tx powers
+            static const uint8_t RU864_RADIO_POWERS[21];                 //!< List of calibrated tx powers
+            static const uint8_t RU864_MAX_PAYLOAD_SIZE[];              //!< List of max payload sizes for each datarate
+            static const uint8_t RU864_MAX_PAYLOAD_SIZE_REPEATER[];     //!< List of repeater compatible max payload sizes for each datarate
+
+            typedef struct __attribute__((packed)) {
+                uint8_t RFU[2];
+                uint8_t Time[4];
+                uint8_t CRC1[2];
+                uint8_t GwSpecific[7];
+                uint8_t CRC2[2];
+            } BCNPayload;
+    };
+}
+
+#endif //__CHANNEL_PLAN_RU864_H__
--- a/plans/ChannelPlan_US915.cpp	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlan_US915.cpp	Tue Sep 18 08:25:41 2018 -0500
@@ -159,8 +159,10 @@
 
 uint8_t ChannelPlan_US915::HandleJoinAccept(const uint8_t* buffer, uint8_t size) {
 
-    if (size > 17) {
-        // TODO: Handle future channel mask settings
+    if (size > 17 && buffer[28] == 0x01) {
+        for (int i = 13; i < size - 5; i += 2) {
+            SetChannelMask((i-13)/2, buffer[i+1] << 8 | buffer[i]);
+        }
     }
 
     return LORA_OK;
@@ -678,9 +680,11 @@
     }
 
     if (GetSettings()->Network.ADREnabled) {
-        GetSettings()->Session.TxDatarate = datarate;
-        GetSettings()->Session.TxPower = TX_POWERS[power];
-        GetSettings()->Session.Redundancy = nbRep;
+        if (status == 0x07) {
+            GetSettings()->Session.TxDatarate = datarate;
+            GetSettings()->Session.TxPower = TX_POWERS[power];
+            GetSettings()->Session.Redundancy = nbRep;
+        }
     } else {
         logDebug("ADR is disabled, DR and Power not changed.");
         status &= 0xFB; // TxPower KO
--- a/plans/ChannelPlans.h	Wed Sep 12 15:04:54 2018 -0500
+++ b/plans/ChannelPlans.h	Tue Sep 18 08:25:41 2018 -0500
@@ -5,6 +5,7 @@
 #include "ChannelPlan_KR920.h"
 #include "ChannelPlan_IN865.h"
 #include "ChannelPlan_AS923_Japan.h"
+#include "ChannelPlan_RU864.h"
 
 
 #define CP_AS923 1
@@ -14,3 +15,4 @@
 #define CP_KR920 5
 #define CP_IN865 6
 #define CP_AS923_JAPAN 7
+#define CP_RU864 8