Skip to content

Commit dee7168

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 c0202f9 commit dee7168

File tree

8 files changed

+372
-17
lines changed

8 files changed

+372
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
radioConfig.h
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../..
5+
6+
STACK_SIZE = 4096
7+
8+
# Which files to compile.
9+
CXX_SRCS := $(wildcard *.cc)
10+
11+
override CPPFLAGS += -DRADIOLIB_CLOCK_DRIFT_MS=9
12+
13+
# If we are building for a testing configuration (e.g. CI) then it's okay to
14+
# use the example config header. However, if someone is doing local testing,
15+
# especially of a different thing, we don't want to accidentally overwrite
16+
# the build obeject here with the example config when a real one exists.
17+
ifneq ($(TOCK_BUILDALL),)
18+
ifeq ($(wildcard radioConfig.h),)
19+
override CPPFLAGS += "-DRADIO_CONFIG_CI=radioConfig_example.h"
20+
endif
21+
endif
22+
23+
ELF2TAB_ARGS += --write_id 2903764429 --read_ids 2903764429 --access_ids 2903764429
24+
25+
# Use the libtock-c Make system
26+
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/RadioLib
27+
28+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
29+
30+
# Protect from the (unlikely) case where the app happens to have been
31+
# built from an unrelated `build all` event, but now the user is trying
32+
# to flash the app with an invalid configuration.
33+
flash: radioConfig.h
34+
35+
program: radioConfig.h
36+
37+
install: radioConfig.h
+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/radioConfig_example.h renamed to examples/lora/lorawan-set-keys/radioConfig_example.h

-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@ uint64_t joinEUI = 0x0000000000000000;
1111
uint64_t devEUI = 0x0000000000000000;
1212
uint8_t appKey[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1313
uint8_t nwkKey[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
14-
15-
// regional choices: EU868, US915, AU915, AS923, IN865, KR920, CN780, CN500
16-
const LoRaWANBand_t* Region = &AU915;
17-
const uint8_t subBand = 2;

examples/lora/sensor-lorawan/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ override CPPFLAGS += "-DRADIO_CONFIG_CI=radioConfig_example.h"
2020
endif
2121
endif
2222

23+
ELF2TAB_ARGS += --read_ids 2903764429
24+
2325
# Use the libtock-c Make system
2426
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/RadioLib
2527

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)