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

Inability to Store OTAA Keys and Retrieve Message Count #38

Open
Javieral95 opened this issue Feb 20, 2024 · 1 comment
Open

Inability to Store OTAA Keys and Retrieve Message Count #38

Javieral95 opened this issue Feb 20, 2024 · 1 comment
Assignees
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@Javieral95
Copy link

Javieral95 commented Feb 20, 2024

Issue:

After updating from version 1 (MKRWAN) to version 2 (MKRWAN_V2) of the LoRaWAN library in a MKRWAN1310 Arduino board. I've encountered difficulties in storing OTAA keys in memory and retrieving the message count.

Expected Behavior:

In the previous version (V1), I was able to successfully store OTAA keys in memory and retrieve the message count using appropriate functions. However, after updating to V2, the OTAA keys are being stored incorrectly, and I couldn't find a function capable of retrieving the message count.

Update Framecounter

In Version 1 I can update the frame counter.

LoRaModem modem(LORA_SERIAL);
typedef struct // Structure of data to be saved over reset
{
    bool valid;
    char* devaddr;    // Device address after join //4 bytes
    char* nwkKey;     // Network session key after join //16 bytes
    char* appSKey;    // Aplication session key after join //16bytes
    unsigned int fcnt; // Frame counter
    unsigned int error_cnt;
} lora_savdata;
FlashStorage(my_flash_store, lora_savdata);
lora_savdata lora_store_data;

// . . .

    int current_fc = modem.getFCU(); //In V2 this function doesnt exist
    if (current_fc != lora_store_data.fcnt){        
        modem.setFCU(lora_store_data.fcnt);  //In V2 this function doesnt exist
        Serial.print("The Frame Counter stored in memory has been updated to ");
        Serial.println(lora_store_data.fcnt);

// After send a message I increment the fcnt in memory

Save Keys

And, in version V1, I can save the keys too:


// . . .
    modem.getDevAddr().toCharArray(lora_store_data.devaddr, 4);
    modem.getNwkSKey().toCharArray(lora_store_data.nwkKey, 64);
    modem.getAppSKey().toCharArray(lora_store_data.appSKey, 64);

    my_flash_store.write(lora_store_data);
}

In version V2 I don`t know why but the values got from this functions returns strange values.

modem.getDevAddr()
modem.getNwkSKey()
modem.getAppSKey()

Why this?

To enhance device connectivity efficiency, I propose implementing a mechanism to store negotiated OTAA keys in non-volatile memory after successful join, allowing subsequent ABP joins using the stored keys, thus avoiding repetitive OTAA negotiations.

  if (lora_store_data.valid && lora_store_data.error_cnt < MAX_ERROR_COUNTER) {
    Serial.println("Have stored valid data, joining using ABP...");
    Serial.println("KEYS:");
    connected = modem.joinABP(lora_store_data.devaddr, lora_store_data.nwkKey, lora_store_data.appSKey);
  } else {
    Serial.println("Joining using OTAA...");
    connected = modem.joinOTAA(APP_EUI, APP_KEY);
  }

Thanks for all!!!

@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Feb 20, 2024
@fuma01
Copy link

fuma01 commented Jun 14, 2024

I have the same problem and cant set the frame counter as in V1: setFCU
please, can you implement the function also in V2?
I want to use MKR1310 with TPL5110 sleep timer, store the session keys before sleep and set it after waking up.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

4 participants