4141#include "LoRaMacHeaderTypes.h"
4242#include "LoRaMacMessageTypes.h"
4343#include "LoRaMacParser.h"
44- #include "LoRaMacFCntHandler.h"
4544#include "LoRaMacCommands.h"
4645#include "LoRaMacAdr.h"
4746
@@ -444,6 +443,22 @@ static void OnAckTimeoutTimerEvent( void* context );
444443 */
445444static void SetMlmeScheduleUplinkIndication ( void );
446445
446+ /*!
447+ * Computes next 32 bit downlink counter value and determines the frame counter ID.
448+ *
449+ * \param[IN] addrID - Address identifier
450+ * \param[IN] fType - Frame type
451+ * \param[IN] macMsg - Data message object, holding the current 16 bit transmitted frame counter
452+ * \param[IN] lrWanVersion - LoRaWAN version
453+ * \param[IN] maxFCntGap - Maximum allowed frame counter difference (only for 1.0.X necessary)
454+ * \param[OUT] fCntID - Frame counter identifier
455+ * \param[OUT] currentDown - Current downlink counter value
456+ *
457+ * \retval - Status of the operation
458+ */
459+ static LoRaMacCryptoStatus_t GetFCntDown ( AddressIdentifier_t addrID , FType_t fType , LoRaMacMessageData_t * macMsg , Version_t lrWanVersion ,
460+ uint16_t maxFCntGap , FCntIdentifier_t * fCntID , uint32_t * currentDown );
461+
447462/*!
448463 * \brief Switches the device class
449464 *
@@ -716,11 +731,6 @@ static void EventClassBNvmCtxChanged( void );
716731 */
717732static void EventConfirmQueueNvmCtxChanged ( void );
718733
719- /*!
720- * \brief FCnt Handler module nvm context has been changed
721- */
722- static void EventFCntHandlerNvmCtxChanged ( void );
723-
724734/*!
725735 * \brief Verifies if a request is pending currently
726736 *
@@ -942,7 +952,6 @@ static void ProcessRadioRxDone( void )
942952 uint32_t address = MacCtx .NvmCtx -> DevAddr ;
943953 uint8_t multicast = 0 ;
944954 AddressIdentifier_t addrID = UNICAST_DEV_ADDR ;
945- LoRaMacFCntHandlerStatus_t fCntHandlerStatus ;
946955 FCntIdentifier_t fCntID ;
947956
948957 MacCtx .McpsConfirm .AckReceived = false;
@@ -1124,10 +1133,10 @@ static void ProcessRadioRxDone( void )
11241133 phyParam = RegionGetPhyParam ( MacCtx .NvmCtx -> Region , & getPhy );
11251134
11261135 // Get downlink frame counter value
1127- fCntHandlerStatus = LoRaMacGetFCntDown ( addrID , fType , & macMsgData , MacCtx .NvmCtx -> Version , phyParam .Value , & fCntID , & downLinkCounter );
1128- if ( fCntHandlerStatus != LORAMAC_FCNT_HANDLER_SUCCESS )
1136+ macCryptoStatus = GetFCntDown ( addrID , fType , & macMsgData , MacCtx .NvmCtx -> Version , phyParam .Value , & fCntID , & downLinkCounter );
1137+ if ( macCryptoStatus != LORAMAC_CRYPTO_SUCCESS )
11291138 {
1130- if ( fCntHandlerStatus == LORAMAC_FCNT_HANDLER_CHECK_FAIL )
1139+ if ( macCryptoStatus == LORAMAC_CRYPTO_FAIL_FCNT_DUPLICATED )
11311140 {
11321141 // Catch the case of repeated downlink frame counter
11331142 MacCtx .McpsIndication .Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_REPEATED ;
@@ -1136,7 +1145,7 @@ static void ProcessRadioRxDone( void )
11361145 MacCtx .NvmCtx -> SrvAckRequested = true;
11371146 }
11381147 }
1139- else if ( fCntHandlerStatus == LORAMAC_FCNT_HANDLER_MAX_GAP_FAIL )
1148+ else if ( macCryptoStatus == LORAMAC_CRYPTO_FAIL_MAX_GAP_FCNT )
11401149 {
11411150 // Lost too many frames
11421151 MacCtx .McpsIndication .Status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS ;
@@ -1205,14 +1214,6 @@ static void ProcessRadioRxDone( void )
12051214 }
12061215 }
12071216
1208- // Update downlink counter in mac context / multicast context.
1209- if ( LORAMAC_FCNT_HANDLER_SUCCESS != LoRaMacSetFCntDown ( fCntID , downLinkCounter ) )
1210- {
1211- MacCtx .McpsIndication .Status = LORAMAC_EVENT_INFO_STATUS_ERROR ;
1212- PrepareRxDoneAbort ( );
1213- return ;
1214- }
1215-
12161217 RemoveMacCommands ( MacCtx .McpsIndication .RxSlot , macMsgData .FHDR .FCtrl , MacCtx .McpsConfirm .McpsRequest );
12171218
12181219 switch ( fType )
@@ -1752,6 +1753,54 @@ static void OnAckTimeoutTimerEvent( void* context )
17521753 }
17531754}
17541755
1756+ static LoRaMacCryptoStatus_t GetFCntDown ( AddressIdentifier_t addrID , FType_t fType , LoRaMacMessageData_t * macMsg , Version_t lrWanVersion ,
1757+ uint16_t maxFCntGap , FCntIdentifier_t * fCntID , uint32_t * currentDown )
1758+ {
1759+ if ( ( macMsg == NULL ) || ( fCntID == NULL ) ||
1760+ ( currentDown == NULL ) )
1761+ {
1762+ return LORAMAC_CRYPTO_ERROR_NPE ;
1763+ }
1764+
1765+ // Determine the frame counter identifier and choose counter from FCntList
1766+ switch ( addrID )
1767+ {
1768+ case UNICAST_DEV_ADDR :
1769+ if ( lrWanVersion .Fields .Minor == 1 )
1770+ {
1771+ if ( ( fType == FRAME_TYPE_A ) || ( fType == FRAME_TYPE_D ) )
1772+ {
1773+ * fCntID = A_FCNT_DOWN ;
1774+ }
1775+ else
1776+ {
1777+ * fCntID = N_FCNT_DOWN ;
1778+ }
1779+ }
1780+ else
1781+ { // For LoRaWAN 1.0.X
1782+ * fCntID = FCNT_DOWN ;
1783+ }
1784+ break ;
1785+ case MULTICAST_0_ADDR :
1786+ * fCntID = MC_FCNT_DOWN_0 ;
1787+ break ;
1788+ case MULTICAST_1_ADDR :
1789+ * fCntID = MC_FCNT_DOWN_1 ;
1790+ break ;
1791+ case MULTICAST_2_ADDR :
1792+ * fCntID = MC_FCNT_DOWN_2 ;
1793+ break ;
1794+ case MULTICAST_3_ADDR :
1795+ * fCntID = MC_FCNT_DOWN_3 ;
1796+ break ;
1797+ default :
1798+ return LORAMAC_CRYPTO_FAIL_FCNT_ID ;
1799+ }
1800+
1801+ return LoRaMacCryptoGetFCntDown ( * fCntID , maxFCntGap , macMsg -> FHDR .FCnt , currentDown );
1802+ }
1803+
17551804static LoRaMacStatus_t SwitchClass ( DeviceClass_t deviceClass )
17561805{
17571806 LoRaMacStatus_t status = LORAMAC_STATUS_PARAMETER_INVALID ;
@@ -2419,11 +2468,16 @@ static LoRaMacStatus_t SecureFrame( uint8_t txDr, uint8_t txCh )
24192468 break ;
24202469 case LORAMAC_MSG_TYPE_DATA :
24212470
2422- if ( LORAMAC_FCNT_HANDLER_SUCCESS != LoRaMacGetFCntUp ( & fCntUp ) )
2471+ if ( LORAMAC_CRYPTO_SUCCESS != LoRaMacCryptoGetFCntUp ( & fCntUp ) )
24232472 {
24242473 return LORAMAC_STATUS_FCNT_HANDLER_ERROR ;
24252474 }
24262475
2476+ if ( ( MacCtx .ChannelsNbTransCounter >= 1 ) || ( MacCtx .AckTimeoutRetriesCounter > 1 ) )
2477+ {
2478+ fCntUp -= 1 ;
2479+ }
2480+
24272481 macCryptoStatus = LoRaMacCryptoSecureMessage ( fCntUp , txDr , txCh , & MacCtx .TxMsg .Message .Data );
24282482 if ( LORAMAC_CRYPTO_SUCCESS != macCryptoStatus )
24292483 {
@@ -2497,9 +2551,6 @@ static void ResetMacParameters( void )
24972551 // ADR counter
24982552 MacCtx .NvmCtx -> AdrAckCounter = 0 ;
24992553
2500- // Initialize the uplink and downlink counters default value
2501- LoRaMacResetFCnts ( );
2502-
25032554 MacCtx .ChannelsNbTransCounter = 0 ;
25042555 MacCtx .AckTimeoutRetries = 1 ;
25052556 MacCtx .AckTimeoutRetriesCounter = 1 ;
@@ -2600,11 +2651,11 @@ LoRaMacStatus_t PrepareFrame( LoRaMacHeader_t* macHdr, LoRaMacFrameCtrl_t* fCtrl
26002651 MacCtx .TxMsg .Message .Data .FRMPayloadSize = MacCtx .AppDataSize ;
26012652 MacCtx .TxMsg .Message .Data .FRMPayload = MacCtx .AppData ;
26022653
2603- if ( LORAMAC_FCNT_HANDLER_SUCCESS != LoRaMacGetFCntUp ( & fCntUp ) )
2654+ if ( LORAMAC_CRYPTO_SUCCESS != LoRaMacCryptoGetFCntUp ( & fCntUp ) )
26042655 {
26052656 return LORAMAC_STATUS_FCNT_HANDLER_ERROR ;
26062657 }
2607- MacCtx .TxMsg .Message .Data .FHDR .FCnt = ( uint16_t ) fCntUp ;
2658+ MacCtx .TxMsg .Message .Data .FHDR .FCnt = ( uint16_t )fCntUp ;
26082659
26092660 // Reset confirm parameters
26102661 MacCtx .McpsConfirm .NbRetries = 0 ;
@@ -2781,7 +2832,6 @@ LoRaMacCtxs_t* GetCtxs( void )
27812832 Contexts .CommandsNvmCtx = LoRaMacCommandsGetNvmCtx ( & Contexts .CommandsNvmCtxSize );
27822833 Contexts .ClassBNvmCtx = LoRaMacClassBGetNvmCtx ( & Contexts .ClassBNvmCtxSize );
27832834 Contexts .ConfirmQueueNvmCtx = LoRaMacConfirmQueueGetNvmCtx ( & Contexts .ConfirmQueueNvmCtxSize );
2784- Contexts .FCntHandlerNvmCtx = LoRaMacFCntHandlerGetNvmCtx ( & Contexts .FCntHandlerNvmCtxSize );
27852835 return & Contexts ;
27862836}
27872837
@@ -2816,11 +2866,6 @@ LoRaMacStatus_t RestoreCtxs( LoRaMacCtxs_t* contexts )
28162866 return LORAMAC_STATUS_CRYPTO_ERROR ;
28172867 }
28182868
2819- if ( LoRaMacFCntHandlerRestoreNvmCtx ( contexts -> FCntHandlerNvmCtx ) != LORAMAC_FCNT_HANDLER_SUCCESS )
2820- {
2821- return LORAMAC_STATUS_FCNT_HANDLER_ERROR ;
2822- }
2823-
28242869 if ( LoRaMacCommandsRestoreNvmCtx ( contexts -> CommandsNvmCtx ) != LORAMAC_COMMANDS_SUCCESS )
28252870 {
28262871 return LORAMAC_STATUS_MAC_COMMAD_ERROR ;
@@ -2938,17 +2983,6 @@ static bool CheckRetransConfirmedUplink( void )
29382983
29392984static bool StopRetransmission ( void )
29402985{
2941- // Increase the current value by 1
2942- uint32_t fCntUp = 0 ;
2943- if ( LORAMAC_FCNT_HANDLER_SUCCESS != LoRaMacGetFCntUp ( & fCntUp ) )
2944- {
2945- return false;
2946- }
2947- if ( LORAMAC_FCNT_HANDLER_SUCCESS != LoRaMacSetFCntUp ( ( fCntUp ) ) )
2948- {
2949- return false;
2950- }
2951-
29522986 if ( MacCtx .MacFlags .Bits .McpsInd == 0 )
29532987 { // Maximum repetitions without downlink. Increase ADR Ack counter.
29542988 // Only process the case when the MAC did not receive a downlink.
@@ -3043,11 +3077,6 @@ static void EventConfirmQueueNvmCtxChanged( void )
30433077 CallNvmCtxCallback ( LORAMAC_NVMCTXMODULE_CONFIRM_QUEUE );
30443078}
30453079
3046- static void EventFCntHandlerNvmCtxChanged ( void )
3047- {
3048- CallNvmCtxCallback ( LORAMAC_NVMCTXMODULE_FCNT_HANDLER );
3049- }
3050-
30513080static uint8_t IsRequestPending ( void )
30523081{
30533082 if ( ( MacCtx .MacFlags .Bits .MlmeReq == 1 ) ||
@@ -3245,15 +3274,12 @@ LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t* primitives, LoRaMacC
32453274 return LORAMAC_STATUS_MAC_COMMAD_ERROR ;
32463275 }
32473276
3248- // Initialize FCnt Handler module
3249- if ( LoRaMacFCntHandlerInit ( EventFCntHandlerNvmCtxChanged ) != LORAMAC_FCNT_HANDLER_SUCCESS )
3277+ // Set multicast downlink counter reference
3278+ if ( LoRaMacCryptoSetMulticastReference ( MacCtx . NvmCtx -> MulticastChannelList ) != LORAMAC_CRYPTO_SUCCESS )
32503279 {
3251- return LORAMAC_STATUS_FCNT_HANDLER_ERROR ;
3280+ return LORAMAC_STATUS_CRYPTO_ERROR ;
32523281 }
32533282
3254- // Set multicast downlink counter reference
3255- LoRaMacFCntHandlerSetMulticastReference ( MacCtx .NvmCtx -> MulticastChannelList );
3256-
32573283 // Random seed initialization
32583284 srand1 ( Radio .Random ( ) );
32593285
0 commit comments