Fixed ADR issues with not applying new channel map

Dependents:   eleven-x-semtech-workshop-demo

Fork of LoRaWAN-lib by Semtech

Revision:
8:9a74243c53c0
Parent:
7:c16969e0f70f
Child:
9:2d057079554c
diff -r c16969e0f70f -r 9a74243c53c0 LoRaMac.cpp
--- a/LoRaMac.cpp	Tue Jul 05 13:24:54 2016 +0000
+++ b/LoRaMac.cpp	Fri Apr 21 15:54:15 2017 +0000
@@ -22,6 +22,7 @@
 #include "LoRaMacCrypto.h"
 #include "LoRaMac.h"
 #include "LoRaMacTest.h"
+#include "SerialDisplay.h"
 
 /*!
  * Maximum PHY layer payload size
@@ -1828,7 +1829,14 @@
                         delayTx++;
                         continue;
                     }
-                    enabledChannels[nbEnabledChannels++] = i + j;
+                    
+                    //check if channel enabled in LoRaMacParams.ChannelsMask
+                    //if(LoRaMacParams.ChannelsMask[i/16] & ( 1 << j )  != 0){
+                        enabledChannels[nbEnabledChannels++] = i + j;
+                    //}
+                    //else{//else disable channel
+                    //    DisableChannelInMask( i + j, ChannelsMaskRemaining );
+                    //}
                 }
             }
         }
@@ -1844,7 +1852,13 @@
         Channel = enabledChannels[randr( 0, nbEnabledChannels - 1 )];
 #if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
         if( Channel < ( LORA_MAX_NB_CHANNELS - 8 ) )
-        {
+        {   
+            //char maskStr[40];
+            //ChannelsMaskRemaining
+            //sprintf(maskStr, "%d %04X %04X %04X %04X %04X %04X\n", Channels[Channel].Frequency, LoRaMacParams.ChannelsMask[0], LoRaMacParams.ChannelsMask[1], LoRaMacParams.ChannelsMask[2], LoRaMacParams.ChannelsMask[3], LoRaMacParams.ChannelsMask[4], LoRaMacParams.ChannelsMask[5]);
+            //sprintf(maskStr, "%d %04X %04X %04X %04X %04X %04X\n", Channels[Channel].Frequency, ChannelsMaskRemaining[0], ChannelsMaskRemaining[1], ChannelsMaskRemaining[2], ChannelsMaskRemaining[3], ChannelsMaskRemaining[4], ChannelsMaskRemaining[5]);
+            //SerialDisplayWrite(maskStr);
+    
             DisableChannelInMask( Channel, ChannelsMaskRemaining );
         }
 #endif
@@ -2303,6 +2317,17 @@
 
 static void ProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8_t commandsSize, uint8_t snr )
 {
+/* Eleven-x Addition */
+/*
+#if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) 
+    //this variable is used as a flag for if the channel map changed.
+    //At the end of ProcessMacCommands, if the flag is true, then check if any ChannelsMaskRemaining channels are enabled that are now off in the LoRaMacParams.ChannelsMask
+    //The check can not be done when processing the command because if the first command turns off all 125kHz channels and a later command enables channels that were previously enabled the we have lost the list of channels used for channels that remained enabled.
+    //Have to wait for the channel map to be complete before disabling channels in ChannelsMaskRemaining
+    int newChMask = 0;
+#endif
+*/
+/* Eleven-x Addition End */
     while( macIndex < commandsSize )
     {
         // Decode Frame MAC commands
@@ -2324,6 +2349,14 @@
                     uint8_t chMaskCntl = 0;
                     uint16_t channelsMask[6] = { 0, 0, 0, 0, 0, 0 };
 
+/* Eleven-x Addition */
+/*
+#if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) 
+                    newChMask++;
+#endif
+*/
+/* Eleven-x Addition End*/
+                    
                     // Initialize local copy of the channels mask array
                     for( i = 0; i < 6; i++ )
                     {
@@ -2468,6 +2501,7 @@
 
                         LoRaMacParams.ChannelsNbRep = nbRep;
                     }
+                    
                     AddMacCommand( MOTE_MAC_LINK_ADR_ANS, status, 0 );
                 }
                 break;
@@ -2618,6 +2652,28 @@
                 return;
         }
     }
+   
+/* Eleven-x Addition */
+/*
+#if defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) 
+    if(newChMask){
+        //check that all ChannelsMaskRemaining are still enabled        
+        for( uint8_t i = 0, k = 0; i < LORA_MAX_NB_CHANNELS; i += 16, k++ )
+        {
+            for( uint8_t j = 0; j < 16; j++ )
+            {
+                if( ( ChannelsMaskRemaining[k] & ( 1 << j ) ) != 0 &&  ( LoRaMacParams.ChannelsMask[k] & ( 1 << j ) ) == 0 )
+                {
+                    //if enabled in ChannelsMaskRemaining but disabled in ChannelsMask
+                    //disable in ChannelsMaskRemaining
+                    DisableChannelInMask( i + j, ChannelsMaskRemaining );
+                }
+            }
+        }
+    }
+#endif
+*/
+/* Eleven-x Addition End*/
 }
 
 LoRaMacStatus_t Send( LoRaMacHeader_t *macHdr, uint8_t fPort, void *fBuffer, uint16_t fBufferSize )
@@ -3994,7 +4050,7 @@
         default:
             break;
     }
-
+    
     if( readyToSend == true )
     {
         if( AdrCtrlOn == false )
@@ -4008,7 +4064,7 @@
                 return LORAMAC_STATUS_PARAMETER_INVALID;
             }
         }
-
+        
         status = Send( &macHdr, fPort, fBuffer, fBufferSize );
         if( status == LORAMAC_STATUS_OK )
         {