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 <bluetooth/log.h>
34 #include <string.h>
35 
36 #include <cstdint>
37 #include <list>
38 #include <string>
39 
40 #include "btif/include/stack_manager_t.h"
41 #include "btif_common.h"
42 #include "hardware/bluetooth.h"
43 #include "types/raw_address.h"
44 
45 using namespace bluetooth;
46 
47 /*******************************************************************************
48  *  Local type definitions
49  ******************************************************************************/
50 
51 // Class to store connect info.
52 class ConnectNode {
53 public:
ConnectNode(const RawAddress & address,uint16_t uuid,btif_connect_cb_t connect_cb)54   ConnectNode(const RawAddress& address, uint16_t uuid, btif_connect_cb_t connect_cb)
55       : address_(address), uuid_(uuid), busy_(false), connect_cb_(connect_cb) {}
56 
ToString() const57   std::string ToString() const {
58     return std::format("address={} UUID={:04X} busy={}", address_, uuid_, busy_);
59   }
60 
address() const61   const RawAddress& address() const { return address_; }
uuid() const62   uint16_t uuid() const { return uuid_; }
63 
64   /**
65    * Initiate the connection.
66    *
67    * @return BT_STATUS_SUCCESS on success, othewise the corresponding error
68    * code. Note: if a previous connect request hasn't been completed, the
69    * return value is BT_STATUS_SUCCESS.
70    */
connect()71   bt_status_t connect() {
72     if (busy_) {
73       return BT_STATUS_SUCCESS;
74     }
75     busy_ = true;
76     return connect_cb_(&address_, uuid_);
77   }
78 
79 private:
80   RawAddress address_;
81   uint16_t uuid_;
82   bool busy_;
83   btif_connect_cb_t connect_cb_;
84 };
85 
86 /*******************************************************************************
87  *  Static variables
88  ******************************************************************************/
89 
90 static std::list<ConnectNode> connect_queue;
91 
92 static const size_t MAX_REASONABLE_REQUESTS = 20;
93 
94 /*******************************************************************************
95  *  Queue helper functions
96  ******************************************************************************/
97 
queue_int_add(uint16_t uuid,const RawAddress & bda,btif_connect_cb_t connect_cb)98 static void queue_int_add(uint16_t uuid, const RawAddress& bda, btif_connect_cb_t connect_cb) {
99   // Sanity check to make sure we're not leaking connection requests
100   log::assert_that(connect_queue.size() < MAX_REASONABLE_REQUESTS,
101                    "assert failed: connect_queue.size() < MAX_REASONABLE_REQUESTS");
102 
103   ConnectNode param(bda, uuid, connect_cb);
104   for (const auto& node : connect_queue) {
105     if (node.uuid() == param.uuid() && node.address() == param.address()) {
106       log::error("Dropping duplicate profile connection request:{}", param.ToString());
107       return;
108     }
109   }
110 
111   log::info("Queueing profile connection request:{}", param.ToString());
112   connect_queue.push_back(param);
113 
114   btif_queue_connect_next();
115 }
116 
queue_int_advance()117 static void queue_int_advance() {
118   if (connect_queue.empty()) {
119     return;
120   }
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, btif_connect_cb_t connect_cb) {
155   return do_in_jni_thread(base::BindOnce(&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(base::BindOnce(&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() { do_in_jni_thread(base::BindOnce(&queue_int_advance)); }
182 
btif_queue_connect_next(void)183 bt_status_t btif_queue_connect_next(void) {
184   // The call must be on the JNI thread, otherwise the access to connect_queue
185   // is not thread-safe.
186   log::assert_that(is_on_jni_thread(), "assert failed: is_on_jni_thread()");
187 
188   if (connect_queue.empty()) {
189     return BT_STATUS_FAIL;
190   }
191   if (!stack_manager_get_interface()->get_stack_is_running()) {
192     return BT_STATUS_UNEXPECTED_STATE;
193   }
194 
195   ConnectNode& head = connect_queue.front();
196 
197   log::info("Executing profile connection request:{}", head.ToString());
198   bt_status_t b_status = head.connect();
199   if (b_status != BT_STATUS_SUCCESS) {
200     log::info("connect {} failed, advance to next scheduled connection.", head.ToString());
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("");
217   if (do_in_jni_thread(base::BindOnce(&queue_int_release)) != BT_STATUS_SUCCESS) {
218     log::fatal("Failed to schedule on JNI thread");
219   }
220 }
221