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