Loading...
Searching...
No Matches
gnrc_lorawan_internal.h File Reference

GNRC LoRaWAN internal header. More...

Detailed Description

GNRC LoRaWAN internal header.

Author
Jose Ignacio Alamos jose..nosp@m.alam.nosp@m.os@ha.nosp@m.w-ha.nosp@m.mburg.nosp@m..de

Definition in file gnrc_lorawan_internal.h.

#include <stdio.h>
#include <string.h>
#include "iolist.h"
#include "net/lora.h"
#include "net/lorawan/hdr.h"
#include "net/gnrc/pktbuf.h"
#include "net/netdev.h"
#include "net/loramac.h"
+ Include dependency graph for gnrc_lorawan_internal.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  lorawan_buffer_t
 buffer helper for parsing and constructing LoRaWAN packets. More...
 
struct  mlme_lorawan_join_t
 MLME Join Request data. More...
 
struct  mlme_link_req_confirm_t
 MLME Link Check confirmation data. More...
 
struct  mcps_data_t
 MCPS data. More...
 
struct  gnrc_lorawan_mcps_t
 MCPS service access point descriptor. More...
 
struct  gnrc_lorawan_mlme_t
 MLME service access point descriptor. More...
 
struct  gnrc_lorawan_key_ctx_t
 GNRC LoRaWAN key context struct. More...
 
struct  gnrc_lorawan_t
 GNRC LoRaWAN mac descriptor. More...
 
struct  gnrc_lorawan_persistent_state_t
 LoRaWAN state that needs to be preserved across reboots. More...
 
#define MSG_TYPE_TIMEOUT   (0x3457)
 Timeout message type.
 
#define MTYPE_MASK   0xE0
 MHDR mtype mask.
 
#define MTYPE_JOIN_REQUEST   0x0
 Join Request type.
 
#define MTYPE_JOIN_ACCEPT   0x1
 Join Accept type.
 
#define MTYPE_UNCNF_UPLINK   0x2
 Unconfirmed uplink type.
 
#define MTYPE_UNCNF_DOWNLINK   0x3
 Unconfirmed downlink type.
 
#define MTYPE_CNF_UPLINK   0x4
 Confirmed uplink type.
 
#define MTYPE_CNF_DOWNLINK   0x5
 Confirmed downlink type.
 
#define MTYPE_REJOIN_REQ   0x6
 Re-join request type.
 
#define MTYPE_PROPIETARY   0x7
 Proprietary frame type.
 
#define MAJOR_MASK   0x3
 Major mtype mask.
 
#define MAJOR_LRWAN_R1   0x0
 LoRaWAN R1 version type.
 
#define MINOR_LRWAN   0x1
 Minor LoRaWAN version of device.
 
#define JOIN_REQUEST_SIZE   (23U)
 Join Request size in bytes.
 
#define MIC_SIZE   (4U)
 MIC size in bytes.
 
#define CFLIST_SIZE   (16U)
 Channel Frequency list size in bytes.
 
#define GNRC_LORAWAN_MAX_CHANNELS   (16U)
 Maximum number of channels.
 
#define LORAWAN_STATE_IDLE   (0)
 MAC state machine in idle.
 
#define LORAWAN_STATE_RX_1   (1)
 MAC state machine in RX1.
 
#define LORAWAN_STATE_RX_2   (2)
 MAC state machine in RX2.
 
#define LORAWAN_STATE_TX   (3)
 MAC state machine in TX.
 
#define LORAWAN_STATE_JOIN   (4)
 MAC state machine in Join.
 
#define GNRC_LORAWAN_DIR_UPLINK   (0U)
 uplink frame direction
 
#define GNRC_LORAWAN_DIR_DOWNLINK   (1U)
 downlink frame direction
 
#define GNRC_LORAWAN_BACKOFF_WINDOW_TICK   (3600000000LL)
 backoff expire tick in usecs (set to 1 second)
 
#define GNRC_LORAWAN_BACKOFF_BUDGET_1   (36000000LL)
 budget of time on air during the first hour
 
#define GNRC_LORAWAN_BACKOFF_BUDGET_2   (36000000LL)
 budget of time on air between 1-10 hours after boot
 
#define GNRC_LORAWAN_BACKOFF_BUDGET_3   (8700000LL)
 budget of time on air every 24 hours
 
#define GNRC_LORAWAN_MLME_OPTS_LINK_CHECK_REQ   (1 << 0)
 Internal Link Check request flag.
 
#define GNRC_LORAWAN_MLME_OPTS_REKEY_IND_REQ   (1 << 1)
 Internal Rekey Indication flag.
 
#define GNRC_LORAWAN_CID_SIZE   (1U)
 size of Command ID in FOps
 
#define GNRC_LORAWAN_CID_LINK_CHECK_ANS   (0x02)
 Link Check CID.
 
#define GNCR_LORAWAN_REKEY_IND_SIZE   (1U)
 RekeyInd MAC command size.
 
#define GNCR_LORAWAN_CID_REKEY_CONF   (0x0B)
 Reykey Confirmation CID.
 
#define GNRC_LORAWAN_FOPT_LINK_CHECK_ANS_SIZE   (3U)
 size of Link check answer
 
#define GNRC_LORAWAN_FOPT_REKEY_CONF_SIZE   (2U)
 size of Rekey confirmation
 
#define GNRC_LORAWAN_JOIN_DELAY_U32_MASK   (0x1FFFFF)
 mask for detecting overflow in frame counter
 
