Skip to content

Commit 4ba5a65

Browse files
committed
examples: lora: Store the LoRa keys in the K/V store
This modifies the LoRaWAN example to use keys set in Tock's K/V store. The `lorawan-set-keys` application is added to set keys, which can then be used over and over again by the LoRaWAN transmit application. Signed-off-by: Alistair Francis <[email protected]>
1 parent ae70ee6 commit 4ba5a65

File tree

8 files changed

+358
-16
lines changed

8 files changed

+358
-16
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
build/
22
.vscode/
3-
examples/lora/sensor-lorawan/radioConfig.h
3+
examples/lora/*/radioConfig.h
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../
5+
6+
# Which files to compile.
7+
CXX_SRCS := $(wildcard *.cc)
8+
9+
override CPPFLAGS += -DRADIOLIB_CLOCK_DRIFT_MS=9
10+
11+
ifneq ($(CI),)
12+
override CPPFLAGS += "-DRADIO_CONFIG_CI=radioConfig_example.h"
13+
endif
14+
15+
ELF2TAB_ARGS += --write_id 17767 --read_ids 17767 --access_ids 17767
16+
17+
# Use the libtock-c Make system
18+
EXTERN_LIBS := $(TOCK_USERLAND_BASE_DIR)/libradio/RadioLib
19+
include $(TOCK_USERLAND_BASE_DIR)/libradio/Makefile
20+
21+
# `EXTERN_LIBS` points to `libradio/RadioLib` so we pull in `Makefile.app`
22+
# manually.
23+
include $(TOCK_USERLAND_BASE_DIR)/libradio/Makefile.app
24+
25+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
LoRaWAN Set Keys
2+
================
3+
4+
This will set the keys and secrets for LoRaWAN. Copy the `radioConfig_example.h`
5+
and call it `radioConfig.h`. Set the values based on the values from your LoRaWAN
6+
gateway. Then flash this application. That will set the keys in flash on the board.
7+
8+
After that the keys will be retrieved when running the LoRaWAN examples.
+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
RadioLib Non-Arduino Tock Library LoRaWAN test application
3+
4+
Licensed under the MIT or Apache License
5+
6+
Copyright (c) 2023 Alistair Francis <[email protected]>
7+
*/
8+
9+
#include <cinttypes>
10+
#include <stdlib.h>
11+
12+
// include the library
13+
#include <RadioLib.h>
14+
15+
// Include some libtock-c helpers
16+
#include <libtock-sync/storage/kv.h>
17+
18+
// To get this working copy radioConfig_example.h to radioConfig.h
19+
// and then modify it to match the LoRaWAN gateway settings.
20+
#ifdef RADIO_CONFIG_CI
21+
#include "radioConfig_example.h"
22+
#else
23+
#include "radioConfig.h"
24+
#endif
25+
26+
#define JOIN_EUI_KEY_LEN 8
27+
uint8_t join_eui_key_buf[JOIN_EUI_KEY_LEN] = "joinEUI";
28+
29+
#define DEV_EUI_KEY_LEN 7
30+
uint8_t dev_eui_key_buf[DEV_EUI_KEY_LEN] = "devEUI";
31+
32+
#define NWK_KEY_KEY_LEN 7
33+
uint8_t nwk_key_key_buf[NWK_KEY_KEY_LEN] = "nwkKey";
34+
35+
#define APP_KEY_KEY_LEN 7
36+
uint8_t app_key_key_buf[APP_KEY_KEY_LEN] = "appKey";
37+
38+
#define KV_DATA_LEN 8
39+
uint8_t kv_data_buf[KV_DATA_LEN];
40+
41+
// Store the joinEUI to the Tock K/V store
42+
static int set_join_eui(void) {
43+
returncode_t ret;
44+
45+
if (!libtock_kv_exists()) {
46+
return 1;
47+
}
48+
49+
kv_data_buf[0] = joinEUI & 0xFF;
50+
kv_data_buf[1] = (joinEUI >> 8) & 0xFF;
51+
kv_data_buf[2] = (joinEUI >> 16) & 0xFF;
52+
kv_data_buf[3] = (joinEUI >> 24) & 0xFF;
53+
kv_data_buf[4] = (joinEUI >> 32) & 0xFF;
54+
kv_data_buf[5] = (joinEUI >> 40) & 0xFF;
55+
kv_data_buf[6] = (joinEUI >> 48) & 0xFF;
56+
kv_data_buf[7] = (joinEUI >> 56) & 0xFF;
57+
58+
ret = libtocksync_kv_set(join_eui_key_buf, JOIN_EUI_KEY_LEN, kv_data_buf, KV_DATA_LEN);
59+
60+
if (ret == RETURNCODE_SUCCESS) {
61+
return 0;
62+
} else {
63+
return 1;
64+
}
65+
}
66+
67+
// Store the devEUI to the Tock K/V store
68+
static int set_dev_eui(void) {
69+
returncode_t ret;
70+
71+
if (!libtock_kv_exists()) {
72+
return 1;
73+
}
74+
75+
kv_data_buf[0] = devEUI & 0xFF;
76+
kv_data_buf[1] = (devEUI >> 8) & 0xFF;
77+
kv_data_buf[2] = (devEUI >> 16) & 0xFF;
78+
kv_data_buf[3] = (devEUI >> 24) & 0xFF;
79+
kv_data_buf[4] = (devEUI >> 32) & 0xFF;
80+
kv_data_buf[5] = (devEUI >> 40) & 0xFF;
81+
kv_data_buf[6] = (devEUI >> 48) & 0xFF;
82+
kv_data_buf[7] = (devEUI >> 56) & 0xFF;
83+
84+
ret = libtocksync_kv_set(dev_eui_key_buf, DEV_EUI_KEY_LEN, kv_data_buf, KV_DATA_LEN);
85+
86+
if (ret == RETURNCODE_SUCCESS) {
87+
return 0;
88+
} else {
89+
return 1;
90+
}
91+
}
92+
93+
// Store the nwkKey to the Tock K/V store
94+
static int set_nwk_key(void) {
95+
returncode_t ret;
96+
97+
if (!libtock_kv_exists()) {
98+
return 1;
99+
}
100+
101+
ret = libtocksync_kv_set(nwk_key_key_buf, NWK_KEY_KEY_LEN, nwkKey, 16);
102+
103+
if (ret == RETURNCODE_SUCCESS) {
104+
return 0;
105+
} else {
106+
return 1;
107+
}
108+
}
109+
110+
// Store the appKey to the Tock K/V store
111+
static int set_app_key(void) {
112+
returncode_t ret;
113+
114+
if (!libtock_kv_exists()) {
115+
return 1;
116+
}
117+
118+
ret = libtocksync_kv_set(app_key_key_buf, APP_KEY_KEY_LEN, appKey, 16);
119+
120+
if (ret == RETURNCODE_SUCCESS) {
121+
return 0;
122+
} else {
123+
return 1;
124+
}
125+
}
126+
127+
// the entry point for the program
128+
int main(void) {
129+
if (set_join_eui() == 0) {
130+
printf("Set joinEUI key to storage: 0x%lx%lx\r\n",
131+
(uint32_t)(joinEUI >> 32), (uint32_t)joinEUI);
132+
} else {
133+
printf("Unable to store joinEUI key to storage\r\n");
134+
return 1;
135+
}
136+
137+
if (set_dev_eui() == 0) {
138+
printf("Set devEUI key to storage: 0x%lx%lx\r\n",
139+
(uint32_t)(devEUI >> 32), (uint32_t)devEUI);
140+
} else {
141+
printf("Unable to store devEUI key to storage\r\n");
142+
return 1;
143+
}
144+
145+
if (set_nwk_key() == 0) {
146+
printf("Set nwkKey key to storage\r\n");
147+
} else {
148+
printf("Unable to store nwkKey to storage\r\n");
149+
return 1;
150+
}
151+
152+
if (set_app_key() == 0) {
153+
printf("Set appKey key to storage\r\n");
154+
} else {
155+
printf("Unable to store appKey to storage\r\n");
156+
return 1;
157+
}
158+
159+
return 0;
160+
}

