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:
91:9439ad14d7f0
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 41:81d035fb0b6a 1 #ifndef _TEST_TCP_SOCKET_ECHO_H_
sgodinez 41:81d035fb0b6a 2 #define _TEST_TCP_SOCKET_ECHO_H_
sgodinez 41:81d035fb0b6a 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
sgodinez 41:81d035fb0b6a 7
mfiore 124:6d964b4343c8 8 /* test TCP socket communication
mfiore 124:6d964b4343c8 9 * designed to talk to remote echo server
mfiore 124:6d964b4343c8 10 * will talk to server until echo doesn't match sent data */
sgodinez 41:81d035fb0b6a 11 //Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo'
mfiore 124:6d964b4343c8 12
mfiore 124:6d964b4343c8 13 using namespace mts;
mfiore 124:6d964b4343c8 14
sgodinez 68:c490e4a51778 15 bool testTcpSocketEchoLoop();
sgodinez 41:81d035fb0b6a 16
sgodinez 41:81d035fb0b6a 17 void testTcpSocketEcho() {
sgodinez 71:82205735732b 18 Code code;
sgodinez 41:81d035fb0b6a 19 const int TEST_PORT = 5798;
mfiore 124:6d964b4343c8 20 const std::string TEST_SERVER( /* public IP of server running the netcat command given above */);
sgodinez 41:81d035fb0b6a 21
sgodinez 41:81d035fb0b6a 22 printf("TCP SOCKET TESTING\r\n");
mfiore 124:6d964b4343c8 23 #if CELL_SHIELD
mfiore 124:6d964b4343c8 24 for (int i = 30; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 25 wait(2);
mfiore 124:6d964b4343c8 26 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 27 }
mfiore 124:6d964b4343c8 28 Transport::setTransport(Transport::CELLULAR);
mfiore 124:6d964b4343c8 29 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 124:6d964b4343c8 30 serial->baud(115200);
mfiore 124:6d964b4343c8 31 Cellular::getInstance()->init(serial);
mfiore 124:6d964b4343c8 32
sgodinez 41:81d035fb0b6a 33 printf("Setting APN\r\n");
mfiore 124:6d964b4343c8 34 code = Cellular::getInstance()->setApn("wap.cingular");
sgodinez 71:82205735732b 35 if(code == SUCCESS) {
sgodinez 41:81d035fb0b6a 36 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 37 } else {
sgodinez 41:81d035fb0b6a 38 printf("Error during APN setup [%d]\r\n", (int)code);
sgodinez 41:81d035fb0b6a 39 }
mfiore 124:6d964b4343c8 40 #else
mfiore 124:6d964b4343c8 41 for (int i = 6; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 42 wait(2);
mfiore 124:6d964b4343c8 43 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 44 }
mfiore 124:6d964b4343c8 45 Transport::setTransport(Transport::WIFI);
mfiore 124:6d964b4343c8 46 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
mfiore 124:6d964b4343c8 47 serial->baud(9600);
mfiore 124:6d964b4343c8 48 Wifi::getInstance()->init(serial);
sgodinez 41:81d035fb0b6a 49
mfiore 124:6d964b4343c8 50 code = Wifi::getInstance()->setNetwork("your wireless network" /* SSID of wireless */, Wifi::WPA2 /* security type of wireless */, "your wireless network password" /* password for wireless */);
mfiore 124:6d964b4343c8 51 if(code == SUCCESS) {
mfiore 124:6d964b4343c8 52 printf("Success!\r\n");
mfiore 124:6d964b4343c8 53 } else {
mfiore 124:6d964b4343c8 54 printf("Error during network setup [%d]\r\n", (int)code);
mfiore 124:6d964b4343c8 55 }
mfiore 124:6d964b4343c8 56 code = Wifi::getInstance()->setDeviceIP();
sgodinez 71:82205735732b 57 if(code == SUCCESS) {
sgodinez 41:81d035fb0b6a 58 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 59 } else {
mfiore 124:6d964b4343c8 60 printf("Error during IP setup [%d]\r\n", (int)code);
sgodinez 41:81d035fb0b6a 61 }
mfiore 124:6d964b4343c8 62 #endif
sgodinez 41:81d035fb0b6a 63
mfiore 124:6d964b4343c8 64 printf("Establishing Connection\r\n");
mfiore 124:6d964b4343c8 65 #if CELL_SHIELD
mfiore 124:6d964b4343c8 66 if(Cellular::getInstance()->connect()) {
mfiore 124:6d964b4343c8 67 #else
mfiore 124:6d964b4343c8 68 if(Wifi::getInstance()->connect()) {
mfiore 124:6d964b4343c8 69 #endif
sgodinez 53:27c9622de0f9 70 printf("Success!\r\n");
sgodinez 53:27c9622de0f9 71 } else {
mfiore 124:6d964b4343c8 72 printf("Error during connection. Aborting.\r\n");
sgodinez 55:56d9a9d98079 73 return;
sgodinez 41:81d035fb0b6a 74 }
sgodinez 41:81d035fb0b6a 75
mfiore 124:6d964b4343c8 76 #if CELL_SHIELD
sgodinez 41:81d035fb0b6a 77 if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
mfiore 124:6d964b4343c8 78 #else
mfiore 124:6d964b4343c8 79 if(Wifi::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
mfiore 124:6d964b4343c8 80 #endif
sgodinez 41:81d035fb0b6a 81 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 82 } else {
sgodinez 55:56d9a9d98079 83 printf("Error during TCP socket open [%s:%d]. Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT);
sgodinez 55:56d9a9d98079 84 return;
sgodinez 41:81d035fb0b6a 85 }
sgodinez 41:81d035fb0b6a 86
sgodinez 68:c490e4a51778 87 int count = 0;
sgodinez 68:c490e4a51778 88 while(testTcpSocketEchoLoop()) {
sgodinez 68:c490e4a51778 89 count++;
sgodinez 68:c490e4a51778 90 printf("Successful Echos: [%d]\r\n", count);
sgodinez 68:c490e4a51778 91 }
sgodinez 68:c490e4a51778 92
sgodinez 68:c490e4a51778 93 printf("Closing socket\r\n");
mfiore 124:6d964b4343c8 94 #if CELL_SHIELD
sgodinez 68:c490e4a51778 95 Cellular::getInstance()->close();
mfiore 124:6d964b4343c8 96 #else
mfiore 124:6d964b4343c8 97 Wifi::getInstance()->close();
mfiore 124:6d964b4343c8 98 #endif
sgodinez 68:c490e4a51778 99
mfiore 124:6d964b4343c8 100 wait(10);
mfiore 124:6d964b4343c8 101
mfiore 124:6d964b4343c8 102 printf("Disconnecting\r\n");
mfiore 124:6d964b4343c8 103 #if CELL_SHIELD
mfiore 124:6d964b4343c8 104 Cellular::getInstance()->disconnect();
mfiore 124:6d964b4343c8 105 #else
mfiore 124:6d964b4343c8 106 Wifi::getInstance()->disconnect();
mfiore 124:6d964b4343c8 107 #endif
sgodinez 68:c490e4a51778 108 }
sgodinez 68:c490e4a51778 109
sgodinez 68:c490e4a51778 110 bool testTcpSocketEchoLoop() {
sgodinez 91:9439ad14d7f0 111 using namespace mts;
sgodinez 68:c490e4a51778 112 const char buffer[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*";
sgodinez 84:77c5ab16534d 113
sgodinez 84:77c5ab16534d 114 /*//Big Buffer
sgodinez 84:77c5ab16534d 115 const char buffer[] = "1ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 116 "2ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 117 "3ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 118 "4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 119 "5ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 120 "6ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 121 "7ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 122 "8ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 123 "9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 124 "0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*";
sgodinez 84:77c5ab16534d 125 */
sgodinez 84:77c5ab16534d 126
sgodinez 68:c490e4a51778 127 const int size = sizeof(buffer);
sgodinez 68:c490e4a51778 128 char echoData[size];
sgodinez 68:c490e4a51778 129
sgodinez 41:81d035fb0b6a 130 printf("Sending buffer\r\n");
mfiore 124:6d964b4343c8 131 #if CELL_SHIELD
sgodinez 41:81d035fb0b6a 132 int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000);
mfiore 124:6d964b4343c8 133 #else
mfiore 124:6d964b4343c8 134 int bytesWritten = Wifi::getInstance()->write(buffer, size, 10000);
mfiore 124:6d964b4343c8 135 #endif
sgodinez 41:81d035fb0b6a 136 if(bytesWritten == size) {
sgodinez 41:81d035fb0b6a 137 printf("Successfully sent buffer\r\n");
sgodinez 41:81d035fb0b6a 138 } else {
sgodinez 55:56d9a9d98079 139 printf("Failed to send buffer. Closing socket and aborting.\r\n");
mfiore 124:6d964b4343c8 140 #if CELL_SHIELD
sgodinez 55:56d9a9d98079 141 Cellular::getInstance()->close();
mfiore 124:6d964b4343c8 142 #else
mfiore 124:6d964b4343c8 143 Wifi::getInstance()->close();
mfiore 124:6d964b4343c8 144 #endif
sgodinez 68:c490e4a51778 145 return false;
sgodinez 41:81d035fb0b6a 146 }
sgodinez 41:81d035fb0b6a 147
sgodinez 41:81d035fb0b6a 148 printf("Receiving echo (timeout = 15 seconds)\r\n");
sgodinez 41:81d035fb0b6a 149 Timer tmr;
sgodinez 41:81d035fb0b6a 150 int bytesRead = 0;
sgodinez 41:81d035fb0b6a 151 tmr.start();
sgodinez 41:81d035fb0b6a 152 do {
mfiore 124:6d964b4343c8 153 #if CELL_SHIELD
sgodinez 55:56d9a9d98079 154 int status = Cellular::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000);
mfiore 124:6d964b4343c8 155 #else
mfiore 124:6d964b4343c8 156 int status = Wifi::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000);
mfiore 124:6d964b4343c8 157 #endif
sgodinez 55:56d9a9d98079 158 if(status != -1) {
sgodinez 55:56d9a9d98079 159 bytesRead += status;
sgodinez 55:56d9a9d98079 160 } else {
sgodinez 55:56d9a9d98079 161 printf("Error reading from socket. Closing socket and aborting.\r\n");
mfiore 124:6d964b4343c8 162 #if CELL_SHIELD
sgodinez 55:56d9a9d98079 163 Cellular::getInstance()->close();
mfiore 124:6d964b4343c8 164 #else
mfiore 124:6d964b4343c8 165 Wifi::getInstance()->close();
mfiore 124:6d964b4343c8 166 #endif
sgodinez 68:c490e4a51778 167 return false;
sgodinez 55:56d9a9d98079 168 }
sgodinez 41:81d035fb0b6a 169 printf("Total bytes read %d\r\n", bytesRead);
sgodinez 41:81d035fb0b6a 170 } while (tmr.read_ms() <= 15000 && bytesRead < size);
sgodinez 41:81d035fb0b6a 171
sgodinez 55:56d9a9d98079 172
sgodinez 41:81d035fb0b6a 173 //Safely Cap at Max Size
sgodinez 41:81d035fb0b6a 174 echoData[size - 1] = '\0';
sgodinez 41:81d035fb0b6a 175 printf("Comparing Buffers\r\n");
sgodinez 68:c490e4a51778 176 printf("SENT [%d]: [%s]\r\n", size, buffer);
sgodinez 68:c490e4a51778 177 printf("RECV [%d]: [%s]\r\n", bytesRead, echoData);
sgodinez 41:81d035fb0b6a 178
sgodinez 41:81d035fb0b6a 179 for(int i = 0; i < size - 1; i++) {
sgodinez 41:81d035fb0b6a 180 if(buffer[i] != echoData[i]) {
sgodinez 41:81d035fb0b6a 181 printf("Buffers do not match at index %d\r\n", i);
sgodinez 68:c490e4a51778 182 return false;
sgodinez 41:81d035fb0b6a 183 }
sgodinez 68:c490e4a51778 184 }
sgodinez 68:c490e4a51778 185 return true;
sgodinez 41:81d035fb0b6a 186 }
sgodinez 41:81d035fb0b6a 187
sgodinez 41:81d035fb0b6a 188 #endif