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
Wiconnect.cpp@36:a30cbf4bdb13, 2014-11-27 (annotated)
- Committer:
- dan_ackme
- Date:
- Thu Nov 27 00:46:50 2014 -0800
- Revision:
- 36:a30cbf4bdb13
- Parent:
- 35:15725177aa60
- Child:
- 37:5ee74d72efe4
Fix compiler warning
Who changed what in which revision?
User | Revision | Line number | New 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 | #include <stdio.h> |
dan_ackme | 29:b6af04b77a56 | 30 | #include <stdarg.h> |
dan_ackme | 29:b6af04b77a56 | 31 | #include <string.h> |
dan_ackme | 29:b6af04b77a56 | 32 | |
dan_ackme | 29:b6af04b77a56 | 33 | #include "api/WiconnectInterface.h" |
dan_ackme | 29:b6af04b77a56 | 34 | #include "internal/common.h" |
dan_ackme | 29:b6af04b77a56 | 35 | #include "internal/CommandCommon.h" |
dan_ackme | 29:b6af04b77a56 | 36 | |
dan_ackme | 29:b6af04b77a56 | 37 | |
dan_ackme | 29:b6af04b77a56 | 38 | |
dan_ackme | 29:b6af04b77a56 | 39 | |
dan_ackme | 29:b6af04b77a56 | 40 | using namespace wiconnect; |
dan_ackme | 29:b6af04b77a56 | 41 | |
dan_ackme | 29:b6af04b77a56 | 42 | |
dan_ackme | 29:b6af04b77a56 | 43 | |
dan_ackme | 29:b6af04b77a56 | 44 | |
dan_ackme | 29:b6af04b77a56 | 45 | |
dan_ackme | 29:b6af04b77a56 | 46 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 47 | #define MALLOC_ARGS , void* (*mallocPtr)(size_t), void (*freePtr)(void*) |
dan_ackme | 29:b6af04b77a56 | 48 | #define MALLOC_CONSTRUCTORS _malloc(mallocPtr), _free(freePtr), |
dan_ackme | 29:b6af04b77a56 | 49 | #else |
dan_ackme | 29:b6af04b77a56 | 50 | #define MALLOC_ARGS |
dan_ackme | 29:b6af04b77a56 | 51 | #define MALLOC_CONSTRUCTORS |
dan_ackme | 29:b6af04b77a56 | 52 | #endif |
dan_ackme | 29:b6af04b77a56 | 53 | |
dan_ackme | 29:b6af04b77a56 | 54 | |
dan_ackme | 29:b6af04b77a56 | 55 | static Wiconnect* instance = NULL; |
dan_ackme | 29:b6af04b77a56 | 56 | |
dan_ackme | 29:b6af04b77a56 | 57 | |
dan_ackme | 29:b6af04b77a56 | 58 | |
dan_ackme | 29:b6af04b77a56 | 59 | |
dan_ackme | 29:b6af04b77a56 | 60 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 61 | void Wiconnect::prepare(void *internalBuffer_, int internalBufferSize_, bool nonBlocking_) |
dan_ackme | 29:b6af04b77a56 | 62 | { |
dan_ackme | 29:b6af04b77a56 | 63 | instance = this; |
dan_ackme | 29:b6af04b77a56 | 64 | internalBufferAlloc = false; |
dan_ackme | 29:b6af04b77a56 | 65 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 66 | if(internalBufferSize_ > 0 && internalBuffer_ == NULL) |
dan_ackme | 29:b6af04b77a56 | 67 | { |
dan_ackme | 29:b6af04b77a56 | 68 | wiconnect_assert(this, "Wiconnect(), malloc not defined", _malloc != NULL); |
dan_ackme | 29:b6af04b77a56 | 69 | internalBuffer = (char*)_malloc(internalBufferSize_); |
dan_ackme | 29:b6af04b77a56 | 70 | internalBufferAlloc = true; |
dan_ackme | 29:b6af04b77a56 | 71 | } |
dan_ackme | 29:b6af04b77a56 | 72 | else |
dan_ackme | 29:b6af04b77a56 | 73 | #endif |
dan_ackme | 29:b6af04b77a56 | 74 | { |
dan_ackme | 29:b6af04b77a56 | 75 | internalBuffer = (char*)internalBuffer_; |
dan_ackme | 29:b6af04b77a56 | 76 | } |
dan_ackme | 29:b6af04b77a56 | 77 | |
dan_ackme | 29:b6af04b77a56 | 78 | internalProcessingState = 0; |
dan_ackme | 29:b6af04b77a56 | 79 | currentCommandId = NULL; |
dan_ackme | 29:b6af04b77a56 | 80 | internalBufferSize = internalBufferSize_;; |
dan_ackme | 29:b6af04b77a56 | 81 | nonBlocking = nonBlocking_; |
dan_ackme | 29:b6af04b77a56 | 82 | commandExecuting = false; |
dan_ackme | 35:15725177aa60 | 83 | initialized = false; |
dan_ackme | 35:15725177aa60 | 84 | needUpdate = false; |
dan_ackme | 29:b6af04b77a56 | 85 | pinToGpioMapper = NULL; |
dan_ackme | 29:b6af04b77a56 | 86 | defaultTimeoutMs = WICONNECT_DEFAULT_TIMEOUT; |
dan_ackme | 29:b6af04b77a56 | 87 | |
dan_ackme | 29:b6af04b77a56 | 88 | memset(commandContext, 0, sizeof(commandContext)); |
dan_ackme | 29:b6af04b77a56 | 89 | |
dan_ackme | 29:b6af04b77a56 | 90 | #ifdef WICONNECT_ASYNC_TIMER_ENABLED |
dan_ackme | 29:b6af04b77a56 | 91 | commandProcessingPeriod = WICONNECT_DEFAULT_COMMAND_PROCESSING_PERIOD; |
dan_ackme | 29:b6af04b77a56 | 92 | currentQueuedCommand = NULL; |
dan_ackme | 29:b6af04b77a56 | 93 | #endif |
dan_ackme | 29:b6af04b77a56 | 94 | } |
dan_ackme | 29:b6af04b77a56 | 95 | |
dan_ackme | 29:b6af04b77a56 | 96 | |
dan_ackme | 29:b6af04b77a56 | 97 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 98 | Wiconnect::Wiconnect(const SerialConfig &serialConfig, int internalBufferSize, void *internalBuffer, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) : |
dan_ackme | 29:b6af04b77a56 | 99 | NetworkInterface(this), SocketInterface(this), FileInterface(this), GhmInterface(this), |
dan_ackme | 29:b6af04b77a56 | 100 | MALLOC_CONSTRUCTORS serial(serialConfig, this), resetGpio(reset), wakeGpio(wake) |
dan_ackme | 29:b6af04b77a56 | 101 | { |
dan_ackme | 29:b6af04b77a56 | 102 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 103 | wiconnect_assert(this, "Wiconnect(), bad malloc/free", (mallocPtr == NULL && freePtr == NULL) || (mallocPtr != NULL && freePtr != NULL)); |
dan_ackme | 29:b6af04b77a56 | 104 | #endif |
dan_ackme | 29:b6af04b77a56 | 105 | prepare(internalBuffer, internalBufferSize, nonBlocking); |
dan_ackme | 29:b6af04b77a56 | 106 | } |
dan_ackme | 29:b6af04b77a56 | 107 | |
dan_ackme | 29:b6af04b77a56 | 108 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 109 | Wiconnect::Wiconnect(const SerialConfig &serialConfig, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) : |
dan_ackme | 29:b6af04b77a56 | 110 | NetworkInterface(this), SocketInterface(this), FileInterface(this), GhmInterface(this), |
dan_ackme | 29:b6af04b77a56 | 111 | MALLOC_CONSTRUCTORS serial(serialConfig, this), resetGpio(reset), wakeGpio(wake) |
dan_ackme | 29:b6af04b77a56 | 112 | { |
dan_ackme | 29:b6af04b77a56 | 113 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 114 | wiconnect_assert(this, "Wiconnect(), bad malloc/free", (mallocPtr == NULL && freePtr == NULL) || (mallocPtr != NULL && freePtr != NULL)); |
dan_ackme | 29:b6af04b77a56 | 115 | #endif |
dan_ackme | 29:b6af04b77a56 | 116 | prepare(NULL, 0, nonBlocking); |
dan_ackme | 29:b6af04b77a56 | 117 | } |
dan_ackme | 29:b6af04b77a56 | 118 | |
dan_ackme | 29:b6af04b77a56 | 119 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 120 | Wiconnect::~Wiconnect() |
dan_ackme | 29:b6af04b77a56 | 121 | { |
dan_ackme | 29:b6af04b77a56 | 122 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 123 | if(internalBufferAlloc) |
dan_ackme | 29:b6af04b77a56 | 124 | { |
dan_ackme | 29:b6af04b77a56 | 125 | _free(internalBuffer); |
dan_ackme | 29:b6af04b77a56 | 126 | } |
dan_ackme | 29:b6af04b77a56 | 127 | #endif |
dan_ackme | 29:b6af04b77a56 | 128 | } |
dan_ackme | 29:b6af04b77a56 | 129 | |
dan_ackme | 29:b6af04b77a56 | 130 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 131 | WiconnectResult Wiconnect::init(bool bringNetworkUp) |
dan_ackme | 29:b6af04b77a56 | 132 | { |
dan_ackme | 29:b6af04b77a56 | 133 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 134 | int retries; |
dan_ackme | 29:b6af04b77a56 | 135 | bool configuredBus = false; |
dan_ackme | 29:b6af04b77a56 | 136 | bool savedNonBlocking = nonBlocking; |
dan_ackme | 29:b6af04b77a56 | 137 | |
dan_ackme | 29:b6af04b77a56 | 138 | DEBUG_INFO("Initializing wiconnect"); |
dan_ackme | 29:b6af04b77a56 | 139 | |
dan_ackme | 29:b6af04b77a56 | 140 | serial.initialize(); |
dan_ackme | 29:b6af04b77a56 | 141 | |
dan_ackme | 29:b6af04b77a56 | 142 | if(WICONNECT_FAILED(result, reset())) |
dan_ackme | 29:b6af04b77a56 | 143 | { |
dan_ackme | 29:b6af04b77a56 | 144 | return result; |
dan_ackme | 29:b6af04b77a56 | 145 | } |
dan_ackme | 29:b6af04b77a56 | 146 | |
dan_ackme | 29:b6af04b77a56 | 147 | delayMs(1000); |
dan_ackme | 29:b6af04b77a56 | 148 | |
dan_ackme | 29:b6af04b77a56 | 149 | initialized = true; |
dan_ackme | 29:b6af04b77a56 | 150 | nonBlocking = false; |
dan_ackme | 29:b6af04b77a56 | 151 | |
dan_ackme | 29:b6af04b77a56 | 152 | |
dan_ackme | 29:b6af04b77a56 | 153 | loop: |
dan_ackme | 29:b6af04b77a56 | 154 | for(retries = 3; retries > 0; --retries) |
dan_ackme | 29:b6af04b77a56 | 155 | { |
dan_ackme | 29:b6af04b77a56 | 156 | result = sendCommand(1000, CMD_SET_SYSTEM_COMMAND_MODE, "machine"); |
dan_ackme | 29:b6af04b77a56 | 157 | if(result != WICONNECT_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 158 | { |
dan_ackme | 29:b6af04b77a56 | 159 | delayMs(100); |
dan_ackme | 29:b6af04b77a56 | 160 | } |
dan_ackme | 29:b6af04b77a56 | 161 | else |
dan_ackme | 29:b6af04b77a56 | 162 | { |
dan_ackme | 29:b6af04b77a56 | 163 | break; |
dan_ackme | 29:b6af04b77a56 | 164 | } |
dan_ackme | 29:b6af04b77a56 | 165 | } |
dan_ackme | 29:b6af04b77a56 | 166 | |
dan_ackme | 29:b6af04b77a56 | 167 | if(result != WICONNECT_SUCCESS && !configuredBus) |
dan_ackme | 29:b6af04b77a56 | 168 | { |
dan_ackme | 29:b6af04b77a56 | 169 | configuredBus = true; |
dan_ackme | 29:b6af04b77a56 | 170 | if(configureModuleDataBus()) |
dan_ackme | 29:b6af04b77a56 | 171 | { |
dan_ackme | 29:b6af04b77a56 | 172 | goto loop; |
dan_ackme | 29:b6af04b77a56 | 173 | } |
dan_ackme | 29:b6af04b77a56 | 174 | } |
dan_ackme | 29:b6af04b77a56 | 175 | |
dan_ackme | 29:b6af04b77a56 | 176 | if(result == WICONNECT_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 177 | { |
dan_ackme | 35:15725177aa60 | 178 | if(WICONNECT_SUCCEEDED(result, getVersion())) |
dan_ackme | 35:15725177aa60 | 179 | { |
dan_ackme | 35:15725177aa60 | 180 | const uint32_t version = Wiconnect::wiconnectVersionToInt(this->internalBuffer); |
dan_ackme | 35:15725177aa60 | 181 | if(version < WICONNECT_MINIMUM_VERSION) |
dan_ackme | 35:15725177aa60 | 182 | { |
dan_ackme | 35:15725177aa60 | 183 | needUpdate = true; |
dan_ackme | 35:15725177aa60 | 184 | result = WICONNECT_FIRMWARE_OUTDATED; |
dan_ackme | 35:15725177aa60 | 185 | } |
dan_ackme | 35:15725177aa60 | 186 | } |
dan_ackme | 35:15725177aa60 | 187 | } |
dan_ackme | 35:15725177aa60 | 188 | if(result == WICONNECT_SUCCESS) |
dan_ackme | 35:15725177aa60 | 189 | { |
dan_ackme | 29:b6af04b77a56 | 190 | sendCommand("set stream.auto_close 0"); |
dan_ackme | 29:b6af04b77a56 | 191 | } |
dan_ackme | 29:b6af04b77a56 | 192 | if(result == WICONNECT_SUCCESS && bringNetworkUp) |
dan_ackme | 29:b6af04b77a56 | 193 | { |
dan_ackme | 29:b6af04b77a56 | 194 | sendCommand(15000, "ping -g"); |
dan_ackme | 29:b6af04b77a56 | 195 | } |
dan_ackme | 29:b6af04b77a56 | 196 | |
dan_ackme | 29:b6af04b77a56 | 197 | nonBlocking = savedNonBlocking; |
dan_ackme | 29:b6af04b77a56 | 198 | if(result != WICONNECT_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 199 | { |
dan_ackme | 29:b6af04b77a56 | 200 | initialized = false; |
dan_ackme | 29:b6af04b77a56 | 201 | } |
dan_ackme | 29:b6af04b77a56 | 202 | |
dan_ackme | 29:b6af04b77a56 | 203 | |
dan_ackme | 29:b6af04b77a56 | 204 | return result; |
dan_ackme | 29:b6af04b77a56 | 205 | } |
dan_ackme | 29:b6af04b77a56 | 206 | |
dan_ackme | 29:b6af04b77a56 | 207 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 208 | void Wiconnect::deinit(void) |
dan_ackme | 29:b6af04b77a56 | 209 | { |
dan_ackme | 29:b6af04b77a56 | 210 | initialized = false; |
dan_ackme | 29:b6af04b77a56 | 211 | } |
dan_ackme | 29:b6af04b77a56 | 212 | |
dan_ackme | 29:b6af04b77a56 | 213 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 214 | Wiconnect* Wiconnect::getInstance() |
dan_ackme | 29:b6af04b77a56 | 215 | { |
dan_ackme | 29:b6af04b77a56 | 216 | return instance; |
dan_ackme | 29:b6af04b77a56 | 217 | } |
dan_ackme | 29:b6af04b77a56 | 218 | |
dan_ackme | 29:b6af04b77a56 | 219 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 220 | bool Wiconnect::isInitialized() |
dan_ackme | 29:b6af04b77a56 | 221 | { |
dan_ackme | 29:b6af04b77a56 | 222 | return initialized; |
dan_ackme | 35:15725177aa60 | 223 | } |
dan_ackme | 35:15725177aa60 | 224 | |
dan_ackme | 35:15725177aa60 | 225 | /*************************************************************************************************/ |
dan_ackme | 35:15725177aa60 | 226 | bool Wiconnect::updateRequired() |
dan_ackme | 35:15725177aa60 | 227 | { |
dan_ackme | 35:15725177aa60 | 228 | return needUpdate; |
dan_ackme | 29:b6af04b77a56 | 229 | } |
dan_ackme | 29:b6af04b77a56 | 230 | |
dan_ackme | 29:b6af04b77a56 | 231 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 232 | WiconnectResult Wiconnect::reset() |
dan_ackme | 29:b6af04b77a56 | 233 | { |
dan_ackme | 29:b6af04b77a56 | 234 | resetGpio = 0; |
dan_ackme | 29:b6af04b77a56 | 235 | delayMs(10); |
dan_ackme | 29:b6af04b77a56 | 236 | resetGpio = 1; |
dan_ackme | 29:b6af04b77a56 | 237 | delayMs(1000); |
dan_ackme | 29:b6af04b77a56 | 238 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 239 | } |
dan_ackme | 29:b6af04b77a56 | 240 | |
dan_ackme | 29:b6af04b77a56 | 241 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 242 | WiconnectResult Wiconnect::wakeup() |
dan_ackme | 29:b6af04b77a56 | 243 | { |
dan_ackme | 29:b6af04b77a56 | 244 | wakeGpio = 1; |
dan_ackme | 29:b6af04b77a56 | 245 | delayMs(1); |
dan_ackme | 29:b6af04b77a56 | 246 | wakeGpio = 0; |
dan_ackme | 29:b6af04b77a56 | 247 | return WICONNECT_SUCCESS; |
dan_ackme | 29:b6af04b77a56 | 248 | } |
dan_ackme | 29:b6af04b77a56 | 249 | |
dan_ackme | 29:b6af04b77a56 | 250 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 251 | void Wiconnect::flush(int delayMs) |
dan_ackme | 29:b6af04b77a56 | 252 | { |
dan_ackme | 35:15725177aa60 | 253 | if(delayMs != 0) |
dan_ackme | 35:15725177aa60 | 254 | { |
dan_ackme | 35:15725177aa60 | 255 | serial.write("\r\n\r\n", 4, 0); |
dan_ackme | 35:15725177aa60 | 256 | } |
dan_ackme | 35:15725177aa60 | 257 | delayMs(delayMs); |
dan_ackme | 35:15725177aa60 | 258 | serial.flush(); |
dan_ackme | 29:b6af04b77a56 | 259 | } |
dan_ackme | 29:b6af04b77a56 | 260 | |
dan_ackme | 29:b6af04b77a56 | 261 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 262 | void Wiconnect::setPinToGpioMapper(PinToGpioMapper mapper) |
dan_ackme | 29:b6af04b77a56 | 263 | { |
dan_ackme | 29:b6af04b77a56 | 264 | pinToGpioMapper = mapper; |
dan_ackme | 29:b6af04b77a56 | 265 | } |
dan_ackme | 29:b6af04b77a56 | 266 | |
dan_ackme | 29:b6af04b77a56 | 267 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 268 | WiconnectResult Wiconnect::getVersion(char *versionBuffer, int versionBufferSize, const Callback &completeCallback) |
dan_ackme | 29:b6af04b77a56 | 269 | { |
dan_ackme | 29:b6af04b77a56 | 270 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 271 | |
dan_ackme | 29:b6af04b77a56 | 272 | if(versionBuffer != NULL && versionBufferSize == 0) |
dan_ackme | 29:b6af04b77a56 | 273 | { |
dan_ackme | 29:b6af04b77a56 | 274 | return WICONNECT_BAD_ARG; |
dan_ackme | 29:b6af04b77a56 | 275 | } |
dan_ackme | 29:b6af04b77a56 | 276 | |
dan_ackme | 29:b6af04b77a56 | 277 | _CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 278 | |
dan_ackme | 29:b6af04b77a56 | 279 | if(versionBuffer == NULL) |
dan_ackme | 29:b6af04b77a56 | 280 | { |
dan_ackme | 29:b6af04b77a56 | 281 | result = sendCommand(completeCallback, CMD_GET_VERSION); |
dan_ackme | 29:b6af04b77a56 | 282 | } |
dan_ackme | 29:b6af04b77a56 | 283 | else |
dan_ackme | 29:b6af04b77a56 | 284 | { |
dan_ackme | 29:b6af04b77a56 | 285 | result = sendCommand(completeCallback, versionBuffer, versionBufferSize, CMD_GET_VERSION); |
dan_ackme | 29:b6af04b77a56 | 286 | } |
dan_ackme | 29:b6af04b77a56 | 287 | |
dan_ackme | 29:b6af04b77a56 | 288 | _CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 289 | |
dan_ackme | 29:b6af04b77a56 | 290 | return result; |
dan_ackme | 29:b6af04b77a56 | 291 | } |
dan_ackme | 29:b6af04b77a56 | 292 | |
dan_ackme | 29:b6af04b77a56 | 293 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 294 | WiconnectResult Wiconnect::updateFirmware(bool forced, const char *versionStr, const Callback &completeCallback) |
dan_ackme | 29:b6af04b77a56 | 295 | { |
dan_ackme | 29:b6af04b77a56 | 296 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 297 | char *cmdBuffer = internalBuffer; |
dan_ackme | 29:b6af04b77a56 | 298 | |
dan_ackme | 29:b6af04b77a56 | 299 | if(_WICONNECT_IS_IDLE()) |
dan_ackme | 29:b6af04b77a56 | 300 | { |
dan_ackme | 29:b6af04b77a56 | 301 | strcpy(cmdBuffer, "ota "); |
dan_ackme | 29:b6af04b77a56 | 302 | if(versionStr != NULL) |
dan_ackme | 29:b6af04b77a56 | 303 | { |
dan_ackme | 29:b6af04b77a56 | 304 | strcat(cmdBuffer, "-b wiconnect-"); |
dan_ackme | 29:b6af04b77a56 | 305 | strcat(cmdBuffer, versionStr); |
dan_ackme | 29:b6af04b77a56 | 306 | } |
dan_ackme | 29:b6af04b77a56 | 307 | else if(forced) |
dan_ackme | 29:b6af04b77a56 | 308 | { |
dan_ackme | 29:b6af04b77a56 | 309 | strcat(cmdBuffer, "-f"); |
dan_ackme | 29:b6af04b77a56 | 310 | } |
dan_ackme | 29:b6af04b77a56 | 311 | } |
dan_ackme | 29:b6af04b77a56 | 312 | |
dan_ackme | 29:b6af04b77a56 | 313 | _CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 314 | |
dan_ackme | 29:b6af04b77a56 | 315 | result = sendCommand(completeCallback, WICONNECT_FIRMWARE_UPDATE_TIMEOUT, cmdBuffer); |
dan_ackme | 29:b6af04b77a56 | 316 | |
dan_ackme | 29:b6af04b77a56 | 317 | _CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 318 | |
dan_ackme | 29:b6af04b77a56 | 319 | return result; |
dan_ackme | 29:b6af04b77a56 | 320 | } |
dan_ackme | 29:b6af04b77a56 | 321 | |
dan_ackme | 29:b6af04b77a56 | 322 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 323 | const char* Wiconnect::getWiconnectResultStr(WiconnectResult wiconnectResult) |
dan_ackme | 29:b6af04b77a56 | 324 | { |
dan_ackme | 29:b6af04b77a56 | 325 | static const char* const wiconnectSuccessStrTable[] = { |
dan_ackme | 29:b6af04b77a56 | 326 | "Success", // WICONNECT_SUCCESS |
dan_ackme | 29:b6af04b77a56 | 327 | "Processing command", // WICONNECT_PROCESSING |
dan_ackme | 29:b6af04b77a56 | 328 | "Idle", // WICONNECT_IDLE |
dan_ackme | 29:b6af04b77a56 | 329 | "Aborted", // WICONNECT_ABORTED |
dan_ackme | 29:b6af04b77a56 | 330 | }; |
dan_ackme | 29:b6af04b77a56 | 331 | static const char* const wiconnectErrorStrTable[] = { |
dan_ackme | 29:b6af04b77a56 | 332 | "", |
dan_ackme | 29:b6af04b77a56 | 333 | "General error", // WICONNECT_ERROR |
dan_ackme | 29:b6af04b77a56 | 334 | "WiConnect command code error", // WICONNECT_CMD_RESPONSE_ERROR |
dan_ackme | 29:b6af04b77a56 | 335 | "Null buffer", // WICONNECT_NULL_BUFFER |
dan_ackme | 29:b6af04b77a56 | 336 | "Not initialized", // WICONNECT_NOT_INITIALIZED |
dan_ackme | 29:b6af04b77a56 | 337 | "Overflow", // WICONNECT_OVERFLOW |
dan_ackme | 29:b6af04b77a56 | 338 | "Timeout", // WICONNECT_TIMEOUT |
dan_ackme | 29:b6af04b77a56 | 339 | "Response handler null", // WICONNECT_RESPONSE_HANDLER_NULL |
dan_ackme | 29:b6af04b77a56 | 340 | "Response parse error", // WICONNECT_RESPONSE_PARSE_ERROR |
dan_ackme | 29:b6af04b77a56 | 341 | "Another command is executing", // WICONNECT_ANOTHER_CMD_EXECUTING |
dan_ackme | 29:b6af04b77a56 | 342 | "Bad argument(s)", // WICONNECT_BAD_ARG |
dan_ackme | 29:b6af04b77a56 | 343 | "Unsupported", // WICONNECT_UNSUPPORTED |
dan_ackme | 29:b6af04b77a56 | 344 | "Pin name to GPIO mapper null", // WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL |
dan_ackme | 29:b6af04b77a56 | 345 | "Duplicate", // WICONNECT_DUPLICATE |
dan_ackme | 29:b6af04b77a56 | 346 | "Not found", // WICONNECT_NOT_FOUND |
dan_ackme | 29:b6af04b77a56 | 347 | "No mapping for pinname to GPIO", // WICONNECT_PINNAME_TO_GPIO_NO_MAPPING |
dan_ackme | 29:b6af04b77a56 | 348 | "Not connected", // WICONNECT_NOT_CONNECTED |
dan_ackme | 29:b6af04b77a56 | 349 | "Underflow", // WICONNECT_UNDERFLOW |
dan_ackme | 29:b6af04b77a56 | 350 | "A monitor is not available", // WICONNECT_MONITOR_NOT_AVAILABLE |
dan_ackme | 35:15725177aa60 | 351 | "Not opened for reading", // WICONNECT_NOT_OPENED_FOR_READING |
dan_ackme | 35:15725177aa60 | 352 | "WiFi firmware update required", // WICONNECT_FIRMWARE_OUTDATED |
dan_ackme | 29:b6af04b77a56 | 353 | }; |
dan_ackme | 29:b6af04b77a56 | 354 | |
dan_ackme | 29:b6af04b77a56 | 355 | if((int)wiconnectResult >= (int)WICONNECT_SUCCESS) |
dan_ackme | 29:b6af04b77a56 | 356 | { |
dan_ackme | 29:b6af04b77a56 | 357 | return wiconnectSuccessStrTable[wiconnectResult]; |
dan_ackme | 29:b6af04b77a56 | 358 | } |
dan_ackme | 29:b6af04b77a56 | 359 | else |
dan_ackme | 29:b6af04b77a56 | 360 | { |
dan_ackme | 29:b6af04b77a56 | 361 | wiconnectResult = (WiconnectResult)(-((int)wiconnectResult)); |
dan_ackme | 29:b6af04b77a56 | 362 | return wiconnectErrorStrTable[wiconnectResult]; |
dan_ackme | 29:b6af04b77a56 | 363 | } |
dan_ackme | 29:b6af04b77a56 | 364 | } |
dan_ackme | 35:15725177aa60 | 365 | |
dan_ackme | 35:15725177aa60 | 366 | /*************************************************************************************************/ |
dan_ackme | 35:15725177aa60 | 367 | uint32_t Wiconnect::wiconnectVersionToInt(char *versionStr) |
dan_ackme | 35:15725177aa60 | 368 | { |
dan_ackme | 35:15725177aa60 | 369 | char *idx = strchr(versionStr, ','); |
dan_ackme | 35:15725177aa60 | 370 | if(idx == NULL) |
dan_ackme | 35:15725177aa60 | 371 | { |
dan_ackme | 36:a30cbf4bdb13 | 372 | return UINT_MAX; |
dan_ackme | 35:15725177aa60 | 373 | } |
dan_ackme | 35:15725177aa60 | 374 | *idx = 0; |
dan_ackme | 35:15725177aa60 | 375 | idx = versionStr; |
dan_ackme | 35:15725177aa60 | 376 | while((idx = strchr(versionStr, '-')) != NULL) |
dan_ackme | 35:15725177aa60 | 377 | { |
dan_ackme | 35:15725177aa60 | 378 | versionStr = idx + 1; |
dan_ackme | 35:15725177aa60 | 379 | } |
dan_ackme | 35:15725177aa60 | 380 | |
dan_ackme | 35:15725177aa60 | 381 | uint32_t ver; |
dan_ackme | 35:15725177aa60 | 382 | |
dan_ackme | 35:15725177aa60 | 383 | if(Wiconnect::fileVersionStrToInt(versionStr, &ver)) |
dan_ackme | 35:15725177aa60 | 384 | { |
dan_ackme | 35:15725177aa60 | 385 | return ver; |
dan_ackme | 35:15725177aa60 | 386 | } |
dan_ackme | 35:15725177aa60 | 387 | |
dan_ackme | 36:a30cbf4bdb13 | 388 | return UINT_MAX; |
dan_ackme | 35:15725177aa60 | 389 | } |
dan_ackme | 29:b6af04b77a56 | 390 | |
dan_ackme | 29:b6af04b77a56 | 391 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 392 | void Wiconnect::setDebugLogger(LogFunc logFunc) |
dan_ackme | 29:b6af04b77a56 | 393 | { |
dan_ackme | 29:b6af04b77a56 | 394 | #ifdef WICONNECT_ENABLE_DEBUGGING |
dan_ackme | 29:b6af04b77a56 | 395 | debugLogger = logFunc; |
dan_ackme | 29:b6af04b77a56 | 396 | #endif |
dan_ackme | 29:b6af04b77a56 | 397 | } |
dan_ackme | 29:b6af04b77a56 | 398 | |
dan_ackme | 29:b6af04b77a56 | 399 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 400 | void Wiconnect::setAssertLogger(LogFunc assertLogFunc) |
dan_ackme | 29:b6af04b77a56 | 401 | { |
dan_ackme | 29:b6af04b77a56 | 402 | assertLogger = assertLogFunc; |
dan_ackme | 29:b6af04b77a56 | 403 | } |
dan_ackme | 29:b6af04b77a56 | 404 | |
dan_ackme | 29:b6af04b77a56 | 405 | #ifdef WICONNECT_ENABLE_DEBUGGING |
dan_ackme | 29:b6af04b77a56 | 406 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 407 | void Wiconnect::debugLog(const char *msg, ...) |
dan_ackme | 29:b6af04b77a56 | 408 | { |
dan_ackme | 29:b6af04b77a56 | 409 | if(!debugLogger.isValid()) |
dan_ackme | 29:b6af04b77a56 | 410 | { |
dan_ackme | 29:b6af04b77a56 | 411 | return; |
dan_ackme | 29:b6af04b77a56 | 412 | } |
dan_ackme | 29:b6af04b77a56 | 413 | |
dan_ackme | 29:b6af04b77a56 | 414 | static char buffer[96]; |
dan_ackme | 29:b6af04b77a56 | 415 | va_list args; |
dan_ackme | 29:b6af04b77a56 | 416 | va_start(args, msg); |
dan_ackme | 29:b6af04b77a56 | 417 | int len = vsnprintf(buffer, sizeof(buffer)-1, msg, args); |
dan_ackme | 29:b6af04b77a56 | 418 | va_end(args); |
dan_ackme | 29:b6af04b77a56 | 419 | |
dan_ackme | 29:b6af04b77a56 | 420 | if(len > (int)(sizeof(buffer)-6)) |
dan_ackme | 29:b6af04b77a56 | 421 | { |
dan_ackme | 29:b6af04b77a56 | 422 | char *p = &buffer[sizeof(buffer)-6]; |
dan_ackme | 29:b6af04b77a56 | 423 | *p++ = '.'; |
dan_ackme | 29:b6af04b77a56 | 424 | *p++ = '.'; |
dan_ackme | 29:b6af04b77a56 | 425 | *p++ = '.'; |
dan_ackme | 29:b6af04b77a56 | 426 | *p++ = '\r'; |
dan_ackme | 29:b6af04b77a56 | 427 | *p++ = '\n'; |
dan_ackme | 29:b6af04b77a56 | 428 | *p = 0; |
dan_ackme | 29:b6af04b77a56 | 429 | } |
dan_ackme | 29:b6af04b77a56 | 430 | else |
dan_ackme | 29:b6af04b77a56 | 431 | { |
dan_ackme | 29:b6af04b77a56 | 432 | if(buffer[len-2] != '\r') |
dan_ackme | 29:b6af04b77a56 | 433 | { |
dan_ackme | 29:b6af04b77a56 | 434 | char *p = &buffer[len]; |
dan_ackme | 29:b6af04b77a56 | 435 | *p++ = '\r'; |
dan_ackme | 29:b6af04b77a56 | 436 | *p++ = '\n'; |
dan_ackme | 29:b6af04b77a56 | 437 | *p = 0; |
dan_ackme | 29:b6af04b77a56 | 438 | } |
dan_ackme | 29:b6af04b77a56 | 439 | } |
dan_ackme | 29:b6af04b77a56 | 440 | debugLogger.call(buffer); |
dan_ackme | 29:b6af04b77a56 | 441 | } |
dan_ackme | 29:b6af04b77a56 | 442 | #endif |