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