Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Wed Dec 15 18:01:30 2010 +0000
Revision:
7:4e2468d7d5cb
Parent:
0:ac1725ba162c

        

Who changed what in which revision?

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