Skip to content

Commit

Permalink
Moved Frame fields and Frame sizes definitions to a single place. (Lo…
Browse files Browse the repository at this point in the history
…RaMacHeaderTypes.h)

Updated name definitions.
  • Loading branch information
mluis1 committed Apr 16, 2020
1 parent dbdd03c commit 830a0f9
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 161 deletions.
13 changes: 0 additions & 13 deletions src/mac/LoRaMac.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,11 @@ extern "C"
*/
#define DOWN_LINK 1

/*!
* Sets the length of the LoRaMAC footer field.
* Mainly indicates the MIC field length
*/
#define LORAMAC_MFR_LEN 4

/*!
* LoRaMac MLME-Confirm queue length
*/
#define LORA_MAC_MLME_CONFIRM_QUEUE_LEN 5

/*!
* FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
* in RxWindowSetup function.
* Maximum PHYPayload = MaxPayloadOfDatarate + LORA_MAC_FRMPAYLOAD_OVERHEAD
*/
#define LORA_MAC_FRMPAYLOAD_OVERHEAD 13 // MHDR(1) + FHDR(7) + Port(1) + MIC(4)

/*!
* Maximum number of multicast context
*/
Expand Down
15 changes: 0 additions & 15 deletions src/mac/LoRaMacCrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@
*/
#define MIC_BLOCK_BX_SIZE 16

/*
* Size of JoinReqType is field for integrity check
*/
#define JOIN_REQ_TYPE_SIZE 1

/*
* Size of DevNonce is field for integrity check
*/
#define DEV_NONCE_SIZE 2

/*
* Number of security context entries
*/
Expand All @@ -91,11 +81,6 @@
*/
#define CRYPTO_BUFFER_SIZE CRYPTO_MAXMESSAGE_SIZE + MIC_BLOCK_BX_SIZE

/*
* MIC computation offset
*/
#define CRYPTO_MIC_COMPUTATION_OFFSET JOIN_REQ_TYPE_SIZE + LORAMAC_JOIN_EUI_FIELD_SIZE + DEV_NONCE_SIZE + LORAMAC_MHDR_FIELD_SIZE

/*!
* LoRaWAN Frame counter list.
*/
Expand Down
144 changes: 138 additions & 6 deletions src/mac/LoRaMacHeaderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,153 @@ extern "C"

#include <stdint.h>

/*! Frame header (FHDR) maximum field size */
#define LORAMAC_FHDR_MAX_FIELD_SIZE 22
/*! MAC header field size */
#define LORAMAC_MHDR_FIELD_SIZE 1

/*! ReJoinType field size */
#define LORAMAC_JOIN_TYPE_FIELD_SIZE 1

/*! Join EUI field size */
#define LORAMAC_JOIN_EUI_FIELD_SIZE 8

/*! Device EUI field size */
#define LORAMAC_DEV_EUI_FIELD_SIZE 8

/*! End-device nonce field size */
#define LORAMAC_DEV_NONCE_FIELD_SIZE 2

/*! Join-server nonce field size */
#define LORAMAC_JOIN_NONCE_FIELD_SIZE 3

/*! RJcount0 field size */
#define LORAMAC_RJCOUNT_0_FIELD_SIZE 2

/*! RJcount1 field size */
#define LORAMAC_RJCOUNT_1_FIELD_SIZE 2

/*! Network ID field size */
#define LORAMAC_NET_ID_FIELD_SIZE 3

/*! Device address field size */
#define LORAMAC_DEV_ADDR_FIELD_SIZE 4

/*! DLSettings field size */
#define LORAMAC_DL_SETTINGS_FIELD_SIZE 1

/*! RxDelay field size */
#define LORAMAC_RX_DELAY_FIELD_SIZE 1

/*! CFList field size */
#define LORAMAC_CF_LIST_FIELD_SIZE 16

