@@ -1451,7 +1451,7 @@ static void LoRaMacProcessMacCommands( uint8_t *payload, uint8_t macIndex, uint8
14511451 // Data rate ACK = 0
14521452 // Channel mask = 0
14531453 AddMacCommand ( MOTE_MAC_LINK_ADR_ANS , 0 , 0 );
1454- macIndex += 3 ; // Skip over the remaining bytes of the request
1454+ macIndex += 3 ; // Skip over the remaining bytes of the request
14551455 break ;
14561456 }
14571457 chMask = ( uint16_t )payload [macIndex ++ ];
@@ -1936,24 +1936,21 @@ static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t
19361936 if ( sequenceCounterDiff < ( 1 << 15 ) )
19371937 {
19381938 downLinkCounter += sequenceCounterDiff ;
1939+ LoRaMacComputeMic ( payload , size - LORAMAC_MFR_LEN , nwkSKey , address , DOWN_LINK , downLinkCounter , & mic );
1940+ if ( micRx == mic )
1941+ {
1942+ isMicOk = true;
1943+ }
19391944 }
1940-
1941- // Normal operation
1942- if ( isMicOk == false )
1945+ else
19431946 {
1944- LoRaMacComputeMic ( payload , size - LORAMAC_MFR_LEN , nwkSKey , address , DOWN_LINK , downLinkCounter , & mic );
1947+ // check for downlink counter roll-over
1948+ uint32_t downLinkCounterTmp = downLinkCounter + 0x10000 + ( int16_t )sequenceCounterDiff ;
1949+ LoRaMacComputeMic ( payload , size - LORAMAC_MFR_LEN , nwkSKey , address , DOWN_LINK , downLinkCounterTmp , & mic );
19451950 if ( micRx == mic )
19461951 {
19471952 isMicOk = true;
1948- // Update 32 bits downlink counter
1949- if ( LoRaMacEventFlags .Bits .Multicast == 1 )
1950- {
1951- curMulticastParams -> DownLinkCounter = downLinkCounter ;
1952- }
1953- else
1954- {
1955- DownLinkCounter = downLinkCounter ;
1956- }
1953+ downLinkCounter = downLinkCounterTmp ;
19571954 }
19581955 }
19591956
@@ -1965,6 +1962,16 @@ static void OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t
19651962 LoRaMacEventInfo .RxBufferSize = 0 ;
19661963 AdrAckCounter = 0 ;
19671964
1965+ // Update 32 bits downlink counter
1966+ if ( LoRaMacEventFlags .Bits .Multicast == 1 )
1967+ {
1968+ curMulticastParams -> DownLinkCounter = downLinkCounter ;
1969+ }
1970+ else
1971+ {
1972+ DownLinkCounter = downLinkCounter ;
1973+ }
1974+
19681975 if ( macHdr .Bits .MType == FRAME_TYPE_DATA_CONFIRMED_DOWN )
19691976 {
19701977 SrvAckRequested = true;
@@ -2117,21 +2124,36 @@ static void OnRadioRxError( void )
21172124 */
21182125void LoRaMacRxWindowSetup ( uint32_t freq , int8_t datarate , uint32_t bandwidth , uint16_t timeout , bool rxContinuous )
21192126{
2127+ uint8_t downlinkDatarate = Datarates [datarate ];
2128+ RadioModems_t modem ;
2129+
21202130 if ( Radio .GetStatus ( ) == RF_IDLE )
21212131 {
21222132 Radio .SetChannel ( freq );
21232133#if defined( USE_BAND_433 ) || defined( USE_BAND_780 ) || defined( USE_BAND_868 )
21242134 if ( datarate == DR_7 )
21252135 {
2126- Radio .SetRxConfig ( MODEM_FSK , 50e3 , Datarates [datarate ] * 1e3 , 0 , 83.333e3 , 5 , 0 , false, 0 , true, 0 , 0 , false, rxContinuous );
2136+ modem = MODEM_FSK ;
2137+ Radio .SetRxConfig ( MODEM_FSK , 50e3 , downlinkDatarate * 1e3 , 0 , 83.333e3 , 5 , 0 , false, 0 , true, 0 , 0 , false, rxContinuous );
21272138 }
21282139 else
21292140 {
2130- Radio .SetRxConfig ( MODEM_LORA , bandwidth , Datarates [datarate ], 1 , 0 , 8 , timeout , false, 0 , false, 0 , 0 , true, rxContinuous );
2141+ modem = MODEM_LORA ;
2142+ Radio .SetRxConfig ( MODEM_LORA , bandwidth , downlinkDatarate , 1 , 0 , 8 , timeout , false, 0 , false, 0 , 0 , true, rxContinuous );
21312143 }
21322144#elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID )
2133- Radio .SetRxConfig ( MODEM_LORA , bandwidth , Datarates [datarate ], 1 , 0 , 8 , timeout , false, 0 , false, 0 , 0 , true, rxContinuous );
2145+ modem = MODEM_LORA ;
2146+ Radio .SetRxConfig ( MODEM_LORA , bandwidth , downlinkDatarate , 1 , 0 , 8 , timeout , false, 0 , false, 0 , 0 , true, rxContinuous );
21342147#endif
2148+ if ( RepeaterSupport == true )
2149+ {
2150+ Radio .SetMaxPayloadLength ( modem , MaxPayloadOfDatarateRepeater [datarate ] );
2151+ }
2152+ else
2153+ {
2154+ Radio .SetMaxPayloadLength ( modem , MaxPayloadOfDatarate [datarate ] );
2155+ }
2156+
21352157 if ( rxContinuous == false )
21362158 {
21372159 Radio .Rx ( MaxRxWindow );
@@ -2284,6 +2306,17 @@ static void OnMacStateCheckTimerEvent( void )
22842306 }
22852307 }
22862308 }
2309+ else
2310+ {
2311+ /*
2312+ * For confirmed uplinks, ignore MIC and address errors and keep retrying.
2313+ */
2314+ if ( ( LoRaMacEventInfo .Status == LORAMAC_EVENT_INFO_STATUS_MIC_FAIL ) ||
2315+ ( LoRaMacEventInfo .Status == LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL ) )
2316+ {
2317+ AckTimeoutRetry = true;
2318+ }
2319+ }
22872320
22882321 if ( LoRaMacEventFlags .Bits .Rx == 1 )
22892322 {
0 commit comments