Skip to content
8 changes: 7 additions & 1 deletion examples/Catena4430_Sensor/Catena4430_cMeasurementLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ cMeasurementLoop::fsmDispatch(
if (this->handleSdFirmwareUpdate())
newState = State::stRebootForUpdate;
else
newState = State::stSleeping;
newState = State::stTryToMigrate;
break;

// try to migrate to TTN V3
case State::stTryToMigrate:
this->handleSdTTNv3Migrate();
newState = State::stSleeping;
break;

// no SD card....
Expand Down
4 changes: 4 additions & 0 deletions examples/Catena4430_Sensor/Catena4430_cMeasurementLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class cMeasurementLoop : public McciCatena::cPollableObject
stTransmit, // transmit data
stWriteFile, // write file data
stTryToUpdate, // try to update firmware
stTryToMigrate, // try to migrate device to TTN V3
stAwaitCard, // wait for a card to show up.
stRebootForUpdate, // reboot system to complete firmware update

Expand All @@ -281,6 +282,7 @@ class cMeasurementLoop : public McciCatena::cPollableObject
case State::stTransmit: return "stTransmit";
case State::stWriteFile: return "stWriteFile";
case State::stTryToUpdate: return "stTryToUpdate";
case State::stTryToMigrate: return "stTryToMigrate";
case State::stAwaitCard: return "stAwaitCard";
case State::stRebootForUpdate: return "stRebootForUpdate";
case State::stFinal: return "stFinal";
Expand Down Expand Up @@ -382,6 +384,8 @@ class cMeasurementLoop : public McciCatena::cPollableObject
bool handleSdFirmwareUpdate();
bool handleSdFirmwareUpdateCardUp();
bool updateFromSd(const char *sFile, McciCatena::cDownload::DownloadRq_t rq);
void handleSdTTNv3Migrate();
void rejoinNetwork();
void sdPowerUp(bool fOn);
void sdPrep();

Expand Down
59 changes: 59 additions & 0 deletions examples/Catena4430_Sensor/Catena4430_cMeasurementLoop_SDcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Module: Catena4430_cMeasurementLoop_SDcard.cpp

#include <Catena_Download.h>

#include <Catena_Fram.h>

#include <Arduino_LoRaWAN_lmic.h>

#include <SD.h>
#include <mcciadk_baselib.h>

Expand All @@ -33,6 +37,8 @@ using namespace McciCatena;

SDClass gSD;

constexpr char gkMigrateFileName[] = "MIGRATE.V3";

/****************************************************************************\
|
| Some utilities
Expand Down Expand Up @@ -571,4 +577,57 @@ cMeasurementLoop::updateFromSd(
}
}

void
cMeasurementLoop::handleSdTTNv3Migrate(
void
)
{
bool fMigrate = false;
bool fResult = this->checkSdCard();

if (fResult)
{
if (! gSD.exists(gkMigrateFileName))
fMigrate = false;
else
fMigrate = true;
}

if (fMigrate)
{
bool fFramUpdate = false;
auto const pFram = gCatena.getFram();
static const uint8_t AppEUI[] = { 1, 0, 0, 0, 0, 0, 0, 0 };

if (pFram == nullptr)
fFramUpdate = false;
else
{
pFram->saveField(cFramStorage::kAppEUI, AppEUI);
fFramUpdate = true;
}

if (fFramUpdate)
{
this->rejoinNetwork();
gSD.remove(gkMigrateFileName);
gLog.printf(gLog.kInfo, "cFramStorage::kAppEUI: update: success\n");
}
else
gLog.printf(gLog.kError, "cFramStorage::kAppEUI: not updated\n");
}
}

void
cMeasurementLoop::rejoinNetwork(
void
)
{
auto const pFram = gCatena.getFram();
pFram->saveField(cFramStorage::kDevAddr, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can return an error. Check return value. As a consequence, rejoinNetwork can return an error.

Copy link
Contributor Author

@dhineshkumarmcci dhineshkumarmcci Jul 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I have planned to check return value, unfortunately API saveField is of datatype void.

In this case, I believe we can use API getField to read the value written in FRAM, and then compare to check for error.

@terrillmoore Please advice on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I filed a bug report but didn't make the change. No, don't use getField(); the saveField() API is going to change to be bool, and we'll deal with it then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created a pull-request for changes with McciCatena::cFram::saveField() - Catena-Arduino-Platform PR-317


LMIC_unjoin();
LMIC_startJoining();
}

#undef FUNCTION