increased chunk size

Dependencies:   HTTPClient-SSL

Fork of MTS-Socket by MultiTech

Test/TestTCPSocketConnection.h

Committer:
mfiore
Date:
2014-06-03
Revision:
7:08b474178245
Child:
8:a3b41ec82e63

File content as of revision 7:08b474178245:

#ifndef TESTTCPSOCKETCONNECTION_H
#define TESTTCPSOCKETCONNECTION_H

#include "mtsas.h"
#include <string>

using namespace mts;

class TestTCPSocketConnection : public TestCollection
{
public:
    TestTCPSocketConnection();
    virtual void run();
    
private:
    bool runIteration(const string name);
    MTSSerialFlowControl* io;
    Cellular* radio;
    TCPSocketConnection* sock;
};

const char PATTERN_LINE1[] = "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|";
const char PATTERN[] = "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}/"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}-"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\"
                       "abcdefghiiklmnopqrstuvwzyzABCDEFGHIiKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*";

const char MENU_LINE1[] = "1       send ascii pattern until keypress";
const char MENU[] = "1       send ascii pattern until keypress"
                    "2       send ascii pattern (numbered)"
                    "3       send pattern and close socket"
                    "4       send [ETX] and wait for keypress"
                    "5       send [DLE] and wait for keypress"
                    "6       send all hex values (00-FF)"
                    "q       quit"
                    ">:";

const char TEST_SERVER[] = "204.26.122.5";
const int TEST_PORT = 7000;

TestTCPSocketConnection::TestTCPSocketConnection() : TestCollection("TestTCPSocketConnection") {}

void TestTCPSocketConnection::run() {
    MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
    
    Test::start("Setup");
    io = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
    io->baud(115200);
    radio = CellularFactory::create(io);
    if (! radio) {
        Test::assertTrue(false);
    }
    radio->configureSignals(PTA4, PTC9, PTA20);
    Transport::setTransport(radio);
    
    for (int i = 0; i < 10; i++) {
        if (i >= 10) {
            Test::assertTrue(false);
        }
        if (radio->setApn("wap.cingular") == SUCCESS) {
            break;
        } else {
            wait(1);
        }
    }
    for (int i = 0; i < 3; i++) {
        if (i >= 3) {
            Test::assertTrue(false);
        }
        if (radio->connect()) {
            break;
        } else {
            wait(1);
        }
    }
    
    for (int i = 0; i < 5; i++) {
        if (i >= 5) {
            Test::assertTrue(false);
        }
        if (radio->ping()) {
            break;
        } else {
            wait(1);
        }
    }
    
    sock = new TCPSocketConnection();
    sock->set_blocking(false);
    Test::end();
    
    for (int i = 0; i < 5; i++) {
        Test::assertTrue(runIteration("Iteration " + i));
    }
}

bool TestTCPSocketConnection::runIteration(const string iteration) {
    Timer tmr;
    int bytesRead = 0;
    const int readSize = 1024;
    char buffer[readSize] = {0};
    string result;
    
    for (int i = 0; i < 5; i++) {
        if (i >= 5) {
            return false;
        }
        if (! sock->connect(TEST_SERVER, TEST_PORT)) {
            break;
        } else {
            wait(1);
        }
    }
    
    logInfo("Receiving Data");
    tmr.reset();
    tmr.start();
    do {
        bytesRead = sock->receive(buffer, readSize);
        
        if (bytesRead > 0) {
            result.append(buffer, bytesRead);
        } else if (bytesRead <= 0) {
            break;
        }
        
        logInfo("Total Bytes Read: %d", result.size());
    } while(tmr.read() <= 15 && result.size() < readSize);
    
    size_t pos = result.find(MENU_LINE1);
    if(pos != string::npos) {
        int patternSize = sizeof(MENU) - 1;
        const char* ptr = &result.data()[pos];
        bool match = true;
        for(int i = 0; i < patternSize; i++) {
            if(MENU[i] != ptr[i]) {
                logError("1st Pattern Doesn't Match At [%d]", i);
                logError("Pattern [%02X]  Buffer [%02X]", MENU[i], ptr[i]);
                match = false;
                break;   
            }
        }
        if(match) {
            logError("Found Menu");   
        }
    }
    
    result.clear();
    
    logInfo("Writing To Socket: 2");
    if(sock->send("2\r\n", 3) == 3) {
        logInfo("Successfully Wrote '2'");
    } else {
        logError("Failed To Write '2'");   
        return false;
    }
    logInfo("Expecting 'how many ? >:");
    bytesRead = sock->receive(buffer, readSize);
    if(bytesRead > 0) {
        result.append(buffer, bytesRead);
        logInfo("Received: [%d] [%s]", bytesRead, result.c_str());
        if(result.find("how many") != std::string::npos) {
            logInfo("Successfully Found 'how many'");   
            logInfo("Writing To Socket: 2");
            if(sock->send("2\r\n", 3) == 3) {
                logInfo("Successfully wrote '2'");
            } else {
                logError("Failed to write '2'");   
                return false;
            }
        } else {
            logInfo("Missing second option to menu item 2");
        }
    } else {
        logError("Error reading from socket");
        return false;
    }
    
    result.clear();
    
    logInfo("Receiving Data");
    tmr.reset();
    tmr.start();
    do {
        bytesRead = sock->receive(buffer, readSize);
        
        if (bytesRead > 0) {
            result.append(buffer, bytesRead);
        } else if (bytesRead <= 0) {
            break;
        }
        
        logInfo("Total Bytes Read: %d", result.size());
    } while(tmr.read() <= 15 && result.size() < readSize);
    
    logInfo("Received Data: [%d] [%s]", result.size(), result.c_str());
    
    pos = result.find(PATTERN_LINE1);
    if(pos != string::npos) {
        int patternSize = sizeof(PATTERN) - 1;
        const char* ptr = &result.data()[pos];
        bool match = true;
        for(int i = 0; i < patternSize; i++) {
            if(PATTERN[i] != ptr[i]) {
                logError("1st Pattern Doesn't Match At [%d]", i);
                logError("Pattern [%02X]  Buffer [%02X]", PATTERN[i], ptr[i]);
                match = false;
                break;   
            }
        }
        if(match) {
            logError("Found 1st Pattern");   
        }
        
        pos = result.find(PATTERN_LINE1, pos + patternSize);
        if(pos != std::string::npos) {
            ptr = &result.data()[pos];
            match = true;
            for(int i = 0; i < patternSize; i++) {
                if(PATTERN[i] != ptr[i]) {
                    logError("2nd Pattern Doesn't Match At [%d]", i);
                    logError("Pattern [%02X]  Buffer [%02X]", PATTERN[i], ptr[i]);
                    match = false;
                    break;   
                }
            }
            if(match) {
                logError("Found 2nd Pattern");     
            }
        }
    }
    
    result.clear();
    
    sock->close();
    
    return true;
}

#endif