Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Revision:
10:553f9ffaf657
Parent:
9:9bcb87c27208
Child:
11:fd406d4c4227
--- a/ATParser.h	Mon Jul 20 21:28:39 2015 +0000
+++ b/ATParser.h	Sun Jul 26 21:53:28 2015 +0000
@@ -17,13 +17,12 @@
  * Parser for the AT command syntax
  *
  */
- 
+
 #include "mbed.h"
 #include <cstdarg>
-
 #include "BufferedSerial.h"
 
- 
+
 /**
 * Parser class for parsing AT commands
 *
@@ -41,17 +40,19 @@
 * at.recv("OK");
 * @endcode
 */
-class ATParser {
+class ATParser
+{
 private:
     // Serial information
     BufferedSerial *_serial;
     int _buffer_size;
     char *_buffer;
     int _timeout;
-    
+
     // Parsing information
     const char *_delimiter;
     int _delim_size;
+    uint8_t at_echo;
 
 public:
     /**
@@ -62,22 +63,22 @@
     * @param timeout timeout of the connection
     * @param delimiter string of characters to use as line delimiters
     */
-    ATParser(BufferedSerial *serial, const char *delimiter = "\r\n", 
-             int buffer_size = 256, int timeout = 8000) :
-            _serial(serial),
-            _buffer_size(buffer_size) {
+    ATParser(BufferedSerial &serial, const char *delimiter = "\r\n", int buffer_size = 256, int timeout = 8000, uint8_t echo = 0) :
+        _serial(&serial),
+        _buffer_size(buffer_size) {
         _buffer = new char[buffer_size];
         setTimeout(timeout);
         setDelimiter(delimiter);
+        setEcho(echo);
     }
-    
+
     /**
     * Destructor
     */
     ~ATParser() {
         delete [] _buffer;
     }
-    
+
     /**
     * Allows timeout to be changed between commands
     *
@@ -86,7 +87,7 @@
     void setTimeout(int timeout) {
         _timeout = timeout;
     }
-    
+
     /**
     * Sets string of characters to use as line delimiters
     *
@@ -98,19 +99,28 @@
     }
     
     /**
+    * Allows echo to be on or off
+    *
+    * @param echo 1 for echo and 0 turns it off
+    */
+    void setEcho(uint8_t echo) {
+        at_echo = (echo) ? 1 : 0;
+    }
+
+    /**
     * Sends an AT command
     *
     * Sends a formatted command using printf style formatting
     * @see ::printf
     *
-    * @param command printf-like format string of command to send which 
+    * @param command printf-like format string of command to send which
     *                is appended with the specified delimiter
     * @param ... all printf-like arguments to insert into command
     * @return true only if command is successfully sent
     */
     bool send(const char *command, ...);
     bool vsend(const char *command, va_list args);
-    
+
     /**
     * Recieve an AT response
     *
@@ -127,23 +137,23 @@
     */
     bool recv(const char *response, ...);
     bool vrecv(const char *response, va_list args);
-    
-    /** 
+
+    /**
     * Write a single byte to the underlying stream
     *
     * @param c The byte to write
     * @return The byte that was written or -1 during a timeout
     */
     int putc(char c);
-    
-    /** 
+
+    /**
     * Get a single byte from the underlying stream
     *
     * @return The byte that was read or -1 during a timeout
     */
     int getc();
-    
-    /** 
+
+    /**
     * Write an array of bytes to the underlying stream
     *
     * @param data the array of bytes to write
@@ -151,8 +161,8 @@
     * @return number of bytes written or -1 on failure
     */
     int write(const char *data, int size);
-    
-    /** 
+
+    /**
     * Read an array of bytes from the underlying stream
     *
     * @param data the destination for the read bytes
@@ -160,7 +170,7 @@
     * @return number of bytes read or -1 on failure
     */
     int read(char *data, int size);
-    
+
     /**
     * Direct printf to underlying stream
     * @see ::printf
@@ -171,7 +181,7 @@
     */
     int printf(const char *format, ...);
     int vprintf(const char *format, va_list args);
-    
+
     /**
     * Direct scanf on underlying stream
     * @see ::scanf
@@ -182,7 +192,7 @@
     */
     int scanf(const char *format, ...);
     int vscanf(const char *format, va_list args);
-    
+
     /**
     * Flushes the underlying stream
     */