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