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