Fix for UIP read of DLE escape character.

Dependents:   mtsas

Fork of MTS-Cellular by MultiTech

Files at this revision

API Documentation at this revision

Comitter:
jfanucch
Date:
Tue Sep 15 14:46:25 2015 +0000
Parent:
78:fc9d2b983744
Commit message:
Fixes for UIP read of DLE escaped characters in incoming packets.

Changed in this revision

Cellular/UIP.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Cellular/UIP.cpp	Mon Aug 17 21:05:41 2015 +0000
+++ b/Cellular/UIP.cpp	Tue Sep 15 14:46:25 2015 +0000
@@ -301,7 +301,7 @@
         logError("Timed out attempting to close socket");
         return false;
     }
-
+    
     if (shutdown) {
         int counter = 0;
         char tmp[256];
@@ -342,34 +342,26 @@
     }
 
     if(bytesRead > 0 && socketCloseable) {
-        //Remove escape characters
-        int index = 0;
-        bool escapeFlag = false;
-        for(int i = 0; i < bytesRead; i++) {
-            if(data[i] == DLE || data[i] == ETX) {
-                if(escapeFlag == true) {
-                    //This character has been escaped
-                    escapeFlag = false;
-                } else if(data[bytesRead] == DLE) {
-                    //Found escape character
-                    escapeFlag = true;
-                    continue;
-                } else {
-                    //ETX sent without escape -> Socket closed
-                    logInfo("Read ETX character without DLE escape. Socket closed");
-                    socketOpened = false;
-                    continue;
-                }
+        int index, i;
+        bool inEsc = false;
+        for (i=0, index=0; i < bytesRead; i++, index++) {
+            if (data[i] == DLE && (data[i+1] == ETX || data[i+1] == DLE)) {
+                // This is an escape sequence.
+                logInfo("Found DLE sequence. Escaping the character: %02x.", (int)data[i+1]);
+                index--;
+                inEsc = true;
+                continue;
+            } else if (data[i] == DLE && !inEsc) {
+                logError("Found DLE sequence. Socket is closing.");
+                socketOpened = false;
             }
-
-            if(index != i) {
-                data[index] = data[i];
-            }
-            index++;
+            data[index] = data[i];
+            inEsc = false;
         }
         bytesRead = index;
     }
 
+
     //Scan for socket closed message
     for(size_t i = 0; i < bytesRead; i++) {
         if(data[i] == 'O') {