semtech_loramac.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Inria
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 
19 #ifndef SEMTECH_LORAMAC_H
20 #define SEMTECH_LORAMAC_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <inttypes.h>
27 
28 #include "mutex.h"
29 
30 #include "net/netdev.h"
31 #include "net/loramac.h"
32 
33 #include "sx127x.h"
34 
39 #define MSG_TYPE_ISR (0x3456)
40 #define MSG_TYPE_RX_TIMEOUT (0x3457)
41 #define MSG_TYPE_TX_TIMEOUT (0x3458)
42 #define MSG_TYPE_MAC_TIMEOUT (0x3459)
43 #define MSG_TYPE_LORAMAC_CMD (0x3460)
44 #define MSG_TYPE_LORAMAC_JOIN_STATUS (0x3461)
45 #define MSG_TYPE_LORAMAC_TX_STATUS (0x3462)
46 #define MSG_TYPE_LORAMAC_MLME_CONFIRM (0x3463)
47 #define MSG_TYPE_LORAMAC_MLME_INDICATION (0x3464)
48 #define MSG_TYPE_LORAMAC_MCPS_CONFIRM (0x3465)
49 #define MSG_TYPE_LORAMAC_MCPS_INDICATION (0x3466)
55 #define LORAWAN_APP_DATA_MAX_SIZE (242U)
56 
60 enum {
75 };
76 
80 enum {
81  SEMTECH_LORAMAC_STATE_IDLE = 0,
82  SEMTECH_LORAMAC_STATE_BUSY
83 };
84 
88 typedef struct {
89  uint32_t frequency;
90  uint8_t datarate;
92 
96 typedef struct {
97  uint8_t payload[LORAWAN_APP_DATA_MAX_SIZE];
98  uint8_t payload_len;
99  uint8_t port;
101 
102 #if defined(MODULE_SEMTECH_LORAMAC_RX) || DOXYGEN
103 
106 typedef struct {
107  uint8_t demod_margin;
108  uint8_t nb_gateways;
110 #endif
111 
115 typedef struct {
117  uint8_t tx_pid;
118 #if defined(MODULE_SEMTECH_LORAMAC_RX) || DOXYGEN
119  uint8_t rx_pid;
120 #endif
121  uint8_t port;
122  uint8_t cnf;
123  uint8_t deveui[LORAMAC_DEVEUI_LEN];
124  uint8_t appeui[LORAMAC_APPEUI_LEN];
125  uint8_t appkey[LORAMAC_APPKEY_LEN];
126 #if defined(MODULE_SEMTECH_LORAMAC_RX) || DOXYGEN
129 #endif
131 
141 
155 uint8_t semtech_loramac_join(semtech_loramac_t *mac, uint8_t type);
156 
178 uint8_t semtech_loramac_send(semtech_loramac_t *mac, uint8_t *data, uint8_t len);
179 
180 #if defined(MODULE_SEMTECH_LORAMAC_RX) || DOXYGEN
181 
205 #endif
206 
215 
216 #if defined(MODULE_SEMTECH_LORAMAC_RX) || DOXYGEN
217 
226 #endif
227 
234 void semtech_loramac_set_deveui(semtech_loramac_t *mac, const uint8_t *eui);
235 
242 void semtech_loramac_get_deveui(const semtech_loramac_t *mac, uint8_t *eui);
243 
250 void semtech_loramac_set_appeui(semtech_loramac_t *mac, const uint8_t *eui);
251 
258 void semtech_loramac_get_appeui(const semtech_loramac_t *mac, uint8_t *eui);
259 
266 void semtech_loramac_set_appkey(semtech_loramac_t *mac, const uint8_t *key);
267 
274 void semtech_loramac_get_appkey(const semtech_loramac_t *mac, uint8_t *key);
275 
282 void semtech_loramac_set_appskey(semtech_loramac_t *mac, const uint8_t *skey);
283 
290 void semtech_loramac_get_appskey(semtech_loramac_t *mac, uint8_t *skey);
291 
298 void semtech_loramac_set_nwkskey(semtech_loramac_t *mac, const uint8_t *skey);
299 
306 void semtech_loramac_get_nwkskey(semtech_loramac_t *mac, uint8_t *skey);
307 
314 void semtech_loramac_set_devaddr(semtech_loramac_t *mac, const uint8_t *addr);
315 
322 void semtech_loramac_get_devaddr(semtech_loramac_t *mac, uint8_t *addr);
323 
331 
339 
346 void semtech_loramac_set_dr(semtech_loramac_t *mac, uint8_t dr);
347 
355 
362 void semtech_loramac_set_adr(semtech_loramac_t *mac, bool adr);
363 
371 
379 
387 
394 void semtech_loramac_set_netid(semtech_loramac_t *mac, uint32_t netid);
395 
403 
410 void semtech_loramac_set_tx_power(semtech_loramac_t *mac, uint8_t power);
411 
419 
426 void semtech_loramac_set_tx_port(semtech_loramac_t *mac, uint8_t port);
427 
435 
442 void semtech_loramac_set_min_rx_symbols(semtech_loramac_t *mac, uint8_t min_rx);
443 
451 
458 void semtech_loramac_set_tx_mode(semtech_loramac_t *mac, uint8_t mode);
459 
467 
474 void semtech_loramac_set_rx2_freq(semtech_loramac_t *mac, uint32_t freq);
475 
483 
490 void semtech_loramac_set_rx2_dr(semtech_loramac_t *mac, uint8_t dr);
491 
499 
506 void semtech_loramac_set_uplink_counter(semtech_loramac_t *mac, uint32_t counter);
507 
515 
516 #ifdef MODULE_PERIPH_EEPROM
517 
520 #ifndef SEMTECH_LORAMAC_EEPROM_MAGIC
521 #define SEMTECH_LORAMAC_EEPROM_MAGIC {0x52, 0x49, 0x4F, 0x54} /* RIOT */
522 #endif
523 
527 #ifndef SEMTECH_LORAMAC_EEPROM_MAGIC_LEN
528 #define SEMTECH_LORAMAC_EEPROM_MAGIC_LEN 4
529 #endif
530 
534 #ifndef SEMTECH_LORAMAC_EEPROM_START
535 #define SEMTECH_LORAMAC_EEPROM_START (0)
536 #endif
537 
543 void semtech_loramac_save_config(semtech_loramac_t *mac);
544 
548 void semtech_loramac_erase_config(void);
549 #endif
550 
551 #ifdef __cplusplus
552 }
553 #endif
554 
555 #endif /* SEMTECH_LORAMAC_H */
556 
SEMTECH_LORAMAC_TX_SCHEDULE
@ SEMTECH_LORAMAC_TX_SCHEDULE
TX needs reschedule.
Definition: semtech_loramac.h:66
semtech_loramac_channel_params_t::datarate
uint8_t datarate
channel datarate
Definition: semtech_loramac.h:90
semtech_loramac_get_devaddr
void semtech_loramac_get_devaddr(semtech_loramac_t *mac, uint8_t *addr)
Gets the device address.
SEMTECH_LORAMAC_TX_OK
@ SEMTECH_LORAMAC_TX_OK
Transmission is in progress.
Definition: semtech_loramac.h:65
semtech_loramac_set_appskey
void semtech_loramac_set_appskey(semtech_loramac_t *mac, const uint8_t *skey)
Sets the application session key.
SEMTECH_LORAMAC_JOIN_FAILED
@ SEMTECH_LORAMAC_JOIN_FAILED
Join procedure failed.
Definition: semtech_loramac.h:62
SEMTECH_LORAMAC_RX_LINK_CHECK
@ SEMTECH_LORAMAC_RX_LINK_CHECK
Link check info received.
Definition: semtech_loramac.h:71
SEMTECH_LORAMAC_ALREADY_JOINED
@ SEMTECH_LORAMAC_ALREADY_JOINED
MAC is already joined.
Definition: semtech_loramac.h:64
LORAMAC_APPKEY_LEN
#define LORAMAC_APPKEY_LEN
Application key length in bytes.
Definition: loramac.h:320
semtech_loramac_set_devaddr
void semtech_loramac_set_devaddr(semtech_loramac_t *mac, const uint8_t *addr)
Sets the device address.
semtech_loramac_get_rx2_freq
uint32_t semtech_loramac_get_rx2_freq(semtech_loramac_t *mac)
Gets the RX2 frequency.
SEMTECH_LORAMAC_RX_CONFIRMED
@ SEMTECH_LORAMAC_RX_CONFIRMED
Confirmed ACK received.
Definition: semtech_loramac.h:72
SEMTECH_LORAMAC_TX_ERROR
@ SEMTECH_LORAMAC_TX_ERROR
Error in TX (invalid param, unknown service)
Definition: semtech_loramac.h:69
loramac.h
LoRaMAC header definitions.
semtech_loramac_t::rx_pid
uint8_t rx_pid
pid of receiver thread
Definition: semtech_loramac.h:119
semtech_loramac_set_class
void semtech_loramac_set_class(semtech_loramac_t *mac, loramac_class_t cls)
Sets the device class.
semtech_loramac_get_appskey
void semtech_loramac_get_appskey(semtech_loramac_t *mac, uint8_t *skey)
Gets the application session key.
semtech_loramac_get_netid
uint32_t semtech_loramac_get_netid(semtech_loramac_t *mac)
Gets the NetID.
semtech_loramac_get_dr
uint8_t semtech_loramac_get_dr(semtech_loramac_t *mac)
Gets the channels datarate.
semtech_loramac_t::cnf
uint8_t cnf
enable/disable confirmable messages
Definition: semtech_loramac.h:122
semtech_loramac_rx_data_t
Structure containing LoRaWAN RX data.
Definition: semtech_loramac.h:96
semtech_loramac_set_netid
void semtech_loramac_set_netid(semtech_loramac_t *mac, uint32_t netid)
Sets the NetID (only useful with ABP join procedure)
SEMTECH_LORAMAC_JOIN_SUCCEEDED
@ SEMTECH_LORAMAC_JOIN_SUCCEEDED
Join procedure succeeded.
Definition: semtech_loramac.h:61
semtech_loramac_set_system_max_rx_error
void semtech_loramac_set_system_max_rx_error(semtech_loramac_t *mac, uint32_t error)
Sets the maximum system overall timing error for RX (in ms)
semtech_loramac_is_mac_joined
bool semtech_loramac_is_mac_joined(semtech_loramac_t *mac)
Check if network is already joined.
semtech_loramac_set_tx_mode
void semtech_loramac_set_tx_mode(semtech_loramac_t *mac, uint8_t mode)
Sets the TX confirmable mode.
semtech_loramac_set_appkey
void semtech_loramac_set_appkey(semtech_loramac_t *mac, const uint8_t *key)
Sets the application key.
semtech_loramac_get_tx_port
uint8_t semtech_loramac_get_tx_port(semtech_loramac_t *mac)
Gets the TX application port.
SEMTECH_LORAMAC_TX_CNF_FAILED
@ SEMTECH_LORAMAC_TX_CNF_FAILED
Confirmable transmission failed.
Definition: semtech_loramac.h:68
SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED
@ SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED
Restricted access to channels.
Definition: semtech_loramac.h:74
semtech_loramac_t::tx_pid
uint8_t tx_pid
pid of sender thread
Definition: semtech_loramac.h:117
LORAWAN_APP_DATA_MAX_SIZE
#define LORAWAN_APP_DATA_MAX_SIZE
Maximum payload size of a LoRaWAN application data.
Definition: semtech_loramac.h:55
semtech_loramac_rx_data_t::port
uint8_t port
RX port.
Definition: semtech_loramac.h:99
SEMTECH_LORAMAC_NOT_JOINED
@ SEMTECH_LORAMAC_NOT_JOINED
MAC is not joined.
Definition: semtech_loramac.h:63
semtech_loramac_t::port
uint8_t port
application TX port
Definition: semtech_loramac.h:121
semtech_loramac_get_deveui
void semtech_loramac_get_deveui(const semtech_loramac_t *mac, uint8_t *eui)
Gets the device EUI.
semtech_loramac_get_class
loramac_class_t semtech_loramac_get_class(semtech_loramac_t *mac)
Gets the device class.
semtech_loramac_request_link_check
void semtech_loramac_request_link_check(semtech_loramac_t *mac)
Requests a LoRaWAN link check.
netdev.h
Definitions low-level network driver interface.
semtech_loramac_get_nwkskey
void semtech_loramac_get_nwkskey(semtech_loramac_t *mac, uint8_t *skey)
Gets the network session key.
semtech_loramac_set_uplink_counter
void semtech_loramac_set_uplink_counter(semtech_loramac_t *mac, uint32_t counter)
Sets the Uplink Frame Counter.
semtech_loramac_set_rx2_dr
void semtech_loramac_set_rx2_dr(semtech_loramac_t *mac, uint8_t dr)
Sets the RX2 datarate.
semtech_loramac_set_public_network
void semtech_loramac_set_public_network(semtech_loramac_t *mac, bool public)
Enable/disable the public network mode.
semtech_loramac_set_tx_power
void semtech_loramac_set_tx_power(semtech_loramac_t *mac, uint8_t power)
Sets the channels TX power index.
semtech_loramac_get_adr
bool semtech_loramac_get_adr(semtech_loramac_t *mac)
Checks if adaptive datarate is set.
semtech_loramac_set_deveui
void semtech_loramac_set_deveui(semtech_loramac_t *mac, const uint8_t *eui)
Sets the device EUI.
LORAMAC_APPEUI_LEN
#define LORAMAC_APPEUI_LEN
Application EUI length in bytes.
Definition: loramac.h:315
semtech_loramac_set_min_rx_symbols
void semtech_loramac_set_min_rx_symbols(semtech_loramac_t *mac, uint8_t min_rx)
Sets the minimum required number of symbols to detect a frame.
semtech_loramac_t
Semtech LoRaMAC descriptor.
Definition: semtech_loramac.h:115
semtech_loramac_get_appeui
void semtech_loramac_get_appeui(const semtech_loramac_t *mac, uint8_t *eui)
Gets the application EUI.
SEMTECH_LORAMAC_BUSY
@ SEMTECH_LORAMAC_BUSY
Internal MAC is busy.
Definition: semtech_loramac.h:73
semtech_loramac_channel_params_t::frequency
uint32_t frequency
channel center frequency
Definition: semtech_loramac.h:89
semtech_loramac_set_adr
void semtech_loramac_set_adr(semtech_loramac_t *mac, bool adr)
Enables/disable adaptive datarate.
semtech_loramac_set_dr
void semtech_loramac_set_dr(semtech_loramac_t *mac, uint8_t dr)
Sets the channels datarate.
semtech_loramac_set_appeui
void semtech_loramac_set_appeui(semtech_loramac_t *mac, const uint8_t *eui)
Sets the application EUI.
semtech_loramac_get_tx_power
uint8_t semtech_loramac_get_tx_power(semtech_loramac_t *mac)
Gets the channels TX power index.
loramac_class_t
loramac_class_t
Device class.
Definition: loramac.h:361
LORAMAC_DEVEUI_LEN
#define LORAMAC_DEVEUI_LEN
Device EUI length in bytes.
Definition: loramac.h:305
semtech_loramac_channel_params_t
LoRaMAC channel radio parameters.
Definition: semtech_loramac.h:88
semtech_loramac_get_public_network
bool semtech_loramac_get_public_network(semtech_loramac_t *mac)
Checks if public network is set.
mutex.h
Mutex for thread synchronization.
semtech_loramac_t::lock
mutex_t lock
loramac access lock
Definition: semtech_loramac.h:116
semtech_loramac_get_appkey
void semtech_loramac_get_appkey(const semtech_loramac_t *mac, uint8_t *key)
Gets the application key.
semtech_loramac_rx_data_t::payload_len
uint8_t payload_len
Length of the RX payload.
Definition: semtech_loramac.h:98
semtech_loramac_get_uplink_counter
uint32_t semtech_loramac_get_uplink_counter(semtech_loramac_t *mac)
Gets the Uplink Frame Counter.
semtech_loramac_t::link_chk
semtech_loramac_link_check_info_t link_chk
link check information
Definition: semtech_loramac.h:128
sx127x.h
Public interface for SX127X driver.
SEMTECH_LORAMAC_TX_DONE
@ SEMTECH_LORAMAC_TX_DONE
Transmission completed.
Definition: semtech_loramac.h:67
semtech_loramac_set_tx_port
void semtech_loramac_set_tx_port(semtech_loramac_t *mac, uint8_t port)
Sets the TX application port.
SEMTECH_LORAMAC_RX_DATA
@ SEMTECH_LORAMAC_RX_DATA
Data received.
Definition: semtech_loramac.h:70
semtech_loramac_t::rx_data
semtech_loramac_rx_data_t rx_data
struct handling the RX data
Definition: semtech_loramac.h:127
inttypes.h
Adds include for missing inttype definitions.
semtech_loramac_get_rx2_dr
uint8_t semtech_loramac_get_rx2_dr(semtech_loramac_t *mac)
Gets the RX2 datarate.
semtech_loramac_set_nwkskey
void semtech_loramac_set_nwkskey(semtech_loramac_t *mac, const uint8_t *skey)
Sets the network session key.
semtech_loramac_init
int semtech_loramac_init(semtech_loramac_t *mac)
Initializes the semtech loramac mac.
semtech_loramac_recv
uint8_t semtech_loramac_recv(semtech_loramac_t *mac)
Wait for a message sent by the LoRaWAN network.
mutex_t
Mutex structure.
Definition: mutex.h:120
semtech_loramac_join
uint8_t semtech_loramac_join(semtech_loramac_t *mac, uint8_t type)
Starts a LoRaWAN network join procedure.
semtech_loramac_send
uint8_t semtech_loramac_send(semtech_loramac_t *mac, uint8_t *data, uint8_t len)
Sends data to the LoRaWAN network.
semtech_loramac_get_tx_mode
uint8_t semtech_loramac_get_tx_mode(semtech_loramac_t *mac)
Gets the TX confirmable mode.
semtech_loramac_set_rx2_freq
void semtech_loramac_set_rx2_freq(semtech_loramac_t *mac, uint32_t freq)
Sets the RX2 frequency.