Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 /* Copyright (c) 2017 ARM Limited
borlanic 0:02dd72d1d465 2 *
borlanic 0:02dd72d1d465 3 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 4 * you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 5 * You may obtain a copy of the License at
borlanic 0:02dd72d1d465 6 *
borlanic 0:02dd72d1d465 7 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 8 *
borlanic 0:02dd72d1d465 9 * Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 10 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 12 * See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 13 * limitations under the License.
borlanic 0:02dd72d1d465 14 *
borlanic 0:02dd72d1d465 15 * @section DESCRIPTION
borlanic 0:02dd72d1d465 16 *
borlanic 0:02dd72d1d465 17 * Parser for the AT command syntax
borlanic 0:02dd72d1d465 18 *
borlanic 0:02dd72d1d465 19 */
borlanic 0:02dd72d1d465 20 #ifndef MBED_ATCMDPARSER_H
borlanic 0:02dd72d1d465 21 #define MBED_ATCMDPARSER_H
borlanic 0:02dd72d1d465 22
borlanic 0:02dd72d1d465 23 #include "mbed.h"
borlanic 0:02dd72d1d465 24 #include <cstdarg>
borlanic 0:02dd72d1d465 25 #include "Callback.h"
borlanic 0:02dd72d1d465 26
borlanic 0:02dd72d1d465 27 namespace mbed {
borlanic 0:02dd72d1d465 28
borlanic 0:02dd72d1d465 29 /** \addtogroup platform */
borlanic 0:02dd72d1d465 30 /** @{*/
borlanic 0:02dd72d1d465 31 /**
borlanic 0:02dd72d1d465 32 * \defgroup platform_ATCmdParser ATCmdParser class
borlanic 0:02dd72d1d465 33 * @{
borlanic 0:02dd72d1d465 34 */
borlanic 0:02dd72d1d465 35
borlanic 0:02dd72d1d465 36 /**
borlanic 0:02dd72d1d465 37 * Parser class for parsing AT commands
borlanic 0:02dd72d1d465 38 *
borlanic 0:02dd72d1d465 39 * Here are some examples:
borlanic 0:02dd72d1d465 40 * @code
borlanic 0:02dd72d1d465 41 * UARTSerial serial = UARTSerial(D1, D0);
borlanic 0:02dd72d1d465 42 * ATCmdParser at = ATCmdParser(&serial, "\r\n");
borlanic 0:02dd72d1d465 43 * int value;
borlanic 0:02dd72d1d465 44 * char buffer[100];
borlanic 0:02dd72d1d465 45 *
borlanic 0:02dd72d1d465 46 * at.send("AT") && at.recv("OK");
borlanic 0:02dd72d1d465 47 * at.send("AT+CWMODE=%d", 3) && at.recv("OK");
borlanic 0:02dd72d1d465 48 * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value);
borlanic 0:02dd72d1d465 49 * at.recv("+IPD,%d:", &value);
borlanic 0:02dd72d1d465 50 * at.read(buffer, value);
borlanic 0:02dd72d1d465 51 * at.recv("OK");
borlanic 0:02dd72d1d465 52 * @endcode
borlanic 0:02dd72d1d465 53 */
borlanic 0:02dd72d1d465 54
borlanic 0:02dd72d1d465 55 class ATCmdParser : private NonCopyable<ATCmdParser>
borlanic 0:02dd72d1d465 56 {
borlanic 0:02dd72d1d465 57 private:
borlanic 0:02dd72d1d465 58 // File handle
borlanic 0:02dd72d1d465 59 // Not owned by ATCmdParser
borlanic 0:02dd72d1d465 60 FileHandle *_fh;
borlanic 0:02dd72d1d465 61
borlanic 0:02dd72d1d465 62 int _buffer_size;
borlanic 0:02dd72d1d465 63 char *_buffer;
borlanic 0:02dd72d1d465 64 int _timeout;
borlanic 0:02dd72d1d465 65
borlanic 0:02dd72d1d465 66 // Parsing information
borlanic 0:02dd72d1d465 67 const char *_output_delimiter;
borlanic 0:02dd72d1d465 68 int _output_delim_size;
borlanic 0:02dd72d1d465 69 char _in_prev;
borlanic 0:02dd72d1d465 70 bool _dbg_on;
borlanic 0:02dd72d1d465 71 bool _aborted;
borlanic 0:02dd72d1d465 72
borlanic 0:02dd72d1d465 73 struct oob {
borlanic 0:02dd72d1d465 74 unsigned len;
borlanic 0:02dd72d1d465 75 const char *prefix;
borlanic 0:02dd72d1d465 76 mbed::Callback<void()> cb;
borlanic 0:02dd72d1d465 77 oob *next;
borlanic 0:02dd72d1d465 78 };
borlanic 0:02dd72d1d465 79 oob *_oobs;
borlanic 0:02dd72d1d465 80
borlanic 0:02dd72d1d465 81 public:
borlanic 0:02dd72d1d465 82
borlanic 0:02dd72d1d465 83 /**
borlanic 0:02dd72d1d465 84 * Constructor
borlanic 0:02dd72d1d465 85 *
borlanic 0:02dd72d1d465 86 * @param fh A FileHandle to a digital interface to use for AT commands
borlanic 0:02dd72d1d465 87 * @param output_delimiter end of command line termination
borlanic 0:02dd72d1d465 88 * @param buffer_size size of internal buffer for transaction
borlanic 0:02dd72d1d465 89 * @param timeout timeout of the connection
borlanic 0:02dd72d1d465 90 * @param debug turns on/off debug output for AT commands
borlanic 0:02dd72d1d465 91 */
borlanic 0:02dd72d1d465 92 ATCmdParser(FileHandle *fh, const char *output_delimiter = "\r",
borlanic 0:02dd72d1d465 93 int buffer_size = 256, int timeout = 8000, bool debug = false)
borlanic 0:02dd72d1d465 94 : _fh(fh), _buffer_size(buffer_size), _in_prev(0), _oobs(NULL)
borlanic 0:02dd72d1d465 95 {
borlanic 0:02dd72d1d465 96 _buffer = new char[buffer_size];
borlanic 0:02dd72d1d465 97 set_timeout(timeout);
borlanic 0:02dd72d1d465 98 set_delimiter(output_delimiter);
borlanic 0:02dd72d1d465 99 debug_on(debug);
borlanic 0:02dd72d1d465 100 }
borlanic 0:02dd72d1d465 101
borlanic 0:02dd72d1d465 102 /**
borlanic 0:02dd72d1d465 103 * Destructor
borlanic 0:02dd72d1d465 104 */
borlanic 0:02dd72d1d465 105 ~ATCmdParser()
borlanic 0:02dd72d1d465 106 {
borlanic 0:02dd72d1d465 107 while (_oobs) {
borlanic 0:02dd72d1d465 108 struct oob *oob = _oobs;
borlanic 0:02dd72d1d465 109 _oobs = oob->next;
borlanic 0:02dd72d1d465 110 delete oob;
borlanic 0:02dd72d1d465 111 }
borlanic 0:02dd72d1d465 112 delete[] _buffer;
borlanic 0:02dd72d1d465 113 }
borlanic 0:02dd72d1d465 114
borlanic 0:02dd72d1d465 115 /**
borlanic 0:02dd72d1d465 116 * Allows timeout to be changed between commands
borlanic 0:02dd72d1d465 117 *
borlanic 0:02dd72d1d465 118 * @param timeout timeout of the connection
borlanic 0:02dd72d1d465 119 */
borlanic 0:02dd72d1d465 120 void set_timeout(int timeout)
borlanic 0:02dd72d1d465 121 {
borlanic 0:02dd72d1d465 122 _timeout = timeout;
borlanic 0:02dd72d1d465 123 }
borlanic 0:02dd72d1d465 124
borlanic 0:02dd72d1d465 125 /**
borlanic 0:02dd72d1d465 126 * For backwards compatibility.
borlanic 0:02dd72d1d465 127 * @deprecated Do not use this function. This function has been replaced with set_timeout for consistency.
borlanic 0:02dd72d1d465 128 *
borlanic 0:02dd72d1d465 129 * Please use set_timeout(int) API only from now on.
borlanic 0:02dd72d1d465 130 * Allows timeout to be changed between commands
borlanic 0:02dd72d1d465 131 *
borlanic 0:02dd72d1d465 132 * @param timeout timeout of the connection
borlanic 0:02dd72d1d465 133 */
borlanic 0:02dd72d1d465 134 MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with set_timeout for consistency")
borlanic 0:02dd72d1d465 135 void setTimeout(int timeout)
borlanic 0:02dd72d1d465 136 {
borlanic 0:02dd72d1d465 137 set_timeout(timeout);
borlanic 0:02dd72d1d465 138 }
borlanic 0:02dd72d1d465 139
borlanic 0:02dd72d1d465 140 /**
borlanic 0:02dd72d1d465 141 * Sets string of characters to use as line delimiters
borlanic 0:02dd72d1d465 142 *
borlanic 0:02dd72d1d465 143 * @param output_delimiter string of characters to use as line delimiters
borlanic 0:02dd72d1d465 144 */
borlanic 0:02dd72d1d465 145 void set_delimiter(const char *output_delimiter)
borlanic 0:02dd72d1d465 146 {
borlanic 0:02dd72d1d465 147 _output_delimiter = output_delimiter;
borlanic 0:02dd72d1d465 148 _output_delim_size = strlen(output_delimiter);
borlanic 0:02dd72d1d465 149 }
borlanic 0:02dd72d1d465 150
borlanic 0:02dd72d1d465 151 /**
borlanic 0:02dd72d1d465 152 * For backwards compatibility.
borlanic 0:02dd72d1d465 153 * @deprecated Do not use this function. This function has been replaced with set_delimiter for consistency.
borlanic 0:02dd72d1d465 154 *
borlanic 0:02dd72d1d465 155 * Please use set_delimiter(const char *) API only from now on.
borlanic 0:02dd72d1d465 156 * Sets string of characters to use as line delimiters
borlanic 0:02dd72d1d465 157 *
borlanic 0:02dd72d1d465 158 * @param output_delimiter string of characters to use as line delimiters
borlanic 0:02dd72d1d465 159 */
borlanic 0:02dd72d1d465 160 MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with set_delimiter for consistency")
borlanic 0:02dd72d1d465 161 void setDelimiter(const char *output_delimiter)
borlanic 0:02dd72d1d465 162 {
borlanic 0:02dd72d1d465 163 set_delimiter(output_delimiter);
borlanic 0:02dd72d1d465 164 }
borlanic 0:02dd72d1d465 165
borlanic 0:02dd72d1d465 166 /**
borlanic 0:02dd72d1d465 167 * Allows traces from modem to be turned on or off
borlanic 0:02dd72d1d465 168 *
borlanic 0:02dd72d1d465 169 * @param on set as 1 to turn on traces and vice versa.
borlanic 0:02dd72d1d465 170 */
borlanic 0:02dd72d1d465 171 void debug_on(uint8_t on)
borlanic 0:02dd72d1d465 172 {
borlanic 0:02dd72d1d465 173 _dbg_on = (on) ? 1 : 0;
borlanic 0:02dd72d1d465 174 }
borlanic 0:02dd72d1d465 175
borlanic 0:02dd72d1d465 176 /**
borlanic 0:02dd72d1d465 177 * For backwards compatibility.
borlanic 0:02dd72d1d465 178 * @deprecated Do not use this function. This function has been replaced with debug_on for consistency.
borlanic 0:02dd72d1d465 179 *
borlanic 0:02dd72d1d465 180 * Allows traces from modem to be turned on or off
borlanic 0:02dd72d1d465 181 *
borlanic 0:02dd72d1d465 182 * @param on set as 1 to turn on traces and vice versa.
borlanic 0:02dd72d1d465 183 */
borlanic 0:02dd72d1d465 184 MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with debug_on for consistency")
borlanic 0:02dd72d1d465 185 void debugOn(uint8_t on)
borlanic 0:02dd72d1d465 186 {
borlanic 0:02dd72d1d465 187 debug_on(on);
borlanic 0:02dd72d1d465 188 }
borlanic 0:02dd72d1d465 189
borlanic 0:02dd72d1d465 190 /**
borlanic 0:02dd72d1d465 191 * Sends an AT command
borlanic 0:02dd72d1d465 192 *
borlanic 0:02dd72d1d465 193 * Sends a formatted command using printf style formatting
borlanic 0:02dd72d1d465 194 * @see printf
borlanic 0:02dd72d1d465 195 *
borlanic 0:02dd72d1d465 196 * @param command printf-like format string of command to send which
borlanic 0:02dd72d1d465 197 * is appended with a newline
borlanic 0:02dd72d1d465 198 * @param ... all printf-like arguments to insert into command
borlanic 0:02dd72d1d465 199 * @return true only if command is successfully sent
borlanic 0:02dd72d1d465 200 */
borlanic 0:02dd72d1d465 201 bool send(const char *command, ...) MBED_PRINTF_METHOD(1,2);
borlanic 0:02dd72d1d465 202
borlanic 0:02dd72d1d465 203 bool vsend(const char *command, va_list args);
borlanic 0:02dd72d1d465 204
borlanic 0:02dd72d1d465 205 /**
borlanic 0:02dd72d1d465 206 * Receive an AT response
borlanic 0:02dd72d1d465 207 *
borlanic 0:02dd72d1d465 208 * Receives a formatted response using scanf style formatting
borlanic 0:02dd72d1d465 209 * @see scanf
borlanic 0:02dd72d1d465 210 *
borlanic 0:02dd72d1d465 211 * Responses are parsed line at a time.
borlanic 0:02dd72d1d465 212 * Any received data that does not match the response is ignored until
borlanic 0:02dd72d1d465 213 * a timeout occurs.
borlanic 0:02dd72d1d465 214 *
borlanic 0:02dd72d1d465 215 * @param response scanf-like format string of response to expect
borlanic 0:02dd72d1d465 216 * @param ... all scanf-like arguments to extract from response
borlanic 0:02dd72d1d465 217 * @return true only if response is successfully matched
borlanic 0:02dd72d1d465 218 */
borlanic 0:02dd72d1d465 219 bool recv(const char *response, ...) MBED_SCANF_METHOD(1,2);
borlanic 0:02dd72d1d465 220
borlanic 0:02dd72d1d465 221 bool vrecv(const char *response, va_list args);
borlanic 0:02dd72d1d465 222
borlanic 0:02dd72d1d465 223 /**
borlanic 0:02dd72d1d465 224 * Write a single byte to the underlying stream
borlanic 0:02dd72d1d465 225 *
borlanic 0:02dd72d1d465 226 * @param c The byte to write
borlanic 0:02dd72d1d465 227 * @return The byte that was written or -1 during a timeout
borlanic 0:02dd72d1d465 228 */
borlanic 0:02dd72d1d465 229 int putc(char c);
borlanic 0:02dd72d1d465 230
borlanic 0:02dd72d1d465 231 /**
borlanic 0:02dd72d1d465 232 * Get a single byte from the underlying stream
borlanic 0:02dd72d1d465 233 *
borlanic 0:02dd72d1d465 234 * @return The byte that was read or -1 during a timeout
borlanic 0:02dd72d1d465 235 */
borlanic 0:02dd72d1d465 236 int getc();
borlanic 0:02dd72d1d465 237
borlanic 0:02dd72d1d465 238 /**
borlanic 0:02dd72d1d465 239 * Write an array of bytes to the underlying stream
borlanic 0:02dd72d1d465 240 *
borlanic 0:02dd72d1d465 241 * @param data the array of bytes to write
borlanic 0:02dd72d1d465 242 * @param size number of bytes to write
borlanic 0:02dd72d1d465 243 * @return number of bytes written or -1 on failure
borlanic 0:02dd72d1d465 244 */
borlanic 0:02dd72d1d465 245 int write(const char *data, int size);
borlanic 0:02dd72d1d465 246
borlanic 0:02dd72d1d465 247 /**
borlanic 0:02dd72d1d465 248 * Read an array of bytes from the underlying stream
borlanic 0:02dd72d1d465 249 *
borlanic 0:02dd72d1d465 250 * @param data the destination for the read bytes
borlanic 0:02dd72d1d465 251 * @param size number of bytes to read
borlanic 0:02dd72d1d465 252 * @return number of bytes read or -1 on failure
borlanic 0:02dd72d1d465 253 */
borlanic 0:02dd72d1d465 254 int read(char *data, int size);
borlanic 0:02dd72d1d465 255
borlanic 0:02dd72d1d465 256 /**
borlanic 0:02dd72d1d465 257 * Direct printf to underlying stream
borlanic 0:02dd72d1d465 258 * @see printf
borlanic 0:02dd72d1d465 259 *
borlanic 0:02dd72d1d465 260 * @param format format string to pass to printf
borlanic 0:02dd72d1d465 261 * @param ... arguments to printf
borlanic 0:02dd72d1d465 262 * @return number of bytes written or -1 on failure
borlanic 0:02dd72d1d465 263 */
borlanic 0:02dd72d1d465 264 int printf(const char *format, ...) MBED_PRINTF_METHOD(1,2);
borlanic 0:02dd72d1d465 265
borlanic 0:02dd72d1d465 266 int vprintf(const char *format, va_list args);
borlanic 0:02dd72d1d465 267
borlanic 0:02dd72d1d465 268 /**
borlanic 0:02dd72d1d465 269 * Direct scanf on underlying stream
borlanic 0:02dd72d1d465 270 * @see scanf
borlanic 0:02dd72d1d465 271 *
borlanic 0:02dd72d1d465 272 * @param format format string to pass to scanf
borlanic 0:02dd72d1d465 273 * @param ... arguments to scanf
borlanic 0:02dd72d1d465 274 * @return number of bytes read or -1 on failure
borlanic 0:02dd72d1d465 275 */
borlanic 0:02dd72d1d465 276 int scanf(const char *format, ...) MBED_SCANF_METHOD(1,2);
borlanic 0:02dd72d1d465 277
borlanic 0:02dd72d1d465 278 int vscanf(const char *format, va_list args);
borlanic 0:02dd72d1d465 279
borlanic 0:02dd72d1d465 280 /**
borlanic 0:02dd72d1d465 281 * Attach a callback for out-of-band data
borlanic 0:02dd72d1d465 282 *
borlanic 0:02dd72d1d465 283 * @param prefix string on when to initiate callback
borlanic 0:02dd72d1d465 284 * @param func callback to call when string is read
borlanic 0:02dd72d1d465 285 * @note out-of-band data is only processed during a scanf call
borlanic 0:02dd72d1d465 286 */
borlanic 0:02dd72d1d465 287 void oob(const char *prefix, mbed::Callback<void()> func);
borlanic 0:02dd72d1d465 288
borlanic 0:02dd72d1d465 289 /**
borlanic 0:02dd72d1d465 290 * Flushes the underlying stream
borlanic 0:02dd72d1d465 291 */
borlanic 0:02dd72d1d465 292 void flush();
borlanic 0:02dd72d1d465 293
borlanic 0:02dd72d1d465 294 /**
borlanic 0:02dd72d1d465 295 * Abort current recv
borlanic 0:02dd72d1d465 296 *
borlanic 0:02dd72d1d465 297 * Can be called from oob handler to interrupt the current
borlanic 0:02dd72d1d465 298 * recv operation.
borlanic 0:02dd72d1d465 299 */
borlanic 0:02dd72d1d465 300 void abort();
borlanic 0:02dd72d1d465 301
borlanic 0:02dd72d1d465 302 /**
borlanic 0:02dd72d1d465 303 * Process out-of-band data
borlanic 0:02dd72d1d465 304 *
borlanic 0:02dd72d1d465 305 * Process out-of-band data in the receive buffer. This function
borlanic 0:02dd72d1d465 306 * returns immediately if there is no data to process.
borlanic 0:02dd72d1d465 307 *
borlanic 0:02dd72d1d465 308 * @return true if oob data processed, false otherwise
borlanic 0:02dd72d1d465 309 */
borlanic 0:02dd72d1d465 310 bool process_oob(void);
borlanic 0:02dd72d1d465 311 };
borlanic 0:02dd72d1d465 312
borlanic 0:02dd72d1d465 313 /**@}*/
borlanic 0:02dd72d1d465 314
borlanic 0:02dd72d1d465 315 /**@}*/
borlanic 0:02dd72d1d465 316
borlanic 0:02dd72d1d465 317 } //namespace mbed
borlanic 0:02dd72d1d465 318
borlanic 0:02dd72d1d465 319 #endif //MBED_ATCMDPARSER_H