#define GNRC_LORAWAN_MAX_PAYLOAD_1   (59U)
 max MAC payload in DR0, DR1 and DR2
 
#define GNRC_LORAWAN_MAX_PAYLOAD_2   (123U)
 max MAC payload in DR3
 
#define GNRC_LORAWAN_MAX_PAYLOAD_3   (250U)
 max MAC payload above DR3
 
#define GNRC_LORAWAN_CFLIST_ENTRY_SIZE   (3U)
 size of Channel Frequency list
 
#define GNRC_LORAWAN_JOIN_ACCEPT_MAX_SIZE   (33U)
 max size of Join Accept frame
 
#define GNRC_LORAWAN_BACKOFF_STATE_1   (0U)
 backoff state during the first hour after boot
 
#define GNRC_LORAWAN_BACKOFF_STATE_2   (1U)
 backoff state between 1-10 hours after boot
 
#define GNRC_LORAWAN_BACKOFF_STATE_3   (2U)
 backoff state past 11 hours after boot
 
#define GNRC_LORAWAN_BACKOFF_TIME_1   (1U)
 duration of first backoff state (in hours)
 
#define GNRC_LORAWAN_BACKOFF_TIME_2   (10U)
 duration of second backoff state (in hours)
 
#define GNRC_LORAWAN_BACKOFF_TIME_3   (24U)
 duration of third backoff state (in hours)
 
#define GNRC_LORAWAN_APP_NONCE_SIZE   (3U)
 App Nonce size.
 
#define GNRC_LORAWAN_JOIN_NONCE_SIZE   (3U)
 Join nonce size.
 
#define GNRC_LORAWAN_NET_ID_SIZE   (3U)
 Net ID size.
 
#define GNRC_LORAWAN_DEV_NONCE_SIZE   (2U)
 Dev Nonce size.
 
#define GNRC_LORAWAN_FOPTS_MAX_SIZE   (15U)
 Maximum size of Fopts field.
 
#define GNRC_LORAWAN_FPORT_SIZE   (1U)
 Size of the Fport field.
 
#define GNRC_LORAWAN_STATE_FLASHPAGE_NUM   (FLASHPAGE_NUMOF - 0x4)
 Flashpage number where to store state.
 
#define GNRC_LORAWAN_INITIALIZED_MARKER   (0x52f94f54U)
 Persistent state initialized marker.
 
#define MHDR_MIC_BUF_SIZE
 Size of the internal MHDR-MIC buffer.
 
enum  mlme_join_req_type_t { REJOIN_REQ_0 , REJOIN_REQ_1 , REJOIN_REQ_2 , JOIN_REQ = 0xFF }
 MLME Join Request type. More...
 
void gnrc_lorawan_encrypt_payload (iolist_t *iolist, const le_uint32_t *dev_addr, uint32_t fcnt, uint8_t dir, const uint8_t *appskey)
 Encrypts LoRaWAN payload.
 
void gnrc_lorawan_encrypt_fopts (uint8_t *fopts, size_t len, const le_uint32_t *dev_addr, uint32_t fcnt, bool afcnt, uint8_t dir, const uint8_t *key)
 Encrypts FOpts field.
 
void gnrc_lorawan_decrypt_join_accept (const uint8_t *key, uint8_t *pkt, int has_clist, uint8_t *out)
 Decrypts join accept message.
 
void gnrc_lorawan_generate_session_keys (const uint8_t *join_nonce, const uint8_t *dev_nonce, const uint8_t *joineui, gnrc_lorawan_t *mac)
 Generate LoRaWAN session keys.
 
void gnrc_lorawan_generate_lifetime_session_keys (const uint8_t *deveui, const uint8_t *nwkkey, uint8_t *jsintkey, uint8_t *jsenckey)
 Generate LoRaWAN 1.1x lifetime session keys.
 
int gnrc_lorawan_set_dr (gnrc_lorawan_t *mac, uint8_t datarate)
 Set datarate for the next transmission.
 
size_t gnrc_lorawan_build_uplink (gnrc_lorawan_t *mac, iolist_t *payload, int confirmed_data, uint8_t port)
 build uplink frame
 
uint8_t gnrc_lorawan_pick_channel (gnrc_lorawan_t *mac)
 pick a random available LoRaWAN channel
 
uint8_t gnrc_lorawan_build_options (gnrc_lorawan_t *mac, lorawan_buffer_t *buf)
 Build fopts header.
 
void gnrc_lorawan_process_fopts (gnrc_lorawan_t *mac, uint8_t *fopts, size_t size)
 Process an fopts frame.
 
void gnrc_lorawan_calculate_join_req_mic (const uint8_t *buf, size_t len, uint8_t *key, le_uint32_t *out)
 Calculate join-request Message Integrity Code.
 
void gnrc_lorawan_calculate_join_acpt_mic (const uint8_t *buf, size_t len, gnrc_lorawan_t *mac, le_uint32_t *out)
 Calculate join-accept Message Integrity Code.
 
void gnrc_lorawan_calculate_mic_uplink (iolist_t *frame, uint16_t conf_fcnt, gnrc_lorawan_t *mac, le_uint32_t *out)
 Calculate Message Integrity Code for an uplink MCPS message.
 
void gnrc_lorawan_calculate_mic_downlink (const le_uint32_t *dev_addr, uint32_t fcnt, uint16_t conf_fcnt, iolist_t *frame, const uint8_t *snwksintkey, le_uint32_t *out)
 Calculate Message Integrity Code for downlink MCPS message.
 
