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

Committer:
mfiore
Date:
Thu Jan 02 18:11:25 2014 +0000
Revision:
124:6d964b4343c8
Parent:
115:b26176f23e89
Child:
141:571e0ef6c8dc
finish cleaning up test directory; socket, socket echo, and ping tests can be run on wifi or cellular by changing #define value in each test header; test_main.cpp has commented out main function with all tests ready to go

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgodinez 19:38794784e009 1 #ifndef _TEST_TCP_SOCKET_H_
sgodinez 19:38794784e009 2 #define _TEST_TCP_SOCKET_H_
sgodinez 19:38794784e009 3
mfiore 124:6d964b4343c8 4 // 0 for shield board with wifi
mfiore 124:6d964b4343c8 5 // 1 for shield board with cellular
mfiore 124:6d964b4343c8 6 #define CELL_SHIELD 0
mfiore 124:6d964b4343c8 7
mfiore 124:6d964b4343c8 8 /* test TCP socket communication
mfiore 124:6d964b4343c8 9 * will keep talking to server until data doesn't match expected */
mfiore 115:b26176f23e89 10
mfiore 39:6e94520a3217 11 using namespace mts;
sgodinez 96:27bdf4aa3a31 12 const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|";
sgodinez 96:27bdf4aa3a31 13 const char PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
sgodinez 96:27bdf4aa3a31 14 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
sgodinez 96:27bdf4aa3a31 15 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
sgodinez 96:27bdf4aa3a31 16 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
sgodinez 96:27bdf4aa3a31 17 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n"
sgodinez 96:27bdf4aa3a31 18 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/\r\n"
sgodinez 96:27bdf4aa3a31 19 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-\r\n"
sgodinez 96:27bdf4aa3a31 20 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n"
sgodinez 96:27bdf4aa3a31 21 "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*";
sgodinez 96:27bdf4aa3a31 22
sgodinez 96:27bdf4aa3a31 23 const char MENU[] = "1 send ascii pattern until keypress\r\n"
sgodinez 96:27bdf4aa3a31 24 "2 send ascii pattern (numbered)\r\n"
sgodinez 96:27bdf4aa3a31 25 "3 send pattern and close socket\r\n"
sgodinez 96:27bdf4aa3a31 26 "4 send [ETX] and wait for keypress\r\n"
sgodinez 96:27bdf4aa3a31 27 "5 send [DLE] and wait for keypress\r\n"
sgodinez 96:27bdf4aa3a31 28 "6 send all hex values (00-FF)\r\n"
sgodinez 96:27bdf4aa3a31 29 "q quit\r\n"
sgodinez 96:27bdf4aa3a31 30 ">:\r\n";
sgodinez 96:27bdf4aa3a31 31
sgodinez 96:27bdf4aa3a31 32 const char WELCOME[] = "Connected to: TCP test server";
sgodinez 96:27bdf4aa3a31 33
sgodinez 96:27bdf4aa3a31 34 bool testTcpSocketIteration();
mfiore 39:6e94520a3217 35
sgodinez 19:38794784e009 36 void testTcpSocket() {
sgodinez 71:82205735732b 37 Code code;
mfiore 124:6d964b4343c8 38 /* this test is set up to interact with a server listening at the following address and port */
sgodinez 19:38794784e009 39 const int TEST_PORT = 7000;
mfiore 124:6d964b4343c8 40 const std::string TEST_SERVER("204.26.122.5";
sgodinez 19:38794784e009 41
sgodinez 19:38794784e009 42 printf("TCP SOCKET TESTING\r\n");
mfiore 115:b26176f23e89 43 #if CELL_SHIELD
mfiore 124:6d964b4343c8 44 for (int i = 30; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 45 wait(2);
mfiore 124:6d964b4343c8 46 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 47 }
mfiore 115:b26176f23e89 48 Transport::setTransport(Transport::CELLULAR);
mfiore 115:b26176f23e89 49 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 115:b26176f23e89 50 serial->baud(115200);
mfiore 115:b26176f23e89 51 Cellular::getInstance()->init(serial);
mfiore 115:b26176f23e89 52
sgodinez 19:38794784e009 53 printf("Setting APN\r\n");
mfiore 115:b26176f23e89 54 code = Cellular::getInstance()->setApn("wap.cingular");
sgodinez 71:82205735732b 55 if(code == SUCCESS) {
sgodinez 19:38794784e009 56 printf("Success!\r\n");
sgodinez 19:38794784e009 57 } else {
sgodinez 19:38794784e009 58 printf("Error during APN setup [%d]\r\n", (int)code);
sgodinez 19:38794784e009 59 }
mfiore 115:b26176f23e89 60 #else
mfiore 124:6d964b4343c8 61 for (int i = 6; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 62 wait(2);
mfiore 124:6d964b4343c8 63 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 64 }
mfiore 115:b26176f23e89 65 Transport::setTransport(Transport::WIFI);
mfiore 115:b26176f23e89 66 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
mfiore 115:b26176f23e89 67 serial->baud(9600);
mfiore 115:b26176f23e89 68 Wifi::getInstance()->init(serial);
sgodinez 19:38794784e009 69
mfiore 124:6d964b4343c8 70 code = Wifi::getInstance()->setNetwork("your wireless network" /* SSID of wireless */, Wifi::WPA2 /* security type of wireless */, "your wireless network password" /* password for wireless */);
mfiore 115:b26176f23e89 71 if(code == SUCCESS) {
sgodinez 19:38794784e009 72 printf("Success!\r\n");
sgodinez 19:38794784e009 73 } else {
mfiore 115:b26176f23e89 74 printf("Error during network setup [%d]\r\n", (int)code);
mfiore 115:b26176f23e89 75 }
mfiore 115:b26176f23e89 76 code = Wifi::getInstance()->setDeviceIP();
mfiore 115:b26176f23e89 77 if(code == SUCCESS) {
mfiore 115:b26176f23e89 78 printf("Success!\r\n");
mfiore 115:b26176f23e89 79 } else {
mfiore 115:b26176f23e89 80 printf("Error during IP setup [%d]\r\n", (int)code);
mfiore 115:b26176f23e89 81 }
mfiore 115:b26176f23e89 82 #endif
mfiore 115:b26176f23e89 83
mfiore 115:b26176f23e89 84 printf("Establishing Connection\r\n");
mfiore 115:b26176f23e89 85 #if CELL_SHIELD
mfiore 115:b26176f23e89 86 if(Cellular::getInstance()->connect()) {
mfiore 115:b26176f23e89 87 #else
mfiore 115:b26176f23e89 88 if(Wifi::getInstance()->connect()) {
mfiore 115:b26176f23e89 89 #endif
mfiore 115:b26176f23e89 90 printf("Success!\r\n");
mfiore 115:b26176f23e89 91 } else {
mfiore 115:b26176f23e89 92 printf("Error during connection\r\n");
sgodinez 19:38794784e009 93 }
sgodinez 19:38794784e009 94
sgodinez 19:38794784e009 95 printf("Opening a TCP Socket\r\n");
mfiore 115:b26176f23e89 96 #if CELL_SHIELD
sgodinez 19:38794784e009 97 if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
mfiore 115:b26176f23e89 98 #else
mfiore 115:b26176f23e89 99 if(Wifi::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
mfiore 115:b26176f23e89 100 #endif
sgodinez 19:38794784e009 101 printf("Success!\r\n");
sgodinez 19:38794784e009 102 } else {
sgodinez 19:38794784e009 103 printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT);
sgodinez 19:38794784e009 104 }
sgodinez 19:38794784e009 105
sgodinez 96:27bdf4aa3a31 106 //Find Welcome Message and Menu
sgodinez 19:38794784e009 107
sgodinez 96:27bdf4aa3a31 108 int count = 0;
sgodinez 96:27bdf4aa3a31 109 while(testTcpSocketIteration()) {
sgodinez 96:27bdf4aa3a31 110 count++;
sgodinez 96:27bdf4aa3a31 111 printf("Successful Iterations: %d\r\n", count);
sgodinez 19:38794784e009 112 }
sgodinez 19:38794784e009 113
sgodinez 19:38794784e009 114 printf("Closing socket\r\n");
mfiore 115:b26176f23e89 115 #if CELL_SHIELD
sgodinez 19:38794784e009 116 Cellular::getInstance()->close();
mfiore 115:b26176f23e89 117 #else
mfiore 115:b26176f23e89 118 Wifi::getInstance()->close();
mfiore 115:b26176f23e89 119 #endif
sgodinez 19:38794784e009 120
sgodinez 58:408f67fa292f 121 wait(10);
sgodinez 19:38794784e009 122
sgodinez 19:38794784e009 123 printf("Disconnecting\r\n");
mfiore 115:b26176f23e89 124 #if CELL_SHIELD
sgodinez 19:38794784e009 125 Cellular::getInstance()->disconnect();
mfiore 115:b26176f23e89 126 #else
mfiore 115:b26176f23e89 127 Wifi::getInstance()->disconnect();
mfiore 115:b26176f23e89 128 #endif
sgodinez 19:38794784e009 129 }
sgodinez 19:38794784e009 130
sgodinez 96:27bdf4aa3a31 131 bool testTcpSocketIteration() {
sgodinez 96:27bdf4aa3a31 132 Timer tmr;
sgodinez 96:27bdf4aa3a31 133 int bytesRead = 0;
sgodinez 96:27bdf4aa3a31 134 const int bufferSize = 1024;
sgodinez 96:27bdf4aa3a31 135 char buffer[bufferSize] = { 0 };
sgodinez 96:27bdf4aa3a31 136 std::string result;
sgodinez 96:27bdf4aa3a31 137
sgodinez 96:27bdf4aa3a31 138 printf("Receiving Data\r\n");
sgodinez 96:27bdf4aa3a31 139 tmr.start();
sgodinez 96:27bdf4aa3a31 140 do {
mfiore 115:b26176f23e89 141 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 142 int size = Cellular::getInstance()->read(buffer, bufferSize, 1000);
mfiore 115:b26176f23e89 143 #else
mfiore 115:b26176f23e89 144 int size = Wifi::getInstance()->read(buffer, bufferSize, 1000);
mfiore 115:b26176f23e89 145 #endif
sgodinez 96:27bdf4aa3a31 146 if(size != -1) {
sgodinez 96:27bdf4aa3a31 147 result.append(buffer, size);
sgodinez 96:27bdf4aa3a31 148 } else {
sgodinez 96:27bdf4aa3a31 149 printf("Error reading from socket\r\n");
sgodinez 96:27bdf4aa3a31 150 return false;
sgodinez 96:27bdf4aa3a31 151 }
sgodinez 96:27bdf4aa3a31 152 printf("Total bytes read %d\r\n", result.size());
sgodinez 96:27bdf4aa3a31 153 } while (tmr.read() <= 15 && bytesRead < bufferSize);
sgodinez 96:27bdf4aa3a31 154
sgodinez 96:27bdf4aa3a31 155 printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str());
sgodinez 96:27bdf4aa3a31 156
sgodinez 96:27bdf4aa3a31 157 size_t pos = result.find(PATTERN_LINE1);
sgodinez 96:27bdf4aa3a31 158 if(pos != std::string::npos) {
sgodinez 96:27bdf4aa3a31 159 //compare buffers
sgodinez 96:27bdf4aa3a31 160 int patternSize = sizeof(PATTERN) - 1;
sgodinez 96:27bdf4aa3a31 161 const char* ptr = &result.data()[pos];
sgodinez 96:27bdf4aa3a31 162 bool match = true;
sgodinez 96:27bdf4aa3a31 163 for(int i = 0; i < patternSize; i++) {
sgodinez 96:27bdf4aa3a31 164 if(PATTERN[i] != ptr[i]) {
sgodinez 96:27bdf4aa3a31 165 printf("1ST PATTERN DOESN'T MATCH AT [%d]\r\n", i);
sgodinez 96:27bdf4aa3a31 166 printf("PATTERN [%02X] BUFFER [%02X]\r\n", PATTERN[i], ptr[i]);
sgodinez 96:27bdf4aa3a31 167 match = false;
sgodinez 96:27bdf4aa3a31 168 break;
sgodinez 96:27bdf4aa3a31 169 }
sgodinez 96:27bdf4aa3a31 170 }
sgodinez 96:27bdf4aa3a31 171 if(match) {
sgodinez 96:27bdf4aa3a31 172 printf("FOUND 1ST PATTERN\r\n");
sgodinez 96:27bdf4aa3a31 173 }
sgodinez 96:27bdf4aa3a31 174
sgodinez 96:27bdf4aa3a31 175 pos = result.find(PATTERN_LINE1, pos + patternSize);
sgodinez 96:27bdf4aa3a31 176 if(pos != std::string::npos) {
sgodinez 96:27bdf4aa3a31 177 //compare buffers
sgodinez 96:27bdf4aa3a31 178 ptr = &result.data()[pos];
sgodinez 96:27bdf4aa3a31 179 match = true;
sgodinez 96:27bdf4aa3a31 180 for(int i = 0; i < patternSize; i++) {
sgodinez 96:27bdf4aa3a31 181 if(PATTERN[i] != ptr[i]) {
sgodinez 96:27bdf4aa3a31 182 printf("2ND PATTERN DOESN'T MATCH AT [%d]\r\n", i);
sgodinez 96:27bdf4aa3a31 183 printf("PATTERN [%02X] BUFFER [%02X]\r\n", PATTERN[i], ptr[i]);
sgodinez 96:27bdf4aa3a31 184 match = false;
sgodinez 96:27bdf4aa3a31 185 break;
sgodinez 96:27bdf4aa3a31 186 }
sgodinez 96:27bdf4aa3a31 187 }
sgodinez 96:27bdf4aa3a31 188 if(match) {
sgodinez 96:27bdf4aa3a31 189 printf("FOUND 2ND PATTERN\r\n");
sgodinez 96:27bdf4aa3a31 190 }
sgodinez 96:27bdf4aa3a31 191 }
sgodinez 96:27bdf4aa3a31 192 }
sgodinez 96:27bdf4aa3a31 193
sgodinez 96:27bdf4aa3a31 194 result.clear();
sgodinez 96:27bdf4aa3a31 195
sgodinez 96:27bdf4aa3a31 196 printf("Writing to socket: 2\r\n");
mfiore 115:b26176f23e89 197 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 198 if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 199 #else
mfiore 115:b26176f23e89 200 if(Wifi::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 201 #endif
sgodinez 96:27bdf4aa3a31 202 printf("Successfully wrote '2'\r\n");
sgodinez 96:27bdf4aa3a31 203 } else {
sgodinez 96:27bdf4aa3a31 204 printf("Failed to write '2'\r\n");
sgodinez 96:27bdf4aa3a31 205 return false;
sgodinez 96:27bdf4aa3a31 206 }
sgodinez 96:27bdf4aa3a31 207 printf("Expecting 'how many ? >:\r\n");
mfiore 115:b26176f23e89 208 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 209 bytesRead = Cellular::getInstance()->read(buffer, bufferSize, 10000);
mfiore 115:b26176f23e89 210 #else
mfiore 115:b26176f23e89 211 bytesRead = Wifi::getInstance()->read(buffer, bufferSize, 10000);
mfiore 115:b26176f23e89 212 #endif
sgodinez 96:27bdf4aa3a31 213 if(bytesRead != -1) {
sgodinez 96:27bdf4aa3a31 214 result.append(buffer, bytesRead);
sgodinez 96:27bdf4aa3a31 215 printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str());
sgodinez 96:27bdf4aa3a31 216 if(result.find("how many") != std::string::npos) {
sgodinez 96:27bdf4aa3a31 217 printf("Successfully found 'how many'\r\n");
sgodinez 96:27bdf4aa3a31 218 printf("Writing to socket: 2\r\n");
mfiore 115:b26176f23e89 219 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 220 if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 221 #else
mfiore 115:b26176f23e89 222 if(Wifi::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 223 #endif
sgodinez 96:27bdf4aa3a31 224 printf("Successfully wrote '2'\r\n");
sgodinez 96:27bdf4aa3a31 225 } else {
sgodinez 96:27bdf4aa3a31 226 printf("Failed to write '2'\r\n");
sgodinez 96:27bdf4aa3a31 227 return false;
sgodinez 96:27bdf4aa3a31 228 }
sgodinez 96:27bdf4aa3a31 229 } else {
sgodinez 96:27bdf4aa3a31 230 printf("Missing second option to menu item 2\r\n");
sgodinez 96:27bdf4aa3a31 231 }
sgodinez 96:27bdf4aa3a31 232 } else {
sgodinez 96:27bdf4aa3a31 233 printf("Error reading from socket\r\n");
sgodinez 96:27bdf4aa3a31 234 return false;
sgodinez 96:27bdf4aa3a31 235 }
sgodinez 96:27bdf4aa3a31 236
sgodinez 96:27bdf4aa3a31 237 return true;
sgodinez 96:27bdf4aa3a31 238 }
sgodinez 96:27bdf4aa3a31 239
mfiore 124:6d964b4343c8 240 #endif