summaryrefslogtreecommitdiff
path: root/src/MTS_IO_QuectelRadio.cpp
diff options
context:
space:
mode:
authorSerhii Kostiuk <serhii.o.kostiuk@globallogic.com>2019-06-11 18:19:36 +0300
committerSerhii Kostiuk <serhii.o.kostiuk@globallogic.com>2019-06-11 18:19:36 +0300
commit20471af5d887ab2255716c734661abb95953676c (patch)
tree6fdc82fcb14408daa318b5539cbe184aab29e6d0 /src/MTS_IO_QuectelRadio.cpp
parent69e73df7f4939f68b105362777db26e728ade091 (diff)
downloadlibmts-io-20471af5d887ab2255716c734661abb95953676c.tar.gz
libmts-io-20471af5d887ab2255716c734661abb95953676c.tar.bz2
libmts-io-20471af5d887ab2255716c734661abb95953676c.zip
[MTS-MTQ] QuectelRadio implementation
Added the full implementation of QuectelRadio class. Ported the changes from a local branch to the new libmts-io architecture.
Diffstat (limited to 'src/MTS_IO_QuectelRadio.cpp')
-rw-r--r--src/MTS_IO_QuectelRadio.cpp442
1 files changed, 442 insertions, 0 deletions
diff --git a/src/MTS_IO_QuectelRadio.cpp b/src/MTS_IO_QuectelRadio.cpp
index 365bbca..597bdac 100644
--- a/src/MTS_IO_QuectelRadio.cpp
+++ b/src/MTS_IO_QuectelRadio.cpp
@@ -1 +1,443 @@
+/*
+ * Copyright (C) 2019 by Multi-Tech Systems
+ *
+ * This file is part of libmts-io.
+ *
+ * libmts-io is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * libmts-io is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libmts-io. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
#include "mts/MTS_IO_QuectelRadio.h"
+
+#include <mts/MTS_Logger.h>
+#include <mts/MTS_Thread.h>
+#include <mts/MTS_Text.h>
+
+using namespace MTS::IO;
+
+QuectelRadio::QuectelRadio(const std::string& sName, const std::string& sRadioPort)
+: CellularRadio (sName, sRadioPort)
+{
+}
+
+bool QuectelRadio::resetRadio(uint32_t iTimeoutMillis) {
+ printInfo("%s| Rebooting radio", getName().c_str());
+ if(sendBasicCommand("AT+CFUN=1,1") == SUCCESS) {
+ if(iTimeoutMillis > 5000) {
+ MTS::Thread::sleep(5000);
+ iTimeoutMillis -= 5000;
+ }
+ return resetConnection(iTimeoutMillis);
+ }
+
+ return false;
+}
+
+CellularRadio::CODE QuectelRadio::getModel(std::string& sModel) {
+ printTrace("%s| Get Model", getName().c_str());
+ //Always returns SUCCESS because the model should be m_sName
+ sModel = getName();
+ std::string sCmd("AT+GMM");
+ std::string sResult = sendCommand(sCmd);
+ if (sResult.find("OK") == std::string::npos) {
+ printWarning("%s| Unable to get model from radio. Returning [%s]", getName().c_str(), getName().c_str());
+ return SUCCESS;
+ } else {
+ sModel = extractModelFromResult(sResult);
+ if(sModel.size() == 0) {
+ printWarning("%s| Unable to get model from radio. Returning [%s]", getName().c_str(), getName().c_str());
+ return SUCCESS;
+ }
+ }
+
+ printDebug("%s| Extracted [%s] from [%s] query", getName().c_str(), sModel.c_str(), sCmd.c_str());
+ if(sModel != getName()) {
+ printWarning("%s| Model identified [%s] does not match expected [%s]. Returning [%s]",
+ getName().c_str(), sModel.c_str(), getName().c_str(), sModel.c_str());
+ }
+
+ return SUCCESS;
+}
+
+CellularRadio::CODE QuectelRadio::getIccid(std::string& sIccid) {
+ printTrace("%s| Get ICCID", getName().c_str());
+ sIccid = VALUE_NOT_SUPPORTED;
+ std::string sCmd("AT+QCCID");
+ std::string sResult = CellularRadio::sendCommand(sCmd);
+ size_t end = sResult.find(RSP_OK);
+ if (end == std::string::npos) {
+ printWarning("%s| Unable to get ICCID from radio using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ size_t start = sResult.find("+QCCID:");
+ if(start != std::string::npos) {
+ start += sizeof("+QCCID:");
+ sIccid = MTS::Text::trim(sResult.substr(start, end-start));
+ if(sIccid.size() == 0) {
+ printWarning("%s| Unable to get ICCID from radio using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+ }
+ return SUCCESS;
+}
+
+CellularRadio::CODE QuectelRadio::getService(std::string& sService) {
+ printTrace("%s| Get Service", getName().c_str());
+ sService = VALUE_NOT_SUPPORTED;
+ std::string sCmd("AT+COPS?");
+ std::string sResult = CellularRadio::sendCommand(sCmd);
+ size_t end = sResult.find(RSP_OK);
+ if (end == std::string::npos) {
+ printWarning("%s| Unable to get Service from radio using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ // +COPS: <mode>[,<format>[,<oper>][,<Act>]]
+ int32_t iAccessTechnology = -1;
+ sscanf(sResult.c_str(), "%*d,%*d,%*s,%d", &iAccessTechnology);
+
+ switch(iAccessTechnology) {
+ case 0 : sService = "GPRS" ; break; // GSM
+ case 2 : sService = "WCDMA" ; break; // UTRAN
+ case 3 : sService = "EGPRS" ; break; // GSM W/EGPRS
+ case 4 : sService = "HSDPA" ; break; // UTRAN W/HSDPA
+ case 5 : sService = "WCDMA" ; break; // UTRAN W/HSUPA
+ case 6 : sService = "HSDPA" ; break; // UTRAN W/HSDPA and HSUPA
+ case 7 : sService = "LTE" ; break; // E-UTRAN
+ case 100 : sService = "CDMA" ; break; // CDMA
+
+ default: sService = VALUE_UNKNOWN; break;
+ }
+
+ printDebug("%s| Service ID: [%d][%s]", getName().c_str(), iAccessTechnology, sService.c_str());
+
+ return SUCCESS;
+}
+
+CellularRadio::CODE QuectelRadio::getNetwork(std::string& sNetwork) {
+ /*
+ * TODO: Refactor using MccMncTable once it'll be corrected.
+ *
+ * The proper way to determine the current network is to do that
+ * by MCC and MNC fetched from the `getNetworkStatus` and `AT+QENG` command.
+ * By using MCC and MNC from `AT+QENG` we can fetch the name of the network
+ * reported by a currently connected base station even if the SIM card is
+ * not installed or if we are currently working is a roaming mode.
+ *
+ * Until MccMncTable implementation is not fixed, we are using the name
+ * of a currently selected operator (AT+COPS).
+ */
+ printTrace("%s| Get Network", getName().c_str());
+ sNetwork = VALUE_NOT_SUPPORTED;
+ std::string sCmd("AT+COPS?");
+ std::string sResult = CellularRadio::sendCommand(sCmd);
+ size_t end = sResult.find(RSP_OK);
+
+ if (end == std::string::npos) {
+ printWarning("%s| Unable to get network name from radio using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ // +COPS: <mode>[, <format>, <oper>,<AcT>]
+ // +COPS: vParts[0],vParts[1],vParts[2],vParts[3]
+ size_t start = sResult.find(":") + 1; //Position right after "+COPS:"
+ std::vector<std::string> vParts = MTS::Text::split(MTS::Text::trim(sResult.substr(start)), ",");
+
+ if(vParts.size() > 3) {
+ sNetwork = vParts[2];
+ } else {
+ sNetwork = ""; // Not connected to any network
+ }
+
+ return SUCCESS;
+}
+
+/* AT+QENG="servingcell" - Query the information of serving cells
+
+ (GSM network)
+ +QENG:"servingscell",<state>,"GSM",<mcc>,<mnc>,<lac>,<cellid>,<bsic>,<arfcn>,<band>,<rxlev>,<txp>,<rla>,<drx>,<c1>,<c2>,<gprs>,<tch>,<ts>,<ta>,<maio>,<hsn>,<rxlevsub>,<rxlevfull>,<rxqualsub>,<rxqualfull>,<voicecodec>
+
+ (WCDMA network)
+ +QENG:"servingcell",<state>,"WCDMA",<mcc>,<mnc>,<lac>,<cellid>,<uarfcn>,<psc>,<rac>,<rscp>,<ecio>,<phych>,<sf>,<slot>,<speech_code>,<comMod>
+
+ (LTE Network)
+ +QENG:"servingcell",<state>,"LTE",<is_tdd>,<mcc>,<mnc>,<cellid>,<pcid>,<earfcn>,<freq_band_ind>,<ul_bandwidth>,<dl_bandwidth>,<tac>,<rsrp>,<rsrq>,<rssi>,<sinr>,<srxlev>
+
+ The following modes are NOT currently handled:
+ - TD-SCDMA mode;
+ - CDMA mode;
+ - HDR mode;
+ - SRLTE mode.
+
+ In the case of TD-SCDMA mode:
+ +QENG:"servingscell",<state>,"TDSCDMA",<mcc>,<mnc>,<lac>,<cellid>,<pfreq>,<rssi>,<rscp>,<ecio>
+
+ In the case of CDMA mode or CDMA+HDR mode:
+ +QENG:"servingscell",<state>,"CDMA",<mcc>,<mnc>,<lac>,<cellid>,<bcch>,<rxpwr>,<ecio>,<txpwr>
+ [+QENG:"servingscell",<state>,"HDR",<mcc>,<mnc>,<lac>,<cellid>,<bcch>,<rxpwr>,<ecio>,<txpwr>]
+
+ In the case of SRLTE mode:
+ +QENG:"servingscell",<state>,"CDMA",<mcc>,<mnc>,<lac>,<cellid>,<bcch>,<rxpwr>,<ecio>,<txpwr>
+ +QENG:"servingcell",<state>,"LTE",<is_tdd>,<mcc>,<mnc>,<cellid>,<pcid>,<earfcn>,<freq_band_ind>,<ul_bandwidth>,<dl_bandwidth>,<tac>,<rsrp>,<rsrq>,<rssi>,<sinr><srxlev>
+*/
+CellularRadio::CODE QuectelRadio::getNetworkStatus(Json::Value& jData) {
+ int32_t iValue;
+ ACTIVEBAND abnd;
+ SERVICEDOMAIN sd;
+ std::string sValue;
+ const uint32_t GSM_NETWORK_FORMAT = 27;
+ const uint32_t WCDMA_NETWORK_FORMAT = 17;
+ const uint32_t LTE_NETWORK_FORMAT = 18;
+
+ printTrace("%s| Get Network Status", getName().c_str());
+
+ //Always get common network stats because this should never fail
+ //This way the basic stats are always returned even if AT+QENG fails below
+ getCommonNetworkStats(jData);
+
+ // IMSI is not provided by AT+QENG. Fetch it separately to keep the same interface
+ if (getImsi(sValue) == SUCCESS) {
+ jData[KEY_IMSI] = sValue;
+ }
+
+ // Network Name is not explicitly provided by AT+QENG. Fetch it separately to keep the same interface
+ // TODO: Replace with lookup by MCC and MNC once MccMncTable is fixed.
+ if (getNetwork(sValue) == SUCCESS) {
+ jData[KEY_NETWORK] = sValue;
+ }
+
+ // Service Domain is not provided by AT+QENG. Fetch it separately to keep the same interface
+ if (getServiceDomain(sd) == SUCCESS && convertServiceDomainToString(sd, sValue) == SUCCESS) {
+ jData[KEY_SD] = sValue;
+ }
+
+ std::string sCmd;
+ std::string sResult;
+
+ sCmd = "AT+QENG=\"servingcell\"";
+ sResult = sendCommand(sCmd, DEFAULT_BAIL_STRINGS, 200);
+ if (sResult.find("+QENG:\"servingscell\"") == std::string::npos) {
+ printDebug("%s| Network Status command returned unexpected response: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
+ printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
+ return SUCCESS; //return SUCCESS because getCommonNetworkStats() succeeded at top of this function
+ }
+
+ size_t start = sResult.find(":") + 1; //Position right after "+QENG:"
+ std::vector<std::string> vParts = MTS::Text::split(MTS::Text::trim(sResult.substr(start)), ",");
+ Json::Value jDebug;
+
+ if (vParts.size() < 3) {
+ printDebug("%s| Network Status command reponse is an unknown format: [%s][%s]", getName().c_str(), sCmd.c_str(), sResult.c_str());
+ printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
+ return SUCCESS; //return SUCCESS because getCommonNetworkStats() succeeded at top of this function
+ }
+
+ // +QENG:"servingscell",<state>,"GSM",<mcc>,<mnc>,<lac>,<cellid>,<bsic>,<arfcn>,<band>,<rxlev>,<txp>,<rla>,<drx>,<c1>,<c2>,<gprs>,<tch>,<ts>,<ta>,<maio>,<hsn>,<rxlevsub>,<rxlevfull>,<rxqualsub>,<rxqualfull>,<voicecodec>
+ // +QENG: [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13],[14],[15], [16], [17],[18],[19], [20], [21], [22], [23], [24], [25], [26]
+ if (vParts.size() == GSM_NETWORK_FORMAT ) {
+ //Parse as GSM Network Format
+ jData[KEY_MCC] = vParts[3];
+ jData[KEY_MNC] = vParts[4];
+ jData[KEY_LAC] = vParts[5];
+ jData[KEY_CID] = vParts[6];
+ jData[KEY_CHANNEL] = vParts[8];
+
+ if (convertToActiveBand(vParts[9], abnd) == SUCCESS && convertActiveBandToString(abnd, sValue) == SUCCESS) {
+ jData[KEY_ABND] = sValue;
+ }
+
+ if (MTS::Text::parse(iValue, vParts[10])) {
+ // Number format. RX level value for base station selection in dB (see 3GPP 25.304).
+ // Range: 0-63. Subtracting 111 from the RX level value, a dBm value will be got.
+ int rssi = iValue - 111;
+ jData[KEY_RSSIDBM] = MTS::Text::format(rssi);
+ }
+
+ jData[KEY_TXPWR] = vParts[11];
+
+ // The following fields can NOT be fetched for Quectel in GSM mode: RAC, MM, RR, NOM
+
+ jData[KEY_DEBUG] = jDebug;
+ }
+
+ // +QENG:"servingcell",<state>,"WCDMA",<mcc>,<mnc>,<lac>,<cellid>,<uarfcn>,<psc>,<rac>,<rscp>,<ecio>,<phych>,<sf>,<slot>,<speech_code>,<comMod>
+ // +QENG: [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12],[13], [14], [15], [16]
+ else if((vParts.size() == WCDMA_NETWORK_FORMAT)) {
+ //Parse as WCDMA Network Format
+ jData[KEY_MCC] = vParts[3];
+ jData[KEY_MNC] = vParts[4];
+ jData[KEY_LAC] = vParts[5];
+ jData[KEY_CID] = vParts[6];
+ jData[KEY_CHANNEL] = vParts[7];
+ jDebug[KEY_PSC] = vParts[8];
+ jData[KEY_RAC] = vParts[9];
+ jDebug[KEY_RSCP] = vParts[10];
+ jDebug[KEY_ECIO] = vParts[11];
+
+ // The following fields can NOT be fetched for Quectel in WCDMA mode: TXPWR, DRX, MM, RR, NOM, BLER
+
+ // RSSI is not provided by AT+QENG in WCDMA mode. It was filled above by the getCommonNetworkStats
+
+ // BLER is not provided by AT+QENG. Set to constant
+ jDebug[KEY_BLER] = "000";
+
+ // Get the radio band given the channel (UARFCN)
+ RadioBandMap radioBandMap(vParts[7], CellularRadio::VALUE_TYPE_CDMA);
+ jData[KEY_ABND] = radioBandMap.getRadioBandName();
+
+ jData[KEY_DEBUG] = jDebug;
+ }
+
+ // +QENG:"servingcell",<state>,"LTE",<is_tdd>,<mcc>,<mnc>,<cellid>,<pcid>,<earfcn>,<freq_band_ind>,<ul_bandwidth>,<dl_bandwidth>,<tac>,<rsrp>,<rsrq>,<rssi>,<sinr>,<srxlev>
+ // +QENG: [0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17]
+ else if(vParts.size() == LTE_NETWORK_FORMAT) {
+ //Parse as LTE Network Format
+ jData[KEY_MCC] = vParts[4];
+ jData[KEY_MNC] = vParts[5];
+ jData[KEY_CID] = vParts[6];
+ jData[KEY_CHANNEL] = vParts[8];
+ jData["tac"] = vParts[12];
+ jDebug["rsrp"] = vParts[13];
+ jDebug["rsrq"] = vParts[14];
+ jData[KEY_RSSIDBM] = vParts[15];
+
+ // Get the radio band given the channel (EARFCN)
+ RadioBandMap radioBandMap(vParts[8], CellularRadio::VALUE_TYPE_LTE);
+ jData[KEY_ABND] = radioBandMap.getRadioBandName();
+
+ // LAC is not provided by AT+QENG in WCDMA mode. Use another command instead
+ jData[KEY_LAC] = queryLteLac();
+
+ jData[KEY_DEBUG] = jDebug;
+ }
+
+ printTrace("%s| Network Status:\n%s\n", getName().c_str(), jData.toStyledString().c_str());
+ return SUCCESS;
+}
+
+CellularRadio::CODE QuectelRadio::convertSignalStrengthTodBm(const int32_t& iRssi, int32_t& iDbm) {
+ int dbmSteps, minValue, maxValue, rssiOffset;
+ int rawDbm;
+
+ if(iRssi >= 0 && iRssi < 99) {
+ // normal scaling
+ dbmSteps = 2;
+ minValue = -113;
+ maxValue = -51;
+ rssiOffset = 0;
+ } else if(iRssi >= 100 && iRssi < 199) {
+ // TD-SCDMA scaling
+ dbmSteps = 1;
+ minValue = -116;
+ maxValue = -25;
+ rssiOffset = 100;
+ } else {
+ return FAILURE; // invalid, not known or not detectable
+ }
+
+ rawDbm = minValue + ((iRssi - rssiOffset) * dbmSteps);
+ iDbm = std::min(maxValue, rawDbm);
+
+ return SUCCESS;
+}
+
+CellularRadio::CODE QuectelRadio::convertdBmToSignalStrength(const int32_t& iDBm, int32_t& iRssi) {
+ //Quectel Conversion FOR NORMAL SCALING
+ const int dbmSteps = 2;
+ const int minValue = -113;
+ const int rssiOffset = 0;
+
+ if (iDBm < -113) {
+ iRssi = 0;
+ } else if (iDBm > -51) {
+ iRssi = 31;
+ } else {
+ iRssi = ((iDBm - minValue) / dbmSteps) + rssiOffset;
+ }
+
+ return SUCCESS;
+}
+
+CellularRadio::CODE QuectelRadio::setMdn(const Json::Value& jArgs) {
+ printTrace("%s| Set MDN", getName().c_str());
+ return NOT_APPLICABLE;
+}
+
+CellularRadio::CODE QuectelRadio::getServiceDomain(CellularRadio::SERVICEDOMAIN& sd) {
+ printTrace("%s| Get Service Domain", getName().c_str());
+
+ std::string sCmd("AT+QCFG=\"servicedomain\"");
+ std::string sResult = CellularRadio::sendCommand(sCmd);
+ size_t end = sResult.find(RSP_OK);
+
+ if (end == std::string::npos) {
+ printWarning("%s| Unable to get service domain using command [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ // +QCFG: "servicedomain",<service>
+ size_t start = sResult.find(",") + 1; // Position right after comma
+ std::string sServiceDomain = MTS::Text::trim(sResult.substr(start, end));
+ int iValue = -1;
+
+ if (!MTS::Text::parse(iValue, sServiceDomain)) {
+ printWarning("%s| Failed to parse service domain from command output [%s]", getName().c_str(), sCmd.c_str());
+ return FAILURE;
+ }
+
+ switch (iValue) {
+ case 0: sd = SERVICEDOMAIN::CS_ONLY; break;
+ case 1: sd = SERVICEDOMAIN::PS_ONLY; break;
+ case 2: sd = SERVICEDOMAIN::CSPS; break;
+
+ default: return FAILURE; // Unknown
+ }
+
+ return SUCCESS;
+}
+
+bool QuectelRadio::getCarrierFromFirmware(const std::string& sFirmware, std::string& sCarrier) {
+ // TODO: Implement properly if needed
+ // This function is used only in CdmaRadio implementation but was defined in
+ // the CellularRadio class before refactoring.
+
+ // Find out if and how we can determine the operator by firmware version.
+
+ return false;
+}
+
+bool QuectelRadio::getHardwareVersionFromFirmware(const std::string& sFirmware, std::string& sHardware) {
+ // TODO: Implement properly
+ // Find out if and how we can extract the hardware version from firmware version on Quectel devices
+
+ return false;
+}
+
+CellularRadio::CODE QuectelRadio::convertToActiveBand(const std::string& sQuectelBand, CellularRadio::ACTIVEBAND& band) {
+ int iQuectelBand = -1;
+
+ if (!MTS::Text::parse(iQuectelBand, sQuectelBand)) {
+ return FAILURE; // probably "-", other band
+ }
+
+ switch (iQuectelBand) {
+ case 0: band = ACTIVEBAND::DCS_1800; break;
+ case 1: band = ACTIVEBAND::PCS_1900; break;
+
+ default: return FAILURE; // actually, this case should never happen
+ }
+
+ return SUCCESS;
+}