size_t gnrc_lorawan_build_hdr (uint8_t mtype, le_uint32_t *dev_addr, uint32_t fcnt, uint8_t ack, uint8_t fopts_length, lorawan_buffer_t *buf)
 Build a MCPS LoRaWAN header.
 
void gnrc_lorawan_mcps_process_downlink (gnrc_lorawan_t *mac, uint8_t *psdu, size_t size)
 Process an MCPS downlink message (confirmable or non comfirmable)
 
void gnrc_lorawan_channels_init (gnrc_lorawan_t *mac)
 Init regional channel settings.
 
void gnrc_lorawan_reset (gnrc_lorawan_t *mac)
 Reset MAC parameters.
 
void gnrc_lorawan_send_pkt (gnrc_lorawan_t *mac, iolist_t *psdu, uint8_t dr, uint32_t chan)
 Send a LoRaWAN packet.
 
void gnrc_lorawan_mlme_process_join (gnrc_lorawan_t *mac, uint8_t *data, size_t size)
 Process join accept message.
 
void gnrc_lorawan_mlme_no_rx (gnrc_lorawan_t *mac)
 Inform the MAC layer that no packet was received during reception.
 
void gnrc_lorawan_event_no_rx (gnrc_lorawan_t *mac)
 Mac callback for no RX.
 
void gnrc_lorawan_event_retrans_timeout (gnrc_lorawan_t *mac)
 Mac callback for retransmission timeout event.
 
uint8_t gnrc_lorawan_region_mac_payload_max (uint8_t datarate)
 Get the maximum MAC payload (M value) for a given datarate.
 
void gnrc_lorawan_mlme_backoff_expire_cb (gnrc_lorawan_t *mac)
 MLME Backoff expiration tick.
 
void gnrc_lorawan_process_pkt (gnrc_lorawan_t *mac, iolist_t *pkt)
 Process and dispatch a full LoRaWAN packet.
 
void gnrc_lorawan_open_rx_window (gnrc_lorawan_t *mac)
 Open a reception window.
 
void gnrc_lorawan_perform_save (gnrc_lorawan_t *mac)
 save internal MAC state in non-volatile storage and shutdown the MAC layer gracefully.
 
static int gnrc_lorawan_mac_acquire (gnrc_lorawan_t *mac)
 Acquire the MAC layer.
 
static void gnrc_lorawan_mac_release (gnrc_lorawan_t *mac)
 Release the MAC layer.
 
void gnrc_lorawan_set_rx2_dr (gnrc_lorawan_t *mac, uint8_t rx2_dr)
 Set the datarate of the second reception window.
 
void gnrc_lorawan_trigger_join (gnrc_lorawan_t *mac)
 Trigger the transmission of the Join Request packet.
 
void gnrc_lorawan_store_dev_nonce (uint8_t *dev_nonce)
 Store DevNonce in flash memory.
 
static bool gnrc_lorawan_optneg_is_set (const gnrc_lorawan_t *mac)
 Check whether OptNeg bit is set.
 
static void gnrc_lorawan_set_optneg (gnrc_lorawan_t *mac, uint8_t optneg)
 Set OptNeg bit in the MAC descriptor.
 
static uint8_t * gnrc_lorawan_get_jsintkey (const gnrc_lorawan_t *mac)
 Get the join session integrity key from the MAC descriptor.
 
static uint8_t * gnrc_lorawan_get_jsenckey (const gnrc_lorawan_t *mac)
 Get the join session encryption key from the MAC descriptor.
 
static uint32_t gnrc_lorawan_get_afcnt_down (const gnrc_lorawan_t *mac)
 Get the application downlink frame counter from the MAC descriptor.
 
static void gnrc_lorawan_set_afcnt_down (gnrc_lorawan_t *mac, uint32_t afcnt_down)
 Set the application downlink frame counter in the MAC descriptor.
 
static uint32_t gnrc_lorawan_get_last_fcnt_down (const gnrc_lorawan_t *mac)
 Get the last downlink frame counter from the MAC descriptor.
 
static void gnrc_lorawan_set_last_fcnt_down (gnrc_lorawan_t *mac, uint32_t last_fcnt_down)
 Set the last downlink frame counter in the MAC descriptor.
 
static uint8_t * gnrc_lorawan_mlme_join_get_appkey (const mlme_lorawan_join_t *mlme_join)
 Get the app key from the MLME join request data.
 
static void gnrc_lorawan_mlme_join_set_appkey (mlme_lorawan_join_t *mlme_join, uint8_t *key)
 Se the app key in the MLME join request data.
 

Macro Definition Documentation

◆ CFLIST_SIZE

#define CFLIST_SIZE   (16U)

Channel Frequency list size in bytes.

Definition at line 53 of file gnrc_lorawan_internal.h.

◆ GNCR_LORAWAN_CID_REKEY_CONF

#define GNCR_LORAWAN_CID_REKEY_CONF   (0x0B)

Reykey Confirmation CID.

Definition at line 79 of file gnrc_lorawan_internal.h.

◆ GNCR_LORAWAN_REKEY_IND_SIZE

#define GNCR_LORAWAN_REKEY_IND_SIZE   (1U)

RekeyInd MAC command size.

Definition at line 78 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_APP_NONCE_SIZE

#define GNRC_LORAWAN_APP_NONCE_SIZE   (3U)

