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