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