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