ACKme / Mbed 2 deprecated wiconnect-test-console

Dependencies:   WiConnect mbed

tests/blocking/file/FileCreateTest.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"


typedef struct
{
    uint32_t bytesRemaining;
} fileInfo_t;


static WiconnectResult fileReadCallback(void *user, void *data, int maxReadSize, int *bytesRead);





/*************************************************************************************************/
WiconnectResult fileCreateCommand(int argc, char **argv)
{
    WiconnectResult result;
    Wiconnect *wiconnect = Wiconnect::getInstance();
    const char *name;
    uint32_t size;
    uint32_t version = 0;
    uint32_t type = FILE_TYPE_ANY;
    const int savedTimeOut = wiconnect->getCommandDefaultTimeout();
    fileInfo_t fileInfo;


    if(argc < 2)
    {
        LOG_ERROR("must specify file name and size");
        return WICONNECT_BAD_ARG;
    }

    name = argv[0];
    if(!StringUtil::strToUint32(argv[1], &size))
    {
        LOG_ERROR("invalid file size");
        return WICONNECT_BAD_ARG;
    }

    if(argc > 2)
    {
        if(!Wiconnect::fileVersionStrToInt(argv[2], &version))
        {
            LOG_ERROR("invalid file version");
            return WICONNECT_BAD_ARG;
        }
    }
    if(argc > 3)
    {
        if(!StringUtil::strHexToUint32(argv[3], &type))
        {
            LOG_ERROR("invalid file type");
            return WICONNECT_BAD_ARG;
        }
    }

    fileInfo.bytesRemaining = size;
    wiconnect->setCommandDefaultTimeout(30000); // increase the timeout so the user can enter data

    if(!WICONNECT_FAILED(result, wiconnect->createFile(ReaderFunc(fileReadCallback), &fileInfo, name, size, version, (FileType)type)))
    {
        LOG_INFO("File created");
    }
    wiconnect->setCommandDefaultTimeout(savedTimeOut);

    return result;
}

/*************************************************************************************************/
static WiconnectResult fileReadCallback(void *user, void *data, int maxReadSize, int *bytesReadPtr)
{
    fileInfo_t *info = (fileInfo_t*)user;
    int bytesToRead = MIN(maxReadSize, info->bytesRemaining);

    if(info->bytesRemaining == 0)
    {
        LOG_INFO("All data written");
        *bytesReadPtr = EOF;
        return WICONNECT_SUCCESS;
    }

    LOG_INFO("Enter up to %d bytes (%d bytes remaining,\r\n       Issue $$$ terminate current write):", bytesToRead, info->bytesRemaining);

    uint8_t *ptr = (uint8_t*)data;
    int bytesRead = 0;
    int terminateCount = 0;

    while(bytesToRead > 0)
    {
        int c = consoleSerial.getc();
        consoleSerial.putc(c);
        terminateCount = (c == '$') ? terminateCount+1 : 0;
        if(terminateCount >= 3)
        {
            break;
        }
        *ptr++ = (uint8_t)c;
        ++bytesRead;
        --bytesToRead;
    }

    // remove '$$' from data if we terminated
    if(terminateCount == 3)
    {
        bytesRead -= 2;
    }

    info->bytesRemaining -= bytesRead;
    *bytesReadPtr = bytesRead;
    consoleSerial.write("\r\n");

    return WICONNECT_SUCCESS;
}