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