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