s
Embed:
(wiki syntax)
Show/hide line numbers
ATParser.h
00001 /* Copyright (c) 2015 ARM Limited 00002 * 00003 * Licensed under the Apache License, Version 2.0 (the "License"); 00004 * you may not use this file except in compliance with the License. 00005 * You may obtain a copy of the License at 00006 * 00007 * http://www.apache.org/licenses/LICENSE-2.0 00008 * 00009 * Unless required by applicable law or agreed to in writing, software 00010 * distributed under the License is distributed on an "AS IS" BASIS, 00011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 * See the License for the specific language governing permissions and 00013 * limitations under the License. 00014 * 00015 * @section DESCRIPTION 00016 * 00017 * Parser for the AT command syntax 00018 * 00019 */ 00020 00021 #include "mbed.h" 00022 #include <cstdarg> 00023 #include "BufferedSerial.h" 00024 00025 00026 /** 00027 * Parser class for parsing AT commands 00028 * 00029 * Here are some examples: 00030 * @code 00031 * ATParser at = ATParser(serial, "\r\n"); 00032 * int value; 00033 * char buffer[100]; 00034 * 00035 * at.send("AT") && at.recv("OK"); 00036 * at.send("AT+CWMODE=%d", 3) && at.recv("OK"); 00037 * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value); 00038 * at.recv("+IPD,%d:", &value); 00039 * at.read(buffer, value); 00040 * at.recv("OK"); 00041 * @endcode 00042 */ 00043 class ATParser 00044 { 00045 private: 00046 // Serial information 00047 BufferedSerial *_serial; 00048 int _buffer_size; 00049 char *_buffer; 00050 int _timeout; 00051 00052 // Parsing information 00053 const char *_delimiter; 00054 int _delim_size; 00055 bool dbg_on; 00056 00057 public: 00058 /** 00059 * Constructor 00060 * 00061 * @param serial serial interface to use for AT commands 00062 * @param buffer_size size of internal buffer for transaction 00063 * @param timeout timeout of the connection 00064 * @param delimiter string of characters to use as line delimiters 00065 */ 00066 ATParser(BufferedSerial &serial, const char *delimiter = "\r\n", int buffer_size = 256, int timeout = 8000, bool debug = false) : 00067 _serial(&serial), 00068 _buffer_size(buffer_size) { 00069 _buffer = new char[buffer_size]; 00070 setTimeout(timeout); 00071 setDelimiter(delimiter); 00072 debugOn(debug); 00073 } 00074 00075 /** 00076 * Destructor 00077 */ 00078 ~ATParser() { 00079 delete [] _buffer; 00080 } 00081 00082 /** 00083 * Allows timeout to be changed between commands 00084 * 00085 * @param timeout timeout of the connection 00086 */ 00087 void setTimeout(int timeout) { 00088 _timeout = timeout; 00089 } 00090 00091 int getTimeout(void) { 00092 return _timeout; 00093 } 00094 00095 /** 00096 * Sets string of characters to use as line delimiters 00097 * 00098 * @param delimiter string of characters to use as line delimiters 00099 */ 00100 void setDelimiter(const char *delimiter) { 00101 _delimiter = delimiter; 00102 _delim_size = strlen(delimiter); 00103 } 00104 00105 /** 00106 * Allows echo to be on or off 00107 * 00108 * @param echo 1 for echo and 0 turns it off 00109 */ 00110 void debugOn(uint8_t on) { 00111 dbg_on = (on) ? 1 : 0; 00112 } 00113 00114 /** 00115 * Sends an AT command 00116 * 00117 * Sends a formatted command using printf style formatting 00118 * @see ::printf 00119 * 00120 * @param command printf-like format string of command to send which 00121 * is appended with the specified delimiter 00122 * @param ... all printf-like arguments to insert into command 00123 * @return true only if command is successfully sent 00124 */ 00125 bool send(const char *command, ...); 00126 bool vsend(const char *command, va_list args); 00127 00128 /** 00129 * Recieve an AT response 00130 * 00131 * Recieves a formatted response using scanf style formatting 00132 * @see ::scanf 00133 * 00134 * Responses are parsed line at a time using the specified delimiter. 00135 * Any recieved data that does not match the response is ignored until 00136 * a timeout occurs. 00137 * 00138 * @param response scanf-like format string of response to expect 00139 * @param ... all scanf-like arguments to extract from response 00140 * @return true only if response is successfully matched 00141 */ 00142 bool recv(const char *response, ...); 00143 bool vrecv(const char *response, va_list args); 00144 00145 /** 00146 * Write a single byte to the underlying stream 00147 * 00148 * @param c The byte to write 00149 * @return The byte that was written or -1 during a timeout 00150 */ 00151 int putc(char c); 00152 00153 /** 00154 * Get a single byte from the underlying stream 00155 * 00156 * @return The byte that was read or -1 during a timeout 00157 */ 00158 int getc(); 00159 00160 /** 00161 * Write an array of bytes to the underlying stream 00162 * 00163 * @param data the array of bytes to write 00164 * @param size number of bytes to write 00165 * @return number of bytes written or -1 on failure 00166 */ 00167 int write(const char *data, int size); 00168 00169 /** 00170 * Read an array of bytes from the underlying stream 00171 * 00172 * @param data the destination for the read bytes 00173 * @param size number of bytes to read 00174 * @return number of bytes read or -1 on failure 00175 */ 00176 int read(char *data, int size); 00177 00178 /** 00179 * Direct printf to underlying stream 00180 * @see ::printf 00181 * 00182 * @param format format string to pass to printf 00183 * @param ... arguments to printf 00184 * @return number of bytes written or -1 on failure 00185 */ 00186 int printf(const char *format, ...); 00187 int vprintf(const char *format, va_list args); 00188 00189 /** 00190 * Direct scanf on underlying stream 00191 * @see ::scanf 00192 * 00193 * @param format format string to pass to scanf 00194 * @param ... arguments to scanf 00195 * @return number of bytes read or -1 on failure 00196 */ 00197 int scanf(const char *format, ...); 00198 int vscanf(const char *format, va_list args); 00199 00200 /** 00201 * Flushes the underlying stream 00202 */ 00203 void flush(); 00204 }; 00205
Generated on Thu Sep 1 2022 04:16:33 by 1.7.2