App Nonce size.

Definition at line 101 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_BUDGET_1

#define GNRC_LORAWAN_BACKOFF_BUDGET_1   (36000000LL)

budget of time on air during the first hour

Definition at line 68 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_BUDGET_2

#define GNRC_LORAWAN_BACKOFF_BUDGET_2   (36000000LL)

budget of time on air between 1-10 hours after boot

Definition at line 69 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_BUDGET_3

#define GNRC_LORAWAN_BACKOFF_BUDGET_3   (8700000LL)

budget of time on air every 24 hours

Definition at line 70 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_STATE_1

#define GNRC_LORAWAN_BACKOFF_STATE_1   (0U)

backoff state during the first hour after boot

Definition at line 93 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_STATE_2

#define GNRC_LORAWAN_BACKOFF_STATE_2   (1U)

backoff state between 1-10 hours after boot

Definition at line 94 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_STATE_3

#define GNRC_LORAWAN_BACKOFF_STATE_3   (2U)

backoff state past 11 hours after boot

Definition at line 95 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_TIME_1

#define GNRC_LORAWAN_BACKOFF_TIME_1   (1U)

duration of first backoff state (in hours)

Definition at line 97 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_TIME_2

#define GNRC_LORAWAN_BACKOFF_TIME_2   (10U)

duration of second backoff state (in hours)

Definition at line 98 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_TIME_3

#define GNRC_LORAWAN_BACKOFF_TIME_3   (24U)

duration of third backoff state (in hours)

Definition at line 99 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_BACKOFF_WINDOW_TICK

#define GNRC_LORAWAN_BACKOFF_WINDOW_TICK   (3600000000LL)

backoff expire tick in usecs (set to 1 second)

Definition at line 66 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_CFLIST_ENTRY_SIZE

#define GNRC_LORAWAN_CFLIST_ENTRY_SIZE   (3U)

size of Channel Frequency list

Definition at line 90 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_CID_LINK_CHECK_ANS

#define GNRC_LORAWAN_CID_LINK_CHECK_ANS   (0x02)

Link Check CID.

Definition at line 76 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_CID_SIZE

#define GNRC_LORAWAN_CID_SIZE   (1U)

size of Command ID in FOps

Definition at line 75 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_DEV_NONCE_SIZE

#define GNRC_LORAWAN_DEV_NONCE_SIZE   (2U)

Dev Nonce size.

Definition at line 104 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_DIR_DOWNLINK

#define GNRC_LORAWAN_DIR_DOWNLINK   (1U)

downlink frame direction

Definition at line 64 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_DIR_UPLINK

#define GNRC_LORAWAN_DIR_UPLINK   (0U)

uplink frame direction

Definition at line 63 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_FOPT_LINK_CHECK_ANS_SIZE

#define GNRC_LORAWAN_FOPT_LINK_CHECK_ANS_SIZE   (3U)

size of Link check answer

Definition at line 81 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_FOPT_REKEY_CONF_SIZE

#define GNRC_LORAWAN_FOPT_REKEY_CONF_SIZE   (2U)

size of Rekey confirmation

Definition at line 82 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_FOPTS_MAX_SIZE

#define GNRC_LORAWAN_FOPTS_MAX_SIZE   (15U)

Maximum size of Fopts field.

Definition at line 106 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_FPORT_SIZE

#define GNRC_LORAWAN_FPORT_SIZE   (1U)

Size of the Fport field.

Definition at line 107 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_INITIALIZED_MARKER

#define GNRC_LORAWAN_INITIALIZED_MARKER   (0x52f94f54U)

Persistent state initialized marker.

Definition at line 115 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_JOIN_ACCEPT_MAX_SIZE

#define GNRC_LORAWAN_JOIN_ACCEPT_MAX_SIZE   (33U)

max size of Join Accept frame

Definition at line 91 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_JOIN_DELAY_U32_MASK

#define GNRC_LORAWAN_JOIN_DELAY_U32_MASK   (0x1FFFFF)

mask for detecting overflow in frame counter

Definition at line 84 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_JOIN_NONCE_SIZE

#define GNRC_LORAWAN_JOIN_NONCE_SIZE   (3U)

Join nonce size.

Definition at line 102 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_MAX_CHANNELS

#define GNRC_LORAWAN_MAX_CHANNELS   (16U)

Maximum number of channels.

Definition at line 55 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_MAX_PAYLOAD_1

#define GNRC_LORAWAN_MAX_PAYLOAD_1   (59U)

max MAC payload in DR0, DR1 and DR2

Definition at line 86 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_MAX_PAYLOAD_2

#define GNRC_LORAWAN_MAX_PAYLOAD_2   (123U)

max MAC payload in DR3

Definition at line 87 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_MAX_PAYLOAD_3

#define GNRC_LORAWAN_MAX_PAYLOAD_3   (250U)

max MAC payload above DR3

Definition at line 88 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_MLME_OPTS_LINK_CHECK_REQ

#define GNRC_LORAWAN_MLME_OPTS_LINK_CHECK_REQ   (1 << 0)

Internal Link Check request flag.

Definition at line 72 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_MLME_OPTS_REKEY_IND_REQ

#define GNRC_LORAWAN_MLME_OPTS_REKEY_IND_REQ   (1 << 1)

Internal Rekey Indication flag.

Definition at line 73 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_NET_ID_SIZE

#define GNRC_LORAWAN_NET_ID_SIZE   (3U)

Net ID size.

