Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist LoRaWAN keys after OTAA join #60

Closed
cyberman54 opened this issue May 18, 2018 · 7 comments
Closed

Persist LoRaWAN keys after OTAA join #60

cyberman54 opened this issue May 18, 2018 · 7 comments
Labels
enhancement New feature or request

Comments

@cyberman54
Copy link
Owner

HowTo:

After an OTAA Join has finished, your node has the very same details you’d have for an ABP device. So: store the DevAddr and the secret session keys NwkSKey and AppSKey, and keep track of both frame counters. After deep sleep, configure your node as an ABP node using LMIC_setSession(...), and set the frame counters to their last stored values, using LMIC.seqnoUp = ... and LMIC.seqnoDn = ....

For LMiC, these session details have different names (which originate from the early days where LoRaWAN was still called LoRaMAC). The documentation shows:

2.5.4 void LMIC_setSession (u4_t netid, devaddr_t devaddr, u1_t* nwkKey, u1_t* artKey)

Set static session parameters. Instead of dynamically establishing a session by joining the network, precomputed session parameters can be provided. To resume a session with precomputed parameters, the frame sequence counters (LMIC.seqnoUp and LMIC.seqnoDn) must be restored to their latest values.

And so does the code:

void LMIC_setSession (u4_t netid, devaddr_t devaddr, xref2u1_t nwkKey, xref2u1_t artKey);
Indeed, the global LMIC variable provides access to properties such as LMIC.nwkKey for NwkSKey, LMIC.artKey for AppSkey and LMIC.devaddr for DevAddr, which you should be able to retrieve and persist in case EV_JOINED.

Beware that you might also need to store and set the frequency and RX2 details that you get in the Join Accept, or after subsequent ADR commands…

(I have not used this.)

@cyberman54 cyberman54 added the enhancement New feature or request label May 18, 2018
@stkdiretto
Copy link

I am currently looking into this since I could need this in another project also. Do you suggest storing the data in the configdata_t struct? I'd check for set parameters on reboot and using them if available.

@cyberman54
Copy link
Owner Author

Could be a way. Another, maybe more straight forward way, would be to create/open a separare, second partition in NVRAM and store it there.

@stkdiretto
Copy link

I have to dig into the storing mechanism a bit more. Your suggestion sounds more feasible – from what I've interpreted so far, saving the data would always mean storing all the variables. Since seqnoDn and seqnoUp need to be saved on every package, that might wear out the nvram rather quickly.

@FlorianLudwig
Copy link
Collaborator

To reduce write wearing only persist counters every n packages and when rejoining just add n to the counter. This way the join works and the network still accepts the packages and regard the counter difference as package loss.

n should depend on send interval.

@cyberman54
Copy link
Owner Author

@cyberman54
Copy link
Owner Author

Example how to save keys in RTC RAM of ESP32 while deep sleep:

manuelbl/ttn-esp32#15

@cyberman54
Copy link
Owner Author

RTC RAM save keys is now implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants