Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: tests/blocking/file/FileCreateTest.cpp
- Revision:
- 0:836c9a6383e0
- Child:
- 1:5137ec8f4c45
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/blocking/file/FileCreateTest.cpp Mon Aug 11 11:31:32 2014 +0000 @@ -0,0 +1,127 @@ +/* + * 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; +} +