examples/lora/sensor-lorawan/Makefile

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ CXX_SRCS := $(wildcard *.cc)
1010

1111
override CPPFLAGS += -DRADIOLIB_CLOCK_DRIFT_MS=9
1212

13-
ifneq ($(CI),)
14-
override CPPFLAGS += "-DRADIO_CONFIG_CI=radioConfig_example.h"
15-
endif
13+
ELF2TAB_ARGS += --write_id 17767 --read_ids 17767 --access_ids 17767
1614

1715
# Use the libtock-c Make system
1816
EXTERN_LIBS := $(TOCK_USERLAND_BASE_DIR)/libradio/RadioLib

examples/lora/sensor-lorawan/README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ This example builds an application to transmit sensor data via LoRaWAN.
66
See https://github.com/jgromes/RadioLib/blob/master/examples/LoRaWAN/LoRaWAN_Starter/notes.md
77
for notes on setting up the LoRaWAN device.
88

9-
The most important part is creating a radioConfig.h file with the secrets
10-
from your LoRaWAN server and any country specific settings. There is an
11-
existing radioConfig_example.h which can be used as a useful starting point.
9+
The most important part is setting the secrets from your LoRaWAN server
10+
and any country specific settings.
11+
12+
To set the secrets first run the `lorawan-set-keys` example. That will set
13+
the keys in flash. Then everytime you run the sensor-lorwan application
14+
it will use those secrets.
1215

1316
This has been tested against The Things Network. Before changing settings
1417
make sure you consider regulatory duty cycles and TTN's Fair Usage Policy,

0 commit comments

Comments
 (0)