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 "main/shim/dumpsys.h"
41 #include "stack_manager.h"
42 
43 /*******************************************************************************
44  *  Local type definitions
45  ******************************************************************************/
46 
47 // Class to store connect info.
48 class ConnectNode {
49  public:
ConnectNode(const RawAddress & address,uint16_t uuid,btif_connect_cb_t connect_cb)50   ConnectNode(const RawAddress& address, uint16_t uuid,
51               btif_connect_cb_t connect_cb)
52       : address_(address), uuid_(uuid), busy_(false), connect_cb_(connect_cb) {}
53 
ToString() const54   std::string ToString() const {
55     return base::StringPrintf("address=%s UUID=%04X busy=%s",
56                               PRIVATE_ADDRESS(address_), uuid_,
57                               (busy_) ? "true" : "false");
58   }
59 
address() const60   const RawAddress& address() const { return address_; }
uuid() const61   uint16_t uuid() const { return uuid_; }
62 
63   /**
64    * Initiate the connection.
65    *
66    * @return BT_STATUS_SUCCESS on success, othewise the corresponding error
67    * code. Note: if a previous connect request hasn't been completed, the
68    * return value is BT_STATUS_SUCCESS.
69    */
connect()70   bt_status_t connect() {
71     if (busy_) return BT_STATUS_SUCCESS;
72     busy_ = true;
73     return connect_cb_(&address_, uuid_);
74   }
75 
76  private:
77   RawAddress address_;
78   uint16_t uuid_;
79   bool busy_;
80   btif_connect_cb_t connect_cb_;
81 };
82 
83 /*******************************************************************************
84  *  Static variables
85  ******************************************************************************/
86 
87 static std::list<ConnectNode> connect_queue;
88 
89 static const size_t MAX_REASONABLE_REQUESTS = 20;
90 
91 /*******************************************************************************
92  *  Queue helper functions
93  ******************************************************************************/
94 
queue_int_add(uint16_t uuid,const RawAddress & bda,btif_connect_cb_t connect_cb)95 static void queue_int_add(uint16_t uuid, const RawAddress& bda,
96                           btif_connect_cb_t connect_cb) {
97   // Sanity check to make sure we're not leaking connection requests
98   CHECK(connect_queue.size() < MAX_REASONABLE_REQUESTS);
99 
100   ConnectNode param(bda, uuid, connect_cb);
101   for (const auto& node : connect_queue) {
102     if (node.uuid() == param.uuid() && node.address() == param.address()) {
103       LOG_ERROR("Dropping duplicate profile connection request:%s",
104                 param.ToString().c_str());
105       return;
106     }
107   }
108 
109   LOG_INFO("Queueing profile connection request:%s", 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("%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("%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("%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("Executing profile connection request:%s", head.ToString().c_str());
197   bt_status_t b_status = head.connect();
198   if (b_status != BT_STATUS_SUCCESS) {
199     LOG_INFO("%s: connect %s failed, advance to next scheduled connection.",
200              __func__, head.ToString().c_str());
201     btif_queue_advance();
202   }
203   return b_status;
204 }
205 
206 /*******************************************************************************
207  *
208  * Function         btif_queue_release
209  *
210  * Description      Free up all the queue nodes and set the queue head to NULL
211  *
212  * Returns          void
213  *
214  ******************************************************************************/
btif_queue_release()215 void btif_queue_release() {
216   LOG_INFO("%s", __func__);
217   if (do_in_jni_thread(FROM_HERE, base::Bind(&queue_int_release)) !=
218       BT_STATUS_SUCCESS) {
219     LOG(FATAL) << __func__ << ": Failed to schedule on JNI thread";
220   }
221 }
222