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@5:9a57892f206c, 2013-12-17 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Dec 17 14:30:17 2013 +0000
- Revision:
- 5:9a57892f206c
- Parent:
- 1:4a23efdf0da9
- Child:
- 7:0fd95907b5b3
Synchronized with git revision 8ec31ead8026189df63512b6d5ae69081b0aaf18
Full URL: https://github.com/mbedmicro/mbed/commit/8ec31ead8026189df63512b6d5ae69081b0aaf18/
Detection of modem when using serial port, CDMA version of linkmonitor
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); |
bogdanm | 1:4a23efdf0da9 | 61 | int v; |
bogdanm | 1:4a23efdf0da9 | 62 | if( sscanf(line, "+CREG: %*d,%d", &v) >= 1 ) //Reg state is valid |
bogdanm | 1:4a23efdf0da9 | 63 | { |
bogdanm | 1:4a23efdf0da9 | 64 | DBG("+CREG %d", v); |
bogdanm | 1:4a23efdf0da9 | 65 | switch( v ) |
bogdanm | 1:4a23efdf0da9 | 66 | { |
bogdanm | 1:4a23efdf0da9 | 67 | case 0: |
bogdanm | 1:4a23efdf0da9 | 68 | m_registrationState = REGISTRATION_STATE_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 69 | break; |
bogdanm | 1:4a23efdf0da9 | 70 | case 1: |
bogdanm | 1:4a23efdf0da9 | 71 | m_registrationState = REGISTRATION_STATE_HOME_NETWORK; |
bogdanm | 1:4a23efdf0da9 | 72 | break; |
bogdanm | 1:4a23efdf0da9 | 73 | case 2: |
bogdanm | 1:4a23efdf0da9 | 74 | m_registrationState = REGISTRATION_STATE_REGISTERING; |
bogdanm | 1:4a23efdf0da9 | 75 | break; |
bogdanm | 1:4a23efdf0da9 | 76 | case 3: |
bogdanm | 1:4a23efdf0da9 | 77 | m_registrationState = REGISTRATION_STATE_DENIED; |
bogdanm | 1:4a23efdf0da9 | 78 | break; |
bogdanm | 1:4a23efdf0da9 | 79 | case 4: |
bogdanm | 1:4a23efdf0da9 | 80 | m_registrationState = REGISTRATION_STATE_NO_SIGNAL; |
bogdanm | 1:4a23efdf0da9 | 81 | break; |
bogdanm | 1:4a23efdf0da9 | 82 | case 5: |
bogdanm | 1:4a23efdf0da9 | 83 | m_registrationState = REGISTRATION_STATE_ROAMING; |
bogdanm | 1:4a23efdf0da9 | 84 | break; |
bogdanm | 1:4a23efdf0da9 | 85 | default: |
bogdanm | 1:4a23efdf0da9 | 86 | m_registrationState = REGISTRATION_STATE_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 87 | break; |
bogdanm | 1:4a23efdf0da9 | 88 | } |
bogdanm | 1:4a23efdf0da9 | 89 | } |
bogdanm | 1:4a23efdf0da9 | 90 | else if( sscanf(line, "+COPS: %*d,%*d,\"%*[^\"]\",%d", &v) >= 1 ) |
bogdanm | 1:4a23efdf0da9 | 91 | { |
bogdanm | 1:4a23efdf0da9 | 92 | DBG("+COPS %d", v); |
bogdanm | 1:4a23efdf0da9 | 93 | switch( v ) |
bogdanm | 1:4a23efdf0da9 | 94 | { |
bogdanm | 1:4a23efdf0da9 | 95 | case 0: |
bogdanm | 1:4a23efdf0da9 | 96 | m_bearer = BEARER_GSM; |
bogdanm | 1:4a23efdf0da9 | 97 | break; |
bogdanm | 1:4a23efdf0da9 | 98 | case 2: |
bogdanm | 1:4a23efdf0da9 | 99 | m_bearer = BEARER_UMTS; |
bogdanm | 1:4a23efdf0da9 | 100 | break; |
bogdanm | 1:4a23efdf0da9 | 101 | case 3: |
bogdanm | 1:4a23efdf0da9 | 102 | m_bearer = BEARER_EDGE; |
bogdanm | 1:4a23efdf0da9 | 103 | break; |
bogdanm | 1:4a23efdf0da9 | 104 | case 4: //HSDPA |
bogdanm | 1:4a23efdf0da9 | 105 | case 5: //HSUPA |
bogdanm | 1:4a23efdf0da9 | 106 | case 6: //HSDPA + HSUPA |
bogdanm | 1:4a23efdf0da9 | 107 | m_bearer = BEARER_HSPA; |
bogdanm | 1:4a23efdf0da9 | 108 | break; |
bogdanm | 1:4a23efdf0da9 | 109 | case 7: |
bogdanm | 1:4a23efdf0da9 | 110 | m_bearer = BEARER_LTE; |
bogdanm | 1:4a23efdf0da9 | 111 | break; |
bogdanm | 1:4a23efdf0da9 | 112 | case 1: //GSM Compact |
bogdanm | 1:4a23efdf0da9 | 113 | default: |
bogdanm | 1:4a23efdf0da9 | 114 | m_bearer = BEARER_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 115 | break; |
bogdanm | 1:4a23efdf0da9 | 116 | } |
bogdanm | 1:4a23efdf0da9 | 117 | } |
bogdanm | 1:4a23efdf0da9 | 118 | else if( sscanf(line, "+CSQ: %d,%*d", &v) >= 1 ) |
bogdanm | 1:4a23efdf0da9 | 119 | { |
bogdanm | 1:4a23efdf0da9 | 120 | DBG("+CSQ %d", v); |
bogdanm | 1:4a23efdf0da9 | 121 | if(v == 99) //Unknown |
bogdanm | 1:4a23efdf0da9 | 122 | { |
bogdanm | 1:4a23efdf0da9 | 123 | m_rssi = 0; //Unknown |
bogdanm | 1:4a23efdf0da9 | 124 | } |
bogdanm | 1:4a23efdf0da9 | 125 | else |
bogdanm | 1:4a23efdf0da9 | 126 | { |
bogdanm | 1:4a23efdf0da9 | 127 | m_rssi = -113 + 2*v; |
bogdanm | 1:4a23efdf0da9 | 128 | } |
bogdanm | 1:4a23efdf0da9 | 129 | } |
bogdanm | 1:4a23efdf0da9 | 130 | return OK; |
bogdanm | 1:4a23efdf0da9 | 131 | } |
bogdanm | 1:4a23efdf0da9 | 132 | |
bogdanm | 1:4a23efdf0da9 | 133 | /*virtual*/ int LinkMonitor::onNewEntryPrompt(ATCommandsInterface* pInst) |
bogdanm | 1:4a23efdf0da9 | 134 | { |
bogdanm | 1:4a23efdf0da9 | 135 | return OK; |
bogdanm | 1:4a23efdf0da9 | 136 | } |
bogdanm | 1:4a23efdf0da9 | 137 | |
bogdanm | 1:4a23efdf0da9 | 138 | int LinkMonitor::getState(int* pRssi, REGISTRATION_STATE* pRegistrationState, BEARER* pBearer) |
bogdanm | 1:4a23efdf0da9 | 139 | { |
bogdanm | 1:4a23efdf0da9 | 140 | m_rssi = 0; |
bogdanm | 1:4a23efdf0da9 | 141 | m_registrationState = REGISTRATION_STATE_UNKNOWN; |
bogdanm | 1:4a23efdf0da9 | 142 | m_bearer = BEARER_UNKNOWN; |
mbed_official | 5:9a57892f206c | 143 | 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 | 144 | if(ret != OK) |
bogdanm | 1:4a23efdf0da9 | 145 | { |
bogdanm | 1:4a23efdf0da9 | 146 | return NET_PROTOCOL; |
bogdanm | 1:4a23efdf0da9 | 147 | } |
bogdanm | 1:4a23efdf0da9 | 148 | *pRssi = m_rssi; |
bogdanm | 1:4a23efdf0da9 | 149 | *pRegistrationState = m_registrationState; |
bogdanm | 1:4a23efdf0da9 | 150 | *pBearer = m_bearer; |
bogdanm | 1:4a23efdf0da9 | 151 | return OK; |
bogdanm | 1:4a23efdf0da9 | 152 | } |