mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Oct 25 14:53:38 2017 +0100
Revision:
176:447f873cad2f
Parent:
170:19eb464bc2be
Child:
178:79309dc6340a
This updates the lib to the mbed lib v 154

Who changed what in which revision?

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