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:
Mon Nov 03 23:48:38 2014 +0000
Revision:
33:9b690d76eedf
Parent:
29:b6af04b77a56
Minor bug fixes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 33:9b690d76eedf 1 /**
dan_ackme 33:9b690d76eedf 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 33:9b690d76eedf 3 *
dan_ackme 33:9b690d76eedf 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 33:9b690d76eedf 5 * All rights reserved.
dan_ackme 33:9b690d76eedf 6 *
dan_ackme 33:9b690d76eedf 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 33:9b690d76eedf 8 * are permitted provided that the following conditions are met:
dan_ackme 33:9b690d76eedf 9 *
dan_ackme 33:9b690d76eedf 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 33:9b690d76eedf 11 * this list of conditions and the following disclaimer.
dan_ackme 33:9b690d76eedf 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 33:9b690d76eedf 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 33:9b690d76eedf 14 * and/or other materials provided with the distribution.
dan_ackme 33:9b690d76eedf 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 33:9b690d76eedf 16 * derived from this software without specific prior written permission.
dan_ackme 33:9b690d76eedf 17 *
dan_ackme 33:9b690d76eedf 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 33:9b690d76eedf 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 33:9b690d76eedf 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 33:9b690d76eedf 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 33:9b690d76eedf 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 33:9b690d76eedf 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 33:9b690d76eedf 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 33:9b690d76eedf 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 33:9b690d76eedf 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 33:9b690d76eedf 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 29:b6af04b77a56 83 initialized = false;
dan_ackme 29:b6af04b77a56 84 pinToGpioMapper = NULL;
dan_ackme 29:b6af04b77a56 85 defaultTimeoutMs = WICONNECT_DEFAULT_TIMEOUT;
dan_ackme 29:b6af04b77a56 86
dan_ackme 29:b6af04b77a56 87 memset(commandContext, 0, sizeof(commandContext));
dan_ackme 29:b6af04b77a56 88
dan_ackme 29:b6af04b77a56 89 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
dan_ackme 29:b6af04b77a56 90 commandProcessingPeriod = WICONNECT_DEFAULT_COMMAND_PROCESSING_PERIOD;
dan_ackme 29:b6af04b77a56 91 currentQueuedCommand = NULL;
dan_ackme 29:b6af04b77a56 92 #endif
dan_ackme 29:b6af04b77a56 93 }
dan_ackme 29:b6af04b77a56 94
dan_ackme 29:b6af04b77a56 95
dan_ackme 29:b6af04b77a56 96 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 97 Wiconnect::Wiconnect(const SerialConfig &serialConfig, int internalBufferSize, void *internalBuffer, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) :
dan_ackme 33:9b690d76eedf 98 NetworkInterface(this), SocketInterface(this), FileInterface(this), GhmInterface(this),
dan_ackme 29:b6af04b77a56 99 MALLOC_CONSTRUCTORS serial(serialConfig, this), resetGpio(reset), wakeGpio(wake)
dan_ackme 29:b6af04b77a56 100 {
dan_ackme 29:b6af04b77a56 101 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 29:b6af04b77a56 102 wiconnect_assert(this, "Wiconnect(), bad malloc/free", (mallocPtr == NULL && freePtr == NULL) || (mallocPtr != NULL && freePtr != NULL));
dan_ackme 29:b6af04b77a56 103 #endif
dan_ackme 29:b6af04b77a56 104 prepare(internalBuffer, internalBufferSize, nonBlocking);
dan_ackme 29:b6af04b77a56 105 }
dan_ackme 29:b6af04b77a56 106
dan_ackme 29:b6af04b77a56 107 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 108 Wiconnect::Wiconnect(const SerialConfig &serialConfig, Pin reset, Pin wake, bool nonBlocking MALLOC_ARGS) :
dan_ackme 33:9b690d76eedf 109 NetworkInterface(this), SocketInterface(this), FileInterface(this), GhmInterface(this),
dan_ackme 29:b6af04b77a56 110 MALLOC_CONSTRUCTORS serial(serialConfig, this), resetGpio(reset), wakeGpio(wake)
dan_ackme 29:b6af04b77a56 111 {
dan_ackme 29:b6af04b77a56 112 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 29:b6af04b77a56 113 wiconnect_assert(this, "Wiconnect(), bad malloc/free", (mallocPtr == NULL && freePtr == NULL) || (mallocPtr != NULL && freePtr != NULL));
dan_ackme 29:b6af04b77a56 114 #endif
dan_ackme 29:b6af04b77a56 115 prepare(NULL, 0, nonBlocking);
dan_ackme 29:b6af04b77a56 116 }
dan_ackme 29:b6af04b77a56 117
dan_ackme 29:b6af04b77a56 118 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 119 Wiconnect::~Wiconnect()
dan_ackme 29:b6af04b77a56 120 {
dan_ackme 29:b6af04b77a56 121 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 29:b6af04b77a56 122 if(internalBufferAlloc)
dan_ackme 29:b6af04b77a56 123 {
dan_ackme 29:b6af04b77a56 124 _free(internalBuffer);
dan_ackme 29:b6af04b77a56 125 }
dan_ackme 29:b6af04b77a56 126 #endif
dan_ackme 29:b6af04b77a56 127 }
dan_ackme 29:b6af04b77a56 128
dan_ackme 29:b6af04b77a56 129 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 130 WiconnectResult Wiconnect::init(bool bringNetworkUp)
dan_ackme 29:b6af04b77a56 131 {
dan_ackme 29:b6af04b77a56 132 WiconnectResult result;
dan_ackme 33:9b690d76eedf 133 int retries;
dan_ackme 29:b6af04b77a56 134 bool configuredBus = false;
dan_ackme 29:b6af04b77a56 135 bool savedNonBlocking = nonBlocking;
dan_ackme 33:9b690d76eedf 136
dan_ackme 33:9b690d76eedf 137 DEBUG_INFO("Initializing wiconnect");
dan_ackme 33:9b690d76eedf 138
dan_ackme 33:9b690d76eedf 139 serial.initialize();
dan_ackme 29:b6af04b77a56 140
dan_ackme 29:b6af04b77a56 141 if(WICONNECT_FAILED(result, reset()))
dan_ackme 29:b6af04b77a56 142 {
dan_ackme 29:b6af04b77a56 143 return result;
dan_ackme 29:b6af04b77a56 144 }
dan_ackme 29:b6af04b77a56 145
dan_ackme 29:b6af04b77a56 146 delayMs(1000);
dan_ackme 29:b6af04b77a56 147
dan_ackme 29:b6af04b77a56 148 initialized = true;
dan_ackme 33:9b690d76eedf 149 nonBlocking = false;
dan_ackme 33:9b690d76eedf 150
dan_ackme 29:b6af04b77a56 151
dan_ackme 29:b6af04b77a56 152 loop:
dan_ackme 29:b6af04b77a56 153 for(retries = 3; retries > 0; --retries)
dan_ackme 29:b6af04b77a56 154 {
dan_ackme 29:b6af04b77a56 155 result = sendCommand(1000, CMD_SET_SYSTEM_COMMAND_MODE, "machine");
dan_ackme 29:b6af04b77a56 156 if(result != WICONNECT_SUCCESS)
dan_ackme 29:b6af04b77a56 157 {
dan_ackme 29:b6af04b77a56 158 delayMs(100);
dan_ackme 29:b6af04b77a56 159 }
dan_ackme 29:b6af04b77a56 160 else
dan_ackme 29:b6af04b77a56 161 {
dan_ackme 29:b6af04b77a56 162 break;
dan_ackme 29:b6af04b77a56 163 }
dan_ackme 29:b6af04b77a56 164 }
dan_ackme 33:9b690d76eedf 165
dan_ackme 33:9b690d76eedf 166 if(result != WICONNECT_SUCCESS && !configuredBus)
dan_ackme 33:9b690d76eedf 167 {
dan_ackme 33:9b690d76eedf 168 configuredBus = true;
dan_ackme 33:9b690d76eedf 169 if(configureModuleDataBus())
dan_ackme 33:9b690d76eedf 170 {
dan_ackme 33:9b690d76eedf 171 goto loop;
dan_ackme 33:9b690d76eedf 172 }
dan_ackme 33:9b690d76eedf 173 }
dan_ackme 33:9b690d76eedf 174
dan_ackme 33:9b690d76eedf 175 if(result == WICONNECT_SUCCESS)
dan_ackme 33:9b690d76eedf 176 {
dan_ackme 33:9b690d76eedf 177 sendCommand("set stream.auto_close 0");
dan_ackme 29:b6af04b77a56 178 }
dan_ackme 29:b6af04b77a56 179 if(result == WICONNECT_SUCCESS && bringNetworkUp)
dan_ackme 29:b6af04b77a56 180 {
dan_ackme 29:b6af04b77a56 181 sendCommand(15000, "ping -g");
dan_ackme 29:b6af04b77a56 182 }
dan_ackme 29:b6af04b77a56 183
dan_ackme 29:b6af04b77a56 184 nonBlocking = savedNonBlocking;
dan_ackme 29:b6af04b77a56 185 if(result != WICONNECT_SUCCESS)
dan_ackme 29:b6af04b77a56 186 {
dan_ackme 29:b6af04b77a56 187 initialized = false;
dan_ackme 29:b6af04b77a56 188 }
dan_ackme 29:b6af04b77a56 189
dan_ackme 29:b6af04b77a56 190
dan_ackme 29:b6af04b77a56 191 return result;
dan_ackme 29:b6af04b77a56 192 }
dan_ackme 29:b6af04b77a56 193
dan_ackme 29:b6af04b77a56 194 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 195 void Wiconnect::deinit(void)
dan_ackme 29:b6af04b77a56 196 {
dan_ackme 29:b6af04b77a56 197 initialized = false;
dan_ackme 29:b6af04b77a56 198 }
dan_ackme 29:b6af04b77a56 199
dan_ackme 29:b6af04b77a56 200 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 201 Wiconnect* Wiconnect::getInstance()
dan_ackme 33:9b690d76eedf 202 {
dan_ackme 29:b6af04b77a56 203 return instance;
dan_ackme 29:b6af04b77a56 204 }
dan_ackme 29:b6af04b77a56 205
dan_ackme 29:b6af04b77a56 206 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 207 bool Wiconnect::isInitialized()
dan_ackme 29:b6af04b77a56 208 {
dan_ackme 29:b6af04b77a56 209 return initialized;
dan_ackme 29:b6af04b77a56 210 }
dan_ackme 29:b6af04b77a56 211
dan_ackme 29:b6af04b77a56 212 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 213 WiconnectResult Wiconnect::reset()
dan_ackme 29:b6af04b77a56 214 {
dan_ackme 29:b6af04b77a56 215 resetGpio = 0;
dan_ackme 29:b6af04b77a56 216 delayMs(10);
dan_ackme 29:b6af04b77a56 217 resetGpio = 1;
dan_ackme 29:b6af04b77a56 218 delayMs(1000);
dan_ackme 29:b6af04b77a56 219 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 220 }
dan_ackme 29:b6af04b77a56 221
dan_ackme 29:b6af04b77a56 222 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 223 WiconnectResult Wiconnect::wakeup()
dan_ackme 29:b6af04b77a56 224 {
dan_ackme 29:b6af04b77a56 225 wakeGpio = 1;
dan_ackme 29:b6af04b77a56 226 delayMs(1);
dan_ackme 29:b6af04b77a56 227 wakeGpio = 0;
dan_ackme 29:b6af04b77a56 228 return WICONNECT_SUCCESS;
dan_ackme 29:b6af04b77a56 229 }
dan_ackme 29:b6af04b77a56 230
dan_ackme 29:b6af04b77a56 231 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 232 void Wiconnect::flush(int delayMs)
dan_ackme 29:b6af04b77a56 233 {
dan_ackme 33:9b690d76eedf 234 if(delayMs != 0)
dan_ackme 33:9b690d76eedf 235 {
dan_ackme 33:9b690d76eedf 236 serial.write("\r\n\r\n", 4, 0);
dan_ackme 33:9b690d76eedf 237 }
dan_ackme 33:9b690d76eedf 238 delayMs(delayMs);
dan_ackme 33:9b690d76eedf 239 serial.flush();
dan_ackme 29:b6af04b77a56 240 }
dan_ackme 29:b6af04b77a56 241
dan_ackme 29:b6af04b77a56 242 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 243 void Wiconnect::setPinToGpioMapper(PinToGpioMapper mapper)
dan_ackme 29:b6af04b77a56 244 {
dan_ackme 29:b6af04b77a56 245 pinToGpioMapper = mapper;
dan_ackme 29:b6af04b77a56 246 }
dan_ackme 29:b6af04b77a56 247
dan_ackme 29:b6af04b77a56 248 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 249 WiconnectResult Wiconnect::getVersion(char *versionBuffer, int versionBufferSize, const Callback &completeCallback)
dan_ackme 33:9b690d76eedf 250 {
dan_ackme 33:9b690d76eedf 251 WiconnectResult result;
dan_ackme 29:b6af04b77a56 252
dan_ackme 29:b6af04b77a56 253 if(versionBuffer != NULL && versionBufferSize == 0)
dan_ackme 29:b6af04b77a56 254 {
dan_ackme 29:b6af04b77a56 255 return WICONNECT_BAD_ARG;
dan_ackme 33:9b690d76eedf 256 }
dan_ackme 33:9b690d76eedf 257
dan_ackme 33:9b690d76eedf 258 _CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 33:9b690d76eedf 259
dan_ackme 33:9b690d76eedf 260 if(versionBuffer == NULL)
dan_ackme 29:b6af04b77a56 261 {
dan_ackme 33:9b690d76eedf 262 result = sendCommand(completeCallback, CMD_GET_VERSION);
dan_ackme 33:9b690d76eedf 263 }
dan_ackme 33:9b690d76eedf 264 else
dan_ackme 33:9b690d76eedf 265 {
dan_ackme 33:9b690d76eedf 266 result = sendCommand(completeCallback, versionBuffer, versionBufferSize, CMD_GET_VERSION);
dan_ackme 33:9b690d76eedf 267 }
dan_ackme 33:9b690d76eedf 268
dan_ackme 33:9b690d76eedf 269 _CHECK_CLEANUP_COMMAND();
dan_ackme 33:9b690d76eedf 270
dan_ackme 29:b6af04b77a56 271 return result;
dan_ackme 29:b6af04b77a56 272 }
dan_ackme 33:9b690d76eedf 273
dan_ackme 33:9b690d76eedf 274 /*************************************************************************************************/
dan_ackme 33:9b690d76eedf 275 WiconnectResult Wiconnect::updateFirmware(bool forced, const char *versionStr, const Callback &completeCallback)
dan_ackme 33:9b690d76eedf 276 {
dan_ackme 33:9b690d76eedf 277 WiconnectResult result;
dan_ackme 33:9b690d76eedf 278 char *cmdBuffer = internalBuffer;
dan_ackme 33:9b690d76eedf 279
dan_ackme 33:9b690d76eedf 280 if(_WICONNECT_IS_IDLE())
dan_ackme 33:9b690d76eedf 281 {
dan_ackme 33:9b690d76eedf 282 strcpy(cmdBuffer, "ota ");
dan_ackme 33:9b690d76eedf 283 if(versionStr != NULL)
dan_ackme 33:9b690d76eedf 284 {
dan_ackme 33:9b690d76eedf 285 strcat(cmdBuffer, "-b wiconnect-");
dan_ackme 33:9b690d76eedf 286 strcat(cmdBuffer, versionStr);
dan_ackme 33:9b690d76eedf 287 }
dan_ackme 33:9b690d76eedf 288 else if(forced)
dan_ackme 33:9b690d76eedf 289 {
dan_ackme 33:9b690d76eedf 290 strcat(cmdBuffer, "-f");
dan_ackme 33:9b690d76eedf 291 }
dan_ackme 33:9b690d76eedf 292 }
dan_ackme 33:9b690d76eedf 293
dan_ackme 33:9b690d76eedf 294 _CHECK_OTHER_COMMAND_EXECUTING();
dan_ackme 33:9b690d76eedf 295
dan_ackme 33:9b690d76eedf 296 result = sendCommand(completeCallback, WICONNECT_FIRMWARE_UPDATE_TIMEOUT, cmdBuffer);
dan_ackme 33:9b690d76eedf 297
dan_ackme 33:9b690d76eedf 298 _CHECK_CLEANUP_COMMAND();
dan_ackme 33:9b690d76eedf 299
dan_ackme 33:9b690d76eedf 300 return result;
dan_ackme 33:9b690d76eedf 301 }
dan_ackme 29:b6af04b77a56 302
dan_ackme 29:b6af04b77a56 303 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 304 const char* Wiconnect::getWiconnectResultStr(WiconnectResult wiconnectResult)
dan_ackme 29:b6af04b77a56 305 {
dan_ackme 29:b6af04b77a56 306 static const char* const wiconnectSuccessStrTable[] = {
dan_ackme 29:b6af04b77a56 307 "Success", // WICONNECT_SUCCESS
dan_ackme 29:b6af04b77a56 308 "Processing command", // WICONNECT_PROCESSING
dan_ackme 29:b6af04b77a56 309 "Idle", // WICONNECT_IDLE
dan_ackme 29:b6af04b77a56 310 "Aborted", // WICONNECT_ABORTED
dan_ackme 29:b6af04b77a56 311 };
dan_ackme 29:b6af04b77a56 312 static const char* const wiconnectErrorStrTable[] = {
dan_ackme 29:b6af04b77a56 313 "",
dan_ackme 29:b6af04b77a56 314 "General error", // WICONNECT_ERROR
dan_ackme 29:b6af04b77a56 315 "WiConnect command code error", // WICONNECT_CMD_RESPONSE_ERROR
dan_ackme 29:b6af04b77a56 316 "Null buffer", // WICONNECT_NULL_BUFFER
dan_ackme 29:b6af04b77a56 317 "Not initialized", // WICONNECT_NOT_INITIALIZED
dan_ackme 29:b6af04b77a56 318 "Overflow", // WICONNECT_OVERFLOW
dan_ackme 29:b6af04b77a56 319 "Timeout", // WICONNECT_TIMEOUT
dan_ackme 29:b6af04b77a56 320 "Response handler null", // WICONNECT_RESPONSE_HANDLER_NULL
dan_ackme 29:b6af04b77a56 321 "Response parse error", // WICONNECT_RESPONSE_PARSE_ERROR
dan_ackme 29:b6af04b77a56 322 "Another command is executing", // WICONNECT_ANOTHER_CMD_EXECUTING
dan_ackme 29:b6af04b77a56 323 "Bad argument(s)", // WICONNECT_BAD_ARG
dan_ackme 29:b6af04b77a56 324 "Unsupported", // WICONNECT_UNSUPPORTED
dan_ackme 29:b6af04b77a56 325 "Pin name to GPIO mapper null", // WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL
dan_ackme 29:b6af04b77a56 326 "Duplicate", // WICONNECT_DUPLICATE
dan_ackme 29:b6af04b77a56 327 "Not found", // WICONNECT_NOT_FOUND
dan_ackme 29:b6af04b77a56 328 "No mapping for pinname to GPIO", // WICONNECT_PINNAME_TO_GPIO_NO_MAPPING
dan_ackme 29:b6af04b77a56 329 "Not connected", // WICONNECT_NOT_CONNECTED
dan_ackme 29:b6af04b77a56 330 "Underflow", // WICONNECT_UNDERFLOW
dan_ackme 29:b6af04b77a56 331 "A monitor is not available", // WICONNECT_MONITOR_NOT_AVAILABLE
dan_ackme 29:b6af04b77a56 332 "Not opened for reading", // WICONNECT_NOT_OPENED_FOR_READING
dan_ackme 29:b6af04b77a56 333 };
dan_ackme 29:b6af04b77a56 334
dan_ackme 29:b6af04b77a56 335 if((int)wiconnectResult >= (int)WICONNECT_SUCCESS)
dan_ackme 29:b6af04b77a56 336 {
dan_ackme 29:b6af04b77a56 337 return wiconnectSuccessStrTable[wiconnectResult];
dan_ackme 29:b6af04b77a56 338 }
dan_ackme 29:b6af04b77a56 339 else
dan_ackme 29:b6af04b77a56 340 {
dan_ackme 29:b6af04b77a56 341 wiconnectResult = (WiconnectResult)(-((int)wiconnectResult));
dan_ackme 29:b6af04b77a56 342 return wiconnectErrorStrTable[wiconnectResult];
dan_ackme 29:b6af04b77a56 343 }
dan_ackme 29:b6af04b77a56 344 }
dan_ackme 29:b6af04b77a56 345
dan_ackme 29:b6af04b77a56 346
dan_ackme 29:b6af04b77a56 347 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 348 void Wiconnect::setDebugLogger(LogFunc logFunc)
dan_ackme 33:9b690d76eedf 349 {
dan_ackme 29:b6af04b77a56 350 #ifdef WICONNECT_ENABLE_DEBUGGING
dan_ackme 33:9b690d76eedf 351 debugLogger = logFunc;
dan_ackme 29:b6af04b77a56 352 #endif
dan_ackme 29:b6af04b77a56 353 }
dan_ackme 29:b6af04b77a56 354
dan_ackme 29:b6af04b77a56 355 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 356 void Wiconnect::setAssertLogger(LogFunc assertLogFunc)
dan_ackme 29:b6af04b77a56 357 {
dan_ackme 29:b6af04b77a56 358 assertLogger = assertLogFunc;
dan_ackme 29:b6af04b77a56 359 }
dan_ackme 33:9b690d76eedf 360
dan_ackme 29:b6af04b77a56 361 #ifdef WICONNECT_ENABLE_DEBUGGING
dan_ackme 29:b6af04b77a56 362 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 363 void Wiconnect::debugLog(const char *msg, ...)
dan_ackme 29:b6af04b77a56 364 {
dan_ackme 29:b6af04b77a56 365 if(!debugLogger.isValid())
dan_ackme 29:b6af04b77a56 366 {
dan_ackme 29:b6af04b77a56 367 return;
dan_ackme 29:b6af04b77a56 368 }
dan_ackme 29:b6af04b77a56 369
dan_ackme 29:b6af04b77a56 370 static char buffer[96];
dan_ackme 29:b6af04b77a56 371 va_list args;
dan_ackme 29:b6af04b77a56 372 va_start(args, msg);
dan_ackme 29:b6af04b77a56 373 int len = vsnprintf(buffer, sizeof(buffer)-1, msg, args);
dan_ackme 29:b6af04b77a56 374 va_end(args);
dan_ackme 29:b6af04b77a56 375
dan_ackme 29:b6af04b77a56 376 if(len > (int)(sizeof(buffer)-6))
dan_ackme 29:b6af04b77a56 377 {
dan_ackme 29:b6af04b77a56 378 char *p = &buffer[sizeof(buffer)-6];
dan_ackme 29:b6af04b77a56 379 *p++ = '.';
dan_ackme 29:b6af04b77a56 380 *p++ = '.';
dan_ackme 29:b6af04b77a56 381 *p++ = '.';
dan_ackme 29:b6af04b77a56 382 *p++ = '\r';
dan_ackme 29:b6af04b77a56 383 *p++ = '\n';
dan_ackme 29:b6af04b77a56 384 *p = 0;
dan_ackme 29:b6af04b77a56 385 }
dan_ackme 29:b6af04b77a56 386 else
dan_ackme 29:b6af04b77a56 387 {
dan_ackme 29:b6af04b77a56 388 if(buffer[len-2] != '\r')
dan_ackme 29:b6af04b77a56 389 {
dan_ackme 29:b6af04b77a56 390 char *p = &buffer[len];
dan_ackme 29:b6af04b77a56 391 *p++ = '\r';
dan_ackme 29:b6af04b77a56 392 *p++ = '\n';
dan_ackme 29:b6af04b77a56 393 *p = 0;
dan_ackme 29:b6af04b77a56 394 }
dan_ackme 29:b6af04b77a56 395 }
dan_ackme 29:b6af04b77a56 396 debugLogger.call(buffer);
dan_ackme 29:b6af04b77a56 397 }
dan_ackme 33:9b690d76eedf 398 #endif