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