summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrii Pientsov <andrii.pientsov@globallogic.com>2020-07-06 12:58:51 -0500
committerAndrii Pientsov <andrii.pientsov@globallogic.com>2020-07-06 12:58:51 -0500
commit9e007df4a7e3fafff27a0c3fa11d4ba53e785985 (patch)
tree48851ebc4c9dbb8c4f7d9885453aa1986ac41148
parent343e662b6224cf03fea5ebfd419c7cf990528b53 (diff)
parent63b1b15a510ff3acdc4f93bbac1ed14548e6620b (diff)
downloadlibmts-io-9e007df4a7e3fafff27a0c3fa11d4ba53e785985.tar.gz
libmts-io-9e007df4a7e3fafff27a0c3fa11d4ba53e785985.tar.bz2
libmts-io-9e007df4a7e3fafff27a0c3fa11d4ba53e785985.zip
Merge branch 'sk/quectel-delta-fwu' into 'delta-radio-fwu'
Quectel Delta Radio Firmware Upgrade support - libmts-io implementation See merge request !26
-rw-r--r--include/mts/MTS_IO_CellularRadio.h6
-rw-r--r--include/mts/MTS_IO_ICellularRadio.h66
-rw-r--r--include/mts/MTS_IO_QuectelRadio.h55
-rw-r--r--src/MTS_IO_CellularRadio.cpp41
-rw-r--r--src/MTS_IO_ICellularRadio.cpp2
-rw-r--r--src/MTS_IO_QuectelRadio.cpp520
6 files changed, 671 insertions, 19 deletions
diff --git a/include/mts/MTS_IO_CellularRadio.h b/include/mts/MTS_IO_CellularRadio.h
index ed87a24..e3941c3 100644
--- a/include/mts/MTS_IO_CellularRadio.h
+++ b/include/mts/MTS_IO_CellularRadio.h
@@ -99,6 +99,10 @@ namespace MTS {
CODE updateDc(const Json::Value& jArgs, UpdateCb& stepCb) override;
CODE updatePrl(const Json::Value& jArgs, UpdateCb& stepCb) override;
CODE updateFumo(const Json::Value& jArgs, UpdateCb& stepCb) override;
+ CODE updateFumoLocal(int fd, UpdateCb& stepCb) override;
+ CODE fumoLocalInject(int fd, UpdateCb& stepCb) override;
+ CODE fumoLocalApply(UpdateCb& stepCb) override;
+ CODE fumoLocalCleanup() override;
CODE resetHfa(const Json::Value& jArgs, UpdateCb& stepCb) override;
CODE activate(const Json::Value& jArgs, UpdateCb& stepCb) override;
CODE startOmaDm(UpdateCb& stepCb) override;
@@ -179,6 +183,8 @@ namespace MTS {
REGISTRATION parseRegResponse(std::string sResult);
CODE getRegistration(REGISTRATION& eRegistration, const std::string& sType);
+ virtual CODE sendData(const char* pData, size_t nBytes);
+
class RadioBandMap : public MTS::NonCopyable {
public:
RadioBandMap()
diff --git a/include/mts/MTS_IO_ICellularRadio.h b/include/mts/MTS_IO_ICellularRadio.h
index f2d4dfe..e1edbcd 100644
--- a/include/mts/MTS_IO_ICellularRadio.h
+++ b/include/mts/MTS_IO_ICellularRadio.h
@@ -103,6 +103,7 @@ namespace MTS {
static const char *RSP_OK;
static const char *RSP_ERROR;
+ static const char *RSP_CONNECT;
static const char *VALUE_NOT_REGISTERED;
static const char *VALUE_REGISTERED;
@@ -427,6 +428,71 @@ namespace MTS {
*/
virtual CODE updateFumo(const Json::Value& jArgs, UpdateCb& stepCb) = 0;
+ /**
+ * @brief updateFumoLocal - Performs the radio firmware upgrade using local firmware image.
+ *
+ * This command uploads (injects) the whole delta firmware image to the radio, performs the
+ * upgrade and waits for it to complete.
+ *
+ * @param fd - file descriptor of a file that shall be injected to the radio.
+ * @param stepCb - callback to receive status updates during the firmware upgrade.
+ * @return CODE::SUCCESS when the firmware upgrade was successful,
+ * CODE::INVALID_ARGS when the file can't be opened for reading,
+ * CODE::FAILURE when upgrade failed on the radio side,
+ * CODE::NOT_APPLICABLE when not supported by this radio,
+ * CODE::ERROR otherwise.
+ */
+ virtual CODE updateFumoLocal(int fd, UpdateCb& stepCb) = 0;
+
+ /**
+ * @brief fumoLocalInject - upload delta file to the radio's internal memory.
+ *
+ * This command uploads (injects) the whole delta firmware image to some place that
+ * can be later used by the radio to perform the Delta Radio Firmware Upgrade.
+ *
+ * This delta firmware image is NOT validated on the firmware image upload step.
+ *
+ * @param fd - file sescriptor of a file that shall be uploaded to the radio.
+ * @param stepCb - callback to receive status updates during the upload.
+ * @return CODE::SUCCESS when the file was successfully uploaded,
+ * CODE::INVALID_ARGS when the file can't be opened for reading,
+ * CODE::NOT_APPLICABLE when not supported by this radio,
+ * CODE::ERROR otherwise.
+ */
+ virtual CODE fumoLocalInject(int fd, UpdateCb& stepCb) = 0;
+
+ /**
+ * @brief fumoLocalApply - apply the delta file that was previously uploaded.
+ *
+ * This commands initializes and tracks the progress of the delta firmware upgrade
+ * procedure using the delta firmware image file that was previously uploaded
+ * into internal memory of the radio.
+ *
+ * See ICellularRadio::fumoLocalInject to upload the file and prepare
+ * for the upgrade.
+ *
+ * @param fd - file descriptor of a file that shall be uploaded to the radio.
+ * @param stepCb - the callback to receive status updates during the upgrade.
+ * @return CODE::SUCCESS when the firmware upgrade was successful,
+ * CODE::FAILURE when upgrade failed on the radio side,
+ * CODE::NOT_APPLICABLE when not supported by this radio,
+ * CODE::ERROR otherwise.
+ */
+ virtual CODE fumoLocalApply(UpdateCb& stepCb) = 0;
+
+ /**
+ * @brief fumoLocalCleanup - remove the delta file from the radio's internal memory.
+ *
+ * This command allows to remove the old delta firmware image from the radio's internal
+ * memory for cases when it's no longer needed.
+ *
+ * @return CODE::SUCCESS when the file was successfully deleted,
+ * CODE::FAILURE when the file can't be deleted (i.e. no such file),
+ * CODE::NOT_APPLICABLE when not supported by this radio,
+ * CODE::ERROR otherwise.
+ */
+ virtual CODE fumoLocalCleanup() = 0;
+
/*
* jArgs = {
* "msl" : "Master Subsidy Lock (Sprint): STRING"
diff --git a/include/mts/MTS_IO_QuectelRadio.h b/include/mts/MTS_IO_QuectelRadio.h
index fcee069..d8228cb 100644
--- a/include/mts/MTS_IO_QuectelRadio.h
+++ b/include/mts/MTS_IO_QuectelRadio.h
@@ -45,6 +45,11 @@ namespace MTS {
CODE setCellularMode(CELLULAR_MODES networks) override;
+ CODE updateFumoLocal(int fd, UpdateCb& stepCb) override;
+ CODE fumoLocalInject(int fd, UpdateCb& stepCb) override;
+ CODE fumoLocalCleanup() override;
+ CODE fumoLocalApply(UpdateCb& stepCb) override;
+
protected:
QuectelRadio(const std::string& sName, const std::string& sRadioPort);
@@ -54,10 +59,60 @@ namespace MTS {
virtual CODE getServiceDomain(SERVICEDOMAIN& sd);
virtual CODE convertToActiveBand(const std::string& sQuectelBand, ACTIVEBAND& band);
+ virtual CODE uploadFile(int fd, const std::string& sTargetFilename, UpdateCb& stepCb);
+ virtual CODE removeFile(const std::string& sTargetFilename);
+ virtual CODE checkFile(bool& bFilePresent, const std::string& sTargetFilename);
+
private:
+ // private variable to save old firmware versions during FOTA
+ std::string m_sQuectelFirmware;
+
+ static const size_t FILE_CHUNK_SIZE;
+ static const std::string CMD_ABORT_UPLOAD;
+ static const std::string VALUE_MTS_DELTA_NAME;
+ static const std::string VALUE_MTS_DELTA_PATH;
+
+ // TODO: Consider asbtracting to the ICellularRadio::getFirmwareBuild
+ //!< Get Quectel-specific firmware version (firmware build?)
+ CODE getQuectelFirmware(std::string& sFirmware);
+ CODE startFileUpload(const std::string& sTargetFilename, size_t nBytes);
+ CODE abortFileUpload();
+
+ static inline void callNextStep(UpdateCb& stepCb, const char* csMessage);
+ static inline void callNextStep(UpdateCb& stepCb, const std::string& sMessage);
+
+ static uint16_t getQuectelChecksum(const void* data, size_t nBytes);
+ static inline void updateQuectelChecksum(uint16_t& iChecksum, uint16_t iNewFragment);
+ static inline uint16_t bytesToUint16(uint8_t high, uint8_t low);
+ static CODE getFileSize(int fd, size_t& nBytes, size_t& nFileChunks);
+ static CODE readChunk(int fd, char* pChunk, size_t dChunkSize, size_t& nReadBytes);
+ CODE fumoWaitUpgradeFinished(UpdateCb& stepCb);
+ CODE fumoWaitNewFirmware(UpdateCb& stepCb);
};
}
}
+void MTS::IO::QuectelRadio::callNextStep(ICellularRadio::UpdateCb& stepCb, const char* csMessage) {
+ if (stepCb) {
+ stepCb(Json::Value(csMessage));
+ }
+}
+
+void MTS::IO::QuectelRadio::callNextStep(ICellularRadio::UpdateCb& stepCb, const std::string& sMessage) {
+ if (stepCb) {
+ stepCb(Json::Value(sMessage));
+ }
+}
+
+void MTS::IO::QuectelRadio::updateQuectelChecksum(uint16_t& iChecksum, uint16_t iNewFragment) {
+ iChecksum = iChecksum ^ iNewFragment;
+}
+
+uint16_t MTS::IO::QuectelRadio::bytesToUint16(uint8_t high, uint8_t low) {
+ uint16_t comboHigh = static_cast<uint16_t>(high << 8); // explicit cast to prevent warnings
+ uint16_t comboLow = low;
+ return (comboHigh | comboLow);
+}
+
#endif /* MTS_IO_QUECTELRADIO_H_ */
diff --git a/src/MTS_IO_CellularRadio.cpp b/src/MTS_IO_CellularRadio.cpp
index b18478e..bb7cd40 100644
--- a/src/MTS_IO_CellularRadio.cpp
+++ b/src/MTS_IO_CellularRadio.cpp
@@ -1051,6 +1051,30 @@ ICellularRadio::CODE CellularRadio::updateFumo(const Json::Value&, UpdateCb&) {
return NOT_APPLICABLE;
}
+ICellularRadio::CODE CellularRadio::updateFumoLocal(int, ICellularRadio::UpdateCb&) {
+ printTrace("%s| Update Local Firmware Update Management Object", m_sName.c_str());
+
+ return NOT_APPLICABLE;
+}
+
+ICellularRadio::CODE CellularRadio::fumoLocalInject(int, ICellularRadio::UpdateCb&) {
+ printTrace("%s| Inject Delta Firmware Image File: not applicable", m_sName.c_str());
+
+ return NOT_APPLICABLE;
+}
+
+ICellularRadio::CODE CellularRadio::fumoLocalApply(ICellularRadio::UpdateCb&) {
+ printTrace("%s| Apply Delta Firmware Image File: not applicable", m_sName.c_str());
+
+ return NOT_APPLICABLE;
+}
+
+ICellularRadio::CODE CellularRadio::fumoLocalCleanup() {
+ printTrace("%s| Cleanup Delta Firmware Image File: not applicable", m_sName.c_str());
+
+ return NOT_APPLICABLE;
+}
+
ICellularRadio::CODE CellularRadio::resetHfa(const Json::Value&, UpdateCb&) {
printTrace("%s| HFA Reset", m_sName.c_str());
@@ -1103,6 +1127,23 @@ std::string CellularRadio::sendCommand(const std::string& sCmd, MTS::IO::Cellula
return ICellularRadio::sendCommand(m_apIo, sCmd, isNeedMoreData, timeoutMillis, ESC);
}
+ICellularRadio::CODE CellularRadio::sendData(const char* pData, size_t nBytes) {
+ if(m_apIo.isNull()) {
+ printError("RADIO| IO is not set in sendData");
+ return ERROR;
+ }
+
+ int32_t iResult;
+ iResult = m_apIo->write(pData, nBytes);
+
+ if(iResult == -1) {
+ printError("RADIO| Failed to send data to radio");
+ return ERROR;
+ }
+
+ return SUCCESS;
+}
+
bool CellularRadio::splitAndAssign(const std::string& sLine, const std::string& sKey, Json::Value& jParent, const std::string& sJsonKey, Json::ValueType eType) {
std::vector<std::string> vParts = MTS::Text::split(sLine, ":", 2);
diff --git a/src/MTS_IO_ICellularRadio.cpp b/src/MTS_IO_ICellularRadio.cpp
index 1a428e4..4b58189 100644
--- a/src/MTS_IO_ICellularRadio.cpp
+++ b/src/MTS_IO_ICellularRadio.cpp
@@ -11,7 +11,7 @@ const char MTS::IO::ICellularRadio::CTRL_Z = 0x1A;
const char *MTS::IO::ICellularRadio::RSP_ERROR = "ERROR";
const char *MTS::IO::ICellularRadio::RSP_OK = "OK";
-
+const char *MTS::IO::ICellularRadio::RSP_CONNECT = "CONNECT";
const char *MTS::IO::ICellularRadio::DEFAULT_RADIO_PORT = "/dev/modem_at1";
const char *MTS::IO::ICellularRadio::DEFAULT_RADIO_DIR = "/var/run/radio/";
diff --git a/src/MTS_IO_QuectelRadio.cpp b/src/MTS_IO_QuectelRadio.cpp
index 5c2c352..7aeed96 100644
--- a/src/MTS_IO_QuectelRadio.cpp
+++ b/src/MTS_IO_QuectelRadio.cpp
@@ -25,9 +25,19 @@
#include <mts/MTS_Logger.h>
#include <mts/MTS_Thread.h>
#include <mts/MTS_Text.h>
+#include <mts/MTS_Timer.h>
+
+#include <unistd.h>
using namespace MTS::IO;
+const size_t QuectelRadio::FILE_CHUNK_SIZE = 1024;
+const std::string QuectelRadio::CMD_ABORT_UPLOAD = "+++";
+
+// It is strongly recommended to use DOS 8.3 file name format for <filename>.
+const std::string QuectelRadio::VALUE_MTS_DELTA_NAME = "mtsdelta.zip";
+const std::string QuectelRadio::VALUE_MTS_DELTA_PATH = "/data/ufs/" + QuectelRadio::VALUE_MTS_DELTA_NAME;
+
QuectelRadio::QuectelRadio(const std::string& sName, const std::string& sRadioPort)
: CellularRadio (sName, sRadioPort)
{
@@ -466,9 +476,7 @@ ICellularRadio::CODE QuectelRadio::startOmaDm(ICellularRadio::UpdateCb& stepCb)
eCode = sendBasicCommand(sCmdOdmStart, iTimeoutOk);
if (eCode != SUCCESS) {
printError("%s| OMA DM procedure can not be started", getName().c_str());
- if (stepCb) {
- stepCb(Json::Value("OMA DM Error: OMA DM can not be started"));
- }
+ callNextStep(stepCb, "OMA DM Error: OMA DM can not be started");
break;
}
@@ -479,18 +487,14 @@ ICellularRadio::CODE QuectelRadio::startOmaDm(ICellularRadio::UpdateCb& stepCb)
// Received something unexpected or nothing at all?
if (sResponse.find(sOdmStarted) == std::string::npos) {
printError("%s| OMA DM procedure failed due to timeout", getName().c_str());
- if (stepCb) {
- stepCb(Json::Value("OMA DM Error: OMA DM failed due to timeout"));
- }
+ callNextStep(stepCb, "OMA DM Error: OMA DM failed due to timeout");
eCode = FAILURE;
break;
}
// Got "DM Started" message from the radio
printTrace("%s| OMA DM started", getName().c_str());
- if (stepCb) {
- stepCb(Json::Value("OMA DM Info: OMA DM started"));
- }
+ callNextStep(stepCb, "OMA DM Info: OMA DM started");
// Wait for the "End" or "Abnormal" response
sResponse = sendCommand("", vOdmFinishedStrings, iTimeoutEnd, 0x00);
@@ -499,9 +503,8 @@ ICellularRadio::CODE QuectelRadio::startOmaDm(ICellularRadio::UpdateCb& stepCb)
// Received "Abnormal"?
if (sResponse.find(sOdmAbnormal) != std::string::npos) {
printError("%s| OMA DM procedure failed due to internal error: [%s]", getName().c_str(), sResponse.c_str());
- if (stepCb) {
- stepCb(Json::Value("OMA DM Error: OMA DM failed due to internal error"));
- }
+ callNextStep(stepCb, "OMA DM Error: OMA DM failed due to internal error");
+
eCode = FAILURE;
break;
}
@@ -509,9 +512,8 @@ ICellularRadio::CODE QuectelRadio::startOmaDm(ICellularRadio::UpdateCb& stepCb)
// Received something unexpected or nothing at all?
if (sResponse.find(sOdmFinished) == std::string::npos) {
printError("%s| OMA DM procedure failed due to timeout", getName().c_str());
- if (stepCb) {
- stepCb(Json::Value("OMA DM Error: OMA DM failed due to timeout"));
- }
+ callNextStep(stepCb, "OMA DM Error: OMA DM failed due to timeout");
+
sendBasicCommand(sCmdOdmAbort, iTimeoutAbort); // abort the procedure
eCode = FAILURE;
break;
@@ -519,9 +521,7 @@ ICellularRadio::CODE QuectelRadio::startOmaDm(ICellularRadio::UpdateCb& stepCb)
// Got "DM End" message from the radio
printTrace("%s| OMA DM finished", getName().c_str());
- if (stepCb) {
- stepCb(Json::Value("OMA DM Info: OMA DM finished"));
- }
+ callNextStep(stepCb, "OMA DM Info: OMA DM finished");
eCode = SUCCESS;
@@ -530,6 +530,143 @@ ICellularRadio::CODE QuectelRadio::startOmaDm(ICellularRadio::UpdateCb& stepCb)
return eCode;
}
+ICellularRadio::CODE QuectelRadio::updateFumoLocal(int fd, ICellularRadio::UpdateCb& stepCb) {
+ CODE rc;
+
+ rc = fumoLocalInject(fd, stepCb);
+ if (rc != SUCCESS) {
+ return rc;
+ }
+
+ rc = fumoLocalApply(stepCb);
+ (void)fumoLocalCleanup(); // try to cleanup the injected file but cleanup errors are not fatal
+
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::fumoLocalInject(int fd, ICellularRadio::UpdateCb& stepCb) {
+ CODE rc = FAILURE;
+ bool bIsFilePresent = false;
+
+ do {
+ callNextStep(stepCb, "FUMO Info: downloading the firmware");
+
+ rc = checkFile(bIsFilePresent, VALUE_MTS_DELTA_NAME);
+ if (rc != SUCCESS) {
+ printError("Failed to check if the delta file was already download.");
+ callNextStep(stepCb, "FUMO Error: failed to download the firmware file");
+ break;
+ }
+
+ if (bIsFilePresent) {
+ rc = fumoLocalCleanup();
+ }
+
+ if (rc != SUCCESS) {
+ printError("Failed to remove the previous delta file.");
+ callNextStep(stepCb, "FUMO Error: failed to download the firmware file");
+ break;
+ }
+
+ rc = uploadFile(fd, VALUE_MTS_DELTA_NAME, stepCb);
+ if (rc != SUCCESS) {
+ printError("Failed to inject the delta file.");
+ callNextStep(stepCb, "FUMO Error: failed to download the firmware file");
+ break;
+ }
+
+ callNextStep(stepCb, "FUMO Info: firmware downloaded successfully");
+
+ } while (false);
+
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::fumoLocalCleanup() {
+ printTrace("Removing the delta upgrade file: %s", VALUE_MTS_DELTA_NAME.c_str());
+ return removeFile(VALUE_MTS_DELTA_NAME);
+}
+
+ICellularRadio::CODE QuectelRadio::fumoLocalApply(ICellularRadio::UpdateCb& stepCb) {
+ ICellularRadio::CODE rc;
+ std::string sCmd;
+
+ rc = getQuectelFirmware(m_sQuectelFirmware);
+ if (rc != SUCCESS) {
+ callNextStep(stepCb, "FUMO Error: Failed to obtain current firmware version");
+ return rc;
+ }
+ printInfo("Current firmware version: %s", m_sQuectelFirmware.c_str());
+
+ // Send "AT+QFOTADL" command to start the upgrade. OK response follows shortly.
+ sCmd = "AT+QFOTADL=\"";
+ sCmd += VALUE_MTS_DELTA_PATH;
+ sCmd += "\"";
+
+ rc = sendBasicCommand(sCmd, 1000);
+
+ if (rc != SUCCESS) {
+ printError("FUMO failed, OK not received from the radio");
+ callNextStep(stepCb, "FUMO Error: failed to apply the firmware");
+ return rc;
+ }
+
+ const uint32_t duDetachTimeout = 10000; // wait for 10 seconds for the radio to detach
+ const uint32_t duAttachTimeout = 30000; // wait for 30 seconds for the radio to attach
+ const int dMaxAttempts = 5; // the radio makes 5 attempts to update the firmware
+
+ for (int i = 0; i < dMaxAttempts; i++) {
+
+ printInfo("Waiting for the radio to enter recovery mode");
+ callNextStep(stepCb, "FUMO Info: waiting for the radio to enter recovery mode");
+
+ // Wait for the radio to detach from the USB bus
+ MTS::Thread::sleep(duDetachTimeout);
+
+ // It's now detached. Try to reconnect
+ if (!resetConnection(duAttachTimeout)) {
+ printError("Can't connect to the radio in %d ms", (duAttachTimeout));
+ callNextStep(stepCb, "FUMO Error: unable to obtain radio after reset");
+ break;
+ }
+
+ // It's now back on the bus. Wait for the URC messages.
+ printInfo("Applying the radio firmware");
+ callNextStep(stepCb, "FUMO Info: applying the radio firmware");
+ rc = fumoWaitUpgradeFinished(stepCb);
+
+ if (rc == ERROR) {
+ // unrecoverable error
+ callNextStep(stepCb, "FUMO Error: failed to apply the firmware, consider radio reset");
+ break;
+ }
+
+ if (rc != SUCCESS) {
+ // attempt failed, radio reboots and starts its next attempt
+ printError("Failed to apply the firmware, attempts left: %d", (dMaxAttempts - i - 1));
+ callNextStep(stepCb, "FUMO Error: failed to apply the firmware");
+ continue;
+ }
+
+ // Wait for the radio to finish update and reboot
+ printTrace("Waiting for the radio to come up");
+ callNextStep(stepCb, "FUMO Info: waiting for the radio to enter normal mode");
+
+ rc = fumoWaitNewFirmware(stepCb);
+ break;
+ }
+
+ if (rc == SUCCESS) {
+ printInfo("Radio firmware applied successfully");
+ callNextStep(stepCb, "FUMO Done: radio firmware applied successfully");
+ } else {
+ printError("Radio firmware has not been updated");
+ callNextStep(stepCb, "FUMO Error: radio firmware has not been updated");
+ }
+
+ return rc;
+}
+
ICellularRadio::CODE QuectelRadio::getServiceDomain(ICellularRadio::SERVICEDOMAIN& sd) {
printTrace("%s| Get Service Domain", getName().c_str());
@@ -694,3 +831,350 @@ ICellularRadio::CODE QuectelRadio::setCellularMode(CELLULAR_MODES networks) {
}
return SUCCESS;
}
+
+ICellularRadio::CODE QuectelRadio::uploadFile(int fd, const std::string& sTargetFilename, ICellularRadio::UpdateCb& stepCb) {
+ size_t dPayloadLenght;
+ size_t nChunks;
+ CODE rc;
+
+ rc = getFileSize(fd, dPayloadLenght, nChunks);
+ if (rc != SUCCESS) {
+ return rc;
+ }
+ printTrace("File size: %d bytes and %d chunks", dPayloadLenght, nChunks);
+ printTrace("Starting file upload...");
+
+ rc = startFileUpload(sTargetFilename, dPayloadLenght);
+ if (rc != SUCCESS) {
+ return rc;
+ }
+
+ printTrace("File upload started.");
+ callNextStep(stepCb, "FILE Info: Started file upload for " + sTargetFilename);
+
+ uint16_t dChecksum = 0;
+ size_t nChunksPerCent = (nChunks / 100) + 1;
+ size_t nFragmentLenght = 0;
+ std::array<char, FILE_CHUNK_SIZE> vBuffer;
+
+ for (size_t iChunk = 1; iChunk < (nChunks + 1); iChunk++) {
+
+ rc = readChunk(fd, vBuffer.data(), vBuffer.size(), nFragmentLenght);
+ if (rc != SUCCESS) {
+ break;
+ }
+
+ // we got our fragment, calculate checksum and flush the data
+ uint16_t dFragmentChecksum = getQuectelChecksum(vBuffer.data(), nFragmentLenght);
+ updateQuectelChecksum(dChecksum, dFragmentChecksum);
+
+ rc = sendData(vBuffer.data(), nFragmentLenght);
+ if (rc != SUCCESS) {
+ break;
+ }
+
+ if (stepCb && ((iChunk % nChunksPerCent) == 0)) {
+ size_t dPercentsCompleted = iChunk / nChunksPerCent;
+ callNextStep(stepCb, "FILE Info: Uploaded " + MTS::Text::format(dPercentsCompleted) + "%");
+ }
+
+ }
+
+ if (rc != SUCCESS) {
+ // cancel upload and terminate
+ callNextStep(stepCb, "FILE Error: Upload failed due to internal error");
+ abortFileUpload();
+ return rc;
+ }
+
+ printTrace("Waiting for acknoledge from the radio");
+ std::string sExpectedResult = "+QFUPL: ";
+ sExpectedResult += MTS::Text::format(dPayloadLenght);
+ sExpectedResult += ",";
+ sExpectedResult += MTS::Text::toLowerCase(MTS::Text::formatHex(dChecksum));
+
+ // "send" empty string to read acknoledge string
+ std::string sResult = sendCommand("", DEFAULT_BAIL_STRINGS, 3000, 0x00);
+
+ if (sResult.find(sExpectedResult) != std::string::npos) {
+ printDebug("Radio returned: [%s]", sResult.c_str());
+ printTrace("Upload finished, checksum matched");
+ } else {
+ printError("Upload failed: checksum mismatch. Expected: [%s], Actual: [%s]", sExpectedResult.c_str(), sResult.c_str());
+ abortFileUpload();
+ rc = FAILURE;
+ }
+
+ if (rc == SUCCESS) {
+ callNextStep(stepCb, "FILE Info: Upload finished successfully");
+ } else {
+ callNextStep(stepCb, "FILE Error: Upload failed due to internal error");
+ }
+
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::removeFile(const std::string& sTargetFilename) {
+ printTrace("Removing file [%s] from the radio memory", sTargetFilename.c_str());
+
+ const int dTimeout = 1000; //ms
+ const std::string sCmd = "AT+QFDEL=\"" + sTargetFilename + "\"";
+
+ std::string sResult = sendCommand(sCmd, ICellularRadio::DEFAULT_BAIL_STRINGS, dTimeout);
+ if (sResult.find(ICellularRadio::RSP_OK) == std::string::npos) {
+ printError("Failed to remove file [%s]: [%s]", sTargetFilename.c_str(), sResult.c_str());
+ return FAILURE;
+ }
+
+ printTrace("File [%s] removed", sTargetFilename.c_str());
+ return SUCCESS;
+}
+
+ICellularRadio::CODE QuectelRadio::checkFile(bool& bIsFilePresent, const std::string& sTargetFilename) {
+ printTrace("Checking status of the [%s] file", sTargetFilename.c_str());
+
+ const int dTimeout = 1000; //ms
+ const std::string sCmd = "AT+QFLST"; // list all files in the UFS memory
+
+ std::string sResult = sendCommand(sCmd, ICellularRadio::DEFAULT_BAIL_STRINGS, dTimeout);
+ if (sResult.rfind(ICellularRadio::RSP_OK) == std::string::npos) {
+ printError("Unable to list files from the radio memory: [%s]", sResult.c_str());
+ return FAILURE;
+ }
+
+ const std::string sExpected = "+QFLST: \"UFS:" + sTargetFilename + "\"";
+ bIsFilePresent = (sResult.find(sExpected) != std::string::npos);
+
+ return SUCCESS;
+}
+
+ICellularRadio::CODE QuectelRadio::getQuectelFirmware(std::string& sFirmware) {
+ printTrace("%s| Get Quectel-specific firmware version", getName().c_str());
+ sFirmware = ICellularRadio::VALUE_NOT_SUPPORTED;
+ std::string sCmd("AT+QGMR");
+ std::string sResult = sendCommand(sCmd);
+ size_t pos = sResult.find(ICellularRadio::RSP_OK);
+ if (pos == std::string::npos) {
+ printWarning("%s| Unable to get firmware from radio using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ sFirmware = MTS::Text::trim(sResult.substr(0, pos));
+ if(sFirmware.size() == 0) {
+ printWarning("%s| Unable to get firmware from radio using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ return SUCCESS;
+}
+
+uint16_t QuectelRadio::getQuectelChecksum(const void* data, size_t nBytes) {
+ auto castData = static_cast<const uint8_t*>(data);
+ uint16_t iChecksum = 0;
+
+ for (size_t i = 0; i < nBytes; i += 2) {
+ // If the number of the characters is odd, set the last character as the high 8 bit, and the low 8 bit as 0,
+ // and then use an XOR operator to calculate the checksum.
+ uint8_t high = castData[i];
+ uint8_t low = (i < nBytes) ? (castData[i+1]) : (0);
+
+ uint16_t iFragment = bytesToUint16(high, low);
+ updateQuectelChecksum(iChecksum, iFragment);
+ }
+
+ return iChecksum;
+}
+
+ICellularRadio::CODE QuectelRadio::getFileSize(int fd, size_t& nBytes, size_t& nChunks) {
+ CODE rc = FAILURE;
+
+ do {
+ ssize_t dScrollPos;
+ dScrollPos = lseek(fd, 0, SEEK_SET);
+ if (dScrollPos < 0) {
+ printError("Failed to seek to the start of the file: %d", errno);
+ break;
+ }
+
+ dScrollPos = lseek(fd, 0, SEEK_END);
+ if (dScrollPos < 0) {
+ printError("Failed to determine the file size: %d", errno);
+ break;
+ }
+
+ nBytes = static_cast<size_t>(dScrollPos);
+ nChunks = (nBytes + FILE_CHUNK_SIZE - 1) / FILE_CHUNK_SIZE; // round up
+
+ rc = SUCCESS;
+
+ } while (false);
+
+ lseek(fd, 0, SEEK_SET);
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::readChunk(int fd, char* pChunk, size_t dChunkSize, size_t& nReadBytes) {
+ size_t nUsedBuffer = 0;
+ CODE rc = FAILURE;
+
+ while (true) {
+
+ if (nUsedBuffer > dChunkSize) {
+ printError("Internal pointer error, abort upload: %d", nUsedBuffer);
+ rc = ERROR;
+ break;
+ }
+
+ if (nUsedBuffer == dChunkSize) {
+ // full chunk received
+ rc = SUCCESS;
+ nReadBytes = dChunkSize;
+ break;
+ }
+
+ char* pData = pChunk + nUsedBuffer;
+ size_t nFreeBuffer = dChunkSize - nUsedBuffer;
+
+ ssize_t dReadCount = read(fd, pData, nFreeBuffer);
+ if (dReadCount < 0) {
+ printError("Failed to read from the source file: %d", errno);
+ rc = ERROR;
+ break;
+ }
+
+ size_t duReadCount = static_cast<size_t>(dReadCount);
+ if (duReadCount == 0) {
+ // EOF. Return what was already read
+ nReadBytes = nUsedBuffer;
+ rc = SUCCESS;
+ break;
+ }
+
+ nUsedBuffer += duReadCount;
+
+ }
+
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::fumoWaitUpgradeFinished(ICellularRadio::UpdateCb& stepCb) {
+ const uint32_t duUrcTimeout = 4 * 60 * 1000; // wait for 4 minutes for the next URC message
+ const std::string sFotaUrcPrefix = "+QIND: \"FOTA\""; // prefix for the URC notification messages
+ const std::string sFotaUrcStart = "\"START\"";
+ const std::string sFotaUrcProgress = "\"UPDATING\"";
+ const std::string sFotaUrcEnd = "\"END\"";
+ const std::vector<std::string> vFotaBailStrings{ sFotaUrcPrefix };
+
+ CODE rc = FAILURE;
+ std::string sResponse;
+
+ while (true) { // breaks on "FOTA","END"
+ sResponse = sendCommand("", vFotaBailStrings, duUrcTimeout, 0x00);
+ printTrace("Radio response: [%s]", sResponse.c_str());
+
+ if (sResponse.find(sFotaUrcPrefix) == std::string::npos) {
+ printError("No URC messages from the radio in %d ms", duUrcTimeout);
+ callNextStep(stepCb, "FUMO error: timeout, radio is not responding");
+ rc = ERROR;
+ break;
+ }
+
+ const auto vParts = MTS::Text::split(MTS::Text::trim(sResponse), ',', 3);
+ const std::string& sStage = vParts[1];
+ if (sStage == sFotaUrcEnd) {
+ // FOTA finished
+ printTrace("Got FOTA END message");
+ const std::string& sCode = vParts[2];
+
+ if (sCode == "0") {
+ // finished successfully
+ rc = SUCCESS;
+ break;
+ }
+ // attempt failed, the radio attempts to recover
+ callNextStep(stepCb, "FUMO Error: radio returned error code " + sCode);
+ break;
+ } else if (sStage == sFotaUrcStart) {
+ printTrace("Got FOTA START message");
+ } else if (sStage == sFotaUrcProgress) {
+ printTrace("Got FOTA progress message");
+ const std::string& sPercents = vParts[2];
+ printInfo("FOTA progress: [%s]", sPercents.c_str());
+ callNextStep(stepCb, "FUMO Info: firmware apply progress " + sPercents);
+ } else {
+ printInfo("FOTA unexpected URC code: [%s]", sStage.c_str());
+ }
+ }
+
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::fumoWaitNewFirmware(ICellularRadio::UpdateCb& stepCb) {
+ MTS::Timer oTimer;
+ oTimer.start();
+ std::string sQuectelFirmware;
+ CODE rc = ERROR;
+
+ while (oTimer.getSeconds() < (5 * 60)) { // 5 minutes
+
+ MTS::Thread::sleep(10000);
+
+ if (getQuectelFirmware(sQuectelFirmware) != SUCCESS) {
+ // The radio is probably unavailable
+ resetConnection(100);
+ continue;
+ }
+
+ printInfo("Firmware version before the upgrade: %s", m_sQuectelFirmware.c_str());
+ printInfo("Current firmware version: %s", sQuectelFirmware.c_str());
+
+ if (sQuectelFirmware == m_sQuectelFirmware) {
+ // Radio will not reset anymore, firmware version left the same, not updated
+ printError("Radio firmware version not changed after upgrade");
+ rc = FAILURE;
+ break;
+ }
+
+ // The firmware numbers have changed
+ rc = SUCCESS;
+ break;
+ }
+ oTimer.stop();
+
+ if (rc == ERROR) {
+ printError("Radio is not responding");
+ callNextStep(stepCb, "FUMO Error: unable to obtain radio after reset");
+ }
+
+ return rc;
+}
+
+ICellularRadio::CODE QuectelRadio::startFileUpload(const std::string& sTargetFilename, size_t nBytes) {
+ const std::vector<std::string> vBailStrings{ ICellularRadio::RSP_CONNECT, ICellularRadio::RSP_ERROR };
+ const int dTimeout = 1000; //ms
+ std::string sCommand, sResult;
+
+ sCommand = "AT+QFUPL=\"";
+ sCommand += sTargetFilename;
+ sCommand += "\",";
+ sCommand += MTS::Text::format(nBytes);
+
+ sResult = sendCommand(sCommand, vBailStrings, dTimeout);
+ if (sResult.find(ICellularRadio::RSP_CONNECT) == std::string::npos) {
+ printError("Radio is not ready to accept the file: [%s]", sResult.c_str());
+ return FAILURE;
+ }
+
+ return SUCCESS;
+}
+
+ICellularRadio::CODE QuectelRadio::abortFileUpload() {
+ /*
+ * To prevent the “+++” from being mistaken for data, the following sequence should be followed:
+ * 1) Do not input any character within 1s or longer before inputting “+++”.
+ * 2) Input “+++” within 1s, and no other characters can be inputted during the time.
+ * 3) Do not input any character within 1s after “+++” has been inputted.
+ */
+ sleep(1);
+ return sendBasicCommand(CMD_ABORT_UPLOAD, 2000);
+}