The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Anna Bridge
Date:
Fri Jun 22 15:38:59 2018 +0100
Revision:
169:a7c7b631e539
Parent:
165:d1b4690b3f8b
Child:
170:e95d10626187
mbed library. Release version 162

Who changed what in which revision?

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