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