Definition at line 103 of file gnrc_lorawan_internal.h.

◆ GNRC_LORAWAN_STATE_FLASHPAGE_NUM

#define GNRC_LORAWAN_STATE_FLASHPAGE_NUM   (FLASHPAGE_NUMOF - 0x4)

Flashpage number where to store state.

Definition at line 113 of file gnrc_lorawan_internal.h.

◆ JOIN_REQUEST_SIZE

#define JOIN_REQUEST_SIZE   (23U)

Join Request size in bytes.

Definition at line 51 of file gnrc_lorawan_internal.h.

◆ LORAWAN_STATE_IDLE

#define LORAWAN_STATE_IDLE   (0)

MAC state machine in idle.

Definition at line 57 of file gnrc_lorawan_internal.h.

◆ LORAWAN_STATE_JOIN

#define LORAWAN_STATE_JOIN   (4)

MAC state machine in Join.

Definition at line 61 of file gnrc_lorawan_internal.h.

◆ LORAWAN_STATE_RX_1

#define LORAWAN_STATE_RX_1   (1)

MAC state machine in RX1.

Definition at line 58 of file gnrc_lorawan_internal.h.

◆ LORAWAN_STATE_RX_2

#define LORAWAN_STATE_RX_2   (2)

MAC state machine in RX2.

Definition at line 59 of file gnrc_lorawan_internal.h.

◆ LORAWAN_STATE_TX

#define LORAWAN_STATE_TX   (3)

MAC state machine in TX.

Definition at line 60 of file gnrc_lorawan_internal.h.

◆ MAJOR_LRWAN_R1

#define MAJOR_LRWAN_R1   0x0

LoRaWAN R1 version type.

Definition at line 47 of file gnrc_lorawan_internal.h.

◆ MAJOR_MASK

#define MAJOR_MASK   0x3

Major mtype mask.

Definition at line 46 of file gnrc_lorawan_internal.h.

◆ MHDR_MIC_BUF_SIZE

