Skip to content

Commit

Permalink
[LoRaWAN] Fix fcntUp wrap-around after 50 transmissions
Browse files Browse the repository at this point in the history
Resolves #950
  • Loading branch information
barnslig committed Feb 13, 2024
1 parent a907026 commit ea2ccfd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/protocols/LoRaWAN/LoRaWAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,15 @@ int16_t LoRaWANNode::saveSession() {
int16_t LoRaWANNode::saveFcntUp() {
Module* mod = this->phyLayer->getMod();

uint8_t fcntBuff[30] = { 0 };
mod->hal->readPersistentStorage(mod->hal->getPersistentAddr(RADIOLIB_EEPROM_LORAWAN_FCNT_UP_ID), fcntBuff, 30);
uint8_t fcntBuffStart = mod->hal->getPersistentAddr(RADIOLIB_EEPROM_LORAWAN_FCNT_UP_ID);
uint8_t fcntBuffEnd = mod->hal->getPersistentAddr(RADIOLIB_EEPROM_LORAWAN_FCNT_UP_ID + 1);
uint8_t buffSize = fcntBuffEnd - fcntBuffStart;
#if RADIOLIB_STATIC_ONLY
uint8_t fcntBuff[RADIOLIB_STATIC_ARRAY_SIZE];
#else
uint8_t* fcntBuff = new uint8_t[buffSize];
#endif
mod->hal->readPersistentStorage(mod->hal->getPersistentAddr(RADIOLIB_EEPROM_LORAWAN_FCNT_UP_ID), fcntBuff, buffSize);

// we discard the first two bits - your flash will likely be far dead by the time you reach 2^30 uplinks
// the first two bytes of the remaining 30 bytes are stored straight into storage without additional wear leveling
Expand Down Expand Up @@ -859,12 +866,12 @@ int16_t LoRaWANNode::saveFcntUp() {
// always flip the state bit of the byte that we write to, to indicate that this is the most recently written byte
idx = 5;
state = fcntBuff[idx] >> 7;
for(; idx < 30; idx++) {
for(; idx < buffSize; idx++) {
if(fcntBuff[idx] >> 7 != state) {
break;
}
}
idx = idx < 30 ? idx : 5;
idx = idx < buffSize ? idx : 5;
uint8_t bits_7_0 = (this->fcntUp >> 0) & 0x7F;

// flip the first bit of this byte to indicate that we just wrote here
Expand Down

0 comments on commit ea2ccfd

Please sign in to comment.