my customized lib

Committer:
DuyLionTran
Date:
Sun Nov 26 15:08:14 2017 +0000
Revision:
0:8094b249013c
Initial commit

Who changed what in which revision?

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