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