Forked LoRaWAN-lib for Legacy Gateway testing

Fork of LoRaWAN-lib by Miguel Luis

Files at this revision

API Documentation at this revision

Comitter:
mick_ccc
Date:
Thu Apr 20 14:26:48 2017 +0000
Parent:
8:2d237e069df2
Commit message:
Hacked for Legacy Gateway testing

Changed in this revision

LoRaMac-definitions.h Show annotated file Show diff for this revision Revisions of this file
LoRaMac.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2d237e069df2 -r e52525844705 LoRaMac-definitions.h
--- a/LoRaMac-definitions.h	Wed Mar 22 12:38:38 2017 +0000
+++ b/LoRaMac-definitions.h	Thu Apr 20 14:26:48 2017 +0000
@@ -472,6 +472,135 @@
  */
 #define JOIN_CHANNELS      ( uint16_t )( LC( 1 ) | LC( 2 ) | LC( 3 ) )
 
+#elif defined( USE_BAND_868_LEGACY )
+
+/*!
+ * LoRaMac maximum number of channels
+ */
+#define LORA_MAX_NB_CHANNELS                        16
+
+/*!
+ * Minimal datarate that can be used by the node
+ */
+#define LORAMAC_TX_MIN_DATARATE                     DR_0
+
+/*!
+ * Maximal datarate that can be used by the node
+ */
+#define LORAMAC_TX_MAX_DATARATE                     DR_7
+
+/*!
+ * Minimal datarate that can be used by the node
+ */
+#define LORAMAC_RX_MIN_DATARATE                     DR_0
+
+/*!
+ * Maximal datarate that can be used by the node
+ */
+#define LORAMAC_RX_MAX_DATARATE                     DR_7
+
+/*!
+ * Default datarate used by the node
+ */
+#define LORAMAC_DEFAULT_DATARATE                    DR_0
+
+/*!
+ * Minimal Rx1 receive datarate offset
+ */
+#define LORAMAC_MIN_RX1_DR_OFFSET                   0
+
+/*!
+ * Maximal Rx1 receive datarate offset
+ */
+#define LORAMAC_MAX_RX1_DR_OFFSET                   7
+
+/*!
+ * Minimal Tx output power that can be used by the node
+ */
+#define LORAMAC_MIN_TX_POWER                        TX_POWER_02_DBM
+
+/*!
+ * Maximal Tx output power that can be used by the node
+ */
+#define LORAMAC_MAX_TX_POWER                        TX_POWER_20_DBM
+
+/*!
+ * Default Tx output power used by the node
+ */
+#define LORAMAC_DEFAULT_TX_POWER                    TX_POWER_14_DBM
+
+/*!
+ * LoRaMac TxPower definition
+ */
+#define TX_POWER_20_DBM                             0
+#define TX_POWER_14_DBM                             1
+#define TX_POWER_11_DBM                             2
+#define TX_POWER_08_DBM                             3
+#define TX_POWER_05_DBM                             4
+#define TX_POWER_02_DBM                             5
+
+/*!
+ * LoRaMac datarates definition
+ */
+#define DR_0                                        0  // SF12 - BW125
+#define DR_1                                        1  // SF11 - BW125
+#define DR_2                                        2  // SF10 - BW125
+#define DR_3                                        3  // SF9  - BW125
+#define DR_4                                        4  // SF8  - BW125
+#define DR_5                                        5  // SF7  - BW125
+#define DR_6                                        6  // SF7  - BW250
+#define DR_7                                        7  // FSK
+
+/*!
+ * Verification of default datarate
+ */
+#if ( LORAMAC_DEFAULT_DATARATE > DR_5 )
+#error "A default DR higher than DR_5 may lead to connectivity loss."
+#endif
+
+/*!
+ * Second reception window channel definition.
+ */
+// Channel = { Frequency [Hz], Datarate }
+#define RX_WND_2_CHANNEL                                  { 865150000, DR_4 }
+
+/*!
+ * LoRaMac maximum number of bands
+ */
+#define LORA_MAX_NB_BANDS                           5
+
+/*!
+ * LoRaMac EU868 default bands
+ */
+typedef enum
+{
+    BAND_G1_0,
+    BAND_G1_1,
+    BAND_G1_2,
+    BAND_G1_3,
+    BAND_G1_4,
+}BandId_t;
+
+// Band = { DutyCycle, TxMaxPower, LastTxDoneTime, TimeOff }
+#define BAND0              { 100 , TX_POWER_14_DBM, 0,  0 } //  1.0 %
+#define BAND1              { 100 , TX_POWER_14_DBM, 0,  0 } //  1.0 %
+#define BAND2              { 1000, TX_POWER_14_DBM, 0,  0 } //  0.1 %
+#define BAND3              { 10  , TX_POWER_14_DBM, 0,  0 } // 10.0 %
+#define BAND4              { 100 , TX_POWER_14_DBM, 0,  0 } //  1.0 %
+
+/*!
+ * LoRaMac default channels
+ */
+// Channel = { Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
+#define LC1                { 868090000, { ( ( DR_6 << 4 ) | DR_6 ) }, 0 }
+#define LC2                { 868270000, { ( ( DR_7 << 4 ) | DR_6 ) }, 0 }
+#define LC3                { 868450000, { ( ( DR_7 << 4 ) | DR_6 ) }, 0 }
+
+/*!
+ * LoRaMac channels which are allowed for the join procedure
+ */
+#define JOIN_CHANNELS      ( uint16_t )( LC( 1 ) | LC( 2 ) | LC( 3 ) )
+
 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
 
 /*!
diff -r 2d237e069df2 -r e52525844705 LoRaMac.cpp
--- a/LoRaMac.cpp	Wed Mar 22 12:38:38 2017 +0000
+++ b/LoRaMac.cpp	Thu Apr 20 14:26:48 2017 +0000
@@ -36,7 +36,7 @@
 
 /*!
  * FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
- * in RxWindowSetup function.
+ * in  function.
  * Maximum PHYPayload = MaxPayloadOfDatarate/MaxPayloadOfDatarateRepeater + LORA_MAC_FRMPAYLOAD_OVERHEAD
  */
 #define LORA_MAC_FRMPAYLOAD_OVERHEAD                13 // MHDR(1) + FHDR(7) + Port(1) + MIC(4)
