From b9a5e7ab27ad313b95fe6642f08e230091efef0f Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Tue, 21 Jul 2020 20:24:00 +0300 Subject: Telit Delta Radio Firmware Upgrade support - libmts-io implementation Changes after code review: 1. Handle cases when the number of bytes written is different from the number of bytes requested to transfer. 2. Use fstat instead of lseek to determine the firmware size. 3. Renamed TelitRadio::startWrite and TelitRadio::abortWrite functions to better represent their applicability scope. --- src/MTS_IO_CellularRadio.cpp | 31 +++++++++++++++++++++---------- src/MTS_IO_TelitRadio.cpp | 8 ++++---- 2 files changed, 25 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/MTS_IO_CellularRadio.cpp b/src/MTS_IO_CellularRadio.cpp index 19f1b7b..49a2b32 100644 --- a/src/MTS_IO_CellularRadio.cpp +++ b/src/MTS_IO_CellularRadio.cpp @@ -21,6 +21,7 @@ #include "mts/MTS_IO_CellularRadio.h" #include +#include #include #include @@ -1138,10 +1139,21 @@ ICellularRadio::CODE CellularRadio::sendData(const char* pData, size_t nBytes) { return ERROR; } + // This limit comes from the Connection::write implementation. Otherwise we can get overflows. + const size_t maxInt32 = INT32_MAX; // iSize parameter type in Connection::write + const size_t maxUInt32 = UINT32_MAX; // return value type in Connection::write + const size_t nSizeLimit = std::min(maxInt32, maxUInt32); + + if (nBytes > nSizeLimit) { + printError("RADIO| Chunks larger than %d bytes are not supported", nSizeLimit); + return INVALID_ARGS; + } + + // Now we can ignore conversion and overflow warnings emitted by compiler. int32_t iResult; iResult = m_apIo->write(pData, nBytes); - if(iResult == -1) { + if(iResult != static_cast(nBytes)) { printError("RADIO| Failed to send data to radio"); return ERROR; } @@ -1272,21 +1284,20 @@ ICellularRadio::CODE CellularRadio::getFileSize(int fd, size_t& nBytes) { 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); + struct stat fileStatus; + + // On success, zero is returned. On error, -1 is returned, and errno is set appropriately. + if (fstat(fd, &fileStatus) < 0) { + printError("Failed to determine file size: %d", errno); break; } - dScrollPos = lseek(fd, 0, SEEK_END); - if (dScrollPos < 0) { - printError("Failed to determine the file size: %d", errno); + if (fileStatus.st_size < 0) { + printError("Failed to determine file size, file size is negative: %d", fileStatus.st_size); break; } - nBytes = static_cast(dScrollPos); - + nBytes = static_cast(fileStatus.st_size); rc = SUCCESS; } while (false); diff --git a/src/MTS_IO_TelitRadio.cpp b/src/MTS_IO_TelitRadio.cpp index a82004c..8c7e174 100644 --- a/src/MTS_IO_TelitRadio.cpp +++ b/src/MTS_IO_TelitRadio.cpp @@ -943,7 +943,7 @@ ICellularRadio::CODE TelitRadio::fumoWriteGroupsABD(int fd, ICellularRadio::Upda printTrace("File size: %d bytes and %d chunks", dPayloadLength, nChunks); printTrace("Starting file upload..."); - rc = startWrite(); + rc = startFotaWriteABD(); if (rc != SUCCESS) { return rc; } @@ -980,12 +980,12 @@ ICellularRadio::CODE TelitRadio::fumoWriteGroupsABD(int fd, ICellularRadio::Upda } // send +++ - abortWrite(); + abortFotaWriteABD(); return rc; } -ICellularRadio::CODE TelitRadio::startWrite() { +ICellularRadio::CODE TelitRadio::startFotaWriteABD() { const std::vector vBailStrings{ ICellularRadio::RSP_CONNECT, ICellularRadio::RSP_ERROR }; const int dTimeout = 1000; //ms std::string sCommand, sResult; @@ -1001,7 +1001,7 @@ ICellularRadio::CODE TelitRadio::startWrite() { return SUCCESS; } -ICellularRadio::CODE TelitRadio::abortWrite() { +ICellularRadio::CODE TelitRadio::abortFotaWriteABD() { /* * 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 “+++”. -- cgit v1.2.3