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