Michael Fischler / Mbed 2 deprecated Contest_Winner

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 17:02:29 2011 +0000
Revision:
0:d9266031f832

        

Who changed what in which revision?

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