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 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25 #include <malloc.h>
26 #include <string.h>
27 
28 using android::base::StringPrintf;
29 /*******************************************************************************
30 **
31 ** Function:        DataQueue
32 **
33 ** Description:     Initialize member variables.
34 **
35 ** Returns:         None.
36 **
37 *******************************************************************************/
DataQueue()38 DataQueue::DataQueue() {}
39 
40 /*******************************************************************************
41 **
42 ** Function:        ~DataQueue
43 **
44 ** Description:      Release all resources.
45 **
46 ** Returns:         None.
47 **
48 *******************************************************************************/
~DataQueue()49 DataQueue::~DataQueue() {
50   mMutex.lock();
51   while (mQueue.empty() == false) {
52     tHeader* header = mQueue.front();
53     mQueue.pop_front();
54     free(header);
55   }
56   mMutex.unlock();
57 }
58 
isEmpty()59 bool DataQueue::isEmpty() {
60   mMutex.lock();
61   bool retval = mQueue.empty();
62   mMutex.unlock();
63   return retval;
64 }
65 
66 /*******************************************************************************
67 **
68 ** Function:        enqueue
69 **
70 ** Description:     Append data to the queue.
71 **                  data: array of bytes
72 **                  dataLen: length of the data.
73 **
74 ** Returns:         True if ok.
75 **
76 *******************************************************************************/
enqueue(uint8_t * data,uint16_t dataLen)77 bool DataQueue::enqueue(uint8_t* data, uint16_t dataLen) {
78   if ((data == NULL) || (dataLen == 0)) return false;
79 
80   mMutex.lock();
81 
82   bool retval = false;
83   tHeader* header = (tHeader*)malloc(sizeof(tHeader) + dataLen);
84 
85   if (header) {
86     memset(header, 0, sizeof(tHeader));
87     header->mDataLen = dataLen;
88     memcpy(header + 1, data, dataLen);
89 
90     mQueue.push_back(header);
91 
92     retval = true;
93   } else {
94     LOG(ERROR) << StringPrintf("DataQueue::enqueue: out of memory ?????");
95   }
96   mMutex.unlock();
97   return retval;
98 }
99 
100 /*******************************************************************************
101 **
102 ** Function:        dequeue
103 **
104 ** Description:     Retrieve and remove data from the front of the queue.
105 **                  buffer: array to store the data.
106 **                  bufferMaxLen: maximum size of the buffer.
107 **                  actualLen: actual length of the data.
108 **
109 ** Returns:         True if ok.
110 **
111 *******************************************************************************/
dequeue(uint8_t * buffer,uint16_t bufferMaxLen,uint16_t & actualLen)112 bool DataQueue::dequeue(uint8_t* buffer, uint16_t bufferMaxLen,
113                         uint16_t& actualLen) {
114   mMutex.lock();
115 
116   tHeader* header = mQueue.front();
117   bool retval = false;
118 
119   if (header && buffer && (bufferMaxLen > 0)) {
120     if (header->mDataLen <= bufferMaxLen) {
121       // caller's buffer is big enough to store all data
122       actualLen = header->mDataLen;
123       char* src = (char*)(header) + sizeof(tHeader) + header->mOffset;
124       memcpy(buffer, src, actualLen);
125 
126       mQueue.pop_front();
127       free(header);
128     } else {
129       // caller's buffer is too small
130       actualLen = bufferMaxLen;
131       char* src = (char*)(header) + sizeof(tHeader) + header->mOffset;
132       memcpy(buffer, src, actualLen);
133       // adjust offset so the next dequeue() will get the remainder
134       header->mDataLen -= actualLen;
135       header->mOffset += actualLen;
136     }
137     retval = true;
138   }
139   mMutex.unlock();
140   return retval;
141 }
142