Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Branch:
v4.2.0
Revision:
29:c7583fcfad8c
Parent:
27:cda377b2a41f
Parent:
28:307e6c4831ad
Child:
30:0926a366a2c5
--- a/LoRaMac.cpp	Fri Nov 11 16:03:36 2016 -0500
+++ b/LoRaMac.cpp	Thu Nov 17 14:12:24 2016 +0000
@@ -1237,7 +1237,6 @@
 
                     McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
 
-                    AdrAckCounter = 0;
                     MacCommandsBufferToRepeatIndex = 0;
 
                     // Update 32 bits downlink counter
@@ -1285,8 +1284,7 @@
                         }
                         DownLinkCounter = downLinkCounter;
                     }
-
-
+                    
                     if( ( ( size - 4 ) - appPayloadStartIndex ) > 0 )
                     {
                         port = payload[appPayloadStartIndex++];
@@ -1475,6 +1473,7 @@
 {
     TimerStop( &MacStateCheckTimer );
     bool txTimeout = false;
+    bool txDone = false;
 
     if( LoRaMacFlags.Bits.MacDone == 1 )
     {
@@ -1518,12 +1517,7 @@
                 {
                     ChannelsNbRepCounter = 0;
 
-                    AdrAckCounter++;
-                    if( IsUpLinkCounterFixed == false )
-                    {
-                        UpLinkCounter++;
-                    }
-
+                    txDone = true;
                     LoRaMacState &= ~MAC_TX_RUNNING;
                 }
                 else
@@ -1541,10 +1535,7 @@
             {
                 AckTimeoutRetry = false;
                 NodeAckRequested = false;
-                if( IsUpLinkCounterFixed == false )
-                {
-                    UpLinkCounter++;
-                }
+                txDone = true;
                 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
 
                 LoRaMacState &= ~MAC_TX_RUNNING;
@@ -1585,18 +1576,32 @@
                 NodeAckRequested = false;
                 McpsConfirm.AckReceived = false;
                 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
-                if( IsUpLinkCounterFixed == false )
-                {
-                    UpLinkCounter++;
-                }
+                txDone = true;
             }
         }
+        
+        // Update uplink counter
+    	if( ( txDone == true ) && ( IsUpLinkCounterFixed == false ) )
+    	{	
+    		UpLinkCounter++;
+    		
+    		// Reset AdrAckCounter if downlink received
+    		if( ( LoRaMacFlags.Bits.McpsInd == 1 ) || ( UpLinkCounter == 1 ) )
+    		{
+    			AdrAckCounter = 0;
+    		}
+    		else
+    		{
+    			AdrAckCounter++;
+    		}
+    	}
     }
     // Handle reception for Class B and Class C
     if( ( LoRaMacState & MAC_RX ) == MAC_RX )
     {
         LoRaMacState &= ~MAC_RX;
     }
+    
     if( LoRaMacState == MAC_IDLE )
     {
         if( LoRaMacFlags.Bits.McpsReq == 1 )
@@ -1610,7 +1615,7 @@
             LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
             LoRaMacFlags.Bits.MlmeReq = 0;
         }
-
+        
         LoRaMacFlags.Bits.MacDone = 0;
     }
     else
@@ -2345,6 +2350,7 @@
                 		datarate = DR_4;
                 	else
                 		datarate--;
+                		
                 }
                 else if( isTx == true )
                 {
@@ -4157,6 +4163,7 @@
         {
             readyToSend = true;
             AckTimeoutRetries = 1;
+            AckTimeoutRetriesCounter = 1;
 
             macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED_UP;
             fPort = mcpsRequest->Req.Unconfirmed.fPort;