FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 /* Copyright (c) 2015 ARM Limited
ram54288 0:dbad57390bd1 2 *
ram54288 0:dbad57390bd1 3 * Licensed under the Apache License, Version 2.0 (the "License");
ram54288 0:dbad57390bd1 4 * you may not use this file except in compliance with the License.
ram54288 0:dbad57390bd1 5 * You may obtain a copy of the License at
ram54288 0:dbad57390bd1 6 *
ram54288 0:dbad57390bd1 7 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:dbad57390bd1 8 *
ram54288 0:dbad57390bd1 9 * Unless required by applicable law or agreed to in writing, software
ram54288 0:dbad57390bd1 10 * distributed under the License is distributed on an "AS IS" BASIS,
ram54288 0:dbad57390bd1 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:dbad57390bd1 12 * See the License for the specific language governing permissions and
ram54288 0:dbad57390bd1 13 * limitations under the License.
ram54288 0:dbad57390bd1 14 *
ram54288 0:dbad57390bd1 15 * @section DESCRIPTION
ram54288 0:dbad57390bd1 16 *
ram54288 0:dbad57390bd1 17 * Parser for the AT command syntax
ram54288 0:dbad57390bd1 18 *
ram54288 0:dbad57390bd1 19 */
ram54288 0:dbad57390bd1 20 #ifndef AT_PARSER_H
ram54288 0:dbad57390bd1 21 #define AT_PARSER_H
ram54288 0:dbad57390bd1 22
ram54288 0:dbad57390bd1 23 #include "mbed.h"
ram54288 0:dbad57390bd1 24 #include <cstdarg>
ram54288 0:dbad57390bd1 25 #include <vector>
ram54288 0:dbad57390bd1 26 #include "BufferedSerial.h"
ram54288 0:dbad57390bd1 27 #include "Callback.h"
ram54288 0:dbad57390bd1 28
ram54288 0:dbad57390bd1 29
ram54288 0:dbad57390bd1 30 /**
ram54288 0:dbad57390bd1 31 * Parser class for parsing AT commands
ram54288 0:dbad57390bd1 32 *
ram54288 0:dbad57390bd1 33 * Here are some examples:
ram54288 0:dbad57390bd1 34 * @code
ram54288 0:dbad57390bd1 35 * ATParser at = ATParser(serial, "\r\n");
ram54288 0:dbad57390bd1 36 * int value;
ram54288 0:dbad57390bd1 37 * char buffer[100];
ram54288 0:dbad57390bd1 38 *
ram54288 0:dbad57390bd1 39 * at.send("AT") && at.recv("OK");
ram54288 0:dbad57390bd1 40 * at.send("AT+CWMODE=%d", 3) && at.recv("OK");
ram54288 0:dbad57390bd1 41 * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value);
ram54288 0:dbad57390bd1 42 * at.recv("+IPD,%d:", &value);
ram54288 0:dbad57390bd1 43 * at.read(buffer, value);
ram54288 0:dbad57390bd1 44 * at.recv("OK");
ram54288 0:dbad57390bd1 45 * @endcode
ram54288 0:dbad57390bd1 46 */
ram54288 0:dbad57390bd1 47 class ATParser
ram54288 0:dbad57390bd1 48 {
ram54288 0:dbad57390bd1 49 private:
ram54288 0:dbad57390bd1 50 // Serial information
ram54288 0:dbad57390bd1 51 BufferedSerial *_serial;
ram54288 0:dbad57390bd1 52 int _buffer_size;
ram54288 0:dbad57390bd1 53 char *_buffer;
ram54288 0:dbad57390bd1 54 int _timeout;
ram54288 0:dbad57390bd1 55
ram54288 0:dbad57390bd1 56 // Parsing information
ram54288 0:dbad57390bd1 57 const char *_delimiter;
ram54288 0:dbad57390bd1 58 int _delim_size;
ram54288 0:dbad57390bd1 59 bool dbg_on;
ram54288 0:dbad57390bd1 60
ram54288 0:dbad57390bd1 61 struct oob {
ram54288 0:dbad57390bd1 62 unsigned len;
ram54288 0:dbad57390bd1 63 const char *prefix;
ram54288 0:dbad57390bd1 64 mbed::Callback<void()> cb;
ram54288 0:dbad57390bd1 65 };
ram54288 0:dbad57390bd1 66 std::vector<oob> _oobs;
ram54288 0:dbad57390bd1 67
ram54288 0:dbad57390bd1 68 public:
ram54288 0:dbad57390bd1 69 /**
ram54288 0:dbad57390bd1 70 * Constructor
ram54288 0:dbad57390bd1 71 *
ram54288 0:dbad57390bd1 72 * @param serial serial interface to use for AT commands
ram54288 0:dbad57390bd1 73 * @param buffer_size size of internal buffer for transaction
ram54288 0:dbad57390bd1 74 * @param timeout timeout of the connection
ram54288 0:dbad57390bd1 75 * @param delimiter string of characters to use as line delimiters
ram54288 0:dbad57390bd1 76 */
ram54288 0:dbad57390bd1 77 ATParser(BufferedSerial &serial, const char *delimiter = "\r\n", int buffer_size = 256, int timeout = 8000, bool debug = false) :
ram54288 0:dbad57390bd1 78 _serial(&serial),
ram54288 0:dbad57390bd1 79 _buffer_size(buffer_size) {
ram54288 0:dbad57390bd1 80 _buffer = new char[buffer_size];
ram54288 0:dbad57390bd1 81 setTimeout(timeout);
ram54288 0:dbad57390bd1 82 setDelimiter(delimiter);
ram54288 0:dbad57390bd1 83 debugOn(debug);
ram54288 0:dbad57390bd1 84 }
ram54288 0:dbad57390bd1 85
ram54288 0:dbad57390bd1 86 /**
ram54288 0:dbad57390bd1 87 * Destructor
ram54288 0:dbad57390bd1 88 */
ram54288 0:dbad57390bd1 89 ~ATParser() {
ram54288 0:dbad57390bd1 90 delete [] _buffer;
ram54288 0:dbad57390bd1 91 }
ram54288 0:dbad57390bd1 92
ram54288 0:dbad57390bd1 93 /**
ram54288 0:dbad57390bd1 94 * Allows timeout to be changed between commands
ram54288 0:dbad57390bd1 95 *
ram54288 0:dbad57390bd1 96 * @param timeout timeout of the connection
ram54288 0:dbad57390bd1 97 */
ram54288 0:dbad57390bd1 98 void setTimeout(int timeout) {
ram54288 0:dbad57390bd1 99 _timeout = timeout;
ram54288 0:dbad57390bd1 100 }
ram54288 0:dbad57390bd1 101
ram54288 0:dbad57390bd1 102 /**
ram54288 0:dbad57390bd1 103 * Sets string of characters to use as line delimiters
ram54288 0:dbad57390bd1 104 *
ram54288 0:dbad57390bd1 105 * @param delimiter string of characters to use as line delimiters
ram54288 0:dbad57390bd1 106 */
ram54288 0:dbad57390bd1 107 void setDelimiter(const char *delimiter) {
ram54288 0:dbad57390bd1 108 _delimiter = delimiter;
ram54288 0:dbad57390bd1 109 _delim_size = strlen(delimiter);
ram54288 0:dbad57390bd1 110 }
ram54288 0:dbad57390bd1 111
ram54288 0:dbad57390bd1 112 /**
ram54288 0:dbad57390bd1 113 * Allows echo to be on or off
ram54288 0:dbad57390bd1 114 *
ram54288 0:dbad57390bd1 115 * @param echo 1 for echo and 0 turns it off
ram54288 0:dbad57390bd1 116 */
ram54288 0:dbad57390bd1 117 void debugOn(uint8_t on) {
ram54288 0:dbad57390bd1 118 dbg_on = (on) ? 1 : 0;
ram54288 0:dbad57390bd1 119 }
ram54288 0:dbad57390bd1 120
ram54288 0:dbad57390bd1 121 /**
ram54288 0:dbad57390bd1 122 * Sends an AT command
ram54288 0:dbad57390bd1 123 *
ram54288 0:dbad57390bd1 124 * Sends a formatted command using printf style formatting
ram54288 0:dbad57390bd1 125 * @see ::printf
ram54288 0:dbad57390bd1 126 *
ram54288 0:dbad57390bd1 127 * @param command printf-like format string of command to send which
ram54288 0:dbad57390bd1 128 * is appended with the specified delimiter
ram54288 0:dbad57390bd1 129 * @param ... all printf-like arguments to insert into command
ram54288 0:dbad57390bd1 130 * @return true only if command is successfully sent
ram54288 0:dbad57390bd1 131 */
ram54288 0:dbad57390bd1 132 bool send(const char *command, ...);
ram54288 0:dbad57390bd1 133 bool vsend(const char *command, va_list args);
ram54288 0:dbad57390bd1 134
ram54288 0:dbad57390bd1 135 /**
ram54288 0:dbad57390bd1 136 * Recieve an AT response
ram54288 0:dbad57390bd1 137 *
ram54288 0:dbad57390bd1 138 * Recieves a formatted response using scanf style formatting
ram54288 0:dbad57390bd1 139 * @see ::scanf
ram54288 0:dbad57390bd1 140 *
ram54288 0:dbad57390bd1 141 * Responses are parsed line at a time using the specified delimiter.
ram54288 0:dbad57390bd1 142 * Any recieved data that does not match the response is ignored until
ram54288 0:dbad57390bd1 143 * a timeout occurs.
ram54288 0:dbad57390bd1 144 *
ram54288 0:dbad57390bd1 145 * @param response scanf-like format string of response to expect
ram54288 0:dbad57390bd1 146 * @param ... all scanf-like arguments to extract from response
ram54288 0:dbad57390bd1 147 * @return true only if response is successfully matched
ram54288 0:dbad57390bd1 148 */
ram54288 0:dbad57390bd1 149 bool recv(const char *response, ...);
ram54288 0:dbad57390bd1 150 bool vrecv(const char *response, va_list args);
ram54288 0:dbad57390bd1 151
ram54288 0:dbad57390bd1 152 /**
ram54288 0:dbad57390bd1 153 * Write a single byte to the underlying stream
ram54288 0:dbad57390bd1 154 *
ram54288 0:dbad57390bd1 155 * @param c The byte to write
ram54288 0:dbad57390bd1 156 * @return The byte that was written or -1 during a timeout
ram54288 0:dbad57390bd1 157 */
ram54288 0:dbad57390bd1 158 int putc(char c);
ram54288 0:dbad57390bd1 159
ram54288 0:dbad57390bd1 160 /**
ram54288 0:dbad57390bd1 161 * Get a single byte from the underlying stream
ram54288 0:dbad57390bd1 162 *
ram54288 0:dbad57390bd1 163 * @return The byte that was read or -1 during a timeout
ram54288 0:dbad57390bd1 164 */
ram54288 0:dbad57390bd1 165 int getc();
ram54288 0:dbad57390bd1 166
ram54288 0:dbad57390bd1 167 /**
ram54288 0:dbad57390bd1 168 * Write an array of bytes to the underlying stream
ram54288 0:dbad57390bd1 169 *
ram54288 0:dbad57390bd1 170 * @param data the array of bytes to write
ram54288 0:dbad57390bd1 171 * @param size number of bytes to write
ram54288 0:dbad57390bd1 172 * @return number of bytes written or -1 on failure
ram54288 0:dbad57390bd1 173 */
ram54288 0:dbad57390bd1 174 int write(const char *data, int size);
ram54288 0:dbad57390bd1 175
ram54288 0:dbad57390bd1 176 /**
ram54288 0:dbad57390bd1 177 * Read an array of bytes from the underlying stream
ram54288 0:dbad57390bd1 178 *
ram54288 0:dbad57390bd1 179 * @param data the destination for the read bytes
ram54288 0:dbad57390bd1 180 * @param size number of bytes to read
ram54288 0:dbad57390bd1 181 * @return number of bytes read or -1 on failure
ram54288 0:dbad57390bd1 182 */
ram54288 0:dbad57390bd1 183 int read(char *data, int size);
ram54288 0:dbad57390bd1 184
ram54288 0:dbad57390bd1 185 /**
ram54288 0:dbad57390bd1 186 * Direct printf to underlying stream
ram54288 0:dbad57390bd1 187 * @see ::printf
ram54288 0:dbad57390bd1 188 *
ram54288 0:dbad57390bd1 189 * @param format format string to pass to printf
ram54288 0:dbad57390bd1 190 * @param ... arguments to printf
ram54288 0:dbad57390bd1 191 * @return number of bytes written or -1 on failure
ram54288 0:dbad57390bd1 192 */
ram54288 0:dbad57390bd1 193 int printf(const char *format, ...);
ram54288 0:dbad57390bd1 194 int vprintf(const char *format, va_list args);
ram54288 0:dbad57390bd1 195
ram54288 0:dbad57390bd1 196 /**
ram54288 0:dbad57390bd1 197 * Direct scanf on underlying stream
ram54288 0:dbad57390bd1 198 * @see ::scanf
ram54288 0:dbad57390bd1 199 *
ram54288 0:dbad57390bd1 200 * @param format format string to pass to scanf
ram54288 0:dbad57390bd1 201 * @param ... arguments to scanf
ram54288 0:dbad57390bd1 202 * @return number of bytes read or -1 on failure
ram54288 0:dbad57390bd1 203 */
ram54288 0:dbad57390bd1 204 int scanf(const char *format, ...);
ram54288 0:dbad57390bd1 205 int vscanf(const char *format, va_list args);
ram54288 0:dbad57390bd1 206
ram54288 0:dbad57390bd1 207 /**
ram54288 0:dbad57390bd1 208 * Attach a callback for out-of-band data
ram54288 0:dbad57390bd1 209 *
ram54288 0:dbad57390bd1 210 * @param prefix string on when to initiate callback
ram54288 0:dbad57390bd1 211 * @param func callback to call when string is read
ram54288 0:dbad57390bd1 212 * @note out-of-band data is only processed during a scanf call
ram54288 0:dbad57390bd1 213 */
ram54288 0:dbad57390bd1 214 void oob(const char *prefix, mbed::Callback<void()> func);
ram54288 0:dbad57390bd1 215
ram54288 0:dbad57390bd1 216 /**
ram54288 0:dbad57390bd1 217 * Attach a callback for out-of-band data
ram54288 0:dbad57390bd1 218 *
ram54288 0:dbad57390bd1 219 * @param prefix string on when to initiate callback
ram54288 0:dbad57390bd1 220 * @param obj pointer to object to call member function on
ram54288 0:dbad57390bd1 221 * @param method callback to call when string is read
ram54288 0:dbad57390bd1 222 * @note out-of-band data is only processed during a scanf call
ram54288 0:dbad57390bd1 223 */
ram54288 0:dbad57390bd1 224 template <typename T, typename M>
ram54288 0:dbad57390bd1 225 void oob(const char *prefix, T *obj, M method) {
ram54288 0:dbad57390bd1 226 return oob(prefix, mbed::Callback<void()>(obj, method));
ram54288 0:dbad57390bd1 227 }
ram54288 0:dbad57390bd1 228
ram54288 0:dbad57390bd1 229 /**
ram54288 0:dbad57390bd1 230 * Flushes the underlying stream
ram54288 0:dbad57390bd1 231 */
ram54288 0:dbad57390bd1 232 void flush();
ram54288 0:dbad57390bd1 233 };
ram54288 0:dbad57390bd1 234 #endif