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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers QueuedCommand.cpp Source File

QueuedCommand.cpp

00001 /**
00002  * ACKme WiConnect Host Library is licensed under the BSD licence: 
00003  * 
00004  * Copyright (c)2014 ACKme Networks.
00005  * All rights reserved. 
00006  * 
00007  * Redistribution and use in source and binary forms, with or without modification, 
00008  * are permitted provided that the following conditions are met: 
00009  * 
00010  * 1. Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * 2. Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * 3. The name of the author may not be used to endorse or promote products 
00016  * derived from this software without specific prior written permission. 
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED 
00019  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00020  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00021  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00023  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00026  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00027  * OF SUCH DAMAGE.
00028  */
00029 
00030 #include "Wiconnect.h"
00031 #include "internal/common.h"
00032 
00033 #ifdef WICONNECT_ASYNC_TIMER_ENABLED
00034 
00035 /*************************************************************************************************/
00036 QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList)
00037 {
00038     initialize(responseBufferLen_, responseBuffer_, timeoutMs_, reader_, user_, cmd_, vaList);
00039 }
00040 
00041 /*************************************************************************************************/
00042 QueuedCommand::QueuedCommand(int responseBufferLen_, char* responseBuffer_, int timeoutMs_, const char *cmd_, ...)
00043 {
00044     va_list args;
00045     va_start(args, cmd_);
00046     initialize(responseBufferLen_, responseBuffer_, timeoutMs_, ReaderFunc(), NULL, cmd_, args);
00047     va_end(args);
00048 }
00049 
00050 /*************************************************************************************************/
00051 QueuedCommand::QueuedCommand(int responseBufferLen_, char *responseBuffer_, const char *cmd_, ...)
00052 {
00053     va_list args;
00054     va_start(args, cmd_);
00055     initialize(responseBufferLen_, responseBuffer_, WICONNECT_DEFAULT_TIMEOUT, ReaderFunc(), NULL, cmd_, args);
00056     va_end(args);
00057 }
00058 
00059 /*************************************************************************************************/
00060 QueuedCommand::QueuedCommand(int timeoutMs_, const char *cmd_, ...)
00061 {
00062     va_list args;
00063     va_start(args, cmd_);
00064     initialize(0, NULL, timeoutMs_, ReaderFunc(), NULL, cmd_, args);
00065     va_end(args);
00066 }
00067 
00068 /*************************************************************************************************/
00069 QueuedCommand::QueuedCommand(const char *cmd_, ...)
00070 {
00071     va_list args;
00072     va_start(args, cmd_);
00073     initialize(0, NULL, WICONNECT_DEFAULT_TIMEOUT, ReaderFunc(), NULL, cmd_, args);
00074     va_end(args);
00075 }
00076 
00077 /*************************************************************************************************/
00078 QueuedCommand::~QueuedCommand()
00079 {
00080 #ifdef WICONNECT_ENABLE_MALLOC
00081     if(allocatedBuffer)
00082     {
00083         Wiconnect::getInstance()->_free(responseBuffer);
00084     }
00085 #endif
00086 }
00087 
00088 /*************************************************************************************************/
00089 char *QueuedCommand::getResponseBuffer()
00090 {
00091     return responseBuffer;
00092 }
00093 /*************************************************************************************************/
00094 int QueuedCommand::getResponseBufferLen()
00095 {
00096     return responseBufferLen;
00097 }
00098 /*************************************************************************************************/
00099 int QueuedCommand::getTimeoutMs()
00100 {
00101     return timeoutMs;
00102 }
00103 /*************************************************************************************************/
00104 ReaderFunc QueuedCommand::getReader()
00105 {
00106     return reader;
00107 }
00108 /*************************************************************************************************/
00109 void * QueuedCommand::getReaderUserData()
00110 {
00111     return user;
00112 }
00113 /*************************************************************************************************/
00114 char* QueuedCommand::getCommand()
00115 {
00116     return command;
00117 }
00118 /*************************************************************************************************/
00119 Callback QueuedCommand::getCompletedCallback()
00120 {
00121     return completeCallback;
00122 }
00123 /*************************************************************************************************/
00124 void QueuedCommand::setCompletedCallback(const Callback &cb)
00125 {
00126     completeCallback = cb;
00127 }
00128 
00129 /*************************************************************************************************/
00130 QueuedCommand& QueuedCommand::operator=( const QueuedCommand& other )
00131 {
00132     responseBuffer = other.responseBuffer;
00133     responseBufferLen = other.responseBufferLen;
00134     timeoutMs = other.timeoutMs;
00135     reader = other.reader;
00136     user = other.user;
00137     completeCallback = other.completeCallback;
00138     memcpy(command, other.command, sizeof(command));
00139     return *this;
00140 }
00141 
00142 /*************************************************************************************************/
00143 void* QueuedCommand::operator new(size_t size)
00144 {
00145     Wiconnect *wiconnect = Wiconnect::getInstance();
00146     wiconnect_assert(wiconnect, "QueuedCommand:new malloc not defined", wiconnect->_malloc != NULL);
00147     return Wiconnect::getInstance()->_malloc(size);
00148 }
00149 
00150 /*************************************************************************************************/
00151 void QueuedCommand::operator delete(void* ptr)
00152 {
00153     Wiconnect *wiconnect = Wiconnect::getInstance();
00154     wiconnect_assert(wiconnect, "QueuedCommand:delete free not defined", wiconnect->_free != NULL);
00155     Wiconnect::getInstance()->_free(ptr);
00156 }
00157 
00158 /*************************************************************************************************/
00159 void QueuedCommand::initialize(int responseBufferLen_, char *responseBuffer_, int timeoutMs_, const ReaderFunc &reader_, void *user_, const char *cmd_, va_list vaList)
00160 {
00161     if(responseBufferLen_ > 0)
00162     {
00163 #ifdef WICONNECT_ENABLE_MALLOC
00164         Wiconnect *wiconnect = Wiconnect::getInstance();
00165         allocatedBuffer = false;
00166         if(responseBuffer_ == NULL)
00167         {
00168             wiconnect_assert(wiconnect, "QueuedCommand() malloc not defined", wiconnect->_malloc != NULL);
00169             responseBuffer = (char*)wiconnect->_malloc(responseBufferLen_);
00170             wiconnect_assert(wiconnect, "QueuedCommand() responseBuffer malloc failed", responseBuffer != NULL);
00171             allocatedBuffer = true;
00172         }
00173         else
00174 #endif
00175         {
00176             wiconnect_assert(wiconnect, "QueuedCommand(), null buffer", responseBuffer_ != NULL);
00177             responseBuffer = responseBuffer_;
00178         }
00179     }
00180     responseBufferLen = responseBufferLen_;
00181     timeoutMs = timeoutMs_;
00182     reader = reader_;
00183     user = user_;
00184     userData = NULL;
00185 
00186     if(cmd_ != NULL)
00187     {
00188         int len = vsnprintf(command, sizeof(command)-3, cmd_, vaList);
00189         command[len++] = '\r';
00190         command[len++] = '\n';
00191         command[len] = 0;
00192     }
00193 }
00194 
00195 
00196 #endif