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.
link/LinkMonitor.cpp@7:0fd95907b5b3, 2014-03-04 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Mar 04 10:00:52 2014 +0000
- Revision:
- 7:0fd95907b5b3
- Parent:
- 5:9a57892f206c
Synchronized with git revision e4faeb42a7013bfc4d1dccf0a6d905d10aca5c00
Full URL: https://github.com/mbedmicro/mbed/commit/e4faeb42a7013bfc4d1dccf0a6d905d10aca5c00/
Update of cellular modem
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
mbed_official | 5:9a57892f206c | 36 | m_gsm = true; |
bogdanm | 1:4a23efdf0da9 | 37 | } |
bogdanm | 1:4a23efdf0da9 | 38 | |
mbed_official | 5:9a57892f206c | 39 | int LinkMonitor::init(bool gsm) |
bogdanm | 1:4a23efdf0da9 | 40 | { |
mbed_official | 5:9a57892f206c | 41 | m_gsm = gsm; |
mbed_official | 5:9a57892f206c | 42 | if (m_gsm) |
bogdanm | 1:4a23efdf0da9 | 43 | { |
mbed_official | 5:9a57892f206c | 44 | // we need to make sure that we setup the operator selection to be in 'numeric' format. |
mbed_official | 5:9a57892f206c | 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 |
mbed_official | 5:9a57892f206c | 46 | // setting up other network parameters in future. |
mbed_official | 5:9a57892f206c | 47 | DBG("LinkMonitor::init() being called. This should only happen once: executinging AT+COPS=0,2"); |
mbed_official | 5:9a57892f206c | 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 |
mbed_official | 5:9a57892f206c | 49 | if(ret != OK) |
mbed_official | 5:9a57892f206c | 50 | { |
mbed_official | 5:9a57892f206c | 51 | WARN(" NET_PROTOCOL error from sending the AT+COPS command to the modem. "); |
mbed_official | 5:9a57892f206c | 52 | return NET_PROTOCOL; |
mbed_official | 5:9a57892f206c | 53 | } |
bogdanm | 1:4a23efdf0da9 | 54 | } |
bogdanm | 1:4a23efdf0da9 | 55 | return OK; |
bogdanm | 1:4a23efdf0da9 | 56 | } |
bogdanm | 1:4a23efdf0da9 | 57 | |
bogdanm | 1:4a23efdf0da9 | 58 | /*virtual*/ int LinkMonitor::onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
bogdanm | 1:4a23efdf0da9 | 59 | { |
bogdanm | 1:4a23efdf0da9 | 60 | DBG("Line is %s", line); |
mbed_official | 7:0fd95907b5b3 | 61 | char n[32] = ""; |
mbed_official | 7:0fd95907b5b3 | 62 | char s[32] = ""; |
bogdanm | 1:4a23efdf0da9 | 63 | int v; |
bogdanm | 1:4a23efdf0da9 | 64 | if( sscanf(line, "+CREG: %*d,%d", &v) >= 1 ) //Reg state is valid |
bogdanm | 1:4a23efdf0da9 | 65 | { |
bogdanm | 1:4a23efdf0da9 | 66 | DBG("+CREG %d", v); |
bogdanm | 1:4a23efdf0da9 | 67 | switch( v ) |
bogdanm | 1:4a23efdf0da9 | 68 | { |
bogdanm | 1:4a23efdf0da9 | 69 | case 0: |
bogdanm | 1:4a23efdf0da9 | 70 | m_registrationState = REGISTRATION_STATE_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 71 | break; |
bogdanm | 1:4a23efdf0da9 | 72 | case 1: |
bogdanm | 1:4a23efdf0da9 | 73 | m_registrationState = REGISTRATION_STATE_HOME_NETWORK; |
bogdanm | 1:4a23efdf0da9 | 74 | break; |
bogdanm | 1:4a23efdf0da9 | 75 | case 2: |
bogdanm | 1:4a23efdf0da9 | 76 | m_registrationState = REGISTRATION_STATE_REGISTERING; |
bogdanm | 1:4a23efdf0da9 | 77 | break; |
bogdanm | 1:4a23efdf0da9 | 78 | case 3: |
bogdanm | 1:4a23efdf0da9 | 79 | m_registrationState = REGISTRATION_STATE_DENIED; |
bogdanm | 1:4a23efdf0da9 | 80 | break; |
bogdanm | 1:4a23efdf0da9 | 81 | case 4: |
bogdanm | 1:4a23efdf0da9 | 82 | m_registrationState = REGISTRATION_STATE_NO_SIGNAL; |
bogdanm | 1:4a23efdf0da9 | 83 | break; |
bogdanm | 1:4a23efdf0da9 | 84 | case 5: |
bogdanm | 1:4a23efdf0da9 | 85 | m_registrationState = REGISTRATION_STATE_ROAMING; |
bogdanm | 1:4a23efdf0da9 | 86 | break; |
bogdanm | 1:4a23efdf0da9 | 87 | default: |
bogdanm | 1:4a23efdf0da9 | 88 | m_registrationState = REGISTRATION_STATE_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 89 | break; |
bogdanm | 1:4a23efdf0da9 | 90 | } |
bogdanm | 1:4a23efdf0da9 | 91 | } |
bogdanm | 1:4a23efdf0da9 | 92 | else if( sscanf(line, "+COPS: %*d,%*d,\"%*[^\"]\",%d", &v) >= 1 ) |
bogdanm | 1:4a23efdf0da9 | 93 | { |
bogdanm | 1:4a23efdf0da9 | 94 | DBG("+COPS %d", v); |
bogdanm | 1:4a23efdf0da9 | 95 | switch( v ) |
bogdanm | 1:4a23efdf0da9 | 96 | { |
bogdanm | 1:4a23efdf0da9 | 97 | case 0: |
bogdanm | 1:4a23efdf0da9 | 98 | m_bearer = BEARER_GSM; |
bogdanm | 1:4a23efdf0da9 | 99 | break; |
bogdanm | 1:4a23efdf0da9 | 100 | case 2: |
bogdanm | 1:4a23efdf0da9 | 101 | m_bearer = BEARER_UMTS; |
bogdanm | 1:4a23efdf0da9 | 102 | break; |
bogdanm | 1:4a23efdf0da9 | 103 | case 3: |
bogdanm | 1:4a23efdf0da9 | 104 | m_bearer = BEARER_EDGE; |
bogdanm | 1:4a23efdf0da9 | 105 | break; |
bogdanm | 1:4a23efdf0da9 | 106 | case 4: //HSDPA |
bogdanm | 1:4a23efdf0da9 | 107 | case 5: //HSUPA |
bogdanm | 1:4a23efdf0da9 | 108 | case 6: //HSDPA + HSUPA |
bogdanm | 1:4a23efdf0da9 | 109 | m_bearer = BEARER_HSPA; |
bogdanm | 1:4a23efdf0da9 | 110 | break; |
bogdanm | 1:4a23efdf0da9 | 111 | case 7: |
bogdanm | 1:4a23efdf0da9 | 112 | m_bearer = BEARER_LTE; |
bogdanm | 1:4a23efdf0da9 | 113 | break; |
bogdanm | 1:4a23efdf0da9 | 114 | case 1: //GSM Compact |
bogdanm | 1:4a23efdf0da9 | 115 | default: |
bogdanm | 1:4a23efdf0da9 | 116 | m_bearer = BEARER_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 117 | break; |
bogdanm | 1:4a23efdf0da9 | 118 | } |
bogdanm | 1:4a23efdf0da9 | 119 | } |
bogdanm | 1:4a23efdf0da9 | 120 | else if( sscanf(line, "+CSQ: %d,%*d", &v) >= 1 ) |
bogdanm | 1:4a23efdf0da9 | 121 | { |
bogdanm | 1:4a23efdf0da9 | 122 | DBG("+CSQ %d", v); |
bogdanm | 1:4a23efdf0da9 | 123 | if(v == 99) //Unknown |
bogdanm | 1:4a23efdf0da9 | 124 | { |
bogdanm | 1:4a23efdf0da9 | 125 | m_rssi = 0; //Unknown |
bogdanm | 1:4a23efdf0da9 | 126 | } |
bogdanm | 1:4a23efdf0da9 | 127 | else |
bogdanm | 1:4a23efdf0da9 | 128 | { |
bogdanm | 1:4a23efdf0da9 | 129 | m_rssi = -113 + 2*v; |
bogdanm | 1:4a23efdf0da9 | 130 | } |
bogdanm | 1:4a23efdf0da9 | 131 | } |
mbed_official | 7:0fd95907b5b3 | 132 | else if ( (sscanf(line, "+CNUM: \"%[^\"]\",\"%[^\"]\",%d", n, s, &v) == 3) || |
mbed_official | 7:0fd95907b5b3 | 133 | (sscanf(line, "+CNUM: \"\",\"%[^\"]\",%d", s, &v) == 2) ) |
mbed_official | 7:0fd95907b5b3 | 134 | { |
mbed_official | 7:0fd95907b5b3 | 135 | if (*s && ((v == 145/*number includes + */) || (v == 129/*otherwise*/))) { |
mbed_official | 7:0fd95907b5b3 | 136 | strcpy(m_phoneNumber, s); |
mbed_official | 7:0fd95907b5b3 | 137 | } |
mbed_official | 7:0fd95907b5b3 | 138 | } |
bogdanm | 1:4a23efdf0da9 | 139 | return OK; |
bogdanm | 1:4a23efdf0da9 | 140 | } |
bogdanm | 1:4a23efdf0da9 | 141 | |
bogdanm | 1:4a23efdf0da9 | 142 | /*virtual*/ int LinkMonitor::onNewEntryPrompt(ATCommandsInterface* pInst) |
bogdanm | 1:4a23efdf0da9 | 143 | { |
bogdanm | 1:4a23efdf0da9 | 144 | return OK; |
bogdanm | 1:4a23efdf0da9 | 145 | } |
bogdanm | 1:4a23efdf0da9 | 146 | |
bogdanm | 1:4a23efdf0da9 | 147 | int LinkMonitor::getState(int* pRssi, REGISTRATION_STATE* pRegistrationState, BEARER* pBearer) |
bogdanm | 1:4a23efdf0da9 | 148 | { |
bogdanm | 1:4a23efdf0da9 | 149 | m_rssi = 0; |
bogdanm | 1:4a23efdf0da9 | 150 | m_registrationState = REGISTRATION_STATE_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 151 | m_bearer = BEARER_UNKNOWN; |
mbed_official | 5:9a57892f206c | 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 |
bogdanm | 1:4a23efdf0da9 | 153 | if(ret != OK) |
bogdanm | 1:4a23efdf0da9 | 154 | { |
bogdanm | 1:4a23efdf0da9 | 155 | return NET_PROTOCOL; |
bogdanm | 1:4a23efdf0da9 | 156 | } |
bogdanm | 1:4a23efdf0da9 | 157 | *pRssi = m_rssi; |
bogdanm | 1:4a23efdf0da9 | 158 | *pRegistrationState = m_registrationState; |
bogdanm | 1:4a23efdf0da9 | 159 | *pBearer = m_bearer; |
bogdanm | 1:4a23efdf0da9 | 160 | return OK; |
bogdanm | 1:4a23efdf0da9 | 161 | } |
mbed_official | 7:0fd95907b5b3 | 162 | |
mbed_official | 7:0fd95907b5b3 | 163 | int LinkMonitor::getPhoneNumber(char* phoneNumber) |
mbed_official | 7:0fd95907b5b3 | 164 | { |
mbed_official | 7:0fd95907b5b3 | 165 | *m_phoneNumber = '\0'; |
mbed_official | 7:0fd95907b5b3 | 166 | if (m_gsm) { |
mbed_official | 7:0fd95907b5b3 | 167 | int ret = m_pIf->execute("AT+CNUM", this, NULL, DEFAULT_TIMEOUT); |
mbed_official | 7:0fd95907b5b3 | 168 | if(ret != OK) |
mbed_official | 7:0fd95907b5b3 | 169 | { |
mbed_official | 7:0fd95907b5b3 | 170 | return NET_PROTOCOL; |
mbed_official | 7:0fd95907b5b3 | 171 | } |
mbed_official | 7:0fd95907b5b3 | 172 | } |
mbed_official | 7:0fd95907b5b3 | 173 | strcpy(phoneNumber, m_phoneNumber); |
mbed_official | 7:0fd95907b5b3 | 174 | return OK; |
mbed_official | 7:0fd95907b5b3 | 175 | } |