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
FileInterface.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 /*************************************************************************************************/ 00036 FileInterface::FileInterface(Wiconnect *wiconnect_) 00037 { 00038 wiconnect = wiconnect_; 00039 } 00040 00041 /*************************************************************************************************/ 00042 WiconnectResult FileInterface::openFile(WiconnectFile &file, const char *name) 00043 { 00044 WiconnectResult result; 00045 00046 CHECK_OTHER_COMMAND_EXECUTING(); 00047 00048 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("fop %s", name))) 00049 { 00050 int32_t handle; 00051 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&handle))) 00052 { 00053 file.openForRead(handle, name); 00054 } 00055 } 00056 00057 CHECK_CLEANUP_COMMAND(); 00058 00059 return result; 00060 } 00061 00062 /*************************************************************************************************/ 00063 WiconnectResult FileInterface::createFile(const ReaderFunc &reader, void *user, const char *name, uint32_t size, uint32_t version, FileType type, bool isEssential, int32_t checksum) 00064 { 00065 WiconnectResult result; 00066 char *cmdBuffer = wiconnect->internalBuffer; 00067 00068 if(WICONNECT_IS_IDLE()) 00069 { 00070 char *ptr = cmdBuffer; 00071 00072 ptr += sprintf(cmdBuffer, "fcr %s%s %u", isEssential ? "-e " : "", name, (unsigned int)size); 00073 00074 if(version != 0) 00075 { 00076 *ptr = ' '; 00077 ++ptr; 00078 FileInterface::fileVersionIntToStr(version, true, ptr); 00079 ptr = ptr + strlen(ptr); 00080 } 00081 if(type != FILE_TYPE_ANY) 00082 { 00083 ptr += sprintf(ptr, " %X", type); 00084 } 00085 if(checksum != -1) 00086 { 00087 ptr += sprintf(ptr, " %X", (unsigned int)checksum); 00088 } 00089 } 00090 00091 CHECK_OTHER_COMMAND_EXECUTING(); 00092 00093 result = wiconnect->sendCommand(reader, user, (const char *)cmdBuffer); 00094 00095 CHECK_CLEANUP_COMMAND(); 00096 00097 return result; 00098 } 00099 00100 00101 /*************************************************************************************************/ 00102 WiconnectResult FileInterface::deleteFile(const char *name) 00103 { 00104 WiconnectResult result; 00105 00106 CHECK_OTHER_COMMAND_EXECUTING(); 00107 00108 result = wiconnect->sendCommand("fde %s", name); 00109 00110 CHECK_CLEANUP_COMMAND(); 00111 00112 return result; 00113 } 00114 00115 /*************************************************************************************************/ 00116 WiconnectResult FileInterface::deleteFile(const WiconnectFile &file) 00117 { 00118 return deleteFile(file.getName()); 00119 } 00120 00121 00122 /*************************************************************************************************/ 00123 const char* FileInterface::fileVersionIntToStr(uint32_t version, bool verbose, char *buffer) 00124 { 00125 SET_STR_BUFFER(buffer, 32); 00126 const char *fmt = verbose ? "%u.%u.%u.%u" : "%u.%u.%u"; 00127 sprintf(ptr, fmt, FILE_VERSION_ARGS(version)); 00128 return ptr; 00129 } 00130 00131 /*************************************************************************************************/ 00132 bool FileInterface::fileVersionStrToInt(const char *versionStr, uint32_t *versionIntPtr) 00133 { 00134 const uint8_t offsets[] = {27, 21, 8, 0}; 00135 char buffer[18]; 00136 char *tok, *ptr = buffer; 00137 uint32_t version = 0; 00138 00139 strcpy(buffer, versionStr); 00140 00141 for(int i = 0; i < 4 && (tok = strtok(ptr, ".")) != NULL; ++i) 00142 { 00143 char *end; 00144 const uint32_t value = strtol(tok, &end, 10); 00145 if(*end != 0) 00146 { 00147 return false; 00148 } 00149 version |= (value << offsets[i]); 00150 ptr = NULL; 00151 } 00152 00153 *versionIntPtr = version; 00154 00155 return true; 00156 } 00157 00158 /*************************************************************************************************/ 00159 const char* FileInterface::fileTypeToStr(FileType type) 00160 { 00161 switch(type) 00162 { 00163 case FILE_TYPE_UPGRADE_APP: 00164 return "Upgrade App"; 00165 case FILE_TYPE_WIFI_FW: 00166 return "Wifi Firmware"; 00167 case FILE_TYPE_REGULAR_APP: 00168 return "Regular App"; 00169 case FILE_TYPE_TEMPORY: 00170 return "Temporary"; 00171 case FILE_TYPE_GPIO_CONFIG: 00172 return "GPIO Default Configuration"; 00173 case FILE_TYPE_COMMAND_HELP: 00174 return "Command Help"; 00175 case FILE_TYPE_SDC_CAPS: 00176 return "goHACK.me Capabilities"; 00177 case FILE_TYPE_SETUP_SCRIPT: 00178 return "Setup Script"; 00179 case FILE_TYPE_MISC_FIX_LEN: 00180 return "Miscellaneous"; 00181 default: 00182 if(type >= FILE_TYPE_USER_RANGE_START && type <= FILE_TYPE_USER_RANGE_END) 00183 return "User"; 00184 else 00185 return "Unknown"; 00186 } 00187 } 00188 00189 /*************************************************************************************************/ 00190 const char* FileInterface::fileFlagsToStr(FileFlags flags, char *buffer) 00191 { 00192 SET_STR_BUFFER(buffer, 64); 00193 char *buf = ptr; 00194 00195 static const char* const flag_strings[] = { 00196 "Valid", 00197 "Executable", 00198 "Encrypted", 00199 "Internal", 00200 "Bootable", 00201 "User", 00202 "Essential", 00203 }; 00204 00205 int i = 0; 00206 *ptr = 0; 00207 00208 for(uint16_t f = flags; f != 0 && i < 7; f >>= 1, ++i) 00209 { 00210 if(f & 0x0001) 00211 { 00212 ptr += sprintf(ptr, "%s,", flag_strings[i]); 00213 } 00214 } 00215 00216 if(ptr == buffer) 00217 { 00218 strcpy(buffer, "None"); 00219 } 00220 else 00221 { 00222 *(ptr-1) = 0; 00223 } 00224 00225 return buf; 00226 }
Generated on Tue Jul 12 2022 17:35:58 by 1.7.2