Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Branch:
india_support
Revision:
43:eed39925bade
Parent:
31:930f949fdd9e
diff -r fc9baa35ae1a -r eed39925bade LoRaMac.cpp
--- a/LoRaMac.cpp	Tue Aug 08 11:21:18 2017 -0400
+++ b/LoRaMac.cpp	Mon Aug 21 16:09:09 2017 -0400
@@ -329,6 +329,65 @@
     LC2,
     LC3,
 };
+#elif defined( USE_BAND_INDIA_865 )
+/*!
+ * Data rates table definition
+ */
+const uint8_t Datarates[]  = { 12, 11, 10,  9,  8,  7,  7, 50 };
+
+/*!
+ * Bandwidths table definition in Hz
+ */
+const uint32_t Bandwidths[] = { 125000, 125000, 125000, 125000, 125000, 125000, 250000, 0 };
+
+/*!
+ * Up/Down link data rates offset definition
+ */
+const int8_t DatarateOffsets[8] =
+{
+      DR_0, // DR_0
+      DR_1, // DR_1
+      DR_2, // DR_2
+      DR_3, // DR_3
+      DR_4, // DR_4
+      DR_5, // DR_5
+      -1,   // DR_6
+      -2,   // DR_7
+};
+
+/*!
+ * Maximum payload with respect to the datarate index. Cannot operate with repeater.
+ */
+const uint8_t MaxPayloadOfDatarate[] = { 51, 51, 51, 115, 242, 242, 242, 242 };
+
+/*!
+ * Maximum payload with respect to the datarate index. Can operate with repeater.
+ */
+const uint8_t MaxPayloadOfDatarateRepeater[] = { 51, 51, 51, 115, 222, 222, 222, 222 };
+
+/*!
+ * Tx output powers table definition
+ */
+const int8_t TxPowers[]    = { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10 };
+
+/*!
+ * LoRaMac bands
+ */
+static Band_t Bands[LORA_MAX_NB_BANDS] =
+{
+    BAND0,
+};
+
+/*!
+ * LoRaMAC channels
+ */
+static ChannelParams_t Channels[LORA_MAX_NB_CHANNELS] =
+{
+    LC1,
+    LC2,
+    LC3,
+};
+
 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
 /*!
  * Data rates table definition
@@ -1565,7 +1624,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_INDIA_865)
                 // Re-enable default channels LC1, LC2, LC3
                 ChannelsMask[0] = ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
 #elif !defined( USE_BAND_915 ) && !defined( USE_BAND_915_HYBRID )
@@ -1654,7 +1713,7 @@
         Radio.Standby( );
     }
 
-#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_INDIA_865)
     datarate = ChannelsDatarate - Rx1DrOffset;
     if( datarate < 0 )
     {
@@ -1731,7 +1790,7 @@
     TimerStop( &RxWindowTimer2 );
     RxSlot = 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_INDIA_865)
     // For higher datarates, we increase the number of symbols generating a Rx Timeout
     if( ( Rx2Channel.Datarate == DR_3 ) || ( Rx2Channel.Datarate == DR_4 ) )
     { // DR_4, DR_3
@@ -1979,7 +2038,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_INDIA_865)
                     if( IsLoRaMacNetworkJoined == false )
                     {
                         if( ( JOIN_CHANNELS & ( 1 << j ) ) == 0 )
@@ -2095,7 +2154,7 @@
         // Store downlink datarate
         McpsIndication.RxDatarate = ( uint8_t ) 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_INDIA_865)
         if( datarate == DR_7 )
         {
             modem = MODEM_FSK;
@@ -2133,7 +2192,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_INDIA_865)
     if( Radio.CheckRfFrequency( freq ) == true )
 #elif ( defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) )
     if( ( Radio.CheckRfFrequency( freq ) == true ) &&
@@ -2316,7 +2375,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_INDIA_865)
                 if( datarate > LORAMAC_TX_MIN_DATARATE )
                 {
                 	datarate--;
@@ -2496,8 +2555,8 @@
     int8_t   txPower          = 0;
     int8_t   datarate         = 0;
     uint8_t  nbRep            = 0;
+    uint8_t  LinkAdrMacCmdBufferIndex = 0;
     uint16_t channelsMask[6]  = {0,0,0,0,0,0}; 
-    uint8_t  LinkAdrMacCmdBufferIndex = 0;
 
     while( macIndex < commandsSize )
     {
@@ -2537,9 +2596,9 @@
                         {
                             channelsMask[i] = ChannelsMask[i];
                         }
+
                     }
 
-
                     chMask = ( uint16_t )payload[macIndex++];
                     chMask |= ( uint16_t )payload[macIndex++] << 8;
 
@@ -2550,7 +2609,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_INDIA_865)
                     if( ( chMaskCntl == 0 ) && ( chMask == 0 ) )
                     {
                         status &= 0xFE; // Channel mask KO
@@ -2563,7 +2622,7 @@
                     }
                     else
                     {
-                        for( i = 0; i < LORA_MAX_NB_CHANNELS; i++ )
+                        for( uint8_t i = 0; i < LORA_MAX_NB_CHANNELS; i++ )
                         {
                             if( chMaskCntl == 6 )
                             {
@@ -2794,10 +2853,11 @@
     // Process accumulated Link ADR channel mask changes
     if( validLinkAdrCmds > 0 ) 
     {
-        uint8_t drOkCounter = 0;
-        uint8_t status      = 0x07;
+        uint8_t status = 0x07;
 
 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
+
+        uint8_t drOkCounter = 0;
         for(uint8_t i=0; i< 6; i++)
         {
             for( uint8_t j = 0; j < 16; j++ )
@@ -2836,12 +2896,14 @@
 #endif
 
 #endif
+
         if( ( status & 0x07 ) == 0x07 )
         {
             ChannelsDatarate = datarate;
 			ChannelsDefaultDatarate = datarate;
             ChannelsTxPower = txPower;
 
+#if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
             ChannelsMask[0] = channelsMask[0];
             ChannelsMask[1] = channelsMask[1];
             ChannelsMask[2] = channelsMask[2];
@@ -2849,10 +2911,11 @@
             ChannelsMask[4] = channelsMask[4];
             ChannelsMask[5] = channelsMask[5];
 
-            ChannelsNbRep = nbRep; 
             
             // Clear remaining channels mask
             memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) ChannelsMask, 10 );
+#endif
+            ChannelsNbRep = nbRep;
         }
         // Channel Mask KO
         else
@@ -2917,7 +2980,7 @@
         // Set the default datarate
         ChannelsDatarate = ChannelsDefaultDatarate;
 
-#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_INDIA_865)
         // Re-enable default channels LC1, LC2, LC3
         ChannelsMask[0] = ChannelsMask[0] | ( LC( 1 ) + LC( 2 ) + LC( 3 ) );
 #endif
@@ -3137,7 +3200,7 @@
 
     Radio.SetChannel( channel.Frequency );
     
-#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_INDIA_865)
     if( ChannelsDatarate == DR_7 )
     { // High Speed FSK channel
         Radio.SetMaxPayloadLength( MODEM_FSK, LoRaMacBufferPktLen );
@@ -3244,6 +3307,8 @@
     ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
 #elif defined( USE_BAND_868 )
     ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
+#elif defined( USE_BAND_INDIA_865 )
+    ChannelsMask[0] = LC( 1 ) + LC( 2 ) + LC( 3 );
 #elif defined( USE_BAND_915 )
     ChannelsMask[0] = 0xFFFF;
     ChannelsMask[1] = 0xFFFF;
@@ -3288,9 +3353,11 @@
     ChannelsNbRep = 1;
     ChannelsNbRepCounter = 0;
 
+#if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
 	Rx2Channel.Frequency = 923300000;
     Rx2Channel.Datarate = DR_8;
     Rx1DrOffset = 0;
+#endif
 
 
     MaxDCycle = 0;
@@ -3304,6 +3371,8 @@
     DutyCycleOn = false;
 #elif defined( USE_BAND_868 )
     DutyCycleOn = true;
+#elif defined( USE_BAND_INDIA_865 )
+    DutyCycleOn = false;
 #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
     DutyCycleOn = false;
 #else
@@ -3828,7 +3897,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_INDIA_865 )
     if( id < 3 )
     {
         if( params.Frequency != Channels[id].Frequency )
@@ -3906,7 +3975,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_INDIA_865)
     if( ( LoRaMacState & MAC_TX_RUNNING ) == MAC_TX_RUNNING )
     {
         if( ( LoRaMacState & MAC_TX_CONFIG ) != MAC_TX_CONFIG )
@@ -4225,7 +4294,7 @@
     TimerTime_t period;
     TimerTime_t onAirTimeMax;
     TimerTime_t lastJoinTime;
-    TimerTime_t elapsedTime;
+    TimerTime_t elapsedTime = 0;
     uint8_t     dcyclePeriod;
 
     // Check retransmit duty-cycle is enabled