• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <base/logging.h>
28 
29 #include <cstdint>
30 
31 #include "bt_target.h"  // Must be first to define build configuration
32 #include "bta/gatt/bta_gattc_int.h"
33 #include "device/include/controller.h"
34 #include "gd/common/init_flags.h"
35 #include "osi/include/allocator.h"
36 #include "osi/include/log.h"
37 #include "types/bt_transport.h"
38 #include "types/hci_role.h"
39 #include "types/raw_address.h"
40 
ble_acceptlist_size()41 static uint8_t ble_acceptlist_size() {
42   const controller_t* controller = controller_get_interface();
43   if (!controller->supports_ble()) {
44     return 0;
45   }
46   return controller->get_ble_acceptlist_size();
47 }
48 
49 /*******************************************************************************
50  *
51  * Function         bta_gattc_cl_get_regcb
52  *
53  * Description      get registration control block by client interface.
54  *
55  * Returns          pointer to the regcb
56  *
57  ******************************************************************************/
bta_gattc_cl_get_regcb(uint8_t client_if)58 tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if) {
59   uint8_t i = 0;
60   tBTA_GATTC_RCB* p_clrcb = &bta_gattc_cb.cl_rcb[0];
61 
62   for (i = 0; i < BTA_GATTC_CL_MAX; i++, p_clrcb++) {
63     if (p_clrcb->in_use && p_clrcb->client_if == client_if) return p_clrcb;
64   }
65   return NULL;
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) {
77   uint8_t i = 0, j = 0;
78 
79   for (i = 0; i < BTA_GATTC_CL_MAX; i++) {
80     if (bta_gattc_cb.cl_rcb[i].in_use) j++;
81   }
82   return j;
83 }
84 /*******************************************************************************
85  *
86  * Function         bta_gattc_find_clcb_by_cif
87  *
88  * Description      get clcb by client interface and remote bd adddress
89  *
90  * Returns          pointer to the clcb
91  *
92  ******************************************************************************/
bta_gattc_find_clcb_by_cif(uint8_t client_if,const RawAddress & remote_bda,tBT_TRANSPORT transport)93 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if,
94                                             const RawAddress& remote_bda,
95                                             tBT_TRANSPORT transport) {
96   tBTA_GATTC_CLCB* p_clcb = &bta_gattc_cb.clcb[0];
97   uint8_t i;
98 
99   for (i = 0; i < BTA_GATTC_CLCB_MAX; i++, p_clcb++) {
100     if (p_clcb->in_use && p_clcb->p_rcb->client_if == client_if &&
101         p_clcb->transport == transport && p_clcb->bda == remote_bda)
102       return p_clcb;
103   }
104   return NULL;
105 }
106 /*******************************************************************************
107  *
108  * Function         bta_gattc_find_clcb_by_conn_id
109  *
110  * Description      get clcb by connection ID
111  *
112  * Returns          pointer to the clcb
113  *
114  ******************************************************************************/
bta_gattc_find_clcb_by_conn_id(uint16_t conn_id)115 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id) {
116   tBTA_GATTC_CLCB* p_clcb = &bta_gattc_cb.clcb[0];
117   uint8_t i;
118 
119   for (i = 0; i < BTA_GATTC_CLCB_MAX; i++, p_clcb++) {
120     if (p_clcb->in_use && p_clcb->bta_conn_id == conn_id) return p_clcb;
121   }
122   return NULL;
123 }
124 
125 /*******************************************************************************
126  *
127  * Function         bta_gattc_clcb_alloc
128  *
129  * Description      allocate CLCB
130  *
131  * Returns          pointer to the clcb
132  *
133  ******************************************************************************/
bta_gattc_clcb_alloc(tGATT_IF client_if,const RawAddress & remote_bda,tBT_TRANSPORT transport)134 tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tGATT_IF client_if,
135                                       const RawAddress& remote_bda,
136                                       tBT_TRANSPORT transport) {
137   uint8_t i_clcb = 0;
138   tBTA_GATTC_CLCB* p_clcb = NULL;
139 
140   for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++) {
141     if (!bta_gattc_cb.clcb[i_clcb].in_use) {
142 #if (BTA_GATT_DEBUG == TRUE)
143       VLOG(1) << __func__ << ": found clcb:" << +i_clcb << " available";
144 #endif
145       p_clcb = &bta_gattc_cb.clcb[i_clcb];
146       p_clcb->in_use = true;
147       p_clcb->status = GATT_SUCCESS;
148       p_clcb->transport = transport;
149       p_clcb->bda = remote_bda;
150       p_clcb->p_q_cmd = NULL;
151 
152       p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
153 
154       p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda);
155       if (p_clcb->p_srcb == NULL)
156         p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda);
157 
158       if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL) {
159         p_clcb->p_srcb->num_clcb++;
160         p_clcb->p_rcb->num_clcb++;
161       } else {
162         /* release this clcb if clcb or srcb allocation failed */
163         p_clcb->in_use = false;
164         p_clcb = NULL;
165       }
166       break;
167     }
168   }
169   return p_clcb;
170 }
171 /*******************************************************************************
172  *
173  * Function         bta_gattc_find_alloc_clcb
174  *
175  * Description      find or allocate CLCB if not found.
176  *
177  * Returns          pointer to the clcb
178  *
179  ******************************************************************************/
bta_gattc_find_alloc_clcb(tGATT_IF client_if,const RawAddress & remote_bda,tBT_TRANSPORT transport)180 tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tGATT_IF client_if,
181                                            const RawAddress& remote_bda,
182                                            tBT_TRANSPORT transport) {
183   tBTA_GATTC_CLCB* p_clcb;
184 
185   p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport);
186   if (p_clcb == NULL) {
187     p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
188   }
189   return p_clcb;
190 }
191 
192 /*******************************************************************************
193  *
194  * Function         bta_gattc_server_disconnected
195  *
196  * Description      Set server cache disconnected
197  *
198  * Returns          pointer to the srcb
199  *
200  ******************************************************************************/
bta_gattc_server_disconnected(tBTA_GATTC_SERV * p_srcb)201 void bta_gattc_server_disconnected(tBTA_GATTC_SERV* p_srcb) {
202   if (p_srcb && p_srcb->connected) {
203     p_srcb->connected = false;
204     p_srcb->state = BTA_GATTC_SERV_IDLE;
205     p_srcb->mtu = 0;
206 
207     // clear reallocating
208     p_srcb->gatt_database.Clear();
209   }
210 }
211 
212 /*******************************************************************************
213  *
214  * Function         bta_gattc_clcb_dealloc
215  *
216  * Description      Deallocte a clcb
217  *
218  * Returns          pointer to the clcb
219  *
220  ******************************************************************************/
bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB * p_clcb)221 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb) {
222   if (!p_clcb) {
223     LOG(ERROR) << __func__ << " p_clcb=NULL";
224     return;
225   }
226 
227   tBTA_GATTC_SERV* p_srcb = p_clcb->p_srcb;
228   if (p_srcb->num_clcb) p_srcb->num_clcb--;
229 
230   if (p_clcb->p_rcb->num_clcb) p_clcb->p_rcb->num_clcb--;
231 
232   /* if the srcb is no longer needed, reset the state */
233   if (p_srcb->num_clcb == 0) {
234     p_srcb->connected = false;
235     p_srcb->state = BTA_GATTC_SERV_IDLE;
236     p_srcb->mtu = 0;
237 
238     // clear reallocating
239     p_srcb->gatt_database.Clear();
240   }
241 
242   while (!p_clcb->p_q_cmd_queue.empty()) {
243     auto p_q_cmd = p_clcb->p_q_cmd_queue.front();
244     p_clcb->p_q_cmd_queue.pop_front();
245     osi_free_and_reset((void**)&p_q_cmd);
246   }
247 
248   if (p_clcb->p_q_cmd != NULL) {
249     osi_free_and_reset((void**)&p_clcb->p_q_cmd);
250   }
251 
252   /* Clear p_clcb. Some of the fields are already reset e.g. p_q_cmd_queue and
253    * p_q_cmd. */
254   p_clcb->bta_conn_id = 0;
255   p_clcb->bda = {};
256   p_clcb->transport = 0;
257   p_clcb->p_rcb = NULL;
258   p_clcb->p_srcb = NULL;
259   p_clcb->request_during_discovery = 0;
260   p_clcb->auto_update = 0;
261   p_clcb->disc_active = 0;
262   p_clcb->in_use = 0;
263   p_clcb->state = BTA_GATTC_IDLE_ST;
264   p_clcb->status = GATT_SUCCESS;
265 }
266 
267 /*******************************************************************************
268  *
269  * Function         bta_gattc_find_srcb
270  *
271  * Description      find server cache by remote bd address currently in use
272  *
273  * Returns          pointer to the server cache.
274  *
275  ******************************************************************************/
bta_gattc_find_srcb(const RawAddress & bda)276 tBTA_GATTC_SERV* bta_gattc_find_srcb(const RawAddress& bda) {
277   tBTA_GATTC_SERV* p_srcb = &bta_gattc_cb.known_server[0];
278   uint8_t i;
279 
280   for (i = 0; i < ble_acceptlist_size(); i++, p_srcb++) {
281     if (p_srcb->in_use && p_srcb->server_bda == bda) return p_srcb;
282   }
283   return NULL;
284 }
285 
286 /*******************************************************************************
287  *
288  * Function         bta_gattc_find_srvr_cache
289  *
290  * Description      find server cache by remote bd address
291  *
292  * Returns          pointer to the server cache.
293  *
294  ******************************************************************************/
bta_gattc_find_srvr_cache(const RawAddress & bda)295 tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(const RawAddress& bda) {
296   tBTA_GATTC_SERV* p_srcb = &bta_gattc_cb.known_server[0];
297   uint8_t i;
298 
299   for (i = 0; i < ble_acceptlist_size(); i++, p_srcb++) {
300     if (p_srcb->server_bda == bda) return p_srcb;
301   }
302   return NULL;
303 }
304 /*******************************************************************************
305  *
306  * Function         bta_gattc_find_scb_by_cid
307  *
308  * Description      find server control block by connection ID
309  *
310  * Returns          pointer to the server cache.
311  *
312  ******************************************************************************/
bta_gattc_find_scb_by_cid(uint16_t conn_id)313 tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id) {
314   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
315 
316   if (p_clcb)
317     return p_clcb->p_srcb;
318   else
319     return NULL;
320 }
321 /*******************************************************************************
322  *
323  * Function         bta_gattc_srcb_alloc
324  *
325  * Description      allocate server cache control block
326  *
327  * Returns          pointer to the server cache.
328  *
329  ******************************************************************************/
bta_gattc_srcb_alloc(const RawAddress & bda)330 tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda) {
331   tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0], *p_recycle = NULL;
332   bool found = false;
333   uint8_t i;
334 
335   for (i = 0; i < ble_acceptlist_size(); i++, p_tcb++) {
336     if (!p_tcb->in_use) {
337       found = true;
338       break;
339     } else if (!p_tcb->connected) {
340       p_recycle = p_tcb;
341     }
342   }
343 
344   /* if not found, try to recycle one known device */
345   if (!found && !p_recycle)
346     p_tcb = NULL;
347   else if (!found && p_recycle)
348     p_tcb = p_recycle;
349 
350   if (p_tcb != NULL) {
351     // clear reallocating
352     p_tcb->gatt_database.Clear();
353     p_tcb->pending_discovery.Clear();
354     *p_tcb = tBTA_GATTC_SERV();
355 
356     p_tcb->in_use = true;
357     p_tcb->server_bda = bda;
358   }
359   return p_tcb;
360 }
361 
bta_gattc_continue(tBTA_GATTC_CLCB * p_clcb)362 void bta_gattc_continue(tBTA_GATTC_CLCB* p_clcb) {
363   if (p_clcb->p_q_cmd != NULL) {
364     LOG_INFO("Already scheduled another request for conn_id = 0x%04x",
365              p_clcb->bta_conn_id);
366     return;
367   }
368 
369   if (p_clcb->p_q_cmd_queue.empty()) {
370     LOG_INFO("Nothing to do for conn_id = 0x%04x", p_clcb->bta_conn_id);
371     return;
372   }
373 
374   const tBTA_GATTC_DATA* p_q_cmd = p_clcb->p_q_cmd_queue.front();
375   p_clcb->p_q_cmd_queue.pop_front();
376   bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
377 }
378 
bta_gattc_is_data_queued(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)379 bool bta_gattc_is_data_queued(tBTA_GATTC_CLCB* p_clcb,
380                               const tBTA_GATTC_DATA* p_data) {
381   if (p_clcb->p_q_cmd == p_data) {
382     return true;
383   }
384 
385   auto it = std::find(p_clcb->p_q_cmd_queue.begin(),
386                       p_clcb->p_q_cmd_queue.end(), p_data);
387   return it != p_clcb->p_q_cmd_queue.end();
388 }
389 /*******************************************************************************
390  *
391  * Function         bta_gattc_enqueue
392  *
393  * Description      enqueue a client request in clcb.
394  *
395  * Returns          BtaEnqueuedResult_t
396  *
397  ******************************************************************************/
bta_gattc_enqueue(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)398 BtaEnqueuedResult_t bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb,
399                                       const tBTA_GATTC_DATA* p_data) {
400   if (p_clcb->p_q_cmd == NULL) {
401     p_clcb->p_q_cmd = p_data;
402     return ENQUEUED_READY_TO_SEND;
403   }
404 
405   LOG_INFO(
406       "Already has a pending command to executer. Queuing for later %s conn "
407       "id=0x%04x",
408       p_clcb->bda.ToString().c_str(), p_clcb->bta_conn_id);
409   p_clcb->p_q_cmd_queue.push_back(p_data);
410 
411   return ENQUEUED_FOR_LATER;
412 }
413 
414 /*******************************************************************************
415  *
416  * Function         bta_gattc_check_notif_registry
417  *
418  * Description      check if the service notificaition has been registered.
419  *
420  * Returns
421  *
422  ******************************************************************************/
bta_gattc_check_notif_registry(tBTA_GATTC_RCB * p_clreg,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_NOTIFY * p_notify)423 bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg,
424                                     tBTA_GATTC_SERV* p_srcb,
425                                     tBTA_GATTC_NOTIFY* p_notify) {
426   uint8_t i;
427 
428   for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
429     if (p_clreg->notif_reg[i].in_use &&
430         p_clreg->notif_reg[i].remote_bda == p_srcb->server_bda &&
431         p_clreg->notif_reg[i].handle == p_notify->handle &&
432         !p_clreg->notif_reg[i].app_disconnected) {
433       VLOG(1) << "Notification registered!";
434       return true;
435     }
436   }
437   return false;
438 }
439 /*******************************************************************************
440  *
441  * Function         bta_gattc_clear_notif_registration
442  *
443  * Description      Clear up the notification registration information by
444  *                  RawAddress.
445  *                  Where handle is between start_handle and end_handle, and
446  *                  start_handle and end_handle are boundaries of service
447  *                  containing characteristic.
448  *
449  * Returns          None.
450  *
451  ******************************************************************************/
bta_gattc_clear_notif_registration(tBTA_GATTC_SERV * p_srcb,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle)452 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb,
453                                         uint16_t conn_id, uint16_t start_handle,
454                                         uint16_t end_handle) {
455   RawAddress remote_bda;
456   tGATT_IF gatt_if;
457   tBTA_GATTC_RCB* p_clrcb;
458   uint8_t i;
459   tBT_TRANSPORT transport;
460   uint16_t handle;
461 
462   if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
463     p_clrcb = bta_gattc_cl_get_regcb(gatt_if);
464     if (p_clrcb != NULL) {
465       for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
466         if (p_clrcb->notif_reg[i].in_use &&
467             p_clrcb->notif_reg[i].remote_bda == remote_bda) {
468           /* It's enough to get service or characteristic handle, as
469            * clear boundaries are always around service.
470            */
471           handle = p_clrcb->notif_reg[i].handle;
472           if (handle >= start_handle && handle <= end_handle)
473             memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
474         }
475       }
476     }
477   } else {
478     LOG(ERROR) << "can not clear indication/notif registration for unknown app";
479   }
480   return;
481 }
482 
483 /*******************************************************************************
484  *
485  * Function         bta_gattc_mark_bg_conn
486  *
487  * Description      mark background connection status when a bg connection is
488  *                  initiated or terminated.
489  *
490  * Returns          true if success; false otherwise.
491  *
492  ******************************************************************************/
bta_gattc_mark_bg_conn(tGATT_IF client_if,const RawAddress & remote_bda_ptr,bool add)493 bool bta_gattc_mark_bg_conn(tGATT_IF client_if,
494                             const RawAddress& remote_bda_ptr, bool add) {
495   tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0];
496   uint8_t i = 0;
497   tBTA_GATTC_CIF_MASK* p_cif_mask;
498 
499   for (i = 0; i < ble_acceptlist_size(); i++, p_bg_tck++) {
500     if (p_bg_tck->in_use && ((p_bg_tck->remote_bda == remote_bda_ptr) ||
501                              (p_bg_tck->remote_bda.IsEmpty()))) {
502       p_cif_mask = &p_bg_tck->cif_mask;
503 
504       if (add) /* mask on the cif bit */
505         *p_cif_mask |= (1 << (client_if - 1));
506       else {
507         if (client_if != 0)
508           *p_cif_mask &= (~(1 << (client_if - 1)));
509         else
510           *p_cif_mask = 0;
511       }
512       /* no BG connection for this device, make it available */
513       if (p_bg_tck->cif_mask == 0) {
514         memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
515       }
516       return true;
517     }
518   }
519   if (!add) {
520     LOG(ERROR) << __func__
521                << " unable to find the bg connection mask for bd_addr="
522                << remote_bda_ptr;
523     return false;
524   } else /* adding a new device mask */
525   {
526     for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0]; i < ble_acceptlist_size();
527          i++, p_bg_tck++) {
528       if (!p_bg_tck->in_use) {
529         p_bg_tck->in_use = true;
530         p_bg_tck->remote_bda = remote_bda_ptr;
531 
532         p_cif_mask = &p_bg_tck->cif_mask;
533 
534         *p_cif_mask = (1 << (client_if - 1));
535         return true;
536       }
537     }
538     LOG(ERROR) << "no available space to mark the bg connection status";
539     return false;
540   }
541 }
542 /*******************************************************************************
543  *
544  * Function         bta_gattc_check_bg_conn
545  *
546  * Description      check if this is a background connection background
547  *                  connection.
548  *
549  * Returns          true if success; false otherwise.
550  *
551  ******************************************************************************/
bta_gattc_check_bg_conn(tGATT_IF client_if,const RawAddress & remote_bda,uint8_t role)552 bool bta_gattc_check_bg_conn(tGATT_IF client_if, const RawAddress& remote_bda,
553                              uint8_t role) {
554   tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0];
555   uint8_t i = 0;
556   bool is_bg_conn = false;
557 
558   for (i = 0; i < ble_acceptlist_size() && !is_bg_conn; i++, p_bg_tck++) {
559     if (p_bg_tck->in_use && (p_bg_tck->remote_bda == remote_bda ||
560                              p_bg_tck->remote_bda.IsEmpty())) {
561       if (((p_bg_tck->cif_mask & (1 << (client_if - 1))) != 0) &&
562           role == HCI_ROLE_CENTRAL)
563         is_bg_conn = true;
564     }
565   }
566   return is_bg_conn;
567 }
568 /*******************************************************************************
569  *
570  * Function         bta_gattc_send_open_cback
571  *
572  * Description      send open callback
573  *
574  * Returns
575  *
576  ******************************************************************************/
bta_gattc_send_open_cback(tBTA_GATTC_RCB * p_clreg,tGATT_STATUS status,const RawAddress & remote_bda,uint16_t conn_id,tBT_TRANSPORT transport,uint16_t mtu)577 void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg, tGATT_STATUS status,
578                                const RawAddress& remote_bda, uint16_t conn_id,
579                                tBT_TRANSPORT transport, uint16_t mtu) {
580   tBTA_GATTC cb_data;
581 
582   if (p_clreg->p_cback) {
583     memset(&cb_data, 0, sizeof(tBTA_GATTC));
584 
585     cb_data.open.status = status;
586     cb_data.open.client_if = p_clreg->client_if;
587     cb_data.open.conn_id = conn_id;
588     cb_data.open.mtu = mtu;
589     cb_data.open.transport = transport;
590     cb_data.open.remote_bda = remote_bda;
591 
592     (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
593   }
594 }
595 /*******************************************************************************
596  *
597  * Function         bta_gattc_conn_alloc
598  *
599  * Description      allocate connection tracking spot
600  *
601  * Returns          pointer to the clcb
602  *
603  ******************************************************************************/
bta_gattc_conn_alloc(const RawAddress & remote_bda)604 tBTA_GATTC_CONN* bta_gattc_conn_alloc(const RawAddress& remote_bda) {
605   uint8_t i_conn = 0;
606   tBTA_GATTC_CONN* p_conn = &bta_gattc_cb.conn_track[0];
607 
608   for (i_conn = 0; i_conn < GATT_MAX_PHY_CHANNEL; i_conn++, p_conn++) {
609     if (!p_conn->in_use) {
610 #if (BTA_GATT_DEBUG == TRUE)
611       VLOG(1) << __func__ << ": found conn_track:" << +i_conn << " available";
612 #endif
613       p_conn->in_use = true;
614       p_conn->remote_bda = remote_bda;
615       return p_conn;
616     }
617   }
618   return NULL;
619 }
620 
621 /*******************************************************************************
622  *
623  * Function         bta_gattc_conn_find
624  *
625  * Description      allocate connection tracking spot
626  *
627  * Returns          pointer to the clcb
628  *
629  ******************************************************************************/
bta_gattc_conn_find(const RawAddress & remote_bda)630 tBTA_GATTC_CONN* bta_gattc_conn_find(const RawAddress& remote_bda) {
631   uint8_t i_conn = 0;
632   tBTA_GATTC_CONN* p_conn = &bta_gattc_cb.conn_track[0];
633 
634   for (i_conn = 0; i_conn < GATT_MAX_PHY_CHANNEL; i_conn++, p_conn++) {
635     if (p_conn->in_use && remote_bda == p_conn->remote_bda) {
636 #if (BTA_GATT_DEBUG == TRUE)
637       VLOG(1) << __func__ << ": found conn_track:" << +i_conn << " matched";
638 #endif
639       return p_conn;
640     }
641   }
642   return NULL;
643 }
644 
645 /*******************************************************************************
646  *
647  * Function         bta_gattc_conn_find_alloc
648  *
649  * Description      find or allocate connection tracking spot
650  *
651  * Returns          pointer to the clcb
652  *
653  ******************************************************************************/
bta_gattc_conn_find_alloc(const RawAddress & remote_bda)654 tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(const RawAddress& remote_bda) {
655   tBTA_GATTC_CONN* p_conn = bta_gattc_conn_find(remote_bda);
656 
657   if (p_conn == NULL) {
658     p_conn = bta_gattc_conn_alloc(remote_bda);
659   }
660   return p_conn;
661 }
662 
663 /*******************************************************************************
664  *
665  * Function         bta_gattc_conn_dealloc
666  *
667  * Description      de-allocate connection tracking spot
668  *
669  * Returns          pointer to the clcb
670  *
671  ******************************************************************************/
bta_gattc_conn_dealloc(const RawAddress & remote_bda)672 bool bta_gattc_conn_dealloc(const RawAddress& remote_bda) {
673   tBTA_GATTC_CONN* p_conn = bta_gattc_conn_find(remote_bda);
674 
675   if (p_conn != NULL) {
676     p_conn->in_use = false;
677     p_conn->remote_bda = RawAddress::kEmpty;
678     return true;
679   }
680   return false;
681 }
682 
683 /*******************************************************************************
684  *
685  * Function         bta_gattc_find_int_conn_clcb
686  *
687  * Description      try to locate a clcb when an internal connecion event
688  *                  arrives.
689  *
690  * Returns          pointer to the clcb
691  *
692  ******************************************************************************/
bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA * p_msg)693 tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg) {
694   tBTA_GATTC_CLCB* p_clcb = NULL;
695 
696   if (p_msg->int_conn.role == HCI_ROLE_PERIPHERAL)
697     bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
698 
699   /* try to locate a logic channel */
700   p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
701                                       p_msg->int_conn.remote_bda,
702                                       p_msg->int_conn.transport);
703   if (p_clcb == NULL) {
704     /* for a background connection or listening connection */
705     if (/*p_msg->int_conn.role == HCI_ROLE_PERIPHERAL ||  */
706         bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
707                                 p_msg->int_conn.remote_bda,
708                                 p_msg->int_conn.role)) {
709       /* allocate a new channel */
710       p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
711                                     p_msg->int_conn.remote_bda,
712                                     p_msg->int_conn.transport);
713     }
714   }
715   return p_clcb;
716 }
717 
718 /*******************************************************************************
719  *
720  * Function         bta_gattc_find_int_disconn_clcb
721  *
722  * Description      try to locate a clcb when an internal disconnect callback
723  *                  arrives.
724  *
725  * Returns          pointer to the clcb
726  *
727  ******************************************************************************/
bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA * p_msg)728 tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg) {
729   tBTA_GATTC_CLCB* p_clcb = NULL;
730 
731   bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
732   p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific);
733   if (p_clcb == NULL) {
734     /* connection attempt failed, send connection callback event */
735     p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
736                                         p_msg->int_conn.remote_bda,
737                                         p_msg->int_conn.transport);
738   }
739   if (p_clcb == NULL) {
740     VLOG(1) << " disconnection ID:" << +p_msg->int_conn.hdr.layer_specific
741             << " not used by BTA";
742   }
743   return p_clcb;
744 }
745 
746 /*******************************************************************************
747  *
748  * Function         bta_gattc_is_robust_caching_enabled
749  *
750  * Description      check if robust caching is enabled
751  *
752  * Returns          true if enabled; otherwise false
753  *
754  ******************************************************************************/
bta_gattc_is_robust_caching_enabled()755 bool bta_gattc_is_robust_caching_enabled() {
756   return bluetooth::common::init_flags::gatt_robust_caching_client_is_enabled();
757 }
758