summaryrefslogtreecommitdiff
path: root/src/MTS_IO_ICellularRadio.cpp
diff options
context:
space:
mode:
authorMaksym Telychko <maksym.telychko@globallogic.com>2019-06-10 11:27:27 +0300
committerMaksym Telychko <maksym.telychko@globallogic.com>2019-06-10 11:27:27 +0300
commit5a7d8772ea898b3567685431b5a6be13015e5887 (patch)
tree96084a4cc149322d18d0adea52b5c85e952e314e /src/MTS_IO_ICellularRadio.cpp
parent596f8f8393bf837d73e0a62c87d52557d9191f96 (diff)
downloadlibmts-io-5a7d8772ea898b3567685431b5a6be13015e5887.tar.gz
libmts-io-5a7d8772ea898b3567685431b5a6be13015e5887.tar.bz2
libmts-io-5a7d8772ea898b3567685431b5a6be13015e5887.zip
[MTS-MTQ] refactoring: static function moved to ICellularRadio
Diffstat (limited to 'src/MTS_IO_ICellularRadio.cpp')
-rw-r--r--src/MTS_IO_ICellularRadio.cpp164
1 files changed, 162 insertions, 2 deletions
diff --git a/src/MTS_IO_ICellularRadio.cpp b/src/MTS_IO_ICellularRadio.cpp
index 343480a..e206a59 100644
--- a/src/MTS_IO_ICellularRadio.cpp
+++ b/src/MTS_IO_ICellularRadio.cpp
@@ -1,5 +1,8 @@
#include "mts/MTS_IO_ICellularRadio.h"
+#include <mts/MTS_Logger.h>
+#include <mts/MTS_Timer.h>
+
const char MTS::IO::ICellularRadio::ETX = 0x03; //Ends socket connection
const char MTS::IO::ICellularRadio::DLE = 0x10; //Escapes ETX and DLE within Payload
const char MTS::IO::ICellularRadio::CR = 0x0D;
@@ -180,7 +183,7 @@ MTS::IO::ICellularRadio::CODE MTS::IO::ICellularRadio::convertModelToMtsShortCod
eCode = SUCCESS;
} else {
sCode = VALUE_NOT_SUPPORTED;
- //printError("RADIO| Could not identify MTS short code from model. [%s]", sModel.c_str());
+ printError("RADIO| Could not identify MTS short code from model. [%s]", sModel.c_str());
eCode = ERROR;
}
return eCode;
@@ -274,7 +277,164 @@ MTS::IO::ICellularRadio::CODE MTS::IO::ICellularRadio::convertModelToType(const
} else {
sType = VALUE_TYPE_GSM;
eCode = ERROR;
- //printError("RADIO| Could not identify type from model. [%s]. Assuming [%s]", sModel.c_str(), sType.c_str());
+ printError("RADIO| Could not identify type from model. [%s]. Assuming [%s]", sModel.c_str(), sType.c_str());
}
return eCode;
}
+
+std::string MTS::IO::ICellularRadio::sendCommand(MTS::AutoPtr<MTS::IO::Connection>& apIo, const std::string& sCmd,
+ const std::vector<std::string>& vBail, int32_t timeoutMillis, const char& ESC) {
+ IsNeedMoreData isNeedMoreData = [&vBail](const std::string&, const std::string& allData)->bool {
+ for(size_t i = 0; i < vBail.size(); i++) {
+ const std::string& sBail = vBail[i];
+ if(sBail.size() > 0) {
+ if(allData.find(sBail) != std::string::npos) {
+ //Return when bail string is found
+ printTrace("RADIO| Found bail string [%s]", sBail.c_str());
+ return false;
+ }
+ }
+ }
+ return true;
+ };
+
+ return sendCommand(apIo, sCmd, isNeedMoreData, timeoutMillis, ESC);
+}
+
+std::string MTS::IO::ICellularRadio::sendCommand(MTS::AutoPtr<MTS::IO::Connection>& apIo, const std::string& sCmd,
+ IsNeedMoreData& isNeedMoreData, int32_t timeoutMillis, const char& ESC) {
+ if(MTS::Logger::getPrintLevel() >= MTS::Logger::PrintLevel::TRACE_LEVEL) {
+ printTrace("RADIO| Sending command [%s]", sCmd.c_str());
+ }
+ if(apIo.isNull()) {
+ printError("RADIO| IO is not set in sendCommand");
+ return "";
+ }
+
+ int32_t iResult;
+ if(ESC == 0x00) {
+ iResult = apIo->write(sCmd.data(), sCmd.size());
+ } else {
+ std::string sNewCmd(sCmd);
+ sNewCmd.push_back(ESC);
+ iResult = apIo->write(sNewCmd.data(), sNewCmd.size());
+ }
+
+ if(iResult == -1) {
+ printError("RADIO| Failed to send command to radio");
+ return "";
+ }
+
+ bool done = false;
+ const uint32_t capacity = 1024;
+ char buffer[capacity];
+ std::string sResult;
+ Timer timer;
+ timer.start();
+ do {
+ int32_t iterationTimeout = 100;
+ int bytesRead = apIo->read((char*)buffer, capacity, iterationTimeout);
+ if(bytesRead == -1) {
+ printError("RADIO| Failed to read from radio");
+ break;
+ }
+
+ std::string sIteration((char*)buffer, bytesRead);
+ sResult += sIteration;
+
+ if(isNeedMoreData && !isNeedMoreData(sIteration, sResult)) {
+ printTrace("RADIO| No more data needed");
+ return sResult;
+ }
+ if(timeoutMillis >= 0) {
+ done = (timer.getMillis() >= (uint64_t)timeoutMillis);
+ } else {
+ //Do not stop looping until bail string is found
+ }
+ } while(!done);
+
+ //Timed out
+ return sResult;
+}
+
+MTS::IO::ICellularRadio::CODE MTS::IO::ICellularRadio::test(MTS::AutoPtr<MTS::IO::Connection>& apIo, uint32_t timeoutSeconds) {
+ printTrace("RADIO| Basic Test");
+ uint32_t count = 0;
+ std::string sCmd("AT");
+ do {
+ std::string sResult = sendCommand(apIo, sCmd);
+ if (sResult.find(RSP_OK) == std::string::npos) {
+ printTrace("RADIO| Waiting for basic radio communication [%s] ...", sResult.c_str());
+ } else {
+ break;
+ }
+ count++;
+ } while (count < timeoutSeconds);
+
+ if(count == timeoutSeconds) {
+ printWarning("RADIO| Basic radio communication FAILED");
+ return NO_RESPONSE;
+ }
+ return SUCCESS;
+}
+
+std::string MTS::IO::ICellularRadio::extractModelFromResult(const std::string& sResult) {
+ std::string sModel(VALUE_NOT_SUPPORTED);
+
+ if(sResult.find("HE910-D") != std::string::npos) {
+ sModel = "HE910-D";
+ } else if(sResult.find("HE910-EUD") != std::string::npos) {
+ sModel = "HE910-EUD";
+ } else if(sResult.find("LE910-JN1") != std::string::npos) {
+ sModel = "LE910-JN1";
+ } else if(sResult.find("LE866A1-JS") != std::string::npos) {
+ sModel = "LE866A1-JS";
+ } else if(sResult.find("LE910-NAG") != std::string::npos) {
+ sModel = "LE910-NAG";
+ } else if(sResult.find("LE910C4-NF") != std::string::npos) {
+ sModel = "LE910C4-NF";
+ } else if(sResult.find("LE910-NA1") != std::string::npos) {
+ sModel = "LE910-NA1";
+ } else if(sResult.find("ME910C1-NA") != std::string::npos) {
+ sModel = "ME910C1-NA";
+ } else if(sResult.find("ME910C1-NV") != std::string::npos) {
+ sModel = "ME910C1-NV";
+ } else if(sResult.find("ME910C1-WW") != std::string::npos) {
+ sModel = "ME910C1-WW";
+ } else if(sResult.find("LE910-SVG") != std::string::npos) {
+ sModel = "LE910-SVG";
+ } else if(sResult.find("LE910-EUG") != std::string::npos) {
+ sModel = "LE910-EUG";
+ } else if(sResult.find("LE910C4-EU") != std::string::npos) {
+ sModel = "LE910C4-EU";
+ } else if(sResult.find("LE910-EU1") != std::string::npos) {
+ sModel = "LE910-EU1";
+ } else if(sResult.find("LE910C1-NS") != std::string::npos) {
+ sModel = "LE910C1-NS";
+ } else if(sResult.find("LE910C1-AP") != std::string::npos) {
+ sModel = "LE910C1-AP";
+ } else if(sResult.find("GE910") != std::string::npos) {
+ sModel = "GE910";
+ } else if(sResult.find("DE910-DUAL") != std::string::npos) {
+ sModel = "DE910-DUAL";
+ } else if(sResult.find("CE910") != std::string::npos) {
+ sModel = "CE910";
+ }
+ return sModel;
+}
+
+std::string MTS::IO::ICellularRadio::getCodeAsString(CODE eCode) {
+ switch(eCode) {
+ case SUCCESS:
+ return "SUCCESS";
+ case ERROR:
+ return "ERROR";
+ case FAILURE:
+ return "FAILURE";
+ case NO_RESPONSE:
+ return "NO RESPONSE";
+ default:
+ return "UNKNOWN";
+ }
+}
+