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