/*! FHDR Device address field size */
#define LORAMAC_FHDR_DEV_ADD_FIELD_SIZE 4
#define LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE LORAMAC_DEV_ADDR_FIELD_SIZE

/*! FHDR Frame control field size */
#define LORAMAC_FHDR_F_CTRL_FIELD_SIZE 1
#define LORAMAC_FHDR_F_CTRL_FIELD_SIZE 1

/*! FHDR Frame control field size */
#define LORAMAC_FHDR_F_CNT_FIELD_SIZE 2
#define LORAMAC_FHDR_F_CNT_FIELD_SIZE 2

/*! FOpts maximum field size */
#define LORAMAC_FHDR_F_OPTS_MAX_FIELD_SIZE 15
#define LORAMAC_FHDR_F_OPTS_MAX_FIELD_SIZE 15

/*! Port field size */
#define LORAMAC_F_PORT_FIELD_SIZE 1

/*! Port field size */
#define LORAMAC_MAC_PAYLOAD_FIELD_MAX_SIZE 242

/*! MIC field size */
#define LORAMAC_MIC_FIELD_SIZE 4

/*!
* JoinRequest frame size
*
* MHDR(1) + JoinEUI(8) + DevEUI(8) + DevNonce(2) + MIC(4)
*/
#define LORAMAC_JOIN_REQ_MSG_SIZE ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_EUI_FIELD_SIZE + \
LORAMAC_DEV_EUI_FIELD_SIZE + LORAMAC_DEV_NONCE_FIELD_SIZE + \
LORAMAC_MIC_FIELD_SIZE )

/*!
* ReJoinRequest type 1 frame size
*
* MHDR(1) + ReJoinType(1) + JoinEUI(8) + DevEUI(8) + RJcount1(2) + MIC(4)
*/
#define LORAMAC_RE_JOIN_1_MSG_SIZE ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_TYPE_FIELD_SIZE + \
LORAMAC_JOIN_EUI_FIELD_SIZE + LORAMAC_DEV_EUI_FIELD_SIZE + \
LORAMAC_RJCOUNT_1_FIELD_SIZE + \
LORAMAC_MIC_FIELD_SIZE )

/*!
* ReJoinRequest type 0 or 2 frame size
*
* MHDR(1) + ReJoinType(1) + NetID(3) + DevEUI(8) + RJcount0(2) + MIC(4)
*/
#define LORAMAC_RE_JOIN_0_2_MSG_SIZE ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_TYPE_FIELD_SIZE + \
LORAMAC_NET_ID_FIELD_SIZE + LORAMAC_DEV_EUI_FIELD_SIZE + \
LORAMAC_RJCOUNT_0_FIELD_SIZE + \
LORAMAC_MIC_FIELD_SIZE )

/*!
* JoinAccept frame minimum size
*
* MHDR(1) + AppNonce(3) + NetID(3) + DevAddr(4) + DLSettings(1) + RxDelay(1) + MIC(4)
*/
#define LORAMAC_JOIN_ACCEPT_FRAME_MIN_SIZE ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_NONCE_FIELD_SIZE + \
LORAMAC_NET_ID_FIELD_SIZE + LORAMAC_DEV_ADDR_FIELD_SIZE + \
LORAMAC_DL_SETTINGS_FIELD_SIZE + LORAMAC_RX_DELAY_FIELD_SIZE + \
LORAMAC_MIC_FIELD_SIZE )

/*!
* JoinAccept frame maximum size
*
* MHDR(1) + AppNonce(3) + NetID(3) + DevAddr(4) + DLSettings(1) + RxDelay(1) + CFList(16) + MIC(4)
*/
#define LORAMAC_JOIN_ACCEPT_FRAME_MAX_SIZE ( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_NONCE_FIELD_SIZE + \
LORAMAC_NET_ID_FIELD_SIZE + LORAMAC_DEV_ADDR_FIELD_SIZE + \
LORAMAC_DL_SETTINGS_FIELD_SIZE + LORAMAC_RX_DELAY_FIELD_SIZE + \
LORAMAC_CF_LIST_FIELD_SIZE + LORAMAC_MIC_FIELD_SIZE )

