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

Committer:
aymangrais
Date:
Mon Sep 28 03:38:43 2015 +0000
Revision:
42:8ffb253b09e7
Parent:
29:b6af04b77a56
increase ota timeout to be 5 seconds (instead of 1.5 sec)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 29:b6af04b77a56 1 /**
dan_ackme 29:b6af04b77a56 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 29:b6af04b77a56 3 *
dan_ackme 29:b6af04b77a56 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 29:b6af04b77a56 5 * All rights reserved.
dan_ackme 29:b6af04b77a56 6 *
dan_ackme 29:b6af04b77a56 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 29:b6af04b77a56 8 * are permitted provided that the following conditions are met:
dan_ackme 29:b6af04b77a56 9 *
dan_ackme 29:b6af04b77a56 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 29:b6af04b77a56 11 * this list of conditions and the following disclaimer.
dan_ackme 29:b6af04b77a56 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 29:b6af04b77a56 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 29:b6af04b77a56 14 * and/or other materials provided with the distribution.
dan_ackme 29:b6af04b77a56 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 29:b6af04b77a56 16 * derived from this software without specific prior written permission.
dan_ackme 29:b6af04b77a56 17 *
dan_ackme 29:b6af04b77a56 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 29:b6af04b77a56 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 29:b6af04b77a56 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 29:b6af04b77a56 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 29:b6af04b77a56 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 29:b6af04b77a56 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 29:b6af04b77a56 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 29:b6af04b77a56 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 29:b6af04b77a56 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 29:b6af04b77a56 27 * OF SUCH DAMAGE.
dan_ackme 29:b6af04b77a56 28 */
dan_ackme 29:b6af04b77a56 29
dan_ackme 29:b6af04b77a56 30 #include "Wiconnect.h"
dan_ackme 29:b6af04b77a56 31 #include "internal/common.h"
dan_ackme 29:b6af04b77a56 32 #include "api/StringUtil.h"
dan_ackme 29:b6af04b77a56 33
dan_ackme 29:b6af04b77a56 34
dan_ackme 29:b6af04b77a56 35 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 36 FileInterface::FileInterface(Wiconnect *wiconnect_)
dan_ackme 29:b6af04b77a56 37 {
dan_ackme 29:b6af04b77a56 38 wiconnect = wiconnect_;
dan_ackme 29:b6af04b77a56 39 }
dan_ackme 29:b6af04b77a56 40
dan_ackme 29:b6af04b77a56 41 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 42 WiconnectResult FileInterface::openFile(WiconnectFile &file, const char *name)
dan_ackme 29:b6af04b77a56 43 {
dan_ackme 29:b6af04b77a56 44 WiconnectResult result;
dan_ackme 29:b6af04b77a56 45
dan_ackme 29:b6af04b77a56 46 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 47
dan_ackme 29:b6af04b77a56 48 if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand("fop %s", name)))
dan_ackme 29:b6af04b77a56 49 {
dan_ackme 29:b6af04b77a56 50 int32_t handle;
dan_ackme 29:b6af04b77a56 51 if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&handle)))
dan_ackme 29:b6af04b77a56 52 {
dan_ackme 29:b6af04b77a56 53 file.openForRead(handle, name);
dan_ackme 29:b6af04b77a56 54 }
dan_ackme 29:b6af04b77a56 55 }
dan_ackme 29:b6af04b77a56 56
dan_ackme 29:b6af04b77a56 57 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 58
dan_ackme 29:b6af04b77a56 59 return result;
dan_ackme 29:b6af04b77a56 60 }
dan_ackme 29:b6af04b77a56 61
dan_ackme 29:b6af04b77a56 62 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 63 WiconnectResult FileInterface::createFile(const ReaderFunc &reader, void *user, const char *name, uint32_t size, uint32_t version, FileType type, bool isEssential, int32_t checksum)
dan_ackme 29:b6af04b77a56 64 {
dan_ackme 29:b6af04b77a56 65 WiconnectResult result;
dan_ackme 29:b6af04b77a56 66 char *cmdBuffer = wiconnect->internalBuffer;
dan_ackme 29:b6af04b77a56 67
dan_ackme 29:b6af04b77a56 68 if(WICONNECT_IS_IDLE())
dan_ackme 29:b6af04b77a56 69 {
dan_ackme 29:b6af04b77a56 70 char *ptr = cmdBuffer;
dan_ackme 29:b6af04b77a56 71
dan_ackme 29:b6af04b77a56 72 ptr += sprintf(cmdBuffer, "fcr %s%s %u", isEssential ? "-e " : "", name, (unsigned int)size);
dan_ackme 29:b6af04b77a56 73
dan_ackme 29:b6af04b77a56 74 if(version != 0)
dan_ackme 29:b6af04b77a56 75 {
dan_ackme 29:b6af04b77a56 76 *ptr = ' ';
dan_ackme 29:b6af04b77a56 77 ++ptr;
dan_ackme 29:b6af04b77a56 78 FileInterface::fileVersionIntToStr(version, true, ptr);
dan_ackme 29:b6af04b77a56 79 ptr = ptr + strlen(ptr);
dan_ackme 29:b6af04b77a56 80 }
dan_ackme 29:b6af04b77a56 81 if(type != FILE_TYPE_ANY)
dan_ackme 29:b6af04b77a56 82 {
dan_ackme 29:b6af04b77a56 83 ptr += sprintf(ptr, " %X", type);
dan_ackme 29:b6af04b77a56 84 }
dan_ackme 29:b6af04b77a56 85 if(checksum != -1)
dan_ackme 29:b6af04b77a56 86 {
dan_ackme 29:b6af04b77a56 87 ptr += sprintf(ptr, " %X", (unsigned int)checksum);
dan_ackme 29:b6af04b77a56 88 }
dan_ackme 29:b6af04b77a56 89 }
dan_ackme 29:b6af04b77a56 90
dan_ackme 29:b6af04b77a56 91 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 92
dan_ackme 29:b6af04b77a56 93 result = wiconnect->sendCommand(reader, user, (const char *)cmdBuffer);
dan_ackme 29:b6af04b77a56 94
dan_ackme 29:b6af04b77a56 95 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 96
dan_ackme 29:b6af04b77a56 97 return result;
dan_ackme 29:b6af04b77a56 98 }
dan_ackme 29:b6af04b77a56 99
dan_ackme 29:b6af04b77a56 100
dan_ackme 29:b6af04b77a56 101 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 102 WiconnectResult FileInterface::deleteFile(const char *name)
dan_ackme 29:b6af04b77a56 103 {
dan_ackme 29:b6af04b77a56 104 WiconnectResult result;
dan_ackme 29:b6af04b77a56 105
dan_ackme 29:b6af04b77a56 106 CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 29:b6af04b77a56 107
dan_ackme 29:b6af04b77a56 108 result = wiconnect->sendCommand("fde %s", name);
dan_ackme 29:b6af04b77a56 109
dan_ackme 29:b6af04b77a56 110 CHECK_CLEANUP_COMMAND();
dan_ackme 29:b6af04b77a56 111
dan_ackme 29:b6af04b77a56 112 return result;
dan_ackme 29:b6af04b77a56 113 }
dan_ackme 29:b6af04b77a56 114
dan_ackme 29:b6af04b77a56 115 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 116 WiconnectResult FileInterface::deleteFile(const WiconnectFile &file)
dan_ackme 29:b6af04b77a56 117 {
dan_ackme 29:b6af04b77a56 118 return deleteFile(file.getName());
dan_ackme 29:b6af04b77a56 119 }
dan_ackme 29:b6af04b77a56 120
dan_ackme 29:b6af04b77a56 121
dan_ackme 29:b6af04b77a56 122 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 123 const char* FileInterface::fileVersionIntToStr(uint32_t version, bool verbose, char *buffer)
dan_ackme 29:b6af04b77a56 124 {
dan_ackme 29:b6af04b77a56 125 SET_STR_BUFFER(buffer, 32);
dan_ackme 29:b6af04b77a56 126 const char *fmt = verbose ? "%u.%u.%u.%u" : "%u.%u.%u";
dan_ackme 29:b6af04b77a56 127 sprintf(ptr, fmt, FILE_VERSION_ARGS(version));
dan_ackme 29:b6af04b77a56 128 return ptr;
dan_ackme 29:b6af04b77a56 129 }
dan_ackme 29:b6af04b77a56 130
dan_ackme 29:b6af04b77a56 131 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 132 bool FileInterface::fileVersionStrToInt(const char *versionStr, uint32_t *versionIntPtr)
dan_ackme 29:b6af04b77a56 133 {
dan_ackme 29:b6af04b77a56 134 const uint8_t offsets[] = {27, 21, 8, 0};
dan_ackme 29:b6af04b77a56 135 char buffer[18];
dan_ackme 29:b6af04b77a56 136 char *tok, *ptr = buffer;
dan_ackme 29:b6af04b77a56 137 uint32_t version = 0;
dan_ackme 29:b6af04b77a56 138
dan_ackme 29:b6af04b77a56 139 strcpy(buffer, versionStr);
dan_ackme 29:b6af04b77a56 140
dan_ackme 29:b6af04b77a56 141 for(int i = 0; i < 4 && (tok = strtok(ptr, ".")) != NULL; ++i)
dan_ackme 29:b6af04b77a56 142 {
dan_ackme 29:b6af04b77a56 143 char *end;
dan_ackme 29:b6af04b77a56 144 const uint32_t value = strtol(tok, &end, 10);
dan_ackme 29:b6af04b77a56 145 if(*end != 0)
dan_ackme 29:b6af04b77a56 146 {
dan_ackme 29:b6af04b77a56 147 return false;
dan_ackme 29:b6af04b77a56 148 }
dan_ackme 29:b6af04b77a56 149 version |= (value << offsets[i]);
dan_ackme 29:b6af04b77a56 150 ptr = NULL;
dan_ackme 29:b6af04b77a56 151 }
dan_ackme 29:b6af04b77a56 152
dan_ackme 29:b6af04b77a56 153 *versionIntPtr = version;
dan_ackme 29:b6af04b77a56 154
dan_ackme 29:b6af04b77a56 155 return true;
dan_ackme 29:b6af04b77a56 156 }
dan_ackme 29:b6af04b77a56 157
dan_ackme 29:b6af04b77a56 158 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 159 const char* FileInterface::fileTypeToStr(FileType type)
dan_ackme 29:b6af04b77a56 160 {
dan_ackme 29:b6af04b77a56 161 switch(type)
dan_ackme 29:b6af04b77a56 162 {
dan_ackme 29:b6af04b77a56 163 case FILE_TYPE_UPGRADE_APP:
dan_ackme 29:b6af04b77a56 164 return "Upgrade App";
dan_ackme 29:b6af04b77a56 165 case FILE_TYPE_WIFI_FW:
dan_ackme 29:b6af04b77a56 166 return "Wifi Firmware";
dan_ackme 29:b6af04b77a56 167 case FILE_TYPE_REGULAR_APP:
dan_ackme 29:b6af04b77a56 168 return "Regular App";
dan_ackme 29:b6af04b77a56 169 case FILE_TYPE_TEMPORY:
dan_ackme 29:b6af04b77a56 170 return "Temporary";
dan_ackme 29:b6af04b77a56 171 case FILE_TYPE_GPIO_CONFIG:
dan_ackme 29:b6af04b77a56 172 return "GPIO Default Configuration";
dan_ackme 29:b6af04b77a56 173 case FILE_TYPE_COMMAND_HELP:
dan_ackme 29:b6af04b77a56 174 return "Command Help";
dan_ackme 29:b6af04b77a56 175 case FILE_TYPE_SDC_CAPS:
dan_ackme 29:b6af04b77a56 176 return "goHACK.me Capabilities";
dan_ackme 29:b6af04b77a56 177 case FILE_TYPE_SETUP_SCRIPT:
dan_ackme 29:b6af04b77a56 178 return "Setup Script";
dan_ackme 29:b6af04b77a56 179 case FILE_TYPE_MISC_FIX_LEN:
dan_ackme 29:b6af04b77a56 180 return "Miscellaneous";
dan_ackme 29:b6af04b77a56 181 default:
dan_ackme 29:b6af04b77a56 182 if(type >= FILE_TYPE_USER_RANGE_START && type <= FILE_TYPE_USER_RANGE_END)
dan_ackme 29:b6af04b77a56 183 return "User";
dan_ackme 29:b6af04b77a56 184 else
dan_ackme 29:b6af04b77a56 185 return "Unknown";
dan_ackme 29:b6af04b77a56 186 }
dan_ackme 29:b6af04b77a56 187 }
dan_ackme 29:b6af04b77a56 188
dan_ackme 29:b6af04b77a56 189 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 190 const char* FileInterface::fileFlagsToStr(FileFlags flags, char *buffer)
dan_ackme 29:b6af04b77a56 191 {
dan_ackme 29:b6af04b77a56 192 SET_STR_BUFFER(buffer, 64);
dan_ackme 29:b6af04b77a56 193 char *buf = ptr;
dan_ackme 29:b6af04b77a56 194
dan_ackme 29:b6af04b77a56 195 static const char* const flag_strings[] = {
dan_ackme 29:b6af04b77a56 196 "Valid",
dan_ackme 29:b6af04b77a56 197 "Executable",
dan_ackme 29:b6af04b77a56 198 "Encrypted",
dan_ackme 29:b6af04b77a56 199 "Internal",
dan_ackme 29:b6af04b77a56 200 "Bootable",
dan_ackme 29:b6af04b77a56 201 "User",
dan_ackme 29:b6af04b77a56 202 "Essential",
dan_ackme 29:b6af04b77a56 203 };
dan_ackme 29:b6af04b77a56 204
dan_ackme 29:b6af04b77a56 205 int i = 0;
dan_ackme 29:b6af04b77a56 206 *ptr = 0;
dan_ackme 29:b6af04b77a56 207
dan_ackme 29:b6af04b77a56 208 for(uint16_t f = flags; f != 0 && i < 7; f >>= 1, ++i)
dan_ackme 29:b6af04b77a56 209 {
dan_ackme 29:b6af04b77a56 210 if(f & 0x0001)
dan_ackme 29:b6af04b77a56 211 {
dan_ackme 29:b6af04b77a56 212 ptr += sprintf(ptr, "%s,", flag_strings[i]);
dan_ackme 29:b6af04b77a56 213 }
dan_ackme 29:b6af04b77a56 214 }
dan_ackme 29:b6af04b77a56 215
dan_ackme 29:b6af04b77a56 216 if(ptr == buffer)
dan_ackme 29:b6af04b77a56 217 {
dan_ackme 29:b6af04b77a56 218 strcpy(buffer, "None");
dan_ackme 29:b6af04b77a56 219 }
dan_ackme 29:b6af04b77a56 220 else
dan_ackme 29:b6af04b77a56 221 {
dan_ackme 29:b6af04b77a56 222 *(ptr-1) = 0;
dan_ackme 29:b6af04b77a56 223 }
dan_ackme 29:b6af04b77a56 224
dan_ackme 29:b6af04b77a56 225 return buf;
dan_ackme 29:b6af04b77a56 226 }