• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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 #define LOG_TAG "bt_btif_queue"
28 
29 #include "btif_profile_queue.h"
30 
31 #include <base/bind.h>
32 #include <base/callback.h>
33 #include <base/logging.h>
34 #include <base/strings/stringprintf.h>
35 #include <string.h>
36 #include <list>
37 
38 #include "bt_common.h"
39 #include "btif_common.h"
40 #include "stack_manager.h"
41 
42 /*******************************************************************************
43  *  Local type definitions
44  ******************************************************************************/
45 
46 // Class to store connect info.
47 class ConnectNode {
48  public:
ConnectNode(const RawAddress & address,uint16_t uuid,btif_connect_cb_t connect_cb)49   ConnectNode(const RawAddress& address, uint16_t uuid,
50               btif_connect_cb_t connect_cb)
51       : address_(address), uuid_(uuid), busy_(false), connect_cb_(connect_cb) {}
52 
ToString() const53   std::string ToString() const {
54     return base::StringPrintf("address=%s UUID=%04X busy=%s",
55                               address_.ToString().c_str(), uuid_,
56                               (busy_) ? "true" : "false");
57   }
58 
address() const59   const RawAddress& address() const { return address_; }
uuid() const60   uint16_t uuid() const { return uuid_; }
61 
62   /**
63    * Initiate the connection.
64    *
65    * @return BT_STATUS_SUCCESS on success, othewise the corresponding error
66    * code. Note: if a previous connect request hasn't been completed, the
67    * return value is BT_STATUS_SUCCESS.
68    */
connect()69   bt_status_t connect() {
70     if (busy_) return BT_STATUS_SUCCESS;
71     busy_ = true;
72     return connect_cb_(&address_, uuid_);
73   }
74 
75  private:
76   RawAddress address_;
77   uint16_t uuid_;
78   bool busy_;
79   btif_connect_cb_t connect_cb_;
80 };
81 
82 /*******************************************************************************
83  *  Static variables
84  ******************************************************************************/
85 
86 static std::list<ConnectNode> connect_queue;
87 
88 static const size_t MAX_REASONABLE_REQUESTS = 20;
89 
90 /*******************************************************************************
91  *  Queue helper functions
92  ******************************************************************************/
93 
queue_int_add(uint16_t uuid,const RawAddress & bda,btif_connect_cb_t connect_cb)94 static void queue_int_add(uint16_t uuid, const RawAddress& bda,
95                           btif_connect_cb_t connect_cb) {
96   // Sanity check to make sure we're not leaking connection requests
97   CHECK(connect_queue.size() < MAX_REASONABLE_REQUESTS);
98 
99   ConnectNode param(bda, uuid, connect_cb);
100   for (const auto& node : connect_queue) {
101     if (node.uuid() == param.uuid() && node.address() == param.address()) {
102       LOG_ERROR(LOG_TAG, "%s: dropping duplicate connection request: %s",
103                 __func__, param.ToString().c_str());
104       return;
105     }
106   }
107 
108   LOG_INFO(LOG_TAG, "%s: adding connection request: %s", __func__,
109            param.ToString().c_str());
110   connect_queue.push_back(param);
111 
112   btif_queue_connect_next();
113 }
114 
queue_int_advance()115 static void queue_int_advance() {
116   if (connect_queue.empty()) return;
117 
118   const ConnectNode& head = connect_queue.front();
119   LOG_INFO(LOG_TAG, "%s: removing connection request: %s", __func__,
120            head.ToString().c_str());
121   connect_queue.pop_front();
122 
123   btif_queue_connect_next();
124 }
125 
queue_int_cleanup(uint16_t uuid)126 static void queue_int_cleanup(uint16_t uuid) {
127   LOG_INFO(LOG_TAG, "%s: UUID=%04X", __func__, uuid);
128 
129   for (auto it = connect_queue.begin(); it != connect_queue.end();) {
130     auto it_prev = it++;
131     const ConnectNode& node = *it_prev;
132     if (node.uuid() == uuid) {
133       LOG_INFO(LOG_TAG, "%s: removing connection request: %s", __func__,
134                node.ToString().c_str());
135       connect_queue.erase(it_prev);
136     }
137   }
138 }
139 
queue_int_release()140 static void queue_int_release() { connect_queue.clear(); }
141 
142 /*******************************************************************************
143  *
144  * Function         btif_queue_connect
145  *
146  * Description      Add a new connection to the queue and trigger the next
147  *                  scheduled connection.
148  *
149  * Returns          BT_STATUS_SUCCESS if successful
150  *
151  ******************************************************************************/
btif_queue_connect(uint16_t uuid,const RawAddress * bda,btif_connect_cb_t connect_cb)152 bt_status_t btif_queue_connect(uint16_t uuid, const RawAddress* bda,
153                                btif_connect_cb_t connect_cb) {
154   return do_in_jni_thread(FROM_HERE,
155                           base::Bind(&queue_int_add, uuid, *bda, connect_cb));
156 }
157 
158 /*******************************************************************************
159  *
160  * Function         btif_queue_cleanup
161  *
162  * Description      Clean up existing connection requests for a UUID
163  *
164  * Returns          void, always succeed
165  *
166  ******************************************************************************/
btif_queue_cleanup(uint16_t uuid)167 void btif_queue_cleanup(uint16_t uuid) {
168   do_in_jni_thread(FROM_HERE, base::Bind(&queue_int_cleanup, uuid));
169 }
170 
171 /*******************************************************************************
172  *
173  * Function         btif_queue_advance
174  *
175  * Description      Clear the queue's busy status and advance to the next
176  *                  scheduled connection.
177  *
178  * Returns          void
179  *
180  ******************************************************************************/
btif_queue_advance()181 void btif_queue_advance() {
182   do_in_jni_thread(FROM_HERE, base::Bind(&queue_int_advance));
183 }
184 
btif_queue_connect_next(void)185 bt_status_t btif_queue_connect_next(void) {
186   // The call must be on the JNI thread, otherwise the access to connect_queue
187   // is not thread-safe.
188   CHECK(is_on_jni_thread());
189 
190   if (connect_queue.empty()) return BT_STATUS_FAIL;
191   if (!stack_manager_get_interface()->get_stack_is_running())
192     return BT_STATUS_FAIL;
193 
194   ConnectNode& head = connect_queue.front();
195 
196   LOG_INFO(LOG_TAG, "%s: executing connection request: %s", __func__,
197            head.ToString().c_str());
198   return head.connect();
199 }
200 
201 /*******************************************************************************
202  *
203  * Function         btif_queue_release
204  *
205  * Description      Free up all the queue nodes and set the queue head to NULL
206  *
207  * Returns          void
208  *
209  ******************************************************************************/
btif_queue_release()210 void btif_queue_release() {
211   LOG_INFO(LOG_TAG, "%s", __func__);
212   if (do_in_jni_thread(FROM_HERE, base::Bind(&queue_int_release)) !=
213       BT_STATUS_SUCCESS) {
214     LOG(FATAL) << __func__ << ": Failed to schedule on JNI thread";
215   }
216 }
217