Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Branch:
v4.2.0
Revision:
18:5ccaea55a881
Parent:
17:fca344a6ff18
Child:
19:838d3e8dffc6
--- a/LoRaMac.cpp	Wed Sep 14 02:53:13 2016 +0000
+++ b/LoRaMac.cpp	Thu Sep 15 15:39:41 2016 -0400
@@ -1837,7 +1837,7 @@
             {
                 Join500KHzRemaining &= ~(1 << curChannel);
                 channel = 64 + curChannel;
-                if( ( Join500KHzRemaining == 0 ) )
+                if( Join500KHzRemaining == 0 )
                 {
                     Join500KHzRemaining = 0xFF;
                 }
@@ -2418,11 +2418,11 @@
 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr )
 {
     uint8_t  validLinkAdrCmds = 0;
-    uint8_t  linkAdrStatus    = 0x07;
     int8_t   txPower          = 0;
     int8_t   datarate         = 0;
     uint8_t  nbRep            = 0;
     uint16_t channelsMask[6]  = {0,0,0,0,0,0}; 
+    uint8_t  LinkAdrMacCmdBufferIndex = 0;
 
     while( macIndex < commandsSize )
     {
@@ -2437,6 +2437,7 @@
             case SRV_MAC_LINK_ADR_REQ:
                 {
                     uint8_t  chMaskCntl = 0;
+                    uint8_t  status     = 0x07; 
                     uint16_t chMask;
 
                     datarate = payload[macIndex++];
@@ -2477,13 +2478,13 @@
 #if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
                     if( ( chMaskCntl == 0 ) && ( chMask == 0 ) )
                     {
-                        linkAdrStatus &= 0xFE; // Channel mask KO
+                        status &= 0xFE; // Channel mask KO
                     }
                     else if( ( ( chMaskCntl >= 1 ) && ( chMaskCntl <= 5 )) ||
                              ( chMaskCntl >= 7 ) )
                     {
                         // RFU
-                        linkAdrStatus &= 0xFE; // Channel mask KO
+                        status &= 0xFE; // Channel mask KO
                     }
                     else
                     {
@@ -2512,11 +2513,6 @@
                     {
                         channelsMask[chMaskCntl] = chMask;
                     }
-                    else if( chMaskCntl == 5 )
-                    {
-                        // RFU
-                        linkAdrStatus &= 0xFE; // Channel mask KO
-                    }
                     else if( chMaskCntl == 6 )
                     {
                         // Enable all 125 kHz channels
@@ -2542,14 +2538,20 @@
                         channelsMask[3] = 0x0000;
 
                         // channel mask applied to 500 kHz channels
-                        channelsMask[4] =  chMask; 
+                        channelsMask[4] = chMask;
+                        chMaskCntl = 4;
+                    }
+                    else 
+                    {
+                        // RFU
+                        status &= 0xFE; // Channel mask KO
                     }
 #else
     #error "Please define a frequency band in the compiler options."
 #endif
                     if( ValueInRange( datarate, LORAMAC_TX_MIN_DATARATE, LORAMAC_TX_MAX_DATARATE ) == false )
                     {
-                        linkAdrStatus &= 0xFD; // Datarate KO
+                        status &= 0xFD; // Datarate KO
                     }
 
                     //
@@ -2557,8 +2559,14 @@
                     //
                     if( ValueInRange( txPower, LORAMAC_MAX_TX_POWER, LORAMAC_MIN_TX_POWER ) == false )
                     {
-                        linkAdrStatus &= 0xFB; // TxPower KO
-                    }
+                        status &= 0xFB; // TxPower KO
+                    } 
+
+
+                    // Save the mac command status location for possible channel mask KO later
+                    LinkAdrMacCmdBufferIndex = MacCommandsBufferIndex;
+
+                    AddMacCommand( MOTE_MAC_LINK_ADR_ANS, status, 0 );
                 }
                 break;
             case SRV_MAC_DUTY_CYCLE_REQ:
@@ -2586,7 +2594,6 @@
                     {
                         status &= 0xFE; // Channel frequency KO
                     }
-                    
 
                     if( ValueInRange( datarate, LORAMAC_RX_MIN_DATARATE, LORAMAC_RX_MAX_DATARATE ) == false )
                     {
@@ -2714,6 +2721,7 @@
     if( validLinkAdrCmds > 0 ) 
     {
         uint8_t drOkCounter = 0;
+        uint8_t status      = 0x07;
 
 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
         for(uint8_t i=0; i< 6; i++)
@@ -2724,7 +2732,7 @@
                 {
                     if ( ( Channels[i * 16 + j].Frequency == 0 ) )
                     {
-                        linkAdrStatus &= 0xFE; // Channel mask KO
+                        status &= 0xFE; // Channel mask KO
                         break;
                     }
                     else if( ( datarate >= Channels[i * 16 + j].DrRange.Fields.Min ) &&
@@ -2738,23 +2746,23 @@
 
         if ( drOkCounter == 0 )
         {
-            linkAdrStatus &= 0xFD;  // Datarate KO
+            status &= 0xFD;  // Datarate KO
         }
 
         if( CountNbEnabled125kHzChannels( channelsMask ) < 6 )
         {
-            linkAdrStatus &= 0xFE; // Channel mask KO
+            status &= 0xFE; // Channel mask KO
         }
 
 #if defined( USE_BAND_915_HYBRID )
         if( ValidateChannelMask( channelsMask ) == false )
         {
-            linkAdrStatus &= 0xFE; // Channel mask KO
+            status &= 0xFE; // Channel mask KO
         }
 #endif
 
 #endif
-        if( ( linkAdrStatus & 0x07 ) == 0x07 )
+        if( ( status & 0x07 ) == 0x07 )
         {
             ChannelsDatarate = datarate;
             ChannelsTxPower = txPower;
@@ -2766,12 +2774,18 @@
             ChannelsMask[4] = channelsMask[4];
             ChannelsMask[5] = channelsMask[5];
 
-            ChannelsNbRep = nbRep;
-
+            ChannelsNbRep = nbRep; 
+            
+            // Clear remaining channels mask
             memcpy1( ( uint8_t* ) ChannelsMaskRemaining, ( uint8_t* ) ChannelsMask, 10 );
         }
-
-        AddMacCommand( MOTE_MAC_LINK_ADR_ANS, linkAdrStatus, 0 );
+        // Channel Mask KO
+        else
+        {
+            // Update the last Link ADR status with channel mask KO
+            if( (LinkAdrMacCmdBufferIndex+1 ) < LORA_MAC_COMMAND_MAX_LENGTH )
+                MacCommandsBuffer[LinkAdrMacCmdBufferIndex+1] &= status;
+        }
     }
 }
 
@@ -4104,8 +4118,8 @@
         return 0;
 
     // Normalize system time values to seconds
-    uptime = TimerGetCurrentTime( ) / 1e3;
-    lastJoinTime = LastJoinTxTime / 1e3;
+    uptime = TimerGetCurrentTime( ) / 1e6;
+    lastJoinTime = LastJoinTxTime / 1e6;
 
     // Get dutycycle for current time
     prevDCycleEndTime = 0;
@@ -4151,7 +4165,10 @@
     }
 
 
-    return ( timeOff * 1e6);
+    if(timeOff > JOIN_RETRANSMISSION_DC_WAIT_MAX)
+    	timeOff = JOIN_RETRANSMISSION_DC_WAIT_MAX;
+
+    return ( timeOff * 1e6 );
 }
 
 void LoRaMacTestRxWindowsOn( bool enable )