Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*
     18  *  Store data bytes in a variable-size queue.
     19  */
     20 
     21 #include "DataQueue.h"
     22 
     23 
     24 /*******************************************************************************
     25 **
     26 ** Function:        DataQueue
     27 **
     28 ** Description:     Initialize member variables.
     29 **
     30 ** Returns:         None.
     31 **
     32 *******************************************************************************/
     33 DataQueue::DataQueue ()
     34 {
     35 }
     36 
     37 
     38 /*******************************************************************************
     39 **
     40 ** Function:        ~DataQueue
     41 **
     42 ** Description:      Release all resources.
     43 **
     44 ** Returns:         None.
     45 **
     46 *******************************************************************************/
     47 DataQueue::~DataQueue ()
     48 {
     49     mMutex.lock ();
     50     while (mQueue.empty() == false)
     51     {
     52         tHeader* header = mQueue.front ();
     53         mQueue.pop_front ();
     54         free (header);
     55     }
     56     mMutex.unlock ();
     57 }
     58 
     59 
     60 bool DataQueue::isEmpty()
     61 {
     62     mMutex.lock ();
     63     bool retval = mQueue.empty();
     64     mMutex.unlock ();
     65     return retval;
     66 }
     67 
     68 
     69 /*******************************************************************************
     70 **
     71 ** Function:        enqueue
     72 **
     73 ** Description:     Append data to the queue.
     74 **                  data: array of bytes
     75 **                  dataLen: length of the data.
     76 **
     77 ** Returns:         True if ok.
     78 **
     79 *******************************************************************************/
     80 bool DataQueue::enqueue (UINT8* data, UINT16 dataLen)
     81 {
     82     if ((data == NULL) || (dataLen==0))
     83         return false;
     84 
     85     mMutex.lock ();
     86 
     87     bool retval = false;
     88     tHeader* header = (tHeader*) malloc (sizeof(tHeader) + dataLen);
     89 
     90     if (header)
     91     {
     92         memset (header, 0, sizeof(tHeader));
     93         header->mDataLen = dataLen;
     94         memcpy (header+1, data, dataLen);
     95 
     96         mQueue.push_back (header);
     97 
     98         retval = true;
     99     }
    100     else
    101     {
    102         ALOGE ("DataQueue::enqueue: out of memory ?????");
    103     }
    104     mMutex.unlock ();
    105     return retval;
    106 }
    107 
    108 
    109 /*******************************************************************************
    110 **
    111 ** Function:        dequeue
    112 **
    113 ** Description:     Retrieve and remove data from the front of the queue.
    114 **                  buffer: array to store the data.
    115 **                  bufferMaxLen: maximum size of the buffer.
    116 **                  actualLen: actual length of the data.
    117 **
    118 ** Returns:         True if ok.
    119 **
    120 *******************************************************************************/
    121 bool DataQueue::dequeue (UINT8* buffer, UINT16 bufferMaxLen, UINT16& actualLen)
    122 {
    123     mMutex.lock ();
    124 
    125     tHeader* header = mQueue.front ();
    126     bool retval = false;
    127 
    128     if (header && buffer && (bufferMaxLen>0))
    129     {
    130         if (header->mDataLen <= bufferMaxLen)
    131         {
    132             //caller's buffer is big enough to store all data
    133             actualLen = header->mDataLen;
    134             char* src = (char*)(header) + sizeof(tHeader) + header->mOffset;
    135             memcpy (buffer, src, actualLen);
    136 
    137             mQueue.pop_front ();
    138             free (header);
    139         }
    140         else
    141         {
    142             //caller's buffer is too small
    143             actualLen = bufferMaxLen;
    144             char* src = (char*)(header) + sizeof(tHeader) + header->mOffset;
    145             memcpy (buffer, src, actualLen);
    146             //adjust offset so the next dequeue() will get the remainder
    147             header->mDataLen -= actualLen;
    148             header->mOffset += actualLen;
    149         }
    150         retval = true;
    151     }
    152     mMutex.unlock ();
    153     return retval;
    154 }
    155 
    156