I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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