From 24310f73209b97dd805d09dcaa504d02740d1c23 Mon Sep 17 00:00:00 2001 From: Andrii Pientsov Date: Thu, 13 Feb 2020 15:59:34 +0200 Subject: Commands CREG, CGREG and CEREG should be utilized to get registered status --- src/MTS_IO_CellularRadio.cpp | 80 ++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 21 deletions(-) (limited to 'src/MTS_IO_CellularRadio.cpp') diff --git a/src/MTS_IO_CellularRadio.cpp b/src/MTS_IO_CellularRadio.cpp index 6f45da4..8bdd00e 100644 --- a/src/MTS_IO_CellularRadio.cpp +++ b/src/MTS_IO_CellularRadio.cpp @@ -736,41 +736,79 @@ void CellularRadio::initMipProfile(Json::Value& jData) { jData[ICellularRadio::KEY_MIP_MNHASS] = false; } -ICellularRadio::CODE CellularRadio::getRegistration(REGISTRATION& eRegistration) { - std::string sCmd; - std::string sResp; - - // LE910C1-NS is an LE910, so we stop the scan after the 0. - // NOTE: Eventually this may need to be changed to try all three of CREG, CGREG, and CEREG - if ((m_sName.find("LE910") != std::string::npos) || (m_sName.find("ME910") != std::string::npos)) { - // use AT+CGREG instead for LE910 models - sCmd = "AT+CGREG?"; - sResp = "+CGREG: "; - } - else { - sCmd = "AT+CREG?"; - sResp = "+CREG: "; +const std::vector CellularRadio::getRegistrationCommands() { + std::string sType; + convertModelToType(getName(), sType); + + if (sType == VALUE_TYPE_LTE) { + return { "CREG", "CGREG", "CEREG" }; + } else { + return { "CREG", "CGREG" }; } +} + +ICellularRadio::REGISTRATION CellularRadio::parseRegResponse(std::string sResult) { + size_t start = sResult.find(','); + size_t stop = sResult.find(' ', start); + std::string sRegStat = sResult.substr(start + 1, stop - start - 1); + int32_t value; + sscanf(sRegStat.c_str(), "%d", &value); + return (ICellularRadio::REGISTRATION)value; +} + +ICellularRadio::CODE CellularRadio::getRegistration(REGISTRATION& eRegistration, const std::string& sType) { + std::string sCmd = "AT+" + sType + "?"; + std::string sResp = "+" + sType + ": "; std::string sResult = sendCommand(sCmd, DEFAULT_BAIL_STRINGS, 5000); if (sResult.find(sResp) == std::string::npos) { if(sResult.size() == 0) { - printDebug("%s| Registration command returned no response: [%s]", m_sName.c_str()); + printDebug("%s| Registration command returned no response", m_sName.c_str()); return NO_RESPONSE; } printDebug("%s| Registration command returned unexpected response: [%s]", m_sName.c_str(), sResult.c_str()); return FAILURE; } - size_t start = sResult.find(','); - size_t stop = sResult.find(' ', start); - std::string sRegStat = sResult.substr(start + 1, stop - start - 1); - int32_t value; - sscanf(sRegStat.c_str(), "%d", &value); - eRegistration = (REGISTRATION)value; + eRegistration = parseRegResponse(sResult); return SUCCESS; } +ICellularRadio::CODE CellularRadio::getRegistration(REGISTRATION& eRegistration) { + /* REGISTRATION_PRIORITY: + * REGISTERED = 0 + * ROAMING = 1 + * DENIED = 2 + * SEARCHING = 3 + * NOT_REGISTERED = 4 + * UNKNOWN = 5 + */ + uint8_t uRegPriority[] = { 4, 0, 3, 2, 5, 1 }; + ICellularRadio::CODE ret = ERROR; + REGISTRATION eReg = UNKNOWN; + eRegistration = UNKNOWN; + + // We need to check CREG, CGREG, and CEREG for possible success. + // Depending on the radio, carrier, roaming, sim card, cellular mode and some other factors the registration + // will come back differently depending on the type of connection that is possible. + + const auto & commands = getRegistrationCommands(); + for (const auto & cmd : commands) { + ret = getRegistration(eReg, cmd); + if (ret != SUCCESS) { + break; + } + + if (eReg == REGISTERED || eReg == ROAMING) { + eRegistration = eReg; + break; + } + eRegistration = (uRegPriority[eRegistration] > uRegPriority[eReg]) ? eReg : eRegistration; + } + + return ret; +} + ICellularRadio::CODE CellularRadio::getCellularMode(CELLULAR_MODES &networks) { networks = CELLULAR_MODE_NA; std::string cmdResult = sendCommand("AT+COPS?"); -- cgit v1.2.3