Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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