Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Dependents:   ESP8266 xdot-passthru Lab_10 Lab9 ... more

Fork of ATParser by NetworkSocketAPI

Committer:
geky
Date:
Fri Jul 17 16:38:44 2015 +0000
Revision:
4:38acbd6f9d9e
Parent:
3:32915b9467d2
Child:
5:26bc9255b751
Exposed the underlying putc/getc methods for collecting raw data

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 1:66a14afe650a 22 #include <stdarg.h>
geky 1:66a14afe650a 23
geky 1:66a14afe650a 24 #include "BufferedSerial.h"
geky 0:c741e144517c 25
geky 0:c741e144517c 26
geky 0:c741e144517c 27 /**
geky 0:c741e144517c 28 * The ATParser class wraps information about the serial in use
geky 0:c741e144517c 29 */
geky 0:c741e144517c 30 class ATParser {
geky 0:c741e144517c 31 private:
geky 0:c741e144517c 32 // Serial information
geky 1:66a14afe650a 33 BufferedSerial *_serial;
geky 0:c741e144517c 34 int _buffer_size;
geky 0:c741e144517c 35 char *_buffer;
geky 0:c741e144517c 36 int _timeout;
geky 0:c741e144517c 37
geky 2:4d68f546861c 38 // Parsing information
geky 2:4d68f546861c 39 const char *_delimiter;
geky 4:38acbd6f9d9e 40 int _delim_size;
geky 0:c741e144517c 41
geky 0:c741e144517c 42 // Helper methods for reading/writing lines with
geky 0:c741e144517c 43 // timeout and buffer limitations
geky 0:c741e144517c 44 bool _putline(const char *line);
geky 1:66a14afe650a 45 bool _getline(char *line, int size);
geky 0:c741e144517c 46
geky 0:c741e144517c 47 public:
geky 0:c741e144517c 48 /**
geky 0:c741e144517c 49 * Constructor
geky 0:c741e144517c 50 *
geky 0:c741e144517c 51 * @param serial serial interface to use for AT commands
geky 1:66a14afe650a 52 * @param buffer_size size of internal buffer for transaction
geky 0:c741e144517c 53 * @param timeout timeout of the connection
geky 1:66a14afe650a 54 * @param echo flag to indicate if an echo of sent characters should be expected
geky 2:4d68f546861c 55 * @param delimiter string of characters to use as line delimiters
geky 0:c741e144517c 56 */
geky 2:4d68f546861c 57 ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000,
geky 2:4d68f546861c 58 const char *delimiter = "\r\n") :
geky 0:c741e144517c 59 _serial(serial),
geky 3:32915b9467d2 60 _buffer_size(buffer_size) {
geky 0:c741e144517c 61 _buffer = new char[buffer_size];
geky 3:32915b9467d2 62 setTimeout(timeout);
geky 3:32915b9467d2 63 setDelimiter(delimiter);
geky 0:c741e144517c 64 }
geky 0:c741e144517c 65
geky 0:c741e144517c 66 /**
geky 0:c741e144517c 67 * Destructor
geky 0:c741e144517c 68 */
geky 0:c741e144517c 69 ~ATParser() {
geky 0:c741e144517c 70 delete [] _buffer;
geky 0:c741e144517c 71 }
geky 0:c741e144517c 72
geky 0:c741e144517c 73 /**
geky 0:c741e144517c 74 * Allows timeout to be changed between commands
geky 0:c741e144517c 75 *
geky 0:c741e144517c 76 * @param timeout timeout of the connection
geky 0:c741e144517c 77 */
geky 0:c741e144517c 78 void setTimeout(int timeout) {
geky 0:c741e144517c 79 _timeout = timeout;
geky 0:c741e144517c 80 }
geky 2:4d68f546861c 81
geky 2:4d68f546861c 82 /**
geky 2:4d68f546861c 83 * Sets string of characters to use as line delimiters
geky 2:4d68f546861c 84 *
geky 2:4d68f546861c 85 * @param delimiter string of characters to use as line delimiters
geky 2:4d68f546861c 86 */
geky 2:4d68f546861c 87 void setDelimiter(const char *delimiter) {
geky 2:4d68f546861c 88 _delimiter = delimiter;
geky 3:32915b9467d2 89 _delim_size = strlen(delimiter);
geky 2:4d68f546861c 90 }
geky 0:c741e144517c 91
geky 0:c741e144517c 92 /**
geky 2:4d68f546861c 93 * Issue AT commands
geky 2:4d68f546861c 94 *
geky 2:4d68f546861c 95 * Sends formatted command and waits for formatted response.
geky 2:4d68f546861c 96 * Any recieved data that does not match the specified response
geky 2:4d68f546861c 97 * is ignored until the timeout has passed.
geky 2:4d68f546861c 98 *
geky 2:4d68f546861c 99 * Both the command and response use format strings that are internally
geky 2:4d68f546861c 100 * passed to printf and scanf respectively.
geky 2:4d68f546861c 101 * @see printf
geky 2:4d68f546861c 102 * @see scanf
geky 2:4d68f546861c 103 *
geky 2:4d68f546861c 104 * Commands are expected to be formatted with specified delimiters.
geky 2:4d68f546861c 105 * Sent commands are appended with the delimiter, and responses are
geky 2:4d68f546861c 106 * seperated by delimiters before attempting to parse.
geky 1:66a14afe650a 107 *
geky 1:66a14afe650a 108 * Here are some examples:
geky 1:66a14afe650a 109 * @code
geky 1:66a14afe650a 110 * at.command("AT", "OK");
geky 1:66a14afe650a 111 * at.command("AT+CWMODE=%d", "OK", 3);
geky 2:4d68f546861c 112 * at.command("AT+CWMODE?", "+CWMODE:%d\r\nOK", &result);
geky 1:66a14afe650a 113 * @endcode
geky 0:c741e144517c 114 *
geky 0:c741e144517c 115 * @param command printf-like format string of command to send
geky 0:c741e144517c 116 * @param response scanf-like format string of response to parse
geky 0:c741e144517c 117 * @param ... all printf-like arguments to insert into command followed by
geky 0:c741e144517c 118 * all scanf-like pointers to destinations for response values
geky 1:66a14afe650a 119 * @return true only if response is successfully matched
geky 0:c741e144517c 120 */
geky 0:c741e144517c 121 bool command(const char *command, const char *response, ...);
geky 4:38acbd6f9d9e 122
geky 4:38acbd6f9d9e 123 /**
geky 4:38acbd6f9d9e 124 * Write a single byte to the underlying stream
geky 4:38acbd6f9d9e 125 *
geky 4:38acbd6f9d9e 126 * @param c The byte to write
geky 4:38acbd6f9d9e 127 * @return The byte that was written or -1 during a timeout
geky 4:38acbd6f9d9e 128 */
geky 4:38acbd6f9d9e 129 int putc(char c);
geky 4:38acbd6f9d9e 130
geky 4:38acbd6f9d9e 131 /**
geky 4:38acbd6f9d9e 132 * Get a single byte from the underlying stream
geky 4:38acbd6f9d9e 133 *
geky 4:38acbd6f9d9e 134 * @return The byte that was read or -1 during a timeout
geky 4:38acbd6f9d9e 135 */
geky 4:38acbd6f9d9e 136 int getc();
geky 4:38acbd6f9d9e 137
geky 4:38acbd6f9d9e 138 /**
geky 4:38acbd6f9d9e 139 * Flushes the underlying stream
geky 4:38acbd6f9d9e 140 */
geky 4:38acbd6f9d9e 141 void flush();
geky 0:c741e144517c 142 };
geky 1:66a14afe650a 143