LoRaWAN end device MAC layer for SX1272 and SX1276. Supports LoRaWAN-1.0 and LoRaWAN-1.1

Dependencies:   sx12xx_hal

Dependents:   LoRaWAN-SanJose_Bootcamp LoRaWAN-grove-cayenne LoRaWAN-classC-demo LoRaWAN-grove-cayenne ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaMacPrivate.h Source File

LoRaMacPrivate.h

00001 #ifndef _LORAMACPRIVATE_H_
00002 #define _LORAMACPRIVATE_H_
00003 #include "LoRaMac1v1.h"
00004 
00005 typedef struct sBand
00006 {
00007     /*!
00008      * Duty cycle
00009      */
00010     uint16_t DCycle;
00011     /*!
00012      * Maximum Tx power
00013      */
00014     int8_t TxMaxPower;
00015 #ifdef DUTY_ENABLE
00016     /*!
00017      * Time stamp of the last Tx frame
00018      */
00019     us_timestamp_t LastTxDoneTime;
00020     /*!
00021      * Holds the time where the device is off
00022      */
00023     us_timestamp_t TimeOff;
00024 #endif /* DUTY_ENABLE */
00025 } Band_t;
00026 
00027 #ifdef USE_BAND_ARIB_8CH
00028     #include "region_arib8ch_private.h"
00029 #elif defined(USE_BAND_780)
00030     #include "region_cn780_private.h"
00031 #elif defined(USE_BAND_470)
00032     #include "region_cn470_private.h"
00033 #elif defined(USE_BAND_433)
00034     #include "region_433_private.h"
00035 #elif defined(USE_BAND_868)
00036     #include "region_eu868_private.h"
00037 #elif defined(USE_BAND_915_HYBRID) || defined(USE_BAND_915)
00038     #include "region_us915_private.h"
00039 #else
00040     #error define USE_BAND_*
00041 #endif
00042 
00043 #include "radio.h"
00044 
00045 #define RECEIVE_DELAY1_us                              1000000
00046 #define MAX_RX_WINDOW_us                               3000000
00047 
00048 #define LC( channelIndex )            ( uint16_t )( 1 << ( channelIndex - 1 ) )
00049 
00050 typedef struct {
00051     uint32_t uplink_in_progress  : 4; //0,1,2,3
00052     uint32_t uplink_mtype        : 3; //4,5,6
00053     uint32_t SrvAckRequested     : 1; //8
00054     uint32_t rxing               : 1; //9
00055     uint32_t MacCommandsInNextTx : 1; //10
00056     uint32_t AdrCtrlOn           : 1; //11
00057     uint32_t OptNeg              : 1; //12
00058     uint32_t PublicNetwork       : 1; //13
00059     uint32_t OnTxDelayed         : 1; //14
00060 
00061 #ifdef LORAWAN_JOIN_EUI
00062     uint32_t need_ReKeyConf      : 1; //15
00063     uint32_t IsLoRaMacNetworkJoined: 1; //16
00064 #else
00065     uint32_t have_SNwkSIntKey : 1; // 15
00066     uint32_t have_NwkSEncKey : 1; // 16
00067     uint32_t need_ResetConf: 1; // 17
00068 #endif /* !LORAWAN_JOIN_EUI  */
00069 
00070 } flags_t;
00071 
00072 extern flags_t flags;
00073 
00074 
00075 typedef enum eLoRaMacMoteCmd
00076 {
00077 #ifndef LORAWAN_JOIN_EUI
00078     MOTE_MAC_RESET_IND               = 0x01,
00079 #endif /* !LORAWAN_JOIN_EUI  */
00080     /*!
00081      * LinkCheckReq
00082      */
00083     MOTE_MAC_LINK_CHECK_REQ          = 0x02,
00084     /*!
00085      * LinkADRAns
00086      */
00087     MOTE_MAC_LINK_ADR_ANS            = 0x03,
00088     /*!
00089      * DutyCycleAns
00090      */
00091     MOTE_MAC_DUTY_CYCLE_ANS          = 0x04,
00092     /*!
00093      * RXParamSetupAns
00094      */
00095     MOTE_MAC_RX_PARAM_SETUP_ANS      = 0x05,
00096     /*!
00097      * DevStatusAns
00098      */
00099     MOTE_MAC_DEV_STATUS_ANS          = 0x06,
00100     /*!
00101      * NewChannelAns
00102      */
00103     MOTE_MAC_NEW_CHANNEL_ANS         = 0x07,
00104     /*!
00105      * RXTimingSetupAns
00106      */
00107     MOTE_MAC_RX_TIMING_SETUP_ANS     = 0x08,
00108 
00109     SRV_MAC_ADR_PARAM_SETUP_ANS      = 0x0c,
00110 #ifdef LORAWAN_JOIN_EUI
00111     MOTE_MAC_REKEY_IND               = 0x0b,
00112     MOTE_MAC_REJOIN_PARAM_ANS        = 0x0f,
00113 #endif /* LORAWAN_JOIN_EUI  */
00114     MOTE_MAC_DEVICE_TIME_REQ         = 0x0d,
00115     /*!
00116      * PingSlotInfoReq
00117      */
00118     MOTE_MAC_PING_SLOT_INFO_REQ      = 0x10,
00119     /*!
00120      * PingSlotFreqAns
00121      */
00122     MOTE_MAC_PING_SLOT_FREQ_ANS      = 0x11,
00123     /*!
00124      * BeaconTimingReq
00125      */
00126     MOTE_MAC_BEACON_TIMING_REQ       = 0x12,
00127     /*!
00128      * BeaconFreqAns
00129      */
00130     MOTE_MAC_BEACON_FREQ_ANS         = 0x13,
00131 
00132     MOTE_MAC_MODE_IND                = 0x20
00133 } LoRaMacMoteCmd_t;
00134 
00135 #define LORA_MAC_COMMAND_MAX_LENGTH                 15
00136 
00137 
00138 /*!
00139  * Global MAC layer parameters
00140  */
00141 typedef struct sLoRaMacParams 
00142 {
00143     /*!
00144      * Channels TX power
00145      */
00146     int8_t ChannelsTxPower ;
00147     /*!
00148      * Channels data rate
00149      */
00150     int8_t ChannelsDatarate ;
00151     /*!
00152      * LoRaMac maximum time a reception window stays open
00153      */
00154     uint32_t MaxRxWindow_us ;
00155     /*!
00156      * Receive delay 1
00157      */
00158     uint32_t ReceiveDelay1_us ;
00159     /*!
00160      * Receive delay 2
00161      */
00162     uint32_t ReceiveDelay2_us ;
00163 #ifdef LORAWAN_JOIN_EUI
00164     /*!
00165      * Join accept delay 1
00166      */
00167     uint32_t JoinAcceptDelay1_us ;
00168     /*!
00169      * Join accept delay 1
00170      */
00171     uint32_t JoinAcceptDelay2_us ;
00172 #endif /* LORAWAN_JOIN_EUI  */
00173     /*!
00174      * Number of uplink messages repetitions [1:15] (unconfirmed messages only)
00175      */
00176     uint8_t NbTrans ;
00177     /*!
00178      * Datarate offset between uplink and downlink on first window
00179      */
00180     uint8_t Rx1DrOffset ;
00181     /*!
00182      * LoRaMAC 2nd reception window settings
00183      */
00184     Rx2ChannelParams_t Rx2Channel ;
00185     /*!
00186      * Mask indicating which channels are enabled
00187      */
00188     uint16_t ChannelsMask [6];
00189     uint8_t NbEnabledChannels;
00190     us_timestamp_t MaxListenTime;
00191 } LoRaMacParams_t ;
00192 
00193 /*!
00194  * LoRaMAC header field definition (MHDR field)
00195  *
00196  * LoRaWAN Specification V1.0.1, chapter 4.2
00197  */
00198 typedef union uLoRaMacHeader 
00199 {
00200     /*!
00201      * Byte-access to the bits
00202      */
00203     uint8_t Value ;
00204     /*!
00205      * Structure containing single access to header bits
00206      */
00207     struct sHdrBits 
00208     {
00209         /*!
00210          * Major version
00211          */
00212         uint8_t Major            : 2;
00213         /*!
00214          * RFU
00215          */
00216         uint8_t RFU              : 3;
00217         /*!
00218          * Message type
00219          */
00220         uint8_t MType            : 3;
00221     }Bits;
00222 }LoRaMacHeader_t ;
00223 
00224 /*!
00225  * LoRaMAC frame types
00226  *
00227  * LoRaWAN Specification V1.0.1, chapter 4.2.1, table 1
00228  */
00229 typedef enum eLoRaMacFrameType
00230 {
00231 #ifdef LORAWAN_JOIN_EUI
00232     /*!
00233      * LoRaMAC join request frame
00234      */
00235     FRAME_TYPE_JOIN_REQ              = 0x00,
00236     /*!
00237      * LoRaMAC join accept frame
00238      */
00239     FRAME_TYPE_JOIN_ACCEPT           = 0x01,
00240 #endif /* LORAWAN_JOIN_EUI */
00241     /*!
00242      * LoRaMAC unconfirmed up-link frame
00243      */
00244     FRAME_TYPE_DATA_UNCONFIRMED_UP   = 0x02,
00245     /*!
00246      * LoRaMAC unconfirmed down-link frame
00247      */
00248     FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
00249     /*!
00250      * LoRaMAC confirmed up-link frame
00251      */
00252     FRAME_TYPE_DATA_CONFIRMED_UP     = 0x04,
00253     /*!
00254      * LoRaMAC confirmed down-link frame
00255      */
00256     FRAME_TYPE_DATA_CONFIRMED_DOWN   = 0x05,
00257 #ifdef LORAWAN_JOIN_EUI
00258     /*!
00259      * LoRaMAC rejoin frame
00260      */
00261     FRAME_TYPE_REJOIN_REQ            = 0x06,
00262 #endif /* LORAWAN_JOIN_EUI  */
00263     /*!
00264      * LoRaMAC proprietary frame
00265      */
00266     FRAME_TYPE_PROPRIETARY           = 0x07,
00267 } LoRaMacFrameType_t;
00268 
00269 /*!
00270  * LoRaMAC server MAC commands
00271  *
00272  * LoRaWAN Specification V1.0.1 chapter 5, table 4
00273  */
00274 typedef enum eLoRaMacSrvCmd
00275 {
00276 #ifndef LORAWAN_JOIN_EUI
00277     SRV_MAC_RESET_CONF               = 0x01,
00278 #endif /* !LORAWAN_JOIN_EUI  */
00279     /*!
00280      * LinkCheckAns
00281      */
00282     SRV_MAC_LINK_CHECK_ANS           = 0x02,
00283     /*!
00284      * LinkADRReq
00285      */
00286     SRV_MAC_LINK_ADR_REQ             = 0x03,
00287     /*!
00288      * DutyCycleReq
00289      */
00290     SRV_MAC_DUTY_CYCLE_REQ           = 0x04,
00291     /*!
00292      * RXParamSetupReq
00293      */
00294     SRV_MAC_RX_PARAM_SETUP_REQ       = 0x05,
00295     /*!
00296      * DevStatusReq
00297      */
00298     SRV_MAC_DEV_STATUS_REQ           = 0x06,
00299     /*!
00300      * NewChannelReq
00301      */
00302     SRV_MAC_NEW_CHANNEL_REQ          = 0x07,
00303     /*!
00304      * RXTimingSetupReq
00305      */
00306     SRV_MAC_RX_TIMING_SETUP_REQ      = 0x08,
00307 
00308     SRV_MAC_ADR_PARAM_SETUP_REQ      = 0x0c,
00309 #ifdef LORAWAN_JOIN_EUI
00310     SRV_MAC_REKEY_CONF               = 0x0b,
00311     SRV_MAC_FORCE_REJOIN_REQ         = 0x0e,
00312     SRV_MAC_REJOIN_PARAM_REQ         = 0x0f,
00313 #endif /* LORAWAN_JOIN_EUI  */
00314     SRV_MAC_DEVICE_TIME_ANS          = 0x0d,
00315     /*!
00316      * PingSlotInfoAns
00317      */
00318     SRV_MAC_PING_SLOT_INFO_ANS       = 0x10,
00319     /*!
00320      * PingSlotChannelReq
00321      */
00322     SRV_MAC_PING_SLOT_CHANNEL_REQ    = 0x11,
00323     /*!
00324      * BeaconTimingAns
00325      */
00326     SRV_MAC_BEACON_TIMING_ANS        = 0x12,
00327     /*!
00328      * BeaconFreqReq
00329      */
00330     SRV_MAC_BEACON_FREQ_REQ          = 0x13,
00331 
00332     SRV_MAC_MODE_CONF                = 0x20
00333 } LoRaMacSrvCmd_t;
00334 
00335 typedef union uLoRaMacFrameCtrl
00336 {
00337     /*!
00338      * Byte-access to the bits
00339      */
00340     uint8_t Value;
00341     /*!
00342      * Structure containing single access to bits
00343      */
00344     struct sCtrlBits 
00345     {
00346         /*!
00347          * Frame options length
00348          */
00349         uint8_t FOptsLen         : 4;
00350         /*!
00351          * Frame pending bit
00352          */
00353         uint8_t FPending         : 1;
00354         /*!
00355          * Message acknowledge bit
00356          */
00357         uint8_t Ack              : 1;
00358         /*!
00359          * ADR acknowledgment request bit
00360          */
00361         uint8_t AdrAckReq        : 1;
00362         /*!
00363          * ADR control in frame header
00364          */
00365         uint8_t Adr              : 1;
00366     }Bits;
00367 } LoRaMacFrameCtrl_t;
00368 
00369 typedef struct {
00370     uint16_t channelsMask[6];
00371     uint16_t chMask;
00372     uint8_t status;
00373     uint8_t chMaskCntl;
00374     int8_t datarate;
00375 } adr_t;
00376 
00377 /* from region: */
00378 extern const int8_t TxPowers[];
00379 extern const uint8_t MaxPayloadOfDatarate[];
00380 extern ChannelParams_t Channels[];
00381 extern const uint8_t Datarates[];
00382 LoRaMacStatus_t LoRaMacChannelRemove( uint8_t id );
00383 LoRaMacStatus_t LoRaMacChannelAdd( uint8_t id, ChannelParams_t params );
00384 
00385 //void region_adr_next_dr(int8_t* dr, bool);
00386 void region_ScheduleTx( void );
00387 uint32_t region_GetRxBandwidth( int8_t datarate );
00388 uint16_t region_GetRxSymbolTimeout( int8_t datarate );
00389 void region_mac_init(void);
00390 void region_rx1_setup(uint8_t channel);
00391 void region_adr_request(adr_t*);
00392 void region_tx_setup(int8_t dbm, uint8_t pkt_len);
00393 void region_session_start(LoRaMacEventInfoStatus_t);
00394 int8_t region_LimitTxPower( int8_t txPower );
00395 uint8_t region_CountNbEnabledChannels(void);
00396 #ifdef LORAWAN_JOIN_EUI
00397 int8_t region_AlternateDatarate( uint16_t nbTrials );
00398 #endif /* LORAWAN_JOIN_EUI  */
00399 
00400 /* from LoRaMac.cpp: */
00401 extern LoRaMacParams_t  LoRaMacParams;
00402 extern const LoRaMacParams_t  LoRaMacParamsDefaults;
00403 extern uint8_t Channel;
00404 void SendFrameOnChannel( uint8_t ch_num );
00405 void RxWindowSetup( unsigned freq, int8_t datarate, unsigned bandwidth, uint16_t timeout);
00406 uint8_t CountBits( uint16_t mask, uint8_t nbBits );
00407 LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 );
00408 void OnTxDelayedIsr(void);
00409 extern LowPowerTimeout TxDelayedEvent;
00410 extern DeviceClass_t LoRaMacDeviceClass;
00411 
00412 #endif /* _LORAMACPRIVATE_H_ */