a

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
hydroguy45
Date:
Fri Nov 01 18:52:45 2019 +0000
Revision:
12:ea030e3181d3
Parent:
7:ca226305d28b
Cleaned up the header

Who changed what in which revision?

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