Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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