HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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