From Ben Katz mbed-dev library. Removed unnecessary target files to reduce the overall size by a factor of 10 to make it easier to import into the online IDE.
Dependents: motor_driver motor_driver_screaming_fix
platform/ATCmdParser.h@0:083111ae2a11, 2020-11-26 (annotated)
- Committer:
- saloutos
- Date:
- Thu Nov 26 04:08:56 2020 +0000
- Revision:
- 0:083111ae2a11
first commit of leaned mbed dev lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saloutos | 0:083111ae2a11 | 1 | /* Copyright (c) 2017 ARM Limited |
saloutos | 0:083111ae2a11 | 2 | * |
saloutos | 0:083111ae2a11 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
saloutos | 0:083111ae2a11 | 4 | * you may not use this file except in compliance with the License. |
saloutos | 0:083111ae2a11 | 5 | * You may obtain a copy of the License at |
saloutos | 0:083111ae2a11 | 6 | * |
saloutos | 0:083111ae2a11 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
saloutos | 0:083111ae2a11 | 8 | * |
saloutos | 0:083111ae2a11 | 9 | * Unless required by applicable law or agreed to in writing, software |
saloutos | 0:083111ae2a11 | 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
saloutos | 0:083111ae2a11 | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
saloutos | 0:083111ae2a11 | 12 | * See the License for the specific language governing permissions and |
saloutos | 0:083111ae2a11 | 13 | * limitations under the License. |
saloutos | 0:083111ae2a11 | 14 | * |
saloutos | 0:083111ae2a11 | 15 | * @section DESCRIPTION |
saloutos | 0:083111ae2a11 | 16 | * |
saloutos | 0:083111ae2a11 | 17 | * Parser for the AT command syntax |
saloutos | 0:083111ae2a11 | 18 | * |
saloutos | 0:083111ae2a11 | 19 | */ |
saloutos | 0:083111ae2a11 | 20 | #ifndef MBED_ATCMDPARSER_H |
saloutos | 0:083111ae2a11 | 21 | #define MBED_ATCMDPARSER_H |
saloutos | 0:083111ae2a11 | 22 | |
saloutos | 0:083111ae2a11 | 23 | #include "mbed.h" |
saloutos | 0:083111ae2a11 | 24 | #include <cstdarg> |
saloutos | 0:083111ae2a11 | 25 | #include "Callback.h" |
saloutos | 0:083111ae2a11 | 26 | |
saloutos | 0:083111ae2a11 | 27 | /** |
saloutos | 0:083111ae2a11 | 28 | * Parser class for parsing AT commands |
saloutos | 0:083111ae2a11 | 29 | * |
saloutos | 0:083111ae2a11 | 30 | * Here are some examples: |
saloutos | 0:083111ae2a11 | 31 | * @code |
saloutos | 0:083111ae2a11 | 32 | * UARTSerial serial = UARTSerial(D1, D0); |
saloutos | 0:083111ae2a11 | 33 | * ATCmdParser at = ATCmdParser(&serial, "\r\n"); |
saloutos | 0:083111ae2a11 | 34 | * int value; |
saloutos | 0:083111ae2a11 | 35 | * char buffer[100]; |
saloutos | 0:083111ae2a11 | 36 | * |
saloutos | 0:083111ae2a11 | 37 | * at.send("AT") && at.recv("OK"); |
saloutos | 0:083111ae2a11 | 38 | * at.send("AT+CWMODE=%d", 3) && at.recv("OK"); |
saloutos | 0:083111ae2a11 | 39 | * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value); |
saloutos | 0:083111ae2a11 | 40 | * at.recv("+IPD,%d:", &value); |
saloutos | 0:083111ae2a11 | 41 | * at.read(buffer, value); |
saloutos | 0:083111ae2a11 | 42 | * at.recv("OK"); |
saloutos | 0:083111ae2a11 | 43 | * @endcode |
saloutos | 0:083111ae2a11 | 44 | */ |
saloutos | 0:083111ae2a11 | 45 | |
saloutos | 0:083111ae2a11 | 46 | namespace mbed { |
saloutos | 0:083111ae2a11 | 47 | |
saloutos | 0:083111ae2a11 | 48 | class ATCmdParser : private NonCopyable<ATCmdParser> |
saloutos | 0:083111ae2a11 | 49 | { |
saloutos | 0:083111ae2a11 | 50 | private: |
saloutos | 0:083111ae2a11 | 51 | // File handle |
saloutos | 0:083111ae2a11 | 52 | // Not owned by ATCmdParser |
saloutos | 0:083111ae2a11 | 53 | FileHandle *_fh; |
saloutos | 0:083111ae2a11 | 54 | |
saloutos | 0:083111ae2a11 | 55 | int _buffer_size; |
saloutos | 0:083111ae2a11 | 56 | char *_buffer; |
saloutos | 0:083111ae2a11 | 57 | int _timeout; |
saloutos | 0:083111ae2a11 | 58 | |
saloutos | 0:083111ae2a11 | 59 | // Parsing information |
saloutos | 0:083111ae2a11 | 60 | const char *_output_delimiter; |
saloutos | 0:083111ae2a11 | 61 | int _output_delim_size; |
saloutos | 0:083111ae2a11 | 62 | char _in_prev; |
saloutos | 0:083111ae2a11 | 63 | bool _dbg_on; |
saloutos | 0:083111ae2a11 | 64 | bool _aborted; |
saloutos | 0:083111ae2a11 | 65 | |
saloutos | 0:083111ae2a11 | 66 | struct oob { |
saloutos | 0:083111ae2a11 | 67 | unsigned len; |
saloutos | 0:083111ae2a11 | 68 | const char *prefix; |
saloutos | 0:083111ae2a11 | 69 | mbed::Callback<void()> cb; |
saloutos | 0:083111ae2a11 | 70 | oob *next; |
saloutos | 0:083111ae2a11 | 71 | }; |
saloutos | 0:083111ae2a11 | 72 | oob *_oobs; |
saloutos | 0:083111ae2a11 | 73 | |
saloutos | 0:083111ae2a11 | 74 | public: |
saloutos | 0:083111ae2a11 | 75 | |
saloutos | 0:083111ae2a11 | 76 | /** |
saloutos | 0:083111ae2a11 | 77 | * Constructor |
saloutos | 0:083111ae2a11 | 78 | * |
saloutos | 0:083111ae2a11 | 79 | * @param fh A FileHandle to a digital interface to use for AT commands |
saloutos | 0:083111ae2a11 | 80 | * @param output_delimiter end of command line termination |
saloutos | 0:083111ae2a11 | 81 | * @param buffer_size size of internal buffer for transaction |
saloutos | 0:083111ae2a11 | 82 | * @param timeout timeout of the connection |
saloutos | 0:083111ae2a11 | 83 | * @param debug turns on/off debug output for AT commands |
saloutos | 0:083111ae2a11 | 84 | */ |
saloutos | 0:083111ae2a11 | 85 | ATCmdParser(FileHandle *fh, const char *output_delimiter = "\r", |
saloutos | 0:083111ae2a11 | 86 | int buffer_size = 256, int timeout = 8000, bool debug = false) |
saloutos | 0:083111ae2a11 | 87 | : _fh(fh), _buffer_size(buffer_size), _in_prev(0), _oobs(NULL) |
saloutos | 0:083111ae2a11 | 88 | { |
saloutos | 0:083111ae2a11 | 89 | _buffer = new char[buffer_size]; |
saloutos | 0:083111ae2a11 | 90 | set_timeout(timeout); |
saloutos | 0:083111ae2a11 | 91 | set_delimiter(output_delimiter); |
saloutos | 0:083111ae2a11 | 92 | debug_on(debug); |
saloutos | 0:083111ae2a11 | 93 | } |
saloutos | 0:083111ae2a11 | 94 | |
saloutos | 0:083111ae2a11 | 95 | /** |
saloutos | 0:083111ae2a11 | 96 | * Destructor |
saloutos | 0:083111ae2a11 | 97 | */ |
saloutos | 0:083111ae2a11 | 98 | ~ATCmdParser() |
saloutos | 0:083111ae2a11 | 99 | { |
saloutos | 0:083111ae2a11 | 100 | while (_oobs) { |
saloutos | 0:083111ae2a11 | 101 | struct oob *oob = _oobs; |
saloutos | 0:083111ae2a11 | 102 | _oobs = oob->next; |
saloutos | 0:083111ae2a11 | 103 | delete oob; |
saloutos | 0:083111ae2a11 | 104 | } |
saloutos | 0:083111ae2a11 | 105 | delete[] _buffer; |
saloutos | 0:083111ae2a11 | 106 | } |
saloutos | 0:083111ae2a11 | 107 | |
saloutos | 0:083111ae2a11 | 108 | /** |
saloutos | 0:083111ae2a11 | 109 | * Allows timeout to be changed between commands |
saloutos | 0:083111ae2a11 | 110 | * |
saloutos | 0:083111ae2a11 | 111 | * @param timeout timeout of the connection |
saloutos | 0:083111ae2a11 | 112 | */ |
saloutos | 0:083111ae2a11 | 113 | void set_timeout(int timeout) |
saloutos | 0:083111ae2a11 | 114 | { |
saloutos | 0:083111ae2a11 | 115 | _timeout = timeout; |
saloutos | 0:083111ae2a11 | 116 | } |
saloutos | 0:083111ae2a11 | 117 | |
saloutos | 0:083111ae2a11 | 118 | /** |
saloutos | 0:083111ae2a11 | 119 | * For backwards compatibility. |
saloutos | 0:083111ae2a11 | 120 | * |
saloutos | 0:083111ae2a11 | 121 | * Please use set_timeout(int) API only from now on. |
saloutos | 0:083111ae2a11 | 122 | * Allows timeout to be changed between commands |
saloutos | 0:083111ae2a11 | 123 | * |
saloutos | 0:083111ae2a11 | 124 | * @param timeout timeout of the connection |
saloutos | 0:083111ae2a11 | 125 | */ |
saloutos | 0:083111ae2a11 | 126 | MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with set_timeout for consistency") |
saloutos | 0:083111ae2a11 | 127 | void setTimeout(int timeout) |
saloutos | 0:083111ae2a11 | 128 | { |
saloutos | 0:083111ae2a11 | 129 | set_timeout(timeout); |
saloutos | 0:083111ae2a11 | 130 | } |
saloutos | 0:083111ae2a11 | 131 | |
saloutos | 0:083111ae2a11 | 132 | /** |
saloutos | 0:083111ae2a11 | 133 | * Sets string of characters to use as line delimiters |
saloutos | 0:083111ae2a11 | 134 | * |
saloutos | 0:083111ae2a11 | 135 | * @param output_delimiter string of characters to use as line delimiters |
saloutos | 0:083111ae2a11 | 136 | */ |
saloutos | 0:083111ae2a11 | 137 | void set_delimiter(const char *output_delimiter) |
saloutos | 0:083111ae2a11 | 138 | { |
saloutos | 0:083111ae2a11 | 139 | _output_delimiter = output_delimiter; |
saloutos | 0:083111ae2a11 | 140 | _output_delim_size = strlen(output_delimiter); |
saloutos | 0:083111ae2a11 | 141 | } |
saloutos | 0:083111ae2a11 | 142 | |
saloutos | 0:083111ae2a11 | 143 | /** |
saloutos | 0:083111ae2a11 | 144 | * For backwards compatibility. |
saloutos | 0:083111ae2a11 | 145 | * |
saloutos | 0:083111ae2a11 | 146 | * Please use set_delimiter(const char *) API only from now on. |
saloutos | 0:083111ae2a11 | 147 | * Sets string of characters to use as line delimiters |
saloutos | 0:083111ae2a11 | 148 | * |
saloutos | 0:083111ae2a11 | 149 | * @param output_delimiter string of characters to use as line delimiters |
saloutos | 0:083111ae2a11 | 150 | */ |
saloutos | 0:083111ae2a11 | 151 | MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with set_delimiter for consistency") |
saloutos | 0:083111ae2a11 | 152 | void setDelimiter(const char *output_delimiter) |
saloutos | 0:083111ae2a11 | 153 | { |
saloutos | 0:083111ae2a11 | 154 | set_delimiter(output_delimiter); |
saloutos | 0:083111ae2a11 | 155 | } |
saloutos | 0:083111ae2a11 | 156 | |
saloutos | 0:083111ae2a11 | 157 | /** |
saloutos | 0:083111ae2a11 | 158 | * Allows traces from modem to be turned on or off |
saloutos | 0:083111ae2a11 | 159 | * |
saloutos | 0:083111ae2a11 | 160 | * @param on set as 1 to turn on traces and vice versa. |
saloutos | 0:083111ae2a11 | 161 | */ |
saloutos | 0:083111ae2a11 | 162 | void debug_on(uint8_t on) |
saloutos | 0:083111ae2a11 | 163 | { |
saloutos | 0:083111ae2a11 | 164 | _dbg_on = (on) ? 1 : 0; |
saloutos | 0:083111ae2a11 | 165 | } |
saloutos | 0:083111ae2a11 | 166 | |
saloutos | 0:083111ae2a11 | 167 | /** |
saloutos | 0:083111ae2a11 | 168 | * For backwards compatibility. |
saloutos | 0:083111ae2a11 | 169 | * |
saloutos | 0:083111ae2a11 | 170 | * Allows traces from modem to be turned on or off |
saloutos | 0:083111ae2a11 | 171 | * |
saloutos | 0:083111ae2a11 | 172 | * @param on set as 1 to turn on traces and vice versa. |
saloutos | 0:083111ae2a11 | 173 | */ |
saloutos | 0:083111ae2a11 | 174 | MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with debug_on for consistency") |
saloutos | 0:083111ae2a11 | 175 | void debugOn(uint8_t on) |
saloutos | 0:083111ae2a11 | 176 | { |
saloutos | 0:083111ae2a11 | 177 | debug_on(on); |
saloutos | 0:083111ae2a11 | 178 | } |
saloutos | 0:083111ae2a11 | 179 | |
saloutos | 0:083111ae2a11 | 180 | /** |
saloutos | 0:083111ae2a11 | 181 | * Sends an AT command |
saloutos | 0:083111ae2a11 | 182 | * |
saloutos | 0:083111ae2a11 | 183 | * Sends a formatted command using printf style formatting |
saloutos | 0:083111ae2a11 | 184 | * @see printf |
saloutos | 0:083111ae2a11 | 185 | * |
saloutos | 0:083111ae2a11 | 186 | * @param command printf-like format string of command to send which |
saloutos | 0:083111ae2a11 | 187 | * is appended with a newline |
saloutos | 0:083111ae2a11 | 188 | * @param ... all printf-like arguments to insert into command |
saloutos | 0:083111ae2a11 | 189 | * @return true only if command is successfully sent |
saloutos | 0:083111ae2a11 | 190 | */ |
saloutos | 0:083111ae2a11 | 191 | bool send(const char *command, ...) MBED_PRINTF_METHOD(1,2); |
saloutos | 0:083111ae2a11 | 192 | |
saloutos | 0:083111ae2a11 | 193 | bool vsend(const char *command, va_list args); |
saloutos | 0:083111ae2a11 | 194 | |
saloutos | 0:083111ae2a11 | 195 | /** |
saloutos | 0:083111ae2a11 | 196 | * Receive an AT response |
saloutos | 0:083111ae2a11 | 197 | * |
saloutos | 0:083111ae2a11 | 198 | * Receives a formatted response using scanf style formatting |
saloutos | 0:083111ae2a11 | 199 | * @see scanf |
saloutos | 0:083111ae2a11 | 200 | * |
saloutos | 0:083111ae2a11 | 201 | * Responses are parsed line at a time. |
saloutos | 0:083111ae2a11 | 202 | * Any received data that does not match the response is ignored until |
saloutos | 0:083111ae2a11 | 203 | * a timeout occurs. |
saloutos | 0:083111ae2a11 | 204 | * |
saloutos | 0:083111ae2a11 | 205 | * @param response scanf-like format string of response to expect |
saloutos | 0:083111ae2a11 | 206 | * @param ... all scanf-like arguments to extract from response |
saloutos | 0:083111ae2a11 | 207 | * @return true only if response is successfully matched |
saloutos | 0:083111ae2a11 | 208 | */ |
saloutos | 0:083111ae2a11 | 209 | bool recv(const char *response, ...) MBED_SCANF_METHOD(1,2); |
saloutos | 0:083111ae2a11 | 210 | |
saloutos | 0:083111ae2a11 | 211 | bool vrecv(const char *response, va_list args); |
saloutos | 0:083111ae2a11 | 212 | |
saloutos | 0:083111ae2a11 | 213 | /** |
saloutos | 0:083111ae2a11 | 214 | * Write a single byte to the underlying stream |
saloutos | 0:083111ae2a11 | 215 | * |
saloutos | 0:083111ae2a11 | 216 | * @param c The byte to write |
saloutos | 0:083111ae2a11 | 217 | * @return The byte that was written or -1 during a timeout |
saloutos | 0:083111ae2a11 | 218 | */ |
saloutos | 0:083111ae2a11 | 219 | int putc(char c); |
saloutos | 0:083111ae2a11 | 220 | |
saloutos | 0:083111ae2a11 | 221 | /** |
saloutos | 0:083111ae2a11 | 222 | * Get a single byte from the underlying stream |
saloutos | 0:083111ae2a11 | 223 | * |
saloutos | 0:083111ae2a11 | 224 | * @return The byte that was read or -1 during a timeout |
saloutos | 0:083111ae2a11 | 225 | */ |
saloutos | 0:083111ae2a11 | 226 | int getc(); |
saloutos | 0:083111ae2a11 | 227 | |
saloutos | 0:083111ae2a11 | 228 | /** |
saloutos | 0:083111ae2a11 | 229 | * Write an array of bytes to the underlying stream |
saloutos | 0:083111ae2a11 | 230 | * |
saloutos | 0:083111ae2a11 | 231 | * @param data the array of bytes to write |
saloutos | 0:083111ae2a11 | 232 | * @param size number of bytes to write |
saloutos | 0:083111ae2a11 | 233 | * @return number of bytes written or -1 on failure |
saloutos | 0:083111ae2a11 | 234 | */ |
saloutos | 0:083111ae2a11 | 235 | int write(const char *data, int size); |
saloutos | 0:083111ae2a11 | 236 | |
saloutos | 0:083111ae2a11 | 237 | /** |
saloutos | 0:083111ae2a11 | 238 | * Read an array of bytes from the underlying stream |
saloutos | 0:083111ae2a11 | 239 | * |
saloutos | 0:083111ae2a11 | 240 | * @param data the destination for the read bytes |
saloutos | 0:083111ae2a11 | 241 | * @param size number of bytes to read |
saloutos | 0:083111ae2a11 | 242 | * @return number of bytes read or -1 on failure |
saloutos | 0:083111ae2a11 | 243 | */ |
saloutos | 0:083111ae2a11 | 244 | int read(char *data, int size); |
saloutos | 0:083111ae2a11 | 245 | |
saloutos | 0:083111ae2a11 | 246 | /** |
saloutos | 0:083111ae2a11 | 247 | * Direct printf to underlying stream |
saloutos | 0:083111ae2a11 | 248 | * @see printf |
saloutos | 0:083111ae2a11 | 249 | * |
saloutos | 0:083111ae2a11 | 250 | * @param format format string to pass to printf |
saloutos | 0:083111ae2a11 | 251 | * @param ... arguments to printf |
saloutos | 0:083111ae2a11 | 252 | * @return number of bytes written or -1 on failure |
saloutos | 0:083111ae2a11 | 253 | */ |
saloutos | 0:083111ae2a11 | 254 | int printf(const char *format, ...) MBED_PRINTF_METHOD(1,2); |
saloutos | 0:083111ae2a11 | 255 | |
saloutos | 0:083111ae2a11 | 256 | int vprintf(const char *format, va_list args); |
saloutos | 0:083111ae2a11 | 257 | |
saloutos | 0:083111ae2a11 | 258 | /** |
saloutos | 0:083111ae2a11 | 259 | * Direct scanf on underlying stream |
saloutos | 0:083111ae2a11 | 260 | * @see scanf |
saloutos | 0:083111ae2a11 | 261 | * |
saloutos | 0:083111ae2a11 | 262 | * @param format format string to pass to scanf |
saloutos | 0:083111ae2a11 | 263 | * @param ... arguments to scanf |
saloutos | 0:083111ae2a11 | 264 | * @return number of bytes read or -1 on failure |
saloutos | 0:083111ae2a11 | 265 | */ |
saloutos | 0:083111ae2a11 | 266 | int scanf(const char *format, ...) MBED_SCANF_METHOD(1,2); |
saloutos | 0:083111ae2a11 | 267 | |
saloutos | 0:083111ae2a11 | 268 | int vscanf(const char *format, va_list args); |
saloutos | 0:083111ae2a11 | 269 | |
saloutos | 0:083111ae2a11 | 270 | /** |
saloutos | 0:083111ae2a11 | 271 | * Attach a callback for out-of-band data |
saloutos | 0:083111ae2a11 | 272 | * |
saloutos | 0:083111ae2a11 | 273 | * @param prefix string on when to initiate callback |
saloutos | 0:083111ae2a11 | 274 | * @param func callback to call when string is read |
saloutos | 0:083111ae2a11 | 275 | * @note out-of-band data is only processed during a scanf call |
saloutos | 0:083111ae2a11 | 276 | */ |
saloutos | 0:083111ae2a11 | 277 | void oob(const char *prefix, mbed::Callback<void()> func); |
saloutos | 0:083111ae2a11 | 278 | |
saloutos | 0:083111ae2a11 | 279 | /** |
saloutos | 0:083111ae2a11 | 280 | * Flushes the underlying stream |
saloutos | 0:083111ae2a11 | 281 | */ |
saloutos | 0:083111ae2a11 | 282 | void flush(); |
saloutos | 0:083111ae2a11 | 283 | |
saloutos | 0:083111ae2a11 | 284 | /** |
saloutos | 0:083111ae2a11 | 285 | * Abort current recv |
saloutos | 0:083111ae2a11 | 286 | * |
saloutos | 0:083111ae2a11 | 287 | * Can be called from oob handler to interrupt the current |
saloutos | 0:083111ae2a11 | 288 | * recv operation. |
saloutos | 0:083111ae2a11 | 289 | */ |
saloutos | 0:083111ae2a11 | 290 | void abort(); |
saloutos | 0:083111ae2a11 | 291 | }; |
saloutos | 0:083111ae2a11 | 292 | } //namespace mbed |
saloutos | 0:083111ae2a11 | 293 | |
saloutos | 0:083111ae2a11 | 294 | #endif //MBED_ATCMDPARSER_H |