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.
Fork of ATParser by
ATParser.h@2:4d68f546861c, 2015-07-16 (annotated)
- Committer:
- geky
- Date:
- Thu Jul 16 22:50:43 2015 +0000
- Revision:
- 2:4d68f546861c
- Parent:
- 1:66a14afe650a
- Child:
- 3:32915b9467d2
Fixed issue with uninitialized variable after failed scanf
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 | 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 | 2:4d68f546861c | 40 | |
| geky | 0:c741e144517c | 41 | // Helper methods for putc/getc with timeout |
| geky | 0:c741e144517c | 42 | int _putc(char c); |
| geky | 0:c741e144517c | 43 | int _getc(); |
| geky | 0:c741e144517c | 44 | |
| geky | 0:c741e144517c | 45 | // Flush used to clear serial connection |
| geky | 0:c741e144517c | 46 | void _flush(); |
| geky | 0:c741e144517c | 47 | |
| geky | 0:c741e144517c | 48 | // Helper methods for reading/writing lines with |
| geky | 0:c741e144517c | 49 | // timeout and buffer limitations |
| geky | 0:c741e144517c | 50 | bool _putline(const char *line); |
| geky | 1:66a14afe650a | 51 | bool _getline(char *line, int size); |
| geky | 0:c741e144517c | 52 | |
| geky | 0:c741e144517c | 53 | public: |
| geky | 0:c741e144517c | 54 | /** |
| geky | 0:c741e144517c | 55 | * Constructor |
| geky | 0:c741e144517c | 56 | * |
| geky | 0:c741e144517c | 57 | * @param serial serial interface to use for AT commands |
| geky | 1:66a14afe650a | 58 | * @param buffer_size size of internal buffer for transaction |
| geky | 0:c741e144517c | 59 | * @param timeout timeout of the connection |
| geky | 1:66a14afe650a | 60 | * @param echo flag to indicate if an echo of sent characters should be expected |
| geky | 2:4d68f546861c | 61 | * @param delimiter string of characters to use as line delimiters |
| geky | 0:c741e144517c | 62 | */ |
| geky | 2:4d68f546861c | 63 | ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000, |
| geky | 2:4d68f546861c | 64 | const char *delimiter = "\r\n") : |
| geky | 0:c741e144517c | 65 | _serial(serial), |
| geky | 0:c741e144517c | 66 | _buffer_size(buffer_size), |
| geky | 2:4d68f546861c | 67 | _timeout(timeout), |
| geky | 2:4d68f546861c | 68 | _delimiter(delimiter) { |
| geky | 0:c741e144517c | 69 | _buffer = new char[buffer_size]; |
| geky | 0:c741e144517c | 70 | } |
| geky | 0:c741e144517c | 71 | |
| geky | 0:c741e144517c | 72 | /** |
| geky | 0:c741e144517c | 73 | * Destructor |
| geky | 0:c741e144517c | 74 | */ |
| geky | 0:c741e144517c | 75 | ~ATParser() { |
| geky | 0:c741e144517c | 76 | delete [] _buffer; |
| geky | 0:c741e144517c | 77 | } |
| geky | 0:c741e144517c | 78 | |
| geky | 0:c741e144517c | 79 | /** |
| geky | 0:c741e144517c | 80 | * Allows timeout to be changed between commands |
| geky | 0:c741e144517c | 81 | * |
| geky | 0:c741e144517c | 82 | * @param timeout timeout of the connection |
| geky | 0:c741e144517c | 83 | */ |
| geky | 0:c741e144517c | 84 | void setTimeout(int timeout) { |
| geky | 0:c741e144517c | 85 | _timeout = timeout; |
| geky | 0:c741e144517c | 86 | } |
| geky | 2:4d68f546861c | 87 | |
| geky | 2:4d68f546861c | 88 | /** |
| geky | 2:4d68f546861c | 89 | * Sets string of characters to use as line delimiters |
| geky | 2:4d68f546861c | 90 | * |
| geky | 2:4d68f546861c | 91 | * @param delimiter string of characters to use as line delimiters |
| geky | 2:4d68f546861c | 92 | */ |
| geky | 2:4d68f546861c | 93 | void setDelimiter(const char *delimiter) { |
| geky | 2:4d68f546861c | 94 | _delimiter = delimiter; |
| geky | 2:4d68f546861c | 95 | } |
| geky | 0:c741e144517c | 96 | |
| geky | 0:c741e144517c | 97 | /** |
| geky | 2:4d68f546861c | 98 | * Issue AT commands |
| geky | 2:4d68f546861c | 99 | * |
| geky | 2:4d68f546861c | 100 | * Sends formatted command and waits for formatted response. |
| geky | 2:4d68f546861c | 101 | * Any recieved data that does not match the specified response |
| geky | 2:4d68f546861c | 102 | * is ignored until the timeout has passed. |
| geky | 2:4d68f546861c | 103 | * |
| geky | 2:4d68f546861c | 104 | * Both the command and response use format strings that are internally |
| geky | 2:4d68f546861c | 105 | * passed to printf and scanf respectively. |
| geky | 2:4d68f546861c | 106 | * @see printf |
| geky | 2:4d68f546861c | 107 | * @see scanf |
| geky | 2:4d68f546861c | 108 | * |
| geky | 2:4d68f546861c | 109 | * Commands are expected to be formatted with specified delimiters. |
| geky | 2:4d68f546861c | 110 | * Sent commands are appended with the delimiter, and responses are |
| geky | 2:4d68f546861c | 111 | * seperated by delimiters before attempting to parse. |
| geky | 1:66a14afe650a | 112 | * |
| geky | 1:66a14afe650a | 113 | * Here are some examples: |
| geky | 1:66a14afe650a | 114 | * @code |
| geky | 1:66a14afe650a | 115 | * at.command("AT", "OK"); |
| geky | 1:66a14afe650a | 116 | * at.command("AT+CWMODE=%d", "OK", 3); |
| geky | 2:4d68f546861c | 117 | * at.command("AT+CWMODE?", "+CWMODE:%d\r\nOK", &result); |
| geky | 1:66a14afe650a | 118 | * @endcode |
| geky | 0:c741e144517c | 119 | * |
| geky | 0:c741e144517c | 120 | * @param command printf-like format string of command to send |
| geky | 0:c741e144517c | 121 | * @param response scanf-like format string of response to parse |
| geky | 0:c741e144517c | 122 | * @param ... all printf-like arguments to insert into command followed by |
| geky | 0:c741e144517c | 123 | * all scanf-like pointers to destinations for response values |
| geky | 1:66a14afe650a | 124 | * @return true only if response is successfully matched |
| geky | 0:c741e144517c | 125 | */ |
| geky | 0:c741e144517c | 126 | bool command(const char *command, const char *response, ...); |
| geky | 0:c741e144517c | 127 | }; |
| geky | 1:66a14afe650a | 128 |
