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:
sgodinez
Date:
Fri Dec 27 15:51:19 2013 +0000
Revision:
84:77c5ab16534d
Parent:
71:82205735732b
Child:
91:9439ad14d7f0
Employed RTS threshold algorithm.  Removed header guards from cpp files.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgodinez 41:81d035fb0b6a 1 #ifndef _TEST_TCP_SOCKET_ECHO_H_
sgodinez 41:81d035fb0b6a 2 #define _TEST_TCP_SOCKET_ECHO_H_
sgodinez 41:81d035fb0b6a 3
sgodinez 41:81d035fb0b6a 4
sgodinez 41:81d035fb0b6a 5 //Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo'
sgodinez 68:c490e4a51778 6 bool testTcpSocketEchoLoop();
sgodinez 41:81d035fb0b6a 7
sgodinez 41:81d035fb0b6a 8 void testTcpSocketEcho() {
sgodinez 44:86fa8f50a1df 9 using namespace mts;
sgodinez 44:86fa8f50a1df 10
sgodinez 71:82205735732b 11 Code code;
sgodinez 41:81d035fb0b6a 12 const int TEST_PORT = 5798;
sgodinez 41:81d035fb0b6a 13 const std::string TEST_SERVER("204.26.122.96");
sgodinez 41:81d035fb0b6a 14
sgodinez 41:81d035fb0b6a 15 printf("TCP SOCKET TESTING\r\n");
sgodinez 41:81d035fb0b6a 16 printf("Setting APN\r\n");
sgodinez 41:81d035fb0b6a 17 code = Cellular::getInstance()->setApn("b2b.tmobile.com");
sgodinez 71:82205735732b 18 if(code == SUCCESS) {
sgodinez 41:81d035fb0b6a 19 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 20 } else {
sgodinez 41:81d035fb0b6a 21 printf("Error during APN setup [%d]\r\n", (int)code);
sgodinez 41:81d035fb0b6a 22 }
sgodinez 41:81d035fb0b6a 23
sgodinez 41:81d035fb0b6a 24 printf("Setting Socket Closeable\r\n");
sgodinez 41:81d035fb0b6a 25 code = Cellular::getInstance()->setSocketCloseable();
sgodinez 71:82205735732b 26 if(code == SUCCESS) {
sgodinez 41:81d035fb0b6a 27 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 28 } else {
sgodinez 41:81d035fb0b6a 29 printf("Error setting socket closeable [%d]\r\n", (int)code);
sgodinez 41:81d035fb0b6a 30 }
sgodinez 41:81d035fb0b6a 31
sgodinez 53:27c9622de0f9 32 printf("Setting Local Port\r\n");
sgodinez 53:27c9622de0f9 33 if(Cellular::getInstance()->bind(5000)) {
sgodinez 53:27c9622de0f9 34 printf("Success!\r\n");
sgodinez 53:27c9622de0f9 35 } else {
sgodinez 53:27c9622de0f9 36 printf("Error setting local port [%d]\r\n", (int)code);
sgodinez 53:27c9622de0f9 37 }
sgodinez 53:27c9622de0f9 38
sgodinez 53:27c9622de0f9 39 printf("Setting Primary DNS\r\n");
sgodinez 53:27c9622de0f9 40 code = Cellular::getInstance()->setDns("8.8.8.8");
sgodinez 71:82205735732b 41 if(code == SUCCESS) {
sgodinez 53:27c9622de0f9 42 printf("Success!\r\n");
sgodinez 53:27c9622de0f9 43 } else {
sgodinez 53:27c9622de0f9 44 printf("Error setting primary DNS [%d]\r\n", (int)code);
sgodinez 53:27c9622de0f9 45 }
sgodinez 53:27c9622de0f9 46
sgodinez 41:81d035fb0b6a 47 printf("Establishing PPP Connection\r\n");
sgodinez 41:81d035fb0b6a 48 if(Cellular::getInstance()->connect()) {
sgodinez 41:81d035fb0b6a 49 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 50 } else {
sgodinez 55:56d9a9d98079 51 printf("Error during PPP connection. Aborting.\r\n");
sgodinez 55:56d9a9d98079 52 return;
sgodinez 41:81d035fb0b6a 53 }
sgodinez 41:81d035fb0b6a 54
sgodinez 41:81d035fb0b6a 55 printf("Opening a TCP Socket\r\n");
sgodinez 41:81d035fb0b6a 56 if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
sgodinez 41:81d035fb0b6a 57 printf("Success!\r\n");
sgodinez 41:81d035fb0b6a 58 } else {
sgodinez 55:56d9a9d98079 59 printf("Error during TCP socket open [%s:%d]. Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT);
sgodinez 55:56d9a9d98079 60 return;
sgodinez 41:81d035fb0b6a 61 }
sgodinez 41:81d035fb0b6a 62
sgodinez 68:c490e4a51778 63 int count = 0;
sgodinez 68:c490e4a51778 64 while(testTcpSocketEchoLoop()) {
sgodinez 68:c490e4a51778 65 count++;
sgodinez 68:c490e4a51778 66 printf("Successful Echos: [%d]\r\n", count);
sgodinez 68:c490e4a51778 67 }
sgodinez 68:c490e4a51778 68
sgodinez 68:c490e4a51778 69 printf("Closing socket\r\n");
sgodinez 68:c490e4a51778 70 Cellular::getInstance()->close();
sgodinez 68:c490e4a51778 71
sgodinez 68:c490e4a51778 72
sgodinez 68:c490e4a51778 73 //printf("Disconnecting\r\n");
sgodinez 68:c490e4a51778 74 // Cellular::getInstance()->disconnect();
sgodinez 68:c490e4a51778 75 }
sgodinez 68:c490e4a51778 76
sgodinez 68:c490e4a51778 77 bool testTcpSocketEchoLoop() {
sgodinez 68:c490e4a51778 78 const char buffer[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*";
sgodinez 84:77c5ab16534d 79
sgodinez 84:77c5ab16534d 80 /*//Big Buffer
sgodinez 84:77c5ab16534d 81 const char buffer[] = "1ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 82 "2ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 83 "3ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 84 "4ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 85 "5ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 86 "6ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 87 "7ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 88 "8ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 89 "9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"
sgodinez 84:77c5ab16534d 90 "0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*";
sgodinez 84:77c5ab16534d 91 */
sgodinez 84:77c5ab16534d 92
sgodinez 68:c490e4a51778 93 const int size = sizeof(buffer);
sgodinez 68:c490e4a51778 94 char echoData[size];
sgodinez 68:c490e4a51778 95
sgodinez 41:81d035fb0b6a 96 printf("Sending buffer\r\n");
sgodinez 41:81d035fb0b6a 97 int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000);
sgodinez 41:81d035fb0b6a 98 if(bytesWritten == size) {
sgodinez 41:81d035fb0b6a 99 printf("Successfully sent buffer\r\n");
sgodinez 41:81d035fb0b6a 100 } else {
sgodinez 55:56d9a9d98079 101 printf("Failed to send buffer. Closing socket and aborting.\r\n");
sgodinez 55:56d9a9d98079 102 Cellular::getInstance()->close();
sgodinez 68:c490e4a51778 103 return false;
sgodinez 41:81d035fb0b6a 104 }
sgodinez 41:81d035fb0b6a 105
sgodinez 41:81d035fb0b6a 106 printf("Receiving echo (timeout = 15 seconds)\r\n");
sgodinez 41:81d035fb0b6a 107 Timer tmr;
sgodinez 41:81d035fb0b6a 108 int bytesRead = 0;
sgodinez 41:81d035fb0b6a 109 tmr.start();
sgodinez 41:81d035fb0b6a 110 do {
sgodinez 55:56d9a9d98079 111 int status = Cellular::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000);
sgodinez 55:56d9a9d98079 112 if(status != -1) {
sgodinez 55:56d9a9d98079 113 bytesRead += status;
sgodinez 55:56d9a9d98079 114 } else {
sgodinez 55:56d9a9d98079 115 printf("Error reading from socket. Closing socket and aborting.\r\n");
sgodinez 55:56d9a9d98079 116 Cellular::getInstance()->close();
sgodinez 68:c490e4a51778 117 return false;
sgodinez 55:56d9a9d98079 118 }
sgodinez 41:81d035fb0b6a 119 printf("Total bytes read %d\r\n", bytesRead);
sgodinez 41:81d035fb0b6a 120 } while (tmr.read_ms() <= 15000 && bytesRead < size);
sgodinez 41:81d035fb0b6a 121
sgodinez 55:56d9a9d98079 122
sgodinez 41:81d035fb0b6a 123 //Safely Cap at Max Size
sgodinez 41:81d035fb0b6a 124 echoData[size - 1] = '\0';
sgodinez 41:81d035fb0b6a 125 printf("Comparing Buffers\r\n");
sgodinez 68:c490e4a51778 126 printf("SENT [%d]: [%s]\r\n", size, buffer);
sgodinez 68:c490e4a51778 127 printf("RECV [%d]: [%s]\r\n", bytesRead, echoData);
sgodinez 41:81d035fb0b6a 128
sgodinez 41:81d035fb0b6a 129 for(int i = 0; i < size - 1; i++) {
sgodinez 41:81d035fb0b6a 130 if(buffer[i] != echoData[i]) {
sgodinez 41:81d035fb0b6a 131 printf("Buffers do not match at index %d\r\n", i);
sgodinez 68:c490e4a51778 132 return false;
sgodinez 41:81d035fb0b6a 133 }
sgodinez 68:c490e4a51778 134 }
sgodinez 68:c490e4a51778 135 return true;
sgodinez 41:81d035fb0b6a 136 }
sgodinez 41:81d035fb0b6a 137
sgodinez 41:81d035fb0b6a 138 #endif