Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Committer:
geky
Date:
Wed Jul 15 22:39:25 2015 +0000
Revision:
0:c741e144517c
Child:
1:66a14afe650a
Initial Commit

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 0:c741e144517c 22
geky 0:c741e144517c 23
geky 0:c741e144517c 24 /**
geky 0:c741e144517c 25 * The ATParser class wraps information about the serial in use
geky 0:c741e144517c 26 */
geky 0:c741e144517c 27 class ATParser {
geky 0:c741e144517c 28 private:
geky 0:c741e144517c 29 // Serial information
geky 0:c741e144517c 30 RawSerial *_serial;
geky 0:c741e144517c 31 int _buffer_size;
geky 0:c741e144517c 32 char *_buffer;
geky 0:c741e144517c 33 int _timeout;
geky 0:c741e144517c 34
geky 0:c741e144517c 35 // Helper methods for putc/getc with timeout
geky 0:c741e144517c 36 int _putc(char c);
geky 0:c741e144517c 37 int _getc();
geky 0:c741e144517c 38
geky 0:c741e144517c 39 // Flush used to clear serial connection
geky 0:c741e144517c 40 void _flush();
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 0:c741e144517c 45 bool _getline(int size, char *line);
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 0:c741e144517c 52 * @param timeout timeout of the connection
geky 0:c741e144517c 53 */
geky 0:c741e144517c 54 ATParser(RawSerial *serial, int buffer_size = 256, int timeout = 3000) :
geky 0:c741e144517c 55 _serial(serial),
geky 0:c741e144517c 56 _buffer_size(buffer_size),
geky 0:c741e144517c 57 _timeout(timeout) {
geky 0:c741e144517c 58 _buffer = new char[buffer_size];
geky 0:c741e144517c 59 }
geky 0:c741e144517c 60
geky 0:c741e144517c 61 /**
geky 0:c741e144517c 62 * Destructor
geky 0:c741e144517c 63 */
geky 0:c741e144517c 64 ~ATParser() {
geky 0:c741e144517c 65 delete [] _buffer;
geky 0:c741e144517c 66 }
geky 0:c741e144517c 67
geky 0:c741e144517c 68 /**
geky 0:c741e144517c 69 * Allows timeout to be changed between commands
geky 0:c741e144517c 70 *
geky 0:c741e144517c 71 * @param timeout timeout of the connection
geky 0:c741e144517c 72 */
geky 0:c741e144517c 73 void setTimeout(int timeout) {
geky 0:c741e144517c 74 _timeout = timeout;
geky 0:c741e144517c 75 }
geky 0:c741e144517c 76
geky 0:c741e144517c 77 /**
geky 0:c741e144517c 78 * Issue AT commands with specified command and expected response
geky 0:c741e144517c 79 *
geky 0:c741e144517c 80 * @param command printf-like format string of command to send
geky 0:c741e144517c 81 * @param response scanf-like format string of response to parse
geky 0:c741e144517c 82 * @param ... all printf-like arguments to insert into command followed by
geky 0:c741e144517c 83 * all scanf-like pointers to destinations for response values
geky 0:c741e144517c 84 * @return true if response is successfully matched
geky 0:c741e144517c 85 */
geky 0:c741e144517c 86 bool command(const char *command, const char *response, ...);
geky 0:c741e144517c 87 };
geky 0:c741e144517c 88