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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileListing.cpp Source File

FileListing.cpp

00001 /**
00002  * ACKme WiConnect Host Library is licensed under the BSD licence: 
00003  * 
00004  * Copyright (c)2014 ACKme Networks.
00005  * All rights reserved. 
00006  * 
00007  * Redistribution and use in source and binary forms, with or without modification, 
00008  * are permitted provided that the following conditions are met: 
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * 3. The name of the author may not be used to endorse or promote products 
00016  * derived from this software without specific prior written permission. 
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED 
00019  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00020  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00021  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00023  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00026  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00027  * OF SUCH DAMAGE.
00028  */
00029 
00030 #include "Wiconnect.h"
00031 #include "internal/common.h"
00032 #include "api/StringUtil.h"
00033 
00034 
00035 static bool nameMatches(const char *needle, const char* haystack);
00036 
00037 
00038 
00039 /*************************************************************************************************/
00040 WiconnectResult FileInterface::listFiles(FileList &list, const char *name, FileType type, uint32_t version)
00041 {
00042     WiconnectResult result;
00043 
00044     CHECK_OTHER_COMMAND_EXECUTING();
00045 
00046     if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("ls -v")))
00047     {
00048         result = processFileList(wiconnect->internalBuffer, list, name, type, version);
00049     }
00050 
00051     CHECK_CLEANUP_COMMAND();
00052 
00053     return result;
00054 }
00055 
00056 /*************************************************************************************************/
00057 WiconnectResult FileInterface::processFileList(char *responseStr, FileList &list, const char *name, FileType type, uint32_t version)
00058 {
00059     WiconnectResult result = WICONNECT_SUCCESS;
00060     char *line, *savedLine;
00061 
00062     for(savedLine = responseStr; (line = StringUtil::strtok_r(savedLine, "\r\n", &savedLine)) != NULL;)
00063     {
00064         uint32_t tmp;
00065         char *toks[7], *savedTok;
00066 
00067         if(*line != '#')
00068         {
00069             continue;
00070         }
00071         savedTok = line + 2;
00072 
00073         for(int i = 0; i < 6 && (toks[i] = StringUtil::strtok_r(savedTok, " ", &savedTok)) != NULL; ++i)
00074         {
00075             if(toks[i] == NULL)
00076             {
00077                 result = WICONNECT_RESPONSE_PARSE_ERROR;
00078                 goto exit;
00079             }
00080         }
00081 
00082 
00083         if(name != NULL && !nameMatches(name, savedTok+1))
00084         {
00085             continue;
00086         }
00087         else if((type != FILE_TYPE_ANY) &&
00088                 StringUtil::strHexToUint32((const char*)&toks[1][2], &tmp) &&
00089                 (type != (FileType)tmp))
00090         {
00091             continue;
00092         }
00093         else if((version != 0) &&
00094                 FileInterface::fileVersionStrToInt(toks[5], &tmp) &&
00095                 (version != tmp))
00096         {
00097             continue;
00098         }
00099         else if(WICONNECT_FAILED(result, list.add(toks[1], toks[2], toks[4], toks[5], savedTok+1)))
00100         {
00101             goto exit;
00102         }
00103     }
00104 
00105     exit:
00106     return result;
00107 }
00108 
00109 
00110 /*************************************************************************************************/
00111 static bool nameMatches(const char *needle, const char* haystack)
00112 {
00113     const int haystackLen = strlen(haystack);
00114 
00115     if(*needle == '*')
00116     {
00117         const int n = strlen(needle + 1);
00118 
00119         if(n > haystackLen)
00120         {
00121             return false;
00122         }
00123         return strcmp(needle+1, &haystack[haystackLen - n]) == 0;
00124     }
00125     else
00126     {
00127         return strcmp(needle, haystack) == 0;
00128     }
00129 }