#define MHDR_MIC_BUF_SIZE
Value:
(sizeof(lorawan_hdr_t) + \
#define MIC_SIZE
MIC size in bytes.
#define GNRC_LORAWAN_FOPTS_MAX_SIZE
Maximum size of Fopts field.
#define GNRC_LORAWAN_FPORT_SIZE
Size of the Fport field.
Data type to represent a LoRaWAN packet header.
Definition hdr.h:74

Size of the internal MHDR-MIC buffer.

Definition at line 120 of file gnrc_lorawan_internal.h.

◆ MIC_SIZE

#define MIC_SIZE   (4U)

MIC size in bytes.

Definition at line 52 of file gnrc_lorawan_internal.h.

◆ MINOR_LRWAN

#define MINOR_LRWAN   0x1

Minor LoRaWAN version of device.

Definition at line 49 of file gnrc_lorawan_internal.h.

◆ MSG_TYPE_TIMEOUT

#define MSG_TYPE_TIMEOUT   (0x3457)

Timeout message type.

Definition at line 34 of file gnrc_lorawan_internal.h.

◆ MTYPE_CNF_DOWNLINK

#define MTYPE_CNF_DOWNLINK   0x5

Confirmed downlink type.

Definition at line 42 of file gnrc_lorawan_internal.h.

◆ MTYPE_CNF_UPLINK

#define MTYPE_CNF_UPLINK   0x4

Confirmed uplink type.

Definition at line 41 of file gnrc_lorawan_internal.h.

◆ MTYPE_JOIN_ACCEPT

#define MTYPE_JOIN_ACCEPT   0x1

Join Accept type.

Definition at line 38 of file gnrc_lorawan_internal.h.

◆ MTYPE_JOIN_REQUEST

#define MTYPE_JOIN_REQUEST   0x0

Join Request type.

Definition at line 37 of file gnrc_lorawan_internal.h.

◆ MTYPE_MASK

#define MTYPE_MASK   0xE0

MHDR mtype mask.

Definition at line 36 of file gnrc_lorawan_internal.h.

◆ MTYPE_PROPIETARY

#define MTYPE_PROPIETARY   0x7

Proprietary frame type.

Definition at line 44 of file gnrc_lorawan_internal.h.

◆ MTYPE_REJOIN_REQ

#define MTYPE_REJOIN_REQ   0x6

Re-join request type.

Definition at line 43 of file gnrc_lorawan_internal.h.

◆ MTYPE_UNCNF_DOWNLINK

#define MTYPE_UNCNF_DOWNLINK   0x3

Unconfirmed downlink type.

Definition at line 40 of file gnrc_lorawan_internal.h.

◆ MTYPE_UNCNF_UPLINK

#define MTYPE_UNCNF_UPLINK   0x2

Unconfirmed uplink type.

Definition at line 39 of file gnrc_lorawan_internal.h.

Enumeration Type Documentation

◆ mlme_join_req_type_t

MLME Join Request type.

Enumerator
REJOIN_REQ_0 

Rejoin-request type 0.

REJOIN_REQ_1 

Rejoin-request type 1.

REJOIN_REQ_2 

Rejoin-request type 2.

JOIN_REQ 

Join-request type.

Definition at line 150 of file gnrc_lorawan_internal.h.

Function Documentation

◆ gnrc_lorawan_build_hdr()

size_t gnrc_lorawan_build_hdr ( uint8_t  mtype,
le_uint32_t dev_addr,
uint32_t  fcnt,
uint8_t  ack,
uint8_t  fopts_length,
lorawan_buffer_t buf 
)

Build a MCPS LoRaWAN header.

Parameters
[in]mtypethe MType of the header
[in]dev_addrthe Device Address
[in]fcntframe counter
[in]acktrue if ACK bit is set
[in]fopts_lengththe length of the FOpts field
[out]bufdestination buffer of the hdr
Returns
the size of the header

◆ gnrc_lorawan_build_options()

uint8_t gnrc_lorawan_build_options ( gnrc_lorawan_t mac,
lorawan_buffer_t buf 
)

Build fopts header.

Parameters
[in]macpointer to MAC descriptor
[out]bufdestination buffer of fopts. If NULL, this function just returns the size of the expected fopts frame.
Returns
size of the fopts frame

◆ gnrc_lorawan_build_uplink()

size_t gnrc_lorawan_build_uplink ( gnrc_lorawan_t mac,
iolist_t payload,
int  confirmed_data,
uint8_t  port 
)

build uplink frame

Parameters
[in]macpointer to MAC descriptor
[in]payloadpacket containing payload
[in]confirmed_datatrue if confirmed frame
[in]portMAC port
Returns
full LoRaWAN frame including payload
NULL if packet buffer is full. payload is released

◆ gnrc_lorawan_calculate_join_acpt_mic()

void gnrc_lorawan_calculate_join_acpt_mic ( const uint8_t *  buf,
size_t  len,
gnrc_lorawan_t mac,
le_uint32_t out 
)

Calculate join-accept Message Integrity Code.

Parameters
[in]bufpointer to the frame
[in]lenlength of the frame
[in]macpointer to MAC descriptor
[out]outcalculated MIC

◆ gnrc_lorawan_calculate_join_req_mic()

void gnrc_lorawan_calculate_join_req_mic ( const uint8_t *  buf,
size_t  len,
uint8_t *  key,
le_uint32_t out 
)

Calculate join-request Message Integrity Code.

Parameters
[in]bufpointer to the frame
[in]lenlength of the frame
[in]keykey to be used to calculate the MIC
[out]outcalculated MIC

◆ gnrc_lorawan_calculate_mic_downlink()

void gnrc_lorawan_calculate_mic_downlink ( const le_uint32_t dev_addr,
uint32_t  fcnt,
uint16_t  conf_fcnt,
iolist_t frame,
const uint8_t *  snwksintkey,
le_uint32_t out 
)

Calculate Message Integrity Code for downlink MCPS message.

Parameters
[in]dev_addrthe Device Address
[in]fcntframe counter
[in]conf_fcntframe counter value of confirmed uplink frame
[in]framepointer to the PSDU frame (without MIC)
[in]snwksintkeypointer to serving network session integrity key
[out]outcalculated MIC

◆ gnrc_lorawan_calculate_mic_uplink()

void gnrc_lorawan_calculate_mic_uplink ( iolist_t frame,
uint16_t  conf_fcnt,
gnrc_lorawan_t mac,
le_uint32_t out 
)

Calculate Message Integrity Code for an uplink MCPS message.

Parameters
[in]framepointer to the PSDU frame (without MIC)
[in]conf_fcntframe counter value of confirmed uplink frame. Always zero if using LoRaWAN 1.0x
[in]macpointer to MAC descriptor
[out]outcalculated MIC

◆ gnrc_lorawan_channels_init()

void gnrc_lorawan_channels_init ( gnrc_lorawan_t mac)

Init regional channel settings.

   Intended to be called upon initialization
Parameters
[in]macpointer to the MAC descriptor

◆ gnrc_lorawan_decrypt_join_accept()

void gnrc_lorawan_decrypt_join_accept ( const uint8_t *  key,
uint8_t *  pkt,
int  has_clist,
uint8_t *  out 
)

Decrypts join accept message.

Parameters
[in]keykey to be used in the decryption
[in]pktpointer to Join Accept MAC component (next byte after the MHDR)
[in]has_clisttrue if the Join Accept frame has CFList
[out]outbuffer where the decryption is stored

◆ gnrc_lorawan_encrypt_fopts()

void gnrc_lorawan_encrypt_fopts ( uint8_t *  fopts,
size_t  len,
const le_uint32_t dev_addr,
uint32_t  fcnt,
bool  afcnt,
uint8_t  dir,
const uint8_t *  key 
)

Encrypts FOpts field.

Note
This function is also used for decrypting the FOpts field. The LoRaWAN server encrypts the packet using decryption, so the end device only needs to implement encryption
This function is used only in LoRaWAN 1.1
Parameters
[in]foptspointer to fopts
[in]lenlength of fopts
[in]dev_addrdevice address
[in]fcntframe counter
[in]afcntflag indicating if aFCntDown is used
[in]dirdirection of the packet (0 if uplink, 1 if downlink)
[in]keypointer to key

◆ gnrc_lorawan_encrypt_payload()

void gnrc_lorawan_encrypt_payload ( iolist_t iolist,
const le_uint32_t dev_addr,
uint32_t  fcnt,
uint8_t  dir,
const uint8_t *  appskey 
)

Encrypts LoRaWAN payload.

Note
This function is also used for decrypting a LoRaWAN packet. The LoRaWAN server encrypts the packet using decryption, so the end device only needs to implement encryption
Parameters
[in]iolistpointer to the MSDU frame
[in]dev_addrdevice address
[in]fcntframe counter
[in]dirdirection of the packet (0 if uplink, 1 if downlink)
[in]appskeypointer to the Application Session Key

◆ gnrc_lorawan_event_no_rx()

void gnrc_lorawan_event_no_rx ( gnrc_lorawan_t mac)

Mac callback for no RX.

Parameters
[in]macpointer to the MAC descriptor

◆ gnrc_lorawan_event_retrans_timeout()

void gnrc_lorawan_event_retrans_timeout ( gnrc_lorawan_t mac)

Mac callback for retransmission timeout event.

Parameters
[in]macpointer to the MAC descriptor

◆ gnrc_lorawan_generate_lifetime_session_keys()

void gnrc_lorawan_generate_lifetime_session_keys ( const uint8_t *  deveui,
const uint8_t *  nwkkey,
uint8_t *  jsintkey,
uint8_t *  jsenckey 
)

Generate LoRaWAN 1.1x lifetime session keys.

Called in order to derive JSIntKey and JSEncKey.

LoRaWAN specification 1.1 section 6.1.1.4

Parameters
[in]deveuipointer to the join_nonce of the Join Accept message
[in]nwkkeypointer to the Network key
[out]jsintkeypointer to the Join session integrity key
[out]jsenckeypointer to the Join session encryption key

◆ gnrc_lorawan_generate_session_keys()

void gnrc_lorawan_generate_session_keys ( const uint8_t *  join_nonce,
const uint8_t *  dev_nonce,
const uint8_t *  joineui,
gnrc_lorawan_t mac 
)

Generate LoRaWAN session keys.

Intended to be called after a successful Join Request in order to generate AppSKey, SNwkSIntKey, FNwkSIntKey and NwkSEncKey

Parameters
[in]join_noncepointer to the join_nonce of the Join Accept message
[in]dev_noncepointer to the dev_nonce buffer
[in]joineuipointer to the Join EUI. NULL if LoRaWAN 1.0x
[in,out]macpointer to the LoRaWAN mac descriptor

◆ gnrc_lorawan_get_afcnt_down()

static uint32_t gnrc_lorawan_get_afcnt_down ( const gnrc_lorawan_t mac)
inlinestatic

Get the application downlink frame counter from the MAC descriptor.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
Returns
application downlink frame counter

Definition at line 710 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_get_jsenckey()

static uint8_t * gnrc_lorawan_get_jsenckey ( const gnrc_lorawan_t mac)
inlinestatic

Get the join session encryption key from the MAC descriptor.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
Returns
pointer to join session encryption key

Definition at line 691 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_get_jsintkey()

static uint8_t * gnrc_lorawan_get_jsintkey ( const gnrc_lorawan_t mac)
inlinestatic

Get the join session integrity key from the MAC descriptor.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
Returns
pointer to join session integrity key

Definition at line 672 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_get_last_fcnt_down()

static uint32_t gnrc_lorawan_get_last_fcnt_down ( const gnrc_lorawan_t mac)
inlinestatic

Get the last downlink frame counter from the MAC descriptor.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor

Definition at line 747 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_mac_acquire()

static int gnrc_lorawan_mac_acquire ( gnrc_lorawan_t mac)
inlinestatic

Acquire the MAC layer.

Parameters
[in]macpointer to the MAC descriptor
Returns
true on success
false if MAC is already acquired

Definition at line 578 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_mac_release()

static void gnrc_lorawan_mac_release ( gnrc_lorawan_t mac)
inlinestatic

Release the MAC layer.

Parameters
[in]macpointer to the MAC descriptor

Definition at line 591 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_mcps_process_downlink()

void gnrc_lorawan_mcps_process_downlink ( gnrc_lorawan_t mac,
uint8_t *  psdu,
size_t  size 
)

Process an MCPS downlink message (confirmable or non comfirmable)

Parameters
[in]macpointer to the MAC descriptor
[in]psdupointer to the downlink PSDU
[in]sizesize of the PSDU

◆ gnrc_lorawan_mlme_backoff_expire_cb()

void gnrc_lorawan_mlme_backoff_expire_cb ( gnrc_lorawan_t mac)

MLME Backoff expiration tick.

   Must be called once an hour (right after calling @ref
   gnrc_lorawan_init) in order to maintain the Time On Air budget.
Parameters
[in]macpointer to the MAC descriptor

◆ gnrc_lorawan_mlme_join_get_appkey()

static uint8_t * gnrc_lorawan_mlme_join_get_appkey ( const mlme_lorawan_join_t mlme_join)
inlinestatic

Get the app key from the MLME join request data.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]mlme_joinpointer to the MLME join request data
Returns
pointer to the app key

