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:
Sat Sep 06 20:34:01 2014 -0700
Revision:
24:e27e23297f02
Parent:
16:7f1d6d359787
add api to get/set module settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 24:e27e23297f02 1 /**
dan_ackme 24:e27e23297f02 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 24:e27e23297f02 3 *
dan_ackme 24:e27e23297f02 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 24:e27e23297f02 5 * All rights reserved.
dan_ackme 24:e27e23297f02 6 *
dan_ackme 24:e27e23297f02 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 24:e27e23297f02 8 * are permitted provided that the following conditions are met:
dan_ackme 24:e27e23297f02 9 *
dan_ackme 24:e27e23297f02 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 24:e27e23297f02 11 * this list of conditions and the following disclaimer.
dan_ackme 24:e27e23297f02 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 24:e27e23297f02 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 24:e27e23297f02 14 * and/or other materials provided with the distribution.
dan_ackme 24:e27e23297f02 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 24:e27e23297f02 16 * derived from this software without specific prior written permission.
dan_ackme 24:e27e23297f02 17 *
dan_ackme 24:e27e23297f02 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 24:e27e23297f02 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 24:e27e23297f02 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 24:e27e23297f02 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 24:e27e23297f02 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 24:e27e23297f02 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 24:e27e23297f02 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 24:e27e23297f02 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 24:e27e23297f02 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 24:e27e23297f02 27 * OF SUCH DAMAGE.
dan_ackme 0:ea85c4bb5e1f 28 */
dan_ackme 0:ea85c4bb5e1f 29
dan_ackme 0:ea85c4bb5e1f 30 #include "Wiconnect.h"
dan_ackme 6:8a87a59d0d21 31 #include "internal/common.h"
dan_ackme 0:ea85c4bb5e1f 32
dan_ackme 0:ea85c4bb5e1f 33 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 34 QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList)
dan_ackme 0:ea85c4bb5e1f 35 {
dan_ackme 0:ea85c4bb5e1f 36 initialize(responseBufferLen_, responseBuffer_, timeoutMs_, reader_, user_, cmd_, vaList);
dan_ackme 0:ea85c4bb5e1f 37 }
dan_ackme 0:ea85c4bb5e1f 38
dan_ackme 0:ea85c4bb5e1f 39 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 40 QueuedCommand::QueuedCommand(int responseBufferLen_, char* responseBuffer_, int timeoutMs_, const char *cmd_, ...)
dan_ackme 0:ea85c4bb5e1f 41 {
dan_ackme 0:ea85c4bb5e1f 42 va_list args;
dan_ackme 0:ea85c4bb5e1f 43 va_start(args, cmd_);
dan_ackme 8:1fad4ca6c6a4 44 initialize(responseBufferLen_, responseBuffer_, timeoutMs_, ReaderFunc(), NULL, cmd_, args);
dan_ackme 0:ea85c4bb5e1f 45 va_end(args);
dan_ackme 0:ea85c4bb5e1f 46 }
dan_ackme 0:ea85c4bb5e1f 47
dan_ackme 0:ea85c4bb5e1f 48 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 49 QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, const char *cmd_, ...)
dan_ackme 0:ea85c4bb5e1f 50 {
dan_ackme 0:ea85c4bb5e1f 51 va_list args;
dan_ackme 0:ea85c4bb5e1f 52 va_start(args, cmd_);
dan_ackme 8:1fad4ca6c6a4 53 initialize(responseBufferLen_, responseBuffer_, WICONNECT_DEFAULT_TIMEOUT, ReaderFunc(), NULL, cmd_, args);
dan_ackme 0:ea85c4bb5e1f 54 va_end(args);
dan_ackme 0:ea85c4bb5e1f 55 }
dan_ackme 0:ea85c4bb5e1f 56
dan_ackme 0:ea85c4bb5e1f 57 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 58 QueuedCommand::QueuedCommand(int timeoutMs_, const char *cmd_, ...)
dan_ackme 0:ea85c4bb5e1f 59 {
dan_ackme 0:ea85c4bb5e1f 60 va_list args;
dan_ackme 0:ea85c4bb5e1f 61 va_start(args, cmd_);
dan_ackme 8:1fad4ca6c6a4 62 initialize(0, NULL, timeoutMs_, ReaderFunc(), NULL, cmd_, args);
dan_ackme 0:ea85c4bb5e1f 63 va_end(args);
dan_ackme 0:ea85c4bb5e1f 64 }
dan_ackme 0:ea85c4bb5e1f 65
dan_ackme 0:ea85c4bb5e1f 66 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 67 QueuedCommand::QueuedCommand(const char *cmd_, ...)
dan_ackme 0:ea85c4bb5e1f 68 {
dan_ackme 0:ea85c4bb5e1f 69 va_list args;
dan_ackme 0:ea85c4bb5e1f 70 va_start(args, cmd_);
dan_ackme 8:1fad4ca6c6a4 71 initialize(0, NULL, WICONNECT_DEFAULT_TIMEOUT, ReaderFunc(), NULL, cmd_, args);
dan_ackme 0:ea85c4bb5e1f 72 va_end(args);
dan_ackme 0:ea85c4bb5e1f 73 }
dan_ackme 0:ea85c4bb5e1f 74
dan_ackme 0:ea85c4bb5e1f 75 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 76 QueuedCommand::~QueuedCommand()
dan_ackme 0:ea85c4bb5e1f 77 {
dan_ackme 0:ea85c4bb5e1f 78 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 0:ea85c4bb5e1f 79 if(allocatedBuffer)
dan_ackme 0:ea85c4bb5e1f 80 {
dan_ackme 0:ea85c4bb5e1f 81 Wiconnect::getInstance()->_free(responseBuffer);
dan_ackme 0:ea85c4bb5e1f 82 }
dan_ackme 0:ea85c4bb5e1f 83 #endif
dan_ackme 0:ea85c4bb5e1f 84 }
dan_ackme 0:ea85c4bb5e1f 85
dan_ackme 0:ea85c4bb5e1f 86 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 87 char *QueuedCommand::getResponseBuffer()
dan_ackme 0:ea85c4bb5e1f 88 {
dan_ackme 0:ea85c4bb5e1f 89 return responseBuffer;
dan_ackme 0:ea85c4bb5e1f 90 }
dan_ackme 0:ea85c4bb5e1f 91 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 92 int QueuedCommand::getResponseBufferLen()
dan_ackme 0:ea85c4bb5e1f 93 {
dan_ackme 0:ea85c4bb5e1f 94 return responseBufferLen;
dan_ackme 0:ea85c4bb5e1f 95 }
dan_ackme 0:ea85c4bb5e1f 96 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 97 int QueuedCommand::getTimeoutMs()
dan_ackme 0:ea85c4bb5e1f 98 {
dan_ackme 0:ea85c4bb5e1f 99 return timeoutMs;
dan_ackme 0:ea85c4bb5e1f 100 }
dan_ackme 0:ea85c4bb5e1f 101 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 102 ReaderFunc QueuedCommand::getReader()
dan_ackme 0:ea85c4bb5e1f 103 {
dan_ackme 0:ea85c4bb5e1f 104 return reader;
dan_ackme 0:ea85c4bb5e1f 105 }
dan_ackme 0:ea85c4bb5e1f 106 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 107 void * QueuedCommand::getReaderUserData()
dan_ackme 0:ea85c4bb5e1f 108 {
dan_ackme 0:ea85c4bb5e1f 109 return user;
dan_ackme 0:ea85c4bb5e1f 110 }
dan_ackme 0:ea85c4bb5e1f 111 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 112 char* QueuedCommand::getCommand()
dan_ackme 0:ea85c4bb5e1f 113 {
dan_ackme 0:ea85c4bb5e1f 114 return command;
dan_ackme 0:ea85c4bb5e1f 115 }
dan_ackme 0:ea85c4bb5e1f 116 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 117 Callback QueuedCommand::getCompletedCallback()
dan_ackme 0:ea85c4bb5e1f 118 {
dan_ackme 0:ea85c4bb5e1f 119 return completeCallback;
dan_ackme 0:ea85c4bb5e1f 120 }
dan_ackme 0:ea85c4bb5e1f 121 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 122 void QueuedCommand::setCompletedCallback(const Callback &cb)
dan_ackme 0:ea85c4bb5e1f 123 {
dan_ackme 0:ea85c4bb5e1f 124 completeCallback = cb;
dan_ackme 0:ea85c4bb5e1f 125 }
dan_ackme 0:ea85c4bb5e1f 126
dan_ackme 0:ea85c4bb5e1f 127 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 128 QueuedCommand& QueuedCommand::operator=( const QueuedCommand& other )
dan_ackme 0:ea85c4bb5e1f 129 {
dan_ackme 0:ea85c4bb5e1f 130 responseBuffer = other.responseBuffer;
dan_ackme 0:ea85c4bb5e1f 131 responseBufferLen = other.responseBufferLen;
dan_ackme 0:ea85c4bb5e1f 132 timeoutMs = other.timeoutMs;
dan_ackme 0:ea85c4bb5e1f 133 reader = other.reader;
dan_ackme 0:ea85c4bb5e1f 134 user = other.user;
dan_ackme 0:ea85c4bb5e1f 135 completeCallback = other.completeCallback;
dan_ackme 0:ea85c4bb5e1f 136 memcpy(command, other.command, sizeof(command));
dan_ackme 0:ea85c4bb5e1f 137 return *this;
dan_ackme 0:ea85c4bb5e1f 138 }
dan_ackme 0:ea85c4bb5e1f 139
dan_ackme 0:ea85c4bb5e1f 140 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 141 void* QueuedCommand::operator new(size_t size)
dan_ackme 0:ea85c4bb5e1f 142 {
dan_ackme 6:8a87a59d0d21 143 Wiconnect *wiconnect = Wiconnect::getInstance();
dan_ackme 6:8a87a59d0d21 144 wiconnect_assert(wiconnect, "QueuedCommand:new malloc not defined", wiconnect->_malloc != NULL);
dan_ackme 0:ea85c4bb5e1f 145 return Wiconnect::getInstance()->_malloc(size);
dan_ackme 0:ea85c4bb5e1f 146 }
dan_ackme 0:ea85c4bb5e1f 147
dan_ackme 0:ea85c4bb5e1f 148 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 149 void QueuedCommand::operator delete(void* ptr)
dan_ackme 0:ea85c4bb5e1f 150 {
dan_ackme 6:8a87a59d0d21 151 Wiconnect *wiconnect = Wiconnect::getInstance();
dan_ackme 6:8a87a59d0d21 152 wiconnect_assert(wiconnect, "QueuedCommand:delete free not defined", wiconnect->_free != NULL);
dan_ackme 0:ea85c4bb5e1f 153 Wiconnect::getInstance()->_free(ptr);
dan_ackme 0:ea85c4bb5e1f 154 }
dan_ackme 0:ea85c4bb5e1f 155
dan_ackme 0:ea85c4bb5e1f 156 /*************************************************************************************************/
dan_ackme 0:ea85c4bb5e1f 157 void QueuedCommand::initialize(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList)
dan_ackme 0:ea85c4bb5e1f 158 {
dan_ackme 0:ea85c4bb5e1f 159 if(responseBufferLen_ > 0)
dan_ackme 0:ea85c4bb5e1f 160 {
dan_ackme 0:ea85c4bb5e1f 161 #ifdef WICONNECT_ENABLE_MALLOC
dan_ackme 6:8a87a59d0d21 162 Wiconnect *wiconnect = Wiconnect::getInstance();
dan_ackme 0:ea85c4bb5e1f 163 allocatedBuffer = false;
dan_ackme 0:ea85c4bb5e1f 164 if(responseBuffer_ == NULL)
dan_ackme 0:ea85c4bb5e1f 165 {
dan_ackme 6:8a87a59d0d21 166 wiconnect_assert(wiconnect, "QueuedCommand() malloc not defined", wiconnect->_malloc != NULL);
dan_ackme 6:8a87a59d0d21 167 responseBuffer = (char*)wiconnect->_malloc(responseBufferLen_);
dan_ackme 6:8a87a59d0d21 168 wiconnect_assert(wiconnect, "QueuedCommand() responseBuffer malloc failed", responseBuffer != NULL);
dan_ackme 0:ea85c4bb5e1f 169 allocatedBuffer = true;
dan_ackme 0:ea85c4bb5e1f 170 }
dan_ackme 0:ea85c4bb5e1f 171 else
dan_ackme 0:ea85c4bb5e1f 172 #endif
dan_ackme 0:ea85c4bb5e1f 173 {
dan_ackme 6:8a87a59d0d21 174 wiconnect_assert(wiconnect, "QueuedCommand(), null buffer", responseBuffer_ != NULL);
dan_ackme 0:ea85c4bb5e1f 175 responseBuffer = responseBuffer_;
dan_ackme 0:ea85c4bb5e1f 176 }
dan_ackme 0:ea85c4bb5e1f 177 }
dan_ackme 0:ea85c4bb5e1f 178 responseBufferLen = responseBufferLen_;
dan_ackme 0:ea85c4bb5e1f 179 timeoutMs = timeoutMs_;
dan_ackme 0:ea85c4bb5e1f 180 reader = reader_;
dan_ackme 0:ea85c4bb5e1f 181 user = user_;
dan_ackme 0:ea85c4bb5e1f 182 userData = NULL;
dan_ackme 0:ea85c4bb5e1f 183
dan_ackme 0:ea85c4bb5e1f 184 if(cmd_ != NULL)
dan_ackme 0:ea85c4bb5e1f 185 {
dan_ackme 0:ea85c4bb5e1f 186 int len = vsnprintf(command, sizeof(command)-3, cmd_, vaList);
dan_ackme 0:ea85c4bb5e1f 187 command[len++] = '\r';
dan_ackme 0:ea85c4bb5e1f 188 command[len++] = '\n';
dan_ackme 0:ea85c4bb5e1f 189 command[len] = 0;
dan_ackme 0:ea85c4bb5e1f 190 }
dan_ackme 0:ea85c4bb5e1f 191 }