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

radio chip selection

Radio chip driver is not included, because two options are available.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

application project requirements

This library requires mbed TLS to be enabled.
The file mbed_app.json must be present in the project using this library:

{
    "macros": [ "MBEDTLS_CMAC_C" ]
}

regional PHY selection

All end device configuration is done in Commissioning.h, define desired radio frequency band of operation in this header file.
Commissioning.h is located in the application using this library.

end device provisioning

End device is provisioned by editing Commissioning.h in the application which is using this library
To use LoRaWAN-1.0 OTA: make sure LORAWAN_ROOT_APPKEY is undefined.
To use LoRaWAN-1.1 OTA, define LORAWAN_ROOT_APPKEY.
To select OTA operation, define LORAWAN_JOIN_EUI, then LORAWAN_DEVICE_EUI must be defined, along with root key(s).
To select ABP operation, undefine LORAWAN_JOIN_EUI: then define session keys

LoRaWAN 1.0 nameLoRaWAN 1.1 nameComissioning.h defnedescription
OTADevEUIDevEUILORAWAN_DEVICE_EUIuniquely identifies end device
OTAAppEUIJoinEUILORAWAN_JOIN_EUI
OTAAppKeyNwkKeyLORAWAN_ROOT_NWKKEYroot key for network server
OTA(note 1)AppKeyLORAWAN_ROOT_APPKEYroot key for application server
ABPNwkSKey(note 3)LORAWAN_FNwkSIntKeynetwork session key
ABP(note 2)SNwkSIntKeyLORAWAN_SNwkSIntKeymac layer network integrity key
ABP(note 2)NwkSEncKeyLORAWAN_NwkSEncKeynetwork session encryption key
ABP(note 2)FNwkSIntKeyLORAWAN_FNwkSIntKeyforwarding network session integrity key
ABPAppSKeyAppSKeyLORAWAN_APPSKEYapplication session encryption key

(note 1): LoRaWAN-1.0 OTA uses a single root key for both network server and application server.

In LoRaWAN-1.0 OTA: the single root AppKey is used to generate NwkSkey and AppSKey.
(note 2): In LoRaWAN-1.0 (both OTA and ABP) SNwkSIntKey, NwkSEncKey. FNwkSIntKey are of same value and are collectively known as NwkSKey.
(note 3): LoRaWAN-1.0 uses single network session key, LoRaWAN-1.1 uses 3 network session keys. Both use a unique application session key.


In LoRaWAN-1.1 OTA: the root NwkKey is used to generate SNwkSIntKey, NwkSEncKey, FNwkSIntKey
In LoRaWAN-1.1 OTA: the root AppKey is used to generate AppSKey


in ABP mode, the DevAddr, and session keys are fixed (never change), and frame counters never reset to zero.
ABP operation has no concept of: root keys, or DevEUI or JoinEUI/AppEUI.
in OTA mode, the DevAddr and session keys are assigned at join procedure, and frame counters reset at join.

eeprom

This library includes eeprom driver to support non-volatile storage required by LoRaWAN specification.
Currently eeprom is implemented for STM32L1 family and STM32L0 family.
Writing of values are wear-leveled to increase endurance; each write operation circulates across several memory locations. A read operation returns the highest value found. This simple method is used for sequence numbers which only increase.

value nameused in
DevNonceOTAfor Join request (note 1)
RJcount1OTAfor ReJoin Type 1 request
FCntUpABPuplink frame counter
NFCntDownABPdownlink frame counter
AFCntDownABPdownlink frame counter

AFCntDown is only used in LoRaWAN-1.1 when application payload is present in downlink and FPort > 0.
NFCntDown is used in LoRaWAN-1.1 when FPort is zero in downlink or application payload not present.
NFCntDown is the only downlink frame counter used in LoRaWAN-1.0
(note 1) OTA DevNonce is random number in LoRaWAN-1.0, therefore not stored in eeprom. DevNonce in LoRaWAN-1.1 is forever increasing (non-volatile) number upon each join request,.
RJcount0 is only stored in RAM because the value resets upon new session from JoinAccept, therefore not stored in eeprom.
Frame counters in OTA mode reset upon new session in join request, therefore are stored in RAM instead of eeprom for OTA.

radio driver support

When SX127x driver is used, both SX1272 and SX1276 are supported without defining at compile time. The chip is detected at start-up.
Supported radio platforms:


Alternately, when SX126x driver is imported, the SX126xDVK1xAS board is used.

low-speed clock oscillator selection

LoRaWAN uses 32768Hz crystal to permit low-power operation.
However, some mbed targets might revert to low-speed internal oscillator, which is not accurate enough for LoRaWAN operation.
An oscillator check is performed at initialization; program will not start if internal oscillator is used.
To force LSE watch crystal, add to mbed_app.json

