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:
Tue Sep 02 18:38:55 2014 +0000
Revision:
152:9a2c7ed27744
Parent:
147:cc1789c65687
Wifi: fix compatibility break with old shields by checking for both old and new style responses to "show connection" command

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 147:cc1789c65687 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 115:b26176f23e89 60 Transport::setTransport(Transport::CELLULAR);
mfiore 115:b26176f23e89 61 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 115:b26176f23e89 62 serial->baud(115200);
mfiore 115:b26176f23e89 63 Cellular::getInstance()->init(serial);
mfiore 115:b26176f23e89 64
sgodinez 19:38794784e009 65 printf("Setting APN\r\n");
mfiore 115:b26176f23e89 66 code = Cellular::getInstance()->setApn("wap.cingular");
sgodinez 71:82205735732b 67 if(code == SUCCESS) {
sgodinez 19:38794784e009 68 printf("Success!\r\n");
sgodinez 19:38794784e009 69 } else {
sgodinez 19:38794784e009 70 printf("Error during APN setup [%d]\r\n", (int)code);
sgodinez 19:38794784e009 71 }
mfiore 115:b26176f23e89 72 #else
mfiore 124:6d964b4343c8 73 for (int i = 6; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 74 wait(2);
mfiore 124:6d964b4343c8 75 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 76 }
mfiore 115:b26176f23e89 77 Transport::setTransport(Transport::WIFI);
mfiore 115:b26176f23e89 78 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
mfiore 115:b26176f23e89 79 serial->baud(9600);
mfiore 115:b26176f23e89 80 Wifi::getInstance()->init(serial);
sgodinez 19:38794784e009 81
mfiore 124:6d964b4343c8 82 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 83 if(code == SUCCESS) {
sgodinez 19:38794784e009 84 printf("Success!\r\n");
sgodinez 19:38794784e009 85 } else {
mfiore 115:b26176f23e89 86 printf("Error during network setup [%d]\r\n", (int)code);
mfiore 115:b26176f23e89 87 }
mfiore 115:b26176f23e89 88 code = Wifi::getInstance()->setDeviceIP();
mfiore 115:b26176f23e89 89 if(code == SUCCESS) {
mfiore 115:b26176f23e89 90 printf("Success!\r\n");
mfiore 115:b26176f23e89 91 } else {
mfiore 115:b26176f23e89 92 printf("Error during IP setup [%d]\r\n", (int)code);
mfiore 115:b26176f23e89 93 }
mfiore 115:b26176f23e89 94 #endif
mfiore 115:b26176f23e89 95
mfiore 115:b26176f23e89 96 printf("Establishing Connection\r\n");
mfiore 115:b26176f23e89 97 #if CELL_SHIELD
mfiore 115:b26176f23e89 98 if(Cellular::getInstance()->connect()) {
mfiore 115:b26176f23e89 99 #else
mfiore 115:b26176f23e89 100 if(Wifi::getInstance()->connect()) {
mfiore 115:b26176f23e89 101 #endif
mfiore 115:b26176f23e89 102 printf("Success!\r\n");
mfiore 115:b26176f23e89 103 } else {
mfiore 115:b26176f23e89 104 printf("Error during connection\r\n");
sgodinez 19:38794784e009 105 }
sgodinez 19:38794784e009 106
sgodinez 19:38794784e009 107 printf("Opening a TCP Socket\r\n");
mfiore 115:b26176f23e89 108 #if CELL_SHIELD
sgodinez 19:38794784e009 109 if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
mfiore 115:b26176f23e89 110 #else
mfiore 115:b26176f23e89 111 if(Wifi::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
mfiore 115:b26176f23e89 112 #endif
sgodinez 19:38794784e009 113 printf("Success!\r\n");
sgodinez 19:38794784e009 114 } else {
sgodinez 19:38794784e009 115 printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT);
sgodinez 19:38794784e009 116 }
sgodinez 19:38794784e009 117
sgodinez 96:27bdf4aa3a31 118 //Find Welcome Message and Menu
sgodinez 19:38794784e009 119
sgodinez 96:27bdf4aa3a31 120 int count = 0;
sgodinez 96:27bdf4aa3a31 121 while(testTcpSocketIteration()) {
sgodinez 96:27bdf4aa3a31 122 count++;
sgodinez 96:27bdf4aa3a31 123 printf("Successful Iterations: %d\r\n", count);
sgodinez 19:38794784e009 124 }
sgodinez 19:38794784e009 125
sgodinez 19:38794784e009 126 printf("Closing socket\r\n");
mfiore 115:b26176f23e89 127 #if CELL_SHIELD
sgodinez 19:38794784e009 128 Cellular::getInstance()->close();
mfiore 115:b26176f23e89 129 #else
mfiore 115:b26176f23e89 130 Wifi::getInstance()->close();
mfiore 115:b26176f23e89 131 #endif
sgodinez 19:38794784e009 132
sgodinez 58:408f67fa292f 133 wait(10);
sgodinez 19:38794784e009 134
sgodinez 19:38794784e009 135 printf("Disconnecting\r\n");
mfiore 115:b26176f23e89 136 #if CELL_SHIELD
sgodinez 19:38794784e009 137 Cellular::getInstance()->disconnect();
mfiore 115:b26176f23e89 138 #else
mfiore 115:b26176f23e89 139 Wifi::getInstance()->disconnect();
mfiore 115:b26176f23e89 140 #endif
sgodinez 19:38794784e009 141 }
sgodinez 19:38794784e009 142
sgodinez 96:27bdf4aa3a31 143 bool testTcpSocketIteration() {
sgodinez 96:27bdf4aa3a31 144 Timer tmr;
sgodinez 96:27bdf4aa3a31 145 int bytesRead = 0;
sgodinez 96:27bdf4aa3a31 146 const int bufferSize = 1024;
sgodinez 96:27bdf4aa3a31 147 char buffer[bufferSize] = { 0 };
sgodinez 96:27bdf4aa3a31 148 std::string result;
sgodinez 96:27bdf4aa3a31 149
sgodinez 96:27bdf4aa3a31 150 printf("Receiving Data\r\n");
sgodinez 96:27bdf4aa3a31 151 tmr.start();
sgodinez 96:27bdf4aa3a31 152 do {
mfiore 115:b26176f23e89 153 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 154 int size = Cellular::getInstance()->read(buffer, bufferSize, 1000);
mfiore 115:b26176f23e89 155 #else
mfiore 115:b26176f23e89 156 int size = Wifi::getInstance()->read(buffer, bufferSize, 1000);
mfiore 115:b26176f23e89 157 #endif
sgodinez 96:27bdf4aa3a31 158 if(size != -1) {
sgodinez 96:27bdf4aa3a31 159 result.append(buffer, size);
sgodinez 96:27bdf4aa3a31 160 } else {
sgodinez 96:27bdf4aa3a31 161 printf("Error reading from socket\r\n");
sgodinez 96:27bdf4aa3a31 162 return false;
sgodinez 96:27bdf4aa3a31 163 }
sgodinez 96:27bdf4aa3a31 164 printf("Total bytes read %d\r\n", result.size());
sgodinez 96:27bdf4aa3a31 165 } while (tmr.read() <= 15 && bytesRead < bufferSize);
sgodinez 96:27bdf4aa3a31 166
sgodinez 96:27bdf4aa3a31 167 printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str());
sgodinez 96:27bdf4aa3a31 168
sgodinez 96:27bdf4aa3a31 169 size_t pos = result.find(PATTERN_LINE1);
sgodinez 96:27bdf4aa3a31 170 if(pos != std::string::npos) {
sgodinez 96:27bdf4aa3a31 171 //compare buffers
sgodinez 96:27bdf4aa3a31 172 int patternSize = sizeof(PATTERN) - 1;
sgodinez 96:27bdf4aa3a31 173 const char* ptr = &result.data()[pos];
sgodinez 96:27bdf4aa3a31 174 bool match = true;
sgodinez 96:27bdf4aa3a31 175 for(int i = 0; i < patternSize; i++) {
sgodinez 96:27bdf4aa3a31 176 if(PATTERN[i] != ptr[i]) {
sgodinez 96:27bdf4aa3a31 177 printf("1ST PATTERN DOESN'T MATCH AT [%d]\r\n", i);
sgodinez 96:27bdf4aa3a31 178 printf("PATTERN [%02X] BUFFER [%02X]\r\n", PATTERN[i], ptr[i]);
sgodinez 96:27bdf4aa3a31 179 match = false;
sgodinez 96:27bdf4aa3a31 180 break;
sgodinez 96:27bdf4aa3a31 181 }
sgodinez 96:27bdf4aa3a31 182 }
sgodinez 96:27bdf4aa3a31 183 if(match) {
sgodinez 96:27bdf4aa3a31 184 printf("FOUND 1ST PATTERN\r\n");
sgodinez 96:27bdf4aa3a31 185 }
sgodinez 96:27bdf4aa3a31 186
sgodinez 96:27bdf4aa3a31 187 pos = result.find(PATTERN_LINE1, pos + patternSize);
sgodinez 96:27bdf4aa3a31 188 if(pos != std::string::npos) {
sgodinez 96:27bdf4aa3a31 189 //compare buffers
sgodinez 96:27bdf4aa3a31 190 ptr = &result.data()[pos];
sgodinez 96:27bdf4aa3a31 191 match = true;
sgodinez 96:27bdf4aa3a31 192 for(int i = 0; i < patternSize; i++) {
sgodinez 96:27bdf4aa3a31 193 if(PATTERN[i] != ptr[i]) {
sgodinez 96:27bdf4aa3a31 194 printf("2ND PATTERN DOESN'T MATCH AT [%d]\r\n", i);
sgodinez 96:27bdf4aa3a31 195 printf("PATTERN [%02X] BUFFER [%02X]\r\n", PATTERN[i], ptr[i]);
sgodinez 96:27bdf4aa3a31 196 match = false;
sgodinez 96:27bdf4aa3a31 197 break;
sgodinez 96:27bdf4aa3a31 198 }
sgodinez 96:27bdf4aa3a31 199 }
sgodinez 96:27bdf4aa3a31 200 if(match) {
sgodinez 96:27bdf4aa3a31 201 printf("FOUND 2ND PATTERN\r\n");
sgodinez 96:27bdf4aa3a31 202 }
sgodinez 96:27bdf4aa3a31 203 }
sgodinez 96:27bdf4aa3a31 204 }
sgodinez 96:27bdf4aa3a31 205
sgodinez 96:27bdf4aa3a31 206 result.clear();
sgodinez 96:27bdf4aa3a31 207
sgodinez 96:27bdf4aa3a31 208 printf("Writing to socket: 2\r\n");
mfiore 115:b26176f23e89 209 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 210 if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 211 #else
mfiore 115:b26176f23e89 212 if(Wifi::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 213 #endif
sgodinez 96:27bdf4aa3a31 214 printf("Successfully wrote '2'\r\n");
sgodinez 96:27bdf4aa3a31 215 } else {
sgodinez 96:27bdf4aa3a31 216 printf("Failed to write '2'\r\n");
sgodinez 96:27bdf4aa3a31 217 return false;
sgodinez 96:27bdf4aa3a31 218 }
sgodinez 96:27bdf4aa3a31 219 printf("Expecting 'how many ? >:\r\n");
mfiore 115:b26176f23e89 220 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 221 bytesRead = Cellular::getInstance()->read(buffer, bufferSize, 10000);
mfiore 115:b26176f23e89 222 #else
mfiore 115:b26176f23e89 223 bytesRead = Wifi::getInstance()->read(buffer, bufferSize, 10000);
mfiore 115:b26176f23e89 224 #endif
sgodinez 96:27bdf4aa3a31 225 if(bytesRead != -1) {
sgodinez 96:27bdf4aa3a31 226 result.append(buffer, bytesRead);
sgodinez 96:27bdf4aa3a31 227 printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str());
sgodinez 96:27bdf4aa3a31 228 if(result.find("how many") != std::string::npos) {
sgodinez 96:27bdf4aa3a31 229 printf("Successfully found 'how many'\r\n");
sgodinez 96:27bdf4aa3a31 230 printf("Writing to socket: 2\r\n");
mfiore 115:b26176f23e89 231 #if CELL_SHIELD
sgodinez 96:27bdf4aa3a31 232 if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 233 #else
mfiore 115:b26176f23e89 234 if(Wifi::getInstance()->write("2\r\n", 3, 10000) == 3) {
mfiore 115:b26176f23e89 235 #endif
sgodinez 96:27bdf4aa3a31 236 printf("Successfully wrote '2'\r\n");
sgodinez 96:27bdf4aa3a31 237 } else {
sgodinez 96:27bdf4aa3a31 238 printf("Failed to write '2'\r\n");
sgodinez 96:27bdf4aa3a31 239 return false;
sgodinez 96:27bdf4aa3a31 240 }
sgodinez 96:27bdf4aa3a31 241 } else {
sgodinez 96:27bdf4aa3a31 242 printf("Missing second option to menu item 2\r\n");
sgodinez 96:27bdf4aa3a31 243 }
sgodinez 96:27bdf4aa3a31 244 } else {
sgodinez 96:27bdf4aa3a31 245 printf("Error reading from socket\r\n");
sgodinez 96:27bdf4aa3a31 246 return false;
sgodinez 96:27bdf4aa3a31 247 }
sgodinez 96:27bdf4aa3a31 248
sgodinez 96:27bdf4aa3a31 249 return true;
sgodinez 96:27bdf4aa3a31 250 }
sgodinez 96:27bdf4aa3a31 251
kranjan 141:571e0ef6c8dc 252 #endif