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