NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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