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