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
test_TCP_Socket_Echo.h
00001 /* Universal Socket Modem Interface Library 00002 * Copyright (c) 2013 Multi-Tech Systems 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef _TEST_TCP_SOCKET_ECHO_H_ 00018 #define _TEST_TCP_SOCKET_ECHO_H_ 00019 00020 // 0 for shield board with wifi 00021 // 1 for shield board with cellular 00022 #define CELL_SHIELD 0 00023 00024 /* test TCP socket communication 00025 * designed to talk to remote echo server 00026 * will talk to server until echo doesn't match sent data */ 00027 //Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo' 00028 00029 using namespace mts; 00030 00031 bool testTcpSocketEchoLoop(); 00032 00033 void testTcpSocketEcho() { 00034 Code code; 00035 const int TEST_PORT = 5798; 00036 const std::string TEST_SERVER( /* public IP of server running the netcat command given above */); 00037 00038 printf("TCP SOCKET TESTING\r\n"); 00039 #if CELL_SHIELD 00040 Transport::setTransport(Transport::CELLULAR); 00041 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); 00042 serial->baud(115200); 00043 Cellular::getInstance()->init(serial); 00044 00045 printf("Setting APN\r\n"); 00046 code = Cellular::getInstance()->setApn("wap.cingular"); 00047 if(code == SUCCESS) { 00048 printf("Success!\r\n"); 00049 } else { 00050 printf("Error during APN setup [%d]\r\n", (int)code); 00051 } 00052 #else 00053 for (int i = 6; i >= 0; i = i - 2) { 00054 wait(2); 00055 printf("Waiting %d seconds...\n\r", i); 00056 } 00057 Transport::setTransport(Transport::WIFI); 00058 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); 00059 serial->baud(9600); 00060 Wifi::getInstance()->init(serial); 00061 00062 code = Wifi::getInstance()->setNetwork("your wireless network" /* SSID of wireless */, Wifi::WPA2 /* security type of wireless */, "your wireless network password" /* password for wireless */); 00063 if(code == SUCCESS) { 00064 printf("Success!\r\n"); 00065 } else { 00066 printf("Error during network setup [%d]\r\n", (int)code); 00067 } 00068 code = Wifi::getInstance()->setDeviceIP(); 00069 if(code == SUCCESS) { 00070 printf("Success!\r\n"); 00071 } else { 00072 printf("Error during IP setup [%d]\r\n", (int)code); 00073 } 00074 #endif 00075 00076 printf("Establishing Connection\r\n"); 00077 #if CELL_SHIELD 00078 if(Cellular::getInstance()->connect()) { 00079 #else 00080 if(Wifi::getInstance()->connect()) { 00081 #endif 00082 printf("Success!\r\n"); 00083 } else { 00084 printf("Error during connection. Aborting.\r\n"); 00085 return; 00086 } 00087 00088 #if CELL_SHIELD 00089 if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) { 00090 #else 00091 if(Wifi::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) { 00092 #endif 00093 printf("Success!\r\n"); 00094 } else { 00095 printf("Error during TCP socket open [%s:%d]. Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT); 00096 return; 00097 } 00098 00099 int count = 0; 00100 while(testTcpSocketEchoLoop()) { 00101 count++; 00102 printf("Successful Echos: [%d]\r\n", count); 00103 } 00104 00105 printf("Closing socket\r\n"); 00106 #if CELL_SHIELD 00107 Cellular::getInstance()->close(); 00108 #else 00109 Wifi::getInstance()->close(); 00110 #endif 00111 00112 wait(10); 00113 00114 printf("Disconnecting\r\n"); 00115 #if CELL_SHIELD 00116 Cellular::getInstance()->disconnect(); 00117 #else 00118 Wifi::getInstance()->disconnect(); 00119 #endif 00120 } 00121 00122 bool testTcpSocketEchoLoop() { 00123 using namespace mts; 00124 const char buffer[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; 00125 00126 /*//Big Buffer 00127 const char buffer[] = "1ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00128 "2ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00129 "3ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00130 "4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00131 "5ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00132 "6ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00133 "7ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00134 "8ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00135 "9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*" 00136 "0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; 00137 */ 00138 00139 const int size = sizeof(buffer); 00140 char echoData[size]; 00141 00142 printf("Sending buffer\r\n"); 00143 #if CELL_SHIELD 00144 int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000); 00145 #else 00146 int bytesWritten = Wifi::getInstance()->write(buffer, size, 10000); 00147 #endif 00148 if(bytesWritten == size) { 00149 printf("Successfully sent buffer\r\n"); 00150 } else { 00151 printf("Failed to send buffer. Closing socket and aborting.\r\n"); 00152 #if CELL_SHIELD 00153 Cellular::getInstance()->close(); 00154 #else 00155 Wifi::getInstance()->close(); 00156 #endif 00157 return false; 00158 } 00159 00160 printf("Receiving echo (timeout = 15 seconds)\r\n"); 00161 Timer tmr; 00162 int bytesRead = 0; 00163 tmr.start(); 00164 do { 00165 #if CELL_SHIELD 00166 int status = Cellular::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000); 00167 #else 00168 int status = Wifi::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000); 00169 #endif 00170 if(status != -1) { 00171 bytesRead += status; 00172 } else { 00173 printf("Error reading from socket. Closing socket and aborting.\r\n"); 00174 #if CELL_SHIELD 00175 Cellular::getInstance()->close(); 00176 #else 00177 Wifi::getInstance()->close(); 00178 #endif 00179 return false; 00180 } 00181 printf("Total bytes read %d\r\n", bytesRead); 00182 } while (tmr.read_ms() <= 15000 && bytesRead < size); 00183 00184 00185 //Safely Cap at Max Size 00186 echoData[size - 1] = '\0'; 00187 printf("Comparing Buffers\r\n"); 00188 printf("SENT [%d]: [%s]\r\n", size, buffer); 00189 printf("RECV [%d]: [%s]\r\n", bytesRead, echoData); 00190 00191 for(int i = 0; i < size - 1; i++) { 00192 if(buffer[i] != echoData[i]) { 00193 printf("Buffers do not match at index %d\r\n", i); 00194 return false; 00195 } 00196 } 00197 return true; 00198 } 00199 00200 #endif
Generated on Tue Jul 12 2022 21:46:23 by 1.7.2