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
QueuedCommand.cpp@42:8ffb253b09e7, 2015-09-28 (annotated)
- 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?
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 | |
dan_ackme | 29:b6af04b77a56 | 30 | #include "Wiconnect.h" |
dan_ackme | 29:b6af04b77a56 | 31 | #include "internal/common.h" |
dan_ackme | 29:b6af04b77a56 | 32 | |
dan_ackme | 29:b6af04b77a56 | 33 | #ifdef WICONNECT_ASYNC_TIMER_ENABLED |
dan_ackme | 29:b6af04b77a56 | 34 | |
dan_ackme | 29:b6af04b77a56 | 35 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 36 | QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList) |
dan_ackme | 29:b6af04b77a56 | 37 | { |
dan_ackme | 29:b6af04b77a56 | 38 | initialize(responseBufferLen_, responseBuffer_, timeoutMs_, reader_, user_, cmd_, vaList); |
dan_ackme | 29:b6af04b77a56 | 39 | } |
dan_ackme | 29:b6af04b77a56 | 40 | |
dan_ackme | 29:b6af04b77a56 | 41 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 42 | QueuedCommand::QueuedCommand(int responseBufferLen_, char* responseBuffer_, int timeoutMs_, const char *cmd_, ...) |
dan_ackme | 29:b6af04b77a56 | 43 | { |
dan_ackme | 29:b6af04b77a56 | 44 | va_list args; |
dan_ackme | 29:b6af04b77a56 | 45 | va_start(args, cmd_); |
dan_ackme | 29:b6af04b77a56 | 46 | initialize(responseBufferLen_, responseBuffer_, timeoutMs_, ReaderFunc(), NULL, cmd_, args); |
dan_ackme | 29:b6af04b77a56 | 47 | va_end(args); |
dan_ackme | 29:b6af04b77a56 | 48 | } |
dan_ackme | 29:b6af04b77a56 | 49 | |
dan_ackme | 29:b6af04b77a56 | 50 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 51 | QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, const char *cmd_, ...) |
dan_ackme | 29:b6af04b77a56 | 52 | { |
dan_ackme | 29:b6af04b77a56 | 53 | va_list args; |
dan_ackme | 29:b6af04b77a56 | 54 | va_start(args, cmd_); |
dan_ackme | 29:b6af04b77a56 | 55 | initialize(responseBufferLen_, responseBuffer_, WICONNECT_DEFAULT_TIMEOUT, ReaderFunc(), NULL, cmd_, args); |
dan_ackme | 29:b6af04b77a56 | 56 | va_end(args); |
dan_ackme | 29:b6af04b77a56 | 57 | } |
dan_ackme | 29:b6af04b77a56 | 58 | |
dan_ackme | 29:b6af04b77a56 | 59 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 60 | QueuedCommand::QueuedCommand(int timeoutMs_, const char *cmd_, ...) |
dan_ackme | 29:b6af04b77a56 | 61 | { |
dan_ackme | 29:b6af04b77a56 | 62 | va_list args; |
dan_ackme | 29:b6af04b77a56 | 63 | va_start(args, cmd_); |
dan_ackme | 29:b6af04b77a56 | 64 | initialize(0, NULL, timeoutMs_, ReaderFunc(), NULL, cmd_, args); |
dan_ackme | 29:b6af04b77a56 | 65 | va_end(args); |
dan_ackme | 29:b6af04b77a56 | 66 | } |
dan_ackme | 29:b6af04b77a56 | 67 | |
dan_ackme | 29:b6af04b77a56 | 68 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 69 | QueuedCommand::QueuedCommand(const char *cmd_, ...) |
dan_ackme | 29:b6af04b77a56 | 70 | { |
dan_ackme | 29:b6af04b77a56 | 71 | va_list args; |
dan_ackme | 29:b6af04b77a56 | 72 | va_start(args, cmd_); |
dan_ackme | 29:b6af04b77a56 | 73 | initialize(0, NULL, WICONNECT_DEFAULT_TIMEOUT, ReaderFunc(), NULL, cmd_, args); |
dan_ackme | 29:b6af04b77a56 | 74 | va_end(args); |
dan_ackme | 29:b6af04b77a56 | 75 | } |
dan_ackme | 29:b6af04b77a56 | 76 | |
dan_ackme | 29:b6af04b77a56 | 77 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 78 | QueuedCommand::~QueuedCommand() |
dan_ackme | 29:b6af04b77a56 | 79 | { |
dan_ackme | 29:b6af04b77a56 | 80 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 81 | if(allocatedBuffer) |
dan_ackme | 29:b6af04b77a56 | 82 | { |
dan_ackme | 29:b6af04b77a56 | 83 | Wiconnect::getInstance()->_free(responseBuffer); |
dan_ackme | 29:b6af04b77a56 | 84 | } |
dan_ackme | 29:b6af04b77a56 | 85 | #endif |
dan_ackme | 29:b6af04b77a56 | 86 | } |
dan_ackme | 29:b6af04b77a56 | 87 | |
dan_ackme | 29:b6af04b77a56 | 88 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 89 | char *QueuedCommand::getResponseBuffer() |
dan_ackme | 29:b6af04b77a56 | 90 | { |
dan_ackme | 29:b6af04b77a56 | 91 | return responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 92 | } |
dan_ackme | 29:b6af04b77a56 | 93 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 94 | int QueuedCommand::getResponseBufferLen() |
dan_ackme | 29:b6af04b77a56 | 95 | { |
dan_ackme | 29:b6af04b77a56 | 96 | return responseBufferLen; |
dan_ackme | 29:b6af04b77a56 | 97 | } |
dan_ackme | 29:b6af04b77a56 | 98 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 99 | int QueuedCommand::getTimeoutMs() |
dan_ackme | 29:b6af04b77a56 | 100 | { |
dan_ackme | 29:b6af04b77a56 | 101 | return timeoutMs; |
dan_ackme | 29:b6af04b77a56 | 102 | } |
dan_ackme | 29:b6af04b77a56 | 103 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 104 | ReaderFunc QueuedCommand::getReader() |
dan_ackme | 29:b6af04b77a56 | 105 | { |
dan_ackme | 29:b6af04b77a56 | 106 | return reader; |
dan_ackme | 29:b6af04b77a56 | 107 | } |
dan_ackme | 29:b6af04b77a56 | 108 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 109 | void * QueuedCommand::getReaderUserData() |
dan_ackme | 29:b6af04b77a56 | 110 | { |
dan_ackme | 29:b6af04b77a56 | 111 | return user; |
dan_ackme | 29:b6af04b77a56 | 112 | } |
dan_ackme | 29:b6af04b77a56 | 113 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 114 | char* QueuedCommand::getCommand() |
dan_ackme | 29:b6af04b77a56 | 115 | { |
dan_ackme | 29:b6af04b77a56 | 116 | return command; |
dan_ackme | 29:b6af04b77a56 | 117 | } |
dan_ackme | 29:b6af04b77a56 | 118 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 119 | Callback QueuedCommand::getCompletedCallback() |
dan_ackme | 29:b6af04b77a56 | 120 | { |
dan_ackme | 29:b6af04b77a56 | 121 | return completeCallback; |
dan_ackme | 29:b6af04b77a56 | 122 | } |
dan_ackme | 29:b6af04b77a56 | 123 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 124 | void QueuedCommand::setCompletedCallback(const Callback &cb) |
dan_ackme | 29:b6af04b77a56 | 125 | { |
dan_ackme | 29:b6af04b77a56 | 126 | completeCallback = cb; |
dan_ackme | 29:b6af04b77a56 | 127 | } |
dan_ackme | 29:b6af04b77a56 | 128 | |
dan_ackme | 29:b6af04b77a56 | 129 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 130 | QueuedCommand& QueuedCommand::operator=( const QueuedCommand& other ) |
dan_ackme | 29:b6af04b77a56 | 131 | { |
dan_ackme | 29:b6af04b77a56 | 132 | responseBuffer = other.responseBuffer; |
dan_ackme | 29:b6af04b77a56 | 133 | responseBufferLen = other.responseBufferLen; |
dan_ackme | 29:b6af04b77a56 | 134 | timeoutMs = other.timeoutMs; |
dan_ackme | 29:b6af04b77a56 | 135 | reader = other.reader; |
dan_ackme | 29:b6af04b77a56 | 136 | user = other.user; |
dan_ackme | 29:b6af04b77a56 | 137 | completeCallback = other.completeCallback; |
dan_ackme | 29:b6af04b77a56 | 138 | memcpy(command, other.command, sizeof(command)); |
dan_ackme | 29:b6af04b77a56 | 139 | return *this; |
dan_ackme | 29:b6af04b77a56 | 140 | } |
dan_ackme | 29:b6af04b77a56 | 141 | |
dan_ackme | 29:b6af04b77a56 | 142 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 143 | void* QueuedCommand::operator new(size_t size) |
dan_ackme | 29:b6af04b77a56 | 144 | { |
dan_ackme | 29:b6af04b77a56 | 145 | Wiconnect *wiconnect = Wiconnect::getInstance(); |
dan_ackme | 29:b6af04b77a56 | 146 | wiconnect_assert(wiconnect, "QueuedCommand:new malloc not defined", wiconnect->_malloc != NULL); |
dan_ackme | 29:b6af04b77a56 | 147 | return Wiconnect::getInstance()->_malloc(size); |
dan_ackme | 29:b6af04b77a56 | 148 | } |
dan_ackme | 29:b6af04b77a56 | 149 | |
dan_ackme | 29:b6af04b77a56 | 150 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 151 | void QueuedCommand::operator delete(void* ptr) |
dan_ackme | 29:b6af04b77a56 | 152 | { |
dan_ackme | 29:b6af04b77a56 | 153 | Wiconnect *wiconnect = Wiconnect::getInstance(); |
dan_ackme | 29:b6af04b77a56 | 154 | wiconnect_assert(wiconnect, "QueuedCommand:delete free not defined", wiconnect->_free != NULL); |
dan_ackme | 29:b6af04b77a56 | 155 | Wiconnect::getInstance()->_free(ptr); |
dan_ackme | 29:b6af04b77a56 | 156 | } |
dan_ackme | 29:b6af04b77a56 | 157 | |
dan_ackme | 29:b6af04b77a56 | 158 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 159 | void QueuedCommand::initialize(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList) |
dan_ackme | 29:b6af04b77a56 | 160 | { |
dan_ackme | 29:b6af04b77a56 | 161 | if(responseBufferLen_ > 0) |
dan_ackme | 29:b6af04b77a56 | 162 | { |
dan_ackme | 29:b6af04b77a56 | 163 | #ifdef WICONNECT_ENABLE_MALLOC |
dan_ackme | 29:b6af04b77a56 | 164 | Wiconnect *wiconnect = Wiconnect::getInstance(); |
dan_ackme | 29:b6af04b77a56 | 165 | allocatedBuffer = false; |
dan_ackme | 29:b6af04b77a56 | 166 | if(responseBuffer_ == NULL) |
dan_ackme | 29:b6af04b77a56 | 167 | { |
dan_ackme | 29:b6af04b77a56 | 168 | wiconnect_assert(wiconnect, "QueuedCommand() malloc not defined", wiconnect->_malloc != NULL); |
dan_ackme | 29:b6af04b77a56 | 169 | responseBuffer = (char*)wiconnect->_malloc(responseBufferLen_); |
dan_ackme | 29:b6af04b77a56 | 170 | wiconnect_assert(wiconnect, "QueuedCommand() responseBuffer malloc failed", responseBuffer != NULL); |
dan_ackme | 29:b6af04b77a56 | 171 | allocatedBuffer = true; |
dan_ackme | 29:b6af04b77a56 | 172 | } |
dan_ackme | 29:b6af04b77a56 | 173 | else |
dan_ackme | 29:b6af04b77a56 | 174 | #endif |
dan_ackme | 29:b6af04b77a56 | 175 | { |
dan_ackme | 29:b6af04b77a56 | 176 | wiconnect_assert(wiconnect, "QueuedCommand(), null buffer", responseBuffer_ != NULL); |
dan_ackme | 29:b6af04b77a56 | 177 | responseBuffer = responseBuffer_; |
dan_ackme | 29:b6af04b77a56 | 178 | } |
dan_ackme | 29:b6af04b77a56 | 179 | } |
dan_ackme | 29:b6af04b77a56 | 180 | responseBufferLen = responseBufferLen_; |
dan_ackme | 29:b6af04b77a56 | 181 | timeoutMs = timeoutMs_; |
dan_ackme | 29:b6af04b77a56 | 182 | reader = reader_; |
dan_ackme | 29:b6af04b77a56 | 183 | user = user_; |
dan_ackme | 29:b6af04b77a56 | 184 | userData = NULL; |
dan_ackme | 29:b6af04b77a56 | 185 | |
dan_ackme | 29:b6af04b77a56 | 186 | if(cmd_ != NULL) |
dan_ackme | 29:b6af04b77a56 | 187 | { |
dan_ackme | 29:b6af04b77a56 | 188 | int len = vsnprintf(command, sizeof(command)-3, cmd_, vaList); |
dan_ackme | 29:b6af04b77a56 | 189 | command[len++] = '\r'; |
dan_ackme | 29:b6af04b77a56 | 190 | command[len++] = '\n'; |
dan_ackme | 29:b6af04b77a56 | 191 | command[len] = 0; |
dan_ackme | 29:b6af04b77a56 | 192 | } |
dan_ackme | 29:b6af04b77a56 | 193 | } |
dan_ackme | 29:b6af04b77a56 | 194 | |
dan_ackme | 29:b6af04b77a56 | 195 | |
dan_ackme | 29:b6af04b77a56 | 196 | #endif |