ATParser for X-NUCLEO-IDW01M1 module

Dependencies:   BufferedSerial

Dependents:   SPWF01SA-lapi-1 SPWF01SA Nucleo-AWS-IoT-mbed

Fork of ATParser by ST Expansion SW Team

Committer:
geky
Date:
Mon Jul 20 21:28:39 2015 +0000
Revision:
9:9bcb87c27208
Parent:
8:91515b168c70
Child:
10:553f9ffaf657
Added direct printf/scanf methods as alternatives to the newline-based send/recv methods.

Who changed what in which revision?

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