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