Skip to content

Commit

Permalink
Fix LoRaWAN MAC internal initialization
Browse files Browse the repository at this point in the history
This commit fixes the internal initialization of the LoRaMAC-node
library. This firmware aims to be backward compatible with Murata Modem
firmware which initializes the MAC in ABP mode upon factory default. We
attempted to do the same, but the code that reconfigured the MAC into
ABP mode upon factory default was incorrect. It put the MAC into
incorrect state.

As a consequence, not all state previously saved in NVM was correctly
recovered, including a previously saved application key.

Closes #132
  • Loading branch information
janakj committed May 20, 2023
1 parent 698b503 commit 205b7c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
40 changes: 30 additions & 10 deletions src/lrw.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static void restore_state(void)
};
int rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != LORAMAC_STATUS_OK)
log_error("LoRaMac: Error while restoring NVM state: %d", rc);
log_error("LoRaMac: Error while restoring NVM contexts: %d", rc);
}


Expand Down Expand Up @@ -658,41 +658,57 @@ static void log_network_info(void)
* firmware. It is meant to be called after the MIB has been initialized from
* the defaults built in LoRaMac-node and before settings are restored from NVM.
*/
static void set_defaults(void)
static LoRaMacStatus_t set_defaults(void)
{
LoRaMacStatus_t rc;

// The original firmware has AppEUI set to 0101010101010101
MibRequestConfirm_t r = {
.Type = MIB_JOIN_EUI,
.Param = { .JoinEui = (uint8_t *)"\1\1\1\1\1\1\1\1" }
};
LoRaMacMibSetRequestConfirm(&r);
rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != LORAMAC_STATUS_OK) return rc;

// The original firmware has ADR enabled by default
r.Type = MIB_ADR;
r.Param.AdrEnable = 1;
LoRaMacMibSetRequestConfirm(&r);
rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != LORAMAC_STATUS_OK) return rc;

/// The original firmware configures the TRX with 14 dBm in RFO mode
r.Type = MIB_CHANNELS_TX_POWER;
r.Param.ChannelsTxPower = 1;
LoRaMacMibSetRequestConfirm(&r);
rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != LORAMAC_STATUS_OK) return rc;

#ifdef LORAMAC_ABP_VERSION
// If we are in ABP mode and the application has defined a specific MAC
// version to be used in this mode, set it now. There is no automatic
// version negotiation in ABP mode, so this needs to be done manually.
r.Type = MIB_ABP_LORAWAN_VERSION;
r.Param.AbpLrWanVersion.Value = LORAMAC_ABP_VERSION;
LoRaMacMibSetRequestConfirm(&r);
rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != LORAMAC_STATUS_OK) return rc;
#endif

// The original firmware configures the node in ABP mode by default
lrw_set_mode(0);
// The original firmware configures the node in ABP mode by default. We
// cannot use lrw_set_mode here because that function performs join
// internally. Performing join would put the MAC in a busy state. Here,
// where we are initializing from defaults, internal join is not necessary
// anyway.
r.Type = MIB_NETWORK_ACTIVATION;
r.Param.NetworkActivation = ACTIVATION_TYPE_ABP;
rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != 0) return rc;

// Disable LoRaWAN certification port by default
r.Type = MIB_IS_CERT_FPORT_ON;
r.Param.IsCertPortOn = false;
LoRaMacMibSetRequestConfirm(&r);
rc = LoRaMacMibSetRequestConfirm(&r);
if (rc != LORAMAC_STATUS_OK) return rc;

return rc;
}


Expand Down Expand Up @@ -728,7 +744,11 @@ void lrw_init(void)
return;
}

set_defaults();
// Initializing the LoRa MAC to defaults should never fail. If it does, we
// have a serious problem.
if (set_defaults() != LORAMAC_STATUS_OK)
halt("LoRaMac: Error while initializing to defaults");

restore_state();

r.Type = MIB_SYSTEM_MAX_RX_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/nvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


/* The following partition sizes have been derived from the in-memory size of
* the corresponding data structures in our fork of LoRaMac-node v4.6.0. The
* the corresponding data structures in our fork of LoRaMac-node v4.7.0. The
* sizes have been rounded up to leave some space for expansion in future
* versions. Whenever LoRaMac-node is modified, these sizes need to be
* reevaluated to make sure that each part is large enough to hold its data
Expand Down

0 comments on commit 205b7c1

Please sign in to comment.