Definition at line 784 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_mlme_join_set_appkey()

static void gnrc_lorawan_mlme_join_set_appkey ( mlme_lorawan_join_t mlme_join,
uint8_t *  key 
)
inlinestatic

Se the app key in the MLME join request data.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]mlme_joinpointer to the MLME join request data
[in]keypointer to the app key

Definition at line 804 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_mlme_no_rx()

void gnrc_lorawan_mlme_no_rx ( gnrc_lorawan_t mac)

Inform the MAC layer that no packet was received during reception.

   To be called when the radio reports "NO RX" after the second reception
   window
Parameters
[in]macpointer to the MAC descriptor

◆ gnrc_lorawan_mlme_process_join()

void gnrc_lorawan_mlme_process_join ( gnrc_lorawan_t mac,
uint8_t *  data,
size_t  size 
)

Process join accept message.

Parameters
[in]macpointer to the MAC descriptor
[in]datathe Join Accept packet
[in]sizesize of the Join Accept packet

◆ gnrc_lorawan_open_rx_window()

void gnrc_lorawan_open_rx_window ( gnrc_lorawan_t mac)

Open a reception window.

   This is called by the MAC layer on timeout event.
Parameters
[in]macpointer to the MAC descriptor

◆ gnrc_lorawan_optneg_is_set()

