Parser for AT commands and similar protocols
Dependencies: BufferedSerial
Dependents: ESP8266 xdot-passthru Lab_10 Lab9 ... more
Fork of ATParser by
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 /** 00092 * Sets string of characters to use as line delimiters 00093 * 00094 * @param delimiter string of characters to use as line delimiters 00095 */ 00096 void setDelimiter(const char *delimiter) { 00097 _delimiter = delimiter; 00098 _delim_size = strlen(delimiter); 00099 } 00100 00101 /** 00102 * Allows echo to be on or off 00103 * 00104 * @param echo 1 for echo and 0 turns it off 00105 */ 00106 void debugOn(uint8_t on) { 00107 dbg_on = (on) ? 1 : 0; 00108 } 00109 00110 /** 00111 * Sends an AT command 00112 * 00113 * Sends a formatted command using printf style formatting 00114 * @see ::printf 00115 * 00116 * @param command printf-like format string of command to send which 00117 * is appended with the specified delimiter 00118 * @param ... all printf-like arguments to insert into command 00119 * @return true only if command is successfully sent 00120 */ 00121 bool send(const char *command, ...); 00122 bool vsend(const char *command, va_list args); 00123 00124 /** 00125 * Recieve an AT response 00126 * 00127 * Recieves a formatted response using scanf style formatting 00128 * @see ::scanf 00129 * 00130 * Responses are parsed line at a time using the specified delimiter. 00131 * Any recieved data that does not match the response is ignored until 00132 * a timeout occurs. 00133 * 00134 * @param response scanf-like format string of response to expect 00135 * @param ... all scanf-like arguments to extract from response 00136 * @return true only if response is successfully matched 00137 */ 00138 bool recv(const char *response, ...); 00139 bool vrecv(const char *response, va_list args); 00140 00141 /** 00142 * Write a single byte to the underlying stream 00143 * 00144 * @param c The byte to write 00145 * @return The byte that was written or -1 during a timeout 00146 */ 00147 int putc(char c); 00148 00149 /** 00150 * Get a single byte from the underlying stream 00151 * 00152 * @return The byte that was read or -1 during a timeout 00153 */ 00154 int getc(); 00155 00156 /** 00157 * Write an array of bytes to the underlying stream 00158 * 00159 * @param data the array of bytes to write 00160 * @param size number of bytes to write 00161 * @return number of bytes written or -1 on failure 00162 */ 00163 int write(const char *data, int size); 00164 00165 /** 00166 * Read an array of bytes from the underlying stream 00167 * 00168 * @param data the destination for the read bytes 00169 * @param size number of bytes to read 00170 * @return number of bytes read or -1 on failure 00171 */ 00172 int read(char *data, int size); 00173 00174 /** 00175 * Direct printf to underlying stream 00176 * @see ::printf 00177 * 00178 * @param format format string to pass to printf 00179 * @param ... arguments to printf 00180 * @return number of bytes written or -1 on failure 00181 */ 00182 int printf(const char *format, ...); 00183 int vprintf(const char *format, va_list args); 00184 00185 /** 00186 * Direct scanf on underlying stream 00187 * @see ::scanf 00188 * 00189 * @param format format string to pass to scanf 00190 * @param ... arguments to scanf 00191 * @return number of bytes read or -1 on failure 00192 */ 00193 int scanf(const char *format, ...); 00194 int vscanf(const char *format, va_list args); 00195 00196 /** 00197 * Flushes the underlying stream 00198 */ 00199 void flush(); 00200 }; 00201
Generated on Sun Jul 17 2022 04:40:46 by 1.7.2