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 Axeda Corp

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?

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