Disco-L475VG-IOT / wifi-ism43362
Committer:
marcel1691
Date:
Wed Oct 03 14:03:01 2018 +0000
Revision:
0:62e55edab701
WiFi ISM43363

Who changed what in which revision?

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