Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

ATParser.h

Committer:
geky
Date:
2015-07-16
Revision:
2:4d68f546861c
Parent:
1:66a14afe650a
Child:
3:32915b9467d2

File content as of revision 2:4d68f546861c:

/* Copyright (c) 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @section DESCRIPTION
 *
 * Parser for the AT command syntax
 *
 */
 
#include "mbed.h"
#include <stdarg.h>

#include "BufferedSerial.h"

 
/**
* The ATParser class wraps information about the serial in use
*/
class ATParser {
private:
    // Serial information
    BufferedSerial *_serial;
    int _buffer_size;
    char *_buffer;
    int _timeout;
    
    // Parsing information
    const char *_delimiter;
    
    // Helper methods for putc/getc with timeout
    int _putc(char c);
    int _getc();
    
    // Flush used to clear serial connection
    void _flush();
    
    // Helper methods for reading/writing lines with 
    // timeout and buffer limitations
    bool _putline(const char *line);
    bool _getline(char *line, int size);

public:
    /**
    * Constructor
    *
    * @param serial serial interface to use for AT commands
    * @param buffer_size size of internal buffer for transaction
    * @param timeout timeout of the connection
    * @param echo flag to indicate if an echo of sent characters should be expected
    * @param delimiter string of characters to use as line delimiters
    */
    ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000,
             const char *delimiter = "\r\n") :
            _serial(serial),
            _buffer_size(buffer_size),
            _timeout(timeout),
            _delimiter(delimiter) {
        _buffer = new char[buffer_size];
    }
    
    /**
    * Destructor
    */
    ~ATParser() {
        delete [] _buffer;
    }
    
    /**
    * Allows timeout to be changed between commands
    *
    * @param timeout timeout of the connection
    */
    void setTimeout(int timeout) {
        _timeout = timeout;
    }
    
    /**
    * Sets string of characters to use as line delimiters
    *
    * @param delimiter string of characters to use as line delimiters
    */
    void setDelimiter(const char *delimiter) {
        _delimiter = delimiter;
    }
            
    /**
    * Issue AT commands
    *
    * Sends formatted command and waits for formatted response.
    * Any recieved data that does not match the specified response 
    * is ignored until the timeout has passed.
    *
    * Both the command and response use format strings that are internally
    * passed to printf and scanf respectively.
    * @see printf
    * @see scanf
    *
    * Commands are expected to be formatted with specified delimiters.
    * Sent commands are appended with the delimiter, and responses are
    * seperated by delimiters before attempting to parse.
    *
    * Here are some examples:
    * @code
    * at.command("AT", "OK");
    * at.command("AT+CWMODE=%d", "OK", 3);
    * at.command("AT+CWMODE?", "+CWMODE:%d\r\nOK", &result);
    * @endcode
    *
    * @param command printf-like format string of command to send
    * @param response scanf-like format string of response to parse
    * @param ... all printf-like arguments to insert into command followed by 
    *            all scanf-like pointers to destinations for response values
    * @return true only if response is successfully matched
    */
    bool command(const char *command, const char *response, ...);
};