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

tests/test_TCP_Socket.h

Committer:
mfiore
Date:
2013-12-19
Revision:
39:6e94520a3217
Parent:
19:38794784e009
Child:
58:408f67fa292f

File content as of revision 39:6e94520a3217:

#ifndef _TEST_TCP_SOCKET_H_
#define _TEST_TCP_SOCKET_H_

using namespace mts;

void testTcpSocket() {
    Cellular::Code code;
    const int TEST_PORT = 7000;
    const std::string TEST_SERVER("204.26.122.5");
    
    printf("TCP SOCKET TESTING\r\n");
    printf("Setting APN\r\n");
    code = Cellular::getInstance()->setApn("b2b.tmobile.com");
    if(code == Cellular::CELL_OK) {
        printf("Success!\r\n");
    } else {
        printf("Error during APN setup [%d]\r\n", (int)code);
    }
    
    printf("Setting Socket Closeable\r\n");
    code = Cellular::getInstance()->setSocketCloseable();
    if(code == Cellular::CELL_OK) {
        printf("Success!\r\n");
    } else {
        printf("Error setting socket closeable [%d]\r\n", (int)code);
    }
    
    printf("Establishing PPP Connection\r\n");
    if(Cellular::getInstance()->connect()) {
        printf("Success!\r\n");
    } else {
        printf("Error during PPP connection\r\n");
    }
       
    printf("Opening a TCP Socket\r\n");
    if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) {
        printf("Success!\r\n");   
    } else {
        printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT);
    }
    
    printf("Reading from socket for 15 seconds\r\n");
    char data[1024] = { 0 };
    int bytesRead = Cellular::getInstance()->read(data, 1023, 15000);    //1 less than max
    printf("READ: [%d] [%s]\r\n", bytesRead, data);
    
        
    printf("Waiting 10 seconds\r\n");
    wait(10);
    
    printf("Writing to socket\r\n");
    sprintf(data, "3\r\n");
    int bytesWritten = Cellular::getInstance()->write(data, 4, 10000);
    if(bytesWritten == 4) {
        printf("Successfully wrote 'q'\r\n");
    } else {
        printf("Failed to write 'q'\r\n");   
    }
    
    printf("Reading from socket for 15 seconds\r\n");
    bytesRead = Cellular::getInstance()->read(data, 1023, 15000);    //1 less than max
    printf("READ: [%d] [%s]\r\n", bytesRead, data);
   
    printf("Closing socket\r\n");
    Cellular::getInstance()->close();
    
    
    printf("Disconnecting\r\n");
    Cellular::getInstance()->disconnect();   
}

#endif