-
Notifications
You must be signed in to change notification settings - Fork 2k
/
main.c
199 lines (166 loc) · 5.19 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (C) 2018 Inria
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief Example demonstrating the use of LoRaWAN with RIOT
*
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "msg.h"
#include "thread.h"
#include "fmt.h"
#include "periph/pm.h"
#if IS_USED(MODULE_PERIPH_RTC)
#include "periph/rtc.h"
#else
#include "timex.h"
#include "ztimer.h"
#endif
#include "net/loramac.h"
#include "semtech_loramac.h"
/* By default, messages are sent every 20s to respect the duty cycle
on each channel */
#ifndef SEND_PERIOD_S
#define SEND_PERIOD_S (20U)
#endif
/* Low-power mode level */
#define PM_LOCK_LEVEL (1)
#define SENDER_PRIO (THREAD_PRIORITY_MAIN - 1)
static kernel_pid_t sender_pid;
static char sender_stack[THREAD_STACKSIZE_MAIN / 2];
extern semtech_loramac_t loramac;
#if !IS_USED(MODULE_PERIPH_RTC)
static ztimer_t timer;
#endif
static const char *message = "This is RIOT!";
#ifdef USE_OTAA
static uint8_t deveui[LORAMAC_DEVEUI_LEN];
static uint8_t appeui[LORAMAC_APPEUI_LEN];
static uint8_t appkey[LORAMAC_APPKEY_LEN];
#endif
#ifdef USE_ABP
static uint8_t devaddr[LORAMAC_DEVADDR_LEN];
static uint8_t nwkskey[LORAMAC_NWKSKEY_LEN];
static uint8_t appskey[LORAMAC_APPSKEY_LEN];
#endif
static void _alarm_cb(void *arg)
{
(void)arg;
msg_t msg;
msg_send(&msg, sender_pid);
}
static void _prepare_next_alarm(void)
{
#if IS_USED(MODULE_PERIPH_RTC)
struct tm time;
rtc_get_time(&time);
/* set initial alarm */
time.tm_sec += SEND_PERIOD_S;
mktime(&time);
rtc_set_alarm(&time, _alarm_cb, NULL);
#else
timer.callback = _alarm_cb;
ztimer_set(ZTIMER_MSEC, &timer, SEND_PERIOD_S * MS_PER_SEC);
#endif
}
static void _send_message(void)
{
printf("Sending: %s\n", message);
/* Try to send the message */
uint8_t ret = semtech_loramac_send(&loramac,
(uint8_t *)message, strlen(message));
if (ret != SEMTECH_LORAMAC_TX_DONE) {
printf("Cannot send message '%s', ret code: %d\n", message, ret);
return;
}
}
static void *sender(void *arg)
{
(void)arg;
msg_t msg;
msg_t msg_queue[8];
msg_init_queue(msg_queue, 8);
while (1) {
msg_receive(&msg);
/* Trigger the message send */
_send_message();
/* Schedule the next wake-up alarm */
_prepare_next_alarm();
}
/* this should never be reached */
return NULL;
}
int main(void)
{
puts("LoRaWAN Class A low-power application");
puts("=====================================");
#ifdef USE_OTAA /* OTAA activation mode */
/* Convert identifiers and keys strings to byte arrays */
fmt_hex_bytes(deveui, CONFIG_LORAMAC_DEV_EUI_DEFAULT);
fmt_hex_bytes(appeui, CONFIG_LORAMAC_APP_EUI_DEFAULT);
fmt_hex_bytes(appkey, CONFIG_LORAMAC_APP_KEY_DEFAULT);
semtech_loramac_set_deveui(&loramac, deveui);
semtech_loramac_set_appeui(&loramac, appeui);
semtech_loramac_set_appkey(&loramac, appkey);
/* Use a fast datarate, e.g. BW125/SF7 in EU868 */
semtech_loramac_set_dr(&loramac, LORAMAC_DR_5);
/* Join the network if not already joined */
if (!semtech_loramac_is_mac_joined(&loramac)) {
/* Start the Over-The-Air Activation (OTAA) procedure to retrieve the
* generated device address and to get the network and application session
* keys.
*/
puts("Starting join procedure");
if (semtech_loramac_join(&loramac, LORAMAC_JOIN_OTAA) != SEMTECH_LORAMAC_JOIN_SUCCEEDED) {
puts("Join procedure failed");
return 1;
}
#ifdef MODULE_PERIPH_EEPROM
/* Save current MAC state to EEPROM */
semtech_loramac_save_config(&loramac);
#endif
}
#endif
#ifdef USE_ABP /* ABP activation mode */
/* Convert identifiers and keys strings to byte arrays */
fmt_hex_bytes(devaddr, CONFIG_LORAMAC_DEV_ADDR_DEFAULT);
fmt_hex_bytes(nwkskey, CONFIG_LORAMAC_NWK_SKEY_DEFAULT);
fmt_hex_bytes(appskey, CONFIG_LORAMAC_APP_SKEY_DEFAULT);
semtech_loramac_set_devaddr(&loramac, devaddr);
semtech_loramac_set_nwkskey(&loramac, nwkskey);
semtech_loramac_set_appskey(&loramac, appskey);
/* Configure RX2 parameters */
semtech_loramac_set_rx2_freq(&loramac, CONFIG_LORAMAC_DEFAULT_RX2_FREQ);
semtech_loramac_set_rx2_dr(&loramac, CONFIG_LORAMAC_DEFAULT_RX2_DR);
#ifdef MODULE_PERIPH_EEPROM
/* Store ABP parameters to EEPROM */
semtech_loramac_save_config(&loramac);
#endif
/* Use a fast datarate, e.g. BW125/SF7 in EU868 */
semtech_loramac_set_dr(&loramac, LORAMAC_DR_5);
/* ABP join procedure always succeeds */
semtech_loramac_join(&loramac, LORAMAC_JOIN_ABP);
#endif
puts("Join procedure succeeded");
/* start the sender thread */
sender_pid = thread_create(sender_stack, sizeof(sender_stack),
SENDER_PRIO, 0, sender, NULL, "sender");
/* trigger the first send */
msg_t msg;
msg_send(&msg, sender_pid);
return 0;
}