A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Revision:
96:27bdf4aa3a31
Parent:
71:82205735732b
Child:
115:b26176f23e89
--- a/tests/test_TCP_Socket.h	Fri Dec 27 23:55:18 2013 +0000
+++ b/tests/test_TCP_Socket.h	Mon Dec 30 18:55:48 2013 +0000
@@ -2,6 +2,30 @@
 #define _TEST_TCP_SOCKET_H_
 
 using namespace mts;
+const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|";
+const char PATTERN[] =  "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
+                        "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*";
+                        
+                        
+const char MENU[] =     "1       send ascii pattern until keypress\r\n"
+                        "2       send ascii pattern (numbered)\r\n"
+                        "3       send pattern and close socket\r\n"
+                        "4       send [ETX] and wait for keypress\r\n"
+                        "5       send [DLE] and wait for keypress\r\n"
+                        "6       send all hex values (00-FF)\r\n"
+                        "q       quit\r\n"
+                        ">:\r\n";
+                        
+const char WELCOME[] =  "Connected to: TCP test server";
+
+bool testTcpSocketIteration();
 
 void testTcpSocket() {
     Code code;
@@ -39,54 +63,15 @@
         printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT);
     }
     
-    printf("Receiving Data (timeout = 15 seconds)\r\n");
-    Timer tmr;
-    int bytesRead = 0;
-    const int size = 1024;
-    char data[size] = { 0 };
-    tmr.start();
-    do {
-        int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
-        if(status != -1) {
-            bytesRead += status;
-        } else {
-            printf("Error reading from socket\r\n");
-            data[bytesRead] = '\0';
-            break;
-        }
-        printf("Total bytes read %d\r\n", bytesRead);
-    } while (tmr.read_ms() <= 15000 && bytesRead < size);
-   
-    printf("READ: [%d] [%s]\r\n", bytesRead, data);
+    //Find Welcome Message and Menu
     
-        
-    printf("Waiting 10 seconds\r\n");
-    wait(10);
-    
-    printf("Writing to socket\r\n");
-    sprintf(data, "3\r\n");
-    int bytesWritten = Cellular::getInstance()->write(data, 4, 10000);
-    if(bytesWritten == 4) {
-        printf("Successfully wrote 'q'\r\n");
-    } else {
-        printf("Failed to write 'q'\r\n");   
+    int count = 0;
+    while(testTcpSocketIteration()) {
+        count++;
+        printf("Successful Iterations: %d\r\n", count);
     }
     
-    bytesRead = 0;
-    tmr.start();
-    do {
-        int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
-        if(status != -1) {
-            bytesRead += status;
-        } else {
-            printf("Error reading from socket\r\n");
-            data[bytesRead] = '\0';
-            break;
-        }
-        printf("Total bytes read %d\r\n", bytesRead);
-    } while (tmr.read_ms() <= 15000 && bytesRead < size);
-    printf("READ: [%d] [%s]\r\n", bytesRead, data);
-   
+    
     printf("Closing socket\r\n");
     Cellular::getInstance()->close();
     
@@ -96,4 +81,102 @@
     Cellular::getInstance()->disconnect();   
 }
 
+bool testTcpSocketIteration() {
+    
+    Timer tmr;
+    int bytesRead = 0;
+    const int bufferSize = 1024;
+    char buffer[bufferSize] = { 0 };
+    std::string result;
+    
+    printf("Receiving Data\r\n");
+    tmr.start();
+    do {
+        int size = Cellular::getInstance()->read(buffer, bufferSize, 1000);
+        if(size != -1) {
+            result.append(buffer, size);
+        } else {
+            printf("Error reading from socket\r\n");
+            return false;
+        }
+        printf("Total bytes read %d\r\n", result.size());
+    } while (tmr.read() <= 15 && bytesRead < bufferSize);
+   
+    printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str());
+    
+    size_t pos = result.find(PATTERN_LINE1);
+    if(pos != std::string::npos) {
+        //compare buffers
+        int patternSize = sizeof(PATTERN) - 1;
+        const char* ptr = &result.data()[pos];
+        bool match = true;
+        for(int i = 0; i < patternSize; i++) {
+            if(PATTERN[i] != ptr[i]) {
+                printf("1ST PATTERN DOESN'T MATCH AT [%d]\r\n", i);
+                printf("PATTERN [%02X]  BUFFER [%02X]\r\n", PATTERN[i], ptr[i]);
+                match = false;
+                break;   
+            }
+        }
+        if(match) {
+            printf("FOUND 1ST PATTERN\r\n");   
+        }
+        
+        pos = result.find(PATTERN_LINE1, pos + patternSize);
+        if(pos != std::string::npos) {
+            //compare buffers
+            ptr = &result.data()[pos];
+            match = true;
+            for(int i = 0; i < patternSize; i++) {
+                if(PATTERN[i] != ptr[i]) {
+                    printf("2ND PATTERN DOESN'T MATCH AT [%d]\r\n", i);
+                    printf("PATTERN [%02X]  BUFFER [%02X]\r\n", PATTERN[i], ptr[i]);
+                    match = false;
+                    break;   
+                }
+            }
+            if(match) {
+                printf("FOUND 2ND PATTERN\r\n");   
+            }
+        }
+    }
+    
+    
+
+    result.clear();
+    
+    printf("Writing to socket: 2\r\n");
+    if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) {
+        printf("Successfully wrote '2'\r\n");
+    } else {
+        printf("Failed to write '2'\r\n");   
+        return false;
+    }
+    printf("Expecting 'how many ? >:\r\n");
+    bytesRead = Cellular::getInstance()->read(buffer, bufferSize, 10000);
+    if(bytesRead != -1) {
+        result.append(buffer, bytesRead);
+        printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str());
+        if(result.find("how many") != std::string::npos) {
+            printf("Successfully found 'how many'\r\n");   
+            printf("Writing to socket: 2\r\n");
+            if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) {
+                printf("Successfully wrote '2'\r\n");
+            } else {
+                printf("Failed to write '2'\r\n");   
+                return false;
+            }
+        } else {
+            printf("Missing second option to menu item 2\r\n");
+        }
+    } else {
+        printf("Error reading from socket\r\n");
+        return false;
+    }
+    
+    
+    return true;
+}
+
+
 #endif