Multi-Hackers / SocketModem

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

tests/test_TCP_Socket_Echo.h

Committer:
sgodinez
Date:
2013-12-24
Revision:
68:c490e4a51778
Parent:
55:56d9a9d98079
Child:
71:82205735732b

File content as of revision 68:c490e4a51778:

#ifndef _TEST_TCP_SOCKET_ECHO_H_
#define _TEST_TCP_SOCKET_ECHO_H_


//Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo'
bool testTcpSocketEchoLoop();

void testTcpSocketEcho() {
    using namespace mts;
    
    Cellular::Code code;
    const int TEST_PORT = 5798;
    const std::string TEST_SERVER("204.26.122.96");
    
    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("Setting Local Port\r\n");
    if(Cellular::getInstance()->bind(5000)) {
        printf("Success!\r\n");
    } else {
        printf("Error setting local port [%d]\r\n", (int)code);
    }
    
    printf("Setting Primary DNS\r\n");
    code = Cellular::getInstance()->setDns("8.8.8.8");
    if(code == Cellular::CELL_OK) {
        printf("Success!\r\n");
    } else {
        printf("Error setting primary DNS [%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.  Aborting.\r\n");
        return;
    }
       
    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].  Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT);
        return;
    }
    
    int count = 0;
    while(testTcpSocketEchoLoop()) {
        count++;  
        printf("Successful Echos: [%d]\r\n", count);  
    }
        
    printf("Closing socket\r\n");
    Cellular::getInstance()->close();
    
    
    //printf("Disconnecting\r\n");
//    Cellular::getInstance()->disconnect();   
}

bool testTcpSocketEchoLoop() {
    const char buffer[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*";
    const int size = sizeof(buffer); 
    char echoData[size];
    
    printf("Sending buffer\r\n");
    int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000);
    if(bytesWritten == size) {
        printf("Successfully sent buffer\r\n");
    } else {
        printf("Failed to send buffer.  Closing socket and aborting.\r\n");
        Cellular::getInstance()->close();
        return false;
    }
    
    printf("Receiving echo (timeout = 15 seconds)\r\n");
    Timer tmr;
    int bytesRead = 0;
    tmr.start();
    do {
        int status = Cellular::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000);
        if(status != -1) {
            bytesRead += status;
        } else {
            printf("Error reading from socket.  Closing socket and aborting.\r\n");
            Cellular::getInstance()->close();
            return false;
        }
        printf("Total bytes read %d\r\n", bytesRead);
    } while (tmr.read_ms() <= 15000 && bytesRead < size);


    //Safely Cap at Max Size
    echoData[size - 1] = '\0';
    printf("Comparing Buffers\r\n");
    printf("SENT [%d]: [%s]\r\n", size, buffer);
    printf("RECV [%d]: [%s]\r\n", bytesRead, echoData);
    
    for(int i = 0; i < size - 1; i++) {
        if(buffer[i] != echoData[i]) {
            printf("Buffers do not match at index %d\r\n", i);
            return false;   
        }   
    }   
    return true;
}

#endif