From 5a7d8772ea898b3567685431b5a6be13015e5887 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Mon, 10 Jun 2019 11:27:27 +0300 Subject: [MTS-MTQ] refactoring: static function moved to ICellularRadio --- include/mts/MTS_IO_CellularRadio.h | 15 ---- include/mts/MTS_IO_ICellularRadio.h | 17 ++++ src/MTS_IO_CellularRadio.cpp | 160 +---------------------------------- src/MTS_IO_CellularRadioFactory.cpp | 2 +- src/MTS_IO_ICellularRadio.cpp | 164 +++++++++++++++++++++++++++++++++++- 5 files changed, 182 insertions(+), 176 deletions(-) diff --git a/include/mts/MTS_IO_CellularRadio.h b/include/mts/MTS_IO_CellularRadio.h index e79e6d1..0410e0d 100644 --- a/include/mts/MTS_IO_CellularRadio.h +++ b/include/mts/MTS_IO_CellularRadio.h @@ -109,27 +109,12 @@ namespace MTS { const char& ESC = CR) override; - static std::string sendCommand(MTS::AutoPtr& apIo, - const std::string& sCmd, - const std::vector& vBail = DEFAULT_BAIL_STRINGS, - int32_t timeoutMillis = 100, - const char& ESC = CR); std::string sendCommand(const std::string& sCmd, IsNeedMoreData& isNeedMoreData, int32_t timeoutMillis = 100, const char& ESC = CR) override; - static std::string sendCommand(MTS::AutoPtr& apIo, - const std::string& sCmd, - IsNeedMoreData& isNeedMoreData, - int32_t timeoutMillis = 100, - const char& ESC = CR); - - static CODE test(MTS::AutoPtr& apIo, uint32_t timeoutSeconds = 30); - - static std::string extractModelFromResult(const std::string& sResult); - static std::string getCodeAsString(CODE code); protected: diff --git a/include/mts/MTS_IO_ICellularRadio.h b/include/mts/MTS_IO_ICellularRadio.h index ff8383c..3ab8b7e 100644 --- a/include/mts/MTS_IO_ICellularRadio.h +++ b/include/mts/MTS_IO_ICellularRadio.h @@ -7,6 +7,9 @@ #include +#include +#include + #define EXPORT __attribute__((visibility("default"))) namespace MTS { @@ -62,6 +65,20 @@ namespace MTS { static CODE convertModelToMtsShortCode(const std::string& sModel, std::string& sCode, ICellularRadio *radioObj = NULL); static CODE convertServiceDomainToString(SERVICEDOMAIN eSd, std::string& sSd); static CODE convertActiveBandToString(ACTIVEBAND eBand, std::string& sBand); + + static std::string sendCommand(MTS::AutoPtr& apIo, + const std::string& sCmd, + IsNeedMoreData& isNeedMoreData, + int32_t timeoutMillis = 100, + const char& ESC = CR); + static std::string sendCommand(MTS::AutoPtr& apIo, + const std::string& sCmd, + const std::vector& vBail = DEFAULT_BAIL_STRINGS, + int32_t timeoutMillis = 100, + const char& ESC = CR); + static CODE test(MTS::AutoPtr& apIo, uint32_t timeoutSeconds = 30); + static std::string extractModelFromResult(const std::string& sResult); + static std::string getCodeAsString(CODE code); // XXX static const std::string DEFAULT_RADIO_PORT; diff --git a/src/MTS_IO_CellularRadio.cpp b/src/MTS_IO_CellularRadio.cpp index 840132f..f106b4c 100644 --- a/src/MTS_IO_CellularRadio.cpp +++ b/src/MTS_IO_CellularRadio.cpp @@ -968,167 +968,11 @@ CellularRadio::CODE CellularRadio::sendBasicCommand(const std::string& sCmd, int } std::string CellularRadio::sendCommand(const std::string& sCmd, const std::vector& vBail, int32_t timeoutMillis, const char& ESC) { - return sendCommand(m_apIo, sCmd, vBail, timeoutMillis, ESC); -} - -std::string CellularRadio::sendCommand(MTS::AutoPtr& apIo, const std::string& sCmd, - const std::vector& 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); + return ICellularRadio::sendCommand(m_apIo, sCmd, vBail, timeoutMillis, ESC); } std::string CellularRadio::sendCommand(const std::string& sCmd, MTS::IO::CellularRadio::IsNeedMoreData& isNeedMoreData, int32_t timeoutMillis, const char& ESC) { - return sendCommand(m_apIo, sCmd, isNeedMoreData, timeoutMillis, ESC); -} - -std::string CellularRadio::sendCommand(MTS::AutoPtr& 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; -} - -CellularRadio::CODE CellularRadio::test(MTS::AutoPtr& 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 CellularRadio::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 CellularRadio::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"; - } + return ICellularRadio::sendCommand(m_apIo, sCmd, isNeedMoreData, timeoutMillis, ESC); } bool CellularRadio::splitAndAssign(const std::string& sLine, const std::string& sKey, Json::Value& jParent, const std::string& sJsonKey, Json::ValueType eType) { diff --git a/src/MTS_IO_CellularRadioFactory.cpp b/src/MTS_IO_CellularRadioFactory.cpp index 130d3ed..0c64221 100644 --- a/src/MTS_IO_CellularRadioFactory.cpp +++ b/src/MTS_IO_CellularRadioFactory.cpp @@ -102,7 +102,7 @@ std::string CellularRadioFactory::identifyRadio(const std::string& sPort) { std::string sCmd("ATI4"); std::string sResult; do { - sResult = TelitRadio::sendCommand(apIo, sCmd, TelitRadio::DEFAULT_BAIL_STRINGS, 1000, TelitRadio::CR); + sResult = ICellularRadio::sendCommand(apIo, sCmd, TelitRadio::DEFAULT_BAIL_STRINGS, 1000, TelitRadio::CR); if (sResult.find("OK") == std::string::npos) { printDebug("RADIO| Attempting to get radio model [%s] ...", sResult.c_str()); } else { 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 +#include + 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& apIo, const std::string& sCmd, + const std::vector& 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& 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& 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"; + } +} + -- cgit v1.2.3