gnrc_lorawan_internal.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 HAW Hamburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
18 #ifndef GNRC_LORAWAN_INTERNAL_H
19 #define GNRC_LORAWAN_INTERNAL_H
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include "iolist.h"
24 #include "net/lora.h"
25 #include "net/lorawan/hdr.h"
26 #include "net/gnrc/pktbuf.h"
27 #include "xtimer.h"
28 #include "msg.h"
29 #include "net/netdev.h"
30 #include "net/netdev/layer.h"
31 #include "net/loramac.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define MSG_TYPE_TIMEOUT (0x3457)
38 #define MSG_TYPE_MCPS_ACK_TIMEOUT (0x3458)
39 #define MSG_TYPE_MLME_BACKOFF_EXPIRE (0x3459)
41 #define MTYPE_MASK 0xE0
42 #define MTYPE_JOIN_REQUEST 0x0
43 #define MTYPE_JOIN_ACCEPT 0x1
44 #define MTYPE_UNCNF_UPLINK 0x2
45 #define MTYPE_UNCNF_DOWNLINK 0x3
46 #define MTYPE_CNF_UPLINK 0x4
47 #define MTYPE_CNF_DOWNLINK 0x5
48 #define MTYPE_REJOIN_REQ 0x6
49 #define MTYPE_PROPIETARY 0x7
51 #define MAJOR_MASK 0x3
52 #define MAJOR_LRWAN_R1 0x0
54 #define JOIN_REQUEST_SIZE (23U)
55 #define MIC_SIZE (4U)
56 #define CFLIST_SIZE (16U)
58 #define GNRC_LORAWAN_MAX_CHANNELS (16U)
60 #define LORAWAN_STATE_IDLE (0)
61 #define LORAWAN_STATE_RX_1 (1)
62 #define LORAWAN_STATE_RX_2 (2)
63 #define LORAWAN_STATE_TX (3)
65 #define GNRC_LORAWAN_DIR_UPLINK (0U)
66 #define GNRC_LORAWAN_DIR_DOWNLINK (1U)
68 #define GNRC_LORAWAN_BACKOFF_WINDOW_TICK (3600000000LL)
70 #define GNRC_LORAWAN_BACKOFF_BUDGET_1 (36000000LL)
71 #define GNRC_LORAWAN_BACKOFF_BUDGET_2 (36000000LL)
72 #define GNRC_LORAWAN_BACKOFF_BUDGET_3 (8700000LL)
74 #define GNRC_LORAWAN_MLME_OPTS_LINK_CHECK_REQ (1 << 0)
76 #define GNRC_LORAWAN_CID_SIZE (1U)
77 #define GNRC_LORAWAN_CID_LINK_CHECK_ANS (0x02)
79 #define GNRC_LORAWAN_FOPT_LINK_CHECK_ANS_SIZE (3U)
81 #define GNRC_LORAWAN_JOIN_DELAY_U32_MASK (0x1FFFFF)
83 #define GNRC_LORAWAN_MAX_PAYLOAD_1 (59U)
84 #define GNRC_LORAWAN_MAX_PAYLOAD_2 (123U)
85 #define GNRC_LORAWAN_MAX_PAYLOAD_3 (250U)
87 #define GNRC_LORAWAN_CFLIST_ENTRY_SIZE (3U)
88 #define GNRC_LORAWAN_JOIN_ACCEPT_MAX_SIZE (33U)
90 #define GNRC_LORAWAN_BACKOFF_STATE_1 (0U)
91 #define GNRC_LORAWAN_BACKOFF_STATE_2 (1U)
92 #define GNRC_LORAWAN_BACKOFF_STATE_3 (2U)
94 #define GNRC_LORAWAN_BACKOFF_TIME_1 (1U)
95 #define GNRC_LORAWAN_BACKOFF_TIME_2 (10U)
96 #define GNRC_LORAWAN_BACKOFF_TIME_3 (24U)
98 #define GNRC_LORAWAN_APP_NONCE_SIZE (3U)
99 #define GNRC_LORAWAN_NET_ID_SIZE (3U)
100 #define GNRC_LORAWAN_DEV_NONCE_SIZE (2U)
102 #define GNRC_LORAWAN_FOPTS_MAX_SIZE (15U)
103 #define GNRC_LORAWAN_FPORT_SIZE (1U)
108 #define MHDR_MIC_BUF_SIZE (sizeof(lorawan_hdr_t) + \
109  GNRC_LORAWAN_FOPTS_MAX_SIZE + \
110  GNRC_LORAWAN_FPORT_SIZE + \
111  MIC_SIZE)
112 
116 typedef struct {
117  uint8_t *data;
118  uint8_t size;
119  uint8_t index;
121 
125 typedef struct {
126  void *deveui;
127  void *appeui;
128  void *appkey;
129  uint8_t dr;
131 
135 typedef struct {
136  uint8_t margin;
137  uint8_t num_gateways;
139 
143 typedef struct {
145  uint8_t port;
146  uint8_t dr;
147 } mcps_data_t;
148 
152 typedef struct {
153  uint32_t fcnt;
154  uint32_t fcnt_down;
156  int nb_trials;
159  char mhdr_mic[MHDR_MIC_BUF_SIZE];
161 
165 typedef struct {
168  uint8_t activation;
170  uint32_t nid;
171  int32_t backoff_budget;
172  uint8_t dev_nonce[2];
173  uint8_t backoff_state;
175 
178 typedef struct {
183  void *mlme_buf;
184  void *mcps_buf;
185  uint8_t *nwkskey;
186  uint8_t *appskey;
187  uint32_t channel[GNRC_LORAWAN_MAX_CHANNELS];
188  uint32_t toa;
189  int busy;
192  int state;
193  uint8_t dl_settings;
194  uint8_t rx_delay;
195  uint8_t dr_range[GNRC_LORAWAN_MAX_CHANNELS];
196  uint8_t last_dr;
198 
211  uint32_t fcnt, uint8_t dir,
212  const uint8_t *appskey);
213 
222 void gnrc_lorawan_decrypt_join_accept(const uint8_t *key, uint8_t *pkt,
223  int has_clist, uint8_t *out);
224 
237 void gnrc_lorawan_generate_session_keys(const uint8_t *app_nonce,
238  const uint8_t *dev_nonce,
239  const uint8_t *appkey, uint8_t *nwkskey,
240  uint8_t *appskey);
241 
251 int gnrc_lorawan_set_dr(gnrc_lorawan_t *mac, uint8_t datarate);
252 
265  int confirmed_data, uint8_t port);
266 
275 
286 
294 void gnrc_lorawan_process_fopts(gnrc_lorawan_t *mac, uint8_t *fopts,
295  size_t size);
296 
305 void gnrc_lorawan_calculate_join_mic(const uint8_t *buf, size_t len,
306  const uint8_t *key, le_uint32_t *out);
307 
318 void gnrc_lorawan_calculate_mic(const le_uint32_t *dev_addr, uint32_t fcnt,
319  uint8_t dir, iolist_t *frame,
320  const uint8_t *nwkskey, le_uint32_t *out);
333 size_t gnrc_lorawan_build_hdr(uint8_t mtype, le_uint32_t *dev_addr,
334  uint32_t fcnt, uint8_t ack, uint8_t fopts_length,
335  lorawan_buffer_t *buf);
336 
344 void gnrc_lorawan_mcps_process_downlink(gnrc_lorawan_t *mac, uint8_t *psdu,
345  size_t size);
346 
355 
364 
372 void gnrc_lorawan_send_pkt(gnrc_lorawan_t *mac, iolist_t *psdu, uint8_t dr);
373 
381 void gnrc_lorawan_mlme_process_join(gnrc_lorawan_t *mac, uint8_t *data,
382  size_t size);
383 
393 
400 
407 
417 uint8_t gnrc_lorawan_region_mac_payload_max(uint8_t datarate);
418 
427 
437 
446 
454 
464 {
465  int _c = mac->busy;
466 
467  mac->busy = true;
468  return !_c;
469 }
470 
476 static inline void gnrc_lorawan_mac_release(gnrc_lorawan_t *mac)
477 {
478  mac->busy = false;
479 }
480 
487 void gnrc_lorawan_set_rx2_dr(gnrc_lorawan_t *mac, uint8_t rx2_dr);
488 
489 #ifdef __cplusplus
490 }
491 #endif
492 
493 #endif /* GNRC_LORAWAN_INTERNAL_H */
494 
gnrc_lorawan_mcps_t::fcnt_down
uint32_t fcnt_down
downlink frame counter
Definition: gnrc_lorawan_internal.h:154
gnrc_lorawan_t::msg
msg_t msg
MAC layer message descriptor.
Definition: gnrc_lorawan_internal.h:180
le_uint32_t
A 32 bit integer in little endian.
Definition: byteorder.h:51
gnrc_lorawan_send_pkt
void gnrc_lorawan_send_pkt(gnrc_lorawan_t *mac, iolist_t *psdu, uint8_t dr)
Send a LoRaWAN packet.
xtimer
xtimer timer structure
Definition: xtimer.h:81
mcps_data_t::pkt
iolist_t * pkt
packet of the request
Definition: gnrc_lorawan_internal.h:144
gnrc_lorawan_mlme_backoff_expire
void gnrc_lorawan_mlme_backoff_expire(gnrc_lorawan_t *mac)
MLME Backoff expiration tick.
mlme_lorawan_join_t::deveui
void * deveui
pointer to the Device EUI
Definition: gnrc_lorawan_internal.h:126
gnrc_lorawan_mlme_t::backoff_budget
int32_t backoff_budget
remaining Time On Air budget
Definition: gnrc_lorawan_internal.h:171
mlme_lorawan_join_t::appeui
void * appeui
pointer to the Application EUI
Definition: gnrc_lorawan_internal.h:127
gnrc_lorawan_mcps_t::fcnt
uint32_t fcnt
uplink framecounter
Definition: gnrc_lorawan_internal.h:153
gnrc_lorawan_t::rx
xtimer_t rx
RX timer.
Definition: gnrc_lorawan_internal.h:179
gnrc_lorawan_t::rx_delay
uint8_t rx_delay
Delay of first reception window.
Definition: gnrc_lorawan_internal.h:194
gnrc_lorawan_event_ack_timeout
void gnrc_lorawan_event_ack_timeout(gnrc_lorawan_t *mac)
Mac callback for ACK timeout event.
lorawan_buffer_t::size
uint8_t size
size of the buffer
Definition: gnrc_lorawan_internal.h:118
loramac.h
LoRaMAC header definitions.
gnrc_lorawan_t
GNRC LoRaWAN mac descriptor.
Definition: gnrc_lorawan_internal.h:178
gnrc_lorawan_mcps_t::waiting_for_ack
int waiting_for_ack
true if the MAC layer is waiting for an ACK
Definition: gnrc_lorawan_internal.h:158
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.
gnrc_lorawan_channels_init
void gnrc_lorawan_channels_init(gnrc_lorawan_t *mac)
Init regional channel settings.
layer.h
Netdev layer helper functions.
gnrc_lorawan_calculate_mic
void gnrc_lorawan_calculate_mic(const le_uint32_t *dev_addr, uint32_t fcnt, uint8_t dir, iolist_t *frame, const uint8_t *nwkskey, le_uint32_t *out)
Calculate Message Integrity Code for a MCPS message.
gnrc_lorawan_t::nwkskey
uint8_t * nwkskey
pointer to Network SKey buffer
Definition: gnrc_lorawan_internal.h:185
gnrc_lorawan_t::mlme
gnrc_lorawan_mlme_t mlme
MLME descriptor.
Definition: gnrc_lorawan_internal.h:182
mlme_lorawan_join_t::appkey
void * appkey
pointer to the Application Key
Definition: gnrc_lorawan_internal.h:128
msg.h
Messaging API for inter process communication.
gnrc_lorawan_t::last_dr
uint8_t last_dr
datarate of the last transmission
Definition: gnrc_lorawan_internal.h:196
gnrc_lorawan_mlme_t::backoff_timer
xtimer_t backoff_timer
timer used for backoff expiration
Definition: gnrc_lorawan_internal.h:166
gnrc_lorawan_t::mcps
gnrc_lorawan_mcps_t mcps
MCPS descriptor.
Definition: gnrc_lorawan_internal.h:181
mlme_lorawan_join_t
MLME Join Request data.
Definition: gnrc_lorawan_internal.h:125
gnrc_lorawan_t::mlme_buf
void * mlme_buf
pointer to MLME buffer
Definition: gnrc_lorawan_internal.h:183
iolist.h
iolist scatter / gather IO
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.
gnrc_lorawan_t::shutdown_req
int shutdown_req
MAC Shutdown request.
Definition: gnrc_lorawan_internal.h:190
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.
gnrc_lorawan_process_fopts
void gnrc_lorawan_process_fopts(gnrc_lorawan_t *mac, uint8_t *fopts, size_t size)
Process an fopts frame.
gnrc_lorawan_mac_acquire
static int gnrc_lorawan_mac_acquire(gnrc_lorawan_t *mac)
Acquire the MAC layer.
Definition: gnrc_lorawan_internal.h:463
gnrc_lorawan_mlme_t::backoff_state
uint8_t backoff_state
state in the backoff state machine
Definition: gnrc_lorawan_internal.h:173
gnrc_lorawan_build_options
uint8_t gnrc_lorawan_build_options(gnrc_lorawan_t *mac, lorawan_buffer_t *buf)
Build fopts header.
gnrc_lorawan_calculate_join_mic
void gnrc_lorawan_calculate_join_mic(const uint8_t *buf, size_t len, const uint8_t *key, le_uint32_t *out)
calculate join Message Integrity Code
mcps_data_t::port
uint8_t port
port of the request
Definition: gnrc_lorawan_internal.h:145
gnrc_lorawan_mcps_t::nb_trials
int nb_trials
holds the remaining number of retransmissions
Definition: gnrc_lorawan_internal.h:156
gnrc_lorawan_process_pkt
void gnrc_lorawan_process_pkt(gnrc_lorawan_t *mac, iolist_t *pkt)
Process and dispatch a full LoRaWAN packet.
gnrc_lorawan_t::busy
int busy
MAC busy
Definition: gnrc_lorawan_internal.h:189
netdev.h
Definitions low-level network driver interface.
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)
gnrc_lorawan_mac_release
static void gnrc_lorawan_mac_release(gnrc_lorawan_t *mac)
Release the MAC layer.
Definition: gnrc_lorawan_internal.h:476
gnrc_lorawan_t::dev_addr
le_uint32_t dev_addr
Device address.
Definition: gnrc_lorawan_internal.h:191
gnrc_lorawan_mlme_t::backoff_msg
msg_t backoff_msg
msg for backoff expiration
Definition: gnrc_lorawan_internal.h:167
gnrc_lorawan_pick_channel
uint32_t gnrc_lorawan_pick_channel(gnrc_lorawan_t *mac)
pick a random available LoRaWAN channel
gnrc_lorawan_mcps_t
MCPS service access point descriptor.
Definition: gnrc_lorawan_internal.h:152
iolist
iolist structure definition
Definition: iolist.h:39
lorawan_buffer_t
buffer helper for parsing and constructing LoRaWAN packets.
Definition: gnrc_lorawan_internal.h:116
gnrc_lorawan_t::state
int state
state of MAC layer
Definition: gnrc_lorawan_internal.h:192
gnrc_lorawan_t::mcps_buf
void * mcps_buf
pointer to MCPS buffer
Definition: gnrc_lorawan_internal.h:184
lorawan_buffer_t::index
uint8_t index
current inxed in the buffer
Definition: gnrc_lorawan_internal.h:119
gnrc_lorawan_event_no_rx
void gnrc_lorawan_event_no_rx(gnrc_lorawan_t *mac)
Mac callback for no RX.
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.
gnrc_lorawan_t::appskey
uint8_t * appskey
pointer to Application SKey buffer
Definition: gnrc_lorawan_internal.h:186
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.
pktbuf.h
Interface definition for the global network buffer. Network devices and layers can allocate space for...
MHDR_MIC_BUF_SIZE
#define MHDR_MIC_BUF_SIZE
Size of the internal MHDR-MIC buffer.
Definition: gnrc_lorawan_internal.h:108
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.
gnrc_lorawan_mlme_t
MLME service access point descriptor.
Definition: gnrc_lorawan_internal.h:165
mlme_lorawan_join_t::dr
uint8_t dr
datarate for the Join Request
Definition: gnrc_lorawan_internal.h:129
lorawan_buffer_t::data
uint8_t * data
pointer to the beginning of the buffer holding data
Definition: gnrc_lorawan_internal.h:117
gnrc_lorawan_open_rx_window
void gnrc_lorawan_open_rx_window(gnrc_lorawan_t *mac)
Open a reception window.
gnrc_lorawan_t::toa
uint32_t toa
Time on Air of the last transmission.
Definition: gnrc_lorawan_internal.h:188
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
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
GNRC_LORAWAN_MAX_CHANNELS
#define GNRC_LORAWAN_MAX_CHANNELS
Maximum number of channels.
Definition: gnrc_lorawan_internal.h:58
gnrc_lorawan_reset
void gnrc_lorawan_reset(gnrc_lorawan_t *mac)
Reset MAC parameters.
hdr.h
LoRaWAN header type and helper function definitions.
gnrc_lorawan_mcps_t::ack_requested
int ack_requested
whether the network server requested an ACK
Definition: gnrc_lorawan_internal.h:157
mcps_data_t::dr
uint8_t dr
datarate of the request
Definition: gnrc_lorawan_internal.h:146
gnrc_lorawan_set_dr
int gnrc_lorawan_set_dr(gnrc_lorawan_t *mac, uint8_t datarate)
Set datarate for the next transmission.
gnrc_lorawan_mlme_t::nid
uint32_t nid
current Network ID
Definition: gnrc_lorawan_internal.h:170
gnrc_lorawan_t::dl_settings
uint8_t dl_settings
downlink settings
Definition: gnrc_lorawan_internal.h:193
gnrc_lorawan_generate_session_keys
void gnrc_lorawan_generate_session_keys(const uint8_t *app_nonce, const uint8_t *dev_nonce, const uint8_t *appkey, uint8_t *nwkskey, uint8_t *appskey)
Generate LoRaWAN session keys.
gnrc_lorawan_mlme_t::pending_mlme_opts
int pending_mlme_opts
holds pending mlme opts
Definition: gnrc_lorawan_internal.h:169
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.
xtimer.h
xtimer interface definitions
gnrc_lorawan_mcps_t::msdu
iolist_t * msdu
current MSDU
Definition: gnrc_lorawan_internal.h:155
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.
lora.h
LoRa modulation header definitions.
gnrc_lorawan_mlme_t::activation
uint8_t activation
Activation mechanism of the MAC layer.
Definition: gnrc_lorawan_internal.h:168
mcps_data_t
MCPS data.
Definition: gnrc_lorawan_internal.h:143