/*!
* MIC computation offset
* \remark required for 1.1.x support
*/
#define JOIN_ACCEPT_MIC_COMPUTATION_OFFSET \
( LORAMAC_MHDR_FIELD_SIZE + LORAMAC_JOIN_TYPE_FIELD_SIZE + LORAMAC_JOIN_EUI_FIELD_SIZE + \
LORAMAC_DEV_NONCE_FIELD_SIZE )

/*!
* FRMPayload overhead to be used when setting the Radio.SetMaxPayloadLength
*
* Overhead to be used when setting the Radio.SetMaxPayloadLength in RxWindowSetup function.
*
* MHDR(1) + FHDR(7) + Port(1) + MIC(4)
*
* Maximum PHYPayload = MaxPayloadOfDatarate + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE
*/
#define LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE ( LORAMAC_MHDR_FIELD_SIZE + ( LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE + \
LORAMAC_FHDR_F_CTRL_FIELD_SIZE + LORAMAC_FHDR_F_CNT_FIELD_SIZE ) + \
LORAMAC_F_PORT_FIELD_SIZE + LORAMAC_MIC_FIELD_SIZE )

/*!
* FRMPayload minimum size
*
* MHDR(1) + FHDR(7) + MIC(4)
*/
#define LORAMAC_FRAME_PAYLOAD_MIN_SIZE ( LORAMAC_MHDR_FIELD_SIZE + ( LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE + \
LORAMAC_FHDR_F_CTRL_FIELD_SIZE + LORAMAC_FHDR_F_CNT_FIELD_SIZE ) + \
LORAMAC_MIC_FIELD_SIZE )
/*!
* FRMPayload maximum possible size
*
* MHDR(1) + FHDR(7) + Port(1) + MACPayload(242) + MIC(4)
*/
#define LORAMAC_FRAME_PAYLOAD_MAX_SIZE ( LORAMAC_MHDR_FIELD_SIZE + ( LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE + \
LORAMAC_FHDR_F_CTRL_FIELD_SIZE + LORAMAC_FHDR_F_CNT_FIELD_SIZE ) + \
LORAMAC_F_PORT_FIELD_SIZE + LORAMAC_MAC_PAYLOAD_FIELD_MAX_SIZE + \
LORAMAC_MIC_FIELD_SIZE )

/*!
* LoRaMAC field definition of DLSettings
Expand Down
34 changes: 0 additions & 34 deletions src/mac/LoRaMacMessageTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,6 @@ extern "C"
#include <stdint.h>
#include "LoRaMacHeaderTypes.h"


/*! MAC header field size */
#define LORAMAC_MHDR_FIELD_SIZE 1

/*! Join EUI field size */
#define LORAMAC_JOIN_EUI_FIELD_SIZE 8

/*! Device EUI field size */
#define LORAMAC_DEV_EUI_FIELD_SIZE 8

/*! Join-server nonce field size */
#define LORAMAC_JOIN_NONCE_FIELD_SIZE 3

/*! Network ID field size */
#define LORAMAC_NET_ID_FIELD_SIZE 3

/*! Port field size */
#define LORAMAC_F_PORT_FIELD_SIZE 1

/*! CFList field size */
#define LORAMAC_C_FLIST_FIELD_SIZE 16

/*! MIC field size */
#define LORAMAC_MIC_FIELD_SIZE 4

/*! Join-request message size */
#define LORAMAC_JOIN_REQ_MSG_SIZE 23

/*! ReJoin-request type 1 message size */
#define LORAMAC_RE_JOIN_1_MSG_SIZE 24

/*! ReJoin-request type 0 or 2 message size */
#define LORAMAC_RE_JOIN_0_2_MSG_SIZE 19

