t

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
168:e84263d55307
Child:
169:9672193075cf
This updates the lib to the mbed lib v 145

Who changed what in which revision?

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