Parser for AT commands and similar protocols

Dependencies:   BufferedSerial

Revision:
6:51f1171b5ebc
Parent:
5:26bc9255b751
Child:
7:d1b193880af1
diff -r 26bc9255b751 -r 51f1171b5ebc ATParser.cpp
--- a/ATParser.cpp	Fri Jul 17 17:23:57 2015 +0000
+++ b/ATParser.cpp	Fri Jul 17 21:00:23 2015 +0000
@@ -56,6 +56,33 @@
         _serial->getc();
 }
 
+// read/write handling with timeouts
+int ATParser::write(const char *data, int size) {
+    int i;
+    
+    for (i = 0; i < size; i++) {
+        if (putc(data[i]) < 0)
+            return i;
+    }
+    
+    return i;
+}
+
+int ATParser::read(char *data, int size) {
+    int i;
+    
+    for (i = 0; i < size; i++) {
+        int c = getc();
+        
+        if (c < 0)
+            return i;
+            
+        data[i] = c;
+    }
+    
+    return i;
+}
+
 
 // getline/putline handling with timeouts/bounds checking
 bool ATParser::_putline(const char *line) {    
@@ -165,11 +192,8 @@
             // We only succeed if all characters in the response is matched
             if (count >= 0 && (_buffer+offset)[count] == 0) {
                 // Reuse the front end of the buffer
-                int j;
-                for (j = 0; j < i; j++) {
-                    _buffer[j] = response[j];
-                }
-                _buffer[j] = 0;
+                memcpy(_buffer, response, i);
+                _buffer[i] = 0;
                 
                 // Store the found results
                 vsscanf(_buffer+offset, _buffer, args);