Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

internal/file/FileList.cpp

Committer:
dan_ackme
Date:
2014-08-11
Revision:
0:ea85c4bb5e1f
Child:
1:6ec9998427ad

File content as of revision 0:ea85c4bb5e1f:

/*
 * 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 "Wiconnect.h"
#include "internal/common.h"
#include "StringUtil.h"


static bool nameMatches(const char *needle, const char* haystack);



/*************************************************************************************************/
WiconnectResult FileInterface::listFiles(FileList &list, const char *name, FileType type, uint32_t version)
{
    WiconnectResult result;

    CHECK_OTHER_COMMAND_EXECUTING();

    if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("ls -v")))
    {
        result = processFileList(wiconnect->internalBuffer, list, name, type, version);
    }

    CHECK_CLEANUP_COMMAND();

    return result;
}

/*************************************************************************************************/
WiconnectResult FileInterface::processFileList(char *responseStr, FileList &list, const char *name, FileType type, uint32_t version)
{
    WiconnectResult result = WICONNECT_SUCCESS;
    char *line, *savedLine;

    for(savedLine = responseStr; (line = StringUtil::strtok_r(savedLine, "\r\n", &savedLine)) != NULL;)
    {
        uint32_t tmp;
        char *toks[7], *savedTok;

        if(*line != '#')
        {
            continue;
        }
        savedTok = line + 2;

        for(int i = 0; i < 6 && (toks[i] = StringUtil::strtok_r(savedTok, " ", &savedTok)) != NULL; ++i)
        {
            if(toks[i] == NULL)
            {
                result = WICONNECT_RESPONSE_PARSE_ERROR;
                goto exit;
            }
        }


        if(name != NULL && !nameMatches(name, savedTok+1))
        {
            continue;
        }
        else if((type != FILE_TYPE_ANY) &&
                StringUtil::strHexToUint32((const char*)&toks[1][2], &tmp) &&
                (type != (FileType)tmp))
        {
            continue;
        }
        else if((version != 0) &&
                FileInterface::fileVersionStrToInt(toks[5], &tmp) &&
                (version != tmp))
        {
            continue;
        }
        else if(WICONNECT_FAILED(result, list.add(toks[1], toks[2], toks[4], toks[5], savedTok+1)))
        {
            goto exit;
        }
    }

    exit:
    return result;
}


/*************************************************************************************************/
static bool nameMatches(const char *needle, const char* haystack)
{
    const int haystackLen = strlen(haystack);

    if(*needle == '*')
    {
        const int n = strlen(needle + 1);

        if(n > haystackLen)
        {
            return false;
        }
        return strcmp(needle+1, &haystack[haystackLen - n]) == 0;
    }
    else
    {
        return strcmp(needle, haystack) == 0;
    }
}