Base library for cellular modem implementations

Dependencies:   Socket lwip-sys lwip

Dependents:   CellularUSBModem CellularUSBModem

Deprecated

This is an mbed 2 networking library. For mbed 5, the networking libraries have been revised to better support additional network stacks and thread safety here.

Committer:
bogdanm
Date:
Thu Oct 17 12:29:05 2013 +0300
Revision:
1:4a23efdf0da9
Child:
5:9a57892f206c
Initial release of the CellularModem library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 1:4a23efdf0da9 1 /* LinkMonitor.cpp */
bogdanm 1:4a23efdf0da9 2 /* Copyright (C) 2012 mbed.org, MIT License
bogdanm 1:4a23efdf0da9 3 *
bogdanm 1:4a23efdf0da9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
bogdanm 1:4a23efdf0da9 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
bogdanm 1:4a23efdf0da9 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
bogdanm 1:4a23efdf0da9 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
bogdanm 1:4a23efdf0da9 8 * furnished to do so, subject to the following conditions:
bogdanm 1:4a23efdf0da9 9 *
bogdanm 1:4a23efdf0da9 10 * The above copyright notice and this permission notice shall be included in all copies or
bogdanm 1:4a23efdf0da9 11 * substantial portions of the Software.
bogdanm 1:4a23efdf0da9 12 *
bogdanm 1:4a23efdf0da9 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
bogdanm 1:4a23efdf0da9 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
bogdanm 1:4a23efdf0da9 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
bogdanm 1:4a23efdf0da9 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bogdanm 1:4a23efdf0da9 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
bogdanm 1:4a23efdf0da9 18 */
bogdanm 1:4a23efdf0da9 19
bogdanm 1:4a23efdf0da9 20 #define __DEBUG__ 0
bogdanm 1:4a23efdf0da9 21 #ifndef __MODULE__
bogdanm 1:4a23efdf0da9 22 #define __MODULE__ "LinkMonitor.cpp"
bogdanm 1:4a23efdf0da9 23 #endif
bogdanm 1:4a23efdf0da9 24
bogdanm 1:4a23efdf0da9 25 #include "core/fwk.h"
bogdanm 1:4a23efdf0da9 26
bogdanm 1:4a23efdf0da9 27 #include "LinkMonitor.h"
bogdanm 1:4a23efdf0da9 28
bogdanm 1:4a23efdf0da9 29 #include <cstdio>
bogdanm 1:4a23efdf0da9 30 using std::sscanf;
bogdanm 1:4a23efdf0da9 31
bogdanm 1:4a23efdf0da9 32 #define DEFAULT_TIMEOUT 10000
bogdanm 1:4a23efdf0da9 33
bogdanm 1:4a23efdf0da9 34 LinkMonitor::LinkMonitor(ATCommandsInterface* pIf) : m_pIf(pIf), m_rssi(0), m_registrationState(REGISTRATION_STATE_UNKNOWN), m_bearer(BEARER_UNKNOWN)
bogdanm 1:4a23efdf0da9 35 {
bogdanm 1:4a23efdf0da9 36
bogdanm 1:4a23efdf0da9 37 }
bogdanm 1:4a23efdf0da9 38
bogdanm 1:4a23efdf0da9 39 int LinkMonitor::init()
bogdanm 1:4a23efdf0da9 40 {
bogdanm 1:4a23efdf0da9 41 // we need to make sure that we setup the operator selection to be in 'numeric' format.
bogdanm 1:4a23efdf0da9 42 // 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
bogdanm 1:4a23efdf0da9 43 // setting up other network parameters in future.
bogdanm 1:4a23efdf0da9 44 DBG("LinkMonitor::init() being called. This should only happen once: executinging AT+COPS=0,2");
bogdanm 1:4a23efdf0da9 45 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
bogdanm 1:4a23efdf0da9 46 if(ret != OK)
bogdanm 1:4a23efdf0da9 47 {
bogdanm 1:4a23efdf0da9 48 WARN(" NET_PROTOCOL error from sending the AT+COPS command to the modem. ");
bogdanm 1:4a23efdf0da9 49 return NET_PROTOCOL;
bogdanm 1:4a23efdf0da9 50 }
bogdanm 1:4a23efdf0da9 51 return OK;
bogdanm 1:4a23efdf0da9 52 }
bogdanm 1:4a23efdf0da9 53
bogdanm 1:4a23efdf0da9 54 /*virtual*/ int LinkMonitor::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
bogdanm 1:4a23efdf0da9 55 {
bogdanm 1:4a23efdf0da9 56 DBG("Line is %s", line);
bogdanm 1:4a23efdf0da9 57 int v;
bogdanm 1:4a23efdf0da9 58 if( sscanf(line, "+CREG: %*d,%d", &v) >= 1 ) //Reg state is valid
bogdanm 1:4a23efdf0da9 59 {
bogdanm 1:4a23efdf0da9 60 DBG("+CREG %d", v);
bogdanm 1:4a23efdf0da9 61 switch( v )
bogdanm 1:4a23efdf0da9 62 {
bogdanm 1:4a23efdf0da9 63 case 0:
bogdanm 1:4a23efdf0da9 64 m_registrationState = REGISTRATION_STATE_UNKNOWN;
bogdanm 1:4a23efdf0da9 65 break;
bogdanm 1:4a23efdf0da9 66 case 1:
bogdanm 1:4a23efdf0da9 67 m_registrationState = REGISTRATION_STATE_HOME_NETWORK;
bogdanm 1:4a23efdf0da9 68 break;
bogdanm 1:4a23efdf0da9 69 case 2:
bogdanm 1:4a23efdf0da9 70 m_registrationState = REGISTRATION_STATE_REGISTERING;
bogdanm 1:4a23efdf0da9 71 break;
bogdanm 1:4a23efdf0da9 72 case 3:
bogdanm 1:4a23efdf0da9 73 m_registrationState = REGISTRATION_STATE_DENIED;
bogdanm 1:4a23efdf0da9 74 break;
bogdanm 1:4a23efdf0da9 75 case 4:
bogdanm 1:4a23efdf0da9 76 m_registrationState = REGISTRATION_STATE_NO_SIGNAL;
bogdanm 1:4a23efdf0da9 77 break;
bogdanm 1:4a23efdf0da9 78 case 5:
bogdanm 1:4a23efdf0da9 79 m_registrationState = REGISTRATION_STATE_ROAMING;
bogdanm 1:4a23efdf0da9 80 break;
bogdanm 1:4a23efdf0da9 81 default:
bogdanm 1:4a23efdf0da9 82 m_registrationState = REGISTRATION_STATE_UNKNOWN;
bogdanm 1:4a23efdf0da9 83 break;
bogdanm 1:4a23efdf0da9 84 }
bogdanm 1:4a23efdf0da9 85 }
bogdanm 1:4a23efdf0da9 86 else if( sscanf(line, "+COPS: %*d,%*d,\"%*[^\"]\",%d", &v) >= 1 )
bogdanm 1:4a23efdf0da9 87 {
bogdanm 1:4a23efdf0da9 88 DBG("+COPS %d", v);
bogdanm 1:4a23efdf0da9 89 switch( v )
bogdanm 1:4a23efdf0da9 90 {
bogdanm 1:4a23efdf0da9 91 case 0:
bogdanm 1:4a23efdf0da9 92 m_bearer = BEARER_GSM;
bogdanm 1:4a23efdf0da9 93 break;
bogdanm 1:4a23efdf0da9 94 case 2:
bogdanm 1:4a23efdf0da9 95 m_bearer = BEARER_UMTS;
bogdanm 1:4a23efdf0da9 96 break;
bogdanm 1:4a23efdf0da9 97 case 3:
bogdanm 1:4a23efdf0da9 98 m_bearer = BEARER_EDGE;
bogdanm 1:4a23efdf0da9 99 break;
bogdanm 1:4a23efdf0da9 100 case 4: //HSDPA
bogdanm 1:4a23efdf0da9 101 case 5: //HSUPA
bogdanm 1:4a23efdf0da9 102 case 6: //HSDPA + HSUPA
bogdanm 1:4a23efdf0da9 103 m_bearer = BEARER_HSPA;
bogdanm 1:4a23efdf0da9 104 break;
bogdanm 1:4a23efdf0da9 105 case 7:
bogdanm 1:4a23efdf0da9 106 m_bearer = BEARER_LTE;
bogdanm 1:4a23efdf0da9 107 break;
bogdanm 1:4a23efdf0da9 108 case 1: //GSM Compact
bogdanm 1:4a23efdf0da9 109 default:
bogdanm 1:4a23efdf0da9 110 m_bearer = BEARER_UNKNOWN;
bogdanm 1:4a23efdf0da9 111 break;
bogdanm 1:4a23efdf0da9 112 }
bogdanm 1:4a23efdf0da9 113 }
bogdanm 1:4a23efdf0da9 114 else if( sscanf(line, "+CSQ: %d,%*d", &v) >= 1 )
bogdanm 1:4a23efdf0da9 115 {
bogdanm 1:4a23efdf0da9 116 DBG("+CSQ %d", v);
bogdanm 1:4a23efdf0da9 117 if(v == 99) //Unknown
bogdanm 1:4a23efdf0da9 118 {
bogdanm 1:4a23efdf0da9 119 m_rssi = 0; //Unknown
bogdanm 1:4a23efdf0da9 120 }
bogdanm 1:4a23efdf0da9 121 else
bogdanm 1:4a23efdf0da9 122 {
bogdanm 1:4a23efdf0da9 123 m_rssi = -113 + 2*v;
bogdanm 1:4a23efdf0da9 124 }
bogdanm 1:4a23efdf0da9 125 }
bogdanm 1:4a23efdf0da9 126 return OK;
bogdanm 1:4a23efdf0da9 127 }
bogdanm 1:4a23efdf0da9 128
bogdanm 1:4a23efdf0da9 129 /*virtual*/ int LinkMonitor::onNewEntryPrompt(ATCommandsInterface* pInst)
bogdanm 1:4a23efdf0da9 130 {
bogdanm 1:4a23efdf0da9 131 return OK;
bogdanm 1:4a23efdf0da9 132 }
bogdanm 1:4a23efdf0da9 133
bogdanm 1:4a23efdf0da9 134 int LinkMonitor::getState(int* pRssi, REGISTRATION_STATE* pRegistrationState, BEARER* pBearer)
bogdanm 1:4a23efdf0da9 135 {
bogdanm 1:4a23efdf0da9 136 m_rssi = 0;
bogdanm 1:4a23efdf0da9 137 m_registrationState = REGISTRATION_STATE_UNKNOWN;
bogdanm 1:4a23efdf0da9 138 m_bearer = BEARER_UNKNOWN;
bogdanm 1:4a23efdf0da9 139 int ret = m_pIf->execute("AT+CREG?;+COPS?;+CSQ", this, NULL, DEFAULT_TIMEOUT); //Configure to get registration info & get it; get signal quality
bogdanm 1:4a23efdf0da9 140 if(ret != OK)
bogdanm 1:4a23efdf0da9 141 {
bogdanm 1:4a23efdf0da9 142 return NET_PROTOCOL;
bogdanm 1:4a23efdf0da9 143 }
bogdanm 1:4a23efdf0da9 144 *pRssi = m_rssi;
bogdanm 1:4a23efdf0da9 145 *pRegistrationState = m_registrationState;
bogdanm 1:4a23efdf0da9 146 *pBearer = m_bearer;
bogdanm 1:4a23efdf0da9 147 return OK;
bogdanm 1:4a23efdf0da9 148 }