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 Server action functions for the state
22 * machine.
23 *
24 ******************************************************************************/
25
26 #include <bluetooth/log.h>
27 #include <com_android_bluetooth_flags.h>
28
29 #include <cstdint>
30
31 #include "bta/gatt/bta_gatts_int.h"
32 #include "bta/include/bta_api.h"
33 #include "btif/include/btif_debug_conn.h"
34 #include "internal_include/bt_target.h"
35 #include "internal_include/bt_trace.h"
36 #include "osi/include/allocator.h"
37 #include "osi/include/osi.h"
38 #include "stack/include/gatt_api.h"
39 #include "types/raw_address.h"
40
41 // TODO(b/369381361) Enfore -Wmissing-prototypes
42 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
43
44 using namespace bluetooth;
45
46 static void bta_gatts_nv_save_cback(bool is_saved, tGATTS_HNDL_RANGE* p_hndl_range);
47 static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ* p_req,
48 tGATTS_SRV_CHG_RSP* p_rsp);
49
50 static void bta_gatts_conn_cback(tGATT_IF gatt_if, const RawAddress& bda, tCONN_ID conn_id,
51 bool connected, tGATT_DISCONN_REASON reason,
52 tBT_TRANSPORT transport);
53 static void bta_gatts_send_request_cback(tCONN_ID conn_id, uint32_t trans_id,
54 tGATTS_REQ_TYPE req_type, tGATTS_DATA* p_data);
55 static void bta_gatts_cong_cback(tCONN_ID conn_id, bool congested);
56 static void bta_gatts_phy_update_cback(tGATT_IF gatt_if, tCONN_ID conn_id, uint8_t tx_phy,
57 uint8_t rx_phy, tGATT_STATUS status);
58 static void bta_gatts_conn_update_cback(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t interval,
59 uint16_t latency, uint16_t timeout, tGATT_STATUS status);
60 static void bta_gatts_subrate_chg_cback(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t subrate_factor,
61 uint16_t latency, uint16_t cont_num, uint16_t timeout,
62 tGATT_STATUS status);
63
64 static tGATT_CBACK bta_gatts_cback = {
65 .p_conn_cb = bta_gatts_conn_cback,
66 .p_cmpl_cb = nullptr,
67 .p_disc_res_cb = nullptr,
68 .p_disc_cmpl_cb = nullptr,
69 .p_req_cb = bta_gatts_send_request_cback,
70 .p_enc_cmpl_cb = nullptr,
71 .p_congestion_cb = bta_gatts_cong_cback,
72 .p_phy_update_cb = bta_gatts_phy_update_cback,
73 .p_conn_update_cb = bta_gatts_conn_update_cback,
74 .p_subrate_chg_cb = bta_gatts_subrate_chg_cback,
75 };
76
77 tGATT_APPL_INFO bta_gatts_nv_cback = {bta_gatts_nv_save_cback, bta_gatts_nv_srv_chg_cback};
78
79 /*******************************************************************************
80 *
81 * Function bta_gatts_nv_save_cback
82 *
83 * Description NV save callback function.
84 *
85 * Parameter is_add: true is to add a handle range; otherwise is to
86 * delete.
87 * Returns none.
88 *
89 ******************************************************************************/
bta_gatts_nv_save_cback(bool,tGATTS_HNDL_RANGE *)90 static void bta_gatts_nv_save_cback(bool /*is_add*/, tGATTS_HNDL_RANGE* /*p_hndl_range*/) {}
91
92 /*******************************************************************************
93 *
94 * Function bta_gatts_nv_srv_chg_cback
95 *
96 * Description NV save callback function.
97 *
98 * Parameter is_add: true is to add a handle range; otherwise is to
99 * delete.
100 * Returns none.
101 *
102 ******************************************************************************/
bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD,tGATTS_SRV_CHG_REQ *,tGATTS_SRV_CHG_RSP *)103 static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD /*cmd*/, tGATTS_SRV_CHG_REQ* /*p_req*/,
104 tGATTS_SRV_CHG_RSP* /*p_rsp*/) {
105 return false;
106 }
107
108 /*******************************************************************************
109 *
110 * Function bta_gatts_enable
111 *
112 * Description enable BTA GATTS module.
113 *
114 * Returns none.
115 *
116 ******************************************************************************/
bta_gatts_enable(tBTA_GATTS_CB * p_cb)117 void bta_gatts_enable(tBTA_GATTS_CB* p_cb) {
118 if (p_cb->enabled) {
119 log::verbose("GATTS already enabled.");
120 } else {
121 memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
122
123 p_cb->enabled = true;
124
125 gatt_load_bonded();
126
127 if (!GATTS_NVRegister(&bta_gatts_nv_cback)) {
128 log::error("BTA GATTS NV register failed.");
129 }
130 }
131 }
132
133 /*******************************************************************************
134 *
135 * Function bta_gatts_api_disable
136 *
137 * Description disable BTA GATTS module.
138 *
139 * Returns none.
140 *
141 ******************************************************************************/
bta_gatts_api_disable(tBTA_GATTS_CB * p_cb)142 void bta_gatts_api_disable(tBTA_GATTS_CB* p_cb) {
143 uint8_t i;
144
145 if (p_cb->enabled) {
146 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
147 if (p_cb->rcb[i].in_use) {
148 GATT_Deregister(p_cb->rcb[i].gatt_if);
149 }
150 }
151 memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
152 } else {
153 log::error("GATTS not enabled");
154 }
155 }
156
157 /*******************************************************************************
158 *
159 * Function bta_gatts_register
160 *
161 * Description register an application.
162 *
163 * Returns none.
164 *
165 ******************************************************************************/
bta_gatts_register(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)166 void bta_gatts_register(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
167 tBTA_GATTS cb_data;
168 tGATT_STATUS status = GATT_SUCCESS;
169 uint8_t i, first_unuse = 0xff;
170
171 if (!p_cb->enabled) {
172 bta_gatts_enable(p_cb);
173 }
174
175 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
176 if (p_cb->rcb[i].in_use) {
177 if (p_cb->rcb[i].app_uuid == p_msg->api_reg.app_uuid) {
178 log::error("application already registered.");
179 status = GATT_DUP_REG;
180 break;
181 }
182 }
183 }
184
185 if (status == GATT_SUCCESS) {
186 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
187 if (first_unuse == 0xff && !p_cb->rcb[i].in_use) {
188 first_unuse = i;
189 break;
190 }
191 }
192
193 cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF;
194 cb_data.reg_oper.uuid = p_msg->api_reg.app_uuid;
195 if (first_unuse != 0xff) {
196 log::info("register application first_unuse rcb_idx={}", first_unuse);
197
198 p_cb->rcb[first_unuse].in_use = true;
199 p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback;
200 p_cb->rcb[first_unuse].app_uuid = p_msg->api_reg.app_uuid;
201 cb_data.reg_oper.server_if = p_cb->rcb[first_unuse].gatt_if = GATT_Register(
202 p_msg->api_reg.app_uuid, "GattServer", &bta_gatts_cback, p_msg->api_reg.eatt_support);
203 if (!p_cb->rcb[first_unuse].gatt_if) {
204 status = GATT_NO_RESOURCES;
205 } else {
206 tBTA_GATTS_INT_START_IF* p_buf =
207 (tBTA_GATTS_INT_START_IF*)osi_malloc(sizeof(tBTA_GATTS_INT_START_IF));
208 p_buf->hdr.event = BTA_GATTS_INT_START_IF_EVT;
209 p_buf->server_if = p_cb->rcb[first_unuse].gatt_if;
210
211 bta_sys_sendmsg(p_buf);
212 }
213 } else {
214 status = GATT_NO_RESOURCES;
215 }
216 }
217 cb_data.reg_oper.status = status;
218 if (p_msg->api_reg.p_cback) {
219 (*p_msg->api_reg.p_cback)(BTA_GATTS_REG_EVT, &cb_data);
220 }
221 }
222
223 /*******************************************************************************
224 *
225 * Function bta_gatts_start_if
226 *
227 * Description start an application interface.
228 *
229 * Returns none.
230 *
231 ******************************************************************************/
bta_gatts_start_if(tBTA_GATTS_CB *,tBTA_GATTS_DATA * p_msg)232 void bta_gatts_start_if(tBTA_GATTS_CB* /* p_cb */, tBTA_GATTS_DATA* p_msg) {
233 if (bta_gatts_find_app_rcb_by_app_if(p_msg->int_start_if.server_if)) {
234 GATT_StartIf(p_msg->int_start_if.server_if);
235 } else {
236 log::error("Unable to start app.: Unknown interface={}", p_msg->int_start_if.server_if);
237 }
238 }
239 /*******************************************************************************
240 *
241 * Function bta_gatts_deregister
242 *
243 * Description deregister an application.
244 *
245 * Returns none.
246 *
247 ******************************************************************************/
bta_gatts_deregister(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)248 void bta_gatts_deregister(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
249 tGATT_STATUS status = GATT_ERROR;
250 tBTA_GATTS_CBACK* p_cback = NULL;
251 uint8_t i;
252 tBTA_GATTS cb_data;
253
254 cb_data.reg_oper.server_if = p_msg->api_dereg.server_if;
255 cb_data.reg_oper.status = status;
256
257 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
258 if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == p_msg->api_dereg.server_if) {
259 p_cback = p_cb->rcb[i].p_cback;
260 status = GATT_SUCCESS;
261
262 /* deregister the app */
263 GATT_Deregister(p_cb->rcb[i].gatt_if);
264
265 /* reset cb */
266 memset(&p_cb->rcb[i], 0, sizeof(tBTA_GATTS_RCB));
267 cb_data.reg_oper.status = status;
268 break;
269 }
270 }
271
272 if (p_cback) {
273 (*p_cback)(BTA_GATTS_DEREG_EVT, &cb_data);
274 } else {
275 log::error("application not registered.");
276 }
277 }
278
279 /*******************************************************************************
280 *
281 * Function bta_gatts_delete_service
282 *
283 * Description action function to delete a service.
284 *
285 * Returns none.
286 *
287 ******************************************************************************/
bta_gatts_delete_service(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA *)288 void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB* p_srvc_cb, tBTA_GATTS_DATA* /*p_msg*/) {
289 tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
290 tBTA_GATTS cb_data;
291
292 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
293 cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
294
295 if (GATTS_DeleteService(p_rcb->gatt_if, &p_srvc_cb->service_uuid, p_srvc_cb->service_id)) {
296 cb_data.srvc_oper.status = GATT_SUCCESS;
297 memset(p_srvc_cb, 0, sizeof(tBTA_GATTS_SRVC_CB));
298 } else {
299 cb_data.srvc_oper.status = GATT_ERROR;
300 }
301
302 if (p_rcb->p_cback) {
303 (*p_rcb->p_cback)(BTA_GATTS_DELETE_EVT, &cb_data);
304 }
305 }
306
307 /*******************************************************************************
308 *
309 * Function bta_gatts_stop_service
310 *
311 * Description action function to stop a service.
312 *
313 * Returns none.
314 *
315 ******************************************************************************/
bta_gatts_stop_service(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA *)316 void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB* p_srvc_cb, tBTA_GATTS_DATA* /* p_msg */) {
317 tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
318 tBTA_GATTS cb_data;
319
320 GATTS_StopService(p_srvc_cb->service_id);
321 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
322 cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
323 cb_data.srvc_oper.status = GATT_SUCCESS;
324 log::error("service_id={}", p_srvc_cb->service_id);
325
326 if (p_rcb->p_cback) {
327 (*p_rcb->p_cback)(BTA_GATTS_STOP_EVT, &cb_data);
328 }
329 }
330 /*******************************************************************************
331 *
332 * Function bta_gatts_send_rsp
333 *
334 * Description GATTS send response.
335 *
336 * Returns none.
337 *
338 ******************************************************************************/
bta_gatts_send_rsp(tBTA_GATTS_CB *,tBTA_GATTS_DATA * p_msg)339 void bta_gatts_send_rsp(tBTA_GATTS_CB* /* p_cb */, tBTA_GATTS_DATA* p_msg) {
340 auto conn_id = static_cast<tCONN_ID>(p_msg->api_rsp.hdr.layer_specific);
341 if (GATTS_SendRsp(conn_id, p_msg->api_rsp.trans_id, p_msg->api_rsp.status,
342 (tGATTS_RSP*)p_msg->api_rsp.p_rsp) != GATT_SUCCESS) {
343 log::error("Sending response failed");
344 }
345 }
346 /*******************************************************************************
347 *
348 * Function bta_gatts_indicate_handle
349 *
350 * Description GATTS send handle value indication or notification.
351 *
352 * Returns none.
353 *
354 ******************************************************************************/
bta_gatts_indicate_handle(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)355 void bta_gatts_indicate_handle(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
356 tBTA_GATTS_SRVC_CB* p_srvc_cb;
357 tBTA_GATTS_RCB* p_rcb = NULL;
358 tGATT_STATUS status = GATT_ILLEGAL_PARAMETER;
359 tGATT_IF gatt_if;
360 RawAddress remote_bda;
361 tBT_TRANSPORT transport;
362 tBTA_GATTS cb_data;
363
364 p_srvc_cb = bta_gatts_find_srvc_cb_by_attr_id(p_cb, p_msg->api_indicate.attr_id);
365
366 if (p_srvc_cb) {
367 auto conn_id = static_cast<tCONN_ID>(p_msg->api_indicate.hdr.layer_specific);
368 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
369 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
370
371 if (p_msg->api_indicate.need_confirm) {
372 status = GATTS_HandleValueIndication(conn_id, p_msg->api_indicate.attr_id,
373 p_msg->api_indicate.len, p_msg->api_indicate.value);
374 } else {
375 status = GATTS_HandleValueNotification(conn_id, p_msg->api_indicate.attr_id,
376 p_msg->api_indicate.len, p_msg->api_indicate.value);
377 }
378
379 /* if over BR_EDR, inform PM for mode change */
380 if (transport == BT_TRANSPORT_BR_EDR) {
381 bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
382 bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
383 }
384 } else {
385 log::error("Unknown connection_id=0x{:x} fail sending notification",
386 p_msg->api_indicate.hdr.layer_specific);
387 }
388
389 if ((status != GATT_SUCCESS || !p_msg->api_indicate.need_confirm) && p_rcb &&
390 p_cb->rcb[p_srvc_cb->rcb_idx].p_cback) {
391 cb_data.req_data.status = status;
392 cb_data.req_data.conn_id = conn_id;
393
394 (*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data);
395 }
396 } else {
397 log::error("Not an registered servce attribute ID: 0x{:x}", p_msg->api_indicate.attr_id);
398 }
399 }
400
401 /*******************************************************************************
402 *
403 * Function bta_gatts_open
404 *
405 * Description
406 *
407 * Returns none.
408 *
409 ******************************************************************************/
bta_gatts_open(tBTA_GATTS_CB *,tBTA_GATTS_DATA * p_msg)410 void bta_gatts_open(tBTA_GATTS_CB* /* p_cb */, tBTA_GATTS_DATA* p_msg) {
411 tBTA_GATTS_RCB* p_rcb = NULL;
412 tGATT_STATUS status = GATT_ERROR;
413 tCONN_ID conn_id;
414
415 p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if);
416 if (p_rcb != NULL) {
417 /* should always get the connection ID */
418 bool success = false;
419 if (com::android::bluetooth::flags::ble_gatt_server_use_address_type_in_connection()) {
420 success = GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda,
421 p_msg->api_open.remote_addr_type, p_msg->api_open.connection_type,
422 p_msg->api_open.transport, false, LE_PHY_1M, 0);
423 } else {
424 success = GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda,
425 p_msg->api_open.connection_type, p_msg->api_open.transport, false);
426 }
427
428 if (success) {
429 status = GATT_SUCCESS;
430 if (GATT_GetConnIdIfConnected(p_rcb->gatt_if, p_msg->api_open.remote_bda, &conn_id,
431 p_msg->api_open.transport)) {
432 status = GATT_ALREADY_OPEN;
433 }
434 }
435 } else {
436 log::error("Inavlid server_if={}", p_msg->api_open.server_if);
437 }
438
439 if (p_rcb && p_rcb->p_cback) {
440 tBTA_GATTS bta_gatts;
441 bta_gatts.status = status;
442 (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, &bta_gatts);
443 }
444 }
445 /*******************************************************************************
446 *
447 * Function bta_gatts_cancel_open
448 *
449 * Description
450 *
451 * Returns none.
452 *
453 ******************************************************************************/
bta_gatts_cancel_open(tBTA_GATTS_CB *,tBTA_GATTS_DATA * p_msg)454 void bta_gatts_cancel_open(tBTA_GATTS_CB* /* p_cb */, tBTA_GATTS_DATA* p_msg) {
455 tBTA_GATTS_RCB* p_rcb;
456 tGATT_STATUS status = GATT_ERROR;
457
458 p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if);
459 if (p_rcb != NULL) {
460 if (!GATT_CancelConnect(p_rcb->gatt_if, p_msg->api_cancel_open.remote_bda,
461 p_msg->api_cancel_open.is_direct)) {
462 log::error("failed for open request");
463 } else {
464 status = GATT_SUCCESS;
465 }
466 } else {
467 log::error("Inavlid server_if={}", p_msg->api_cancel_open.server_if);
468 }
469
470 if (p_rcb && p_rcb->p_cback) {
471 tBTA_GATTS bta_gatts;
472 bta_gatts.status = status;
473 (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, &bta_gatts);
474 }
475 }
476 /*******************************************************************************
477 *
478 * Function bta_gatts_close
479 *
480 * Description
481 *
482 * Returns none.
483 *
484 ******************************************************************************/
bta_gatts_close(tBTA_GATTS_CB *,tBTA_GATTS_DATA * p_msg)485 void bta_gatts_close(tBTA_GATTS_CB* /* p_cb */, tBTA_GATTS_DATA* p_msg) {
486 tBTA_GATTS_RCB* p_rcb;
487 tGATT_STATUS status = GATT_ERROR;
488 tGATT_IF gatt_if;
489 RawAddress remote_bda;
490 tBT_TRANSPORT transport;
491 tCONN_ID conn_id = static_cast<tCONN_ID>(p_msg->hdr.layer_specific);
492
493 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
494 log::debug("Disconnecting gatt_if={}, remote_bda={}, transport={}", gatt_if, remote_bda,
495 transport);
496 status = GATT_Disconnect(conn_id);
497 if (status != GATT_SUCCESS) {
498 log::error("fail conn_id={}", p_msg->hdr.layer_specific);
499 status = GATT_ERROR;
500 }
501
502 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
503
504 if (p_rcb && p_rcb->p_cback) {
505 if (transport == BT_TRANSPORT_BR_EDR) {
506 bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
507 }
508
509 tBTA_GATTS bta_gatts;
510 bta_gatts.status = status;
511 (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, &bta_gatts);
512 }
513 } else {
514 log::error("Unknown connection_id=0x{:x}", p_msg->hdr.layer_specific);
515 }
516 }
517
518 /*******************************************************************************
519 *
520 * Function bta_gatts_request_cback
521 *
522 * Description GATTS attribute request callback.
523 *
524 * Returns none.
525 *
526 ******************************************************************************/
bta_gatts_send_request_cback(tCONN_ID conn_id,uint32_t trans_id,tGATTS_REQ_TYPE req_type,tGATTS_DATA * p_data)527 static void bta_gatts_send_request_cback(tCONN_ID conn_id, uint32_t trans_id,
528 tGATTS_REQ_TYPE req_type, tGATTS_DATA* p_data) {
529 tBTA_GATTS cb_data;
530 tBTA_GATTS_RCB* p_rcb;
531 tGATT_IF gatt_if;
532 tBT_TRANSPORT transport;
533
534 memset(&cb_data, 0, sizeof(tBTA_GATTS));
535
536 if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda, &transport)) {
537 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
538
539 log::verbose("conn_id=0x{:x}, trans_id={}, req_type={}", conn_id, trans_id, req_type);
540
541 if (p_rcb && p_rcb->p_cback) {
542 /* if over BR_EDR, inform PM for mode change */
543 if (transport == BT_TRANSPORT_BR_EDR) {
544 bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
545 bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
546 }
547
548 cb_data.req_data.conn_id = conn_id;
549 cb_data.req_data.trans_id = trans_id;
550 cb_data.req_data.p_data = (tGATTS_DATA*)p_data;
551
552 (*p_rcb->p_cback)(req_type, &cb_data);
553 } else {
554 log::error("connection request on gatt_if={} is not interested", gatt_if);
555 }
556 } else {
557 log::error("request received on unknown conn_id=0x{:x}", conn_id);
558 }
559 }
560
561 /*******************************************************************************
562 *
563 * Function bta_gatts_conn_cback
564 *
565 * Description connection callback.
566 *
567 * Returns none.
568 *
569 ******************************************************************************/
bta_gatts_conn_cback(tGATT_IF gatt_if,const RawAddress & bdaddr,tCONN_ID conn_id,bool connected,tGATT_DISCONN_REASON,tBT_TRANSPORT transport)570 static void bta_gatts_conn_cback(tGATT_IF gatt_if, const RawAddress& bdaddr, tCONN_ID conn_id,
571 bool connected, tGATT_DISCONN_REASON, tBT_TRANSPORT transport) {
572 tBTA_GATTS cb_data;
573 uint8_t evt = connected ? BTA_GATTS_CONNECT_EVT : BTA_GATTS_DISCONNECT_EVT;
574 tBTA_GATTS_RCB* p_reg;
575
576 log::verbose("bda={} gatt_if= {}, conn_id=0x{:x} connected={}", bdaddr, gatt_if, conn_id,
577 connected);
578
579 if (connected) {
580 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_OK);
581 } else {
582 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, GATT_CONN_OK);
583 }
584
585 p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
586
587 if (p_reg && p_reg->p_cback) {
588 /* there is no RM for GATT */
589 if (transport == BT_TRANSPORT_BR_EDR) {
590 if (connected) {
591 bta_sys_conn_open(BTA_ID_GATTS, BTA_ALL_APP_ID, bdaddr);
592 } else {
593 bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, bdaddr);
594 }
595 }
596
597 cb_data.conn.conn_id = conn_id;
598 cb_data.conn.server_if = gatt_if;
599 cb_data.conn.transport = transport;
600 cb_data.conn.remote_bda = bdaddr;
601 (*p_reg->p_cback)(evt, &cb_data);
602 } else {
603 log::error("server_if={} not found", gatt_if);
604 }
605 }
606
bta_gatts_phy_update_cback(tGATT_IF gatt_if,tCONN_ID conn_id,uint8_t tx_phy,uint8_t rx_phy,tGATT_STATUS status)607 static void bta_gatts_phy_update_cback(tGATT_IF gatt_if, tCONN_ID conn_id, uint8_t tx_phy,
608 uint8_t rx_phy, tGATT_STATUS status) {
609 tBTA_GATTS_RCB* p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
610 if (!p_reg || !p_reg->p_cback) {
611 log::error("server_if={} not found", gatt_if);
612 return;
613 }
614
615 tBTA_GATTS cb_data;
616 cb_data.phy_update.conn_id = conn_id;
617 cb_data.phy_update.server_if = gatt_if;
618 cb_data.phy_update.tx_phy = tx_phy;
619 cb_data.phy_update.rx_phy = rx_phy;
620 cb_data.phy_update.status = status;
621 (*p_reg->p_cback)(BTA_GATTS_PHY_UPDATE_EVT, &cb_data);
622 }
623
bta_gatts_conn_update_cback(tGATT_IF gatt_if,tCONN_ID conn_id,uint16_t interval,uint16_t latency,uint16_t timeout,tGATT_STATUS status)624 static void bta_gatts_conn_update_cback(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t interval,
625 uint16_t latency, uint16_t timeout, tGATT_STATUS status) {
626 tBTA_GATTS_RCB* p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
627 if (!p_reg || !p_reg->p_cback) {
628 log::error("server_if={} not found", gatt_if);
629 return;
630 }
631
632 tBTA_GATTS cb_data;
633 cb_data.conn_update.conn_id = conn_id;
634 cb_data.conn_update.server_if = gatt_if;
635 cb_data.conn_update.interval = interval;
636 cb_data.conn_update.latency = latency;
637 cb_data.conn_update.timeout = timeout;
638 cb_data.conn_update.status = status;
639 (*p_reg->p_cback)(BTA_GATTS_CONN_UPDATE_EVT, &cb_data);
640 }
641
bta_gatts_subrate_chg_cback(tGATT_IF gatt_if,tCONN_ID conn_id,uint16_t subrate_factor,uint16_t latency,uint16_t cont_num,uint16_t timeout,tGATT_STATUS status)642 static void bta_gatts_subrate_chg_cback(tGATT_IF gatt_if, tCONN_ID conn_id, uint16_t subrate_factor,
643 uint16_t latency, uint16_t cont_num, uint16_t timeout,
644 tGATT_STATUS status) {
645 tBTA_GATTS_RCB* p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
646 if (!p_reg || !p_reg->p_cback) {
647 log::error("server_if={} not found", gatt_if);
648 return;
649 }
650
651 tBTA_GATTS cb_data;
652 cb_data.subrate_chg.conn_id = conn_id;
653 cb_data.subrate_chg.server_if = gatt_if;
654 cb_data.subrate_chg.subrate_factor = subrate_factor;
655 cb_data.subrate_chg.latency = latency;
656 cb_data.subrate_chg.cont_num = cont_num;
657 cb_data.subrate_chg.timeout = timeout;
658 cb_data.subrate_chg.status = status;
659 (*p_reg->p_cback)(BTA_GATTS_SUBRATE_CHG_EVT, &cb_data);
660 }
661
662 /*******************************************************************************
663 *
664 * Function bta_gatts_cong_cback
665 *
666 * Description congestion callback.
667 *
668 * Returns none.
669 *
670 ******************************************************************************/
bta_gatts_cong_cback(tCONN_ID conn_id,bool congested)671 static void bta_gatts_cong_cback(tCONN_ID conn_id, bool congested) {
672 tBTA_GATTS_RCB* p_rcb;
673 tGATT_IF gatt_if;
674 tBT_TRANSPORT transport;
675 tBTA_GATTS cb_data;
676
677 if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda, &transport)) {
678 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
679
680 if (p_rcb && p_rcb->p_cback) {
681 cb_data.congest.conn_id = conn_id;
682 cb_data.congest.congested = congested;
683
684 (*p_rcb->p_cback)(BTA_GATTS_CONGEST_EVT, &cb_data);
685 }
686 }
687 }
688