Used in Live Traffic Update Nokia LCD Display Project
Fork of NetServices by
drv/gprs/GPRSModem.cpp@8:92b57208ab99, 2013-03-06 (annotated)
- Committer:
- rrajan8
- Date:
- Wed Mar 06 19:07:23 2013 +0000
- Revision:
- 8:92b57208ab99
- Parent:
- 0:ac1725ba162c
This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
segundo | 0:ac1725ba162c | 1 | |
segundo | 0:ac1725ba162c | 2 | /* |
segundo | 0:ac1725ba162c | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
segundo | 0:ac1725ba162c | 4 | |
segundo | 0:ac1725ba162c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
segundo | 0:ac1725ba162c | 6 | of this software and associated documentation files (the "Software"), to deal |
segundo | 0:ac1725ba162c | 7 | in the Software without restriction, including without limitation the rights |
segundo | 0:ac1725ba162c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
segundo | 0:ac1725ba162c | 9 | copies of the Software, and to permit persons to whom the Software is |
segundo | 0:ac1725ba162c | 10 | furnished to do so, subject to the following conditions: |
segundo | 0:ac1725ba162c | 11 | |
segundo | 0:ac1725ba162c | 12 | The above copyright notice and this permission notice shall be included in |
segundo | 0:ac1725ba162c | 13 | all copies or substantial portions of the Software. |
segundo | 0:ac1725ba162c | 14 | |
segundo | 0:ac1725ba162c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
segundo | 0:ac1725ba162c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
segundo | 0:ac1725ba162c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
segundo | 0:ac1725ba162c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
segundo | 0:ac1725ba162c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
segundo | 0:ac1725ba162c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
segundo | 0:ac1725ba162c | 21 | THE SOFTWARE. |
segundo | 0:ac1725ba162c | 22 | */ |
segundo | 0:ac1725ba162c | 23 | |
segundo | 0:ac1725ba162c | 24 | #include "GPRSModem.h" |
segundo | 0:ac1725ba162c | 25 | #include "mbed.h" |
segundo | 0:ac1725ba162c | 26 | |
segundo | 0:ac1725ba162c | 27 | //#define __DEBUG |
segundo | 0:ac1725ba162c | 28 | #include "dbg/dbg.h" |
segundo | 0:ac1725ba162c | 29 | |
segundo | 0:ac1725ba162c | 30 | #define WAIT_BTW_NETW_POLLS 3. |
segundo | 0:ac1725ba162c | 31 | |
segundo | 0:ac1725ba162c | 32 | #include "netCfg.h" |
segundo | 0:ac1725ba162c | 33 | #if NET_GPRS |
segundo | 0:ac1725ba162c | 34 | |
segundo | 0:ac1725ba162c | 35 | GPRSModem::GPRSModem() : ATIf() |
segundo | 0:ac1725ba162c | 36 | { |
segundo | 0:ac1725ba162c | 37 | DBG("New GPRSModem@%p\n", this); |
segundo | 0:ac1725ba162c | 38 | } |
segundo | 0:ac1725ba162c | 39 | |
segundo | 0:ac1725ba162c | 40 | GPRSModem::~GPRSModem() |
segundo | 0:ac1725ba162c | 41 | { |
segundo | 0:ac1725ba162c | 42 | |
segundo | 0:ac1725ba162c | 43 | } |
segundo | 0:ac1725ba162c | 44 | |
segundo | 0:ac1725ba162c | 45 | GPRSErr GPRSModem::getNetworkState() |
segundo | 0:ac1725ba162c | 46 | { |
segundo | 0:ac1725ba162c | 47 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 48 | /* |
segundo | 0:ac1725ba162c | 49 | netState can be : (Telit_AT_Reference_Guide.pdf p.98) |
segundo | 0:ac1725ba162c | 50 | 0 - not registered, ME is not currently searching a new operator to register to |
segundo | 0:ac1725ba162c | 51 | 1 - registered, home network |
segundo | 0:ac1725ba162c | 52 | 2 - not registered, but ME is currently searching a new operator to register to |
segundo | 0:ac1725ba162c | 53 | 3 - registration denied |
segundo | 0:ac1725ba162c | 54 | 4 - unknown |
segundo | 0:ac1725ba162c | 55 | 5 - registered, roaming |
segundo | 0:ac1725ba162c | 56 | */ |
segundo | 0:ac1725ba162c | 57 | // DBG("Network?...\r\n"); |
segundo | 0:ac1725ba162c | 58 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 59 | ATIf::setTimeout(10000); |
segundo | 0:ac1725ba162c | 60 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 61 | int netState = 0; |
segundo | 0:ac1725ba162c | 62 | int len; |
segundo | 0:ac1725ba162c | 63 | len = ATIf::printf("AT+CREG?"); //Registered ? |
segundo | 0:ac1725ba162c | 64 | if(!len) DBG("\r\nprintf - len=%d\r\n",len); |
segundo | 0:ac1725ba162c | 65 | if(!len) |
segundo | 0:ac1725ba162c | 66 | return GPRS_MODEM; //Nothing was actually sent |
segundo | 0:ac1725ba162c | 67 | |
segundo | 0:ac1725ba162c | 68 | len = ATIf::scanf("+CREG: 0,%d", &netState); //Get status |
segundo | 0:ac1725ba162c | 69 | if(len != 1) DBG("\r\nscanf - len=%d\r\n",len); |
segundo | 0:ac1725ba162c | 70 | if(len != 1) //Likely +CMS ERROR was returned |
segundo | 0:ac1725ba162c | 71 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 72 | |
segundo | 0:ac1725ba162c | 73 | if( !!ATIf::checkOK() ) //Should not be a problem |
segundo | 0:ac1725ba162c | 74 | {DBG("\r\nNOK\r\n"); return GPRS_MODEM; } |
segundo | 0:ac1725ba162c | 75 | |
segundo | 0:ac1725ba162c | 76 | switch(netState) |
segundo | 0:ac1725ba162c | 77 | { |
segundo | 0:ac1725ba162c | 78 | case 1: |
segundo | 0:ac1725ba162c | 79 | case 5: //TODO: Option allow roaming |
segundo | 0:ac1725ba162c | 80 | DBG("\r\nNetwork is up!\r\n"); |
segundo | 0:ac1725ba162c | 81 | return GPRS_OK; |
segundo | 0:ac1725ba162c | 82 | case 3: |
segundo | 0:ac1725ba162c | 83 | DBG("\r\nAccess to network denied.\r\n"); |
segundo | 0:ac1725ba162c | 84 | return GPRS_DENIED; |
segundo | 0:ac1725ba162c | 85 | case 0: |
segundo | 0:ac1725ba162c | 86 | DBG("\r\nNo network.\r\n"); |
segundo | 0:ac1725ba162c | 87 | return GPRS_NONETWORK; |
segundo | 0:ac1725ba162c | 88 | case 4: |
segundo | 0:ac1725ba162c | 89 | case 2: |
segundo | 0:ac1725ba162c | 90 | //DBG("\r\nRegistering...\r\n"); |
segundo | 0:ac1725ba162c | 91 | return GPRS_REGISTERING; |
segundo | 0:ac1725ba162c | 92 | } |
segundo | 0:ac1725ba162c | 93 | |
segundo | 0:ac1725ba162c | 94 | return GPRS_MODEM; // Should not reach this |
segundo | 0:ac1725ba162c | 95 | |
segundo | 0:ac1725ba162c | 96 | } |
segundo | 0:ac1725ba162c | 97 | |
segundo | 0:ac1725ba162c | 98 | GPRSErr GPRSModem::setNetworkUp() |
segundo | 0:ac1725ba162c | 99 | { |
segundo | 0:ac1725ba162c | 100 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 101 | GPRSErr err = GPRS_REGISTERING; |
segundo | 0:ac1725ba162c | 102 | while(true) |
segundo | 0:ac1725ba162c | 103 | { |
segundo | 0:ac1725ba162c | 104 | err = getNetworkState(); |
segundo | 0:ac1725ba162c | 105 | if(err != GPRS_REGISTERING) |
segundo | 0:ac1725ba162c | 106 | break; |
segundo | 0:ac1725ba162c | 107 | wait(WAIT_BTW_NETW_POLLS); |
segundo | 0:ac1725ba162c | 108 | } |
segundo | 0:ac1725ba162c | 109 | return err; |
segundo | 0:ac1725ba162c | 110 | } |
segundo | 0:ac1725ba162c | 111 | |
segundo | 0:ac1725ba162c | 112 | //Same, but for GPRS |
segundo | 0:ac1725ba162c | 113 | GPRSErr GPRSModem::getGPRSState() |
segundo | 0:ac1725ba162c | 114 | { |
segundo | 0:ac1725ba162c | 115 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 116 | /* |
segundo | 0:ac1725ba162c | 117 | netState can be : (Telit_AT_Reference_Guide.pdf p.192) |
segundo | 0:ac1725ba162c | 118 | 0 - not registered, terminal is not currently searching a new operator to register to |
segundo | 0:ac1725ba162c | 119 | 1 - registered, home network |
segundo | 0:ac1725ba162c | 120 | 2 - not registered, but terminal is currently searching a new operator to register to |
segundo | 0:ac1725ba162c | 121 | 3 - registration denied |
segundo | 0:ac1725ba162c | 122 | 4 - unknown |
segundo | 0:ac1725ba162c | 123 | 5 - registered, roaming |
segundo | 0:ac1725ba162c | 124 | */ |
segundo | 0:ac1725ba162c | 125 | |
segundo | 0:ac1725ba162c | 126 | DBG("GPRS?...\r\n"); |
segundo | 0:ac1725ba162c | 127 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 128 | ATIf::setTimeout(10000); |
segundo | 0:ac1725ba162c | 129 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 130 | int netState = 0; |
segundo | 0:ac1725ba162c | 131 | int len; |
segundo | 0:ac1725ba162c | 132 | len = ATIf::printf("AT+CGREG?"); //Registered ? |
segundo | 0:ac1725ba162c | 133 | if(!len) |
segundo | 0:ac1725ba162c | 134 | return GPRS_MODEM; //Nothing was actually sent |
segundo | 0:ac1725ba162c | 135 | |
segundo | 0:ac1725ba162c | 136 | len = ATIf::scanf("+CGREG: %*d,%d", &netState); //Get GPRS status, see GSM 07.07 spec as Telit AT ref is wrong |
segundo | 0:ac1725ba162c | 137 | if(len != 1) DBG("\r\nscanf - len=%d\r\n",len); |
segundo | 0:ac1725ba162c | 138 | if(len != 1) //Likely +CMS ERROR was returned |
segundo | 0:ac1725ba162c | 139 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 140 | |
segundo | 0:ac1725ba162c | 141 | if( !!ATIf::checkOK() ) //Should not be a problem |
segundo | 0:ac1725ba162c | 142 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 143 | |
segundo | 0:ac1725ba162c | 144 | switch(netState) |
segundo | 0:ac1725ba162c | 145 | { |
segundo | 0:ac1725ba162c | 146 | case 1: |
segundo | 0:ac1725ba162c | 147 | case 5: //TODO: Option allow roaming |
segundo | 0:ac1725ba162c | 148 | DBG("\r\nNetwork is up!\r\n"); |
segundo | 0:ac1725ba162c | 149 | return GPRS_OK; |
segundo | 0:ac1725ba162c | 150 | case 3: |
segundo | 0:ac1725ba162c | 151 | DBG("\r\nAccess to network denied.\r\n"); |
segundo | 0:ac1725ba162c | 152 | return GPRS_DENIED; |
segundo | 0:ac1725ba162c | 153 | case 0: |
segundo | 0:ac1725ba162c | 154 | DBG("\r\nNo network.\r\n"); |
segundo | 0:ac1725ba162c | 155 | return GPRS_NONETWORK; |
segundo | 0:ac1725ba162c | 156 | case 4: |
segundo | 0:ac1725ba162c | 157 | case 2: |
segundo | 0:ac1725ba162c | 158 | DBG("\r\nRegistering...\r\n"); |
segundo | 0:ac1725ba162c | 159 | return GPRS_REGISTERING; |
segundo | 0:ac1725ba162c | 160 | } |
segundo | 0:ac1725ba162c | 161 | |
segundo | 0:ac1725ba162c | 162 | return GPRS_MODEM; // Should not reach this |
segundo | 0:ac1725ba162c | 163 | |
segundo | 0:ac1725ba162c | 164 | } |
segundo | 0:ac1725ba162c | 165 | |
segundo | 0:ac1725ba162c | 166 | GPRSErr GPRSModem::setGPRSUp() |
segundo | 0:ac1725ba162c | 167 | { |
segundo | 0:ac1725ba162c | 168 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 169 | GPRSErr err; |
segundo | 0:ac1725ba162c | 170 | |
segundo | 0:ac1725ba162c | 171 | err = setNetworkUp(); |
segundo | 0:ac1725ba162c | 172 | if(err) |
segundo | 0:ac1725ba162c | 173 | return err; |
segundo | 0:ac1725ba162c | 174 | |
segundo | 0:ac1725ba162c | 175 | DBG("\r\nAttaching GPRS...\r\n"); |
segundo | 0:ac1725ba162c | 176 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 177 | ATIf::setTimeout(10000); |
segundo | 0:ac1725ba162c | 178 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 179 | int len; |
segundo | 0:ac1725ba162c | 180 | |
segundo | 0:ac1725ba162c | 181 | err = getGPRSState(); |
segundo | 0:ac1725ba162c | 182 | if(err == GPRS_NONETWORK) |
segundo | 0:ac1725ba162c | 183 | { |
segundo | 0:ac1725ba162c | 184 | len = ATIf::printf("AT+CGATT=1"); //Attach |
segundo | 0:ac1725ba162c | 185 | if(!len) |
segundo | 0:ac1725ba162c | 186 | return GPRS_MODEM; //Nothing was actually sent |
segundo | 0:ac1725ba162c | 187 | |
segundo | 0:ac1725ba162c | 188 | if( !!ATIf::checkOK() ) //Should not be a problem |
segundo | 0:ac1725ba162c | 189 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 190 | } |
segundo | 0:ac1725ba162c | 191 | |
segundo | 0:ac1725ba162c | 192 | while(true) |
segundo | 0:ac1725ba162c | 193 | { |
segundo | 0:ac1725ba162c | 194 | err = getGPRSState(); |
segundo | 0:ac1725ba162c | 195 | if(err != GPRS_REGISTERING) |
segundo | 0:ac1725ba162c | 196 | break; |
segundo | 0:ac1725ba162c | 197 | wait(WAIT_BTW_NETW_POLLS); |
segundo | 0:ac1725ba162c | 198 | } |
segundo | 0:ac1725ba162c | 199 | return err; |
segundo | 0:ac1725ba162c | 200 | } |
segundo | 0:ac1725ba162c | 201 | |
segundo | 0:ac1725ba162c | 202 | GPRSErr GPRSModem::setGPRSDown() |
segundo | 0:ac1725ba162c | 203 | { |
segundo | 0:ac1725ba162c | 204 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 205 | DBG("\r\nDetaching GPRS...\r\n"); |
segundo | 0:ac1725ba162c | 206 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 207 | ATIf::setTimeout(10000); |
segundo | 0:ac1725ba162c | 208 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 209 | int len; |
segundo | 0:ac1725ba162c | 210 | |
segundo | 0:ac1725ba162c | 211 | len = ATIf::printf("AT+CGATT=0"); //Detach |
segundo | 0:ac1725ba162c | 212 | if(!len) |
segundo | 0:ac1725ba162c | 213 | return GPRS_MODEM; //Nothing was actually sent |
segundo | 0:ac1725ba162c | 214 | |
segundo | 0:ac1725ba162c | 215 | if( !!ATIf::checkOK() ) //Should not be a problem |
segundo | 0:ac1725ba162c | 216 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 217 | |
segundo | 0:ac1725ba162c | 218 | return GPRS_OK; |
segundo | 0:ac1725ba162c | 219 | } |
segundo | 0:ac1725ba162c | 220 | |
segundo | 0:ac1725ba162c | 221 | |
segundo | 0:ac1725ba162c | 222 | GPRSErr GPRSModem::connect(const char* apn /*=NULL*/) |
segundo | 0:ac1725ba162c | 223 | { |
segundo | 0:ac1725ba162c | 224 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 225 | GPRSErr err; |
segundo | 0:ac1725ba162c | 226 | |
segundo | 0:ac1725ba162c | 227 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 228 | ATIf::setTimeout(5000); |
segundo | 0:ac1725ba162c | 229 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 230 | |
segundo | 0:ac1725ba162c | 231 | DBG("\r\nConnecting...\r\n"); |
segundo | 0:ac1725ba162c | 232 | |
segundo | 0:ac1725ba162c | 233 | int len; |
segundo | 0:ac1725ba162c | 234 | |
segundo | 0:ac1725ba162c | 235 | if( apn != NULL ) //Config APN |
segundo | 0:ac1725ba162c | 236 | { |
segundo | 0:ac1725ba162c | 237 | len = ATIf::printf("AT+CGDCONT=1,\"IP\",\"%s\"",apn); //Define APN |
segundo | 0:ac1725ba162c | 238 | if(!len) |
segundo | 0:ac1725ba162c | 239 | return GPRS_MODEM; //Nothing was actually sent |
segundo | 0:ac1725ba162c | 240 | |
segundo | 0:ac1725ba162c | 241 | if( !!ATIf::checkOK() ) //Should not be a problem |
segundo | 0:ac1725ba162c | 242 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 243 | } |
segundo | 0:ac1725ba162c | 244 | |
segundo | 0:ac1725ba162c | 245 | err = setGPRSUp(); |
segundo | 0:ac1725ba162c | 246 | if(err) |
segundo | 0:ac1725ba162c | 247 | return err; |
segundo | 0:ac1725ba162c | 248 | |
segundo | 0:ac1725ba162c | 249 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 250 | ATIf::setTimeout(60000); |
segundo | 0:ac1725ba162c | 251 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 252 | // |
segundo | 0:ac1725ba162c | 253 | //len = ATIf::printf("AT+CGDATA=\"PPP\",1"); //Connect using PDP context #1 |
segundo | 0:ac1725ba162c | 254 | // len = ATIf::printf("ATDT *99***1#"); |
segundo | 0:ac1725ba162c | 255 | len = ATIf::printf("ATDT *99#"); |
segundo | 0:ac1725ba162c | 256 | if(!len) |
segundo | 0:ac1725ba162c | 257 | return GPRS_MODEM; //Nothing was actually sent |
segundo | 0:ac1725ba162c | 258 | |
segundo | 0:ac1725ba162c | 259 | len = ATIf::scanf("CONNECT"); //Beginning of session |
segundo | 0:ac1725ba162c | 260 | if(len != 0) //Likely +CME ERROR was returned or NO CARRIER |
segundo | 0:ac1725ba162c | 261 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 262 | |
segundo | 0:ac1725ba162c | 263 | //ATIf::setSignals(false); |
segundo | 0:ac1725ba162c | 264 | |
segundo | 0:ac1725ba162c | 265 | DBG("\r\nConnected.\r\n"); |
segundo | 0:ac1725ba162c | 266 | |
segundo | 0:ac1725ba162c | 267 | return GPRS_OK; //Time to enter a PPP Session ! |
segundo | 0:ac1725ba162c | 268 | |
segundo | 0:ac1725ba162c | 269 | } |
segundo | 0:ac1725ba162c | 270 | |
segundo | 0:ac1725ba162c | 271 | GPRSErr GPRSModem::disconnect() |
segundo | 0:ac1725ba162c | 272 | { |
segundo | 0:ac1725ba162c | 273 | ATIf::flushBuffer(); |
segundo | 0:ac1725ba162c | 274 | ATIf::setReadMode(false); //Discard chars |
segundo | 0:ac1725ba162c | 275 | ATIf::setTimeout(5000); |
segundo | 0:ac1725ba162c | 276 | ATIf::setLineMode(true); //Line mode |
segundo | 0:ac1725ba162c | 277 | |
segundo | 0:ac1725ba162c | 278 | if( !!ATIf::checkOK() ) //Should be present at the end of connection |
segundo | 0:ac1725ba162c | 279 | return GPRS_MODEM; |
segundo | 0:ac1725ba162c | 280 | |
segundo | 0:ac1725ba162c | 281 | GPRSErr err; |
segundo | 0:ac1725ba162c | 282 | err = setGPRSDown(); |
segundo | 0:ac1725ba162c | 283 | if(err) |
segundo | 0:ac1725ba162c | 284 | return err; |
segundo | 0:ac1725ba162c | 285 | |
segundo | 0:ac1725ba162c | 286 | DBG("\r\nDisconnected.\r\n"); |
segundo | 0:ac1725ba162c | 287 | |
segundo | 0:ac1725ba162c | 288 | return GPRS_OK; |
segundo | 0:ac1725ba162c | 289 | } |
segundo | 0:ac1725ba162c | 290 | |
segundo | 0:ac1725ba162c | 291 | #endif |
segundo | 0:ac1725ba162c | 292 |