Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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