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:
kranjan
Date:
Sat Jan 04 05:28:45 2014 +0000
Revision:
141:571e0ef6c8dc
Parent:
124:6d964b4343c8
Child:
143:c7d8fe37981b
Added licensing header to all files in the library

Who changed what in which revision?

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