summaryrefslogtreecommitdiff
path: root/src/MTS_IO_CellularRadio.cpp
diff options
context:
space:
mode:
authorSerhii Kostiuk <serhii.o.kostiuk@globallogic.com>2020-07-21 20:24:00 +0300
committerSerhii Kostiuk <serhii.o.kostiuk@globallogic.com>2020-07-22 10:18:37 +0300
commitb9a5e7ab27ad313b95fe6642f08e230091efef0f (patch)
treedb50efd264bc4dbb01548bf22cc5ec502703074b /src/MTS_IO_CellularRadio.cpp
parenta9a598dab448dd0d062725e2f3a60bcf68c6fe9e (diff)
downloadlibmts-io-b9a5e7ab27ad313b95fe6642f08e230091efef0f.tar.gz
libmts-io-b9a5e7ab27ad313b95fe6642f08e230091efef0f.tar.bz2
libmts-io-b9a5e7ab27ad313b95fe6642f08e230091efef0f.zip
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.
Diffstat (limited to 'src/MTS_IO_CellularRadio.cpp')
-rw-r--r--src/MTS_IO_CellularRadio.cpp31
1 files changed, 21 insertions, 10 deletions
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 <unistd.h>
+#include <sys/stat.h>
#include <mts/MTS_IO_MccMncTable.h>
#include <mts/MTS_Thread.h>
@@ -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<int32_t>(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<size_t>(dScrollPos);
-
+ nBytes = static_cast<size_t>(fileStatus.st_size);
rc = SUCCESS;
} while (false);