Ram Gandikota
/
ABCD
A metronome using the FRDM K64F board
easy-connect/esp8266-driver/ESP8266/ATParser/ATParser.h@0:a7a43371b306, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:40:18 2017 +0000
- Revision:
- 0:a7a43371b306
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a7a43371b306 | 1 | /* Copyright (c) 2015 ARM Limited |
ram54288 | 0:a7a43371b306 | 2 | * |
ram54288 | 0:a7a43371b306 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ram54288 | 0:a7a43371b306 | 4 | * you may not use this file except in compliance with the License. |
ram54288 | 0:a7a43371b306 | 5 | * You may obtain a copy of the License at |
ram54288 | 0:a7a43371b306 | 6 | * |
ram54288 | 0:a7a43371b306 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a7a43371b306 | 8 | * |
ram54288 | 0:a7a43371b306 | 9 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a7a43371b306 | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
ram54288 | 0:a7a43371b306 | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a7a43371b306 | 12 | * See the License for the specific language governing permissions and |
ram54288 | 0:a7a43371b306 | 13 | * limitations under the License. |
ram54288 | 0:a7a43371b306 | 14 | * |
ram54288 | 0:a7a43371b306 | 15 | * @section DESCRIPTION |
ram54288 | 0:a7a43371b306 | 16 | * |
ram54288 | 0:a7a43371b306 | 17 | * Parser for the AT command syntax |
ram54288 | 0:a7a43371b306 | 18 | * |
ram54288 | 0:a7a43371b306 | 19 | */ |
ram54288 | 0:a7a43371b306 | 20 | #ifndef AT_PARSER_H |
ram54288 | 0:a7a43371b306 | 21 | #define AT_PARSER_H |
ram54288 | 0:a7a43371b306 | 22 | |
ram54288 | 0:a7a43371b306 | 23 | #include "mbed.h" |
ram54288 | 0:a7a43371b306 | 24 | #include <cstdarg> |
ram54288 | 0:a7a43371b306 | 25 | #include <vector> |
ram54288 | 0:a7a43371b306 | 26 | #include "BufferedSerial.h" |
ram54288 | 0:a7a43371b306 | 27 | #include "Callback.h" |
ram54288 | 0:a7a43371b306 | 28 | |
ram54288 | 0:a7a43371b306 | 29 | |
ram54288 | 0:a7a43371b306 | 30 | /** |
ram54288 | 0:a7a43371b306 | 31 | * Parser class for parsing AT commands |
ram54288 | 0:a7a43371b306 | 32 | * |
ram54288 | 0:a7a43371b306 | 33 | * Here are some examples: |
ram54288 | 0:a7a43371b306 | 34 | * @code |
ram54288 | 0:a7a43371b306 | 35 | * ATParser at = ATParser(serial, "\r\n"); |
ram54288 | 0:a7a43371b306 | 36 | * int value; |
ram54288 | 0:a7a43371b306 | 37 | * char buffer[100]; |
ram54288 | 0:a7a43371b306 | 38 | * |
ram54288 | 0:a7a43371b306 | 39 | * at.send("AT") && at.recv("OK"); |
ram54288 | 0:a7a43371b306 | 40 | * at.send("AT+CWMODE=%d", 3) && at.recv("OK"); |
ram54288 | 0:a7a43371b306 | 41 | * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value); |
ram54288 | 0:a7a43371b306 | 42 | * at.recv("+IPD,%d:", &value); |
ram54288 | 0:a7a43371b306 | 43 | * at.read(buffer, value); |
ram54288 | 0:a7a43371b306 | 44 | * at.recv("OK"); |
ram54288 | 0:a7a43371b306 | 45 | * @endcode |
ram54288 | 0:a7a43371b306 | 46 | */ |
ram54288 | 0:a7a43371b306 | 47 | class ATParser |
ram54288 | 0:a7a43371b306 | 48 | { |
ram54288 | 0:a7a43371b306 | 49 | private: |
ram54288 | 0:a7a43371b306 | 50 | // Serial information |
ram54288 | 0:a7a43371b306 | 51 | BufferedSerial *_serial; |
ram54288 | 0:a7a43371b306 | 52 | int _buffer_size; |
ram54288 | 0:a7a43371b306 | 53 | char *_buffer; |
ram54288 | 0:a7a43371b306 | 54 | int _timeout; |
ram54288 | 0:a7a43371b306 | 55 | |
ram54288 | 0:a7a43371b306 | 56 | // Parsing information |
ram54288 | 0:a7a43371b306 | 57 | const char *_delimiter; |
ram54288 | 0:a7a43371b306 | 58 | int _delim_size; |
ram54288 | 0:a7a43371b306 | 59 | bool dbg_on; |
ram54288 | 0:a7a43371b306 | 60 | |
ram54288 | 0:a7a43371b306 | 61 | struct oob { |
ram54288 | 0:a7a43371b306 | 62 | unsigned len; |
ram54288 | 0:a7a43371b306 | 63 | const char *prefix; |
ram54288 | 0:a7a43371b306 | 64 | mbed::Callback<void()> cb; |
ram54288 | 0:a7a43371b306 | 65 | }; |
ram54288 | 0:a7a43371b306 | 66 | std::vector<oob> _oobs; |
ram54288 | 0:a7a43371b306 | 67 | |
ram54288 | 0:a7a43371b306 | 68 | public: |
ram54288 | 0:a7a43371b306 | 69 | /** |
ram54288 | 0:a7a43371b306 | 70 | * Constructor |
ram54288 | 0:a7a43371b306 | 71 | * |
ram54288 | 0:a7a43371b306 | 72 | * @param serial serial interface to use for AT commands |
ram54288 | 0:a7a43371b306 | 73 | * @param buffer_size size of internal buffer for transaction |
ram54288 | 0:a7a43371b306 | 74 | * @param timeout timeout of the connection |
ram54288 | 0:a7a43371b306 | 75 | * @param delimiter string of characters to use as line delimiters |
ram54288 | 0:a7a43371b306 | 76 | */ |
ram54288 | 0:a7a43371b306 | 77 | ATParser(BufferedSerial &serial, const char *delimiter = "\r\n", int buffer_size = 256, int timeout = 8000, bool debug = false) : |
ram54288 | 0:a7a43371b306 | 78 | _serial(&serial), |
ram54288 | 0:a7a43371b306 | 79 | _buffer_size(buffer_size) { |
ram54288 | 0:a7a43371b306 | 80 | _buffer = new char[buffer_size]; |
ram54288 | 0:a7a43371b306 | 81 | setTimeout(timeout); |
ram54288 | 0:a7a43371b306 | 82 | setDelimiter(delimiter); |
ram54288 | 0:a7a43371b306 | 83 | debugOn(debug); |
ram54288 | 0:a7a43371b306 | 84 | } |
ram54288 | 0:a7a43371b306 | 85 | |
ram54288 | 0:a7a43371b306 | 86 | /** |
ram54288 | 0:a7a43371b306 | 87 | * Destructor |
ram54288 | 0:a7a43371b306 | 88 | */ |
ram54288 | 0:a7a43371b306 | 89 | ~ATParser() { |
ram54288 | 0:a7a43371b306 | 90 | delete [] _buffer; |
ram54288 | 0:a7a43371b306 | 91 | } |
ram54288 | 0:a7a43371b306 | 92 | |
ram54288 | 0:a7a43371b306 | 93 | /** |
ram54288 | 0:a7a43371b306 | 94 | * Allows timeout to be changed between commands |
ram54288 | 0:a7a43371b306 | 95 | * |
ram54288 | 0:a7a43371b306 | 96 | * @param timeout timeout of the connection |
ram54288 | 0:a7a43371b306 | 97 | */ |
ram54288 | 0:a7a43371b306 | 98 | void setTimeout(int timeout) { |
ram54288 | 0:a7a43371b306 | 99 | _timeout = timeout; |
ram54288 | 0:a7a43371b306 | 100 | } |
ram54288 | 0:a7a43371b306 | 101 | |
ram54288 | 0:a7a43371b306 | 102 | /** |
ram54288 | 0:a7a43371b306 | 103 | * Sets string of characters to use as line delimiters |
ram54288 | 0:a7a43371b306 | 104 | * |
ram54288 | 0:a7a43371b306 | 105 | * @param delimiter string of characters to use as line delimiters |
ram54288 | 0:a7a43371b306 | 106 | */ |
ram54288 | 0:a7a43371b306 | 107 | void setDelimiter(const char *delimiter) { |
ram54288 | 0:a7a43371b306 | 108 | _delimiter = delimiter; |
ram54288 | 0:a7a43371b306 | 109 | _delim_size = strlen(delimiter); |
ram54288 | 0:a7a43371b306 | 110 | } |
ram54288 | 0:a7a43371b306 | 111 | |
ram54288 | 0:a7a43371b306 | 112 | /** |
ram54288 | 0:a7a43371b306 | 113 | * Allows echo to be on or off |
ram54288 | 0:a7a43371b306 | 114 | * |
ram54288 | 0:a7a43371b306 | 115 | * @param echo 1 for echo and 0 turns it off |
ram54288 | 0:a7a43371b306 | 116 | */ |
ram54288 | 0:a7a43371b306 | 117 | void debugOn(uint8_t on) { |
ram54288 | 0:a7a43371b306 | 118 | dbg_on = (on) ? 1 : 0; |
ram54288 | 0:a7a43371b306 | 119 | } |
ram54288 | 0:a7a43371b306 | 120 | |
ram54288 | 0:a7a43371b306 | 121 | /** |
ram54288 | 0:a7a43371b306 | 122 | * Sends an AT command |
ram54288 | 0:a7a43371b306 | 123 | * |
ram54288 | 0:a7a43371b306 | 124 | * Sends a formatted command using printf style formatting |
ram54288 | 0:a7a43371b306 | 125 | * @see ::printf |
ram54288 | 0:a7a43371b306 | 126 | * |
ram54288 | 0:a7a43371b306 | 127 | * @param command printf-like format string of command to send which |
ram54288 | 0:a7a43371b306 | 128 | * is appended with the specified delimiter |
ram54288 | 0:a7a43371b306 | 129 | * @param ... all printf-like arguments to insert into command |
ram54288 | 0:a7a43371b306 | 130 | * @return true only if command is successfully sent |
ram54288 | 0:a7a43371b306 | 131 | */ |
ram54288 | 0:a7a43371b306 | 132 | bool send(const char *command, ...); |
ram54288 | 0:a7a43371b306 | 133 | bool vsend(const char *command, va_list args); |
ram54288 | 0:a7a43371b306 | 134 | |
ram54288 | 0:a7a43371b306 | 135 | /** |
ram54288 | 0:a7a43371b306 | 136 | * Recieve an AT response |
ram54288 | 0:a7a43371b306 | 137 | * |
ram54288 | 0:a7a43371b306 | 138 | * Recieves a formatted response using scanf style formatting |
ram54288 | 0:a7a43371b306 | 139 | * @see ::scanf |
ram54288 | 0:a7a43371b306 | 140 | * |
ram54288 | 0:a7a43371b306 | 141 | * Responses are parsed line at a time using the specified delimiter. |
ram54288 | 0:a7a43371b306 | 142 | * Any recieved data that does not match the response is ignored until |
ram54288 | 0:a7a43371b306 | 143 | * a timeout occurs. |
ram54288 | 0:a7a43371b306 | 144 | * |
ram54288 | 0:a7a43371b306 | 145 | * @param response scanf-like format string of response to expect |
ram54288 | 0:a7a43371b306 | 146 | * @param ... all scanf-like arguments to extract from response |
ram54288 | 0:a7a43371b306 | 147 | * @return true only if response is successfully matched |
ram54288 | 0:a7a43371b306 | 148 | */ |
ram54288 | 0:a7a43371b306 | 149 | bool recv(const char *response, ...); |
ram54288 | 0:a7a43371b306 | 150 | bool vrecv(const char *response, va_list args); |
ram54288 | 0:a7a43371b306 | 151 | |
ram54288 | 0:a7a43371b306 | 152 | /** |
ram54288 | 0:a7a43371b306 | 153 | * Write a single byte to the underlying stream |
ram54288 | 0:a7a43371b306 | 154 | * |
ram54288 | 0:a7a43371b306 | 155 | * @param c The byte to write |
ram54288 | 0:a7a43371b306 | 156 | * @return The byte that was written or -1 during a timeout |
ram54288 | 0:a7a43371b306 | 157 | */ |
ram54288 | 0:a7a43371b306 | 158 | int putc(char c); |
ram54288 | 0:a7a43371b306 | 159 | |
ram54288 | 0:a7a43371b306 | 160 | /** |
ram54288 | 0:a7a43371b306 | 161 | * Get a single byte from the underlying stream |
ram54288 | 0:a7a43371b306 | 162 | * |
ram54288 | 0:a7a43371b306 | 163 | * @return The byte that was read or -1 during a timeout |
ram54288 | 0:a7a43371b306 | 164 | */ |
ram54288 | 0:a7a43371b306 | 165 | int getc(); |
ram54288 | 0:a7a43371b306 | 166 | |
ram54288 | 0:a7a43371b306 | 167 | /** |
ram54288 | 0:a7a43371b306 | 168 | * Write an array of bytes to the underlying stream |
ram54288 | 0:a7a43371b306 | 169 | * |
ram54288 | 0:a7a43371b306 | 170 | * @param data the array of bytes to write |
ram54288 | 0:a7a43371b306 | 171 | * @param size number of bytes to write |
ram54288 | 0:a7a43371b306 | 172 | * @return number of bytes written or -1 on failure |
ram54288 | 0:a7a43371b306 | 173 | */ |
ram54288 | 0:a7a43371b306 | 174 | int write(const char *data, int size); |
ram54288 | 0:a7a43371b306 | 175 | |
ram54288 | 0:a7a43371b306 | 176 | /** |
ram54288 | 0:a7a43371b306 | 177 | * Read an array of bytes from the underlying stream |
ram54288 | 0:a7a43371b306 | 178 | * |
ram54288 | 0:a7a43371b306 | 179 | * @param data the destination for the read bytes |
ram54288 | 0:a7a43371b306 | 180 | * @param size number of bytes to read |
ram54288 | 0:a7a43371b306 | 181 | * @return number of bytes read or -1 on failure |
ram54288 | 0:a7a43371b306 | 182 | */ |
ram54288 | 0:a7a43371b306 | 183 | int read(char *data, int size); |
ram54288 | 0:a7a43371b306 | 184 | |
ram54288 | 0:a7a43371b306 | 185 | /** |
ram54288 | 0:a7a43371b306 | 186 | * Direct printf to underlying stream |
ram54288 | 0:a7a43371b306 | 187 | * @see ::printf |
ram54288 | 0:a7a43371b306 | 188 | * |
ram54288 | 0:a7a43371b306 | 189 | * @param format format string to pass to printf |
ram54288 | 0:a7a43371b306 | 190 | * @param ... arguments to printf |
ram54288 | 0:a7a43371b306 | 191 | * @return number of bytes written or -1 on failure |
ram54288 | 0:a7a43371b306 | 192 | */ |
ram54288 | 0:a7a43371b306 | 193 | int printf(const char *format, ...); |
ram54288 | 0:a7a43371b306 | 194 | int vprintf(const char *format, va_list args); |
ram54288 | 0:a7a43371b306 | 195 | |
ram54288 | 0:a7a43371b306 | 196 | /** |
ram54288 | 0:a7a43371b306 | 197 | * Direct scanf on underlying stream |
ram54288 | 0:a7a43371b306 | 198 | * @see ::scanf |
ram54288 | 0:a7a43371b306 | 199 | * |
ram54288 | 0:a7a43371b306 | 200 | * @param format format string to pass to scanf |
ram54288 | 0:a7a43371b306 | 201 | * @param ... arguments to scanf |
ram54288 | 0:a7a43371b306 | 202 | * @return number of bytes read or -1 on failure |
ram54288 | 0:a7a43371b306 | 203 | */ |
ram54288 | 0:a7a43371b306 | 204 | int scanf(const char *format, ...); |
ram54288 | 0:a7a43371b306 | 205 | int vscanf(const char *format, va_list args); |
ram54288 | 0:a7a43371b306 | 206 | |
ram54288 | 0:a7a43371b306 | 207 | /** |
ram54288 | 0:a7a43371b306 | 208 | * Attach a callback for out-of-band data |
ram54288 | 0:a7a43371b306 | 209 | * |
ram54288 | 0:a7a43371b306 | 210 | * @param prefix string on when to initiate callback |
ram54288 | 0:a7a43371b306 | 211 | * @param func callback to call when string is read |
ram54288 | 0:a7a43371b306 | 212 | * @note out-of-band data is only processed during a scanf call |
ram54288 | 0:a7a43371b306 | 213 | */ |
ram54288 | 0:a7a43371b306 | 214 | void oob(const char *prefix, mbed::Callback<void()> func); |
ram54288 | 0:a7a43371b306 | 215 | |
ram54288 | 0:a7a43371b306 | 216 | /** |
ram54288 | 0:a7a43371b306 | 217 | * Attach a callback for out-of-band data |
ram54288 | 0:a7a43371b306 | 218 | * |
ram54288 | 0:a7a43371b306 | 219 | * @param prefix string on when to initiate callback |
ram54288 | 0:a7a43371b306 | 220 | * @param obj pointer to object to call member function on |
ram54288 | 0:a7a43371b306 | 221 | * @param method callback to call when string is read |
ram54288 | 0:a7a43371b306 | 222 | * @note out-of-band data is only processed during a scanf call |
ram54288 | 0:a7a43371b306 | 223 | */ |
ram54288 | 0:a7a43371b306 | 224 | template <typename T, typename M> |
ram54288 | 0:a7a43371b306 | 225 | void oob(const char *prefix, T *obj, M method) { |
ram54288 | 0:a7a43371b306 | 226 | return oob(prefix, mbed::Callback<void()>(obj, method)); |
ram54288 | 0:a7a43371b306 | 227 | } |
ram54288 | 0:a7a43371b306 | 228 | |
ram54288 | 0:a7a43371b306 | 229 | /** |
ram54288 | 0:a7a43371b306 | 230 | * Flushes the underlying stream |
ram54288 | 0:a7a43371b306 | 231 | */ |
ram54288 | 0:a7a43371b306 | 232 | void flush(); |
ram54288 | 0:a7a43371b306 | 233 | }; |
ram54288 | 0:a7a43371b306 | 234 | #endif |