bta_gatts_act.cc revision f33b6f434f086b20fabe5913016bc423ac975057
1/******************************************************************************
2 *
3 *  Copyright (C) 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 "bt_target.h"
27
28#include <string.h>
29#include "bt_common.h"
30#include "bta_gatts_co.h"
31#include "bta_gatts_int.h"
32#include "bta_sys.h"
33#include "btif/include/btif_debug_conn.h"
34#include "btm_ble_api.h"
35#include "osi/include/osi.h"
36#include "utl.h"
37
38static void bta_gatts_nv_save_cback(bool is_saved,
39                                    tGATTS_HNDL_RANGE* p_hndl_range);
40static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
41                                       tGATTS_SRV_CHG_REQ* p_req,
42                                       tGATTS_SRV_CHG_RSP* p_rsp);
43
44static void bta_gatts_conn_cback(tGATT_IF gatt_if, BD_ADDR bda,
45                                 uint16_t conn_id, bool connected,
46                                 tGATT_DISCONN_REASON reason,
47                                 tGATT_TRANSPORT transport);
48static void bta_gatts_send_request_cback(uint16_t conn_id, uint32_t trans_id,
49                                         tGATTS_REQ_TYPE req_type,
50                                         tGATTS_DATA* p_data);
51static void bta_gatts_cong_cback(uint16_t conn_id, bool congested);
52
53static tGATT_CBACK bta_gatts_cback = {
54    bta_gatts_conn_cback, NULL, NULL, NULL, bta_gatts_send_request_cback, NULL,
55    bta_gatts_cong_cback};
56
57tGATT_APPL_INFO bta_gatts_nv_cback = {bta_gatts_nv_save_cback,
58                                      bta_gatts_nv_srv_chg_cback};
59
60/*******************************************************************************
61 *
62 * Function         bta_gatts_nv_save_cback
63 *
64 * Description      NV save callback function.
65 *
66 * Parameter        is_add: true is to add a handle range; otherwise is to
67 *                          delete.
68 * Returns          none.
69 *
70 ******************************************************************************/
71static void bta_gatts_nv_save_cback(bool is_add,
72                                    tGATTS_HNDL_RANGE* p_hndl_range) {
73  bta_gatts_co_update_handle_range(is_add,
74                                   (tBTA_GATTS_HNDL_RANGE*)p_hndl_range);
75}
76
77/*******************************************************************************
78 *
79 * Function         bta_gatts_nv_srv_chg_cback
80 *
81 * Description      NV save callback function.
82 *
83 * Parameter        is_add: true is to add a handle range; otherwise is to
84 *                          delete.
85 * Returns          none.
86 *
87 ******************************************************************************/
88static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
89                                       tGATTS_SRV_CHG_REQ* p_req,
90                                       tGATTS_SRV_CHG_RSP* p_rsp) {
91  return bta_gatts_co_srv_chg((tBTA_GATTS_SRV_CHG_CMD)cmd,
92                              (tBTA_GATTS_SRV_CHG_REQ*)p_req,
93                              (tBTA_GATTS_SRV_CHG_RSP*)p_rsp);
94}
95
96/*******************************************************************************
97 *
98 * Function         bta_gatts_enable
99 *
100 * Description      enable BTA GATTS module.
101 *
102 * Returns          none.
103 *
104 ******************************************************************************/
105void bta_gatts_enable(tBTA_GATTS_CB* p_cb) {
106  uint8_t index = 0;
107  tBTA_GATTS_HNDL_RANGE handle_range;
108
109  if (p_cb->enabled) {
110    APPL_TRACE_DEBUG("GATTS already enabled.");
111  } else {
112    memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
113
114    p_cb->enabled = true;
115
116    while (bta_gatts_co_load_handle_range(index, &handle_range)) {
117      GATTS_AddHandleRange((tGATTS_HNDL_RANGE*)&handle_range);
118      memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
119      index++;
120    }
121
122    APPL_TRACE_DEBUG("bta_gatts_enable: num of handle range added=%d", index);
123
124    if (!GATTS_NVRegister(&bta_gatts_nv_cback)) {
125      APPL_TRACE_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 ******************************************************************************/
139void 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    APPL_TRACE_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 ******************************************************************************/
163void bta_gatts_register(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
164  tBTA_GATTS cb_data;
165  tBTA_GATT_STATUS status = BTA_GATT_OK;
166  uint8_t i, first_unuse = 0xff;
167
168  if (p_cb->enabled == false) {
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 (bta_gatts_uuid_compare(p_cb->rcb[i].app_uuid,
175                                 p_msg->api_reg.app_uuid)) {
176        APPL_TRACE_ERROR("application already registered.");
177        status = BTA_GATT_DUP_REG;
178        break;
179      }
180    }
181  }
182
183  if (status == BTA_GATT_OK) {
184    for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
185      if (first_unuse == 0xff && !p_cb->rcb[i].in_use) {
186        first_unuse = i;
187        break;
188      }
189    }
190
191    cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF;
192    memcpy(&cb_data.reg_oper.uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
193    if (first_unuse != 0xff) {
194      APPL_TRACE_ERROR("register application first_unuse rcb_idx = %d",
195                       first_unuse);
196
197      p_cb->rcb[first_unuse].in_use = true;
198      p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback;
199      memcpy(&p_cb->rcb[first_unuse].app_uuid, &p_msg->api_reg.app_uuid,
200             sizeof(tBT_UUID));
201      cb_data.reg_oper.server_if = p_cb->rcb[first_unuse].gatt_if =
202          GATT_Register(&p_msg->api_reg.app_uuid, &bta_gatts_cback);
203      if (!p_cb->rcb[first_unuse].gatt_if) {
204        status = BTA_GATT_NO_RESOURCES;
205      } else {
206        tBTA_GATTS_INT_START_IF* p_buf = (tBTA_GATTS_INT_START_IF*)osi_malloc(
207            sizeof(tBTA_GATTS_INT_START_IF));
208        p_buf->hdr.event = BTA_GATTS_INT_START_IF_EVT;
209        p_buf->server_if = p_cb->rcb[first_unuse].gatt_if;
210
211        bta_sys_sendmsg(p_buf);
212      }
213    } else {
214      status = BTA_GATT_NO_RESOURCES;
215    }
216  }
217  cb_data.reg_oper.status = status;
218  if (p_msg->api_reg.p_cback)
219    (*p_msg->api_reg.p_cback)(BTA_GATTS_REG_EVT, &cb_data);
220}
221
222/*******************************************************************************
223 *
224 * Function         bta_gatts_start_if
225 *
226 * Description      start an application interface.
227 *
228 * Returns          none.
229 *
230 ******************************************************************************/
231void bta_gatts_start_if(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
232                        tBTA_GATTS_DATA* p_msg) {
233  if (bta_gatts_find_app_rcb_by_app_if(p_msg->int_start_if.server_if)) {
234    GATT_StartIf(p_msg->int_start_if.server_if);
235  } else {
236    APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",
237                     p_msg->int_start_if.server_if);
238  }
239}
240/*******************************************************************************
241 *
242 * Function         bta_gatts_deregister
243 *
244 * Description      deregister an application.
245 *
246 * Returns          none.
247 *
248 ******************************************************************************/
249void bta_gatts_deregister(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
250  tBTA_GATT_STATUS status = BTA_GATT_ERROR;
251  tBTA_GATTS_CBACK* p_cback = NULL;
252  uint8_t i;
253  tBTA_GATTS cb_data;
254
255  cb_data.reg_oper.server_if = p_msg->api_dereg.server_if;
256  cb_data.reg_oper.status = status;
257
258  for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
259    if (p_cb->rcb[i].in_use &&
260        p_cb->rcb[i].gatt_if == p_msg->api_dereg.server_if) {
261      p_cback = p_cb->rcb[i].p_cback;
262      status = BTA_GATT_OK;
263
264      /* deregister the app */
265      GATT_Deregister(p_cb->rcb[i].gatt_if);
266
267      /* reset cb */
268      memset(&p_cb->rcb[i], 0, sizeof(tBTA_GATTS_RCB));
269      cb_data.reg_oper.status = status;
270      break;
271    }
272  }
273
274  if (p_cback) {
275    (*p_cback)(BTA_GATTS_DEREG_EVT, &cb_data);
276  } else {
277    APPL_TRACE_ERROR("application not registered.");
278  }
279}
280
281/*******************************************************************************
282 *
283 * Function         bta_gatts_delete_service
284 *
285 * Description      action function to delete a service.
286 *
287 * Returns          none.
288 *
289 ******************************************************************************/
290void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB* p_srvc_cb,
291                              tBTA_GATTS_DATA* p_msg) {
292  tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
293  tBTA_GATTS cb_data;
294
295  cb_data.srvc_oper.server_if = p_rcb->gatt_if;
296  // cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
297
298  if (GATTS_DeleteService(p_rcb->gatt_if, &p_srvc_cb->service_uuid,
299                          p_srvc_cb->service_id)) {
300    cb_data.srvc_oper.status = BTA_GATT_OK;
301    memset(p_srvc_cb, 0, sizeof(tBTA_GATTS_SRVC_CB));
302  } else {
303    cb_data.srvc_oper.status = BTA_GATT_ERROR;
304  }
305
306  if (p_rcb->p_cback) (*p_rcb->p_cback)(BTA_GATTS_DELELTE_EVT, &cb_data);
307}
308
309/*******************************************************************************
310 *
311 * Function         bta_gatts_stop_service
312 *
313 * Description      action function to stop a service.
314 *
315 * Returns          none.
316 *
317 ******************************************************************************/
318void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB* p_srvc_cb,
319                            UNUSED_ATTR tBTA_GATTS_DATA* p_msg) {
320  tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
321  tBTA_GATTS cb_data;
322
323  GATTS_StopService(p_srvc_cb->service_id);
324  cb_data.srvc_oper.server_if = p_rcb->gatt_if;
325  cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
326  cb_data.srvc_oper.status = BTA_GATT_OK;
327  APPL_TRACE_ERROR("bta_gatts_stop_service service_id= %d",
328                   p_srvc_cb->service_id);
329
330  if (p_rcb->p_cback) (*p_rcb->p_cback)(BTA_GATTS_STOP_EVT, &cb_data);
331}
332/*******************************************************************************
333 *
334 * Function         bta_gatts_send_rsp
335 *
336 * Description      GATTS send response.
337 *
338 * Returns          none.
339 *
340 ******************************************************************************/
341void bta_gatts_send_rsp(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
342                        tBTA_GATTS_DATA* p_msg) {
343  if (GATTS_SendRsp(p_msg->api_rsp.hdr.layer_specific, p_msg->api_rsp.trans_id,
344                    p_msg->api_rsp.status,
345                    (tGATTS_RSP*)p_msg->api_rsp.p_rsp) != GATT_SUCCESS) {
346    APPL_TRACE_ERROR("Sending response failed");
347  }
348}
349/*******************************************************************************
350 *
351 * Function         bta_gatts_indicate_handle
352 *
353 * Description      GATTS send handle value indication or notification.
354 *
355 * Returns          none.
356 *
357 ******************************************************************************/
358void bta_gatts_indicate_handle(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
359  tBTA_GATTS_SRVC_CB* p_srvc_cb;
360  tBTA_GATTS_RCB* p_rcb = NULL;
361  tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
362  tGATT_IF gatt_if;
363  BD_ADDR remote_bda;
364  tBTA_TRANSPORT transport;
365  tBTA_GATTS cb_data;
366
367  p_srvc_cb =
368      bta_gatts_find_srvc_cb_by_attr_id(p_cb, p_msg->api_indicate.attr_id);
369
370  if (p_srvc_cb) {
371    if (GATT_GetConnectionInfor(p_msg->api_indicate.hdr.layer_specific,
372                                &gatt_if, remote_bda, &transport)) {
373      p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
374
375      if (p_msg->api_indicate.need_confirm)
376
377        status = GATTS_HandleValueIndication(
378            p_msg->api_indicate.hdr.layer_specific, p_msg->api_indicate.attr_id,
379            p_msg->api_indicate.len, p_msg->api_indicate.value);
380      else
381        status = GATTS_HandleValueNotification(
382            p_msg->api_indicate.hdr.layer_specific, p_msg->api_indicate.attr_id,
383            p_msg->api_indicate.len, p_msg->api_indicate.value);
384
385      /* if over BR_EDR, inform PM for mode change */
386      if (transport == BTA_TRANSPORT_BR_EDR) {
387        bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
388        bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
389      }
390    } else {
391      APPL_TRACE_ERROR("Unknown connection ID: %d fail sending notification",
392                       p_msg->api_indicate.hdr.layer_specific);
393    }
394
395    if ((status != GATT_SUCCESS || !p_msg->api_indicate.need_confirm) &&
396        p_rcb && p_cb->rcb[p_srvc_cb->rcb_idx].p_cback) {
397      cb_data.req_data.status = status;
398      cb_data.req_data.conn_id = p_msg->api_indicate.hdr.layer_specific;
399
400      (*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data);
401    }
402  } else {
403    APPL_TRACE_ERROR("Not an registered servce attribute ID: 0x%04x",
404                     p_msg->api_indicate.attr_id);
405  }
406}
407
408/*******************************************************************************
409 *
410 * Function         bta_gatts_open
411 *
412 * Description
413 *
414 * Returns          none.
415 *
416 ******************************************************************************/
417void bta_gatts_open(UNUSED_ATTR tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
418  tBTA_GATTS_RCB* p_rcb = NULL;
419  tBTA_GATT_STATUS status = BTA_GATT_ERROR;
420  uint16_t conn_id;
421
422  p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if);
423  if (p_rcb != NULL) {
424    /* should always get the connection ID */
425    if (GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda,
426                     p_msg->api_open.is_direct, p_msg->api_open.transport,
427                     false)) {
428      status = BTA_GATT_OK;
429
430      if (GATT_GetConnIdIfConnected(p_rcb->gatt_if, p_msg->api_open.remote_bda,
431                                    &conn_id, p_msg->api_open.transport)) {
432        status = BTA_GATT_ALREADY_OPEN;
433      }
434    }
435  } else {
436    APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_open.server_if);
437  }
438
439  if (p_rcb && p_rcb->p_cback)
440    (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS*)&status);
441}
442/*******************************************************************************
443 *
444 * Function         bta_gatts_cancel_open
445 *
446 * Description
447 *
448 * Returns          none.
449 *
450 ******************************************************************************/
451void bta_gatts_cancel_open(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
452                           tBTA_GATTS_DATA* p_msg) {
453  tBTA_GATTS_RCB* p_rcb;
454  tBTA_GATT_STATUS status = BTA_GATT_ERROR;
455
456  p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if);
457  if (p_rcb != NULL) {
458    if (!GATT_CancelConnect(p_rcb->gatt_if, p_msg->api_cancel_open.remote_bda,
459                            p_msg->api_cancel_open.is_direct)) {
460      APPL_TRACE_ERROR("bta_gatts_cancel_open failed for open request");
461    } else {
462      status = BTA_GATT_OK;
463    }
464  } else {
465    APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_cancel_open.server_if);
466  }
467
468  if (p_rcb && p_rcb->p_cback)
469    (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS*)&status);
470}
471/*******************************************************************************
472 *
473 * Function         bta_gatts_close
474 *
475 * Description
476 *
477 * Returns          none.
478 *
479 ******************************************************************************/
480void bta_gatts_close(UNUSED_ATTR tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
481  tBTA_GATTS_RCB* p_rcb;
482  tBTA_GATT_STATUS status = BTA_GATT_ERROR;
483  tGATT_IF gatt_if;
484  BD_ADDR remote_bda;
485  tBTA_GATT_TRANSPORT transport;
486
487  if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda,
488                              &transport)) {
489    if (GATT_Disconnect(p_msg->hdr.layer_specific) != GATT_SUCCESS) {
490      APPL_TRACE_ERROR("bta_gatts_close fail conn_id=%d",
491                       p_msg->hdr.layer_specific);
492    } else {
493      status = BTA_GATT_OK;
494    }
495
496    p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
497
498    if (p_rcb && p_rcb->p_cback) {
499      if (transport == BTA_TRANSPORT_BR_EDR)
500        bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
501
502      (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS*)&status);
503    }
504  } else {
505    APPL_TRACE_ERROR("Unknown connection ID: %d", 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 ******************************************************************************/
518static void bta_gatts_send_request_cback(uint16_t conn_id, uint32_t trans_id,
519                                         tGATTS_REQ_TYPE req_type,
520                                         tGATTS_DATA* p_data) {
521  tBTA_GATTS cb_data;
522  tBTA_GATTS_RCB* p_rcb;
523  tGATT_IF gatt_if;
524  tBTA_GATT_TRANSPORT transport;
525
526  memset(&cb_data, 0, sizeof(tBTA_GATTS));
527
528  if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda,
529                              &transport)) {
530    p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
531
532    APPL_TRACE_DEBUG("%s: conn_id=%d trans_id=%d req_type=%d", __func__,
533                     conn_id, trans_id, req_type);
534
535    if (p_rcb && p_rcb->p_cback) {
536      /* if over BR_EDR, inform PM for mode change */
537      if (transport == BTA_TRANSPORT_BR_EDR) {
538        bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
539        bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
540      }
541
542      cb_data.req_data.conn_id = conn_id;
543      cb_data.req_data.trans_id = trans_id;
544      cb_data.req_data.p_data = (tBTA_GATTS_REQ_DATA*)p_data;
545
546      (*p_rcb->p_cback)(req_type, &cb_data);
547    } else {
548      APPL_TRACE_ERROR("connection request on gatt_if[%d] is not interested",
549                       gatt_if);
550    }
551  } else {
552    APPL_TRACE_ERROR("request received on unknown connectino ID: %d", conn_id);
553  }
554}
555
556/*******************************************************************************
557 *
558 * Function         bta_gatts_conn_cback
559 *
560 * Description      connection callback.
561 *
562 * Returns          none.
563 *
564 ******************************************************************************/
565static void bta_gatts_conn_cback(tGATT_IF gatt_if, BD_ADDR bda,
566                                 uint16_t conn_id, bool connected,
567                                 tGATT_DISCONN_REASON reason,
568                                 tGATT_TRANSPORT transport) {
569  tBTA_GATTS cb_data;
570  uint8_t evt = connected ? BTA_GATTS_CONNECT_EVT : BTA_GATTS_DISCONNECT_EVT;
571  tBTA_GATTS_RCB* p_reg;
572
573  APPL_TRACE_DEBUG(
574      "bta_gatts_conn_cback gatt_if=%d conn_id=%d connected=%d reason = 0x%04d",
575      gatt_if, conn_id, connected, reason);
576  APPL_TRACE_DEBUG("bta_gatts_conn_cback  bda :%02x-%02x-%02x-%02x-%02x-%02x ",
577                   bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
578
579  bt_bdaddr_t bdaddr;
580  bdcpy(bdaddr.address, bda);
581  if (connected)
582    btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
583  else
584    btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
585
586  p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
587
588  if (p_reg && p_reg->p_cback) {
589    /* there is no RM for GATT */
590    if (transport == BTA_TRANSPORT_BR_EDR) {
591      if (connected)
592        bta_sys_conn_open(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
593      else
594        bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
595    }
596
597    cb_data.conn.conn_id = conn_id;
598    cb_data.conn.server_if = gatt_if;
599    cb_data.conn.reason = reason;
600    cb_data.conn.transport = transport;
601    memcpy(cb_data.conn.remote_bda, bda, BD_ADDR_LEN);
602    (*p_reg->p_cback)(evt, &cb_data);
603  } else {
604    APPL_TRACE_ERROR("bta_gatts_conn_cback server_if=%d not found", gatt_if);
605  }
606}
607
608/*******************************************************************************
609 *
610 * Function         bta_gatts_cong_cback
611 *
612 * Description      congestion callback.
613 *
614 * Returns          none.
615 *
616 ******************************************************************************/
617static void bta_gatts_cong_cback(uint16_t conn_id, bool congested) {
618  tBTA_GATTS_RCB* p_rcb;
619  tGATT_IF gatt_if;
620  tBTA_GATT_TRANSPORT transport;
621  tBTA_GATTS cb_data;
622
623  if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda,
624                              &transport)) {
625    p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
626
627    if (p_rcb && p_rcb->p_cback) {
628      cb_data.congest.conn_id = conn_id;
629      cb_data.congest.congested = congested;
630
631      (*p_rcb->p_cback)(BTA_GATTS_CONGEST_EVT, &cb_data);
632    }
633  }
634}
635