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:
dan_ackme
Date:
Wed Aug 13 04:41:04 2014 -0700
Revision:
16:7f1d6d359787
Parent:
3:2dc2592bae5e
Child:
17:7268f365676b
updated documentation and copyright

Who changed what in which revision?

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