Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSerial
Dependents: SPWF01SA-lapi-1 SPWF01SA Nucleo-AWS-IoT-mbed
Fork of ATParser by
ATParser.h@6:51f1171b5ebc, 2015-07-17 (annotated)
- Committer:
- geky
- Date:
- Fri Jul 17 21:00:23 2015 +0000
- Revision:
- 6:51f1171b5ebc
- Parent:
- 5:26bc9255b751
- Child:
- 7:d1b193880af1
Exposed raw read/write methods
Who changed what in which revision?
| User | Revision | Line number | New 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 | 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 | 2:4d68f546861c | 54 | * @param delimiter string of characters to use as line delimiters |
| geky | 0:c741e144517c | 55 | */ |
| geky | 5:26bc9255b751 | 56 | ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 8000, |
| geky | 2:4d68f546861c | 57 | const char *delimiter = "\r\n") : |
| geky | 0:c741e144517c | 58 | _serial(serial), |
| geky | 3:32915b9467d2 | 59 | _buffer_size(buffer_size) { |
| geky | 0:c741e144517c | 60 | _buffer = new char[buffer_size]; |
| geky | 3:32915b9467d2 | 61 | setTimeout(timeout); |
| geky | 3:32915b9467d2 | 62 | setDelimiter(delimiter); |
| geky | 0:c741e144517c | 63 | } |
| geky | 0:c741e144517c | 64 | |
| geky | 0:c741e144517c | 65 | /** |
| geky | 0:c741e144517c | 66 | * Destructor |
| geky | 0:c741e144517c | 67 | */ |
| geky | 0:c741e144517c | 68 | ~ATParser() { |
| geky | 0:c741e144517c | 69 | delete [] _buffer; |
| geky | 0:c741e144517c | 70 | } |
| geky | 0:c741e144517c | 71 | |
| geky | 0:c741e144517c | 72 | /** |
| geky | 0:c741e144517c | 73 | * Allows timeout to be changed between commands |
| geky | 0:c741e144517c | 74 | * |
| geky | 0:c741e144517c | 75 | * @param timeout timeout of the connection |
| geky | 0:c741e144517c | 76 | */ |
| geky | 0:c741e144517c | 77 | void setTimeout(int timeout) { |
| geky | 0:c741e144517c | 78 | _timeout = timeout; |
| geky | 0:c741e144517c | 79 | } |
| geky | 2:4d68f546861c | 80 | |
| geky | 2:4d68f546861c | 81 | /** |
| geky | 2:4d68f546861c | 82 | * Sets string of characters to use as line delimiters |
| geky | 2:4d68f546861c | 83 | * |
| geky | 2:4d68f546861c | 84 | * @param delimiter string of characters to use as line delimiters |
| geky | 2:4d68f546861c | 85 | */ |
| geky | 2:4d68f546861c | 86 | void setDelimiter(const char *delimiter) { |
| geky | 2:4d68f546861c | 87 | _delimiter = delimiter; |
| geky | 3:32915b9467d2 | 88 | _delim_size = strlen(delimiter); |
| geky | 2:4d68f546861c | 89 | } |
| geky | 5:26bc9255b751 | 90 | |
| geky | 5:26bc9255b751 | 91 | /** |
| geky | 5:26bc9255b751 | 92 | * Sends an AT command |
| geky | 5:26bc9255b751 | 93 | * |
| geky | 5:26bc9255b751 | 94 | * Sends a formatted command using printf style formatting |
| geky | 5:26bc9255b751 | 95 | * @see printf |
| geky | 5:26bc9255b751 | 96 | * |
| geky | 5:26bc9255b751 | 97 | * @param command printf-like format string of command to send which |
| geky | 5:26bc9255b751 | 98 | * is appended with the specified delimiter |
| geky | 5:26bc9255b751 | 99 | * @param ... all printf-like arguments to insert into command |
| geky | 5:26bc9255b751 | 100 | * @return true only if command is successfully sent |
| geky | 5:26bc9255b751 | 101 | */ |
| geky | 5:26bc9255b751 | 102 | bool send(const char *command, ...); |
| geky | 5:26bc9255b751 | 103 | bool vsend(const char *command, va_list args); |
| geky | 5:26bc9255b751 | 104 | |
| geky | 5:26bc9255b751 | 105 | /** |
| geky | 5:26bc9255b751 | 106 | * Recieve an AT response |
| geky | 5:26bc9255b751 | 107 | * |
| geky | 5:26bc9255b751 | 108 | * Recieves a formatted response using scanf style formatting |
| geky | 5:26bc9255b751 | 109 | * @see scanf |
| geky | 5:26bc9255b751 | 110 | * |
| geky | 5:26bc9255b751 | 111 | * Responses are parsed line at a time using the specified delimiter. |
| geky | 5:26bc9255b751 | 112 | * Any recieved data that does not match the response is ignored until |
| geky | 5:26bc9255b751 | 113 | * a timeout occurs. |
| geky | 5:26bc9255b751 | 114 | * |
| geky | 5:26bc9255b751 | 115 | * @param response scanf-like format string of response to expect |
| geky | 5:26bc9255b751 | 116 | * @param ... all scanf-like arguments to extract from response |
| geky | 5:26bc9255b751 | 117 | * @return true only if response is successfully matched |
| geky | 5:26bc9255b751 | 118 | */ |
| geky | 5:26bc9255b751 | 119 | bool recv(const char *response, ...); |
| geky | 5:26bc9255b751 | 120 | bool vrecv(const char *response, va_list args); |
| geky | 0:c741e144517c | 121 | |
| geky | 0:c741e144517c | 122 | /** |
| geky | 2:4d68f546861c | 123 | * Issue AT commands |
| geky | 2:4d68f546861c | 124 | * |
| geky | 5:26bc9255b751 | 125 | * Issues formatted commands and parses formatted responses. |
| geky | 5:26bc9255b751 | 126 | * A command call is identical to a send call followed by a recv call. |
| geky | 5:26bc9255b751 | 127 | * @see send, recv |
| geky | 1:66a14afe650a | 128 | * |
| geky | 1:66a14afe650a | 129 | * Here are some examples: |
| geky | 1:66a14afe650a | 130 | * @code |
| geky | 1:66a14afe650a | 131 | * at.command("AT", "OK"); |
| geky | 1:66a14afe650a | 132 | * at.command("AT+CWMODE=%d", "OK", 3); |
| geky | 2:4d68f546861c | 133 | * at.command("AT+CWMODE?", "+CWMODE:%d\r\nOK", &result); |
| geky | 1:66a14afe650a | 134 | * @endcode |
| geky | 0:c741e144517c | 135 | * |
| geky | 0:c741e144517c | 136 | * @param command printf-like format string of command to send |
| geky | 5:26bc9255b751 | 137 | * @param response scanf-like format string of response to expect |
| geky | 0:c741e144517c | 138 | * @param ... all printf-like arguments to insert into command followed by |
| geky | 5:26bc9255b751 | 139 | * all scanf-like arguments to extract from response |
| geky | 1:66a14afe650a | 140 | * @return true only if response is successfully matched |
| geky | 0:c741e144517c | 141 | */ |
| geky | 0:c741e144517c | 142 | bool command(const char *command, const char *response, ...); |
| geky | 5:26bc9255b751 | 143 | bool vcommand(const char *command, const char *response, va_list args); |
| geky | 4:38acbd6f9d9e | 144 | |
| geky | 4:38acbd6f9d9e | 145 | /** |
| geky | 5:26bc9255b751 | 146 | * Write a single byte to the underlying stream |
| geky | 5:26bc9255b751 | 147 | * |
| geky | 5:26bc9255b751 | 148 | * @param c The byte to write |
| geky | 5:26bc9255b751 | 149 | * @return The byte that was written or -1 during a timeout |
| geky | 5:26bc9255b751 | 150 | */ |
| geky | 4:38acbd6f9d9e | 151 | int putc(char c); |
| geky | 4:38acbd6f9d9e | 152 | |
| geky | 4:38acbd6f9d9e | 153 | /** |
| geky | 5:26bc9255b751 | 154 | * Get a single byte from the underlying stream |
| geky | 5:26bc9255b751 | 155 | * |
| geky | 5:26bc9255b751 | 156 | * @return The byte that was read or -1 during a timeout |
| geky | 5:26bc9255b751 | 157 | */ |
| geky | 4:38acbd6f9d9e | 158 | int getc(); |
| geky | 4:38acbd6f9d9e | 159 | |
| geky | 6:51f1171b5ebc | 160 | /** |
| geky | 6:51f1171b5ebc | 161 | * Write an array of bytes to the underlying stream |
| geky | 6:51f1171b5ebc | 162 | * |
| geky | 6:51f1171b5ebc | 163 | * @param data the array of bytes to write |
| geky | 6:51f1171b5ebc | 164 | * @param size number of bytes to write |
| geky | 6:51f1171b5ebc | 165 | * @return number of bytes written |
| geky | 6:51f1171b5ebc | 166 | */ |
| geky | 6:51f1171b5ebc | 167 | int write(const char *data, int size); |
| geky | 6:51f1171b5ebc | 168 | |
| geky | 6:51f1171b5ebc | 169 | /** |
| geky | 6:51f1171b5ebc | 170 | * Read an array of bytes from the underlying stream |
| geky | 6:51f1171b5ebc | 171 | * |
| geky | 6:51f1171b5ebc | 172 | * @param data the destination for the read bytes |
| geky | 6:51f1171b5ebc | 173 | * @param size number of bytes to read |
| geky | 6:51f1171b5ebc | 174 | * @return number of bytes read |
| geky | 6:51f1171b5ebc | 175 | */ |
| geky | 6:51f1171b5ebc | 176 | int read(char *data, int size); |
| geky | 6:51f1171b5ebc | 177 | |
| geky | 4:38acbd6f9d9e | 178 | /** |
| geky | 5:26bc9255b751 | 179 | * Flushes the underlying stream |
| geky | 5:26bc9255b751 | 180 | */ |
| geky | 4:38acbd6f9d9e | 181 | void flush(); |
| geky | 0:c741e144517c | 182 | }; |
| geky | 1:66a14afe650a | 183 |
