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 "ATIf.h"
tax 0:66300c77c6e9 25 #include "mbed.h"
tax 0:66300c77c6e9 26 #include <cstdarg>
tax 0:66300c77c6e9 27
tax 0:66300c77c6e9 28 #define READ_TIMEOUT 100
tax 0:66300c77c6e9 29 #define TMP_BUF_SIZE 128//512
tax 0:66300c77c6e9 30
tax 0:66300c77c6e9 31 #define SERIAL_BUF_LEN 512 //Huge buf needed for PPP (esp. when transferring big data chunks, using TCP)
tax 0:66300c77c6e9 32
tax 0:66300c77c6e9 33 #define BAUDRATE 9600//9600//115200// 19200
tax 0:66300c77c6e9 34
tax 0:66300c77c6e9 35 #include "netCfg.h"
tax 0:66300c77c6e9 36 #if NET_GPRS
tax 0:66300c77c6e9 37
tax 0:66300c77c6e9 38 //#define __DEBUG
tax 0:66300c77c6e9 39 #include "dbg/dbg.h"
tax 0:66300c77c6e9 40
tax 0:66300c77c6e9 41 ATIf::ATIf() : SerialBuf(SERIAL_BUF_LEN), m_isOpen(false)//, m_signalsEnable(false), m_isOpen(false), m_pCurrentSignal(NULL), m_signals()
tax 0:66300c77c6e9 42 {
tax 0:66300c77c6e9 43 DBG("New AT If@%p\n", this);
tax 0:66300c77c6e9 44
tax 0:66300c77c6e9 45 m_readTimeout = READ_TIMEOUT; //default 1s
tax 0:66300c77c6e9 46 //tmpBuf = NULL;
tax 0:66300c77c6e9 47 m_lineMode = false;
tax 0:66300c77c6e9 48 m_tmpBuf = new char[TMP_BUF_SIZE];
tax 0:66300c77c6e9 49 }
tax 0:66300c77c6e9 50
tax 0:66300c77c6e9 51 ATIf::~ATIf()
tax 0:66300c77c6e9 52 {
tax 0:66300c77c6e9 53 if(m_tmpBuf)
tax 0:66300c77c6e9 54 delete[] m_tmpBuf;
tax 0:66300c77c6e9 55 }
tax 0:66300c77c6e9 56
tax 0:66300c77c6e9 57 int ATIf::printf(const char* format, ... )
tax 0:66300c77c6e9 58 {
tax 0:66300c77c6e9 59
tax 0:66300c77c6e9 60 /*if(!m_tmpBuf)
tax 0:66300c77c6e9 61 m_tmpBuf = new char[TMP_BUF_SIZE]; //is it really necessary ??*/
tax 0:66300c77c6e9 62 *m_tmpBuf=0;
tax 0:66300c77c6e9 63
tax 0:66300c77c6e9 64 int len = 0;
tax 0:66300c77c6e9 65
tax 0:66300c77c6e9 66 //
tax 0:66300c77c6e9 67 // flushBuffer();
tax 0:66300c77c6e9 68 //wait(1);
tax 0:66300c77c6e9 69 //
tax 0:66300c77c6e9 70
tax 0:66300c77c6e9 71 va_list argp;
tax 0:66300c77c6e9 72
tax 0:66300c77c6e9 73 va_start(argp, format);
tax 0:66300c77c6e9 74 len += vsprintf(m_tmpBuf, format, argp);
tax 0:66300c77c6e9 75 va_end(argp);
tax 0:66300c77c6e9 76
tax 0:66300c77c6e9 77 //DBG("\r\nOutBuf is : %s, mode is %d.", m_tmpBuf, m_lineMode);
tax 0:66300c77c6e9 78
tax 0:66300c77c6e9 79 int err = write( m_tmpBuf, m_lineMode );
tax 0:66300c77c6e9 80 if (err<0)
tax 0:66300c77c6e9 81 return 0;
tax 0:66300c77c6e9 82
tax 0:66300c77c6e9 83 return len;
tax 0:66300c77c6e9 84
tax 0:66300c77c6e9 85 }
tax 0:66300c77c6e9 86
tax 0:66300c77c6e9 87 int ATIf::scanf(const char* format, ... )
tax 0:66300c77c6e9 88 {
tax 0:66300c77c6e9 89 /*if(!m_tmpBuf)
tax 0:66300c77c6e9 90 m_tmpBuf = new char[TMP_BUF_SIZE];*/
tax 0:66300c77c6e9 91 int err = read( m_tmpBuf, TMP_BUF_SIZE - 1, m_readTimeout, m_lineMode, 1/*Ensure at least one char is read*/ );
tax 0:66300c77c6e9 92 if (err<0)
tax 0:66300c77c6e9 93 return -1;//EOF
tax 0:66300c77c6e9 94
tax 0:66300c77c6e9 95 DBG("Scanf'ing:\r\n%s\r\n",m_tmpBuf);
tax 0:66300c77c6e9 96
tax 0:66300c77c6e9 97 int len = 0;
tax 0:66300c77c6e9 98
tax 0:66300c77c6e9 99 if(strchr(format,'%')) //Ugly, determines wether format string is null or not
tax 0:66300c77c6e9 100 {
tax 0:66300c77c6e9 101 va_list argp;
tax 0:66300c77c6e9 102
tax 0:66300c77c6e9 103 va_start(argp, format);
tax 0:66300c77c6e9 104 len += vsscanf(m_tmpBuf, format, argp);
tax 0:66300c77c6e9 105 va_end(argp);
tax 0:66300c77c6e9 106 }
tax 0:66300c77c6e9 107 else //No varargs, call strncmp
tax 0:66300c77c6e9 108 {
tax 0:66300c77c6e9 109 /* if(strlen(m_tmpBuf) == 0 )
tax 0:66300c77c6e9 110 return -1;*/
tax 0:66300c77c6e9 111 if( !strncmp(m_tmpBuf, format, strlen(format)) )
tax 0:66300c77c6e9 112 {
tax 0:66300c77c6e9 113 return 0;
tax 0:66300c77c6e9 114 }
tax 0:66300c77c6e9 115 else
tax 0:66300c77c6e9 116 {
tax 0:66300c77c6e9 117 return -1;
tax 0:66300c77c6e9 118 }
tax 0:66300c77c6e9 119 }
tax 0:66300c77c6e9 120
tax 0:66300c77c6e9 121 return len;
tax 0:66300c77c6e9 122
tax 0:66300c77c6e9 123 }
tax 0:66300c77c6e9 124
tax 0:66300c77c6e9 125 void ATIf::setTimeout(int timeout) //used by scanf
tax 0:66300c77c6e9 126 {
tax 0:66300c77c6e9 127 m_readTimeout = timeout;
tax 0:66300c77c6e9 128 }
tax 0:66300c77c6e9 129
tax 0:66300c77c6e9 130 void ATIf::setLineMode(bool lineMode) //Switch btw line & raw fns
tax 0:66300c77c6e9 131 {
tax 0:66300c77c6e9 132 m_lineMode = lineMode;
tax 0:66300c77c6e9 133 }
tax 0:66300c77c6e9 134
tax 0:66300c77c6e9 135 #if 0
tax 0:66300c77c6e9 136 void ATIf::setSignals(bool signalsEnable)
tax 0:66300c77c6e9 137 {
tax 0:66300c77c6e9 138 m_signalsEnable=signalsEnable;
tax 0:66300c77c6e9 139 }
tax 0:66300c77c6e9 140 #endif
tax 0:66300c77c6e9 141
tax 0:66300c77c6e9 142 #if 0
tax 0:66300c77c6e9 143 template<class T>
tax 0:66300c77c6e9 144 void ATIf::attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ) //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn
tax 0:66300c77c6e9 145 {
tax 0:66300c77c6e9 146 ATSigHandler sig(sigName, (ATSigHandler::CDummy*)pItem, (bool (ATSigHandler::CDummy::*)(ATIf*, bool, bool*))pMethod);
tax 0:66300c77c6e9 147 m_signals.push_back(sig);
tax 0:66300c77c6e9 148 }
tax 0:66300c77c6e9 149 #endif
tax 0:66300c77c6e9 150
tax 0:66300c77c6e9 151 #if 0
tax 0:66300c77c6e9 152 void ATIf::detachSignal( const char* sigName )
tax 0:66300c77c6e9 153 {
tax 0:66300c77c6e9 154 list<ATSigHandler>::iterator it;
tax 0:66300c77c6e9 155
tax 0:66300c77c6e9 156 for ( it = m_signals.begin(); it != m_signals.end(); it++ )
tax 0:66300c77c6e9 157 {
tax 0:66300c77c6e9 158 if( !strcmp((*it).m_name,sigName) )
tax 0:66300c77c6e9 159 {
tax 0:66300c77c6e9 160 m_signals.erase(it);
tax 0:66300c77c6e9 161 break;
tax 0:66300c77c6e9 162 }
tax 0:66300c77c6e9 163 }
tax 0:66300c77c6e9 164 }
tax 0:66300c77c6e9 165 #endif
tax 0:66300c77c6e9 166
tax 0:66300c77c6e9 167 ATErr ATIf::open(Serial* pSerial) //Deactivate echo, etc
tax 0:66300c77c6e9 168 {
tax 0:66300c77c6e9 169 DBG("Opening...\n");
tax 0:66300c77c6e9 170 m_isOpen = true; //Must be set so that the serial port-related fns work
tax 0:66300c77c6e9 171 //Setup options
tax 0:66300c77c6e9 172 // pSerial->baud(BAUDRATE); //FIXME
tax 0:66300c77c6e9 173 SerialBuf::attach(pSerial);
tax 0:66300c77c6e9 174
tax 0:66300c77c6e9 175 setReadMode(false); //Discard chars
tax 0:66300c77c6e9 176 setTimeout(1000);
tax 0:66300c77c6e9 177 setLineMode(true); //Line Mode
tax 0:66300c77c6e9 178
tax 0:66300c77c6e9 179 DBG("Trmt...\n");
tax 0:66300c77c6e9 180 // printf("AT+IPR=%d", BAUDRATE); //FIXME
tax 0:66300c77c6e9 181 printf("ATZ"); //Reset
tax 0:66300c77c6e9 182 wait(.100);
tax 0:66300c77c6e9 183 printf("ATE"); //Deactivate echo
tax 0:66300c77c6e9 184 wait(.500);
tax 0:66300c77c6e9 185 flushBuffer();
tax 0:66300c77c6e9 186
tax 0:66300c77c6e9 187 DBG("ATZ ATE...\n");
tax 0:66300c77c6e9 188
tax 0:66300c77c6e9 189 int len = writeLine("ATV1");
tax 0:66300c77c6e9 190 ATErr err = AT_OK;
tax 0:66300c77c6e9 191 if(len<0)
tax 0:66300c77c6e9 192 err=(ATErr)len;
tax 0:66300c77c6e9 193
tax 0:66300c77c6e9 194 if(!err)
tax 0:66300c77c6e9 195 {
tax 0:66300c77c6e9 196 err = checkOK();
tax 0:66300c77c6e9 197 if (err) //No ACK from module
tax 0:66300c77c6e9 198 {
tax 0:66300c77c6e9 199 DBG("\r\nOpening port, error %d.", err);
tax 0:66300c77c6e9 200 if(err==AT_TIMEOUT)
tax 0:66300c77c6e9 201 err = AT_NOANSWER;
tax 0:66300c77c6e9 202 }
tax 0:66300c77c6e9 203 }
tax 0:66300c77c6e9 204
tax 0:66300c77c6e9 205 if(err)
tax 0:66300c77c6e9 206 {
tax 0:66300c77c6e9 207 SerialBuf::detach();
tax 0:66300c77c6e9 208 m_isOpen = false;
tax 0:66300c77c6e9 209 return err;
tax 0:66300c77c6e9 210 }
tax 0:66300c77c6e9 211
tax 0:66300c77c6e9 212 DBG("\r\nNo error.");
tax 0:66300c77c6e9 213 #if 0//FIXME
tax 0:66300c77c6e9 214 m_signalsEnable = true;
tax 0:66300c77c6e9 215 #endif
tax 0:66300c77c6e9 216 //FIXME:
tax 0:66300c77c6e9 217 // m_pSerial->attach<ATIf>(this, &ATIf::onSerialInterrupt);
tax 0:66300c77c6e9 218
tax 0:66300c77c6e9 219 return AT_OK;
tax 0:66300c77c6e9 220 }
tax 0:66300c77c6e9 221
tax 0:66300c77c6e9 222 #if NET_USB_SERIAL
tax 0:66300c77c6e9 223 ATErr ATIf::open(UsbSerial* pUsbSerial) //Deactivate echo, etc
tax 0:66300c77c6e9 224 {
tax 0:66300c77c6e9 225 DBG("Opening...\n");
tax 0:66300c77c6e9 226 m_isOpen = true; //Must be set so that the serial port-related fns work
tax 0:66300c77c6e9 227 //Setup options
tax 0:66300c77c6e9 228 SerialBuf::attach(pUsbSerial);
tax 0:66300c77c6e9 229
tax 0:66300c77c6e9 230 setReadMode(false); //Discard chars
tax 0:66300c77c6e9 231 setTimeout(1000);
tax 0:66300c77c6e9 232 setLineMode(true); //Line Mode
tax 0:66300c77c6e9 233
tax 0:66300c77c6e9 234 printf("ATZ"); //Reinit
tax 0:66300c77c6e9 235 wait(.500);
tax 0:66300c77c6e9 236 //flushBuffer();
tax 0:66300c77c6e9 237 // printf("ATE0 ^CURC=0"); //Deactivate echo & notif
tax 0:66300c77c6e9 238 printf("ATE0"); //Deactivate echo & notif
tax 0:66300c77c6e9 239 wait(.500);
tax 0:66300c77c6e9 240 flushBuffer();
tax 0:66300c77c6e9 241
tax 0:66300c77c6e9 242 DBG("ATZ ATE...\n");
tax 0:66300c77c6e9 243
tax 0:66300c77c6e9 244 int len = writeLine("ATQ0 V1 S0=0 &C1 &D2 +FCLASS=0");//writeLine("ATQ0 V1 S0=0 &C1 &D2 +FCLASS=0");
tax 0:66300c77c6e9 245 ATErr err = AT_OK;
tax 0:66300c77c6e9 246 if(len<0)
tax 0:66300c77c6e9 247 err=(ATErr)len;
tax 0:66300c77c6e9 248
tax 0:66300c77c6e9 249 if(!err)
tax 0:66300c77c6e9 250 {
tax 0:66300c77c6e9 251 err = checkOK();
tax 0:66300c77c6e9 252 if (err) //No ACK from module
tax 0:66300c77c6e9 253 {
tax 0:66300c77c6e9 254 DBG("Opening port, error %d.\n", err);
tax 0:66300c77c6e9 255 if(err==AT_TIMEOUT)
tax 0:66300c77c6e9 256 err = AT_NOANSWER;
tax 0:66300c77c6e9 257 }
tax 0:66300c77c6e9 258 }
tax 0:66300c77c6e9 259
tax 0:66300c77c6e9 260 if(err)
tax 0:66300c77c6e9 261 {
tax 0:66300c77c6e9 262 SerialBuf::detach();
tax 0:66300c77c6e9 263 m_isOpen = false;
tax 0:66300c77c6e9 264 return err;
tax 0:66300c77c6e9 265 }
tax 0:66300c77c6e9 266
tax 0:66300c77c6e9 267 DBG("No error.\n");
tax 0:66300c77c6e9 268 #if 0//FIXME
tax 0:66300c77c6e9 269 m_signalsEnable = true;
tax 0:66300c77c6e9 270 #endif
tax 0:66300c77c6e9 271 //FIXME:
tax 0:66300c77c6e9 272 // m_pSerial->attach<ATIf>(this, &ATIf::onSerialInterrupt);
tax 0:66300c77c6e9 273
tax 0:66300c77c6e9 274 return AT_OK;
tax 0:66300c77c6e9 275 }
tax 0:66300c77c6e9 276 #endif
tax 0:66300c77c6e9 277
tax 0:66300c77c6e9 278 ATErr ATIf::close() //Release port
tax 0:66300c77c6e9 279 {
tax 0:66300c77c6e9 280 SerialBuf::detach(); //Detach serial buf
tax 0:66300c77c6e9 281 m_isOpen = false;
tax 0:66300c77c6e9 282 //m_signalsEnable = false;
tax 0:66300c77c6e9 283 return AT_OK;
tax 0:66300c77c6e9 284 }
tax 0:66300c77c6e9 285
tax 0:66300c77c6e9 286 ATErr ATIf::flushBuffer()
tax 0:66300c77c6e9 287 {
tax 0:66300c77c6e9 288 if(!m_isOpen)
tax 0:66300c77c6e9 289 return AT_CLOSED;
tax 0:66300c77c6e9 290
tax 0:66300c77c6e9 291 int len=0;
tax 0:66300c77c6e9 292 //char c;
tax 0:66300c77c6e9 293 while(readable())
tax 0:66300c77c6e9 294 {
tax 0:66300c77c6e9 295 //DBG("Readable\n");
tax 0:66300c77c6e9 296 /*c =*/ getc();
tax 0:66300c77c6e9 297 //DBG("\r\n[%c] discarded.", c);
tax 0:66300c77c6e9 298 // wait(0.01);
tax 0:66300c77c6e9 299 len++;
tax 0:66300c77c6e9 300 }
tax 0:66300c77c6e9 301
tax 0:66300c77c6e9 302 DBG("\r\n%d chars discarded.", len);
tax 0:66300c77c6e9 303
tax 0:66300c77c6e9 304 return AT_OK;
tax 0:66300c77c6e9 305 }
tax 0:66300c77c6e9 306
tax 0:66300c77c6e9 307 ATErr ATIf::flushLine(int timeout)
tax 0:66300c77c6e9 308 {
tax 0:66300c77c6e9 309 if(!m_isOpen)
tax 0:66300c77c6e9 310 return AT_CLOSED;
tax 0:66300c77c6e9 311
tax 0:66300c77c6e9 312 Timer timer;
tax 0:66300c77c6e9 313
tax 0:66300c77c6e9 314 timer.start();
tax 0:66300c77c6e9 315
tax 0:66300c77c6e9 316 int len=0;
tax 0:66300c77c6e9 317 char c=0;
tax 0:66300c77c6e9 318 while(true)
tax 0:66300c77c6e9 319 {
tax 0:66300c77c6e9 320 while(!readable())
tax 0:66300c77c6e9 321 { if(timer.read_ms()>timeout)
tax 0:66300c77c6e9 322 {
tax 0:66300c77c6e9 323 // DBG("Timeout!!0");
tax 0:66300c77c6e9 324 return AT_TIMEOUT;
tax 0:66300c77c6e9 325 }
tax 0:66300c77c6e9 326 }
tax 0:66300c77c6e9 327 if(c=='\x0D')
tax 0:66300c77c6e9 328 {
tax 0:66300c77c6e9 329 c = getc();
tax 0:66300c77c6e9 330 len++;
tax 0:66300c77c6e9 331 if(c=='\x0A')
tax 0:66300c77c6e9 332 break;
tax 0:66300c77c6e9 333 }
tax 0:66300c77c6e9 334 else
tax 0:66300c77c6e9 335 {
tax 0:66300c77c6e9 336 c = getc();
tax 0:66300c77c6e9 337 len++;
tax 0:66300c77c6e9 338 }
tax 0:66300c77c6e9 339 }
tax 0:66300c77c6e9 340
tax 0:66300c77c6e9 341 // DBG("\r\n%d chars discarded.", len);
tax 0:66300c77c6e9 342
tax 0:66300c77c6e9 343 return AT_OK;
tax 0:66300c77c6e9 344 }
tax 0:66300c77c6e9 345
tax 0:66300c77c6e9 346 #if 0
tax 0:66300c77c6e9 347 bool ATIf::onRead()
tax 0:66300c77c6e9 348 {
tax 0:66300c77c6e9 349 if(!m_signalsEnable)
tax 0:66300c77c6e9 350 return false;
tax 0:66300c77c6e9 351
tax 0:66300c77c6e9 352 //Save Usermode params
tax 0:66300c77c6e9 353 volatile int u_readTimeout = m_readTimeout;
tax 0:66300c77c6e9 354 volatile bool u_lineMode = m_lineMode;
tax 0:66300c77c6e9 355 // bool u_isOpen = m_isOpen;
tax 0:66300c77c6e9 356 SerialBuf::setReadMode(true);
tax 0:66300c77c6e9 357
tax 0:66300c77c6e9 358 m_readTimeout = 0; //No timeout in an interrupt fn!
tax 0:66300c77c6e9 359
tax 0:66300c77c6e9 360 bool handled;
tax 0:66300c77c6e9 361 if(!!flushLine(0))
tax 0:66300c77c6e9 362 {
tax 0:66300c77c6e9 363 SerialBuf::resetRead();
tax 0:66300c77c6e9 364 //Not a complete line here, wait...
tax 0:66300c77c6e9 365 handled = false;
tax 0:66300c77c6e9 366 }
tax 0:66300c77c6e9 367 else
tax 0:66300c77c6e9 368 {
tax 0:66300c77c6e9 369 SerialBuf::resetRead();
tax 0:66300c77c6e9 370 handled = true;
tax 0:66300c77c6e9 371 if( handleSignal() ) //Was that a signal ?
tax 0:66300c77c6e9 372 {
tax 0:66300c77c6e9 373 //OK, discard data since it has been processed
tax 0:66300c77c6e9 374 SerialBuf::flushRead();
tax 0:66300c77c6e9 375 }
tax 0:66300c77c6e9 376 else
tax 0:66300c77c6e9 377 {
tax 0:66300c77c6e9 378 //Keep data since it has not been processed yet
tax 0:66300c77c6e9 379 //Have to be processed in usermode
tax 0:66300c77c6e9 380 SerialBuf::resetRead();
tax 0:66300c77c6e9 381 // handled = false;
tax 0:66300c77c6e9 382 }
tax 0:66300c77c6e9 383 }
tax 0:66300c77c6e9 384 //Restore Usermode params
tax 0:66300c77c6e9 385 m_readTimeout = u_readTimeout;
tax 0:66300c77c6e9 386 m_lineMode = u_lineMode;
tax 0:66300c77c6e9 387 //m_isOpen = u_isOpen;
tax 0:66300c77c6e9 388 return handled;
tax 0:66300c77c6e9 389 }
tax 0:66300c77c6e9 390 #endif
tax 0:66300c77c6e9 391
tax 0:66300c77c6e9 392 ATErr ATIf::rawOpen(Serial* pSerial, int baudrate) //Simple open function for similar non-conforming protocols
tax 0:66300c77c6e9 393 {
tax 0:66300c77c6e9 394 DBG("\r\nOpening...");
tax 0:66300c77c6e9 395 m_isOpen = true; //Must be set so that the serial port-related fns work
tax 0:66300c77c6e9 396 //Setup options
tax 0:66300c77c6e9 397 pSerial->baud(baudrate);
tax 0:66300c77c6e9 398 SerialBuf::attach(pSerial);
tax 0:66300c77c6e9 399
tax 0:66300c77c6e9 400 return AT_OK;
tax 0:66300c77c6e9 401 }
tax 0:66300c77c6e9 402
tax 0:66300c77c6e9 403 #if 0
tax 0:66300c77c6e9 404 ATErr ATIf::command(const char* cmd, char* result, int resultLen, int timeout) ////WARN/FIXME: result has to be long enough!!!
tax 0:66300c77c6e9 405 {
tax 0:66300c77c6e9 406 if(!m_isOpen)
tax 0:66300c77c6e9 407 return AT_CLOSED;
tax 0:66300c77c6e9 408
tax 0:66300c77c6e9 409 flushBuffer();
tax 0:66300c77c6e9 410
tax 0:66300c77c6e9 411 int err;
tax 0:66300c77c6e9 412 err = writeLine(cmd);
tax 0:66300c77c6e9 413
tax 0:66300c77c6e9 414 if(err<0)
tax 0:66300c77c6e9 415 { m_receiveStatus = AT_READY; return (ATErr)err; }
tax 0:66300c77c6e9 416
tax 0:66300c77c6e9 417 err = readLine(result, resultLen, timeout);
tax 0:66300c77c6e9 418
tax 0:66300c77c6e9 419 if(err<0)
tax 0:66300c77c6e9 420 { m_receiveStatus = AT_READY; return (ATErr)err; }
tax 0:66300c77c6e9 421
tax 0:66300c77c6e9 422 m_receiveStatus = AT_READY;
tax 0:66300c77c6e9 423
tax 0:66300c77c6e9 424 return AT_OK;
tax 0:66300c77c6e9 425
tax 0:66300c77c6e9 426 }
tax 0:66300c77c6e9 427 #endif
tax 0:66300c77c6e9 428
tax 0:66300c77c6e9 429 ATErr ATIf::write(const char* cmd, bool lineMode /*= false*/)
tax 0:66300c77c6e9 430 {
tax 0:66300c77c6e9 431 if(!m_isOpen)
tax 0:66300c77c6e9 432 return AT_CLOSED;
tax 0:66300c77c6e9 433
tax 0:66300c77c6e9 434 int err;
tax 0:66300c77c6e9 435 err = lineMode ? writeLine(cmd) : writeRaw(cmd);
tax 0:66300c77c6e9 436
tax 0:66300c77c6e9 437 if(err<0)
tax 0:66300c77c6e9 438 return (ATErr)err;
tax 0:66300c77c6e9 439
tax 0:66300c77c6e9 440 return AT_OK;
tax 0:66300c77c6e9 441 }
tax 0:66300c77c6e9 442
tax 0:66300c77c6e9 443
tax 0:66300c77c6e9 444 ATErr ATIf::read(char* result, int resultMaxLen, int timeout, bool lineMode /*= false*/, int resultMinLen/* = 0*/)
tax 0:66300c77c6e9 445 {
tax 0:66300c77c6e9 446 if(!m_isOpen)
tax 0:66300c77c6e9 447 return AT_CLOSED;
tax 0:66300c77c6e9 448
tax 0:66300c77c6e9 449 int err;
tax 0:66300c77c6e9 450 err = lineMode ? readLine(result, resultMaxLen, timeout) : readRaw(result, resultMaxLen, timeout, resultMinLen);
tax 0:66300c77c6e9 451
tax 0:66300c77c6e9 452 if(err<0)
tax 0:66300c77c6e9 453 return (ATErr)err;
tax 0:66300c77c6e9 454
tax 0:66300c77c6e9 455 return AT_OK;
tax 0:66300c77c6e9 456 }
tax 0:66300c77c6e9 457
tax 0:66300c77c6e9 458 bool ATIf::isOpen()
tax 0:66300c77c6e9 459 {
tax 0:66300c77c6e9 460 return m_isOpen;
tax 0:66300c77c6e9 461 }
tax 0:66300c77c6e9 462
tax 0:66300c77c6e9 463 ATErr ATIf::checkOK() //Helper fn to quickly check that OK has been returned
tax 0:66300c77c6e9 464 {
tax 0:66300c77c6e9 465 char ret[16] = {0};
tax 0:66300c77c6e9 466 int err = readLine(ret,16,m_readTimeout);
tax 0:66300c77c6e9 467
tax 0:66300c77c6e9 468 if(err<0)
tax 0:66300c77c6e9 469 {
tax 0:66300c77c6e9 470 DBG("\r\nError in check (%s).\r\n", ret);
tax 0:66300c77c6e9 471 flushBuffer(); //Discard anything in buf to avoid misparsing in the following calls
tax 0:66300c77c6e9 472 return (ATErr)err;
tax 0:66300c77c6e9 473 }
tax 0:66300c77c6e9 474
tax 0:66300c77c6e9 475 if(!!strcmp("OK",ret))
tax 0:66300c77c6e9 476 {
tax 0:66300c77c6e9 477 DBG("\r\nNot an OK <%s>.\r\n", ret);
tax 0:66300c77c6e9 478 flushBuffer();
tax 0:66300c77c6e9 479 return AT_ERROR;
tax 0:66300c77c6e9 480 }
tax 0:66300c77c6e9 481
tax 0:66300c77c6e9 482 DBG("\r\nCHECK OK\r\n");
tax 0:66300c77c6e9 483
tax 0:66300c77c6e9 484 return AT_OK;
tax 0:66300c77c6e9 485 }
tax 0:66300c77c6e9 486
tax 0:66300c77c6e9 487 #if 0
tax 0:66300c77c6e9 488 void ATIf::onSerialInterrupt() //Callback from m_pSerial
tax 0:66300c77c6e9 489 {
tax 0:66300c77c6e9 490 return;//FIXME
tax 0:66300c77c6e9 491
tax 0:66300c77c6e9 492 if(m_receiveStatus == AT_READING)
tax 0:66300c77c6e9 493 return;
tax 0:66300c77c6e9 494
tax 0:66300c77c6e9 495 if( m_cbObj && m_cbMeth )
tax 0:66300c77c6e9 496 return (m_cbObj->*m_cbMeth)();
tax 0:66300c77c6e9 497 }
tax 0:66300c77c6e9 498 #endif
tax 0:66300c77c6e9 499
tax 0:66300c77c6e9 500 int ATIf::readLine(char* line, int maxLen, int timeout) //Read a single line from serial port, return length or ATErr(<0)
tax 0:66300c77c6e9 501 {
tax 0:66300c77c6e9 502 #ifdef OLDREADLINE
tax 0:66300c77c6e9 503 if(!m_isOpen)
tax 0:66300c77c6e9 504 return AT_CLOSED;
tax 0:66300c77c6e9 505
tax 0:66300c77c6e9 506 int len = 0;
tax 0:66300c77c6e9 507
tax 0:66300c77c6e9 508 Timer timer;
tax 0:66300c77c6e9 509
tax 0:66300c77c6e9 510 timer.start();
tax 0:66300c77c6e9 511 #ifdef __START_CLRF_MANDAT
tax 0:66300c77c6e9 512 for( int i=0; i<2; i++ )
tax 0:66300c77c6e9 513 {
tax 0:66300c77c6e9 514 while(!readable())
tax 0:66300c77c6e9 515 {
tax 0:66300c77c6e9 516 if(timer.read_ms()>timeout)
tax 0:66300c77c6e9 517 {
tax 0:66300c77c6e9 518 // DBG("Timeout!!0");
tax 0:66300c77c6e9 519 return AT_TIMEOUT;
tax 0:66300c77c6e9 520 }
tax 0:66300c77c6e9 521 wait_ms(10); //Wait 10ms
tax 0:66300c77c6e9 522 }
tax 0:66300c77c6e9 523 *line = getc();
tax 0:66300c77c6e9 524 // DBG("In readLine(), read : %c", *line);
tax 0:66300c77c6e9 525 if( ( (i == 0) && (*line!='\x0D') )
tax 0:66300c77c6e9 526 || ( (i == 1) && (*line!='\x0A') ) )
tax 0:66300c77c6e9 527 return AT_PARSE;
tax 0:66300c77c6e9 528 }
tax 0:66300c77c6e9 529 #else
tax 0:66300c77c6e9 530
tax 0:66300c77c6e9 531 #endif
tax 0:66300c77c6e9 532
tax 0:66300c77c6e9 533 for( ; len < maxLen ; len++ )
tax 0:66300c77c6e9 534 {
tax 0:66300c77c6e9 535 timer.reset();
tax 0:66300c77c6e9 536 while(!readable())
tax 0:66300c77c6e9 537 {
tax 0:66300c77c6e9 538 if(timer.read_ms()>timeout)
tax 0:66300c77c6e9 539 {
tax 0:66300c77c6e9 540 // DBG("Timeout!!1");
tax 0:66300c77c6e9 541 return AT_TIMEOUT;
tax 0:66300c77c6e9 542 }
tax 0:66300c77c6e9 543 wait_ms(10); //Wait 10ms
tax 0:66300c77c6e9 544 }
tax 0:66300c77c6e9 545 *line = getc();
tax 0:66300c77c6e9 546 //DBG("In readLine(), read : %c", *line);
tax 0:66300c77c6e9 547
tax 0:66300c77c6e9 548 if(*line=='\x0D')
tax 0:66300c77c6e9 549 {
tax 0:66300c77c6e9 550 timer.reset();
tax 0:66300c77c6e9 551 while(!readable())
tax 0:66300c77c6e9 552 {
tax 0:66300c77c6e9 553 if(timer.read_ms()>timeout)
tax 0:66300c77c6e9 554 {
tax 0:66300c77c6e9 555 return AT_TIMEOUT;
tax 0:66300c77c6e9 556 }
tax 0:66300c77c6e9 557 wait_ms(10); //Wait 1ms
tax 0:66300c77c6e9 558 }
tax 0:66300c77c6e9 559 *line = getc();
tax 0:66300c77c6e9 560 // DBG("In readLine(), read : %c", *line);
tax 0:66300c77c6e9 561 if(*line=='\x0A')
tax 0:66300c77c6e9 562 {
tax 0:66300c77c6e9 563 if(len==0)
tax 0:66300c77c6e9 564 {
tax 0:66300c77c6e9 565 //Start of line
tax 0:66300c77c6e9 566 len--;
tax 0:66300c77c6e9 567 continue;
tax 0:66300c77c6e9 568 }
tax 0:66300c77c6e9 569 else
tax 0:66300c77c6e9 570 {
tax 0:66300c77c6e9 571 *line=0; //End of line
tax 0:66300c77c6e9 572 break;
tax 0:66300c77c6e9 573 }
tax 0:66300c77c6e9 574 }
tax 0:66300c77c6e9 575 else
tax 0:66300c77c6e9 576 {
tax 0:66300c77c6e9 577 //Should not happen, must have lost some bytes somewhere or non AT protocol
tax 0:66300c77c6e9 578 return AT_PARSE;
tax 0:66300c77c6e9 579 }
tax 0:66300c77c6e9 580 }
tax 0:66300c77c6e9 581 line++;
tax 0:66300c77c6e9 582 }
tax 0:66300c77c6e9 583
tax 0:66300c77c6e9 584 if(len==maxLen)
tax 0:66300c77c6e9 585 return AT_INCOMPLETE; //Buffer full, must call this method again to get end of line
tax 0:66300c77c6e9 586
tax 0:66300c77c6e9 587 return len;
tax 0:66300c77c6e9 588 #else
tax 0:66300c77c6e9 589 if(!m_isOpen)
tax 0:66300c77c6e9 590 return AT_CLOSED;
tax 0:66300c77c6e9 591
tax 0:66300c77c6e9 592 Timer timer;
tax 0:66300c77c6e9 593 timer.start();
tax 0:66300c77c6e9 594
tax 0:66300c77c6e9 595 int len = 0;
tax 0:66300c77c6e9 596 while( len < maxLen )
tax 0:66300c77c6e9 597 {
tax 0:66300c77c6e9 598 timer.reset();
tax 0:66300c77c6e9 599 while(!readable())
tax 0:66300c77c6e9 600 {
tax 0:66300c77c6e9 601 if(timer.read_ms()>timeout)
tax 0:66300c77c6e9 602 {
tax 0:66300c77c6e9 603 return AT_TIMEOUT;
tax 0:66300c77c6e9 604 }
tax 0:66300c77c6e9 605 wait_ms(10); //Wait 10ms
tax 0:66300c77c6e9 606 }
tax 0:66300c77c6e9 607 *line = getc();
tax 0:66300c77c6e9 608
tax 0:66300c77c6e9 609 if( (*line=='\x0D') || (*line=='\x0A') )
tax 0:66300c77c6e9 610 {
tax 0:66300c77c6e9 611
tax 0:66300c77c6e9 612 if(len==0)
tax 0:66300c77c6e9 613 {
tax 0:66300c77c6e9 614 //Start of line
tax 0:66300c77c6e9 615 continue;
tax 0:66300c77c6e9 616 }
tax 0:66300c77c6e9 617 else
tax 0:66300c77c6e9 618 {
tax 0:66300c77c6e9 619 *line=0; //End of line
tax 0:66300c77c6e9 620 break;
tax 0:66300c77c6e9 621 }
tax 0:66300c77c6e9 622 }
tax 0:66300c77c6e9 623 len++;
tax 0:66300c77c6e9 624 line++;
tax 0:66300c77c6e9 625 }
tax 0:66300c77c6e9 626
tax 0:66300c77c6e9 627 if(len==maxLen)
tax 0:66300c77c6e9 628 return AT_INCOMPLETE; //Buffer full, must call this method again to get end of line
tax 0:66300c77c6e9 629
tax 0:66300c77c6e9 630 return len;
tax 0:66300c77c6e9 631 #endif
tax 0:66300c77c6e9 632 }
tax 0:66300c77c6e9 633
tax 0:66300c77c6e9 634 int ATIf::writeLine(const char* line) //Write a single line to serial port
tax 0:66300c77c6e9 635 {
tax 0:66300c77c6e9 636 // char* line = (char*) _line;
tax 0:66300c77c6e9 637 if(!m_isOpen)
tax 0:66300c77c6e9 638 return AT_CLOSED;
tax 0:66300c77c6e9 639
tax 0:66300c77c6e9 640 // DBG("\n\rIn writeline.");
tax 0:66300c77c6e9 641
tax 0:66300c77c6e9 642 int len = 0;
tax 0:66300c77c6e9 643
tax 0:66300c77c6e9 644 while(*line)
tax 0:66300c77c6e9 645 {
tax 0:66300c77c6e9 646 putc(*line);
tax 0:66300c77c6e9 647 line++;
tax 0:66300c77c6e9 648 len++;
tax 0:66300c77c6e9 649 }
tax 0:66300c77c6e9 650
tax 0:66300c77c6e9 651 /* putc('\r');
tax 0:66300c77c6e9 652
tax 0:66300c77c6e9 653 putc('\n');*/
tax 0:66300c77c6e9 654
tax 0:66300c77c6e9 655 putc('\x0D');
tax 0:66300c77c6e9 656 // putc('\x0A');
tax 0:66300c77c6e9 657
tax 0:66300c77c6e9 658 // DBG("\n\rWritten %d + 1", len);
tax 0:66300c77c6e9 659
tax 0:66300c77c6e9 660 return len;
tax 0:66300c77c6e9 661
tax 0:66300c77c6e9 662 }
tax 0:66300c77c6e9 663
tax 0:66300c77c6e9 664
tax 0:66300c77c6e9 665
tax 0:66300c77c6e9 666 int ATIf::readRaw(char* str, int maxLen, int timeout /*= 0*/, int minLen /*= 0*/) //Read from serial port in buf
tax 0:66300c77c6e9 667 {
tax 0:66300c77c6e9 668 if(!m_isOpen)
tax 0:66300c77c6e9 669 return AT_CLOSED;
tax 0:66300c77c6e9 670
tax 0:66300c77c6e9 671 int len = 0;
tax 0:66300c77c6e9 672
tax 0:66300c77c6e9 673 Timer timer;
tax 0:66300c77c6e9 674
tax 0:66300c77c6e9 675 timer.start();
tax 0:66300c77c6e9 676
tax 0:66300c77c6e9 677 for( ; len < maxLen ; len++ )
tax 0:66300c77c6e9 678 {
tax 0:66300c77c6e9 679 while( (len < minLen) && !readable())
tax 0:66300c77c6e9 680 {
tax 0:66300c77c6e9 681 if(timer.read_ms()>timeout)
tax 0:66300c77c6e9 682 {
tax 0:66300c77c6e9 683 return AT_TIMEOUT;
tax 0:66300c77c6e9 684 }
tax 0:66300c77c6e9 685 wait(.01); //Wait 10ms
tax 0:66300c77c6e9 686 }
tax 0:66300c77c6e9 687
tax 0:66300c77c6e9 688 if(!readable()) //Buffer read entirely
tax 0:66300c77c6e9 689 break;
tax 0:66300c77c6e9 690
tax 0:66300c77c6e9 691 *str = getc();
tax 0:66300c77c6e9 692 str++;
tax 0:66300c77c6e9 693 len++;
tax 0:66300c77c6e9 694 }
tax 0:66300c77c6e9 695
tax 0:66300c77c6e9 696 *str = 0; //End char
tax 0:66300c77c6e9 697
tax 0:66300c77c6e9 698 return len;
tax 0:66300c77c6e9 699
tax 0:66300c77c6e9 700 }
tax 0:66300c77c6e9 701
tax 0:66300c77c6e9 702 int ATIf::writeRaw(const char* str) //Write directly to serial port
tax 0:66300c77c6e9 703 {
tax 0:66300c77c6e9 704 if(!m_isOpen)
tax 0:66300c77c6e9 705 return AT_CLOSED;
tax 0:66300c77c6e9 706
tax 0:66300c77c6e9 707 int len = 0;
tax 0:66300c77c6e9 708
tax 0:66300c77c6e9 709 while(*str)
tax 0:66300c77c6e9 710 {
tax 0:66300c77c6e9 711 putc(*str);
tax 0:66300c77c6e9 712 str++;
tax 0:66300c77c6e9 713 len++;
tax 0:66300c77c6e9 714 }
tax 0:66300c77c6e9 715
tax 0:66300c77c6e9 716 return len;
tax 0:66300c77c6e9 717 }
tax 0:66300c77c6e9 718
tax 0:66300c77c6e9 719 #if 0
tax 0:66300c77c6e9 720 bool ATIf::handleSignal()
tax 0:66300c77c6e9 721 {
tax 0:66300c77c6e9 722 bool beg = false;
tax 0:66300c77c6e9 723
tax 0:66300c77c6e9 724 // SerialBuf::setReadMode(true); //Keep chars in buf when read
tax 0:66300c77c6e9 725 // SerialBuf::resetRead();
tax 0:66300c77c6e9 726
tax 0:66300c77c6e9 727 //if( !m_pCurrentSignal ) //If no signal asked for this line
tax 0:66300c77c6e9 728 if(true) //Check anyway, could have been some parsing error before
tax 0:66300c77c6e9 729 {
tax 0:66300c77c6e9 730 //Extract Signal Name
tax 0:66300c77c6e9 731 char sigName[32]; //Should not be longer than that
tax 0:66300c77c6e9 732 setLineMode(true); //Read one line
tax 0:66300c77c6e9 733
tax 0:66300c77c6e9 734 int len = scanf("%[^:]:%*[^\n]", sigName);
tax 0:66300c77c6e9 735 if(len != 1)
tax 0:66300c77c6e9 736 return false; //This is not a signal
tax 0:66300c77c6e9 737 // DBG("\r\nGot signal %s\r\n", sigName);
tax 0:66300c77c6e9 738
tax 0:66300c77c6e9 739 list<ATSigHandler>::iterator it;
tax 0:66300c77c6e9 740
tax 0:66300c77c6e9 741 for ( it = m_signals.begin(); it != m_signals.end(); it++ )
tax 0:66300c77c6e9 742 {
tax 0:66300c77c6e9 743 if( !strcmp((*it).m_name, sigName) )
tax 0:66300c77c6e9 744 {
tax 0:66300c77c6e9 745 // DBG("\r\nFound signal %s\r\n", sigName);
tax 0:66300c77c6e9 746 m_pCurrentSignal = &(*it);
tax 0:66300c77c6e9 747 beg = true;
tax 0:66300c77c6e9 748 break;
tax 0:66300c77c6e9 749 }
tax 0:66300c77c6e9 750 }
tax 0:66300c77c6e9 751
tax 0:66300c77c6e9 752
tax 0:66300c77c6e9 753 }
tax 0:66300c77c6e9 754
tax 0:66300c77c6e9 755 if( !m_pCurrentSignal )
tax 0:66300c77c6e9 756 return false; //This is not a signal or it cannot be handled
tax 0:66300c77c6e9 757
tax 0:66300c77c6e9 758 bool moreData = false;
tax 0:66300c77c6e9 759 //Call signal handling routine
tax 0:66300c77c6e9 760 SerialBuf::resetRead(); //Rollback so that the handling fn can call scanf properly
tax 0:66300c77c6e9 761 bool result = ((m_pCurrentSignal->m_cbObj)->*(m_pCurrentSignal->m_cbMeth))(this, beg, &moreData);
tax 0:66300c77c6e9 762
tax 0:66300c77c6e9 763 if( !moreData ) //Processing completed
tax 0:66300c77c6e9 764 {
tax 0:66300c77c6e9 765 m_pCurrentSignal = NULL;
tax 0:66300c77c6e9 766 }
tax 0:66300c77c6e9 767
tax 0:66300c77c6e9 768 return result;
tax 0:66300c77c6e9 769 }
tax 0:66300c77c6e9 770 #endif
tax 0:66300c77c6e9 771
tax 0:66300c77c6e9 772 #endif