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