@@ -362,11 +362,15 @@
     LC2,
     LC3,
 };
-#elif defined( USE_BAND_868 )
+#elif defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
 /*!
  * Data rates table definition
  */
+#if defined( USE_BAND_868 )
 const uint8_t Datarates[]  = { 12, 11, 10,  9,  8,  7,  7, 50 };
+#else
+const uint8_t Datarates[]  = { 12, 11, 10,  9,  8,  7,  50, 50 }; // TODO FSK datarates
+#endif
 
 /*!
  * Bandwidths table definition in Hz
@@ -1712,7 +1716,7 @@
             }
             else
             {
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                 // Re-enable default channels LC1, LC2, LC3
                 LoRaMacParams.ChannelsMask[0] = LoRaMacParams.ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
 #elif defined( USE_BAND_470 )
@@ -1815,6 +1819,15 @@
 
 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
     RxWindowSetup( Channels[Channel].Frequency, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
+#elif defined( USE_BAND_868_LEGACY )
+    if( (RxWindowsParams[0].Datarate == DR_7) || (RxWindowsParams[0].Datarate == DR_6) )
+    {
+        RxWindowSetup( 869525000, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
+    }
+    else
+    {
+        RxWindowSetup( 869850000, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
+    }
 #elif defined( USE_BAND_470 )
     RxWindowSetup( LORAMAC_FIRST_RX1_CHANNEL + ( Channel % 48 ) * LORAMAC_STEPWIDTH_RX1_CHANNEL, RxWindowsParams[0].Datarate, RxWindowsParams[0].Bandwidth, RxWindowsParams[0].RxWindowTimeout, false );
 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
@@ -1934,7 +1947,7 @@
                     { // Check if the channel is enabled
                         continue;
                     }
-#if defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868_LEGACY )
                     if( IsLoRaMacNetworkJoined == false )
                     {
                         if( ( JOIN_CHANNELS & ( 1 << j ) ) == 0 )
@@ -2013,6 +2026,20 @@
             modem = MODEM_LORA;
             Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
         }
+#elif defined( USE_BAND_868_LEGACY )
+        if( (datarate == DR_7) || (datarate == DR_6) )
+        {
+            //Radio.SetChannel( 869525000 );
+            modem = MODEM_FSK;
+            Radio.SetRxConfig( modem, 14.8e3, 4.8 * 1e3, 0, 83.333e3, 5, timeout, false, 0, true, 0, 0, false, rxContinuous );
+        }
+        else
+        {
+            //Radio.SetChannel( 869850000 );
+            modem = MODEM_LORA;
+            Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
+            //Radio.SetRxConfig( modem, bandwidth, 7, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
+        }
 #elif defined( USE_BAND_470 ) || defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
         modem = MODEM_LORA;
         Radio.SetRxConfig( modem, bandwidth, downlinkDatarate, 1, 0, 8, timeout, false, 0, false, 0, 0, true, rxContinuous );
@@ -2042,7 +2069,7 @@
 
 static bool Rx2FreqInRange( uint32_t freq )
 {
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
     if( Radio.CheckRfFrequency( freq ) == true )
 #elif defined( USE_BAND_470 ) || defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
     if( ( Radio.CheckRfFrequency( freq ) == true ) &&
@@ -2273,7 +2300,7 @@
             {
                 if( ( AdrAckCounter % ADR_ACK_DELAY ) == 0 )
                 {
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                     if( datarate > LORAMAC_TX_MIN_DATARATE )
                     {
                         datarate--;
@@ -2518,7 +2545,7 @@
                     {
                         nbRep = 1;
                     }
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
                     if( ( chMaskCntl == 0 ) && ( chMask == 0 ) )
                     {
                         status &= 0xFE; // Channel mask KO
@@ -2861,7 +2888,7 @@
         // Set the default datarate
         LoRaMacParams.ChannelsDatarate = LoRaMacParamsDefaults.ChannelsDatarate;
 
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
         // Re-enable default channels LC1, LC2, LC3
         LoRaMacParams.ChannelsMask[0] = LoRaMacParams.ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
 #endif
@@ -3112,7 +3139,16 @@
             // Compute Rx1 windows parameters
 #if ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
             RxWindowsParams[0] = ComputeRxWindowParameters( datarateOffsets[LoRaMacParams.ChannelsDatarate][LoRaMacParams.Rx1DrOffset], DEFAULT_SYSTEM_MAX_RX_ERROR );
-#else
+#elif defined( USE_BAND_868_LEGACY )
+            if( (LoRaMacParams.ChannelsDatarate == DR_7) || (LoRaMacParams.ChannelsDatarate == DR_6) )
+            {
+                RxWindowsParams[0] = ComputeRxWindowParameters( DR_6, DEFAULT_SYSTEM_MAX_RX_ERROR );
+            }
+            else
+            {
+                RxWindowsParams[0] = ComputeRxWindowParameters( DR_5, DEFAULT_SYSTEM_MAX_RX_ERROR );
+            }
+#else            
             RxWindowsParams[0] = ComputeRxWindowParameters( MAX( DR_0, LoRaMacParams.ChannelsDatarate - LoRaMacParams.Rx1DrOffset ), DEFAULT_SYSTEM_MAX_RX_ERROR );
 #endif
             // Compute Rx2 windows parameters
@@ -3255,6 +3291,26 @@
         Radio.SetTxConfig( MODEM_LORA, txPower, 0, 0, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
         TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
     }
+#elif defined( USE_BAND_868_LEGACY )
+    if( LoRaMacParams.ChannelsDatarate == DR_7 )
+    { // High Speed FSK channel
+        Radio.SetMaxPayloadLength( MODEM_FSK, LoRaMacBufferPktLen );
+        Radio.SetTxConfig( MODEM_FSK, txPower, 20e3, 0, 19.2 * 1e3, 0, 5, false, true, 0, 0, false, 3e3 );
+        TxTimeOnAir = Radio.TimeOnAir( MODEM_FSK, LoRaMacBufferPktLen );
+
+    }
+    else if( LoRaMacParams.ChannelsDatarate == DR_6 )
+    { // Low speed FSK channel
+        Radio.SetMaxPayloadLength( MODEM_FSK, LoRaMacBufferPktLen );
+        Radio.SetTxConfig( MODEM_FSK, txPower, 5e3, 0, 4.8 * 1e3, 0, 5, false, true, 0, 0, false, 3e3 );
+        TxTimeOnAir = Radio.TimeOnAir( MODEM_FSK, LoRaMacBufferPktLen );
+    }
+    else
+    { // Normal LoRa channel
+        Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
+        Radio.SetTxConfig( MODEM_LORA, txPower, 0, 0, datarate, 1, 8, false, true, 0, 0, false, 3e3 );
+        TxTimeOnAir = Radio.TimeOnAir( MODEM_LORA, LoRaMacBufferPktLen );
+    }
 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
     Radio.SetMaxPayloadLength( MODEM_LORA, LoRaMacBufferPktLen );
     if( LoRaMacParams.ChannelsDatarate >= DR_4 )
@@ -3352,7 +3408,7 @@
     DutyCycleOn = false;
 #elif defined( USE_BAND_780 )
     DutyCycleOn = true;
-#elif defined( USE_BAND_868 )
+#elif defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
     DutyCycleOn = true;
 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
     DutyCycleOn = false;
@@ -3387,7 +3443,7 @@
     LoRaMacParamsDefaults.ChannelsMask[5] = 0xFFFF;
 #elif defined( USE_BAND_780 )
     LoRaMacParamsDefaults.ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
-#elif defined( USE_BAND_868 )
+#elif defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
     LoRaMacParamsDefaults.ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
 #elif defined( USE_BAND_915 )
     LoRaMacParamsDefaults.ChannelsMask[0] = 0xFFFF;
@@ -3900,7 +3956,7 @@
         }
         case MIB_CHANNELS_DEFAULT_DATARATE:
         {
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
             if( ValueInRange( mibSet->Param.ChannelsDefaultDatarate,
                               DR_0, DR_5 ) )
             {
@@ -4008,7 +4064,7 @@
         datarateInvalid = true;
     }
 
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
     if( id < 3 )
     {
         if( params.Frequency != Channels[id].Frequency )
@@ -4030,7 +4086,7 @@
     // Validate the frequency
     if( ( Radio.CheckRfFrequency( params.Frequency ) == true ) && ( params.Frequency > 0 ) && ( frequencyInvalid == false ) )
     {
-#if defined( USE_BAND_868 )
+#if defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
         if( ( params.Frequency >= 863000000 ) && ( params.Frequency < 865000000 ) )
         {
             band = BAND_G1_2;
@@ -4090,7 +4146,7 @@
 
 LoRaMacStatus_t LoRaMacChannelRemove( uint8_t id )
 {
-#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
+#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 ) || defined( USE_BAND_868_LEGACY )
     if( ( LoRaMacState & LORAMAC_TX_RUNNING ) == LORAMAC_TX_RUNNING )
     {
         if( ( LoRaMacState & LORAMAC_TX_CONFIG ) != LORAMAC_TX_CONFIG )
@@ -4403,7 +4459,7 @@
 
 void LoRaMacTestSetDutyCycleOn( bool enable )
 {
-#if ( defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 ) )
+#if ( defined( USE_BAND_868 ) || defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868_LEGACY ) )
     DutyCycleOn = enable;
 #else
     DutyCycleOn = false;
@@ -4441,6 +4497,12 @@
         tSymbol = ( 1.0 / ( double )Datarates[datarate] ) * 8.0; // 1 symbol equals 1 byte
     }
     else
+#elif defined( USE_BAND_868_LEGACY )
+    if( (datarate == DR_7) || (datarate == DR_6) )
+    { // FSK
+        tSymbol = ( 1.0 / ( double )Datarates[datarate] ) * 8.0; // 1 symbol equals 1 byte
+    }
+    else
 #endif
     { // LoRa
         tSymbol = ( ( double )( 1 << Datarates[datarate] ) / ( double )Bandwidths[datarate] ) * 1e3;