• 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_send_mtu_response(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data,uint16_t current_mtu)362 void bta_gattc_send_mtu_response(tBTA_GATTC_CLCB* p_clcb,
363                                  const tBTA_GATTC_DATA* p_data,
364                                  uint16_t current_mtu) {
365   GATT_CONFIGURE_MTU_OP_CB cb = p_data->api_mtu.mtu_cb;
366   if (cb) {
367     void* my_cb_data = p_data->api_mtu.mtu_cb_data;
368     cb(p_clcb->bta_conn_id, GATT_SUCCESS, my_cb_data);
369   }
370 
371   tBTA_GATTC cb_data;
372   p_clcb->status = GATT_SUCCESS;
373   cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
374   cb_data.cfg_mtu.status = GATT_SUCCESS;
375 
376   cb_data.cfg_mtu.mtu = current_mtu;
377 
378   if (p_clcb->p_rcb) {
379     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CFG_MTU_EVT, &cb_data);
380   }
381 }
382 
bta_gattc_continue(tBTA_GATTC_CLCB * p_clcb)383 void bta_gattc_continue(tBTA_GATTC_CLCB* p_clcb) {
384   if (p_clcb->p_q_cmd != NULL) {
385     LOG_INFO("Already scheduled another request for conn_id = 0x%04x",
386              p_clcb->bta_conn_id);
387     return;
388   }
389 
390   while (!p_clcb->p_q_cmd_queue.empty()) {
391     const tBTA_GATTC_DATA* p_q_cmd = p_clcb->p_q_cmd_queue.front();
392     if (p_q_cmd->hdr.event != BTA_GATTC_API_CFG_MTU_EVT) {
393       p_clcb->p_q_cmd_queue.pop_front();
394       bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
395       return;
396     }
397 
398     /* The p_q_cmd is the MTU Request event. */
399     uint16_t current_mtu = 0;
400     auto result = GATTC_TryMtuRequest(p_clcb->bda, p_clcb->transport,
401                                       p_clcb->bta_conn_id, &current_mtu);
402     switch (result) {
403       case MTU_EXCHANGE_DEVICE_DISCONNECTED:
404         bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG,
405                                GATT_NO_RESOURCES, NULL);
406         /* Handled, free command below and continue with a p_q_cmd_queue */
407         break;
408       case MTU_EXCHANGE_NOT_ALLOWED:
409         bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG,
410                                GATT_ERR_UNLIKELY, NULL);
411         /* Handled, free command below and continue with a p_q_cmd_queue */
412         break;
413       case MTU_EXCHANGE_ALREADY_DONE:
414         bta_gattc_send_mtu_response(p_clcb, p_q_cmd, current_mtu);
415         /* Handled, free command below and continue with a p_q_cmd_queue */
416         break;
417       case MTU_EXCHANGE_IN_PROGRESS:
418         LOG_WARN("Waiting p_clcb %p", p_clcb);
419         return;
420       case MTU_EXCHANGE_NOT_DONE_YET:
421         p_clcb->p_q_cmd_queue.pop_front();
422         bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
423         return;
424     }
425 
426     /* p_q_cmd was the MTU request and it was handled.
427      * If MTU request was handled without actually ATT request,
428      * it is ok to take another message from the queue and proceed.
429      */
430     p_clcb->p_q_cmd_queue.pop_front();
431     osi_free_and_reset((void**)&p_q_cmd);
432   }
433 }
434 
bta_gattc_is_data_queued(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)435 bool bta_gattc_is_data_queued(tBTA_GATTC_CLCB* p_clcb,
436                               const tBTA_GATTC_DATA* p_data) {
437   if (p_clcb->p_q_cmd == p_data) {
438     return true;
439   }
440 
441   auto it = std::find(p_clcb->p_q_cmd_queue.begin(),
442                       p_clcb->p_q_cmd_queue.end(), p_data);
443   return it != p_clcb->p_q_cmd_queue.end();
444 }
445 /*******************************************************************************
446  *
447  * Function         bta_gattc_enqueue
448  *
449  * Description      enqueue a client request in clcb.
450  *
451  * Returns          BtaEnqueuedResult_t
452  *
453  ******************************************************************************/
bta_gattc_enqueue(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)454 BtaEnqueuedResult_t bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb,
455                                       const tBTA_GATTC_DATA* p_data) {
456   if (p_clcb->p_q_cmd == NULL) {
457     p_clcb->p_q_cmd = p_data;
458     return ENQUEUED_READY_TO_SEND;
459   }
460 
461   LOG_INFO(
462       "Already has a pending command to executer. Queuing for later %s conn "
463       "id=0x%04x",
464       ADDRESS_TO_LOGGABLE_CSTR(p_clcb->bda), p_clcb->bta_conn_id);
465   p_clcb->p_q_cmd_queue.push_back(p_data);
466 
467   return ENQUEUED_FOR_LATER;
468 }
469 
470 /*******************************************************************************
471  *
472  * Function         bta_gattc_check_notif_registry
473  *
474  * Description      check if the service notificaition has been registered.
475  *
476  * Returns
477  *
478  ******************************************************************************/
bta_gattc_check_notif_registry(tBTA_GATTC_RCB * p_clreg,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_NOTIFY * p_notify)479 bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg,
480                                     tBTA_GATTC_SERV* p_srcb,
481                                     tBTA_GATTC_NOTIFY* p_notify) {
482   uint8_t i;
483 
484   for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
485     if (p_clreg->notif_reg[i].in_use &&
486         p_clreg->notif_reg[i].remote_bda == p_srcb->server_bda &&
487         p_clreg->notif_reg[i].handle == p_notify->handle &&
488         !p_clreg->notif_reg[i].app_disconnected) {
489       VLOG(1) << "Notification registered!";
490       return true;
491     }
492   }
493   return false;
494 }
495 /*******************************************************************************
496  *
497  * Function         bta_gattc_clear_notif_registration
498  *
499  * Description      Clear up the notification registration information by
500  *                  RawAddress.
501  *                  Where handle is between start_handle and end_handle, and
502  *                  start_handle and end_handle are boundaries of service
503  *                  containing characteristic.
504  *
505  * Returns          None.
506  *
507  ******************************************************************************/
bta_gattc_clear_notif_registration(tBTA_GATTC_SERV * p_srcb,uint16_t conn_id,uint16_t start_handle,uint16_t end_handle)508 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb,
509                                         uint16_t conn_id, uint16_t start_handle,
510                                         uint16_t end_handle) {
511   RawAddress remote_bda;
512   tGATT_IF gatt_if;
513   tBTA_GATTC_RCB* p_clrcb;
514   uint8_t i;
515   tBT_TRANSPORT transport;
516   uint16_t handle;
517 
518   if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
519     p_clrcb = bta_gattc_cl_get_regcb(gatt_if);
520     if (p_clrcb != NULL) {
521       for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
522         if (p_clrcb->notif_reg[i].in_use &&
523             p_clrcb->notif_reg[i].remote_bda == remote_bda) {
524           /* It's enough to get service or characteristic handle, as
525            * clear boundaries are always around service.
526            */
527           handle = p_clrcb->notif_reg[i].handle;
528           if (handle >= start_handle && handle <= end_handle)
529             memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
530         }
531       }
532     }
533   } else {
534     LOG(ERROR) << "can not clear indication/notif registration for unknown app";
535   }
536   return;
537 }
538 
539 /*******************************************************************************
540  *
541  * Function         bta_gattc_mark_bg_conn
542  *
543  * Description      mark background connection status when a bg connection is
544  *                  initiated or terminated.
545  *
546  * Returns          true if success; false otherwise.
547  *
548  ******************************************************************************/
bta_gattc_mark_bg_conn(tGATT_IF client_if,const RawAddress & remote_bda_ptr,bool add)549 bool bta_gattc_mark_bg_conn(tGATT_IF client_if,
550                             const RawAddress& remote_bda_ptr, bool add) {
551   tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0];
552   uint8_t i = 0;
553   tBTA_GATTC_CIF_MASK* p_cif_mask;
554 
555   for (i = 0; i < ble_acceptlist_size(); i++, p_bg_tck++) {
556     if (p_bg_tck->in_use && ((p_bg_tck->remote_bda == remote_bda_ptr) ||
557                              (p_bg_tck->remote_bda.IsEmpty()))) {
558       p_cif_mask = &p_bg_tck->cif_mask;
559 
560       if (add) /* mask on the cif bit */
561         *p_cif_mask |= (1 << (client_if - 1));
562       else {
563         if (client_if != 0)
564           *p_cif_mask &= (~(1 << (client_if - 1)));
565         else
566           *p_cif_mask = 0;
567       }
568       /* no BG connection for this device, make it available */
569       if (p_bg_tck->cif_mask == 0) {
570         memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
571       }
572       return true;
573     }
574   }
575   if (!add) {
576     LOG(ERROR) << __func__
577                << " unable to find the bg connection mask for bd_addr="
578                << ADDRESS_TO_LOGGABLE_STR(remote_bda_ptr);
579     return false;
580   } else /* adding a new device mask */
581   {
582     for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0]; i < ble_acceptlist_size();
583          i++, p_bg_tck++) {
584       if (!p_bg_tck->in_use) {
585         p_bg_tck->in_use = true;
586         p_bg_tck->remote_bda = remote_bda_ptr;
587 
588         p_cif_mask = &p_bg_tck->cif_mask;
589 
590         *p_cif_mask = ((tBTA_GATTC_CIF_MASK)1 << (client_if - 1));
591         return true;
592       }
593     }
594     LOG(ERROR) << "no available space to mark the bg connection status";
595     return false;
596   }
597 }
598 /*******************************************************************************
599  *
600  * Function         bta_gattc_check_bg_conn
601  *
602  * Description      check if this is a background connection background
603  *                  connection.
604  *
605  * Returns          true if success; false otherwise.
606  *
607  ******************************************************************************/
bta_gattc_check_bg_conn(tGATT_IF client_if,const RawAddress & remote_bda,uint8_t role)608 bool bta_gattc_check_bg_conn(tGATT_IF client_if, const RawAddress& remote_bda,
609                              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 && (p_bg_tck->remote_bda == remote_bda ||
616                              p_bg_tck->remote_bda.IsEmpty())) {
617       if (((p_bg_tck->cif_mask & ((tBTA_GATTC_CIF_MASK)1 << (client_if - 1))) != 0) &&
618           role == HCI_ROLE_CENTRAL)
619         is_bg_conn = true;
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,uint16_t 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, uint16_t 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       VLOG(1) << __func__ << ": found conn_track:" << +i_conn << " available";
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       VLOG(1) << __func__ << ": found conn_track:" << +i_conn << " matched";
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   /* try to locate a logic channel */
756   p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
757                                       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,
763                                 p_msg->int_conn.remote_bda,
764                                 p_msg->int_conn.role)) {
765       /* allocate a new channel */
766       p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
767                                     p_msg->int_conn.remote_bda,
768                                     p_msg->int_conn.transport);
769     }
770   }
771   return p_clcb;
772 }
773 
774 /*******************************************************************************
775  *
776  * Function         bta_gattc_find_int_disconn_clcb
777  *
778  * Description      try to locate a clcb when an internal disconnect callback
779  *                  arrives.
780  *
781  * Returns          pointer to the clcb
782  *
783  ******************************************************************************/
bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA * p_msg)784 tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg) {
785   tBTA_GATTC_CLCB* p_clcb = NULL;
786 
787   bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
788   p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific);
789   if (p_clcb == NULL) {
790     /* connection attempt failed, send connection callback event */
791     p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
792                                         p_msg->int_conn.remote_bda,
793                                         p_msg->int_conn.transport);
794   }
795   if (p_clcb == NULL) {
796     VLOG(1) << " disconnection ID:" << +p_msg->int_conn.hdr.layer_specific
797             << " not used by BTA";
798   }
799   return p_clcb;
800 }
801 
802 /*******************************************************************************
803  *
804  * Function         bta_gattc_is_robust_caching_enabled
805  *
806  * Description      check if robust caching is enabled
807  *
808  * Returns          true if enabled; otherwise false
809  *
810  ******************************************************************************/
bta_gattc_is_robust_caching_enabled()811 bool bta_gattc_is_robust_caching_enabled() {
812   return bluetooth::common::init_flags::gatt_robust_caching_client_is_enabled();
813 }
814