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@1:66a14afe650a, 2015-07-16 (annotated)
- Committer:
- geky
- Date:
- Thu Jul 16 20:42:44 2015 +0000
- Revision:
- 1:66a14afe650a
- Parent:
- 0:c741e144517c
- Child:
- 2:4d68f546861c
Worked around scanf's lack of error reporting; ; This required a rather roundabout method of performing two scanf passes, one with clobbered matches %n, and the other with the actual value matches.; ; Also the parser now scans multiple lines.
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 | 0:c741e144517c | 38 | // Helper methods for putc/getc with timeout |
| geky | 0:c741e144517c | 39 | int _putc(char c); |
| geky | 0:c741e144517c | 40 | int _getc(); |
| geky | 0:c741e144517c | 41 | |
| geky | 0:c741e144517c | 42 | // Flush used to clear serial connection |
| geky | 0:c741e144517c | 43 | void _flush(); |
| geky | 0:c741e144517c | 44 | |
| geky | 0:c741e144517c | 45 | // Helper methods for reading/writing lines with |
| geky | 0:c741e144517c | 46 | // timeout and buffer limitations |
| geky | 0:c741e144517c | 47 | bool _putline(const char *line); |
| geky | 1:66a14afe650a | 48 | bool _getline(char *line, int size); |
| geky | 0:c741e144517c | 49 | |
| geky | 0:c741e144517c | 50 | public: |
| geky | 0:c741e144517c | 51 | /** |
| geky | 0:c741e144517c | 52 | * Constructor |
| geky | 0:c741e144517c | 53 | * |
| geky | 0:c741e144517c | 54 | * @param serial serial interface to use for AT commands |
| geky | 1:66a14afe650a | 55 | * @param buffer_size size of internal buffer for transaction |
| geky | 0:c741e144517c | 56 | * @param timeout timeout of the connection |
| geky | 1:66a14afe650a | 57 | * @param echo flag to indicate if an echo of sent characters should be expected |
| geky | 0:c741e144517c | 58 | */ |
| geky | 1:66a14afe650a | 59 | ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000) : |
| geky | 0:c741e144517c | 60 | _serial(serial), |
| geky | 0:c741e144517c | 61 | _buffer_size(buffer_size), |
| geky | 0:c741e144517c | 62 | _timeout(timeout) { |
| geky | 0:c741e144517c | 63 | _buffer = new char[buffer_size]; |
| 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 | 0:c741e144517c | 81 | |
| geky | 0:c741e144517c | 82 | /** |
| geky | 0:c741e144517c | 83 | * Issue AT commands with specified command and expected response |
| geky | 1:66a14afe650a | 84 | * Uses printf/scanf like format strings to be able to parse the results |
| geky | 1:66a14afe650a | 85 | * |
| geky | 1:66a14afe650a | 86 | * Here are some examples: |
| geky | 1:66a14afe650a | 87 | * @code |
| geky | 1:66a14afe650a | 88 | * at.command("AT", "OK"); |
| geky | 1:66a14afe650a | 89 | * at.command("AT+CWMODE=%d", "OK", 3); |
| geky | 1:66a14afe650a | 90 | * at.command("AT+CWMODE?", "+CWMODE:%d OK", &result); |
| geky | 1:66a14afe650a | 91 | * @endcode |
| geky | 0:c741e144517c | 92 | * |
| geky | 0:c741e144517c | 93 | * @param command printf-like format string of command to send |
| geky | 0:c741e144517c | 94 | * @param response scanf-like format string of response to parse |
| geky | 0:c741e144517c | 95 | * @param ... all printf-like arguments to insert into command followed by |
| geky | 0:c741e144517c | 96 | * all scanf-like pointers to destinations for response values |
| geky | 1:66a14afe650a | 97 | * @return true only if response is successfully matched |
| geky | 0:c741e144517c | 98 | */ |
| geky | 0:c741e144517c | 99 | bool command(const char *command, const char *response, ...); |
| geky | 0:c741e144517c | 100 | }; |
| geky | 1:66a14afe650a | 101 |