static bool gnrc_lorawan_optneg_is_set ( const gnrc_lorawan_t mac)
inlinestatic

Check whether OptNeg bit is set.

When using LoRaWAN 1.1x the OptNeg bit indicates whether the Network Server implements the LoRaWAN1.0 protocol version (unset) or 1.1 and later (set).

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
Returns
true if OptNeg bit is set
false if OptNeg bit is unset

Definition at line 631 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_perform_save()

void gnrc_lorawan_perform_save ( gnrc_lorawan_t mac)

save internal MAC state in non-volatile storage and shutdown the MAC layer gracefully.

Parameters
mac

◆ gnrc_lorawan_pick_channel()

uint8_t gnrc_lorawan_pick_channel ( gnrc_lorawan_t mac)

pick a random available LoRaWAN channel

Parameters
[in]macpointer to the MAC descriptor
Returns
index of free channel inside channel array

◆ gnrc_lorawan_process_fopts()

void gnrc_lorawan_process_fopts ( gnrc_lorawan_t mac,
uint8_t *  fopts,
size_t  size 
)

Process an fopts frame.

Parameters
[in]macpointer to MAC descriptor
[in]foptspointer to fopts frame
[in]sizesize of fopts frame

◆ gnrc_lorawan_process_pkt()

void gnrc_lorawan_process_pkt ( gnrc_lorawan_t mac,
iolist_t pkt 
)

Process and dispatch a full LoRaWAN packet.

   Intended to be called right after reception from the radio
Parameters
[in]macpointer to the MAC descriptor
[in]pktthe received packet

◆ gnrc_lorawan_region_mac_payload_max()

uint8_t gnrc_lorawan_region_mac_payload_max ( uint8_t  datarate)

Get the maximum MAC payload (M value) for a given datarate.

Note
This function is region specific
Parameters
[in]dataratedatarate
Returns
the maximum allowed size of the packet

◆ gnrc_lorawan_reset()

void gnrc_lorawan_reset ( gnrc_lorawan_t mac)

Reset MAC parameters.

Note
This doesn't affect backoff timers variables.
Parameters
[in]macpointer to the MAC layer

◆ gnrc_lorawan_send_pkt()

void gnrc_lorawan_send_pkt ( gnrc_lorawan_t mac,
iolist_t psdu,
uint8_t  dr,
uint32_t  chan 
)

Send a LoRaWAN packet.

Parameters
[in]macpointer to the MAC descriptor
[in]psduthe psdu frame to be sent
[in]drthe datarate used for the transmission
[in]chanthe channel used for transmission

◆ gnrc_lorawan_set_afcnt_down()

static void gnrc_lorawan_set_afcnt_down ( gnrc_lorawan_t mac,
uint32_t  afcnt_down 
)
inlinestatic

Set the application downlink frame counter in the MAC descriptor.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
[in]afcnt_downapplication downlink frame counter

Definition at line 729 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_set_dr()

int gnrc_lorawan_set_dr ( gnrc_lorawan_t mac,
uint8_t  datarate 
)

Set datarate for the next transmission.

Parameters
[in]macpointer to the MAC descriptor
[in]dataratedesired datarate
Returns
0 on success
-EINVAL if datarate is not available in the current region

◆ gnrc_lorawan_set_last_fcnt_down()

static void gnrc_lorawan_set_last_fcnt_down ( gnrc_lorawan_t mac,
uint32_t  last_fcnt_down 
)
inlinestatic

Set the last downlink frame counter in the MAC descriptor.

This getter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
[in]last_fcnt_downlast downlink frame counter

Definition at line 766 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_set_optneg()

static void gnrc_lorawan_set_optneg ( gnrc_lorawan_t mac,
uint8_t  optneg 
)
inlinestatic

Set OptNeg bit in the MAC descriptor.

When using LoRaWAN 1.1x the OptNeg bit indicates whether the Network Server implements the LoRaWAN1.0 protocol version (unset) or 1.1 and later (set).

This setter function exists to allow if (IS_USED(...)) constructs in the LoRaWAN code in order to increase code coverage.

Parameters
[in]macpointer to the MAC descriptor
[in]optnegOptNeg bit

Definition at line 653 of file gnrc_lorawan_internal.h.

◆ gnrc_lorawan_set_rx2_dr()

void gnrc_lorawan_set_rx2_dr ( gnrc_lorawan_t mac,
uint8_t  rx2_dr 
)

Set the datarate of the second reception window.

Parameters
[in]macpointer to the MAC descriptor
[in]rx2_drdatarate of RX2

◆ gnrc_lorawan_store_dev_nonce()

void gnrc_lorawan_store_dev_nonce ( uint8_t *  dev_nonce)

Store DevNonce in flash memory.

Parameters
[in]dev_noncepointer to the DevNonce

◆ gnrc_lorawan_trigger_join()

void gnrc_lorawan_trigger_join ( gnrc_lorawan_t mac)

Trigger the transmission of the Join Request packet.

Parameters
[in]macpointer to the MAC descriptor