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:
Sun Jan 05 05:49:42 2014 +0000
Revision:
143:c7d8fe37981b
Parent:
141:571e0ef6c8dc
revamp Cellular::init() function; resets the radio to make sure it's in a good state before proceeding; this function waits for the radio to come up, so we don't need to outside; ; update documentation and examples in SocketModem to reflect these changes

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