Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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