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