Multi-Hackers / SocketModem

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Revision:
124:6d964b4343c8
Parent:
91:9439ad14d7f0
Child:
141:571e0ef6c8dc
--- a/tests/test_TCP_Socket_Echo.h	Tue Dec 31 23:41:59 2013 +0000
+++ b/tests/test_TCP_Socket_Echo.h	Thu Jan 02 18:11:25 2014 +0000
@@ -1,59 +1,83 @@
 #ifndef _TEST_TCP_SOCKET_ECHO_H_
 #define _TEST_TCP_SOCKET_ECHO_H_
 
+// 0 for shield board with wifi
+// 1 for shield board with cellular
+#define CELL_SHIELD 0
 
+/* test TCP socket communication
+ * designed to talk to remote echo server
+ * will talk to server until echo doesn't match sent data */
 //Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo'
+
+using namespace mts;
+
 bool testTcpSocketEchoLoop();
 
 void testTcpSocketEcho() {
-    using namespace mts;
-    
     Code code;
     const int TEST_PORT = 5798;
-    const std::string TEST_SERVER("204.26.122.96");
+    const std::string TEST_SERVER( /* public IP of server running the netcat command given above */);
     
     printf("TCP SOCKET TESTING\r\n");
+#if CELL_SHIELD
+    for (int i = 30; i >= 0; i = i - 2) {
+        wait(2);
+        printf("Waiting %d seconds...\n\r", i);
+    }  
+    Transport::setTransport(Transport::CELLULAR);
+    MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
+    serial->baud(115200);
+    Cellular::getInstance()->init(serial);
+    
     printf("Setting APN\r\n");
-    code = Cellular::getInstance()->setApn("b2b.tmobile.com");
+    code = Cellular::getInstance()->setApn("wap.cingular");
     if(code == SUCCESS) {
         printf("Success!\r\n");
     } else {
         printf("Error during APN setup [%d]\r\n", (int)code);
     }
+#else
+    for (int i = 6; i >= 0; i = i - 2) {
+        wait(2);
+        printf("Waiting %d seconds...\n\r", i);
+    }  
+    Transport::setTransport(Transport::WIFI);
+    MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
+    serial->baud(9600);
+    Wifi::getInstance()->init(serial);
     
-    printf("Setting Socket Closeable\r\n");
-    code = Cellular::getInstance()->setSocketCloseable();
+    code = Wifi::getInstance()->setNetwork("your wireless network" /* SSID of wireless */, Wifi::WPA2 /* security type of wireless */, "your wireless network password" /* password for wireless */);
+    if(code == SUCCESS) {
+        printf("Success!\r\n");
+    } else {
+        printf("Error during network setup [%d]\r\n", (int)code);
+    }
+    code = Wifi::getInstance()->setDeviceIP();
     if(code == SUCCESS) {
         printf("Success!\r\n");
     } else {
-        printf("Error setting socket closeable [%d]\r\n", (int)code);
+        printf("Error during IP setup [%d]\r\n", (int)code);
     }
+#endif
     
-    printf("Setting Local Port\r\n");
-    if(Cellular::getInstance()->bind(5000)) {
+    printf("Establishing Connection\r\n");
+#if CELL_SHIELD
+    if(Cellular::getInstance()->connect()) {
+#else
+    if(Wifi::getInstance()->connect()) {
+#endif
         printf("Success!\r\n");
     } else {
-        printf("Error setting local port [%d]\r\n", (int)code);
-    }
-    
-    printf("Setting Primary DNS\r\n");
-    code = Cellular::getInstance()->setDns("8.8.8.8");
-    if(code == SUCCESS) {
-        printf("Success!\r\n");
-    } else {
-        printf("Error setting primary DNS [%d]\r\n", (int)code);
-    }
-    
-    printf("Establishing PPP Connection\r\n");
-    if(Cellular::getInstance()->connect()) {
-        printf("Success!\r\n");
-    } else {
-        printf("Error during PPP connection.  Aborting.\r\n");
+        printf("Error during connection.  Aborting.\r\n");
         return;
     }
        
-    printf("Opening a TCP Socket\r\n");
+#if CELL_SHIELD
     if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
+#else
+    if(Wifi::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
+#endif
         printf("Success!\r\n");   
     } else {
         printf("Error during TCP socket open [%s:%d].  Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT);
@@ -67,11 +91,20 @@
     }
         
     printf("Closing socket\r\n");
+#if CELL_SHIELD
     Cellular::getInstance()->close();
-    
+#else
+    Wifi::getInstance()->close();
+#endif
     
-    //printf("Disconnecting\r\n");
-//    Cellular::getInstance()->disconnect();   
+    wait(10);
+    
+    printf("Disconnecting\r\n");
+#if CELL_SHIELD
+    Cellular::getInstance()->disconnect();   
+#else
+    Wifi::getInstance()->disconnect();
+#endif  
 }
 
 bool testTcpSocketEchoLoop() {
@@ -95,12 +128,20 @@
     char echoData[size];
     
     printf("Sending buffer\r\n");
+#if CELL_SHIELD
     int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000);
+#else
+    int bytesWritten = Wifi::getInstance()->write(buffer, size, 10000);
+#endif
     if(bytesWritten == size) {
         printf("Successfully sent buffer\r\n");
     } else {
         printf("Failed to send buffer.  Closing socket and aborting.\r\n");
+#if CELL_SHIELD
         Cellular::getInstance()->close();
+#else
+        Wifi::getInstance()->close();
+#endif
         return false;
     }
     
@@ -109,12 +150,20 @@
     int bytesRead = 0;
     tmr.start();
     do {
+#if CELL_SHIELD
         int status = Cellular::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000);
+#else
+        int status = Wifi::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000);
+#endif
         if(status != -1) {
             bytesRead += status;
         } else {
             printf("Error reading from socket.  Closing socket and aborting.\r\n");
+#if CELL_SHIELD
             Cellular::getInstance()->close();
+#else
+            Wifi::getInstance()->close();
+#endif
             return false;
         }
         printf("Total bytes read %d\r\n", bytesRead);