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 action functions for the state
22  *  machine.
23  *
24  ******************************************************************************/
25 
26 #define LOG_TAG "bt_bta_gattc"
27 
28 #include <base/bind.h>
29 #include <base/strings/stringprintf.h>
30 
31 #include "bt_target.h"  // Must be first to define build configuration
32 
33 #include "bta/gatt/bta_gattc_int.h"
34 #include "bta/hh/bta_hh_int.h"
35 #include "btif/include/btif_debug_conn.h"
36 #include "device/include/controller.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h"  // UNUSED_ATTR
39 #include "stack/include/btm_ble_api_types.h"
40 #include "stack/include/btu.h"  // do_in_main_thread
41 #include "stack/include/l2c_api.h"
42 #include "types/bluetooth/uuid.h"
43 #include "types/raw_address.h"
44 
45 using base::StringPrintf;
46 using bluetooth::Uuid;
47 
48 /*****************************************************************************
49  *  Constants
50  ****************************************************************************/
51 static void bta_gattc_conn_cback(tGATT_IF gattc_if, const RawAddress& bda,
52                                  uint16_t conn_id, bool connected,
53                                  tGATT_DISCONN_REASON reason,
54                                  tBT_TRANSPORT transport);
55 
56 static void bta_gattc_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op,
57                                  tGATT_STATUS status,
58                                  tGATT_CL_COMPLETE* p_data);
59 static void bta_gattc_cmpl_sendmsg(uint16_t conn_id, tGATTC_OPTYPE op,
60                                    tGATT_STATUS status,
61                                    tGATT_CL_COMPLETE* p_data);
62 
63 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB* p_clreg);
64 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, const RawAddress& bda);
65 static void bta_gattc_cong_cback(uint16_t conn_id, bool congested);
66 static void bta_gattc_phy_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
67                                        uint8_t tx_phy, uint8_t rx_phy,
68                                        tGATT_STATUS status);
69 static void bta_gattc_conn_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
70                                         uint16_t interval, uint16_t latency,
71                                         uint16_t timeout, tGATT_STATUS status);
72 
73 static tGATT_CBACK bta_gattc_cl_cback = {
74     .p_conn_cb = bta_gattc_conn_cback,
75     .p_cmpl_cb = bta_gattc_cmpl_cback,
76     .p_disc_res_cb = bta_gattc_disc_res_cback,
77     .p_disc_cmpl_cb = bta_gattc_disc_cmpl_cback,
78     .p_req_cb = nullptr,
79     .p_enc_cmpl_cb = bta_gattc_enc_cmpl_cback,
80     .p_congestion_cb = bta_gattc_cong_cback,
81     .p_phy_update_cb = bta_gattc_phy_update_cback,
82     .p_conn_update_cb = bta_gattc_conn_update_cback};
83 
84 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
85 static uint16_t bta_gattc_opcode_to_int_evt[] = {
86     /* Skip: GATTC_OPTYPE_NONE */
87     /* Skip: GATTC_OPTYPE_DISCOVERY */
88     BTA_GATTC_API_READ_EVT,   /* GATTC_OPTYPE_READ */
89     BTA_GATTC_API_WRITE_EVT,  /* GATTC_OPTYPE_WRITE */
90     BTA_GATTC_API_EXEC_EVT,   /* GATTC_OPTYPE_EXE_WRITE */
91     BTA_GATTC_API_CFG_MTU_EVT /* GATTC_OPTYPE_CONFIG */
92 };
93 
94 static const char* bta_gattc_op_code_name[] = {
95     "Unknown",      /* GATTC_OPTYPE_NONE */
96     "Discovery",    /* GATTC_OPTYPE_DISCOVERY */
97     "Read",         /* GATTC_OPTYPE_READ */
98     "Write",        /* GATTC_OPTYPE_WRITE */
99     "Exec",         /* GATTC_OPTYPE_EXE_WRITE */
100     "Config",       /* GATTC_OPTYPE_CONFIG */
101     "Notification", /* GATTC_OPTYPE_NOTIFICATION */
102     "Indication"    /* GATTC_OPTYPE_INDICATION */
103 };
104 
105 /*****************************************************************************
106  *  Action Functions
107  ****************************************************************************/
108 
109 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb, tGATT_STATUS status);
110 
111 /** Enables GATTC module */
bta_gattc_enable()112 static void bta_gattc_enable() {
113   VLOG(1) << __func__;
114 
115   if (bta_gattc_cb.state == BTA_GATTC_STATE_DISABLED) {
116     /* initialize control block */
117     bta_gattc_cb = tBTA_GATTC_CB();
118     bta_gattc_cb.state = BTA_GATTC_STATE_ENABLED;
119   } else {
120     VLOG(1) << "GATTC is already enabled";
121   }
122 }
123 
124 /** Disable GATTC module by cleaning up all active connections and deregister
125  * all application */
bta_gattc_disable()126 void bta_gattc_disable() {
127   uint8_t i;
128 
129   VLOG(1) << __func__;
130 
131   if (bta_gattc_cb.state != BTA_GATTC_STATE_ENABLED) {
132     LOG(ERROR) << "not enabled, or disabled in progress";
133     return;
134   }
135 
136   for (i = 0; i < BTA_GATTC_CL_MAX; i++) {
137     if (!bta_gattc_cb.cl_rcb[i].in_use) continue;
138 
139     bta_gattc_cb.state = BTA_GATTC_STATE_DISABLING;
140 /* don't deregister HH GATT IF */
141 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
142     if (!bta_hh_le_is_hh_gatt_if(bta_gattc_cb.cl_rcb[i].client_if)) {
143       bta_gattc_deregister(&bta_gattc_cb.cl_rcb[i]);
144     }
145   }
146 
147   /* no registered apps, indicate disable completed */
148   if (bta_gattc_cb.state != BTA_GATTC_STATE_DISABLING) {
149     bta_gattc_cb = tBTA_GATTC_CB();
150     bta_gattc_cb.state = BTA_GATTC_STATE_DISABLED;
151   }
152 }
153 
154 /** start an application interface */
bta_gattc_start_if(uint8_t client_if)155 void bta_gattc_start_if(uint8_t client_if) {
156   if (!bta_gattc_cl_get_regcb(client_if)) {
157     LOG(ERROR) << "Unable to start app.: Unknown client_if=" << +client_if;
158     return;
159   }
160 
161   GATT_StartIf(client_if);
162 }
163 
164 /** Register a GATT client application with BTA */
bta_gattc_register(const Uuid & app_uuid,tBTA_GATTC_CBACK * p_cback,BtaAppRegisterCallback cb,bool eatt_suppport)165 void bta_gattc_register(const Uuid& app_uuid, tBTA_GATTC_CBACK* p_cback,
166                         BtaAppRegisterCallback cb, bool eatt_suppport) {
167   tGATT_STATUS status = GATT_NO_RESOURCES;
168   uint8_t client_if = 0;
169   VLOG(1) << __func__ << ": state:" << +bta_gattc_cb.state;
170 
171   /* check if  GATTC module is already enabled . Else enable */
172   if (bta_gattc_cb.state == BTA_GATTC_STATE_DISABLED) {
173     bta_gattc_enable();
174   }
175   /* todo need to check duplicate uuid */
176   for (uint8_t i = 0; i < BTA_GATTC_CL_MAX; i++) {
177     if (!bta_gattc_cb.cl_rcb[i].in_use) {
178       if ((bta_gattc_cb.cl_rcb[i].client_if = GATT_Register(
179                app_uuid, "GattClient", &bta_gattc_cl_cback, eatt_suppport)) ==
180           0) {
181         LOG(ERROR) << "Register with GATT stack failed.";
182         status = GATT_ERROR;
183       } else {
184         bta_gattc_cb.cl_rcb[i].in_use = true;
185         bta_gattc_cb.cl_rcb[i].p_cback = p_cback;
186         bta_gattc_cb.cl_rcb[i].app_uuid = app_uuid;
187 
188         /* BTA use the same client interface as BTE GATT statck */
189         client_if = bta_gattc_cb.cl_rcb[i].client_if;
190 
191         do_in_main_thread(FROM_HERE,
192                           base::Bind(&bta_gattc_start_if, client_if));
193 
194         status = GATT_SUCCESS;
195         break;
196       }
197     }
198   }
199 
200   if (!cb.is_null()) cb.Run(client_if, status);
201 }
202 
203 /** De-Register a GATT client application with BTA */
bta_gattc_deregister(tBTA_GATTC_RCB * p_clreg)204 void bta_gattc_deregister(tBTA_GATTC_RCB* p_clreg) {
205   if (!p_clreg) {
206     LOG(ERROR) << __func__ << ": Deregister Failed unknown client cif";
207     bta_hh_cleanup_disable(BTA_HH_OK);
208     return;
209   }
210 
211   uint8_t accept_list_size = 0;
212   if (controller_get_interface()->supports_ble()) {
213     accept_list_size = controller_get_interface()->get_ble_acceptlist_size();
214   }
215 
216   /* remove bg connection associated with this rcb */
217   for (uint8_t i = 0; i < accept_list_size; i++) {
218     if (!bta_gattc_cb.bg_track[i].in_use) continue;
219 
220     if (bta_gattc_cb.bg_track[i].cif_mask & (1 << (p_clreg->client_if - 1))) {
221       bta_gattc_mark_bg_conn(p_clreg->client_if,
222                              bta_gattc_cb.bg_track[i].remote_bda, false);
223       GATT_CancelConnect(p_clreg->client_if,
224                          bta_gattc_cb.bg_track[i].remote_bda, false);
225     }
226   }
227 
228   if (p_clreg->num_clcb == 0) {
229     bta_gattc_deregister_cmpl(p_clreg);
230     return;
231   }
232 
233   /* close all CLCB related to this app */
234   for (uint8_t i = 0; i < BTA_GATTC_CLCB_MAX; i++) {
235     if (!bta_gattc_cb.clcb[i].in_use || (bta_gattc_cb.clcb[i].p_rcb != p_clreg))
236       continue;
237 
238     p_clreg->dereg_pending = true;
239 
240     BT_HDR_RIGID buf;
241     buf.event = BTA_GATTC_API_CLOSE_EVT;
242     buf.layer_specific = bta_gattc_cb.clcb[i].bta_conn_id;
243     bta_gattc_close(&bta_gattc_cb.clcb[i], (tBTA_GATTC_DATA*)&buf);
244   }
245 }
246 
247 /** process connect API request */
bta_gattc_process_api_open(const tBTA_GATTC_DATA * p_msg)248 void bta_gattc_process_api_open(const tBTA_GATTC_DATA* p_msg) {
249   uint16_t event = ((BT_HDR_RIGID*)p_msg)->event;
250 
251   tBTA_GATTC_RCB* p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
252   if (!p_clreg) {
253     LOG(ERROR) << __func__
254                << ": Failed, unknown client_if=" << +p_msg->api_conn.client_if;
255     return;
256   }
257 
258   if (!p_msg->api_conn.is_direct) {
259     bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
260     return;
261   }
262 
263   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_alloc_clcb(
264       p_msg->api_conn.client_if, p_msg->api_conn.remote_bda,
265       p_msg->api_conn.transport);
266   if (p_clcb != NULL) {
267     bta_gattc_sm_execute(p_clcb, event, p_msg);
268   } else {
269     LOG(ERROR) << "No resources to open a new connection.";
270 
271     bta_gattc_send_open_cback(p_clreg, GATT_NO_RESOURCES,
272                               p_msg->api_conn.remote_bda, GATT_INVALID_CONN_ID,
273                               p_msg->api_conn.transport, 0);
274   }
275 }
276 
277 /** process connect API request */
bta_gattc_process_api_open_cancel(const tBTA_GATTC_DATA * p_msg)278 void bta_gattc_process_api_open_cancel(const tBTA_GATTC_DATA* p_msg) {
279   CHECK(p_msg != nullptr);
280 
281   uint16_t event = ((BT_HDR_RIGID*)p_msg)->event;
282 
283   if (!p_msg->api_cancel_conn.is_direct) {
284     LOG_DEBUG("Cancel GATT client background connection");
285     bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
286     return;
287   }
288   LOG_DEBUG("Cancel GATT client direct connection");
289 
290   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_cif(
291       p_msg->api_cancel_conn.client_if, p_msg->api_cancel_conn.remote_bda,
292       BT_TRANSPORT_LE);
293   if (p_clcb != NULL) {
294     bta_gattc_sm_execute(p_clcb, event, p_msg);
295     return;
296   }
297 
298   LOG(ERROR) << "No such connection need to be cancelled";
299 
300   tBTA_GATTC_RCB* p_clreg =
301       bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
302 
303   if (p_clreg && p_clreg->p_cback) {
304     tBTA_GATTC cb_data;
305     cb_data.status = GATT_ERROR;
306     (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
307   }
308 }
309 
310 /** process encryption complete message */
bta_gattc_process_enc_cmpl(tGATT_IF client_if,const RawAddress & bda)311 void bta_gattc_process_enc_cmpl(tGATT_IF client_if, const RawAddress& bda) {
312   tBTA_GATTC_RCB* p_clreg = bta_gattc_cl_get_regcb(client_if);
313 
314   if (!p_clreg || !p_clreg->p_cback) return;
315 
316   tBTA_GATTC cb_data;
317   memset(&cb_data, 0, sizeof(tBTA_GATTC));
318 
319   cb_data.enc_cmpl.client_if = client_if;
320   cb_data.enc_cmpl.remote_bda = bda;
321 
322   (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
323 }
324 
bta_gattc_cancel_open_error(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)325 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB* p_clcb,
326                                  UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
327   tBTA_GATTC cb_data;
328 
329   cb_data.status = GATT_ERROR;
330 
331   if (p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback)
332     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
333 }
334 
bta_gattc_open_error(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)335 void bta_gattc_open_error(tBTA_GATTC_CLCB* p_clcb,
336                           UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
337   LOG(ERROR) << "Connection already opened. wrong state";
338 
339   bta_gattc_send_open_cback(p_clcb->p_rcb, GATT_SUCCESS, p_clcb->bda,
340                             p_clcb->bta_conn_id, p_clcb->transport, 0);
341 }
342 
bta_gattc_open_fail(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)343 void bta_gattc_open_fail(tBTA_GATTC_CLCB* p_clcb,
344                          UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
345   LOG(WARNING) << __func__ << ": Cannot establish Connection. conn_id="
346                << loghex(p_clcb->bta_conn_id) << ". Return GATT_ERROR("
347                << +GATT_ERROR << ")";
348 
349   bta_gattc_send_open_cback(p_clcb->p_rcb, GATT_ERROR, p_clcb->bda,
350                             p_clcb->bta_conn_id, p_clcb->transport, 0);
351   /* open failure, remove clcb */
352   bta_gattc_clcb_dealloc(p_clcb);
353 }
354 
355 /** Process API connection function */
bta_gattc_open(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)356 void bta_gattc_open(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
357   tBTA_GATTC_DATA gattc_data;
358 
359   /* open/hold a connection */
360   if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda, true,
361                     p_data->api_conn.transport, p_data->api_conn.opportunistic,
362                     p_data->api_conn.initiating_phys)) {
363     LOG(ERROR) << "Connection open failure";
364 
365     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
366     return;
367   }
368 
369   /* a connected remote device */
370   if (GATT_GetConnIdIfConnected(
371           p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda,
372           &p_clcb->bta_conn_id, p_data->api_conn.transport)) {
373     gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
374 
375     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
376   }
377   /* else wait for the callback event */
378 }
379 
380 /** Process API Open for a background connection */
bta_gattc_init_bk_conn(const tBTA_GATTC_API_OPEN * p_data,tBTA_GATTC_RCB * p_clreg)381 void bta_gattc_init_bk_conn(const tBTA_GATTC_API_OPEN* p_data,
382                             tBTA_GATTC_RCB* p_clreg) {
383   if (!bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, true)) {
384     bta_gattc_send_open_cback(p_clreg, GATT_NO_RESOURCES, p_data->remote_bda,
385                               GATT_INVALID_CONN_ID, BT_TRANSPORT_LE, 0);
386     return;
387   }
388 
389   /* always call open to hold a connection */
390   if (!GATT_Connect(p_data->client_if, p_data->remote_bda, false,
391                     p_data->transport, false)) {
392     LOG(ERROR) << __func__
393                << " unable to connect to remote bd_addr=" << p_data->remote_bda;
394     bta_gattc_send_open_cback(p_clreg, GATT_ERROR, p_data->remote_bda,
395                               GATT_INVALID_CONN_ID, BT_TRANSPORT_LE, 0);
396     return;
397   }
398 
399   uint16_t conn_id;
400   /* if is not a connected remote device */
401   if (!GATT_GetConnIdIfConnected(p_data->client_if, p_data->remote_bda,
402                                  &conn_id, p_data->transport)) {
403     return;
404   }
405 
406   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_alloc_clcb(
407       p_data->client_if, p_data->remote_bda, BT_TRANSPORT_LE);
408   if (!p_clcb) return;
409 
410   tBTA_GATTC_DATA gattc_data;
411   gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
412 
413   /* open connection */
414   bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT,
415                        static_cast<const tBTA_GATTC_DATA*>(&gattc_data));
416 }
417 
418 /** Process API Cancel Open for a background connection */
bta_gattc_cancel_bk_conn(const tBTA_GATTC_API_CANCEL_OPEN * p_data)419 void bta_gattc_cancel_bk_conn(const tBTA_GATTC_API_CANCEL_OPEN* p_data) {
420   tBTA_GATTC_RCB* p_clreg;
421   tBTA_GATTC cb_data;
422   cb_data.status = GATT_ERROR;
423 
424   /* remove the device from the bg connection mask */
425   if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, false)) {
426     if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, false)) {
427       cb_data.status = GATT_SUCCESS;
428     } else {
429       LOG(ERROR) << __func__ << ": failed";
430     }
431   }
432   p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
433 
434   if (p_clreg && p_clreg->p_cback) {
435     (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
436   }
437 }
438 
bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)439 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB* p_clcb,
440                               UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
441   tBTA_GATTC cb_data;
442 
443   if (p_clcb->p_rcb->p_cback) {
444     cb_data.status = GATT_SUCCESS;
445     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
446   }
447 
448   bta_gattc_clcb_dealloc(p_clcb);
449 }
450 
bta_gattc_cancel_open(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)451 void bta_gattc_cancel_open(tBTA_GATTC_CLCB* p_clcb,
452                            const tBTA_GATTC_DATA* p_data) {
453   tBTA_GATTC cb_data;
454 
455   if (GATT_CancelConnect(p_clcb->p_rcb->client_if,
456                          p_data->api_cancel_conn.remote_bda, true)) {
457     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
458   } else {
459     if (p_clcb->p_rcb->p_cback) {
460       cb_data.status = GATT_ERROR;
461       (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
462     }
463   }
464 }
465 
466 /** receive connection callback from stack */
bta_gattc_conn(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)467 void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
468   tGATT_IF gatt_if;
469   VLOG(1) << __func__ << ": server cache state=" << +p_clcb->p_srcb->state;
470 
471   if (p_data != NULL) {
472     VLOG(1) << __func__ << ": conn_id=" << loghex(p_data->hdr.layer_specific);
473     p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific;
474 
475     GATT_GetConnectionInfor(p_data->hdr.layer_specific, &gatt_if, p_clcb->bda,
476                             &p_clcb->transport);
477   }
478 
479   p_clcb->p_srcb->connected = true;
480 
481   if (p_clcb->p_srcb->mtu == 0) p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
482 
483   /* start database cache if needed */
484   if (p_clcb->p_srcb->gatt_database.IsEmpty() ||
485       p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE) {
486     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) {
487       p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
488       if (bta_gattc_cache_load(p_clcb->p_srcb)) {
489         p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
490         bta_gattc_reset_discover_st(p_clcb->p_srcb, GATT_SUCCESS);
491       } else {
492         p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
493         /* cache load failure, start discovery */
494         bta_gattc_start_discover(p_clcb, NULL);
495       }
496     } else /* cache is building */
497       p_clcb->state = BTA_GATTC_DISCOVER_ST;
498   }
499 
500   else {
501     /* a pending service handle change indication */
502     if (p_clcb->p_srcb->srvc_hdl_chg) {
503       p_clcb->p_srcb->srvc_hdl_chg = false;
504 
505       /* set true to read database hash before service discovery */
506       if (bta_gattc_is_robust_caching_enabled()) {
507         p_clcb->p_srcb->srvc_hdl_db_hash = true;
508       }
509 
510       /* start discovery */
511       bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
512     }
513   }
514 
515   if (p_clcb->p_rcb) {
516     /* there is no RM for GATT */
517     if (p_clcb->transport == BT_TRANSPORT_BR_EDR)
518       bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
519 
520     bta_gattc_send_open_cback(p_clcb->p_rcb, GATT_SUCCESS, p_clcb->bda,
521                               p_clcb->bta_conn_id, p_clcb->transport,
522                               p_clcb->p_srcb->mtu);
523   }
524 }
525 
526 /** close a  connection */
bta_gattc_close_fail(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)527 void bta_gattc_close_fail(tBTA_GATTC_CLCB* p_clcb,
528                           const tBTA_GATTC_DATA* p_data) {
529   tBTA_GATTC cb_data;
530 
531   if (p_clcb->p_rcb->p_cback) {
532     memset(&cb_data, 0, sizeof(tBTA_GATTC));
533     cb_data.close.client_if = p_clcb->p_rcb->client_if;
534     cb_data.close.conn_id = p_data->hdr.layer_specific;
535     cb_data.close.remote_bda = p_clcb->bda;
536     cb_data.close.reason = BTA_GATT_CONN_NONE;
537 
538     LOG(WARNING) << __func__ << ": conn_id=" << loghex(cb_data.close.conn_id)
539                  << ". Returns GATT_ERROR(" << +GATT_ERROR << ").";
540 
541     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
542   }
543 }
544 
545 /** close a GATTC connection */
bta_gattc_close(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)546 void bta_gattc_close(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
547   tBTA_GATTC_CBACK* p_cback = p_clcb->p_rcb->p_cback;
548   tBTA_GATTC_RCB* p_clreg = p_clcb->p_rcb;
549   tBTA_GATTC cb_data = {
550       .close =
551           {
552               .client_if = p_clcb->p_rcb->client_if,
553               .conn_id = p_clcb->bta_conn_id,
554               .reason = GATT_CONN_OK,
555               .remote_bda = p_clcb->bda,
556               .status = GATT_SUCCESS,
557           },
558   };
559 
560   if (p_clcb->transport == BT_TRANSPORT_BR_EDR)
561     bta_sys_conn_close(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
562 
563   bta_gattc_clcb_dealloc(p_clcb);
564 
565   if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT) {
566     GATT_Disconnect(p_data->hdr.layer_specific);
567     LOG_DEBUG("Local close event client_if:%hu conn_id:%hu reason:%s",
568               cb_data.close.client_if, cb_data.close.conn_id,
569               gatt_disconnection_reason_text(
570                   static_cast<tGATT_DISCONN_REASON>(cb_data.close.reason))
571                   .c_str());
572   } else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT) {
573     cb_data.close.reason = p_data->int_conn.reason;
574     LOG_DEBUG("Peer close disconnect event client_if:%hu conn_id:%hu reason:%s",
575               cb_data.close.client_if, cb_data.close.conn_id,
576               gatt_disconnection_reason_text(
577                   static_cast<tGATT_DISCONN_REASON>(cb_data.close.reason))
578                   .c_str());
579   }
580 
581   if (p_cback) (*p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
582 
583   if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending) {
584     bta_gattc_deregister_cmpl(p_clreg);
585   }
586 }
587 
588 /** when a SRCB finished discovery, tell all related clcb */
bta_gattc_reset_discover_st(tBTA_GATTC_SERV * p_srcb,tGATT_STATUS status)589 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb, tGATT_STATUS status) {
590   for (uint8_t i = 0; i < BTA_GATTC_CLCB_MAX; i++) {
591     if (bta_gattc_cb.clcb[i].p_srcb == p_srcb) {
592       bta_gattc_cb.clcb[i].status = status;
593       bta_gattc_sm_execute(&bta_gattc_cb.clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT,
594                            NULL);
595     }
596   }
597 }
598 
599 /** close a GATTC connection while in discovery state */
bta_gattc_disc_close(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)600 void bta_gattc_disc_close(tBTA_GATTC_CLCB* p_clcb,
601                           const tBTA_GATTC_DATA* p_data) {
602   VLOG(1) << __func__
603           << ": Discovery cancel conn_id=" << loghex(p_clcb->bta_conn_id);
604 
605   if (p_clcb->disc_active)
606     bta_gattc_reset_discover_st(p_clcb->p_srcb, GATT_ERROR);
607   else
608     p_clcb->state = BTA_GATTC_CONN_ST;
609 
610   // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
611   // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
612   // connection itself still needs to be closed to resolve the original event.
613   if (p_clcb->state == BTA_GATTC_CONN_ST) {
614     VLOG(1) << "State is back to BTA_GATTC_CONN_ST. Trigger connection close";
615     bta_gattc_close(p_clcb, p_data);
616   }
617 }
618 
619 /** when a SRCB start discovery, tell all related clcb and set the state */
bta_gattc_set_discover_st(tBTA_GATTC_SERV * p_srcb)620 void bta_gattc_set_discover_st(tBTA_GATTC_SERV* p_srcb) {
621   uint8_t i;
622 
623   for (i = 0; i < BTA_GATTC_CLCB_MAX; i++) {
624     if (bta_gattc_cb.clcb[i].p_srcb == p_srcb) {
625       bta_gattc_cb.clcb[i].status = GATT_SUCCESS;
626       bta_gattc_cb.clcb[i].state = BTA_GATTC_DISCOVER_ST;
627       bta_gattc_cb.clcb[i].request_during_discovery =
628           BTA_GATTC_DISCOVER_REQ_NONE;
629     }
630   }
631 }
632 
633 /** process service change in discovery state, mark up the auto update flag and
634  * set status to be discovery cancel for current discovery.
635  */
bta_gattc_restart_discover(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)636 void bta_gattc_restart_discover(tBTA_GATTC_CLCB* p_clcb,
637                                 UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
638   p_clcb->status = GATT_CANCEL;
639   p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
640 }
641 
642 /** Configure MTU size on the GATT connection */
bta_gattc_cfg_mtu(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)643 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
644   if (!bta_gattc_enqueue(p_clcb, p_data)) return;
645 
646   tGATT_STATUS status =
647       GATTC_ConfigureMTU(p_clcb->bta_conn_id, p_data->api_mtu.mtu);
648 
649   /* if failed, return callback here */
650   if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) {
651     /* Dequeue the data, if it was enqueued */
652     if (p_clcb->p_q_cmd == p_data) p_clcb->p_q_cmd = NULL;
653 
654     bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status,
655                            NULL);
656   }
657 }
658 
bta_gattc_start_discover_internal(tBTA_GATTC_CLCB * p_clcb)659 void bta_gattc_start_discover_internal(tBTA_GATTC_CLCB* p_clcb) {
660   if (p_clcb->transport == BT_TRANSPORT_LE)
661     L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, false);
662 
663   bta_gattc_init_cache(p_clcb->p_srcb);
664   p_clcb->status = bta_gattc_discover_pri_service(
665       p_clcb->bta_conn_id, p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
666   if (p_clcb->status != GATT_SUCCESS) {
667     LOG(ERROR) << "discovery on server failed";
668     bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
669   } else
670     p_clcb->disc_active = true;
671 }
672 
673 /** Start a discovery on server */
bta_gattc_start_discover(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)674 void bta_gattc_start_discover(tBTA_GATTC_CLCB* p_clcb,
675                               UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
676   VLOG(1) << __func__ << ": conn_id:" << loghex(p_clcb->bta_conn_id)
677           << " p_clcb->p_srcb->state:" << +p_clcb->p_srcb->state;
678 
679   if (((p_clcb->p_q_cmd == NULL ||
680         p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
681        p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
682       p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
683   /* no pending operation, start discovery right away */
684   {
685     p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
686 
687     if (p_clcb->p_srcb != NULL) {
688       /* set all srcb related clcb into discovery ST */
689       bta_gattc_set_discover_st(p_clcb->p_srcb);
690 
691       /* clear the service change mask */
692       p_clcb->p_srcb->srvc_hdl_chg = false;
693       p_clcb->p_srcb->update_count = 0;
694       p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
695 
696       /* read db hash if db hash characteristic exists */
697       if (bta_gattc_is_robust_caching_enabled() &&
698           p_clcb->p_srcb->srvc_hdl_db_hash && bta_gattc_read_db_hash(p_clcb)) {
699         LOG(INFO) << __func__
700                   << ": pending service discovery, read db hash first";
701         p_clcb->p_srcb->srvc_hdl_db_hash = false;
702         return;
703       }
704 
705       bta_gattc_start_discover_internal(p_clcb);
706     } else {
707       LOG(ERROR) << "unknown device, can not start discovery";
708     }
709   }
710   /* pending operation, wait until it finishes */
711   else {
712     p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
713 
714     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
715       p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
716   }
717 }
718 
719 /** discovery on server is finished */
bta_gattc_disc_cmpl(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)720 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb,
721                          UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
722   const tBTA_GATTC_DATA* p_q_cmd = p_clcb->p_q_cmd;
723 
724   VLOG(1) << __func__ << ": conn_id=" << loghex(p_clcb->bta_conn_id);
725 
726   if (p_clcb->transport == BT_TRANSPORT_LE)
727     L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, true);
728   p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
729   p_clcb->disc_active = false;
730 
731   if (p_clcb->status != GATT_SUCCESS) {
732     /* clean up cache */
733     if (p_clcb->p_srcb) {
734       p_clcb->p_srcb->gatt_database.Clear();
735     }
736 
737     /* used to reset cache in application */
738     bta_gattc_cache_reset(p_clcb->p_srcb->server_bda);
739   }
740 
741   if (p_clcb->p_srcb) {
742     p_clcb->p_srcb->pending_discovery.Clear();
743   }
744 
745   if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING) {
746     /* start discovery again */
747     p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
748     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
749   }
750   /* get any queued command to proceed */
751   else if (p_q_cmd != NULL) {
752     p_clcb->p_q_cmd = NULL;
753     /* execute pending operation of link block still present */
754     if (L2CA_IsLinkEstablished(p_clcb->p_srcb->server_bda, p_clcb->transport)) {
755       bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
756     }
757     /* if the command executed requeued the cmd, we don't
758      * want to free the underlying buffer that's being
759      * referenced by p_clcb->p_q_cmd
760      */
761     if (p_q_cmd != p_clcb->p_q_cmd) osi_free_and_reset((void**)&p_q_cmd);
762   }
763 
764   if (p_clcb->p_rcb->p_cback) {
765     tBTA_GATTC bta_gattc;
766     bta_gattc.remote_bda = p_clcb->p_srcb->server_bda;
767     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_SRVC_DISC_DONE_EVT, &bta_gattc);
768   }
769 }
770 
771 /** Read an attribute */
bta_gattc_read(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)772 void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
773   if (!bta_gattc_enqueue(p_clcb, p_data)) return;
774 
775   tGATT_STATUS status;
776   if (p_data->api_read.handle != 0) {
777     tGATT_READ_PARAM read_param;
778     memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
779     read_param.by_handle.handle = p_data->api_read.handle;
780     read_param.by_handle.auth_req = p_data->api_read.auth_req;
781     status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
782   } else {
783     tGATT_READ_PARAM read_param;
784     memset(&read_param, 0, sizeof(tGATT_READ_BY_TYPE));
785 
786     read_param.char_type.s_handle = p_data->api_read.s_handle;
787     read_param.char_type.e_handle = p_data->api_read.e_handle;
788     read_param.char_type.uuid = p_data->api_read.uuid;
789     read_param.char_type.auth_req = p_data->api_read.auth_req;
790     status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_TYPE, &read_param);
791   }
792 
793   /* read fail */
794   if (status != GATT_SUCCESS) {
795     /* Dequeue the data, if it was enqueued */
796     if (p_clcb->p_q_cmd == p_data) p_clcb->p_q_cmd = NULL;
797 
798     bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status,
799                            NULL);
800   }
801 }
802 
803 /** read multiple */
bta_gattc_read_multi(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)804 void bta_gattc_read_multi(tBTA_GATTC_CLCB* p_clcb,
805                           const tBTA_GATTC_DATA* p_data) {
806   if (!bta_gattc_enqueue(p_clcb, p_data)) return;
807 
808   tGATT_READ_PARAM read_param;
809   memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
810 
811   read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
812   read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
813   memcpy(&read_param.read_multiple.handles, p_data->api_read_multi.handles,
814          sizeof(uint16_t) * p_data->api_read_multi.num_attr);
815 
816   tGATT_STATUS status =
817       GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
818   /* read fail */
819   if (status != GATT_SUCCESS) {
820     /* Dequeue the data, if it was enqueued */
821     if (p_clcb->p_q_cmd == p_data) p_clcb->p_q_cmd = NULL;
822 
823     bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status,
824                            NULL);
825   }
826 }
827 
828 /** Write an attribute */
bta_gattc_write(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)829 void bta_gattc_write(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
830   if (!bta_gattc_enqueue(p_clcb, p_data)) return;
831 
832   tGATT_STATUS status = GATT_SUCCESS;
833   tGATT_VALUE attr;
834 
835   attr.conn_id = p_clcb->bta_conn_id;
836   attr.handle = p_data->api_write.handle;
837   attr.offset = p_data->api_write.offset;
838   attr.len = p_data->api_write.len;
839   attr.auth_req = p_data->api_write.auth_req;
840 
841   if (p_data->api_write.p_value)
842     memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
843 
844   status =
845       GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
846 
847   /* write fail */
848   if (status != GATT_SUCCESS) {
849     /* Dequeue the data, if it was enqueued */
850     if (p_clcb->p_q_cmd == p_data) p_clcb->p_q_cmd = NULL;
851 
852     bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status,
853                            NULL);
854   }
855 }
856 
857 /** send execute write */
bta_gattc_execute(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)858 void bta_gattc_execute(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
859   if (!bta_gattc_enqueue(p_clcb, p_data)) return;
860 
861   tGATT_STATUS status =
862       GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
863   if (status != GATT_SUCCESS) {
864     /* Dequeue the data, if it was enqueued */
865     if (p_clcb->p_q_cmd == p_data) p_clcb->p_q_cmd = NULL;
866 
867     bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_EXE_WRITE, status,
868                            NULL);
869   }
870 }
871 
872 /** send handle value confirmation */
bta_gattc_confirm(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)873 void bta_gattc_confirm(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
874   uint16_t cid = p_data->api_confirm.cid;
875 
876   if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific,
877                                    cid) != GATT_SUCCESS) {
878     LOG(ERROR) << __func__ << ": to cid=" << loghex(cid) << " failed";
879   } else {
880     /* if over BR_EDR, inform PM for mode change */
881     if (p_clcb->transport == BT_TRANSPORT_BR_EDR) {
882       bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
883       bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
884     }
885   }
886 }
887 
888 /** read complete */
bta_gattc_read_cmpl(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_OP_CMPL * p_data)889 static void bta_gattc_read_cmpl(tBTA_GATTC_CLCB* p_clcb,
890                                 const tBTA_GATTC_OP_CMPL* p_data) {
891   GATT_READ_OP_CB cb = p_clcb->p_q_cmd->api_read.read_cb;
892   void* my_cb_data = p_clcb->p_q_cmd->api_read.read_cb_data;
893 
894   /* if it was read by handle, return the handle requested, if read by UUID, use
895    * handle returned from remote
896    */
897   uint16_t handle = p_clcb->p_q_cmd->api_read.handle;
898   if (handle == 0) handle = p_data->p_cmpl->att_value.handle;
899 
900   osi_free_and_reset((void**)&p_clcb->p_q_cmd);
901 
902   if (cb) {
903     cb(p_clcb->bta_conn_id, p_data->status, handle,
904        p_data->p_cmpl->att_value.len, p_data->p_cmpl->att_value.value,
905        my_cb_data);
906   }
907 }
908 
909 /** write complete */
bta_gattc_write_cmpl(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_OP_CMPL * p_data)910 static void bta_gattc_write_cmpl(tBTA_GATTC_CLCB* p_clcb,
911                                  const tBTA_GATTC_OP_CMPL* p_data) {
912   GATT_WRITE_OP_CB cb = p_clcb->p_q_cmd->api_write.write_cb;
913   void* my_cb_data = p_clcb->p_q_cmd->api_write.write_cb_data;
914 
915   osi_free_and_reset((void**)&p_clcb->p_q_cmd);
916 
917   if (cb) {
918     cb(p_clcb->bta_conn_id, p_data->status, p_data->p_cmpl->att_value.handle,
919        my_cb_data);
920   }
921 }
922 
923 /** execute write complete */
bta_gattc_exec_cmpl(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_OP_CMPL * p_data)924 static void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB* p_clcb,
925                                 const tBTA_GATTC_OP_CMPL* p_data) {
926   tBTA_GATTC cb_data;
927 
928   osi_free_and_reset((void**)&p_clcb->p_q_cmd);
929   p_clcb->status = GATT_SUCCESS;
930 
931   /* execute complete, callback */
932   cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
933   cb_data.exec_cmpl.status = p_data->status;
934 
935   (*p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
936 }
937 
938 /** configure MTU operation complete */
bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_OP_CMPL * p_data)939 static void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB* p_clcb,
940                                    const tBTA_GATTC_OP_CMPL* p_data) {
941   GATT_CONFIGURE_MTU_OP_CB cb = p_clcb->p_q_cmd->api_mtu.mtu_cb;
942   void* my_cb_data = p_clcb->p_q_cmd->api_mtu.mtu_cb_data;
943   tBTA_GATTC cb_data;
944 
945   osi_free_and_reset((void**)&p_clcb->p_q_cmd);
946 
947   if (p_data->p_cmpl && p_data->status == GATT_SUCCESS)
948     p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
949 
950   /* configure MTU complete, callback */
951   p_clcb->status = p_data->status;
952   cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
953   cb_data.cfg_mtu.status = p_data->status;
954   cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
955 
956   if (cb) {
957     cb(p_clcb->bta_conn_id, p_data->status, my_cb_data);
958   }
959 
960   (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CFG_MTU_EVT, &cb_data);
961 }
962 
963 /** operation completed */
bta_gattc_op_cmpl(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)964 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
965   if (p_clcb->p_q_cmd == NULL) {
966     LOG_ERROR("No pending command gatt client command");
967     return;
968   }
969 
970   const tGATTC_OPTYPE op = p_data->op_cmpl.op_code;
971   switch (op) {
972     case GATTC_OPTYPE_READ:
973     case GATTC_OPTYPE_WRITE:
974     case GATTC_OPTYPE_EXE_WRITE:
975     case GATTC_OPTYPE_CONFIG:
976       break;
977 
978     case GATTC_OPTYPE_NONE:
979     case GATTC_OPTYPE_DISCOVERY:
980     case GATTC_OPTYPE_NOTIFICATION:
981     case GATTC_OPTYPE_INDICATION:
982     default:
983       LOG(ERROR) << "unexpected operation, ignored";
984       return;
985   }
986 
987   if (p_clcb->p_q_cmd->hdr.event !=
988       bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ]) {
989     uint8_t mapped_op =
990         p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
991     if (mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
992 
993     LOG(ERROR) << StringPrintf(
994         "expect op:(%s :0x%04x), receive unexpected operation (%s).",
995         bta_gattc_op_code_name[mapped_op], p_clcb->p_q_cmd->hdr.event,
996         bta_gattc_op_code_name[op]);
997     return;
998   }
999 
1000   /* Except for MTU configuration, discard responses if service change
1001    * indication is received before operation completed
1002    */
1003   if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING &&
1004       p_clcb->p_srcb->srvc_hdl_chg && op != GATTC_OPTYPE_CONFIG) {
1005     VLOG(1) << "Discard all responses when service change indication is "
1006                "received.";
1007     // TODO Fix constness
1008     const_cast<tBTA_GATTC_DATA*>(p_data)->op_cmpl.status = GATT_ERROR;
1009   }
1010 
1011   /* service handle change void the response, discard it */
1012   if (op == GATTC_OPTYPE_READ)
1013     bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1014 
1015   else if (op == GATTC_OPTYPE_WRITE)
1016     bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1017 
1018   else if (op == GATTC_OPTYPE_EXE_WRITE)
1019     bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1020 
1021   else if (op == GATTC_OPTYPE_CONFIG)
1022     bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1023 
1024   // If receive DATABASE_OUT_OF_SYNC error code, bta_gattc should start service
1025   // discovery immediately
1026   if (bta_gattc_is_robust_caching_enabled() &&
1027       p_data->op_cmpl.status == GATT_DATABASE_OUT_OF_SYNC) {
1028     LOG(INFO) << __func__ << ": DATABASE_OUT_OF_SYNC, re-discover service";
1029     p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1030     /* request read db hash first */
1031     p_clcb->p_srcb->srvc_hdl_db_hash = true;
1032     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1033     return;
1034   }
1035 
1036   if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING) {
1037     p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1038 
1039     /* request read db hash first */
1040     if (bta_gattc_is_robust_caching_enabled()) {
1041       p_clcb->p_srcb->srvc_hdl_db_hash = true;
1042     }
1043 
1044     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1045   }
1046 }
1047 
1048 /** start a search in the local server cache */
bta_gattc_search(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)1049 void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
1050   tGATT_STATUS status = GATT_INTERNAL_ERROR;
1051   tBTA_GATTC cb_data;
1052   VLOG(1) << __func__ << ": conn_id=" << loghex(p_clcb->bta_conn_id);
1053   if (p_clcb->p_srcb && !p_clcb->p_srcb->gatt_database.IsEmpty()) {
1054     status = GATT_SUCCESS;
1055     /* search the local cache of a server device */
1056     bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1057   }
1058   cb_data.search_cmpl.status = status;
1059   cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1060 
1061   /* end of search or no server cache available */
1062   (*p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1063 }
1064 
1065 /** enqueue a command into control block, usually because discovery operation is
1066  * busy */
bta_gattc_q_cmd(tBTA_GATTC_CLCB * p_clcb,const tBTA_GATTC_DATA * p_data)1067 void bta_gattc_q_cmd(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data) {
1068   bta_gattc_enqueue(p_clcb, p_data);
1069 }
1070 
1071 /** report API call failure back to apps */
bta_gattc_fail(tBTA_GATTC_CLCB * p_clcb,UNUSED_ATTR const tBTA_GATTC_DATA * p_data)1072 void bta_gattc_fail(tBTA_GATTC_CLCB* p_clcb,
1073                     UNUSED_ATTR const tBTA_GATTC_DATA* p_data) {
1074   if (p_clcb->status == GATT_SUCCESS) {
1075     LOG(ERROR) << "operation not supported at current state " << +p_clcb->state;
1076   }
1077 }
1078 
1079 /* De-Register a GATT client application with BTA completed */
bta_gattc_deregister_cmpl(tBTA_GATTC_RCB * p_clreg)1080 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB* p_clreg) {
1081   tGATT_IF client_if = p_clreg->client_if;
1082   tBTA_GATTC cb_data;
1083   tBTA_GATTC_CBACK* p_cback = p_clreg->p_cback;
1084 
1085   memset(&cb_data, 0, sizeof(tBTA_GATTC));
1086 
1087   GATT_Deregister(p_clreg->client_if);
1088   memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1089 
1090   cb_data.reg_oper.client_if = client_if;
1091   cb_data.reg_oper.status = GATT_SUCCESS;
1092 
1093   if (p_cback) /* callback with de-register event */
1094     (*p_cback)(BTA_GATTC_DEREG_EVT, &cb_data);
1095 
1096   if (bta_gattc_num_reg_app() == 0 &&
1097       bta_gattc_cb.state == BTA_GATTC_STATE_DISABLING) {
1098     bta_gattc_cb.state = BTA_GATTC_STATE_DISABLED;
1099   }
1100 }
1101 
1102 /** callback functions to GATT client stack */
bta_gattc_conn_cback(tGATT_IF gattc_if,const RawAddress & bdaddr,uint16_t conn_id,bool connected,tGATT_DISCONN_REASON reason,tBT_TRANSPORT transport)1103 static void bta_gattc_conn_cback(tGATT_IF gattc_if, const RawAddress& bdaddr,
1104                                  uint16_t conn_id, bool connected,
1105                                  tGATT_DISCONN_REASON reason,
1106                                  tBT_TRANSPORT transport) {
1107   if (connected) {
1108     LOG_INFO("Connected att_id:%hhu transport:%s reason:%s", gattc_if,
1109              bt_transport_text(transport).c_str(),
1110              gatt_disconnection_reason_text(reason).c_str());
1111     btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1112   } else {
1113     LOG_INFO("Disconnected att_id:%hhu transport:%s reason:%s", gattc_if,
1114              bt_transport_text(transport).c_str(),
1115              gatt_disconnection_reason_text(reason).c_str());
1116     btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, GATT_CONN_UNKNOWN);
1117   }
1118 
1119   tBTA_GATTC_DATA* p_buf =
1120       (tBTA_GATTC_DATA*)osi_calloc(sizeof(tBTA_GATTC_DATA));
1121   p_buf->int_conn.hdr.event =
1122       connected ? BTA_GATTC_INT_CONN_EVT : BTA_GATTC_INT_DISCONN_EVT;
1123   p_buf->int_conn.hdr.layer_specific = conn_id;
1124   p_buf->int_conn.client_if = gattc_if;
1125   p_buf->int_conn.role = L2CA_GetBleConnRole(bdaddr);
1126   p_buf->int_conn.reason = reason;
1127   p_buf->int_conn.transport = transport;
1128   p_buf->int_conn.remote_bda = bdaddr;
1129 
1130   bta_sys_sendmsg(p_buf);
1131 }
1132 
1133 /** encryption complete callback function to GATT client stack */
bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if,const RawAddress & bda)1134 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, const RawAddress& bda) {
1135   tBTA_GATTC_CLCB* p_clcb =
1136       bta_gattc_find_clcb_by_cif(gattc_if, bda, BT_TRANSPORT_LE);
1137 
1138   if (p_clcb == NULL) return;
1139 
1140   /* filter this event just for BTA HH LE GATT client,
1141    * In the future, if we want to enable encryption complete event
1142    * for all GATT clients, we can remove this code
1143    */
1144   if (!bta_hh_le_is_hh_gatt_if(gattc_if)) {
1145     return;
1146   }
1147 
1148   VLOG(1) << __func__ << ": cif:" << +gattc_if;
1149 
1150   do_in_main_thread(FROM_HERE,
1151                     base::Bind(&bta_gattc_process_enc_cmpl, gattc_if, bda));
1152 }
1153 
1154 /** process refresh API to delete cache and start a new discovery if currently
1155  * connected */
bta_gattc_process_api_refresh(const RawAddress & remote_bda)1156 void bta_gattc_process_api_refresh(const RawAddress& remote_bda) {
1157   tBTA_GATTC_SERV* p_srvc_cb = bta_gattc_find_srvr_cache(remote_bda);
1158   if (p_srvc_cb) {
1159     /* try to find a CLCB */
1160     if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0) {
1161       bool found = false;
1162       tBTA_GATTC_CLCB* p_clcb = &bta_gattc_cb.clcb[0];
1163       for (uint8_t i = 0; i < BTA_GATTC_CLCB_MAX; i++, p_clcb++) {
1164         if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb) {
1165           found = true;
1166           break;
1167         }
1168       }
1169       if (found) {
1170         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1171         return;
1172       }
1173     }
1174     /* in all other cases, mark it and delete the cache */
1175 
1176     p_srvc_cb->gatt_database.Clear();
1177   }
1178 
1179   /* used to reset cache in application */
1180   bta_gattc_cache_reset(remote_bda);
1181 }
1182 
1183 /** process service change indication */
bta_gattc_process_srvc_chg_ind(uint16_t conn_id,tBTA_GATTC_RCB * p_clrcb,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_NOTIFY * p_notify,tGATT_VALUE * att_value)1184 bool bta_gattc_process_srvc_chg_ind(uint16_t conn_id, tBTA_GATTC_RCB* p_clrcb,
1185                                     tBTA_GATTC_SERV* p_srcb,
1186                                     tBTA_GATTC_CLCB* p_clcb,
1187                                     tBTA_GATTC_NOTIFY* p_notify,
1188                                     tGATT_VALUE* att_value) {
1189 
1190   Uuid gattp_uuid = Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER);
1191   Uuid srvc_chg_uuid = Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD);
1192 
1193   if (p_srcb->gatt_database.IsEmpty() && p_srcb->state == BTA_GATTC_SERV_IDLE) {
1194     bta_gattc_cache_load(p_srcb);
1195   }
1196 
1197   const gatt::Characteristic* p_char =
1198       bta_gattc_get_characteristic_srcb(p_srcb, p_notify->handle);
1199   if (!p_char) return false;
1200   const gatt::Service* p_svc =
1201       bta_gattc_get_service_for_handle_srcb(p_srcb, p_char->value_handle);
1202   if (!p_svc || p_svc->uuid != gattp_uuid || p_char->uuid != srvc_chg_uuid) {
1203     return false;
1204   }
1205 
1206   if (att_value->len != BTA_GATTC_SERVICE_CHANGED_LEN) {
1207     LOG(ERROR) << __func__
1208                << ": received malformed service changed indication, skipping";
1209     return false;
1210   }
1211 
1212   uint8_t* p = att_value->value;
1213   uint16_t s_handle = ((uint16_t)(*(p)) + (((uint16_t)(*(p + 1))) << 8));
1214   uint16_t e_handle = ((uint16_t)(*(p + 2)) + (((uint16_t)(*(p + 3))) << 8));
1215 
1216   LOG(ERROR) << __func__ << ": service changed s_handle=" << loghex(s_handle)
1217              << ", e_handle=" << loghex(e_handle);
1218 
1219   /* mark service handle change pending */
1220   p_srcb->srvc_hdl_chg = true;
1221   /* clear up all notification/indication registration */
1222   bta_gattc_clear_notif_registration(p_srcb, conn_id, s_handle, e_handle);
1223   /* service change indication all received, do discovery update */
1224   if (++p_srcb->update_count == bta_gattc_num_reg_app()) {
1225     /* not an opened connection; or connection busy */
1226     /* search for first available clcb and start discovery */
1227     if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL)) {
1228       for (size_t i = 0; i < BTA_GATTC_CLCB_MAX; i++) {
1229         if (bta_gattc_cb.clcb[i].in_use &&
1230             bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1231             bta_gattc_cb.clcb[i].p_q_cmd == NULL) {
1232           p_clcb = &bta_gattc_cb.clcb[i];
1233           break;
1234         }
1235       }
1236     }
1237     /* send confirmation here if this is an indication, it should always be */
1238     GATTC_SendHandleValueConfirm(conn_id, p_notify->cid);
1239 
1240     /* if connection available, refresh cache by doing discovery now */
1241     if (p_clcb) {
1242       /* request read db hash first */
1243       if (bta_gattc_is_robust_caching_enabled()) {
1244         p_srcb->srvc_hdl_db_hash = true;
1245       }
1246       bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1247     }
1248   }
1249 
1250   /* notify applicationf or service change */
1251   if (p_clrcb->p_cback) {
1252     tBTA_GATTC bta_gattc;
1253     bta_gattc.service_changed.remote_bda = p_srcb->server_bda;
1254     bta_gattc.service_changed.conn_id = conn_id;
1255     (*p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, &bta_gattc);
1256   }
1257 
1258   return true;
1259 }
1260 
1261 /** process all non-service change indication/notification */
bta_gattc_proc_other_indication(tBTA_GATTC_CLCB * p_clcb,uint8_t op,tGATT_CL_COMPLETE * p_data,tBTA_GATTC_NOTIFY * p_notify)1262 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB* p_clcb, uint8_t op,
1263                                      tGATT_CL_COMPLETE* p_data,
1264                                      tBTA_GATTC_NOTIFY* p_notify) {
1265   VLOG(1) << __func__
1266           << StringPrintf(
1267                  ": check p_data->att_value.handle=%d p_data->handle=%d",
1268                  p_data->att_value.handle, p_data->handle);
1269   VLOG(1) << "is_notify " << p_notify->is_notify;
1270 
1271   p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? false : true;
1272   p_notify->len = p_data->att_value.len;
1273   p_notify->bda = p_clcb->bda;
1274   memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
1275   p_notify->conn_id = p_clcb->bta_conn_id;
1276 
1277   if (p_clcb->p_rcb->p_cback) {
1278     tBTA_GATTC bta_gattc;
1279     bta_gattc.notify = *p_notify;
1280     (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, &bta_gattc);
1281   }
1282 }
1283 
1284 /** process indication/notification */
bta_gattc_process_indicate(uint16_t conn_id,tGATTC_OPTYPE op,tGATT_CL_COMPLETE * p_data)1285 void bta_gattc_process_indicate(uint16_t conn_id, tGATTC_OPTYPE op,
1286                                 tGATT_CL_COMPLETE* p_data) {
1287   uint16_t handle = p_data->att_value.handle;
1288   tBTA_GATTC_NOTIFY notify;
1289   RawAddress remote_bda;
1290   tGATT_IF gatt_if;
1291   tBT_TRANSPORT transport;
1292 
1293   if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
1294     LOG(ERROR) << __func__ << ": indication/notif for unknown app";
1295     if (op == GATTC_OPTYPE_INDICATION)
1296       GATTC_SendHandleValueConfirm(conn_id, p_data->cid);
1297     return;
1298   }
1299 
1300   tBTA_GATTC_RCB* p_clrcb = bta_gattc_cl_get_regcb(gatt_if);
1301   if (p_clrcb == NULL) {
1302     LOG(ERROR) << __func__ << ": indication/notif for unregistered app";
1303     if (op == GATTC_OPTYPE_INDICATION)
1304       GATTC_SendHandleValueConfirm(conn_id, p_data->cid);
1305     return;
1306   }
1307 
1308   tBTA_GATTC_SERV* p_srcb = bta_gattc_find_srcb(remote_bda);
1309   if (p_srcb == NULL) {
1310     LOG(ERROR) << __func__ << ": indication/notif for unknown device, ignore";
1311     if (op == GATTC_OPTYPE_INDICATION)
1312       GATTC_SendHandleValueConfirm(conn_id, p_data->cid);
1313     return;
1314   }
1315 
1316   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
1317 
1318   notify.handle = handle;
1319   notify.cid = p_data->cid;
1320 
1321   /* if service change indication/notification, don't forward to application */
1322   if (bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, &notify,
1323                                      &p_data->att_value))
1324     return;
1325 
1326   /* if app registered for the notification */
1327   if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, &notify)) {
1328     /* connection not open yet */
1329     if (p_clcb == NULL) {
1330       p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport);
1331 
1332       if (p_clcb == NULL) {
1333         LOG(ERROR) << "No resources";
1334         return;
1335       }
1336 
1337       p_clcb->bta_conn_id = conn_id;
1338       p_clcb->transport = transport;
1339 
1340       bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
1341     }
1342 
1343     if (p_clcb != NULL)
1344       bta_gattc_proc_other_indication(p_clcb, op, p_data, &notify);
1345   }
1346   /* no one intersted and need ack? */
1347   else if (op == GATTC_OPTYPE_INDICATION) {
1348     VLOG(1) << __func__ << " no one interested, ack now";
1349     GATTC_SendHandleValueConfirm(conn_id, p_data->cid);
1350   }
1351 }
1352 
1353 /** client operation complete callback register with BTE GATT */
bta_gattc_cmpl_cback(uint16_t conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)1354 static void bta_gattc_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op,
1355                                  tGATT_STATUS status,
1356                                  tGATT_CL_COMPLETE* p_data) {
1357   VLOG(1) << __func__ << ": conn_id:" << +conn_id << " op:" << +op
1358           << " status:" << +status;
1359 
1360   /* notification and indication processed right away */
1361   if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION) {
1362     bta_gattc_process_indicate(conn_id, op, p_data);
1363     return;
1364   }
1365   /* for all other operation, not expected if w/o connection */
1366   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
1367   if (!p_clcb) {
1368     LOG(ERROR) << __func__ << ": unknown conn_id=" << loghex(conn_id)
1369                << " ignore data";
1370     return;
1371   }
1372 
1373   /* if over BR_EDR, inform PM for mode change */
1374   if (p_clcb->transport == BT_TRANSPORT_BR_EDR) {
1375     bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1376     bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1377   }
1378 
1379   bta_gattc_cmpl_sendmsg(conn_id, op, status, p_data);
1380 }
1381 
1382 /** client operation complete send message */
bta_gattc_cmpl_sendmsg(uint16_t conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)1383 static void bta_gattc_cmpl_sendmsg(uint16_t conn_id, tGATTC_OPTYPE op,
1384                                    tGATT_STATUS status,
1385                                    tGATT_CL_COMPLETE* p_data) {
1386   const size_t len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
1387   tBTA_GATTC_OP_CMPL* p_buf = (tBTA_GATTC_OP_CMPL*)osi_calloc(len);
1388 
1389   p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
1390   p_buf->hdr.layer_specific = conn_id;
1391   p_buf->status = status;
1392   p_buf->op_code = op;
1393 
1394   if (p_data) {
1395     p_buf->p_cmpl = (tGATT_CL_COMPLETE*)(p_buf + 1);
1396     memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
1397   }
1398 
1399   bta_sys_sendmsg(p_buf);
1400 }
1401 
1402 /** congestion callback for BTA GATT client */
bta_gattc_cong_cback(uint16_t conn_id,bool congested)1403 static void bta_gattc_cong_cback(uint16_t conn_id, bool congested) {
1404   tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
1405   if (!p_clcb || !p_clcb->p_rcb->p_cback) return;
1406 
1407   tBTA_GATTC cb_data;
1408   cb_data.congest.conn_id = conn_id;
1409   cb_data.congest.congested = congested;
1410 
1411   (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
1412 }
1413 
bta_gattc_phy_update_cback(tGATT_IF gatt_if,uint16_t conn_id,uint8_t tx_phy,uint8_t rx_phy,tGATT_STATUS status)1414 static void bta_gattc_phy_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
1415                                        uint8_t tx_phy, uint8_t rx_phy,
1416                                        tGATT_STATUS status) {
1417   tBTA_GATTC_RCB* p_clreg = bta_gattc_cl_get_regcb(gatt_if);
1418 
1419   if (!p_clreg || !p_clreg->p_cback) {
1420     LOG(ERROR) << __func__ << ": client_if=" << +gatt_if << " not found";
1421     return;
1422   }
1423 
1424   tBTA_GATTC cb_data;
1425   cb_data.phy_update.conn_id = conn_id;
1426   cb_data.phy_update.server_if = gatt_if;
1427   cb_data.phy_update.tx_phy = tx_phy;
1428   cb_data.phy_update.rx_phy = rx_phy;
1429   cb_data.phy_update.status = status;
1430   (*p_clreg->p_cback)(BTA_GATTC_PHY_UPDATE_EVT, &cb_data);
1431 }
1432 
bta_gattc_conn_update_cback(tGATT_IF gatt_if,uint16_t conn_id,uint16_t interval,uint16_t latency,uint16_t timeout,tGATT_STATUS status)1433 static void bta_gattc_conn_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
1434                                         uint16_t interval, uint16_t latency,
1435                                         uint16_t timeout, tGATT_STATUS status) {
1436   tBTA_GATTC_RCB* p_clreg = bta_gattc_cl_get_regcb(gatt_if);
1437 
1438   if (!p_clreg || !p_clreg->p_cback) {
1439     LOG(ERROR) << __func__ << ": client_if=" << gatt_if << " not found";
1440     return;
1441   }
1442 
1443   tBTA_GATTC cb_data;
1444   cb_data.conn_update.conn_id = conn_id;
1445   cb_data.conn_update.interval = interval;
1446   cb_data.conn_update.latency = latency;
1447   cb_data.conn_update.timeout = timeout;
1448   cb_data.conn_update.status = status;
1449   (*p_clreg->p_cback)(BTA_GATTC_CONN_UPDATE_EVT, &cb_data);
1450 }
1451