From dcf7bcbf71ba84e40890c146629fe634bb706994 Mon Sep 17 00:00:00 2001 From: Anthony Hinsinger Date: Fri, 12 Apr 2019 13:14:14 +0200 Subject: [PATCH] Used NVStore to keep lora device keys and added a boostrap mode --- main.cpp | 76 +++++++++++++++++++++++++++++++++++++++++-- mbed_app.json | 1 + scripts/gen_images.sh | 2 +- 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 49cd043..cd61ac8 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,50 @@ #include "lorawan/LoRaWANInterface.h" #include "lorawan/system/lorawan_data_structures.h" #include "mbed.h" +#include "nvstore.h" + +#define NVKEY_DEVICE_EUI 1 +#define NVKEY_APP_EUI 2 +#define NVKEY_APP_KEY 3 + +#if MBED_CONF_APP_MODE_BOOTSTRAP + +const static uint8_t dev_eui[] = MBED_CONF_LORA_DEVICE_EUI; +const static uint8_t app_eui[] = MBED_CONF_LORA_APPLICATION_EUI; +const static uint8_t app_key[] = MBED_CONF_LORA_APPLICATION_KEY; + +int main(void) { + int rc; + uint16_t key; + uint16_t rsize; + uint8_t test[8]; + + // NVStore is a sigleton, get its instance + NVStore &nvstore = NVStore::get_instance(); + + rc = nvstore.init(); + printf("Init NVStore : %d\n", rc); + rc = nvstore.reset(); + printf("Reset NVStore : %d\n", rc); + + key = NVKEY_DEVICE_EUI; + nvstore.set(key, sizeof(dev_eui), dev_eui); + key = NVKEY_APP_EUI; + nvstore.set(key, sizeof(app_eui), app_eui); + key = NVKEY_APP_KEY; + nvstore.set(key, sizeof(app_key), app_key); + + while(1) { + printf("Bootstrap done\n"); + wait(1); + } +} + +#else // MBED_CONF_APP_MODE_BOOTSTRAP + +static uint8_t dev_eui[] = MBED_CONF_LORA_DEVICE_EUI; +static uint8_t app_eui[] = MBED_CONF_LORA_APPLICATION_EUI; +static uint8_t app_key[] = MBED_CONF_LORA_APPLICATION_KEY; #if MBED_CONF_APP_DHT_ENABLED #include "DHT.h" @@ -72,6 +116,32 @@ HX711 loadcell(MBED_CONF_APP_HX711_DATA, MBED_CONF_APP_HX711_CLK); int main(void) { lorawan_status_t retcode; + lorawan_connect_t connect; + int rc; + uint16_t key; + uint16_t readsize; + + connect.connect_type = LORAWAN_CONNECTION_OTAA; + + // NVStore is a sigleton, get its instance + NVStore &nvstore = NVStore::get_instance(); + + if (nvstore.init() != NVSTORE_SUCCESS) { + debug("\r\n NVStore initialization failed! \r\n"); + return -1; + } + + key = NVKEY_DEVICE_EUI; + nvstore.get(key, sizeof(dev_eui), dev_eui, readsize); + key = NVKEY_APP_EUI; + nvstore.get(key, sizeof(app_eui), app_eui, readsize); + key = NVKEY_APP_KEY; + nvstore.get(key, sizeof(app_key), app_key, readsize); + + connect.connection_u.otaa.app_eui = const_cast(app_eui); + connect.connection_u.otaa.dev_eui = const_cast(dev_eui); + connect.connection_u.otaa.app_key = const_cast(app_key); + connect.connection_u.otaa.nb_trials = MBED_CONF_LORA_NB_TRIALS; #if MBED_CONF_APP_HX711_ENABLED loadcell.powerDown(); @@ -109,7 +179,7 @@ int main(void) { debug("\r\n Adaptive data rate (ADR) - Enabled \r\n"); - retcode = lorawan.connect(); + retcode = lorawan.connect(connect); if(retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { } else { @@ -246,4 +316,6 @@ static void lora_event_handler(lorawan_event_t event) { default: MBED_ASSERT("Unknown Event"); } -} \ No newline at end of file +} + +#endif // MBED_APP_MODE_BOOTSTRAP \ No newline at end of file diff --git a/mbed_app.json b/mbed_app.json index 7e38afb..facdd45 100644 --- a/mbed_app.json +++ b/mbed_app.json @@ -1,5 +1,6 @@ { "config": { + "mode-bootstrap": { "value": "false" }, "tx-timer": { "value": 600000 }, "lora-radio": { "help": "Which radio to use (options: SX1272,SX1276)", diff --git a/scripts/gen_images.sh b/scripts/gen_images.sh index d510446..9b6c50d 100755 --- a/scripts/gen_images.sh +++ b/scripts/gen_images.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # patterns DEVICEEUI=ABBAABBAABBAABBA