/*!
* LoRaMac type for Join-request message
*/
Expand Down
6 changes: 3 additions & 3 deletions src/mac/LoRaMacParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ LoRaMacParserStatus_t LoRaMacParserJoinAccept( LoRaMacMessageJoinAccept_t* macMs

macMsg->RxDelay = macMsg->Buffer[bufItr++];

if( ( macMsg->BufSize - LORAMAC_MIC_FIELD_SIZE - bufItr ) == LORAMAC_C_FLIST_FIELD_SIZE )
if( ( macMsg->BufSize - LORAMAC_MIC_FIELD_SIZE - bufItr ) == LORAMAC_CF_LIST_FIELD_SIZE )
{
memcpy1( macMsg->CFList, &macMsg->Buffer[bufItr], LORAMAC_C_FLIST_FIELD_SIZE );
bufItr = bufItr + LORAMAC_C_FLIST_FIELD_SIZE;
memcpy1( macMsg->CFList, &macMsg->Buffer[bufItr], LORAMAC_CF_LIST_FIELD_SIZE );
bufItr = bufItr + LORAMAC_CF_LIST_FIELD_SIZE;
}
else if( ( macMsg->BufSize - LORAMAC_MIC_FIELD_SIZE - bufItr ) > 0 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/mac/LoRaMacSerializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ LoRaMacSerializerStatus_t LoRaMacSerializerData( LoRaMacMessageData_t* macMsg )

// Check macMsg->BufSize
uint16_t computedBufSize = LORAMAC_MHDR_FIELD_SIZE
+ LORAMAC_FHDR_DEV_ADD_FIELD_SIZE
+ LORAMAC_FHDR_DEV_ADDR_FIELD_SIZE
+ LORAMAC_FHDR_F_CTRL_FIELD_SIZE
+ LORAMAC_FHDR_F_CNT_FIELD_SIZE;

Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionAS923.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ bool RegionAS923RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
}

Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateDwell0AS923[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateDwell0AS923[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionAU915.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ bool RegionAU915RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
// Radio configuration
Radio.SetRxConfig( MODEM_LORA, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );

Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateDwell0AU915[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateDwell0AU915[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionCN470.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ bool RegionCN470RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
// Radio configuration
Radio.SetRxConfig( MODEM_LORA, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );

Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateCN470[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateCN470[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionCN779.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ bool RegionCN779RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
}

Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateCN779[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateCN779[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
1 change: 1 addition & 0 deletions src/mac/region/RegionCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern "C"
#endif

#include "LoRaMacTypes.h"
#include "LoRaMacHeaderTypes.h"
#include "region/Region.h"

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionEU433.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ bool RegionEU433RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
}

Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateEU433[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateEU433[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionEU868.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ bool RegionEU868RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
}

Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateEU868[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateEU868[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionIN865.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ bool RegionIN865RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
}

Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateIN865[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateIN865[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionKR920.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ bool RegionKR920RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

// Radio configuration
Radio.SetRxConfig( MODEM_LORA, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateKR920[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateKR920[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionRU864.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ bool RegionRU864RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
Radio.SetRxConfig( modem, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );
}

Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateRU864[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( modem, MaxPayloadOfDatarateRU864[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mac/region/RegionUS915.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ bool RegionUS915RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )
// Radio configuration
Radio.SetRxConfig( MODEM_LORA, rxConfig->Bandwidth, phyDr, 1, 0, 8, rxConfig->WindowTimeout, false, 0, false, 0, 0, true, rxConfig->RxContinuous );

Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateUS915[dr] + LORA_MAC_FRMPAYLOAD_OVERHEAD );
Radio.SetMaxPayloadLength( MODEM_LORA, MaxPayloadOfDatarateUS915[dr] + LORAMAC_FRAME_PAYLOAD_OVERHEAD_SIZE );

*datarate = (uint8_t) dr;
return true;
Expand Down
Loading

0 comments on commit 830a0f9

Please sign in to comment.