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