1 /******************************************************************************
3 * Copyright (C) 2008-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * this file contains the main ATT functions
23 ******************************************************************************/
25 #include "bt_target.h"
27 #if BLE_INCLUDED == TRUE
29 #include "bt_common.h"
33 #include "btm_ble_int.h"
36 /* Configuration flags. */
37 #define GATT_L2C_CFG_IND_DONE (1<<0)
38 #define GATT_L2C_CFG_CFM_DONE (1<<1)
40 /* minimum GATT MTU size over BR/EDR link
42 #define GATT_MIN_BR_MTU_SIZE 48
44 /********************************************************************************/
45 /* L O C A L F U N C T I O N P R O T O T Y P E S */
46 /********************************************************************************/
47 static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
48 UINT16 reason, tBT_TRANSPORT transport);
49 static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf);
50 static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congest);
52 static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 l2cap_cid,
53 UINT16 psm, UINT8 l2cap_id);
54 static void gatt_l2cif_connect_cfm_cback (UINT16 l2cap_cid, UINT16 result);
55 static void gatt_l2cif_config_ind_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
56 static void gatt_l2cif_config_cfm_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
57 static void gatt_l2cif_disconnect_ind_cback (UINT16 l2cap_cid, BOOLEAN ack_needed);
58 static void gatt_l2cif_disconnect_cfm_cback (UINT16 l2cap_cid, UINT16 result);
59 static void gatt_l2cif_data_ind_cback (UINT16 l2cap_cid, BT_HDR *p_msg);
60 static void gatt_send_conn_cback (tGATT_TCB *p_tcb);
61 static void gatt_l2cif_congest_cback (UINT16 cid, BOOLEAN congested);
63 static const tL2CAP_APPL_INFO dyn_info =
65 gatt_l2cif_connect_ind_cback,
66 gatt_l2cif_connect_cfm_cback,
68 gatt_l2cif_config_ind_cback,
69 gatt_l2cif_config_cfm_cback,
70 gatt_l2cif_disconnect_ind_cback,
71 gatt_l2cif_disconnect_cfm_cback,
73 gatt_l2cif_data_ind_cback,
74 gatt_l2cif_congest_cback,
78 #if GATT_DYNAMIC_MEMORY == FALSE
82 /*******************************************************************************
86 ** Description This function is enable the GATT profile on the device.
87 ** It clears out the control blocks, and registers with L2CAP.
91 *******************************************************************************/
94 tL2CAP_FIXED_CHNL_REG fixed_reg;
96 GATT_TRACE_DEBUG("gatt_init()");
98 memset (&gatt_cb, 0, sizeof(tGATT_CB));
99 memset (&fixed_reg, 0, sizeof(tL2CAP_FIXED_CHNL_REG));
101 #if defined(GATT_INITIAL_TRACE_LEVEL)
102 gatt_cb.trace_level = GATT_INITIAL_TRACE_LEVEL;
104 gatt_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
106 gatt_cb.def_mtu_size = GATT_DEF_BLE_MTU_SIZE;
107 gatt_cb.sign_op_queue = fixed_queue_new(SIZE_MAX);
108 gatt_cb.srv_chg_clt_q = fixed_queue_new(SIZE_MAX);
109 gatt_cb.pending_new_srv_start_q = fixed_queue_new(SIZE_MAX);
110 /* First, register fixed L2CAP channel for ATT over BLE */
111 fixed_reg.fixed_chnl_opts.mode = L2CAP_FCR_BASIC_MODE;
112 fixed_reg.fixed_chnl_opts.max_transmit = 0xFF;
113 fixed_reg.fixed_chnl_opts.rtrans_tout = 2000;
114 fixed_reg.fixed_chnl_opts.mon_tout = 12000;
115 fixed_reg.fixed_chnl_opts.mps = 670;
116 fixed_reg.fixed_chnl_opts.tx_win_sz = 1;
118 fixed_reg.pL2CA_FixedConn_Cb = gatt_le_connect_cback;
119 fixed_reg.pL2CA_FixedData_Cb = gatt_le_data_ind;
120 fixed_reg.pL2CA_FixedCong_Cb = gatt_le_cong_cback; /* congestion callback */
121 fixed_reg.default_idle_tout = 0xffff; /* 0xffff default idle timeout */
123 L2CA_RegisterFixedChannel (L2CAP_ATT_CID, &fixed_reg);
125 /* Now, register with L2CAP for ATT PSM over BR/EDR */
126 if (!L2CA_Register (BT_PSM_ATT, (tL2CAP_APPL_INFO *) &dyn_info))
128 GATT_TRACE_ERROR ("ATT Dynamic Registration failed");
131 BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT, 0, 0);
132 BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT, 0, 0);
134 gatt_cb.hdl_cfg.gatt_start_hdl = GATT_GATT_START_HANDLE;
135 gatt_cb.hdl_cfg.gap_start_hdl = GATT_GAP_START_HANDLE;
136 gatt_cb.hdl_cfg.app_start_hdl = GATT_APP_START_HANDLE;
137 gatt_profile_db_init();
142 /*******************************************************************************
144 ** Function gatt_free
146 ** Description This function frees resources used by the GATT profile.
150 *******************************************************************************/
154 GATT_TRACE_DEBUG("gatt_free()");
156 fixed_queue_free(gatt_cb.sign_op_queue, NULL);
157 gatt_cb.sign_op_queue = NULL;
158 fixed_queue_free(gatt_cb.srv_chg_clt_q, NULL);
159 gatt_cb.srv_chg_clt_q = NULL;
160 fixed_queue_free(gatt_cb.pending_new_srv_start_q, NULL);
161 gatt_cb.pending_new_srv_start_q = NULL;
162 for (i = 0; i < GATT_MAX_PHY_CHANNEL; i++)
164 fixed_queue_free(gatt_cb.tcb[i].pending_enc_clcb, NULL);
165 gatt_cb.tcb[i].pending_enc_clcb = NULL;
167 fixed_queue_free(gatt_cb.tcb[i].pending_ind_q, NULL);
168 gatt_cb.tcb[i].pending_ind_q = NULL;
170 alarm_free(gatt_cb.tcb[i].conf_timer);
171 gatt_cb.tcb[i].conf_timer = NULL;
173 alarm_free(gatt_cb.tcb[i].ind_ack_timer);
174 gatt_cb.tcb[i].ind_ack_timer = NULL;
176 fixed_queue_free(gatt_cb.tcb[i].sr_cmd.multi_rsp_q, NULL);
177 gatt_cb.tcb[i].sr_cmd.multi_rsp_q = NULL;
179 for (i = 0; i < GATT_MAX_SR_PROFILES; i++)
181 gatt_free_hdl_buffer(&gatt_cb.hdl_list[i]);
185 /*******************************************************************************
187 ** Function gatt_connect
189 ** Description This function is called to initiate a connection to a peer device.
191 ** Parameter rem_bda: remote device address to connect to.
193 ** Returns TRUE if connection is started, otherwise return FALSE.
195 *******************************************************************************/
196 BOOLEAN gatt_connect (BD_ADDR rem_bda, tGATT_TCB *p_tcb, tBT_TRANSPORT transport)
198 BOOLEAN gatt_ret = FALSE;
200 if (gatt_get_ch_state(p_tcb) != GATT_CH_OPEN)
201 gatt_set_ch_state(p_tcb, GATT_CH_CONN);
203 if (transport == BT_TRANSPORT_LE)
205 p_tcb->att_lcid = L2CAP_ATT_CID;
206 gatt_ret = L2CA_ConnectFixedChnl (L2CAP_ATT_CID, rem_bda);
210 if ((p_tcb->att_lcid = L2CA_ConnectReq(BT_PSM_ATT, rem_bda)) != 0)
217 /*******************************************************************************
219 ** Function gatt_disconnect
221 ** Description This function is called to disconnect to an ATT device.
223 ** Parameter p_tcb: pointer to the TCB to disconnect.
225 ** Returns TRUE: if connection found and to be disconnected; otherwise
228 *******************************************************************************/
229 BOOLEAN gatt_disconnect (tGATT_TCB *p_tcb)
232 tGATT_CH_STATE ch_state;
234 GATT_TRACE_EVENT ("%s", __func__);
239 if ( (ch_state = gatt_get_ch_state(p_tcb)) != GATT_CH_CLOSING )
241 if (p_tcb->att_lcid == L2CAP_ATT_CID)
243 if (ch_state == GATT_CH_OPEN)
245 /* only LCB exist between remote device and local */
246 ret = L2CA_RemoveFixedChnl (L2CAP_ATT_CID, p_tcb->peer_bda);
250 gatt_set_ch_state(p_tcb, GATT_CH_CLOSING);
251 ret = L2CA_CancelBleConnectReq (p_tcb->peer_bda);
256 if ((ch_state == GATT_CH_OPEN) || (ch_state == GATT_CH_CFG))
257 ret = L2CA_DisconnectReq(p_tcb->att_lcid);
259 GATT_TRACE_DEBUG ("%s gatt_disconnect channel not opened", __func__);
264 GATT_TRACE_DEBUG ("%s already in closing state", __func__);
271 /*******************************************************************************
273 ** Function gatt_update_app_hold_link_status
275 ** Description Update the application use link status
277 ** Returns true if any modifications are made, false otherwise.
279 *******************************************************************************/
280 BOOLEAN gatt_update_app_hold_link_status(tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add)
282 for (int i=0; i<GATT_MAX_APPS; i++) {
283 if (p_tcb->app_hold_link[i] == 0 && is_add) {
284 p_tcb->app_hold_link[i] = gatt_if;
285 GATT_TRACE_DEBUG("%s: added gatt_if=%d idx=%d ", __func__, gatt_if, i);
287 } else if (p_tcb->app_hold_link[i] == gatt_if && !is_add) {
288 p_tcb->app_hold_link[i] = 0;
289 GATT_TRACE_DEBUG("%s: removed gatt_if=%d idx=%d", __func__, gatt_if, i);
294 GATT_TRACE_DEBUG("%s: gatt_if=%d not found; is_add=%d", __func__, gatt_if, is_add);
298 /*******************************************************************************
300 ** Function gatt_update_app_use_link_flag
302 ** Description Update the application use link flag and optional to check the acl link
303 ** if the link is up then set the idle time out accordingly
307 *******************************************************************************/
308 void gatt_update_app_use_link_flag(tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add,
309 BOOLEAN check_acl_link)
311 GATT_TRACE_DEBUG("%s: is_add=%d chk_link=%d", __func__, is_add, check_acl_link);
316 // If we make no modification, i.e. kill app that was never connected to a device,
317 // skip updating the device state.
318 if (!gatt_update_app_hold_link_status(gatt_if, p_tcb, is_add))
321 if (!check_acl_link ||
322 p_tcb->att_lcid != L2CAP_ATT_CID || /* only update link idle timer for fixed channel */
323 (BTM_GetHCIConnHandle(p_tcb->peer_bda, p_tcb->transport) == GATT_INVALID_ACL_HANDLE)) {
328 GATT_TRACE_DEBUG("%s: disable link idle timer", __func__);
329 /* acl link is connected disable the idle timeout */
330 GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT, p_tcb->transport);
332 if (!gatt_num_apps_hold_link(p_tcb)) {
333 /* acl link is connected but no application needs to use the link
334 so set the timeout value to GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP seconds */
335 GATT_TRACE_DEBUG("%s: start link idle timer =%d sec", __func__,
336 GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP);
337 GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP,
343 /*******************************************************************************
345 ** Function gatt_act_connect
347 ** Description GATT connection initiation.
351 *******************************************************************************/
352 BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr,
353 tBT_TRANSPORT transport, BOOLEAN opportunistic)
359 if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, transport)) != NULL)
362 st = gatt_get_ch_state(p_tcb);
364 /* before link down, another app try to open a GATT connection */
365 if(st == GATT_CH_OPEN && gatt_num_apps_hold_link(p_tcb) == 0 &&
366 transport == BT_TRANSPORT_LE )
368 if (!gatt_connect(bd_addr, p_tcb, transport))
371 else if(st == GATT_CH_CLOSING)
373 /* need to complete the closing first */
379 if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport)) != NULL)
381 if (!gatt_connect(bd_addr, p_tcb, transport))
383 GATT_TRACE_ERROR("gatt_connect failed");
384 fixed_queue_free(p_tcb->pending_enc_clcb, NULL);
385 fixed_queue_free(p_tcb->pending_ind_q, NULL);
386 memset(p_tcb, 0, sizeof(tGATT_TCB));
394 GATT_TRACE_ERROR("Max TCB for gatt_if [%d] reached.", p_reg->gatt_if);
401 gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, FALSE);
403 GATT_TRACE_DEBUG("%s: connection is opportunistic, not updating app usage",
410 /*******************************************************************************
412 ** Function gatt_le_connect_cback
414 ** Description This callback function is called by L2CAP to indicate that
415 ** the ATT fixed channel for LE is
416 ** connected (conn = TRUE)/disconnected (conn = FALSE).
418 *******************************************************************************/
419 static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
420 UINT16 reason, tBT_TRANSPORT transport)
423 tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
424 BOOLEAN check_srv_chg = FALSE;
425 tGATTS_SRV_CHG *p_srv_chg_clt=NULL;
427 /* ignore all fixed channel connect/disconnect on BR/EDR link for GATT */
428 if (transport == BT_TRANSPORT_BR_EDR)
431 GATT_TRACE_DEBUG ("GATT ATT protocol channel with BDA: %08x%04x is %s",
432 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
433 (bd_addr[4]<<8)+bd_addr[5], (connected) ? "connected" : "disconnected");
435 if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(bd_addr)) != NULL)
437 check_srv_chg = TRUE;
441 if (btm_sec_is_a_bonded_dev(bd_addr))
442 gatt_add_a_bonded_dev_for_srv_chg(bd_addr);
447 /* do we have a channel initiating a connection? */
450 /* we are initiating connection */
451 if ( gatt_get_ch_state(p_tcb) == GATT_CH_CONN)
454 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
455 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
457 gatt_send_conn_cback(p_tcb);
460 gatt_chk_srv_chg (p_srv_chg_clt);
462 /* this is incoming connection or background connection callback */
466 if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_LE)) != NULL)
468 p_tcb->att_lcid = L2CAP_ATT_CID;
470 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
472 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
474 gatt_send_conn_cback (p_tcb);
477 gatt_chk_srv_chg (p_srv_chg_clt);
482 GATT_TRACE_ERROR("CCB max out, no rsources");
488 gatt_cleanup_upon_disc(bd_addr, reason, transport);
489 GATT_TRACE_DEBUG ("ATT disconnected");
493 /*******************************************************************************
495 ** Function gatt_channel_congestion
497 ** Description This function is called to process the congestion callback
502 *******************************************************************************/
503 static void gatt_channel_congestion(tGATT_TCB *p_tcb, BOOLEAN congested)
506 tGATT_REG *p_reg=NULL;
509 /* if uncongested, check to see if there is any more pending data */
510 if (p_tcb != NULL && congested == FALSE)
512 gatt_cl_send_next_cmd_inq(p_tcb);
514 /* notifying all applications for the connection up event */
515 for (i = 0, p_reg = gatt_cb.cl_rcb ; i < GATT_MAX_APPS; i++, p_reg++)
519 if (p_reg->app_cb.p_congestion_cb)
521 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
522 (*p_reg->app_cb.p_congestion_cb)(conn_id, congested);
528 /*******************************************************************************
530 ** Function gatt_le_cong_cback
532 ** Description This function is called when GATT fixed channel is congested
537 *******************************************************************************/
538 static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congested)
540 tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(remote_bda, BT_TRANSPORT_LE);
542 /* if uncongested, check to see if there is any more pending data */
545 gatt_channel_congestion(p_tcb, congested);
549 /*******************************************************************************
551 ** Function gatt_le_data_ind
553 ** Description This function is called when data is received from L2CAP.
554 ** if we are the originator of the connection, we are the ATT
555 ** client, and the received message is queued up for the client.
557 ** If we are the destination of the connection, we are the ATT
558 ** server, so the message is passed to the server processing
563 *******************************************************************************/
564 static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf)
568 /* Find CCB based on bd addr */
569 if ((p_tcb = gatt_find_tcb_by_addr (bd_addr, BT_TRANSPORT_LE)) != NULL &&
570 gatt_get_ch_state(p_tcb) >= GATT_CH_OPEN)
572 gatt_data_process(p_tcb, p_buf);
580 GATT_TRACE_WARNING ("ATT - Ignored L2CAP data while in state: %d",
581 gatt_get_ch_state(p_tcb));
586 /*******************************************************************************
588 ** Function gatt_l2cif_connect_ind
590 ** Description This function handles an inbound connection indication
591 ** from L2CAP. This is the case where we are acting as a
596 *******************************************************************************/
597 static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
599 /* do we already have a control channel for this peer? */
600 UINT8 result = L2CAP_CONN_OK;
602 tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_BR_EDR);
605 GATT_TRACE_ERROR("Connection indication cid = %d", lcid);
606 /* new connection ? */
610 if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
612 /* no tcb available, reject L2CAP connection */
613 result = L2CAP_CONN_NO_RESOURCES;
616 p_tcb->att_lcid = lcid;
619 else /* existing connection , reject it */
621 result = L2CAP_CONN_NO_RESOURCES;
624 /* Send L2CAP connect rsp */
625 L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
627 /* if result ok, proceed with connection */
628 if (result == L2CAP_CONN_OK)
630 /* transition to configuration state */
631 gatt_set_ch_state(p_tcb, GATT_CH_CFG);
633 /* Send L2CAP config req */
634 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
635 cfg.mtu_present = TRUE;
636 cfg.mtu = GATT_MAX_MTU_SIZE;
638 L2CA_ConfigReq(lcid, &cfg);
642 /*******************************************************************************
644 ** Function gatt_l2c_connect_cfm_cback
646 ** Description This is the L2CAP connect confirm callback function.
651 *******************************************************************************/
652 static void gatt_l2cif_connect_cfm_cback(UINT16 lcid, UINT16 result)
657 /* look up clcb for this channel */
658 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
660 GATT_TRACE_DEBUG("gatt_l2c_connect_cfm_cback result: %d ch_state: %d, lcid:0x%x", result, gatt_get_ch_state(p_tcb), p_tcb->att_lcid);
662 /* if in correct state */
663 if (gatt_get_ch_state(p_tcb) == GATT_CH_CONN)
665 /* if result successful */
666 if (result == L2CAP_CONN_OK)
668 /* set channel state */
669 gatt_set_ch_state(p_tcb, GATT_CH_CFG);
671 /* Send L2CAP config req */
672 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
673 cfg.mtu_present = TRUE;
674 cfg.mtu = GATT_MAX_MTU_SIZE;
675 L2CA_ConfigReq(lcid, &cfg);
677 /* else initiating connection failure */
680 gatt_cleanup_upon_disc(p_tcb->peer_bda, result, GATT_TRANSPORT_BR_EDR);
683 else /* wrong state, disconnect it */
685 if (result == L2CAP_CONN_OK)
687 /* just in case the peer also accepts our connection - Send L2CAP disconnect req */
688 L2CA_DisconnectReq(lcid);
694 /*******************************************************************************
696 ** Function gatt_l2cif_config_cfm_cback
698 ** Description This is the L2CAP config confirm callback function.
703 *******************************************************************************/
704 void gatt_l2cif_config_cfm_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
707 tGATTS_SRV_CHG *p_srv_chg_clt=NULL;
709 /* look up clcb for this channel */
710 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
712 /* if in correct state */
713 if ( gatt_get_ch_state(p_tcb) == GATT_CH_CFG)
715 /* if result successful */
716 if (p_cfg->result == L2CAP_CFG_OK)
719 p_tcb->ch_flags |= GATT_L2C_CFG_CFM_DONE;
721 /* if configuration complete */
722 if (p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE)
724 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
726 if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
728 gatt_chk_srv_chg(p_srv_chg_clt);
732 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
733 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
737 gatt_send_conn_cback(p_tcb);
743 /* Send L2CAP disconnect req */
744 L2CA_DisconnectReq(lcid);
750 /*******************************************************************************
752 ** Function gatt_l2cif_config_ind_cback
754 ** Description This is the L2CAP config indication callback function.
759 *******************************************************************************/
760 void gatt_l2cif_config_ind_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
763 tGATTS_SRV_CHG *p_srv_chg_clt=NULL;
764 /* look up clcb for this channel */
765 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
767 /* GATT uses the smaller of our MTU and peer's MTU */
768 if ( p_cfg->mtu_present &&
769 (p_cfg->mtu >= GATT_MIN_BR_MTU_SIZE && p_cfg->mtu < L2CAP_DEFAULT_MTU))
770 p_tcb->payload_size = p_cfg->mtu;
772 p_tcb->payload_size = L2CAP_DEFAULT_MTU;
774 /* send L2CAP configure response */
775 memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
776 p_cfg->result = L2CAP_CFG_OK;
777 L2CA_ConfigRsp(lcid, p_cfg);
779 /* if first config ind */
780 if ((p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE) == 0)
783 p_tcb->ch_flags |= GATT_L2C_CFG_IND_DONE;
785 /* if configuration complete */
786 if (p_tcb->ch_flags & GATT_L2C_CFG_CFM_DONE)
788 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
789 if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
791 gatt_chk_srv_chg(p_srv_chg_clt);
795 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
796 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
800 gatt_send_conn_cback(p_tcb);
806 /*******************************************************************************
808 ** Function gatt_l2cif_disconnect_ind_cback
810 ** Description This is the L2CAP disconnect indication callback function.
815 *******************************************************************************/
816 void gatt_l2cif_disconnect_ind_cback(UINT16 lcid, BOOLEAN ack_needed)
821 /* look up clcb for this channel */
822 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
826 /* send L2CAP disconnect response */
827 L2CA_DisconnectRsp(lcid);
829 if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL)
831 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
832 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
834 /* if ACL link is still up, no reason is logged, l2cap is disconnect from peer */
835 if ((reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport)) == 0)
836 reason = GATT_CONN_TERMINATE_PEER_USER;
838 /* send disconnect callback */
839 gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
843 /*******************************************************************************
845 ** Function gatt_l2cif_disconnect_cfm_cback
847 ** Description This is the L2CAP disconnect confirm callback function.
852 *******************************************************************************/
853 static void gatt_l2cif_disconnect_cfm_cback(UINT16 lcid, UINT16 result)
859 /* look up clcb for this channel */
860 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
862 /* If the device is not in the service changed client list, add it... */
863 if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL)
865 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
866 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
869 /* send disconnect callback */
870 /* if ACL link is still up, no reason is logged, l2cap is disconnect from peer */
871 if ((reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport)) == 0)
872 reason = GATT_CONN_TERMINATE_LOCAL_HOST;
874 gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
878 /*******************************************************************************
880 ** Function gatt_l2cif_data_ind_cback
882 ** Description This is the L2CAP data indication callback function.
887 *******************************************************************************/
888 static void gatt_l2cif_data_ind_cback(UINT16 lcid, BT_HDR *p_buf)
892 /* look up clcb for this channel */
893 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL &&
894 gatt_get_ch_state(p_tcb) == GATT_CH_OPEN)
896 /* process the data */
897 gatt_data_process(p_tcb, p_buf);
899 else /* prevent buffer leak */
903 /*******************************************************************************
905 ** Function gatt_l2cif_congest_cback
907 ** Description L2CAP congestion callback
911 *******************************************************************************/
912 static void gatt_l2cif_congest_cback (UINT16 lcid, BOOLEAN congested)
914 tGATT_TCB *p_tcb = gatt_find_tcb_by_cid(lcid);
918 gatt_channel_congestion(p_tcb, congested);
922 /*******************************************************************************
924 ** Function gatt_send_conn_cback
926 ** Description Callback used to notify layer above about a connection.
931 *******************************************************************************/
932 static void gatt_send_conn_cback(tGATT_TCB *p_tcb)
936 tGATT_BG_CONN_DEV *p_bg_dev=NULL;
939 p_bg_dev = gatt_find_bg_dev(p_tcb->peer_bda);
941 /* notifying all applications for the connection up event */
942 for (i = 0, p_reg = gatt_cb.cl_rcb ; i < GATT_MAX_APPS; i++, p_reg++)
946 if (p_bg_dev && gatt_is_bg_dev_for_app(p_bg_dev, p_reg->gatt_if))
947 gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, TRUE);
949 if (p_reg->app_cb.p_conn_cb)
951 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
952 (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id,
953 TRUE, 0, p_tcb->transport);
959 if (gatt_num_apps_hold_link(p_tcb) && p_tcb->att_lcid == L2CAP_ATT_CID )
961 /* disable idle timeout if one or more clients are holding the link disable the idle timer */
962 GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT, p_tcb->transport);
966 /*******************************************************************************
968 ** Function gatt_le_data_ind
970 ** Description This function is called when data is received from L2CAP.
971 ** if we are the originator of the connection, we are the ATT
972 ** client, and the received message is queued up for the client.
974 ** If we are the destination of the connection, we are the ATT
975 ** server, so the message is passed to the server processing
980 *******************************************************************************/
981 void gatt_data_process (tGATT_TCB *p_tcb, BT_HDR *p_buf)
983 UINT8 *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
984 UINT8 op_code, pseudo_op_code;
990 msg_len = p_buf->len - 1;
991 STREAM_TO_UINT8(op_code, p);
993 /* remove the two MSBs associated with sign write and write cmd */
994 pseudo_op_code = op_code & (~GATT_WRITE_CMD_MASK);
996 if (pseudo_op_code < GATT_OP_CODE_MAX)
998 if (op_code == GATT_SIGN_CMD_WRITE)
1000 gatt_verify_signature(p_tcb, p_buf);
1004 /* message from client */
1005 if ((op_code % 2) == 0)
1006 gatt_server_handle_client_req (p_tcb, op_code, msg_len, p);
1008 gatt_client_handle_server_rsp (p_tcb, op_code, msg_len, p);
1013 GATT_TRACE_ERROR ("ATT - Rcvd L2CAP data, unknown cmd: 0x%x", op_code);
1018 GATT_TRACE_ERROR ("invalid data length, ignore");
1024 /*******************************************************************************
1026 ** Function gatt_add_a_bonded_dev_for_srv_chg
1028 ** Description Add a bonded dev to the service changed client list
1032 *******************************************************************************/
1033 void gatt_add_a_bonded_dev_for_srv_chg (BD_ADDR bda)
1035 tGATTS_SRV_CHG_REQ req;
1036 tGATTS_SRV_CHG srv_chg_clt;
1038 memcpy(srv_chg_clt.bda, bda, BD_ADDR_LEN);
1039 srv_chg_clt.srv_changed = FALSE;
1040 if (gatt_add_srv_chg_clt(&srv_chg_clt) != NULL)
1042 memcpy(req.srv_chg.bda, bda, BD_ADDR_LEN);
1043 req.srv_chg.srv_changed = FALSE;
1044 if (gatt_cb.cb_info.p_srv_chg_callback)
1045 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_ADD_CLIENT, &req, NULL);
1049 /*******************************************************************************
1051 ** Function gatt_send_srv_chg_ind
1053 ** Description This function is called to send a service chnaged indication to
1054 ** the specified bd address
1058 *******************************************************************************/
1059 void gatt_send_srv_chg_ind (BD_ADDR peer_bda)
1061 UINT8 handle_range[GATT_SIZE_OF_SRV_CHG_HNDL_RANGE];
1062 UINT8 *p = handle_range;
1065 GATT_TRACE_DEBUG("gatt_send_srv_chg_ind");
1067 if (gatt_cb.handle_of_h_r)
1069 if ((conn_id = gatt_profile_find_conn_id_by_bd_addr(peer_bda)) != GATT_INVALID_CONN_ID)
1071 UINT16_TO_STREAM (p, 1);
1072 UINT16_TO_STREAM (p, 0xFFFF);
1073 GATTS_HandleValueIndication (conn_id,
1074 gatt_cb.handle_of_h_r,
1075 GATT_SIZE_OF_SRV_CHG_HNDL_RANGE,
1080 GATT_TRACE_ERROR("Unable to find conn_id for %08x%04x ",
1081 (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
1082 (peer_bda[4]<<8)+peer_bda[5] );
1087 /*******************************************************************************
1089 ** Function gatt_chk_srv_chg
1091 ** Description Check sending service chnaged Indication is required or not
1092 ** if required then send the Indication
1096 *******************************************************************************/
1097 void gatt_chk_srv_chg(tGATTS_SRV_CHG *p_srv_chg_clt)
1099 GATT_TRACE_DEBUG("gatt_chk_srv_chg srv_changed=%d", p_srv_chg_clt->srv_changed );
1101 if (p_srv_chg_clt->srv_changed)
1103 gatt_send_srv_chg_ind(p_srv_chg_clt->bda);
1107 /*******************************************************************************
1109 ** Function gatt_init_srv_chg
1111 ** Description This function is used to initialize the service changed
1116 *******************************************************************************/
1117 void gatt_init_srv_chg (void)
1119 tGATTS_SRV_CHG_REQ req;
1120 tGATTS_SRV_CHG_RSP rsp;
1122 UINT8 num_clients,i;
1123 tGATTS_SRV_CHG srv_chg_clt;
1125 GATT_TRACE_DEBUG("gatt_init_srv_chg");
1126 if (gatt_cb.cb_info.p_srv_chg_callback)
1128 status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_NUM_CLENTS, NULL, &rsp);
1130 if (status && rsp.num_clients)
1132 GATT_TRACE_DEBUG("gatt_init_srv_chg num_srv_chg_clt_clients=%d", rsp.num_clients);
1133 num_clients = rsp.num_clients;
1134 i = 1; /* use one based index */
1135 while ((i <= num_clients) && status)
1137 req.client_read_index = i;
1138 if ((status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_CLENT, &req, &rsp)) == TRUE)
1140 memcpy(&srv_chg_clt, &rsp.srv_chg ,sizeof(tGATTS_SRV_CHG));
1141 if (gatt_add_srv_chg_clt(&srv_chg_clt) == NULL)
1143 GATT_TRACE_ERROR("Unable to add a service change client");
1153 GATT_TRACE_DEBUG("gatt_init_srv_chg callback not registered yet");
1157 /*******************************************************************************
1159 ** Function gatt_proc_srv_chg
1161 ** Description This function is process the service changed request
1165 *******************************************************************************/
1166 void gatt_proc_srv_chg (void)
1168 UINT8 start_idx, found_idx;
1170 BOOLEAN srv_chg_ind_pending=FALSE;
1172 tBT_TRANSPORT transport;
1174 GATT_TRACE_DEBUG ("gatt_proc_srv_chg");
1176 if (gatt_cb.cb_info.p_srv_chg_callback && gatt_cb.handle_of_h_r)
1180 while (gatt_find_the_connected_bda(start_idx, bda, &found_idx, &transport))
1182 p_tcb = &gatt_cb.tcb[found_idx];;
1183 srv_chg_ind_pending = gatt_is_srv_chg_ind_pending(p_tcb);
1185 if (!srv_chg_ind_pending)
1187 gatt_send_srv_chg_ind(bda);
1191 GATT_TRACE_DEBUG ("discard srv chg - already has one in the queue");
1193 start_idx = ++found_idx;
1198 /*******************************************************************************
1200 ** Function gatt_set_ch_state
1202 ** Description This function set the ch_state in tcb
1206 *******************************************************************************/
1207 void gatt_set_ch_state(tGATT_TCB *p_tcb, tGATT_CH_STATE ch_state)
1211 GATT_TRACE_DEBUG ("gatt_set_ch_state: old=%d new=%d", p_tcb->ch_state, ch_state);
1212 p_tcb->ch_state = ch_state;
1216 /*******************************************************************************
1218 ** Function gatt_get_ch_state
1220 ** Description This function get the ch_state in tcb
1224 *******************************************************************************/
1225 tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB *p_tcb)
1227 tGATT_CH_STATE ch_state = GATT_CH_CLOSE;
1230 GATT_TRACE_DEBUG ("gatt_get_ch_state: ch_state=%d", p_tcb->ch_state);
1231 ch_state = p_tcb->ch_state;
1236 #endif /* BLE_INCLUDED */