local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LinkMonitor.cpp Source File

LinkMonitor.cpp

00001 /* LinkMonitor.cpp */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 #define __DEBUG__ 0
00021 #ifndef __MODULE__
00022 #define __MODULE__ "LinkMonitor.cpp"
00023 #endif
00024 
00025 #include "core/fwk.h"
00026 
00027 #include "LinkMonitor.h"
00028 
00029 #include <cstdio>
00030 using std::sscanf;
00031 
00032 #define DEFAULT_TIMEOUT 10000
00033 
00034 LinkMonitor::LinkMonitor(ATCommandsInterface* pIf) : m_pIf(pIf), m_rssi(0), m_registrationState(REGISTRATION_STATE_UNKNOWN), m_bearer(BEARER_UNKNOWN)
00035 {
00036 
00037 }
00038 
00039 int LinkMonitor::init()
00040 {
00041   // we need to make sure that we setup the operator selection to be in 'numeric' format.
00042   // 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
00043   // setting up other network parameters in future.
00044   DBG("LinkMonitor::init() being called. This should only happen once: executinging AT+COPS=0,2");  
00045   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
00046   if(ret != OK)
00047   {
00048     WARN(" NET_PROTOCOL error from sending the AT+COPS command to the modem. ");
00049     return NET_PROTOCOL;
00050   }
00051   return OK;
00052 }
00053 
00054 /*virtual*/ int LinkMonitor::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
00055 {
00056   DBG("Line is %s", line);
00057   int v;
00058   if( sscanf(line, "+CREG: %*d,%d", &v) >= 1 ) //Reg state is valid
00059   {
00060     DBG("+CREG %d", v);
00061     switch( v )
00062     {
00063       case 0:
00064         m_registrationState = REGISTRATION_STATE_UNKNOWN;
00065         break;
00066       case 1:
00067         m_registrationState = REGISTRATION_STATE_HOME_NETWORK;
00068         break;
00069       case 2:
00070         m_registrationState = REGISTRATION_STATE_REGISTERING;
00071         break;
00072       case 3:
00073         m_registrationState = REGISTRATION_STATE_DENIED;  
00074         break;
00075       case 4:
00076         m_registrationState = REGISTRATION_STATE_NO_SIGNAL;  
00077         break;        
00078       case 5:
00079         m_registrationState = REGISTRATION_STATE_ROAMING;  
00080         break;
00081       default:
00082         m_registrationState = REGISTRATION_STATE_UNKNOWN;  
00083         break;
00084     }
00085   }
00086   else if( sscanf(line, "+COPS: %*d,%*d,\"%*[^\"]\",%d", &v) >= 1 )
00087   {
00088     DBG("+COPS %d", v);
00089     switch( v )
00090     {
00091       case 0:
00092         m_bearer = BEARER_GSM;
00093         break;
00094       case 2:
00095         m_bearer = BEARER_UMTS;
00096         break;
00097       case 3:
00098         m_bearer = BEARER_EDGE;
00099         break;
00100       case 4: //HSDPA
00101       case 5: //HSUPA
00102       case 6: //HSDPA + HSUPA
00103         m_bearer = BEARER_HSPA;
00104         break;
00105       case 7:
00106         m_bearer = BEARER_LTE;
00107         break;  
00108       case 1: //GSM Compact
00109       default:
00110         m_bearer = BEARER_UNKNOWN;
00111         break;
00112     }
00113   }
00114   else if( sscanf(line, "+CSQ: %d,%*d", &v) >= 1 )
00115   {
00116     DBG("+CSQ %d", v);
00117     if(v == 99) //Unknown
00118     {
00119       m_rssi = 0; //Unknown
00120     }
00121     else
00122     {
00123       m_rssi = -113 + 2*v;
00124     }
00125   }
00126   return OK;
00127 }
00128 
00129 /*virtual*/ int LinkMonitor::onNewEntryPrompt(ATCommandsInterface* pInst)
00130 {
00131   return OK;
00132 }
00133 
00134 int LinkMonitor::getState(int* pRssi, REGISTRATION_STATE* pRegistrationState, BEARER* pBearer)
00135 {
00136   m_rssi = 0;
00137   m_registrationState = REGISTRATION_STATE_UNKNOWN;
00138   m_bearer = BEARER_UNKNOWN;
00139   int ret = m_pIf->execute("AT+CREG?;+COPS?;+CSQ", this, NULL, DEFAULT_TIMEOUT); //Configure to get registration info & get it; get signal quality
00140   if(ret != OK)
00141   {
00142     return NET_PROTOCOL;
00143   }
00144   *pRssi = m_rssi;
00145   *pRegistrationState = m_registrationState;
00146   *pBearer = m_bearer;
00147   return OK;
00148 }