arduino-libraries / MKRWAN_v2

[Beta] Library for Arduino MKRWAN 1300 / 1310 suitable for firmwares derived from ST sources
GNU Lesser General Public License v3.0
7 stars 6 forks source link

Inability to Store OTAA Keys and Retrieve Message Count #38

Open Javieral95 opened 4 months ago

Javieral95 commented 4 months ago

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!!!

fuma01 commented 3 weeks ago

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