Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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