2 /* Copyright (C) 2012 mbed.org, MIT License
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #define __MODULE__ "LinkMonitor.cpp"
27 #include "LinkMonitor.h"
32 #define DEFAULT_TIMEOUT 10000
34 LinkMonitor::LinkMonitor(ATCommandsInterface* pIf) : m_pIf(pIf), m_rssi(0), m_registrationState(REGISTRATION_STATE_UNKNOWN), m_bearer(BEARER_UNKNOWN)
39 int LinkMonitor::init(bool gsm)
44 // we need to make sure that we setup the operator selection to be in 'numeric' format.
45 // i.e. it is made up of a network and country code when returned by the modem e.g. Operator = 23415. This allows easy logic parsing for
46 // setting up other network parameters in future.
47 DBG("LinkMonitor::init() being called. This should only happen once: executinging AT+COPS=0,2");
48 int ret = m_pIf->executeSimple("AT+COPS=0,2", NULL, DEFAULT_TIMEOUT); //Configure to set the operator string to Country Code and mobile network code
51 WARN(" NET_PROTOCOL error from sending the AT+COPS command to the modem. ");
58 /*virtual*/ int LinkMonitor::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
60 DBG("Line is %s", line);
64 if( sscanf(line, "+CREG: %*d,%d", &v) >= 1 ) //Reg state is valid
70 m_registrationState = REGISTRATION_STATE_UNKNOWN;
73 m_registrationState = REGISTRATION_STATE_HOME_NETWORK;
76 m_registrationState = REGISTRATION_STATE_REGISTERING;
79 m_registrationState = REGISTRATION_STATE_DENIED;
82 m_registrationState = REGISTRATION_STATE_NO_SIGNAL;
85 m_registrationState = REGISTRATION_STATE_ROAMING;
88 m_registrationState = REGISTRATION_STATE_UNKNOWN;
92 else if( sscanf(line, "+COPS: %*d,%*d,\"%*[^\"]\",%d", &v) >= 1 )
98 m_bearer = BEARER_GSM;
101 m_bearer = BEARER_UMTS;
104 m_bearer = BEARER_EDGE;
108 case 6: //HSDPA + HSUPA
109 m_bearer = BEARER_HSPA;
112 m_bearer = BEARER_LTE;
114 case 1: //GSM Compact
116 m_bearer = BEARER_UNKNOWN;
120 else if( sscanf(line, "+CSQ: %d,%*d", &v) >= 1 )
123 if(v == 99) //Unknown
125 m_rssi = 0; //Unknown
132 else if ( (sscanf(line, "+CNUM: \"%[^\"]\",\"%[^\"]\",%d", n, s, &v) == 3) ||
133 (sscanf(line, "+CNUM: \"\",\"%[^\"]\",%d", s, &v) == 2) )
135 if (*s && ((v == 145/*number includes + */) || (v == 129/*otherwise*/))) {
136 strcpy(m_phoneNumber, s);
142 /*virtual*/ int LinkMonitor::onNewEntryPrompt(ATCommandsInterface* pInst)
147 int LinkMonitor::getState(int* pRssi, REGISTRATION_STATE* pRegistrationState, BEARER* pBearer)
150 m_registrationState = REGISTRATION_STATE_UNKNOWN;
151 m_bearer = BEARER_UNKNOWN;
152 int ret = m_pIf->execute(m_gsm ? "AT+CREG?;+COPS?;+CSQ" : "AT+CREG?;+CSQ", this, NULL, DEFAULT_TIMEOUT); //Configure to get registration info & get it; get signal quality
158 *pRegistrationState = m_registrationState;
163 int LinkMonitor::getPhoneNumber(char* phoneNumber)
165 *m_phoneNumber = '\0';
167 int ret = m_pIf->execute("AT+CNUM", this, NULL, DEFAULT_TIMEOUT);
173 strcpy(phoneNumber, m_phoneNumber);