-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
c0202f9
commit dee7168
Showing
8 changed files
with
372 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
radioConfig.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Makefile for user application | ||
|
||
# Specify this directory relative to the current application. | ||
TOCK_USERLAND_BASE_DIR = ../../.. | ||
|
||
STACK_SIZE = 4096 | ||
|
||
# Which files to compile. | ||
CXX_SRCS := $(wildcard *.cc) | ||
|
||
override CPPFLAGS += -DRADIOLIB_CLOCK_DRIFT_MS=9 | ||
|
||
# If we are building for a testing configuration (e.g. CI) then it's okay to | ||
# use the example config header. However, if someone is doing local testing, | ||
# especially of a different thing, we don't want to accidentally overwrite | ||
# the build obeject here with the example config when a real one exists. | ||
ifneq ($(TOCK_BUILDALL),) | ||
ifeq ($(wildcard radioConfig.h),) | ||
override CPPFLAGS += "-DRADIO_CONFIG_CI=radioConfig_example.h" | ||
endif | ||
endif | ||
|
||
ELF2TAB_ARGS += --write_id 2903764429 --read_ids 2903764429 --access_ids 2903764429 | ||
|
||
# Use the libtock-c Make system | ||
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/RadioLib | ||
|
||
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk | ||
|
||
# Protect from the (unlikely) case where the app happens to have been | ||
# built from an unrelated `build all` event, but now the user is trying | ||
# to flash the app with an invalid configuration. | ||
flash: radioConfig.h | ||
|
||
program: radioConfig.h | ||
|
||
install: radioConfig.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
LoRaWAN Set Keys | ||
================ | ||
|
||
This will set the keys and secrets for LoRaWAN. Copy the `radioConfig_example.h` | ||
and call it `radioConfig.h`. Set the values based on the values from your LoRaWAN | ||
gateway. Then flash this application. That will set the keys in flash on the board. | ||
|
||
After that the keys will be retrieved when running the LoRaWAN examples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
RadioLib Non-Arduino Tock Library LoRaWAN test application | ||
Licensed under the MIT or Apache License | ||
Copyright (c) 2023 Alistair Francis <[email protected]> | ||
*/ | ||
|
||
#include <cinttypes> | ||
#include <stdlib.h> | ||
|
||
// include the library | ||
#include <RadioLib.h> | ||
|
||
// Include some libtock-c helpers | ||
#include <libtock-sync/storage/kv.h> | ||
|
||
// To get this working copy radioConfig_example.h to radioConfig.h | ||
// and then modify it to match the LoRaWAN gateway settings. | ||
#ifdef RADIO_CONFIG_CI | ||
#include "radioConfig_example.h" | ||
#else | ||
#include "radioConfig.h" | ||
#endif | ||
|
||
#define JOIN_EUI_KEY_LEN 8 | ||
uint8_t join_eui_key_buf[JOIN_EUI_KEY_LEN] = "joinEUI"; | ||
|
||
#define DEV_EUI_KEY_LEN 7 | ||
uint8_t dev_eui_key_buf[DEV_EUI_KEY_LEN] = "devEUI"; | ||
|
||
#define NWK_KEY_KEY_LEN 7 | ||
uint8_t nwk_key_key_buf[NWK_KEY_KEY_LEN] = "nwkKey"; | ||
|
||
#define APP_KEY_KEY_LEN 7 | ||
uint8_t app_key_key_buf[APP_KEY_KEY_LEN] = "appKey"; | ||
|
||
#define KV_DATA_LEN 8 | ||
uint8_t kv_data_buf[KV_DATA_LEN]; | ||
|
||
// Store the joinEUI to the Tock K/V store | ||
static int set_join_eui(void) { | ||
returncode_t ret; | ||
|
||
if (!libtock_kv_exists()) { | ||
return 1; | ||
} | ||
|
||
kv_data_buf[0] = joinEUI & 0xFF; | ||
kv_data_buf[1] = (joinEUI >> 8) & 0xFF; | ||
kv_data_buf[2] = (joinEUI >> 16) & 0xFF; | ||
kv_data_buf[3] = (joinEUI >> 24) & 0xFF; | ||
kv_data_buf[4] = (joinEUI >> 32) & 0xFF; | ||
kv_data_buf[5] = (joinEUI >> 40) & 0xFF; | ||
kv_data_buf[6] = (joinEUI >> 48) & 0xFF; | ||
kv_data_buf[7] = (joinEUI >> 56) & 0xFF; | ||
|
||
ret = libtocksync_kv_set(join_eui_key_buf, JOIN_EUI_KEY_LEN, kv_data_buf, KV_DATA_LEN); | ||
|
||
if (ret == RETURNCODE_SUCCESS) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
// Store the devEUI to the Tock K/V store | ||
static int set_dev_eui(void) { | ||
returncode_t ret; | ||
|
||
if (!libtock_kv_exists()) { | ||
return 1; | ||
} | ||
|
||
kv_data_buf[0] = devEUI & 0xFF; | ||
kv_data_buf[1] = (devEUI >> 8) & 0xFF; | ||
kv_data_buf[2] = (devEUI >> 16) & 0xFF; | ||
kv_data_buf[3] = (devEUI >> 24) & 0xFF; | ||
kv_data_buf[4] = (devEUI >> 32) & 0xFF; | ||
kv_data_buf[5] = (devEUI >> 40) & 0xFF; | ||
kv_data_buf[6] = (devEUI >> 48) & 0xFF; | ||
kv_data_buf[7] = (devEUI >> 56) & 0xFF; | ||
|
||
ret = libtocksync_kv_set(dev_eui_key_buf, DEV_EUI_KEY_LEN, kv_data_buf, KV_DATA_LEN); | ||
|
||
if (ret == RETURNCODE_SUCCESS) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
// Store the nwkKey to the Tock K/V store | ||
static int set_nwk_key(void) { | ||
returncode_t ret; | ||
|
||
if (!libtock_kv_exists()) { | ||
return 1; | ||
} | ||
|
||
ret = libtocksync_kv_set(nwk_key_key_buf, NWK_KEY_KEY_LEN, nwkKey, 16); | ||
|
||
if (ret == RETURNCODE_SUCCESS) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
// Store the appKey to the Tock K/V store | ||
static int set_app_key(void) { | ||
returncode_t ret; | ||
|
||
if (!libtock_kv_exists()) { | ||
return 1; | ||
} | ||
|
||
ret = libtocksync_kv_set(app_key_key_buf, APP_KEY_KEY_LEN, appKey, 16); | ||
|
||
if (ret == RETURNCODE_SUCCESS) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
// the entry point for the program | ||
int main(void) { | ||
if (set_join_eui() == 0) { | ||
printf("Set joinEUI key to storage: 0x%lx%lx\r\n", | ||
(uint32_t)(joinEUI >> 32), (uint32_t)joinEUI); | ||
} else { | ||
printf("Unable to store joinEUI key to storage\r\n"); | ||
return 1; | ||
} | ||
|
||
if (set_dev_eui() == 0) { | ||
printf("Set devEUI key to storage: 0x%lx%lx\r\n", | ||
(uint32_t)(devEUI >> 32), (uint32_t)devEUI); | ||
} else { | ||
printf("Unable to store devEUI key to storage\r\n"); | ||
return 1; | ||
} | ||
|
||
if (set_nwk_key() == 0) { | ||
printf("Set nwkKey key to storage\r\n"); | ||
} else { | ||
printf("Unable to store nwkKey to storage\r\n"); | ||
return 1; | ||
} | ||
|
||
if (set_app_key() == 0) { | ||
printf("Set appKey key to storage\r\n"); | ||
} else { | ||
printf("Unable to store appKey to storage\r\n"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.