1 /******************************************************************************
2 *
3 * Copyright 2003-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 * This file contains the GATT client utility function.
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bt_bta_gattc"
26
27 #include <bluetooth/log.h>
28
29 #include <cstdint>
30
31 #include "bta/gatt/bta_gattc_int.h"
32 #include "hci/controller_interface.h"
33 #include "internal_include/bt_target.h"
34 #include "internal_include/bt_trace.h"
35 #include "main/shim/entry.h"
36 #include "osi/include/allocator.h"
37 #include "types/bt_transport.h"
38 #include "types/hci_role.h"
39 #include "types/raw_address.h"
40
41 using namespace bluetooth;
42
ble_acceptlist_size()43 static uint8_t ble_acceptlist_size() {
44 if (!bluetooth::shim::GetController()->SupportsBle()) {
45 return 0;
46 }
47 return bluetooth::shim::GetController()->GetLeFilterAcceptListSize();
48 }
49
50 /*******************************************************************************
51 *
52 * Function bta_gattc_cl_get_regcb
53 *
54 * Description get registration control block by client interface.
55 *
56 * Returns pointer to the regcb
57 *
58 ******************************************************************************/
bta_gattc_cl_get_regcb(uint8_t client_if)59 tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if) {
60 auto it = bta_gattc_cb.cl_rcb_map.find(client_if);
61 if (it == bta_gattc_cb.cl_rcb_map.end()) {
62 return NULL;
63 } else {
64 return it->second.get();
65 }
66 }
67 /*******************************************************************************
68 *
69 * Function bta_gattc_num_reg_app
70 *
71 * Description find the number of registered application.
72 *
73 * Returns pointer to the regcb
74 *
75 ******************************************************************************/
bta_gattc_num_reg_app(void)76 uint8_t bta_gattc_num_reg_app(void) { return (uint8_t)bta_gattc_cb.cl_rcb_map.size(); }
77 /*******************************************************************************
78 *
79 * Function bta_gattc_find_clcb_by_cif
80 *
81 * Description get clcb by client interface and remote bd adddress
82 *
83 * Returns pointer to the clcb
84 *
85 ******************************************************************************/
bta_gattc_find_clcb_by_cif(uint8_t client_if,const RawAddress & remote_bda,tBT_TRANSPORT transport)86 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if, const RawAddress& remote_bda,
87 tBT_TRANSPORT transport) {
88 for (auto& p_clcb : bta_gattc_cb.clcb_set) {
89 if (p_clcb->in_use && p_clcb->p_rcb->client_if == client_if && p_clcb->transport == transport &&
90 p_clcb->bda == remote_bda) {
91 return p_clcb.get();
92 }
93 }
94 return NULL;
95 }
96 /*******************************************************************************
97 *
98 * Function bta_gattc_find_clcb_by_conn_id
99 *
100 * Description get clcb by connection ID
101 *
102 * Returns pointer to the clcb
103 *
104 ******************************************************************************/
bta_gattc_find_clcb_by_conn_id(tCONN_ID conn_id)105 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(tCONN_ID conn_id) {
106 for (auto& p_clcb : bta_gattc_cb.clcb_set) {
107 if (p_clcb != NULL && p_clcb->in_use && p_clcb->bta_conn_id == conn_id) {
108 return p_clcb.get();
109 }
110 }
111 return NULL;
112 }
113
114 /*******************************************************************************
115 *
116 * Function bta_gattc_clcb_alloc
117 *
118 * Description allocate CLCB
119 *
120 * Returns pointer to the clcb
121 *
122 ******************************************************************************/
bta_gattc_clcb_alloc(tGATT_IF client_if,const RawAddress & remote_bda,tBT_TRANSPORT transport)123 tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tGATT_IF client_if, const RawAddress& remote_bda,
124 tBT_TRANSPORT transport) {
125 tBTA_GATTC_CLCB* p_clcb = NULL;
126
127 bta_gattc_cleanup_clcb();
128 auto [p_clcb_i, b] = bta_gattc_cb.clcb_set.emplace(std::make_unique<tBTA_GATTC_CLCB>());
129 p_clcb = p_clcb_i->get();
130
131 p_clcb->in_use = true;
132 p_clcb->status = GATT_SUCCESS;
133 p_clcb->transport = transport;
134 p_clcb->bda = remote_bda;
135 p_clcb->p_q_cmd = NULL;
136
137 p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
138
139 p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda);
140 if (p_clcb->p_srcb == NULL) {
141 p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda);
142 }
143
144 if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL) {
145 p_clcb->p_srcb->num_clcb++;
146 p_clcb->p_rcb->num_clcb++;
147 } else {
148 /* release this clcb if clcb or srcb allocation failed */
149 bta_gattc_cb.clcb_set.erase(p_clcb_i);
150 p_clcb = NULL;
151 }
152 return p_clcb;
153 }
154 /*******************************************************************************
155 *
156 * Function bta_gattc_find_alloc_clcb
157 *
158 * Description find or allocate CLCB if not found.
159 *
160 * Returns pointer to the clcb
161 *
162 ******************************************************************************/
bta_gattc_find_alloc_clcb(tGATT_IF client_if,const RawAddress & remote_bda,tBT_TRANSPORT transport)163 tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tGATT_IF client_if, const RawAddress& remote_bda,
164 tBT_TRANSPORT transport) {
165 tBTA_GATTC_CLCB* p_clcb;
166
167 p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport);
168 if (p_clcb == NULL) {
169 p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
170 }
171 return p_clcb;
172 }
173
174 /*******************************************************************************
175 *
176 * Function bta_gattc_server_disconnected
177 *
178 * Description Set server cache disconnected
179 *
180 * Returns pointer to the srcb
181 *
182 ******************************************************************************/
bta_gattc_server_disconnected(tBTA_GATTC_SERV * p_srcb)183 void bta_gattc_server_disconnected(tBTA_GATTC_SERV* p_srcb) {
184 if (p_srcb && p_srcb->connected) {
185 p_srcb->connected = false;
186 p_srcb->state = BTA_GATTC_SERV_IDLE;
187 p_srcb->mtu = 0;
188
189 // clear reallocating
190 p_srcb->gatt_database.Clear();
191 }
192 }
193
194 /*******************************************************************************
195 *
196 * Function bta_gattc_clcb_dealloc
197 *
198 * Description Deallocte a clcb
199 *
200 * Returns pointer to the clcb
201 *
202 ******************************************************************************/
bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB * p_clcb)203 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb) {
204 if (!p_clcb) {
205 log::error("p_clcb=NULL");
206 return;
207 }
208
209 tBTA_GATTC_SERV* p_srcb = p_clcb->p_srcb;
210 if (p_srcb->num_clcb) {
211 p_srcb->num_clcb--;
212 }
213
214 if (p_clcb->p_rcb->num_clcb) {
215 p_clcb->p_rcb->num_clcb--;
216 }
217
218 /* if the srcb is no longer needed, reset the state */
219 if (p_srcb->num_clcb == 0) {
220 p_srcb->connected = false;
221 p_srcb->state = BTA_GATTC_SERV_IDLE;
222 p_srcb->mtu = 0;
223
224 // clear reallocating
225 p_srcb->gatt_database.Clear();
226 }
227
228 while (!p_clcb->p_q_cmd_queue.empty()) {
229 auto p_q_cmd = p_clcb->p_q_cmd_queue.front();
230 p_clcb->p_q_cmd_queue.pop_front();
231 osi_free_and_reset((void**)&p_q_cmd);
232 }
233
234 if (p_clcb->p_q_cmd != NULL) {
235 osi_free_and_reset((void**)&p_clcb->p_q_cmd);
236 }
237
238 /* Clear p_clcb. Some of the fields are already reset e.g. p_q_cmd_queue and
239 * p_q_cmd. */
240 p_clcb->bta_conn_id = 0;
241 p_clcb->bda = {};
242 p_clcb->transport = BT_TRANSPORT_AUTO;
243 p_clcb->p_rcb = NULL;
244 p_clcb->p_srcb = NULL;
245 p_clcb->request_during_discovery = 0;
246 p_clcb->auto_update = 0;
247 p_clcb->disc_active = 0;
248 p_clcb->in_use = 0;
249 p_clcb->state = BTA_GATTC_IDLE_ST;
250 p_clcb->status = GATT_SUCCESS;
251 // in bta_gattc_sm_execute(), p_clcb is accessed again so we dealloc clcb later.
252 // it will be claned up when the client is deregistered or a new clcb is allocated.
253 bta_gattc_cb.clcb_pending_dealloc.insert(p_clcb);
254 }
255
256 /*******************************************************************************
257 *
258 * Function bta_gattc_cleanup_clcb
259 *
260 * Description cleans up resources from deallocated clcb
261 *
262 * Returns none
263 *
264 ******************************************************************************/
bta_gattc_cleanup_clcb()265 void bta_gattc_cleanup_clcb() {
266 if (bta_gattc_cb.clcb_pending_dealloc.empty()) {
267 return;
268 }
269 auto it = bta_gattc_cb.clcb_set.begin();
270 while (it != bta_gattc_cb.clcb_set.end()) {
271 if (bta_gattc_cb.clcb_pending_dealloc.contains(it->get())) {
272 it = bta_gattc_cb.clcb_set.erase(it);
273 } else {
274 it++;
275 }
276 }
277 bta_gattc_cb.clcb_pending_dealloc.clear();
278 }
279
280 /*******************************************************************************
281 *
282 * Function bta_gattc_find_srcb
283 *
284 * Description find server cache by remote bd address currently in use
285 *
286 * Returns pointer to the server cache.
287 *
288 ******************************************************************************/
bta_gattc_find_srcb(const RawAddress & bda)289 tBTA_GATTC_SERV* bta_gattc_find_srcb(const RawAddress& bda) {
290 tBTA_GATTC_SERV* p_srcb = &bta_gattc_cb.known_server[0];
291 uint8_t i;
292
293 for (i = 0; i < ble_acceptlist_size(); i++, p_srcb++) {
294 if (p_srcb->in_use && p_srcb->server_bda == bda) {
295 return p_srcb;
296 }
297 }
298 return NULL;
299 }
300
301 /*******************************************************************************
302 *
303 * Function bta_gattc_find_srvr_cache
304 *
305 * Description find server cache by remote bd address
306 *
307 * Returns pointer to the server cache.
308 *
309 ******************************************************************************/
bta_gattc_find_srvr_cache(const RawAddress & bda)310 tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(const RawAddress& bda) {
311 tBTA_GATTC_SERV* p_srcb = &bta_gattc_cb.known_server[0];
312 uint8_t i;
313
314 for (i = 0; i < ble_acceptlist_size(); i++, p_srcb++) {
315 if (p_srcb->server_bda == bda) {
316 return p_srcb;
317 }
318 }
319 return NULL;
320 }
321 /*******************************************************************************
322 *
323 * Function bta_gattc_find_scb_by_cid
324 *
325 * Description find server control block by connection ID
326 *
327 * Returns pointer to the server cache.
328 *
329 ******************************************************************************/
bta_gattc_find_scb_by_cid(tCONN_ID conn_id)330 tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(tCONN_ID conn_id) {
331 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
332
333 if (p_clcb) {
334 return p_clcb->p_srcb;
335 } else {
336 return NULL;
337 }
338 }
339 /*******************************************************************************
340 *
341 * Function bta_gattc_srcb_alloc
342 *
343 * Description allocate server cache control block
344 *
345 * Returns pointer to the server cache.
346 *
347 ******************************************************************************/
bta_gattc_srcb_alloc(const RawAddress & bda)348 tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda) {
349 tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0], *p_recycle = NULL;
350 bool found = false;
351 uint8_t i;
352
353 for (i = 0; i < ble_acceptlist_size(); i++, p_tcb++) {
354 if (!p_tcb->in_use) {
355 found = true;
356 break;
357 } else if (!p_tcb->connected) {
358 p_recycle = p_tcb;
359 }
360 }
361
362 /* if not found, try to recycle one known device */
363 if (!found && !p_recycle) {
364 p_tcb = NULL;
365 } else if (!found && p_recycle) {
366 p_tcb = p_recycle;
367 }
368
369 if (p_tcb != NULL) {
370 // clear reallocating
371 p_tcb->gatt_database.Clear();
372 p_tcb->pending_discovery.Clear();
373 *p_tcb = tBTA_GATTC_SERV();
374
375 p_tcb->in_use = true;
376 p_tcb->server_bda = bda;
377 }
378 return p_tcb;
379 }
380
bta_gattc_send_mtu_response(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data,uint16_t current_mtu)381 void bta_gattc_send_mtu_response(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data,
382 uint16_t current_mtu) {
383 GATT_CONFIGURE_MTU_OP_CB cb = p_data->api_mtu.mtu_cb;
384 if (cb) {
385 void* my_cb_data = p_data->api_mtu.mtu_cb_data;
386 cb(p_clcb->bta_conn_id, GATT_SUCCESS, my_cb_data);
387 }
388
389 tBTA_GATTC cb_data;
390 p_clcb->status = GATT_SUCCESS;
391 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
392 cb_data.cfg_mtu.status = GATT_SUCCESS;
393
394 cb_data.cfg_mtu.mtu = current_mtu;
395
396 if (p_clcb->p_rcb) {
397 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CFG_MTU_EVT, &cb_data);
398 }
399 }
400
bta_gattc_continue(tBTA_GATTC_CLCB * p_clcb)401 void bta_gattc_continue(tBTA_GATTC_CLCB* p_clcb) {
402 if (p_clcb->p_q_cmd != NULL) {
403 log::info("Already scheduled another request for conn_id = 0x{:04x}", p_clcb->bta_conn_id);
404 return;
405 }
406
407 while (!p_clcb->p_q_cmd_queue.empty()) {
408 const tBTA_GATTC_DATA* p_q_cmd = p_clcb->p_q_cmd_queue.front();
409 if (p_q_cmd->hdr.event != BTA_GATTC_API_CFG_MTU_EVT) {
410 p_clcb->p_q_cmd_queue.pop_front();
411 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
412 return;
413 }
414
415 /* The p_q_cmd is the MTU Request event. */
416 uint16_t current_mtu = 0;
417 auto result =
418 GATTC_TryMtuRequest(p_clcb->bda, p_clcb->transport, p_clcb->bta_conn_id, ¤t_mtu);
419 switch (result) {
420 case MTU_EXCHANGE_DEVICE_DISCONNECTED:
421 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, GATT_NO_RESOURCES, NULL);
422 /* Handled, free command below and continue with a p_q_cmd_queue */
423 break;
424 case MTU_EXCHANGE_NOT_ALLOWED:
425 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, GATT_ERR_UNLIKELY, NULL);
426 /* Handled, free command below and continue with a p_q_cmd_queue */
427 break;
428 case MTU_EXCHANGE_ALREADY_DONE:
429 bta_gattc_send_mtu_response(p_clcb, p_q_cmd, current_mtu);
430 /* Handled, free command below and continue with a p_q_cmd_queue */
431 break;
432 case MTU_EXCHANGE_IN_PROGRESS:
433 log::warn("Waiting p_clcb {}", std::format_ptr(p_clcb));
434 return;
435 case MTU_EXCHANGE_NOT_DONE_YET:
436 p_clcb->p_q_cmd_queue.pop_front();
437 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
438 return;
439 }
440
441 /* p_q_cmd was the MTU request and it was handled.
442 * If MTU request was handled without actually ATT request,
443 * it is ok to take another message from the queue and proceed.
444 */
445 p_clcb->p_q_cmd_queue.pop_front();
446 osi_free_and_reset((void**)&p_q_cmd);
447 }
448 }
449
bta_gattc_is_data_queued(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)450 bool bta_gattc_is_data_queued(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
451 if (p_clcb->p_q_cmd == p_data) {
452 return true;
453 }
454
455 auto it = std::find(p_clcb->p_q_cmd_queue.begin(), p_clcb->p_q_cmd_queue.end(), p_data);
456 return it != p_clcb->p_q_cmd_queue.end();
457 }
458 /*******************************************************************************
459 *
460 * Function bta_gattc_enqueue
461 *
462 * Description enqueue a client request in clcb.
463 *
464 * Returns BtaEnqueuedResult_t
465 *
466 ******************************************************************************/
bta_gattc_enqueue(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)467 BtaEnqueuedResult_t bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
468 if (p_clcb->p_q_cmd == NULL) {
469 p_clcb->p_q_cmd = p_data;
470 return ENQUEUED_READY_TO_SEND;
471 }
472
473 log::info("Already has a pending command to executer. Queuing for later {} conn id=0x{:04x}",
474 p_clcb->bda, p_clcb->bta_conn_id);
475 p_clcb->p_q_cmd_queue.push_back(p_data);
476
477 return ENQUEUED_FOR_LATER;
478 }
479
480 /*******************************************************************************
481 *
482 * Function bta_gattc_check_notif_registry
483 *
484 * Description check if the service notificaition has been registered.
485 *
486 * Returns
487 *
488 ******************************************************************************/
bta_gattc_check_notif_registry(tBTA_GATTC_RCB * p_clreg,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_NOTIFY * p_notify)489 bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg, tBTA_GATTC_SERV* p_srcb,
490 tBTA_GATTC_NOTIFY* p_notify) {
491 uint8_t i;
492
493 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
494 if (p_clreg->notif_reg[i].in_use && p_clreg->notif_reg[i].remote_bda == p_srcb->server_bda &&
495 p_clreg->notif_reg[i].handle == p_notify->handle &&
496 !p_clreg->notif_reg[i].app_disconnected) {
497 log::verbose("Notification registered!");
498 return true;
499 }
500 }
501 return false;
502 }
503 /*******************************************************************************
504 *
505 * Function bta_gattc_clear_notif_registration
506 *
507 * Description Clear up the notification registration information by
508 * RawAddress.
509 * Where handle is between start_handle and end_handle, and
510 * start_handle and end_handle are boundaries of service
511 * containing characteristic.
512 *
513 * Returns None.
514 *
515 ******************************************************************************/
bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *,tCONN_ID conn_id,uint16_t start_handle,uint16_t end_handle)516 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* /*p_srcb*/, tCONN_ID conn_id,
517 uint16_t start_handle, uint16_t end_handle) {
518 RawAddress remote_bda;
519 tGATT_IF gatt_if;
520 tBTA_GATTC_RCB* p_clrcb;
521 uint8_t i;
522 tBT_TRANSPORT transport;
523 uint16_t handle;
524
525 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
526 p_clrcb = bta_gattc_cl_get_regcb(gatt_if);
527 if (p_clrcb != NULL) {
528 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
529 if (p_clrcb->notif_reg[i].in_use && p_clrcb->notif_reg[i].remote_bda == remote_bda) {
530 /* It's enough to get service or characteristic handle, as
531 * clear boundaries are always around service.
532 */
533 handle = p_clrcb->notif_reg[i].handle;
534 if (handle >= start_handle && handle <= end_handle) {
535 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
536 }
537 }
538 }
539 }
540 } else {
541 log::error("can not clear indication/notif registration for unknown app");
542 }
543 return;
544 }
545
546 /*******************************************************************************
547 *
548 * Function bta_gattc_mark_bg_conn
549 *
550 * Description mark background connection status when a bg connection is
551 * initiated or terminated.
552 *
553 * Returns true if success; false otherwise.
554 *
555 ******************************************************************************/
bta_gattc_mark_bg_conn(tGATT_IF client_if,const RawAddress & remote_bda_ptr,bool add)556 bool bta_gattc_mark_bg_conn(tGATT_IF client_if, const RawAddress& remote_bda_ptr, bool add) {
557 tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0];
558 uint8_t i = 0;
559 tBTA_GATTC_CIF_MASK* p_cif_mask;
560
561 for (i = 0; i < ble_acceptlist_size(); i++, p_bg_tck++) {
562 if (p_bg_tck->in_use &&
563 ((p_bg_tck->remote_bda == remote_bda_ptr) || (p_bg_tck->remote_bda.IsEmpty()))) {
564 auto& p_cif_set = p_bg_tck->cif_set;
565 if (add) { /* mask on the cif bit */
566 p_cif_set.insert(client_if);
567 } else {
568 if (client_if != 0) {
569 p_cif_set.erase(client_if);
570 } else {
571 p_cif_set.clear();
572 }
573 }
574 /* no BG connection for this device, make it available */
575 if (p_bg_tck->cif_set.empty()) {
576 p_bg_tck->in_use = false;
577 p_bg_tck->remote_bda = RawAddress::kEmpty;
578 }
579 return true;
580 }
581 }
582 if (!add) {
583 log::error("unable to find the bg connection mask for bd_addr={}", remote_bda_ptr);
584 return false;
585 } else { /* adding a new device mask */
586 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0]; i < ble_acceptlist_size(); i++, p_bg_tck++) {
587 if (!p_bg_tck->in_use) {
588 p_bg_tck->in_use = true;
589 p_bg_tck->remote_bda = remote_bda_ptr;
590
591 p_bg_tck->cif_set = {client_if};
592 return true;
593 }
594 }
595 log::error("no available space to mark the bg connection status");
596 return false;
597 }
598 }
599 /*******************************************************************************
600 *
601 * Function bta_gattc_check_bg_conn
602 *
603 * Description check if this is a background connection background
604 * connection.
605 *
606 * Returns true if success; false otherwise.
607 *
608 ******************************************************************************/
bta_gattc_check_bg_conn(tGATT_IF client_if,const RawAddress & remote_bda,uint8_t role)609 bool bta_gattc_check_bg_conn(tGATT_IF client_if, const RawAddress& remote_bda, uint8_t role) {
610 tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0];
611 uint8_t i = 0;
612 bool is_bg_conn = false;
613
614 for (i = 0; i < ble_acceptlist_size() && !is_bg_conn; i++, p_bg_tck++) {
615 if (p_bg_tck->in_use &&
616 (p_bg_tck->remote_bda == remote_bda || p_bg_tck->remote_bda.IsEmpty())) {
617 if (p_bg_tck->cif_set.contains(client_if) && role == HCI_ROLE_CENTRAL) {
618 is_bg_conn = true;
619 }
620 }
621 }
622 return is_bg_conn;
623 }
624 /*******************************************************************************
625 *
626 * Function bta_gattc_send_open_cback
627 *
628 * Description send open callback
629 *
630 * Returns
631 *
632 ******************************************************************************/
bta_gattc_send_open_cback(tBTA_GATTC_RCB * p_clreg,tGATT_STATUS status,const RawAddress & remote_bda,tCONN_ID conn_id,tBT_TRANSPORT transport,uint16_t mtu)633 void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg, tGATT_STATUS status,
634 const RawAddress& remote_bda, tCONN_ID conn_id,
635 tBT_TRANSPORT transport, uint16_t mtu) {
636 tBTA_GATTC cb_data;
637
638 if (p_clreg->p_cback) {
639 memset(&cb_data, 0, sizeof(tBTA_GATTC));
640
641 cb_data.open.status = status;
642 cb_data.open.client_if = p_clreg->client_if;
643 cb_data.open.conn_id = conn_id;
644 cb_data.open.mtu = mtu;
645 cb_data.open.transport = transport;
646 cb_data.open.remote_bda = remote_bda;
647
648 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
649 }
650 }
651 /*******************************************************************************
652 *
653 * Function bta_gattc_conn_alloc
654 *
655 * Description allocate connection tracking spot
656 *
657 * Returns pointer to the clcb
658 *
659 ******************************************************************************/
bta_gattc_conn_alloc(const RawAddress & remote_bda)660 tBTA_GATTC_CONN* bta_gattc_conn_alloc(const RawAddress& remote_bda) {
661 uint8_t i_conn = 0;
662 tBTA_GATTC_CONN* p_conn = &bta_gattc_cb.conn_track[0];
663
664 for (i_conn = 0; i_conn < GATT_MAX_PHY_CHANNEL; i_conn++, p_conn++) {
665 if (!p_conn->in_use) {
666 #if (BTA_GATT_DEBUG == TRUE)
667 log::verbose("found conn_track:{} available", i_conn);
668 #endif
669 p_conn->in_use = true;
670 p_conn->remote_bda = remote_bda;
671 return p_conn;
672 }
673 }
674 return NULL;
675 }
676
677 /*******************************************************************************
678 *
679 * Function bta_gattc_conn_find
680 *
681 * Description allocate connection tracking spot
682 *
683 * Returns pointer to the clcb
684 *
685 ******************************************************************************/
bta_gattc_conn_find(const RawAddress & remote_bda)686 tBTA_GATTC_CONN* bta_gattc_conn_find(const RawAddress& remote_bda) {
687 uint8_t i_conn = 0;
688 tBTA_GATTC_CONN* p_conn = &bta_gattc_cb.conn_track[0];
689
690 for (i_conn = 0; i_conn < GATT_MAX_PHY_CHANNEL; i_conn++, p_conn++) {
691 if (p_conn->in_use && remote_bda == p_conn->remote_bda) {
692 #if (BTA_GATT_DEBUG == TRUE)
693 log::verbose("found conn_track:{} matched", i_conn);
694 #endif
695 return p_conn;
696 }
697 }
698 return NULL;
699 }
700
701 /*******************************************************************************
702 *
703 * Function bta_gattc_conn_find_alloc
704 *
705 * Description find or allocate connection tracking spot
706 *
707 * Returns pointer to the clcb
708 *
709 ******************************************************************************/
bta_gattc_conn_find_alloc(const RawAddress & remote_bda)710 tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(const RawAddress& remote_bda) {
711 tBTA_GATTC_CONN* p_conn = bta_gattc_conn_find(remote_bda);
712
713 if (p_conn == NULL) {
714 p_conn = bta_gattc_conn_alloc(remote_bda);
715 }
716 return p_conn;
717 }
718
719 /*******************************************************************************
720 *
721 * Function bta_gattc_conn_dealloc
722 *
723 * Description de-allocate connection tracking spot
724 *
725 * Returns pointer to the clcb
726 *
727 ******************************************************************************/
bta_gattc_conn_dealloc(const RawAddress & remote_bda)728 bool bta_gattc_conn_dealloc(const RawAddress& remote_bda) {
729 tBTA_GATTC_CONN* p_conn = bta_gattc_conn_find(remote_bda);
730
731 if (p_conn != NULL) {
732 p_conn->in_use = false;
733 p_conn->remote_bda = RawAddress::kEmpty;
734 return true;
735 }
736 return false;
737 }
738
739 /*******************************************************************************
740 *
741 * Function bta_gattc_find_int_conn_clcb
742 *
743 * Description try to locate a clcb when an internal connecion event
744 * arrives.
745 *
746 * Returns pointer to the clcb
747 *
748 ******************************************************************************/
bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA * p_msg)749 tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg) {
750 tBTA_GATTC_CLCB* p_clcb = NULL;
751
752 if (p_msg->int_conn.role == HCI_ROLE_PERIPHERAL) {
753 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
754 }
755
756 /* try to locate a logic channel */
757 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, p_msg->int_conn.remote_bda,
758 p_msg->int_conn.transport);
759 if (p_clcb == NULL) {
760 /* for a background connection or listening connection */
761 if (/*p_msg->int_conn.role == HCI_ROLE_PERIPHERAL || */
762 bta_gattc_check_bg_conn(p_msg->int_conn.client_if, p_msg->int_conn.remote_bda,
763 p_msg->int_conn.role)) {
764 /* allocate a new channel */
765 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if, p_msg->int_conn.remote_bda,
766 p_msg->int_conn.transport);
767 }
768 }
769 return p_clcb;
770 }
771
772 /*******************************************************************************
773 *
774 * Function bta_gattc_find_int_disconn_clcb
775 *
776 * Description try to locate a clcb when an internal disconnect callback
777 * arrives.
778 *
779 * Returns pointer to the clcb
780 *
781 ******************************************************************************/
bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA * p_msg)782 tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg) {
783 tBTA_GATTC_CLCB* p_clcb = NULL;
784
785 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
786 p_clcb =
787 bta_gattc_find_clcb_by_conn_id(static_cast<tCONN_ID>(p_msg->int_conn.hdr.layer_specific));
788 if (p_clcb == NULL) {
789 /* connection attempt failed, send connection callback event */
790 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, p_msg->int_conn.remote_bda,
791 p_msg->int_conn.transport);
792 }
793 if (p_clcb == NULL) {
794 log::verbose("disconnection ID:{} not used by BTA", p_msg->int_conn.hdr.layer_specific);
795 }
796 return p_clcb;
797 }
798
bta_gatt_client_dump(int fd)799 void bta_gatt_client_dump(int fd) {
800 std::stringstream stream;
801 int entry_count = 0;
802
803 stream << " ->conn_track (GATT_MAX_PHY_CHANNEL=" << GATT_MAX_PHY_CHANNEL << ")\n";
804 for (int i = 0; i < GATT_MAX_PHY_CHANNEL; i++) {
805 tBTA_GATTC_CONN* p_conn_track = &bta_gattc_cb.conn_track[i];
806 if (p_conn_track->in_use) {
807 entry_count++;
808 stream << " address: " << p_conn_track->remote_bda.ToRedactedStringForLogging();
809 stream << "\n";
810 }
811 }
812 stream << " -- used: " << entry_count << "\n";
813 entry_count = 0;
814
815 stream << " ->bg_track (BTA_GATTC_KNOWN_SR_MAX=" << BTA_GATTC_KNOWN_SR_MAX << ")\n";
816 for (int i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i++) {
817 tBTA_GATTC_BG_TCK* p_bg_track = &bta_gattc_cb.bg_track[i];
818 if (!p_bg_track->in_use) {
819 continue;
820 }
821 entry_count++;
822 stream << " address: " << p_bg_track->remote_bda.ToRedactedStringForLogging()
823 << " cif_mask: " << loghex(p_bg_track->cif_mask);
824 stream << "\n";
825 }
826
827 stream << " -- used: " << entry_count << "\n";
828 entry_count = 0;
829 stream << " ->cl_rcb (dynamic)\n";
830 for (auto& [i, p_cl_rcb] : bta_gattc_cb.cl_rcb_map) {
831 entry_count++;
832 stream << " client_if: " << +p_cl_rcb->client_if << " app uuids: " << p_cl_rcb->app_uuid
833 << " clcb_num: " << +p_cl_rcb->num_clcb;
834 stream << "\n";
835 }
836
837 stream << " -- used: " << entry_count << "\n";
838 entry_count = 0;
839
840 stream << " ->clcb (dynamic)\n";
841 for (auto& p_clcb : bta_gattc_cb.clcb_set) {
842 if (!p_clcb->in_use) {
843 continue;
844 }
845 entry_count++;
846 stream << " conn_id: " << loghex(p_clcb->bta_conn_id)
847 << " address: " << p_clcb->bda.ToRedactedStringForLogging()
848 << " transport: " << bt_transport_text(p_clcb->transport)
849 << " state: " << bta_clcb_state_text(p_clcb->state);
850 stream << "\n";
851 }
852
853 stream << " -- used: " << entry_count << "\n";
854 entry_count = 0;
855 stream << " ->known_server (BTA_GATTC_KNOWN_SR_MAX=" << BTA_GATTC_KNOWN_SR_MAX << ")\n";
856 for (int i = 0; i < BTA_GATTC_CL_MAX; i++) {
857 tBTA_GATTC_SERV* p_known_server = &bta_gattc_cb.known_server[i];
858 if (!p_known_server->in_use) {
859 continue;
860 }
861 entry_count++;
862 stream << " server_address: " << p_known_server->server_bda.ToRedactedStringForLogging()
863 << " mtu: " << p_known_server->mtu
864 << " blocked_conn_id: " << loghex(p_known_server->blocked_conn_id)
865 << " num_clcb: " << +p_known_server->num_clcb
866 << " state: " << bta_server_state_text(p_known_server->state)
867 << " connected: " << p_known_server->connected
868 << " srvc_disc_count: " << p_known_server->srvc_disc_count
869 << " disc_blocked_waiting_on_version: "
870 << p_known_server->disc_blocked_waiting_on_version
871 << " srvc_hdl_chg: " << +p_known_server->srvc_hdl_chg
872 << " srvc_hdl_db_hash: " << p_known_server->srvc_hdl_db_hash
873 << " update_count: " << +p_known_server->update_count;
874
875 stream << "\n";
876 }
877
878 stream << " -- used: " << entry_count << "\n";
879 entry_count = 0;
880 dprintf(fd, "BTA_GATTC_CB state %s \n%s\n", bta_gattc_state_text(bta_gattc_cb.state).c_str(),
881 stream.str().c_str());
882 }
883