Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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