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:
sgodinez
Date:
2013-12-26
Revision:
71:82205735732b
Parent:
58:408f67fa292f
Child:
96:27bdf4aa3a31

File content as of revision 71:82205735732b:

#ifndef _TEST_TCP_SOCKET_H_
#define _TEST_TCP_SOCKET_H_

using namespace mts;

void testTcpSocket() {
    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 == SUCCESS) {
        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 == SUCCESS) {
        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("Receiving Data (timeout = 15 seconds)\r\n");
    Timer tmr;
    int bytesRead = 0;
    const int size = 1024;
    char data[size] = { 0 };
    tmr.start();
    do {
        int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
        if(status != -1) {
            bytesRead += status;
        } else {
            printf("Error reading from socket\r\n");
            data[bytesRead] = '\0';
            break;
        }
        printf("Total bytes read %d\r\n", bytesRead);
    } while (tmr.read_ms() <= 15000 && bytesRead < size);
   
    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");   
    }
    
    bytesRead = 0;
    tmr.start();
    do {
        int status = Cellular::getInstance()->read(&data[bytesRead], size - bytesRead, 1000);
        if(status != -1) {
            bytesRead += status;
        } else {
            printf("Error reading from socket\r\n");
            data[bytesRead] = '\0';
            break;
        }
        printf("Total bytes read %d\r\n", bytesRead);
    } while (tmr.read_ms() <= 15000 && bytesRead < size);
    printf("READ: [%d] [%s]\r\n", bytesRead, data);
   
    printf("Closing socket\r\n");
    Cellular::getInstance()->close();
    
    wait(10);
    
    printf("Disconnecting\r\n");
    Cellular::getInstance()->disconnect();   
}

#endif