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 GhmMessageList.cpp Source File

GhmMessageList.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 #include "Wiconnect.h"
00030 #include "api/types/GhmMessageList.h"
00031 #include "internal/common.h"
00032 
00033 
00034 /*************************************************************************************************/
00035 GhmMessageList::GhmMessageList(int bufferLen_, void *buffer_)
00036 {
00037     Wiconnect *wiconnect = Wiconnect::getInstance();
00038     wiconnect_assert(wiconnect, "GhmMessageList(), bad buffer", (bufferLen_ == 0 && buffer_ == NULL) || (bufferLen_ != 0 && buffer_ != NULL));
00039 
00040     buffer = (uint8_t*)buffer_;
00041     bufferLen = bufferLen_;
00042     reset();
00043 }
00044 
00045 /*************************************************************************************************/
00046 GhmMessageList::~GhmMessageList()
00047 {
00048     reset();
00049 }
00050 
00051 /*************************************************************************************************/
00052 void GhmMessageList::reset(void)
00053 {
00054 #ifdef WICONNECT_ENABLE_MALLOC
00055     if(buffer == NULL)
00056     {
00057         GhmMessage* result = listHead;
00058         while(result != NULL)
00059         {
00060             GhmMessage* tmp = result;
00061             result = result->next;
00062             delete tmp;
00063         }
00064     }
00065 #endif
00066     listHead = listTail = NULL;
00067     bufferPtr = buffer;
00068     bufferRemaining = bufferLen;
00069     count = 0;
00070 }
00071 
00072 /*************************************************************************************************/
00073 WiconnectResult GhmMessageList::add(const char *msgId, const char* timestamp, const char *length)
00074 {
00075     WiconnectResult result;
00076     GhmMessage *res;
00077 
00078     if(buffer == NULL)
00079     {
00080 #ifdef WICONNECT_ENABLE_MALLOC
00081         res = new GhmMessage();
00082         if(res == NULL)
00083 #endif
00084         {
00085             return WICONNECT_NULL_BUFFER;
00086         }
00087     }
00088     else
00089     {
00090         if(bufferRemaining < sizeof(GhmMessage))
00091         {
00092             return WICONNECT_OVERFLOW;
00093         }
00094         res = (GhmMessage*)bufferPtr;
00095         memset(res, 0, sizeof(GhmMessage));
00096         bufferRemaining -= sizeof(GhmMessage);
00097         bufferPtr += sizeof(GhmMessage);
00098     }
00099 
00100     if(WICONNECT_FAILED(result, res->init(msgId, timestamp, length)))
00101     {
00102         if(buffer == NULL)
00103         {
00104             delete res;
00105         }
00106     }
00107     else
00108     {
00109         if(listHead == NULL)
00110         {
00111             listHead = listTail = res;
00112         }
00113         else
00114         {
00115             res->previous = listTail;
00116             listTail->next = res;
00117             listTail = res;
00118         }
00119         ++count;
00120     }
00121 
00122     return result;
00123 }
00124 
00125 /*************************************************************************************************/
00126 const GhmMessage* GhmMessageList::getListHead() const
00127 {
00128     return listHead;
00129 }
00130 
00131 /*************************************************************************************************/
00132 int GhmMessageList::getCount() const
00133 {
00134     return count;
00135 }
00136 
00137 /*************************************************************************************************/
00138 const GhmMessage* GhmMessageList::getResult(int i) const
00139 {
00140     if(i >= count)
00141         return NULL;
00142 
00143     GhmMessage* result = listHead;
00144     while(i-- != 0)
00145         result = result->next;
00146 
00147     return result;
00148 }
00149 
00150 /*************************************************************************************************/
00151 const GhmMessage* GhmMessageList::operator [](int i) const
00152 {
00153     return getResult(i);
00154 }
00155 
00156 
00157 
00158 
00159