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