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