{
    "macros": [ "MBEDTLS_CMAC_C" ],
    "target_overrides": {
        "<your-target>": {
            "target.lse_available": true
        }
    }
}
Committer:
Wayne Roberts
Date:
Mon Mar 05 16:49:15 2018 -0800
Revision:
3:eb174e10afbb
Parent:
0:6b3ac9c5a042
Child:
4:e4bfe9183f94
correct ADR_ACK_CNT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wayne Roberts 0:6b3ac9c5a042 1 #ifndef _LORAMACPRIVATE_H_
Wayne Roberts 0:6b3ac9c5a042 2 #define _LORAMACPRIVATE_H_
Wayne Roberts 0:6b3ac9c5a042 3 #include "LoRaMac.h"
Wayne Roberts 0:6b3ac9c5a042 4
Wayne Roberts 0:6b3ac9c5a042 5 typedef struct sBand
Wayne Roberts 0:6b3ac9c5a042 6 {
Wayne Roberts 0:6b3ac9c5a042 7 /*!
Wayne Roberts 0:6b3ac9c5a042 8 * Duty cycle
Wayne Roberts 0:6b3ac9c5a042 9 */
Wayne Roberts 0:6b3ac9c5a042 10 uint16_t DCycle;
Wayne Roberts 0:6b3ac9c5a042 11 /*!
Wayne Roberts 0:6b3ac9c5a042 12 * Maximum Tx power
Wayne Roberts 0:6b3ac9c5a042 13 */
Wayne Roberts 0:6b3ac9c5a042 14 int8_t TxMaxPower;
Wayne Roberts 0:6b3ac9c5a042 15 #ifdef DUTY_ENABLE
Wayne Roberts 0:6b3ac9c5a042 16 /*!
Wayne Roberts 0:6b3ac9c5a042 17 * Time stamp of the last Tx frame
Wayne Roberts 0:6b3ac9c5a042 18 */
Wayne Roberts 0:6b3ac9c5a042 19 us_timestamp_t LastTxDoneTime;
Wayne Roberts 0:6b3ac9c5a042 20 /*!
Wayne Roberts 0:6b3ac9c5a042 21 * Holds the time where the device is off
Wayne Roberts 0:6b3ac9c5a042 22 */
Wayne Roberts 0:6b3ac9c5a042 23 us_timestamp_t TimeOff;
Wayne Roberts 0:6b3ac9c5a042 24 #endif /* DUTY_ENABLE */
Wayne Roberts 0:6b3ac9c5a042 25 } Band_t;
Wayne Roberts 0:6b3ac9c5a042 26
Wayne Roberts 0:6b3ac9c5a042 27 #ifdef USE_BAND_ARIB_8CH
Wayne Roberts 0:6b3ac9c5a042 28 #include "region_arib8ch_private.h"
Wayne Roberts 0:6b3ac9c5a042 29 #elif defined(USE_BAND_780)
Wayne Roberts 0:6b3ac9c5a042 30 #include "region_cn780_private.h"
Wayne Roberts 0:6b3ac9c5a042 31 #elif defined(USE_BAND_470)
Wayne Roberts 0:6b3ac9c5a042 32 #include "region_cn470_private.h"
Wayne Roberts 0:6b3ac9c5a042 33 #elif defined(USE_BAND_433)
Wayne Roberts 0:6b3ac9c5a042 34 #include "region_433_private.h"
Wayne Roberts 0:6b3ac9c5a042 35 #elif defined(USE_BAND_868)
Wayne Roberts 0:6b3ac9c5a042 36 #include "region_eu868_private.h"
Wayne Roberts 0:6b3ac9c5a042 37 #elif defined(USE_BAND_915_HYBRID) || defined(USE_BAND_915)
Wayne Roberts 0:6b3ac9c5a042 38 #include "region_us915_private.h"
Wayne Roberts 0:6b3ac9c5a042 39 #else
Wayne Roberts 0:6b3ac9c5a042 40 #error define USE_BAND_*
Wayne Roberts 0:6b3ac9c5a042 41 #endif
Wayne Roberts 0:6b3ac9c5a042 42
Wayne Roberts 0:6b3ac9c5a042 43 #include "radio.h"
Wayne Roberts 0:6b3ac9c5a042 44
Wayne Roberts 0:6b3ac9c5a042 45 #define RECEIVE_DELAY1_us 1000000
Wayne Roberts 0:6b3ac9c5a042 46 #define MAX_RX_WINDOW_us 3000000
Wayne Roberts 0:6b3ac9c5a042 47
Wayne Roberts 0:6b3ac9c5a042 48 #define LC( channelIndex ) ( uint16_t )( 1 << ( channelIndex - 1 ) )
Wayne Roberts 0:6b3ac9c5a042 49
Wayne Roberts 0:6b3ac9c5a042 50 typedef struct {
Wayne Roberts 0:6b3ac9c5a042 51 uint32_t uplink_in_progress : 4; //0,1,2,3
Wayne Roberts 0:6b3ac9c5a042 52 uint32_t uplink_mtype : 3; //4,5,6
Wayne Roberts 0:6b3ac9c5a042 53 uint32_t SrvAckRequested : 1; //8
Wayne Roberts 0:6b3ac9c5a042 54 uint32_t rxing : 1; //9
Wayne Roberts 0:6b3ac9c5a042 55 uint32_t MacCommandsInNextTx : 1; //10
Wayne Roberts 0:6b3ac9c5a042 56 uint32_t AdrCtrlOn : 1; //11
Wayne Roberts 0:6b3ac9c5a042 57 uint32_t OptNeg : 1; //12
Wayne Roberts 0:6b3ac9c5a042 58 uint32_t PublicNetwork : 1; //13
Wayne Roberts 0:6b3ac9c5a042 59
Wayne Roberts 0:6b3ac9c5a042 60 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 61 uint32_t need_ReKeyConf : 1; //14
Wayne Roberts 0:6b3ac9c5a042 62 uint32_t IsLoRaMacNetworkJoined: 1; //15
Wayne Roberts 0:6b3ac9c5a042 63 #else
Wayne Roberts 0:6b3ac9c5a042 64 uint32_t have_SNwkSIntKey : 1; // 14
Wayne Roberts 0:6b3ac9c5a042 65 uint32_t have_NwkSEncKey : 1; // 15
Wayne Roberts 0:6b3ac9c5a042 66 uint32_t need_ResetConf: 1; // 16
Wayne Roberts 0:6b3ac9c5a042 67 #endif /* !LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 68
Wayne Roberts 0:6b3ac9c5a042 69 } flags_t;
Wayne Roberts 0:6b3ac9c5a042 70
Wayne Roberts 0:6b3ac9c5a042 71 extern flags_t flags;
Wayne Roberts 0:6b3ac9c5a042 72
Wayne Roberts 0:6b3ac9c5a042 73
Wayne Roberts 0:6b3ac9c5a042 74 typedef enum eLoRaMacMoteCmd
Wayne Roberts 0:6b3ac9c5a042 75 {
Wayne Roberts 0:6b3ac9c5a042 76 #ifndef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 77 MOTE_MAC_RESET_IND = 0x01,
Wayne Roberts 0:6b3ac9c5a042 78 #endif /* !LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 79 /*!
Wayne Roberts 0:6b3ac9c5a042 80 * LinkCheckReq
Wayne Roberts 0:6b3ac9c5a042 81 */
Wayne Roberts 0:6b3ac9c5a042 82 MOTE_MAC_LINK_CHECK_REQ = 0x02,
Wayne Roberts 0:6b3ac9c5a042 83 /*!
Wayne Roberts 0:6b3ac9c5a042 84 * LinkADRAns
Wayne Roberts 0:6b3ac9c5a042 85 */
Wayne Roberts 0:6b3ac9c5a042 86 MOTE_MAC_LINK_ADR_ANS = 0x03,
Wayne Roberts 0:6b3ac9c5a042 87 /*!
Wayne Roberts 0:6b3ac9c5a042 88 * DutyCycleAns
Wayne Roberts 0:6b3ac9c5a042 89 */
Wayne Roberts 0:6b3ac9c5a042 90 MOTE_MAC_DUTY_CYCLE_ANS = 0x04,
Wayne Roberts 0:6b3ac9c5a042 91 /*!
Wayne Roberts 0:6b3ac9c5a042 92 * RXParamSetupAns
Wayne Roberts 0:6b3ac9c5a042 93 */
Wayne Roberts 0:6b3ac9c5a042 94 MOTE_MAC_RX_PARAM_SETUP_ANS = 0x05,
Wayne Roberts 0:6b3ac9c5a042 95 /*!
Wayne Roberts 0:6b3ac9c5a042 96 * DevStatusAns
Wayne Roberts 0:6b3ac9c5a042 97 */
Wayne Roberts 0:6b3ac9c5a042 98 MOTE_MAC_DEV_STATUS_ANS = 0x06,
Wayne Roberts 0:6b3ac9c5a042 99 /*!
Wayne Roberts 0:6b3ac9c5a042 100 * NewChannelAns
Wayne Roberts 0:6b3ac9c5a042 101 */
Wayne Roberts 0:6b3ac9c5a042 102 MOTE_MAC_NEW_CHANNEL_ANS = 0x07,
Wayne Roberts 0:6b3ac9c5a042 103 /*!
Wayne Roberts 0:6b3ac9c5a042 104 * RXTimingSetupAns
Wayne Roberts 0:6b3ac9c5a042 105 */
Wayne Roberts 0:6b3ac9c5a042 106 MOTE_MAC_RX_TIMING_SETUP_ANS = 0x08,
Wayne Roberts 0:6b3ac9c5a042 107
Wayne Roberts 3:eb174e10afbb 108 SRV_MAC_ADR_PARAM_SETUP_ANS = 0x0c,
Wayne Roberts 0:6b3ac9c5a042 109 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 110 MOTE_MAC_REKEY_IND = 0x0b,
Wayne Roberts 0:6b3ac9c5a042 111 MOTE_MAC_REJOIN_PARAM_ANS = 0x0f,
Wayne Roberts 0:6b3ac9c5a042 112 #endif /* LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 113 MOTE_MAC_DEVICE_TIME_REQ = 0x0d,
Wayne Roberts 0:6b3ac9c5a042 114 /*!
Wayne Roberts 0:6b3ac9c5a042 115 * PingSlotInfoReq
Wayne Roberts 0:6b3ac9c5a042 116 */
Wayne Roberts 0:6b3ac9c5a042 117 MOTE_MAC_PING_SLOT_INFO_REQ = 0x10,
Wayne Roberts 0:6b3ac9c5a042 118 /*!
Wayne Roberts 0:6b3ac9c5a042 119 * PingSlotFreqAns
Wayne Roberts 0:6b3ac9c5a042 120 */
Wayne Roberts 0:6b3ac9c5a042 121 MOTE_MAC_PING_SLOT_FREQ_ANS = 0x11,
Wayne Roberts 0:6b3ac9c5a042 122 /*!
Wayne Roberts 0:6b3ac9c5a042 123 * BeaconTimingReq
Wayne Roberts 0:6b3ac9c5a042 124 */
Wayne Roberts 0:6b3ac9c5a042 125 MOTE_MAC_BEACON_TIMING_REQ = 0x12,
Wayne Roberts 0:6b3ac9c5a042 126 /*!
Wayne Roberts 0:6b3ac9c5a042 127 * BeaconFreqAns
Wayne Roberts 0:6b3ac9c5a042 128 */
Wayne Roberts 0:6b3ac9c5a042 129 MOTE_MAC_BEACON_FREQ_ANS = 0x13,
Wayne Roberts 0:6b3ac9c5a042 130 }LoRaMacMoteCmd_t;
Wayne Roberts 0:6b3ac9c5a042 131
Wayne Roberts 0:6b3ac9c5a042 132 #define LORA_MAC_COMMAND_MAX_LENGTH 15
Wayne Roberts 0:6b3ac9c5a042 133
Wayne Roberts 0:6b3ac9c5a042 134
Wayne Roberts 0:6b3ac9c5a042 135 /*!
Wayne Roberts 0:6b3ac9c5a042 136 * Global MAC layer parameters
Wayne Roberts 0:6b3ac9c5a042 137 */
Wayne Roberts 0:6b3ac9c5a042 138 typedef struct sLoRaMacParams
Wayne Roberts 0:6b3ac9c5a042 139 {
Wayne Roberts 0:6b3ac9c5a042 140 /*!
Wayne Roberts 0:6b3ac9c5a042 141 * Channels TX power
Wayne Roberts 0:6b3ac9c5a042 142 */
Wayne Roberts 0:6b3ac9c5a042 143 int8_t ChannelsTxPower;
Wayne Roberts 0:6b3ac9c5a042 144 /*!
Wayne Roberts 0:6b3ac9c5a042 145 * Channels data rate
Wayne Roberts 0:6b3ac9c5a042 146 */
Wayne Roberts 0:6b3ac9c5a042 147 int8_t ChannelsDatarate;
Wayne Roberts 0:6b3ac9c5a042 148 /*!
Wayne Roberts 0:6b3ac9c5a042 149 * LoRaMac maximum time a reception window stays open
Wayne Roberts 0:6b3ac9c5a042 150 */
Wayne Roberts 0:6b3ac9c5a042 151 uint32_t MaxRxWindow_us;
Wayne Roberts 0:6b3ac9c5a042 152 /*!
Wayne Roberts 0:6b3ac9c5a042 153 * Receive delay 1
Wayne Roberts 0:6b3ac9c5a042 154 */
Wayne Roberts 0:6b3ac9c5a042 155 uint32_t ReceiveDelay1_us;
Wayne Roberts 0:6b3ac9c5a042 156 /*!
Wayne Roberts 0:6b3ac9c5a042 157 * Receive delay 2
Wayne Roberts 0:6b3ac9c5a042 158 */
Wayne Roberts 0:6b3ac9c5a042 159 uint32_t ReceiveDelay2_us;
Wayne Roberts 0:6b3ac9c5a042 160 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 161 /*!
Wayne Roberts 0:6b3ac9c5a042 162 * Join accept delay 1
Wayne Roberts 0:6b3ac9c5a042 163 */
Wayne Roberts 0:6b3ac9c5a042 164 uint32_t JoinAcceptDelay1_us;
Wayne Roberts 0:6b3ac9c5a042 165 /*!
Wayne Roberts 0:6b3ac9c5a042 166 * Join accept delay 1
Wayne Roberts 0:6b3ac9c5a042 167 */
Wayne Roberts 0:6b3ac9c5a042 168 uint32_t JoinAcceptDelay2_us;
Wayne Roberts 0:6b3ac9c5a042 169 #endif /* LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 170 /*!
Wayne Roberts 0:6b3ac9c5a042 171 * Number of uplink messages repetitions [1:15] (unconfirmed messages only)
Wayne Roberts 0:6b3ac9c5a042 172 */
Wayne Roberts 3:eb174e10afbb 173 uint8_t NbTrans;
Wayne Roberts 0:6b3ac9c5a042 174 /*!
Wayne Roberts 0:6b3ac9c5a042 175 * Datarate offset between uplink and downlink on first window
Wayne Roberts 0:6b3ac9c5a042 176 */
Wayne Roberts 0:6b3ac9c5a042 177 uint8_t Rx1DrOffset;
Wayne Roberts 0:6b3ac9c5a042 178 /*!
Wayne Roberts 0:6b3ac9c5a042 179 * LoRaMAC 2nd reception window settings
Wayne Roberts 0:6b3ac9c5a042 180 */
Wayne Roberts 0:6b3ac9c5a042 181 Rx2ChannelParams_t Rx2Channel;
Wayne Roberts 0:6b3ac9c5a042 182 /*!
Wayne Roberts 0:6b3ac9c5a042 183 * Mask indicating which channels are enabled
Wayne Roberts 0:6b3ac9c5a042 184 */
Wayne Roberts 0:6b3ac9c5a042 185 uint16_t ChannelsMask[6];
Wayne Roberts 3:eb174e10afbb 186 uint8_t NbEnabledChannels;
Wayne Roberts 0:6b3ac9c5a042 187 } LoRaMacParams_t;
Wayne Roberts 0:6b3ac9c5a042 188
Wayne Roberts 0:6b3ac9c5a042 189 /*!
Wayne Roberts 0:6b3ac9c5a042 190 * LoRaMAC header field definition (MHDR field)
Wayne Roberts 0:6b3ac9c5a042 191 *
Wayne Roberts 0:6b3ac9c5a042 192 * LoRaWAN Specification V1.0.1, chapter 4.2
Wayne Roberts 0:6b3ac9c5a042 193 */
Wayne Roberts 0:6b3ac9c5a042 194 typedef union uLoRaMacHeader
Wayne Roberts 0:6b3ac9c5a042 195 {
Wayne Roberts 0:6b3ac9c5a042 196 /*!
Wayne Roberts 0:6b3ac9c5a042 197 * Byte-access to the bits
Wayne Roberts 0:6b3ac9c5a042 198 */
Wayne Roberts 0:6b3ac9c5a042 199 uint8_t Value;
Wayne Roberts 0:6b3ac9c5a042 200 /*!
Wayne Roberts 0:6b3ac9c5a042 201 * Structure containing single access to header bits
Wayne Roberts 0:6b3ac9c5a042 202 */
Wayne Roberts 0:6b3ac9c5a042 203 struct sHdrBits
Wayne Roberts 0:6b3ac9c5a042 204 {
Wayne Roberts 0:6b3ac9c5a042 205 /*!
Wayne Roberts 0:6b3ac9c5a042 206 * Major version
Wayne Roberts 0:6b3ac9c5a042 207 */
Wayne Roberts 0:6b3ac9c5a042 208 uint8_t Major : 2;
Wayne Roberts 0:6b3ac9c5a042 209 /*!
Wayne Roberts 0:6b3ac9c5a042 210 * RFU
Wayne Roberts 0:6b3ac9c5a042 211 */
Wayne Roberts 0:6b3ac9c5a042 212 uint8_t RFU : 3;
Wayne Roberts 0:6b3ac9c5a042 213 /*!
Wayne Roberts 0:6b3ac9c5a042 214 * Message type
Wayne Roberts 0:6b3ac9c5a042 215 */
Wayne Roberts 0:6b3ac9c5a042 216 uint8_t MType : 3;
Wayne Roberts 0:6b3ac9c5a042 217 }Bits;
Wayne Roberts 0:6b3ac9c5a042 218 }LoRaMacHeader_t;
Wayne Roberts 0:6b3ac9c5a042 219
Wayne Roberts 0:6b3ac9c5a042 220 /*!
Wayne Roberts 0:6b3ac9c5a042 221 * LoRaMAC frame types
Wayne Roberts 0:6b3ac9c5a042 222 *
Wayne Roberts 0:6b3ac9c5a042 223 * LoRaWAN Specification V1.0.1, chapter 4.2.1, table 1
Wayne Roberts 0:6b3ac9c5a042 224 */
Wayne Roberts 0:6b3ac9c5a042 225 typedef enum eLoRaMacFrameType
Wayne Roberts 0:6b3ac9c5a042 226 {
Wayne Roberts 0:6b3ac9c5a042 227 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 228 /*!
Wayne Roberts 0:6b3ac9c5a042 229 * LoRaMAC join request frame
Wayne Roberts 0:6b3ac9c5a042 230 */
Wayne Roberts 0:6b3ac9c5a042 231 FRAME_TYPE_JOIN_REQ = 0x00,
Wayne Roberts 0:6b3ac9c5a042 232 /*!
Wayne Roberts 0:6b3ac9c5a042 233 * LoRaMAC join accept frame
Wayne Roberts 0:6b3ac9c5a042 234 */
Wayne Roberts 0:6b3ac9c5a042 235 FRAME_TYPE_JOIN_ACCEPT = 0x01,
Wayne Roberts 0:6b3ac9c5a042 236 #endif /* LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 237 /*!
Wayne Roberts 0:6b3ac9c5a042 238 * LoRaMAC unconfirmed up-link frame
Wayne Roberts 0:6b3ac9c5a042 239 */
Wayne Roberts 0:6b3ac9c5a042 240 FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02,
Wayne Roberts 0:6b3ac9c5a042 241 /*!
Wayne Roberts 0:6b3ac9c5a042 242 * LoRaMAC unconfirmed down-link frame
Wayne Roberts 0:6b3ac9c5a042 243 */
Wayne Roberts 0:6b3ac9c5a042 244 FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03,
Wayne Roberts 0:6b3ac9c5a042 245 /*!
Wayne Roberts 0:6b3ac9c5a042 246 * LoRaMAC confirmed up-link frame
Wayne Roberts 0:6b3ac9c5a042 247 */
Wayne Roberts 0:6b3ac9c5a042 248 FRAME_TYPE_DATA_CONFIRMED_UP = 0x04,
Wayne Roberts 0:6b3ac9c5a042 249 /*!
Wayne Roberts 0:6b3ac9c5a042 250 * LoRaMAC confirmed down-link frame
Wayne Roberts 0:6b3ac9c5a042 251 */
Wayne Roberts 0:6b3ac9c5a042 252 FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05,
Wayne Roberts 0:6b3ac9c5a042 253 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 254 /*!
Wayne Roberts 0:6b3ac9c5a042 255 * LoRaMAC rejoin frame
Wayne Roberts 0:6b3ac9c5a042 256 */
Wayne Roberts 0:6b3ac9c5a042 257 FRAME_TYPE_REJOIN_REQ = 0x06,
Wayne Roberts 0:6b3ac9c5a042 258 #endif /* LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 259 /*!
Wayne Roberts 0:6b3ac9c5a042 260 * LoRaMAC proprietary frame
Wayne Roberts 0:6b3ac9c5a042 261 */
Wayne Roberts 0:6b3ac9c5a042 262 FRAME_TYPE_PROPRIETARY = 0x07,
Wayne Roberts 0:6b3ac9c5a042 263 } LoRaMacFrameType_t;
Wayne Roberts 0:6b3ac9c5a042 264
Wayne Roberts 0:6b3ac9c5a042 265 /*!
Wayne Roberts 0:6b3ac9c5a042 266 * LoRaMAC server MAC commands
Wayne Roberts 0:6b3ac9c5a042 267 *
Wayne Roberts 0:6b3ac9c5a042 268 * LoRaWAN Specification V1.0.1 chapter 5, table 4
Wayne Roberts 0:6b3ac9c5a042 269 */
Wayne Roberts 0:6b3ac9c5a042 270 typedef enum eLoRaMacSrvCmd
Wayne Roberts 0:6b3ac9c5a042 271 {
Wayne Roberts 0:6b3ac9c5a042 272 #ifndef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 273 SRV_MAC_RESET_CONF = 0x01,
Wayne Roberts 0:6b3ac9c5a042 274 #endif /* !LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 275 /*!
Wayne Roberts 0:6b3ac9c5a042 276 * LinkCheckAns
Wayne Roberts 0:6b3ac9c5a042 277 */
Wayne Roberts 0:6b3ac9c5a042 278 SRV_MAC_LINK_CHECK_ANS = 0x02,
Wayne Roberts 0:6b3ac9c5a042 279 /*!
Wayne Roberts 0:6b3ac9c5a042 280 * LinkADRReq
Wayne Roberts 0:6b3ac9c5a042 281 */
Wayne Roberts 0:6b3ac9c5a042 282 SRV_MAC_LINK_ADR_REQ = 0x03,
Wayne Roberts 0:6b3ac9c5a042 283 /*!
Wayne Roberts 0:6b3ac9c5a042 284 * DutyCycleReq
Wayne Roberts 0:6b3ac9c5a042 285 */
Wayne Roberts 0:6b3ac9c5a042 286 SRV_MAC_DUTY_CYCLE_REQ = 0x04,
Wayne Roberts 0:6b3ac9c5a042 287 /*!
Wayne Roberts 0:6b3ac9c5a042 288 * RXParamSetupReq
Wayne Roberts 0:6b3ac9c5a042 289 */
Wayne Roberts 0:6b3ac9c5a042 290 SRV_MAC_RX_PARAM_SETUP_REQ = 0x05,
Wayne Roberts 0:6b3ac9c5a042 291 /*!
Wayne Roberts 0:6b3ac9c5a042 292 * DevStatusReq
Wayne Roberts 0:6b3ac9c5a042 293 */
Wayne Roberts 0:6b3ac9c5a042 294 SRV_MAC_DEV_STATUS_REQ = 0x06,
Wayne Roberts 0:6b3ac9c5a042 295 /*!
Wayne Roberts 0:6b3ac9c5a042 296 * NewChannelReq
Wayne Roberts 0:6b3ac9c5a042 297 */
Wayne Roberts 0:6b3ac9c5a042 298 SRV_MAC_NEW_CHANNEL_REQ = 0x07,
Wayne Roberts 0:6b3ac9c5a042 299 /*!
Wayne Roberts 0:6b3ac9c5a042 300 * RXTimingSetupReq
Wayne Roberts 0:6b3ac9c5a042 301 */
Wayne Roberts 0:6b3ac9c5a042 302 SRV_MAC_RX_TIMING_SETUP_REQ = 0x08,
Wayne Roberts 0:6b3ac9c5a042 303
Wayne Roberts 3:eb174e10afbb 304 SRV_MAC_ADR_PARAM_SETUP_REQ = 0x0c,
Wayne Roberts 0:6b3ac9c5a042 305 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 0:6b3ac9c5a042 306 SRV_MAC_REKEY_CONF = 0x0b,
Wayne Roberts 0:6b3ac9c5a042 307 SRV_MAC_FORCE_REJOIN_REQ = 0x0e,
Wayne Roberts 0:6b3ac9c5a042 308 SRV_MAC_REJOIN_PARAM_REQ = 0x0f,
Wayne Roberts 0:6b3ac9c5a042 309 #endif /* LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 310 SRV_MAC_DEVICE_TIME_ANS = 0x0d,
Wayne Roberts 0:6b3ac9c5a042 311 /*!
Wayne Roberts 0:6b3ac9c5a042 312 * PingSlotInfoAns
Wayne Roberts 0:6b3ac9c5a042 313 */
Wayne Roberts 0:6b3ac9c5a042 314 SRV_MAC_PING_SLOT_INFO_ANS = 0x10,
Wayne Roberts 0:6b3ac9c5a042 315 /*!
Wayne Roberts 0:6b3ac9c5a042 316 * PingSlotChannelReq
Wayne Roberts 0:6b3ac9c5a042 317 */
Wayne Roberts 0:6b3ac9c5a042 318 SRV_MAC_PING_SLOT_CHANNEL_REQ = 0x11,
Wayne Roberts 0:6b3ac9c5a042 319 /*!
Wayne Roberts 0:6b3ac9c5a042 320 * BeaconTimingAns
Wayne Roberts 0:6b3ac9c5a042 321 */
Wayne Roberts 0:6b3ac9c5a042 322 SRV_MAC_BEACON_TIMING_ANS = 0x12,
Wayne Roberts 0:6b3ac9c5a042 323 /*!
Wayne Roberts 0:6b3ac9c5a042 324 * BeaconFreqReq
Wayne Roberts 0:6b3ac9c5a042 325 */
Wayne Roberts 0:6b3ac9c5a042 326 SRV_MAC_BEACON_FREQ_REQ = 0x13,
Wayne Roberts 0:6b3ac9c5a042 327 }LoRaMacSrvCmd_t;
Wayne Roberts 0:6b3ac9c5a042 328
Wayne Roberts 0:6b3ac9c5a042 329 typedef union uLoRaMacFrameCtrl
Wayne Roberts 0:6b3ac9c5a042 330 {
Wayne Roberts 0:6b3ac9c5a042 331 /*!
Wayne Roberts 0:6b3ac9c5a042 332 * Byte-access to the bits
Wayne Roberts 0:6b3ac9c5a042 333 */
Wayne Roberts 0:6b3ac9c5a042 334 uint8_t Value;
Wayne Roberts 0:6b3ac9c5a042 335 /*!
Wayne Roberts 0:6b3ac9c5a042 336 * Structure containing single access to bits
Wayne Roberts 0:6b3ac9c5a042 337 */
Wayne Roberts 0:6b3ac9c5a042 338 struct sCtrlBits
Wayne Roberts 0:6b3ac9c5a042 339 {
Wayne Roberts 0:6b3ac9c5a042 340 /*!
Wayne Roberts 0:6b3ac9c5a042 341 * Frame options length
Wayne Roberts 0:6b3ac9c5a042 342 */
Wayne Roberts 0:6b3ac9c5a042 343 uint8_t FOptsLen : 4;
Wayne Roberts 0:6b3ac9c5a042 344 /*!
Wayne Roberts 0:6b3ac9c5a042 345 * Frame pending bit
Wayne Roberts 0:6b3ac9c5a042 346 */
Wayne Roberts 0:6b3ac9c5a042 347 uint8_t FPending : 1;
Wayne Roberts 0:6b3ac9c5a042 348 /*!
Wayne Roberts 0:6b3ac9c5a042 349 * Message acknowledge bit
Wayne Roberts 0:6b3ac9c5a042 350 */
Wayne Roberts 0:6b3ac9c5a042 351 uint8_t Ack : 1;
Wayne Roberts 0:6b3ac9c5a042 352 /*!
Wayne Roberts 0:6b3ac9c5a042 353 * ADR acknowledgment request bit
Wayne Roberts 0:6b3ac9c5a042 354 */
Wayne Roberts 0:6b3ac9c5a042 355 uint8_t AdrAckReq : 1;
Wayne Roberts 0:6b3ac9c5a042 356 /*!
Wayne Roberts 0:6b3ac9c5a042 357 * ADR control in frame header
Wayne Roberts 0:6b3ac9c5a042 358 */
Wayne Roberts 0:6b3ac9c5a042 359 uint8_t Adr : 1;
Wayne Roberts 0:6b3ac9c5a042 360 }Bits;
Wayne Roberts 0:6b3ac9c5a042 361 } LoRaMacFrameCtrl_t;
Wayne Roberts 0:6b3ac9c5a042 362
Wayne Roberts 0:6b3ac9c5a042 363 typedef struct {
Wayne Roberts 0:6b3ac9c5a042 364 uint16_t channelsMask[6];
Wayne Roberts 0:6b3ac9c5a042 365 uint16_t chMask;
Wayne Roberts 0:6b3ac9c5a042 366 uint8_t status;
Wayne Roberts 0:6b3ac9c5a042 367 uint8_t chMaskCntl;
Wayne Roberts 0:6b3ac9c5a042 368 int8_t datarate;
Wayne Roberts 0:6b3ac9c5a042 369 } adr_t;
Wayne Roberts 0:6b3ac9c5a042 370
Wayne Roberts 0:6b3ac9c5a042 371 /* from region: */
Wayne Roberts 0:6b3ac9c5a042 372 extern const int8_t TxPowers[];
Wayne Roberts 0:6b3ac9c5a042 373 extern const uint8_t MaxPayloadOfDatarate[];
Wayne Roberts 0:6b3ac9c5a042 374 extern ChannelParams_t Channels[];
Wayne Roberts 0:6b3ac9c5a042 375 extern const uint8_t Datarates[];
Wayne Roberts 0:6b3ac9c5a042 376 LoRaMacStatus_t LoRaMacChannelRemove( uint8_t id );
Wayne Roberts 0:6b3ac9c5a042 377 LoRaMacStatus_t LoRaMacChannelAdd( uint8_t id, ChannelParams_t params );
Wayne Roberts 0:6b3ac9c5a042 378
Wayne Roberts 3:eb174e10afbb 379 //void region_adr_next_dr(int8_t* dr, bool);
Wayne Roberts 0:6b3ac9c5a042 380 void region_ScheduleTx( void );
Wayne Roberts 0:6b3ac9c5a042 381 uint32_t region_GetRxBandwidth( int8_t datarate );
Wayne Roberts 0:6b3ac9c5a042 382 uint16_t region_GetRxSymbolTimeout( int8_t datarate );
Wayne Roberts 0:6b3ac9c5a042 383 void region_mac_init(void);
Wayne Roberts 0:6b3ac9c5a042 384 void region_rx1_setup(uint8_t channel);
Wayne Roberts 0:6b3ac9c5a042 385 void region_adr_request(adr_t*);
Wayne Roberts 0:6b3ac9c5a042 386 void region_tx_setup(int8_t dbm, uint8_t pkt_len);
Wayne Roberts 0:6b3ac9c5a042 387 void region_session_start(LoRaMacEventInfoStatus_t);
Wayne Roberts 3:eb174e10afbb 388 int8_t region_LimitTxPower( int8_t txPower );
Wayne Roberts 3:eb174e10afbb 389 uint8_t region_CountNbEnabledChannels(void);
Wayne Roberts 3:eb174e10afbb 390 #ifdef LORAWAN_JOIN_EUI
Wayne Roberts 3:eb174e10afbb 391 int8_t region_AlternateDatarate( uint16_t nbTrials );
Wayne Roberts 3:eb174e10afbb 392 #endif /* LORAWAN_JOIN_EUI */
Wayne Roberts 0:6b3ac9c5a042 393
Wayne Roberts 0:6b3ac9c5a042 394 /* from LoRaMac.cpp: */
Wayne Roberts 0:6b3ac9c5a042 395 extern LoRaMacParams_t LoRaMacParams;
Wayne Roberts 3:eb174e10afbb 396 extern const LoRaMacParams_t LoRaMacParamsDefaults;
Wayne Roberts 0:6b3ac9c5a042 397 extern uint8_t Channel;
Wayne Roberts 0:6b3ac9c5a042 398 void SendFrameOnChannel( uint8_t ch_num );
Wayne Roberts 0:6b3ac9c5a042 399 void RxWindowSetup( unsigned freq, int8_t datarate, unsigned bandwidth, uint16_t timeout);
Wayne Roberts 0:6b3ac9c5a042 400 uint8_t CountBits( uint16_t mask, uint8_t nbBits );
Wayne Roberts 0:6b3ac9c5a042 401 LoRaMacStatus_t AddMacCommand( uint8_t cmd, uint8_t p1, uint8_t p2 );
Wayne Roberts 0:6b3ac9c5a042 402 extern LowPowerTimeout TxDelayedEvent;
Wayne Roberts 0:6b3ac9c5a042 403 void OnTxDelayedTimerEvent(void);
Wayne Roberts 0:6b3ac9c5a042 404
Wayne Roberts 0:6b3ac9c5a042 405 #endif /* _LORAMACPRIVATE_H_ */