From fd8959044c29ced54aa9c3ad0be4fdd41bd0aab5 Mon Sep 17 00:00:00 2001 From: Kenneth Lu Date: Fri, 9 Aug 2024 22:03:02 +0800 Subject: [PATCH] LoRaWAN: support ABP activation End device activation support for Activation By Personalization(ABP). Default ABP is disabled, set LoRaWAN data object pAbp to true to enable ABP and device address(pDevAddr), session keys for network(pNwkSKey) and application(pAppSKey). --- include/thingset/sdk.h | 4 ++++ src/lorawan.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/thingset/sdk.h b/include/thingset/sdk.h index 7caafee..8537e14 100644 --- a/include/thingset/sdk.h +++ b/include/thingset/sdk.h @@ -51,6 +51,10 @@ extern "C" { #define TS_ID_LORAWAN_JOIN_EUI 0x271 #define TS_ID_LORAWAN_APP_KEY 0x272 #define TS_ID_LORAWAN_DEV_NONCE 0x273 +#define TS_ID_LORAWAN_ABP 0x274 +#define TS_ID_LORAWAN_DEV_ADDR 0x275 +#define TS_ID_LORAWAN_APP_SKEY 0x276 +#define TS_ID_LORAWAN_NWK_SKEY 0x277 /* Networking group items */ #define TS_ID_NET 0x28 diff --git a/src/lorawan.c b/src/lorawan.c index 703bed0..f2cd6bf 100644 --- a/src/lorawan.c +++ b/src/lorawan.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -24,7 +25,11 @@ static uint8_t tx_buf[51]; char lorawan_join_eui[8 * 2 + 1] = "0000000000000000"; char lorawan_app_key[16 * 2 + 1] = ""; +char lorawan_dev_addr[4 * 2 + 1] = ""; +char lorawan_app_skey[16 * 2 + 1] = ""; +char lorawan_nwk_skey[16 * 2 + 1] = ""; uint32_t lorawan_dev_nonce; +bool abp = false; THINGSET_ADD_GROUP(TS_ID_ROOT, TS_ID_LORAWAN, "LoRaWAN", THINGSET_NO_CALLBACK); @@ -40,6 +45,18 @@ THINGSET_ADD_ITEM_STRING(TS_ID_LORAWAN, TS_ID_LORAWAN_APP_KEY, "pAppKey", lorawa THINGSET_ADD_ITEM_UINT32(TS_ID_LORAWAN, TS_ID_LORAWAN_DEV_NONCE, "pDevNonce", &lorawan_dev_nonce, THINGSET_ANY_RW, TS_SUBSET_NVM); +THINGSET_ADD_ITEM_BOOL(TS_ID_LORAWAN, TS_ID_LORAWAN_ABP, "pAbp", &abp, THINGSET_ANY_RW, + TS_SUBSET_NVM); + +THINGSET_ADD_ITEM_STRING(TS_ID_LORAWAN, TS_ID_LORAWAN_DEV_ADDR, "pDevAddr", lorawan_dev_addr, + sizeof(lorawan_dev_addr), THINGSET_ANY_RW, TS_SUBSET_NVM); + +THINGSET_ADD_ITEM_STRING(TS_ID_LORAWAN, TS_ID_LORAWAN_APP_SKEY, "pAppSKey", lorawan_app_skey, + sizeof(lorawan_app_skey), THINGSET_ANY_RW, TS_SUBSET_NVM); + +THINGSET_ADD_ITEM_STRING(TS_ID_LORAWAN, TS_ID_LORAWAN_NWK_SKEY, "pNwkSKey", lorawan_nwk_skey, + sizeof(lorawan_nwk_skey), THINGSET_ANY_RW, TS_SUBSET_NVM); + static void downlink_callback(uint8_t port, bool data_pending, int16_t rssi, int8_t snr, uint8_t len, const uint8_t *data) { @@ -94,12 +111,27 @@ void lorawan_thread(void) join_cfg.otaa.nwk_key = app_key; join_cfg.otaa.dev_nonce = lorawan_dev_nonce; + if (abp) { + uint8_t app_skey[16]; + uint8_t nwk_skey[16]; + uint32_t dev_addr = 0; + + hex2bin(lorawan_app_skey, strlen(lorawan_app_skey), app_skey, sizeof(app_skey)); + hex2bin(lorawan_nwk_skey, strlen(lorawan_nwk_skey), nwk_skey, sizeof(nwk_skey)); + hex2bin(lorawan_dev_addr, strlen(lorawan_dev_addr), (uint8_t *)&dev_addr, sizeof(dev_addr)); + + join_cfg.mode = LORAWAN_ACT_ABP; + join_cfg.abp.dev_addr = sys_cpu_to_be32(dev_addr); + join_cfg.abp.nwk_skey = nwk_skey; + join_cfg.abp.app_skey = app_skey; + } + bool connected = false; uint32_t rejoin_wait_sec = 8; bool increased_dev_nonce = false; while (true) { if (!connected) { - LOG_INF("Joining network over OTAA"); + LOG_INF("Joining network over %s", abp ? "ABP" : "OTAA"); ret = lorawan_join(&join_cfg); if (ret < 0) { LOG_ERR("lorawan_join_network failed: %d", ret);