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 #pragma once
22 #include <list>
23 #include "Mutex.h"
24 #include "NfcJniUtil.h"
25 #include "gki.h"
26 
27 class DataQueue {
28  public:
29   /*******************************************************************************
30   **
31   ** Function:        DataQueue
32   **
33   ** Description:     Initialize member variables.
34   **
35   ** Returns:         None.
36   **
37   *******************************************************************************/
38   DataQueue();
39 
40   /*******************************************************************************
41   **
42   ** Function:        ~DataQueue
43   **
44   ** Description:      Release all resources.
45   **
46   ** Returns:         None.
47   **
48   *******************************************************************************/
49   ~DataQueue();
50 
51   /*******************************************************************************
52   **
53   ** Function:        enqueue
54   **
55   ** Description:     Append data to the queue.
56   **                  data: array of bytes
57   **                  dataLen: length of the data.
58   **
59   ** Returns:         True if ok.
60   **
61   *******************************************************************************/
62   bool enqueue(uint8_t* data, uint16_t dataLen);
63 
64   /*******************************************************************************
65   **
66   ** Function:        dequeue
67   **
68   ** Description:     Retrieve and remove data from the front of the queue.
69   **                  buffer: array to store the data.
70   **                  bufferMaxLen: maximum size of the buffer.
71   **                  actualLen: actual length of the data.
72   **
73   ** Returns:         True if ok.
74   **
75   *******************************************************************************/
76   bool dequeue(uint8_t* buffer, uint16_t bufferMaxLen, uint16_t& actualLen);
77 
78   /*******************************************************************************
79   **
80   ** Function:        isEmpty
81   **
82   ** Description:     Whether the queue is empty.
83   **
84   ** Returns:         True if empty.
85   **
86   *******************************************************************************/
87   bool isEmpty();
88 
89  private:
90   struct tHeader {
91     uint16_t mDataLen;  // number of octets of data
92     uint16_t mOffset;   // offset of the first octet of data
93   };
94   typedef std::list<tHeader*> Queue;
95 
96   Queue mQueue;
97   Mutex mMutex;
98 };
99