Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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