Fork of Semtech LoRaWAN stack

Fork of LoRaWAN-lib by canuck lehead

Branch:
v4.2.0
Revision:
28:307e6c4831ad
Parent:
26:4627985dc5be
Child:
29:c7583fcfad8c
diff -r 4627985dc5be -r 307e6c4831ad LoRaMac.cpp
--- a/LoRaMac.cpp	Wed Nov 09 06:05:41 2016 +0000
+++ b/LoRaMac.cpp	Thu Nov 17 14:02:14 2016 +0000
@@ -1213,8 +1213,6 @@
 
                     McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK;
 
-                    AdrAckCounter = 0;
-
                     // Update 32 bits downlink counter
                     if( multicast == 1 )
                     {
@@ -1260,8 +1258,7 @@
                         }
                         DownLinkCounter = downLinkCounter;
                     }
-
-
+                    
                     if( ( ( size - 4 ) - appPayloadStartIndex ) > 0 )
                     {
                         port = payload[appPayloadStartIndex++];
@@ -1450,6 +1447,7 @@
 {
     TimerStop( &MacStateCheckTimer );
     bool txTimeout = false;
+    bool txDone = false;
 
     if( LoRaMacFlags.Bits.MacDone == 1 )
     {
@@ -1493,12 +1491,7 @@
                 {
                     ChannelsNbRepCounter = 0;
 
-                    AdrAckCounter++;
-                    if( IsUpLinkCounterFixed == false )
-                    {
-                        UpLinkCounter++;
-                    }
-
+                    txDone = true;
                     LoRaMacState &= ~MAC_TX_RUNNING;
                 }
                 else
@@ -1516,10 +1509,7 @@
             {
                 AckTimeoutRetry = false;
                 NodeAckRequested = false;
-                if( IsUpLinkCounterFixed == false )
-                {
-                    UpLinkCounter++;
-                }
+                txDone = true;
                 McpsConfirm.NbRetries = AckTimeoutRetriesCounter;
 
                 LoRaMacState &= ~MAC_TX_RUNNING;
@@ -1560,18 +1550,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 )
@@ -1585,7 +1589,7 @@
             LoRaMacPrimitives->MacMlmeConfirm( &MlmeConfirm );
             LoRaMacFlags.Bits.MlmeReq = 0;
         }
-
+        
         LoRaMacFlags.Bits.MacDone = 0;
     }
     else
@@ -2320,6 +2324,7 @@
                 		datarate = DR_4;
                 	else
                 		datarate--;
+                		
                 }
                 else if( isTx == true )
                 {
@@ -4066,6 +4071,7 @@
         {
             readyToSend = true;
             AckTimeoutRetries = 1;
+            AckTimeoutRetriesCounter = 1;
 
             macHdr.Bits.MType = FRAME_TYPE_DATA_UNCONFIRMED_UP;
             fPort = mcpsRequest->Req.Unconfirmed.fPort;