OSDN Git Service

DO NOT MERGE Use POSIX timer API for wake alarms instead of OSI callouts.
[android-x86/system-bt.git] / btif / src / btif_profile_queue.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /*******************************************************************************
20  *
21  *  Filename:      btif_profile_queue.c
22  *
23  *  Description:   Bluetooth remote device connection queuing implementation.
24  *
25  ******************************************************************************/
26
27 #include <assert.h>
28 #include <string.h>
29 #include <hardware/bluetooth.h>
30 #include <string.h>
31
32 #define LOG_TAG "bt_btif_queue"
33 #include "btif_common.h"
34 #include "btif_profile_queue.h"
35 #include "gki.h"
36 #include "osi/include/list.h"
37 #include "osi/include/allocator.h"
38 #include "stack_manager.h"
39
40 /*******************************************************************************
41 **  Local type definitions
42 *******************************************************************************/
43
44 typedef enum {
45   BTIF_QUEUE_CONNECT_EVT,
46   BTIF_QUEUE_ADVANCE_EVT,
47 } btif_queue_event_t;
48
49 typedef struct {
50     bt_bdaddr_t bda;
51     uint16_t uuid;
52     bool busy;
53     btif_connect_cb_t connect_cb;
54 } connect_node_t;
55
56 /*******************************************************************************
57 **  Static variables
58 *******************************************************************************/
59
60 static list_t *connect_queue;
61
62 static const size_t MAX_REASONABLE_REQUESTS = 10;
63
64 /*******************************************************************************
65 **  Queue helper functions
66 *******************************************************************************/
67
68 static void queue_int_add(connect_node_t *p_param) {
69     if (!connect_queue) {
70         connect_queue = list_new(osi_free);
71         assert(connect_queue != NULL);
72     }
73
74     // Sanity check to make sure we're not leaking connection requests
75     assert(list_length(connect_queue) < MAX_REASONABLE_REQUESTS);
76
77     for (const list_node_t *node = list_begin(connect_queue); node != list_end(connect_queue); node = list_next(node)) {
78         if (((connect_node_t *)list_node(node))->uuid == p_param->uuid) {
79             LOG_INFO("%s dropping duplicate connect request for uuid: %04x", __func__, p_param->uuid);
80             return;
81         }
82     }
83
84     connect_node_t *p_node = osi_malloc(sizeof(connect_node_t));
85     assert(p_node != NULL);
86     memcpy(p_node, p_param, sizeof(connect_node_t));
87     list_append(connect_queue, p_node);
88 }
89
90 static void queue_int_advance() {
91     if (connect_queue && !list_is_empty(connect_queue))
92         list_remove(connect_queue, list_front(connect_queue));
93 }
94
95 static void queue_int_handle_evt(UINT16 event, char *p_param) {
96     switch(event) {
97         case BTIF_QUEUE_CONNECT_EVT:
98             queue_int_add((connect_node_t *)p_param);
99             break;
100
101         case BTIF_QUEUE_ADVANCE_EVT:
102             queue_int_advance();
103             break;
104     }
105
106     if (stack_manager_get_interface()->get_stack_is_running())
107         btif_queue_connect_next();
108 }
109
110 /*******************************************************************************
111 **
112 ** Function         btif_queue_connect
113 **
114 ** Description      Add a new connection to the queue and trigger the next
115 **                  scheduled connection.
116 **
117 ** Returns          BT_STATUS_SUCCESS if successful
118 **
119 *******************************************************************************/
120 bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btif_connect_cb_t connect_cb) {
121     connect_node_t node;
122     memset(&node, 0, sizeof(connect_node_t));
123     memcpy(&node.bda, bda, sizeof(bt_bdaddr_t));
124     node.uuid = uuid;
125     node.connect_cb = connect_cb;
126
127     return btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_CONNECT_EVT,
128                           (char *)&node, sizeof(connect_node_t), NULL);
129 }
130
131 /*******************************************************************************
132 **
133 ** Function         btif_queue_advance
134 **
135 ** Description      Clear the queue's busy status and advance to the next
136 **                  scheduled connection.
137 **
138 ** Returns          void
139 **
140 *******************************************************************************/
141 void btif_queue_advance() {
142     btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_ADVANCE_EVT,
143                           NULL, 0, NULL);
144 }
145
146 // This function dispatches the next pending connect request. It is called from
147 // stack_manager when the stack comes up.
148 bt_status_t btif_queue_connect_next(void) {
149     if (!connect_queue || list_is_empty(connect_queue))
150         return BT_STATUS_FAIL;
151
152     connect_node_t *p_head = list_front(connect_queue);
153
154     // If the queue is currently busy, we return success anyway,
155     // since the connection has been queued...
156     if (p_head->busy)
157         return BT_STATUS_SUCCESS;
158
159     p_head->busy = true;
160     return p_head->connect_cb(&p_head->bda, p_head->uuid);
161 }
162
163
164 /*******************************************************************************
165 **
166 ** Function         btif_queue_release
167 **
168 ** Description      Free up all the queue nodes and set the queue head to NULL
169 **
170 ** Returns          void
171 **
172 *******************************************************************************/
173 void btif_queue_release() {
174     list_free(connect_queue);
175     connect_queue = NULL;
176 }