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