From 9cc32af99e7986e32e1fe82f3c8e5d30e1fc3997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sat, 26 Feb 2022 13:52:18 +0100 Subject: [PATCH] lorawan: allow setting of DevNonce for OTAA re-join MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Starting with LoRaWAN 1.0.4 the DevNonce sent with the OTAA join must be monotonically increasing for each new join with the same EUI. The DevNonce should be stored in non-volatile memory by the application. This commit uses a simple extension of the lorawan_join_otaa struct to allow specifying the DevNonce. Signed-off-by: Martin Jäger --- include/lorawan/lorawan.h | 11 +++++++++++ subsys/lorawan/lorawan.c | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/include/lorawan/lorawan.h b/include/lorawan/lorawan.h index 0ba3211670f..58ace723d32 100644 --- a/include/lorawan/lorawan.h +++ b/include/lorawan/lorawan.h @@ -78,9 +78,20 @@ enum lorawan_message_type { * case the values stored in the secure element will be used instead. */ struct lorawan_join_otaa { + /** Join EUI */ uint8_t *join_eui; + /** Network Key */ uint8_t *nwk_key; + /** Application Key */ uint8_t *app_key; + /** + * Device Nonce + * + * Starting with LoRaWAN 1.0.4 the DevNonce must be monotonically + * increasing for each OTAA join with the same EUI. The DevNonce + * should be stored in non-volatile memory by the application. + */ + uint32_t dev_nonce; }; struct lorawan_join_abp { diff --git a/subsys/lorawan/lorawan.c b/subsys/lorawan/lorawan.c index 84559b99c7b..c10b3f57ac9 100644 --- a/subsys/lorawan/lorawan.c +++ b/subsys/lorawan/lorawan.c @@ -212,6 +212,14 @@ static LoRaMacStatus_t lorawan_join_otaa( mlme_req.Req.Join.Datarate = default_datarate; mlme_req.Req.Join.NetworkActivation = ACTIVATION_TYPE_OTAA; + /* Retrieve the NVM context to store device nonce */ + mib_req.Type = MIB_NVM_CTXS; + if (LoRaMacMibGetRequestConfirm(&mib_req) != LORAMAC_STATUS_OK) { + LOG_ERR("Could not get NVM context"); + return -EINVAL; + } + mib_req.Param.Contexts->Crypto.DevNonce = join_cfg->otaa.dev_nonce; + mib_req.Type = MIB_DEV_EUI; mib_req.Param.DevEui = join_cfg->dev_eui; LoRaMacMibSetRequestConfirm(&mib_req);