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