Multi-Hackers / SocketModem

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Committer:
sgodinez
Date:
Fri Dec 20 21:10:13 2013 +0000
Revision:
58:408f67fa292f
Parent:
39:6e94520a3217
Child:
71:82205735732b
Changed Cellular::read() to allow retrieval of remaining rx bytes after socket closed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgodinez 19:38794784e009 1 #ifndef _TEST_TCP_SOCKET_H_
sgodinez 19:38794784e009 2 #define _TEST_TCP_SOCKET_H_
sgodinez 19:38794784e009 3
mfiore 39:6e94520a3217 4 using namespace mts;
mfiore 39:6e94520a3217 5
sgodinez 19:38794784e009 6 void testTcpSocket() {
sgodinez 19:38794784e009 7 Cellular::Code code;
sgodinez 19:38794784e009 8 const int TEST_PORT = 7000;
sgodinez 19:38794784e009 9 const std::string TEST_SERVER("204.26.122.5");
sgodinez 19:38794784e009 10
sgodinez 19:38794784e009 11 printf("TCP SOCKET TESTING\r\n");
sgodinez 19:38794784e009 12 printf("Setting APN\r\n");
sgodinez 19:38794784e009 13 code = Cellular::getInstance()->setApn("b2b.tmobile.com");
mfiore 39:6e94520a3217 14 if(code == Cellular::CELL_OK) {
sgodinez 19:38794784e009 15 printf("Success!\r\n");
sgodinez 19:38794784e009 16 } else {
sgodinez 19:38794784e009 17 printf("Error during APN setup [%d]\r\n", (int)code);
sgodinez 19:38794784e009 18 }
sgodinez 19:38794784e009 19
sgodinez 19:38794784e009 20 printf("Setting Socket Closeable\r\n");
sgodinez 19:38794784e009 21 code = Cellular::getInstance()->setSocketCloseable();
mfiore 39:6e94520a3217 22 if(code == Cellular::CELL_OK) {
sgodinez 19:38794784e009 23 printf("Success!\r\n");
sgodinez 19:38794784e009 24 } else {
sgodinez 19:38794784e009 25 printf("Error setting socket closeable [%d]\r\n", (int)code);
sgodinez 19:38794784e009 26 }
sgodinez 19:38794784e009 27
sgodinez 19:38794784e009 28 printf("Establishing PPP Connection\r\n");
sgodinez 19:38794784e009 29 if(Cellular::getInstance()->connect()) {
sgodinez 19:38794784e009 30 printf("Success!\r\n");
sgodinez 19:38794784e009 31 } else {
sgodinez 19:38794784e009 32 printf("Error during PPP connection\r\n");
sgodinez 19:38794784e009 33 }
sgodinez 19:38794784e009 34
sgodinez 19:38794784e009 35 printf("Opening a TCP Socket\r\n");
sgodinez 19:38794784e009 36 if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
sgodinez 19:38794784e009 37 printf("Success!\r\n");
sgodinez 19:38794784e009 38 } else {
sgodinez 19:38794784e009 39 printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT);
sgodinez 19:38794784e009 40 }
sgodinez 19:38794784e009 41
sgodinez 58:408f67fa292f 42 printf("Receiving Data (timeout = 15 seconds)\r\n");
sgodinez 58:408f67fa292f 43 Timer tmr;
sgodinez 58:408f67fa292f 44 int bytesRead = 0;
sgodinez 58:408f67fa292f 45 const int size = 1024;
sgodinez 58:408f67fa292f 46 char data[size] = { 0 };
sgodinez 58:408f67fa292f 47 tmr.start();
sgodinez 58:408f67fa292f 48 do {
sgodinez 58:408f67fa292f 49 int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
sgodinez 58:408f67fa292f 50 if(status != -1) {
sgodinez 58:408f67fa292f 51 bytesRead += status;
sgodinez 58:408f67fa292f 52 } else {
sgodinez 58:408f67fa292f 53 printf("Error reading from socket\r\n");
sgodinez 58:408f67fa292f 54 data[bytesRead] = '\0';
sgodinez 58:408f67fa292f 55 break;
sgodinez 58:408f67fa292f 56 }
sgodinez 58:408f67fa292f 57 printf("Total bytes read %d\r\n", bytesRead);
sgodinez 58:408f67fa292f 58 } while (tmr.read_ms() <= 15000 && bytesRead < size);
sgodinez 58:408f67fa292f 59
sgodinez 19:38794784e009 60 printf("READ: [%d] [%s]\r\n", bytesRead, data);
sgodinez 19:38794784e009 61
sgodinez 19:38794784e009 62
sgodinez 19:38794784e009 63 printf("Waiting 10 seconds\r\n");
sgodinez 19:38794784e009 64 wait(10);
sgodinez 19:38794784e009 65
sgodinez 19:38794784e009 66 printf("Writing to socket\r\n");
sgodinez 19:38794784e009 67 sprintf(data, "3\r\n");
sgodinez 19:38794784e009 68 int bytesWritten = Cellular::getInstance()->write(data, 4, 10000);
sgodinez 19:38794784e009 69 if(bytesWritten == 4) {
sgodinez 19:38794784e009 70 printf("Successfully wrote 'q'\r\n");
sgodinez 19:38794784e009 71 } else {
sgodinez 19:38794784e009 72 printf("Failed to write 'q'\r\n");
sgodinez 19:38794784e009 73 }
sgodinez 19:38794784e009 74
sgodinez 58:408f67fa292f 75 bytesRead = 0;
sgodinez 58:408f67fa292f 76 tmr.start();
sgodinez 58:408f67fa292f 77 do {
sgodinez 58:408f67fa292f 78 int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
sgodinez 58:408f67fa292f 79 if(status != -1) {
sgodinez 58:408f67fa292f 80 bytesRead += status;
sgodinez 58:408f67fa292f 81 } else {
sgodinez 58:408f67fa292f 82 printf("Error reading from socket\r\n");
sgodinez 58:408f67fa292f 83 data[bytesRead] = '\0';
sgodinez 58:408f67fa292f 84 break;
sgodinez 58:408f67fa292f 85 }
sgodinez 58:408f67fa292f 86 printf("Total bytes read %d\r\n", bytesRead);
sgodinez 58:408f67fa292f 87 } while (tmr.read_ms() <= 15000 && bytesRead < size);
sgodinez 19:38794784e009 88 printf("READ: [%d] [%s]\r\n", bytesRead, data);
sgodinez 19:38794784e009 89
sgodinez 19:38794784e009 90 printf("Closing socket\r\n");
sgodinez 19:38794784e009 91 Cellular::getInstance()->close();
sgodinez 19:38794784e009 92
sgodinez 58:408f67fa292f 93 wait(10);
sgodinez 19:38794784e009 94
sgodinez 19:38794784e009 95 printf("Disconnecting\r\n");
sgodinez 19:38794784e009 96 Cellular::getInstance()->disconnect();
sgodinez 19:38794784e009 97 }
sgodinez 19:38794784e009 98
sgodinez 19:38794784e009 99 #endif