ACKme / Mbed 2 deprecated wiconnect-test-console

Dependencies:   WiConnect mbed

tests/blocking/socket/TcpEchoTest.cpp

Committer:
dan_ackme
Date:
2014-08-11
Revision:
0:836c9a6383e0
Child:
1:5137ec8f4c45

File content as of revision 0:836c9a6383e0:

/*
 * Copyright 2014, ACKme Networks
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks;
 * the contents of this file may not be disclosed to third parties, copied
 * or duplicated in any form, in whole or in part, without the prior
 * written permission of ACKme Networks.
 */


#include "tests/Tests.h"
#include "Wiconnect.h"



static WiconnectResult processData(Socket &socket, uint32_t count, uint32_t delay, uint32_t size);



/*************************************************************************************************/
WiconnectResult socketTcpClientEchoCommand(int argc, char **argv)
{
    WiconnectResult result;
    Wiconnect *wiconnect = Wiconnect::getInstance();
    Socket socket(sizeof(testBuffer)/2, testBuffer, sizeof(testBuffer)/2, testBuffer + sizeof(testBuffer)/2);
    const char *host;
    uint32_t port;
    uint32_t delay = 5;
    uint32_t count = 1000;
    uint32_t size = 256;

    if(argc < 2)
    {
        LOG_ERROR("Must specify host and port");
        return WICONNECT_BAD_ARG;
    }
    host = argv[0];

    if(!StringUtil::strToUint32((const char*)argv[1], &port))
    {
        LOG_ERROR("Invalid port");
        return WICONNECT_BAD_ARG;
    }
    if(argc > 2 && !StringUtil::strToUint32((const char*)argv[2], &count))
    {
        LOG_ERROR("Invalid packet count");
        return WICONNECT_BAD_ARG;
    }
    if(argc > 3 && !StringUtil::strToUint32((const char*)argv[3], &delay))
    {
        LOG_ERROR("Invalid packet delay");
        return WICONNECT_BAD_ARG;
    }
    if(argc > 4 && (!StringUtil::strToUint32((const char*)argv[4], &size) || size > sizeof(testBuffer)/2))
    {
        LOG_ERROR("Invalid packet size");
        return WICONNECT_BAD_ARG;
    }

    if(WICONNECT_FAILED(result, wiconnect->tcpConnect(socket, host, port)))
    {
        LOG_ERROR("Failed to connect to server");
    }
    else
    {
        result = processData(socket, count, delay, size);
    }

    return result;
}


/*************************************************************************************************/
static WiconnectResult processData(Socket &socket, uint32_t count, uint32_t delay, uint32_t size)
{
    WiconnectResult result;
    uint8_t *txBuffer = socket.getTxBuffer();
    const int txBufferLen = socket.getTxBufferSize();
    uint8_t writeCounter = 32;
    uint8_t readCounter = 32;
    uint32_t bytesSent = 0;
    uint32_t bytesReceived = 0;

    LOG_INFO("\r\n------------------\r\n"
             "Starting TCP Echo Test:\r\n"
            "Packet count: %d\r\n"
            "Packet size: %d\r\n"
            "Packet delay: %d\r\n"
            "(Press any key to terminate)",
            count, size, delay);

    for(int i = 0; i < count || bytesReceived < bytesSent; ++i)
    {
        if(i < count)
        {
            uint8_t *ptr = txBuffer;
            int l = size;
            while(l--)
            {
                *ptr++ = writeCounter++;
                if(writeCounter == 128)
                    writeCounter = 32;
            }

            if(WICONNECT_FAILED(result, socket.write(size)))
            {
                break;
            }
            bytesSent += size;
            consoleSerial.putc('.');
        }

        if(consoleSerial.readable())
        {
            int c = consoleSerial.getc();
            break;
        }

        delayMs(delay);

        uint8_t *readPtr;
        uint16_t readLen;
        if(WICONNECT_FAILED(result, socket.read(&readPtr, &readLen)))
        {
            break;
        }
        bytesReceived += readLen;

        while(readLen--)
        {
            if(*readPtr != readCounter)
            {
                LOG_ERROR("Invalid: %02X != %02X", *readPtr, readCounter);
            }
            ++readPtr;
            ++readCounter;
            if(readCounter == 128)
                readCounter = 32;
        }
    }

    consoleSerial.write("\r\n");
    LOG_INFO("------------------\r\n"
             "TCP Echo Test Complete:\r\n"
            "Bytes sent: %d\r\n"
            "Bytes received: %d\r\n",
            bytesSent, bytesReceived);

    return result;
}