versione corretta

Dependents:   DISCO_L475VG_IOT01-Sensors-BSP

Committer:
group-Farnell24-IOT-Team
Date:
Tue Aug 21 08:34:28 2018 +0000
Revision:
0:766454e296c3
Initial commit

Who changed what in which revision?

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