blob: bcc552fd9e88d20a7c925ff74066c857d558c7d2 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2008-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 main ATT functions
22 *
23 ******************************************************************************/
24
25#include "bt_target.h"
26
27#if BLE_INCLUDED == TRUE
28
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070029#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include "gatt_int.h"
31#include "l2c_api.h"
32#include "btm_int.h"
33#include "btm_ble_int.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080034#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
36/* Configuration flags. */
37#define GATT_L2C_CFG_IND_DONE (1<<0)
38#define GATT_L2C_CFG_CFM_DONE (1<<1)
39
Andre Eisenbach6975b4d2013-08-05 16:55:38 -070040/* minimum GATT MTU size over BR/EDR link
41*/
42#define GATT_MIN_BR_MTU_SIZE 48
43
The Android Open Source Project5738f832012-12-12 16:00:35 -080044/********************************************************************************/
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/********************************************************************************/
Kim Schulz8372aa52015-03-25 10:39:40 +010047static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
48 UINT16 reason, tBT_TRANSPORT transport);
49static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf);
Andre Eisenbach17b04bd2014-03-28 14:54:22 -070050static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congest);
The Android Open Source Project5738f832012-12-12 16:00:35 -080051
Kim Schulz8372aa52015-03-25 10:39:40 +010052static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 l2cap_cid,
53 UINT16 psm, UINT8 l2cap_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -080054static void gatt_l2cif_connect_cfm_cback (UINT16 l2cap_cid, UINT16 result);
55static void gatt_l2cif_config_ind_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
56static void gatt_l2cif_config_cfm_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
57static void gatt_l2cif_disconnect_ind_cback (UINT16 l2cap_cid, BOOLEAN ack_needed);
58static void gatt_l2cif_disconnect_cfm_cback (UINT16 l2cap_cid, UINT16 result);
59static void gatt_l2cif_data_ind_cback (UINT16 l2cap_cid, BT_HDR *p_msg);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080060static void gatt_send_conn_cback (tGATT_TCB *p_tcb);
Andre Eisenbach17b04bd2014-03-28 14:54:22 -070061static void gatt_l2cif_congest_cback (UINT16 cid, BOOLEAN congested);
The Android Open Source Project5738f832012-12-12 16:00:35 -080062
63static const tL2CAP_APPL_INFO dyn_info =
64{
65 gatt_l2cif_connect_ind_cback,
66 gatt_l2cif_connect_cfm_cback,
67 NULL,
68 gatt_l2cif_config_ind_cback,
69 gatt_l2cif_config_cfm_cback,
70 gatt_l2cif_disconnect_ind_cback,
71 gatt_l2cif_disconnect_cfm_cback,
72 NULL,
73 gatt_l2cif_data_ind_cback,
Andre Eisenbach17b04bd2014-03-28 14:54:22 -070074 gatt_l2cif_congest_cback,
The Android Open Source Project5738f832012-12-12 16:00:35 -080075 NULL
76} ;
77
78#if GATT_DYNAMIC_MEMORY == FALSE
79tGATT_CB gatt_cb;
80#endif
81
82/*******************************************************************************
83**
84** Function gatt_init
85**
86** Description This function is enable the GATT profile on the device.
87** It clears out the control blocks, and registers with L2CAP.
88**
89** Returns void
90**
91*******************************************************************************/
92void gatt_init (void)
93{
94 tL2CAP_FIXED_CHNL_REG fixed_reg;
95
Sharvil Nanavatib44cc592014-05-04 10:03:35 -070096 GATT_TRACE_DEBUG("gatt_init()");
The Android Open Source Project5738f832012-12-12 16:00:35 -080097
98 memset (&gatt_cb, 0, sizeof(tGATT_CB));
Satya Calloji444a8da2015-03-06 10:38:22 -080099 memset (&fixed_reg, 0, sizeof(tL2CAP_FIXED_CHNL_REG));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100
101#if defined(GATT_INITIAL_TRACE_LEVEL)
102 gatt_cb.trace_level = GATT_INITIAL_TRACE_LEVEL;
103#else
104 gatt_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
105#endif
106 gatt_cb.def_mtu_size = GATT_DEF_BLE_MTU_SIZE;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700107 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);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800110 /* 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;
117
118 fixed_reg.pL2CA_FixedConn_Cb = gatt_le_connect_cback;
119 fixed_reg.pL2CA_FixedData_Cb = gatt_le_data_ind;
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700120 fixed_reg.pL2CA_FixedCong_Cb = gatt_le_cong_cback; /* congestion callback */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121 fixed_reg.default_idle_tout = 0xffff; /* 0xffff default idle timeout */
122
123 L2CA_RegisterFixedChannel (L2CAP_ATT_CID, &fixed_reg);
124
125 /* Now, register with L2CAP for ATT PSM over BR/EDR */
126 if (!L2CA_Register (BT_PSM_ATT, (tL2CAP_APPL_INFO *) &dyn_info))
127 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700128 GATT_TRACE_ERROR ("ATT Dynamic Registration failed");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129 }
130
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);
133
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();
138
139}
140
141
Chris Manton49ada1e2014-02-21 12:36:18 -0800142/*******************************************************************************
143**
144** Function gatt_free
145**
146** Description This function frees resources used by the GATT profile.
147**
148** Returns void
149**
150*******************************************************************************/
151void gatt_free(void)
152{
153 int i;
Andre Eisenbach80f048b2015-02-17 16:53:48 -0800154 GATT_TRACE_DEBUG("gatt_free()");
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700155
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++)
163 {
164 fixed_queue_free(gatt_cb.tcb[i].pending_enc_clcb, NULL);
165 gatt_cb.tcb[i].pending_enc_clcb = NULL;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800166
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700167 fixed_queue_free(gatt_cb.tcb[i].pending_ind_q, NULL);
168 gatt_cb.tcb[i].pending_ind_q = NULL;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800169
170 alarm_free(gatt_cb.tcb[i].conf_timer);
171 gatt_cb.tcb[i].conf_timer = NULL;
172
173 alarm_free(gatt_cb.tcb[i].ind_ack_timer);
174 gatt_cb.tcb[i].ind_ack_timer = NULL;
175
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700176 fixed_queue_free(gatt_cb.tcb[i].sr_cmd.multi_rsp_q, NULL);
177 gatt_cb.tcb[i].sr_cmd.multi_rsp_q = NULL;
178 }
Chris Manton49ada1e2014-02-21 12:36:18 -0800179 for (i = 0; i < GATT_MAX_SR_PROFILES; i++)
180 {
181 gatt_free_hdl_buffer(&gatt_cb.hdl_list[i]);
182 }
183}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800184
185/*******************************************************************************
186**
187** Function gatt_connect
188**
189** Description This function is called to initiate a connection to a peer device.
190**
191** Parameter rem_bda: remote device address to connect to.
192**
193** Returns TRUE if connection is started, otherwise return FALSE.
194**
195*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700196BOOLEAN gatt_connect (BD_ADDR rem_bda, tGATT_TCB *p_tcb, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800197{
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700198 BOOLEAN gatt_ret = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800200 if (gatt_get_ch_state(p_tcb) != GATT_CH_OPEN)
201 gatt_set_ch_state(p_tcb, GATT_CH_CONN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800202
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700203 if (transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800204 {
205 p_tcb->att_lcid = L2CAP_ATT_CID;
206 gatt_ret = L2CA_ConnectFixedChnl (L2CAP_ATT_CID, rem_bda);
207 }
208 else
209 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700210 if ((p_tcb->att_lcid = L2CA_ConnectReq(BT_PSM_ATT, rem_bda)) != 0)
211 gatt_ret = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212 }
213
214 return gatt_ret;
215}
216
217/*******************************************************************************
218**
219** Function gatt_disconnect
220**
221** Description This function is called to disconnect to an ATT device.
222**
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700223** Parameter p_tcb: pointer to the TCB to disconnect.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224**
225** Returns TRUE: if connection found and to be disconnected; otherwise
226** return FALSE.
227**
228*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700229BOOLEAN gatt_disconnect (tGATT_TCB *p_tcb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231 BOOLEAN ret = FALSE;
232 tGATT_CH_STATE ch_state;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700233 GATT_TRACE_DEBUG ("gatt_disconnect ");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234
235 if (p_tcb != NULL)
236 {
237 ret = TRUE;
238 if ( (ch_state = gatt_get_ch_state(p_tcb)) != GATT_CH_CLOSING )
239 {
240 if (p_tcb->att_lcid == L2CAP_ATT_CID)
241 {
242 if (ch_state == GATT_CH_OPEN)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800243 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244 /* only LCB exist between remote device and local */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700245 ret = L2CA_RemoveFixedChnl (L2CAP_ATT_CID, p_tcb->peer_bda);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800246 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800247 else
248 {
249 gatt_set_ch_state(p_tcb, GATT_CH_CLOSING);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700250 ret = L2CA_CancelBleConnectReq (p_tcb->peer_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800251 }
252 }
253 else
254 {
255 ret = L2CA_DisconnectReq(p_tcb->att_lcid);
256 }
257 }
258 else
259 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700260 GATT_TRACE_DEBUG ("gatt_disconnect already in closing state");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800261 }
262 }
263
264 return ret;
265}
266
267/*******************************************************************************
268**
269** Function gatt_update_app_hold_link_status
270**
271** Description Update the application use link status
272**
273** Returns void.
274**
275*******************************************************************************/
276void gatt_update_app_hold_link_status (tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add)
277{
278 UINT8 i;
279 BOOLEAN found=FALSE;
280
281 if (p_tcb == NULL)
282 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700283 GATT_TRACE_ERROR("gatt_update_app_hold_link_status p_tcb=NULL");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284 return;
285 }
286
287
288 for (i=0; i<GATT_MAX_APPS; i++)
289 {
290 if (p_tcb->app_hold_link[i] == gatt_if)
291 {
292 found = TRUE;
293 if (!is_add)
294 {
295 p_tcb->app_hold_link[i] = 0;
296 break;
297 }
298 }
299 }
300
301 if (!found && is_add)
302 {
303 for (i=0; i<GATT_MAX_APPS; i++)
304 {
305 if (p_tcb->app_hold_link[i] == 0)
306 {
307 p_tcb->app_hold_link[i] = gatt_if;
308 found = TRUE;
309 break;
310 }
311 }
312 }
313
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700314 GATT_TRACE_DEBUG("gatt_update_app_hold_link_status found=%d[1-found] idx=%d gatt_if=%d is_add=%d", found, i, gatt_if, is_add);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315
316}
317
318/*******************************************************************************
319**
320** Function gatt_update_app_use_link_flag
321**
322** Description Update the application use link flag and optional to check the acl link
323** if the link is up then set the idle time out accordingly
324**
325** Returns void.
326**
327*******************************************************************************/
328void gatt_update_app_use_link_flag (tGATT_IF gatt_if, tGATT_TCB *p_tcb, BOOLEAN is_add, BOOLEAN check_acl_link)
329{
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700330 GATT_TRACE_DEBUG("gatt_update_app_use_link_flag is_add=%d chk_link=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800331 is_add, check_acl_link);
332
333 gatt_update_app_hold_link_status(gatt_if, p_tcb, is_add);
334
335 if (check_acl_link &&
336 p_tcb &&
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700337 p_tcb->att_lcid == L2CAP_ATT_CID && /* only update link idle timer for fixed channel */
338 (BTM_GetHCIConnHandle(p_tcb->peer_bda, p_tcb->transport) != GATT_INVALID_ACL_HANDLE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800339 {
340 if (is_add)
341 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700342 GATT_TRACE_DEBUG("GATT disables link idle timer");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800343 /* acl link is connected disable the idle timeout */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700344 GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT, p_tcb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800345 }
346 else
347 {
348 if (!gatt_num_apps_hold_link(p_tcb))
349 {
350 /* acl link is connected but no application needs to use the link
351 so set the timeout value to GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP seconds */
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700352 GATT_TRACE_DEBUG("GATT starts link idle timer =%d sec", GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700353 GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP, p_tcb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800354 }
355
356 }
357 }
358}
359
360/*******************************************************************************
361**
362** Function gatt_act_connect
363**
364** Description GATT connection initiation.
365**
366** Returns void.
367**
368*******************************************************************************/
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700369BOOLEAN gatt_act_connect (tGATT_REG *p_reg, BD_ADDR bd_addr, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800370{
371 BOOLEAN ret = FALSE;
372 tGATT_TCB *p_tcb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800373 UINT8 st;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800374
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700375 if ((p_tcb = gatt_find_tcb_by_addr(bd_addr, transport)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376 {
377 ret = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800378 st = gatt_get_ch_state(p_tcb);
379
380 /* before link down, another app try to open a GATT connection */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700381 if(st == GATT_CH_OPEN && gatt_num_apps_hold_link(p_tcb) == 0 &&
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700382 transport == BT_TRANSPORT_LE )
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800383 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700384 if (!gatt_connect(bd_addr, p_tcb, transport))
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800385 ret = FALSE;
386 }
387 else if(st == GATT_CH_CLOSING)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800388 {
389 /* need to complete the closing first */
390 ret = FALSE;
391 }
392 }
393 else
394 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700395 if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700397 if (!gatt_connect(bd_addr, p_tcb, transport))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700399 GATT_TRACE_ERROR("gatt_connect failed");
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700400 fixed_queue_free(p_tcb->pending_enc_clcb, NULL);
401 fixed_queue_free(p_tcb->pending_ind_q, NULL);
402 fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800403 memset(p_tcb, 0, sizeof(tGATT_TCB));
404 }
405 else
406 ret = TRUE;
407 }
408 else
409 {
410 ret = 0;
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700411 GATT_TRACE_ERROR("Max TCB for gatt_if [%d] reached.", p_reg->gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800412 }
413 }
414
415 if (ret)
416 {
417 gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, FALSE);
418 }
419
420 return ret;
421}
422
423/*******************************************************************************
424**
425** Function gatt_le_connect_cback
426**
427** Description This callback function is called by L2CAP to indicate that
428** the ATT fixed channel for LE is
429** connected (conn = TRUE)/disconnected (conn = FALSE).
430**
431*******************************************************************************/
Kim Schulz8372aa52015-03-25 10:39:40 +0100432static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700433 UINT16 reason, tBT_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434{
435
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700436 tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800437 BOOLEAN check_srv_chg = FALSE;
438 tGATTS_SRV_CHG *p_srv_chg_clt=NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800439
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700440 /* ignore all fixed channel connect/disconnect on BR/EDR link for GATT */
441 if (transport == BT_TRANSPORT_BR_EDR)
442 return;
443
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700444 GATT_TRACE_DEBUG ("GATT ATT protocol channel with BDA: %08x%04x is %s",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800445 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
446 (bd_addr[4]<<8)+bd_addr[5], (connected) ? "connected" : "disconnected");
447
The Android Open Source Project5738f832012-12-12 16:00:35 -0800448 if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(bd_addr)) != NULL)
449 {
450 check_srv_chg = TRUE;
451 }
452 else
453 {
454 if (btm_sec_is_a_bonded_dev(bd_addr))
455 gatt_add_a_bonded_dev_for_srv_chg(bd_addr);
456 }
457
458 if (connected)
459 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800460 /* do we have a channel initiating a connection? */
461 if (p_tcb)
462 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463 /* we are initiating connection */
464 if ( gatt_get_ch_state(p_tcb) == GATT_CH_CONN)
465 {
466 /* send callback */
467 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
468 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
469
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800470 gatt_send_conn_cback(p_tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471 }
Mudumba Ananthf8aaa622014-07-03 23:56:42 -0700472 if (check_srv_chg)
473 gatt_chk_srv_chg (p_srv_chg_clt);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800474 }
475 /* this is incoming connection or background connection callback */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700476
The Android Open Source Project5738f832012-12-12 16:00:35 -0800477 else
478 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700479 if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_LE)) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480 {
481 p_tcb->att_lcid = L2CAP_ATT_CID;
482
483 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
484
485 p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800486
487 gatt_send_conn_cback (p_tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488 if (check_srv_chg)
489 {
490 gatt_chk_srv_chg (p_srv_chg_clt);
491 }
492 }
493 else
494 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700495 GATT_TRACE_ERROR("CCB max out, no rsources");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800496 }
497 }
498 }
499 else
500 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700501 gatt_cleanup_upon_disc(bd_addr, reason, transport);
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700502 GATT_TRACE_DEBUG ("ATT disconnected");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800503 }
504}
505
506/*******************************************************************************
507**
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700508** Function gatt_channel_congestion
509**
510** Description This function is called to process the congestion callback
511** from lcb
512**
513** Returns void
514**
515*******************************************************************************/
516static void gatt_channel_congestion(tGATT_TCB *p_tcb, BOOLEAN congested)
517{
518 UINT8 i = 0;
519 tGATT_REG *p_reg=NULL;
520 UINT16 conn_id;
521
522 /* if uncongested, check to see if there is any more pending data */
523 if (p_tcb != NULL && congested == FALSE)
524 {
525 gatt_cl_send_next_cmd_inq(p_tcb);
526 }
527 /* notifying all applications for the connection up event */
528 for (i = 0, p_reg = gatt_cb.cl_rcb ; i < GATT_MAX_APPS; i++, p_reg++)
529 {
530 if (p_reg->in_use)
531 {
532 if (p_reg->app_cb.p_congestion_cb)
533 {
534 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
535 (*p_reg->app_cb.p_congestion_cb)(conn_id, congested);
536 }
537 }
538 }
539}
540
541/*******************************************************************************
542**
543** Function gatt_le_cong_cback
544**
545** Description This function is called when GATT fixed channel is congested
546** or uncongested.
547**
548** Returns void
549**
550*******************************************************************************/
551static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congested)
552{
553 tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(remote_bda, BT_TRANSPORT_LE);
554
555 /* if uncongested, check to see if there is any more pending data */
556 if (p_tcb != NULL)
557 {
558 gatt_channel_congestion(p_tcb, congested);
559 }
560}
561
562/*******************************************************************************
563**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800564** Function gatt_le_data_ind
565**
566** Description This function is called when data is received from L2CAP.
567** if we are the originator of the connection, we are the ATT
568** client, and the received message is queued up for the client.
569**
570** If we are the destination of the connection, we are the ATT
571** server, so the message is passed to the server processing
572** function.
573**
574** Returns void
575**
576*******************************************************************************/
Kim Schulz8372aa52015-03-25 10:39:40 +0100577static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800578{
579 tGATT_TCB *p_tcb;
580
581 /* Find CCB based on bd addr */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700582 if ((p_tcb = gatt_find_tcb_by_addr (bd_addr, BT_TRANSPORT_LE)) != NULL &&
The Android Open Source Project5738f832012-12-12 16:00:35 -0800583 gatt_get_ch_state(p_tcb) >= GATT_CH_OPEN)
584 {
585 gatt_data_process(p_tcb, p_buf);
586 }
587 else
588 {
Pavlin Radoslavovabd70ab2016-02-05 13:54:43 -0800589 osi_free(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800590
591 if (p_tcb != NULL)
592 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700593 GATT_TRACE_WARNING ("ATT - Ignored L2CAP data while in state: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800594 gatt_get_ch_state(p_tcb));
595 }
596 }
597}
598
599/*******************************************************************************
600**
601** Function gatt_l2cif_connect_ind
602**
603** Description This function handles an inbound connection indication
604** from L2CAP. This is the case where we are acting as a
605** server.
606**
607** Returns void
608**
609*******************************************************************************/
610static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
611{
612 /* do we already have a control channel for this peer? */
613 UINT8 result = L2CAP_CONN_OK;
614 tL2CAP_CFG_INFO cfg;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700615 tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_BR_EDR);
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800616 UNUSED(psm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800617
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700618 GATT_TRACE_ERROR("Connection indication cid = %d", lcid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619 /* new connection ? */
620 if (p_tcb == NULL)
621 {
622 /* allocate tcb */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700623 if ((p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800624 {
625 /* no tcb available, reject L2CAP connection */
626 result = L2CAP_CONN_NO_RESOURCES;
627 }
628 else
629 p_tcb->att_lcid = lcid;
630
631 }
632 else /* existing connection , reject it */
633 {
634 result = L2CAP_CONN_NO_RESOURCES;
635 }
636
637 /* Send L2CAP connect rsp */
638 L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
639
640 /* if result ok, proceed with connection */
641 if (result == L2CAP_CONN_OK)
642 {
643 /* transition to configuration state */
644 gatt_set_ch_state(p_tcb, GATT_CH_CFG);
645
646 /* Send L2CAP config req */
647 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
648 cfg.mtu_present = TRUE;
649 cfg.mtu = GATT_MAX_MTU_SIZE;
650
651 L2CA_ConfigReq(lcid, &cfg);
652 }
653}
654
655/*******************************************************************************
656**
657** Function gatt_l2c_connect_cfm_cback
658**
659** Description This is the L2CAP connect confirm callback function.
660**
661**
662** Returns void
663**
664*******************************************************************************/
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700665static void gatt_l2cif_connect_cfm_cback(UINT16 lcid, UINT16 result)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800666{
667 tGATT_TCB *p_tcb;
668 tL2CAP_CFG_INFO cfg;
669
670 /* look up clcb for this channel */
671 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
672 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -0700673 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);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674
675 /* if in correct state */
676 if (gatt_get_ch_state(p_tcb) == GATT_CH_CONN)
677 {
678 /* if result successful */
679 if (result == L2CAP_CONN_OK)
680 {
681 /* set channel state */
682 gatt_set_ch_state(p_tcb, GATT_CH_CFG);
683
684 /* Send L2CAP config req */
685 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
686 cfg.mtu_present = TRUE;
687 cfg.mtu = GATT_MAX_MTU_SIZE;
688 L2CA_ConfigReq(lcid, &cfg);
689 }
690 /* else initiating connection failure */
691 else
692 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700693 gatt_cleanup_upon_disc(p_tcb->peer_bda, result, GATT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800694 }
695 }
696 else /* wrong state, disconnect it */
697 {
698 if (result == L2CAP_CONN_OK)
699 {
700 /* just in case the peer also accepts our connection - Send L2CAP disconnect req */
701 L2CA_DisconnectReq(lcid);
702 }
703 }
704 }
705}
706
707/*******************************************************************************
708**
709** Function gatt_l2cif_config_cfm_cback
710**
711** Description This is the L2CAP config confirm callback function.
712**
713**
714** Returns void
715**
716*******************************************************************************/
717void gatt_l2cif_config_cfm_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
718{
719 tGATT_TCB *p_tcb;
720 tGATTS_SRV_CHG *p_srv_chg_clt=NULL;
721
722 /* look up clcb for this channel */
723 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
724 {
725 /* if in correct state */
726 if ( gatt_get_ch_state(p_tcb) == GATT_CH_CFG)
727 {
728 /* if result successful */
729 if (p_cfg->result == L2CAP_CFG_OK)
730 {
731 /* update flags */
732 p_tcb->ch_flags |= GATT_L2C_CFG_CFM_DONE;
733
734 /* if configuration complete */
735 if (p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE)
736 {
737 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
738
739 if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
740 {
741 gatt_chk_srv_chg(p_srv_chg_clt);
742 }
743 else
744 {
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700745 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800746 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
747 }
748
749 /* send callback */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800750 gatt_send_conn_cback(p_tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751 }
752 }
753 /* else failure */
754 else
755 {
756 /* Send L2CAP disconnect req */
757 L2CA_DisconnectReq(lcid);
758 }
759 }
760 }
761}
762
763/*******************************************************************************
764**
765** Function gatt_l2cif_config_ind_cback
766**
767** Description This is the L2CAP config indication callback function.
768**
769**
770** Returns void
771**
772*******************************************************************************/
773void gatt_l2cif_config_ind_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
774{
775 tGATT_TCB *p_tcb;
776 tGATTS_SRV_CHG *p_srv_chg_clt=NULL;
777 /* look up clcb for this channel */
778 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
779 {
780 /* GATT uses the smaller of our MTU and peer's MTU */
Andre Eisenbach6975b4d2013-08-05 16:55:38 -0700781 if ( p_cfg->mtu_present &&
782 (p_cfg->mtu >= GATT_MIN_BR_MTU_SIZE && p_cfg->mtu < L2CAP_DEFAULT_MTU))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800783 p_tcb->payload_size = p_cfg->mtu;
784 else
785 p_tcb->payload_size = L2CAP_DEFAULT_MTU;
786
787 /* send L2CAP configure response */
788 memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
789 p_cfg->result = L2CAP_CFG_OK;
790 L2CA_ConfigRsp(lcid, p_cfg);
791
792 /* if first config ind */
793 if ((p_tcb->ch_flags & GATT_L2C_CFG_IND_DONE) == 0)
794 {
795 /* update flags */
796 p_tcb->ch_flags |= GATT_L2C_CFG_IND_DONE;
797
798 /* if configuration complete */
799 if (p_tcb->ch_flags & GATT_L2C_CFG_CFM_DONE)
800 {
801 gatt_set_ch_state(p_tcb, GATT_CH_OPEN);
802 if ((p_srv_chg_clt = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL)
803 {
804 gatt_chk_srv_chg(p_srv_chg_clt);
805 }
806 else
807 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700808 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800809 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
810 }
811
812 /* send callback */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800813 gatt_send_conn_cback(p_tcb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800814 }
815 }
816 }
817}
818
819/*******************************************************************************
820**
821** Function gatt_l2cif_disconnect_ind_cback
822**
823** Description This is the L2CAP disconnect indication callback function.
824**
825**
826** Returns void
827**
828*******************************************************************************/
829void gatt_l2cif_disconnect_ind_cback(UINT16 lcid, BOOLEAN ack_needed)
830{
831 tGATT_TCB *p_tcb;
832 UINT16 reason;
833
834 /* look up clcb for this channel */
835 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
836 {
837 if (ack_needed)
838 {
839 /* send L2CAP disconnect response */
840 L2CA_DisconnectRsp(lcid);
841 }
Andre Eisenbach0d3786e2013-07-22 17:42:18 -0700842 if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800843 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700844 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
Andre Eisenbach0d3786e2013-07-22 17:42:18 -0700845 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800846 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847 /* if ACL link is still up, no reason is logged, l2cap is disconnect from peer */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700848 if ((reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport)) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800849 reason = GATT_CONN_TERMINATE_PEER_USER;
850
851 /* send disconnect callback */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700852 gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800853 }
854}
855
856/*******************************************************************************
857**
858** Function gatt_l2cif_disconnect_cfm_cback
859**
860** Description This is the L2CAP disconnect confirm callback function.
861**
862**
863** Returns void
864**
865*******************************************************************************/
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700866static void gatt_l2cif_disconnect_cfm_cback(UINT16 lcid, UINT16 result)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800867{
868 tGATT_TCB *p_tcb;
869 UINT16 reason;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800870 UNUSED(result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871
872 /* look up clcb for this channel */
873 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL)
874 {
Andre Eisenbach0d3786e2013-07-22 17:42:18 -0700875 /* If the device is not in the service changed client list, add it... */
876 if (gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda) == NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800877 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700878 if (btm_sec_is_a_bonded_dev(p_tcb->peer_bda))
Andre Eisenbach0d3786e2013-07-22 17:42:18 -0700879 gatt_add_a_bonded_dev_for_srv_chg(p_tcb->peer_bda);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800880 }
881
The Android Open Source Project5738f832012-12-12 16:00:35 -0800882 /* send disconnect callback */
883 /* if ACL link is still up, no reason is logged, l2cap is disconnect from peer */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700884 if ((reason = L2CA_GetDisconnectReason(p_tcb->peer_bda, p_tcb->transport)) == 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800885 reason = GATT_CONN_TERMINATE_LOCAL_HOST;
886
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700887 gatt_cleanup_upon_disc(p_tcb->peer_bda, reason, GATT_TRANSPORT_BR_EDR);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800888 }
889}
890
891/*******************************************************************************
892**
893** Function gatt_l2cif_data_ind_cback
894**
895** Description This is the L2CAP data indication callback function.
896**
897**
898** Returns void
899**
900*******************************************************************************/
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700901static void gatt_l2cif_data_ind_cback(UINT16 lcid, BT_HDR *p_buf)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800902{
903 tGATT_TCB *p_tcb;
904
905 /* look up clcb for this channel */
906 if ((p_tcb = gatt_find_tcb_by_cid(lcid)) != NULL &&
907 gatt_get_ch_state(p_tcb) == GATT_CH_OPEN)
908 {
909 /* process the data */
910 gatt_data_process(p_tcb, p_buf);
911 }
912 else /* prevent buffer leak */
Pavlin Radoslavovabd70ab2016-02-05 13:54:43 -0800913 osi_free(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800914}
915
916/*******************************************************************************
917**
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700918** Function gatt_l2cif_congest_cback
919**
920** Description L2CAP congestion callback
921**
922** Returns void
923**
924*******************************************************************************/
925static void gatt_l2cif_congest_cback (UINT16 lcid, BOOLEAN congested)
926{
927 tGATT_TCB *p_tcb = gatt_find_tcb_by_cid(lcid);
928
929 if (p_tcb != NULL)
930 {
931 gatt_channel_congestion(p_tcb, congested);
932 }
933}
934
935/*******************************************************************************
936**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800937** Function gatt_send_conn_cback
938**
939** Description Callback used to notify layer above about a connection.
940**
941**
942** Returns void
943**
944*******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800945static void gatt_send_conn_cback(tGATT_TCB *p_tcb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800946{
947 UINT8 i;
948 tGATT_REG *p_reg;
949 tGATT_BG_CONN_DEV *p_bg_dev=NULL;
950 UINT16 conn_id;
951
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800952 p_bg_dev = gatt_find_bg_dev(p_tcb->peer_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800953
954 /* notifying all applications for the connection up event */
955 for (i = 0, p_reg = gatt_cb.cl_rcb ; i < GATT_MAX_APPS; i++, p_reg++)
956 {
957 if (p_reg->in_use)
958 {
959 if (p_bg_dev && gatt_is_bg_dev_for_app(p_bg_dev, p_reg->gatt_if))
960 gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, TRUE, TRUE);
961
962 if (p_reg->app_cb.p_conn_cb)
963 {
964 conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700965 (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, p_tcb->peer_bda, conn_id,
966 TRUE, 0, p_tcb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800967 }
968 }
969 }
970
971
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700972 if (gatt_num_apps_hold_link(p_tcb) && p_tcb->att_lcid == L2CAP_ATT_CID )
The Android Open Source Project5738f832012-12-12 16:00:35 -0800973 {
974 /* disable idle timeout if one or more clients are holding the link disable the idle timer */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700975 GATT_SetIdleTimeout(p_tcb->peer_bda, GATT_LINK_NO_IDLE_TIMEOUT, p_tcb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800976 }
977}
978
979/*******************************************************************************
980**
981** Function gatt_le_data_ind
982**
983** Description This function is called when data is received from L2CAP.
984** if we are the originator of the connection, we are the ATT
985** client, and the received message is queued up for the client.
986**
987** If we are the destination of the connection, we are the ATT
988** server, so the message is passed to the server processing
989** function.
990**
991** Returns void
992**
993*******************************************************************************/
994void gatt_data_process (tGATT_TCB *p_tcb, BT_HDR *p_buf)
995{
996 UINT8 *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
997 UINT8 op_code, pseudo_op_code;
998 UINT16 msg_len;
999
1000
1001 if (p_buf->len > 0)
1002 {
1003 msg_len = p_buf->len - 1;
1004 STREAM_TO_UINT8(op_code, p);
1005
1006 /* remove the two MSBs associated with sign write and write cmd */
1007 pseudo_op_code = op_code & (~GATT_WRITE_CMD_MASK);
1008
1009 if (pseudo_op_code < GATT_OP_CODE_MAX)
1010 {
1011 if (op_code == GATT_SIGN_CMD_WRITE)
1012 {
1013 gatt_verify_signature(p_tcb, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014 }
1015 else
1016 {
1017 /* message from client */
1018 if ((op_code % 2) == 0)
1019 gatt_server_handle_client_req (p_tcb, op_code, msg_len, p);
1020 else
1021 gatt_client_handle_server_rsp (p_tcb, op_code, msg_len, p);
1022 }
1023 }
1024 else
1025 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001026 GATT_TRACE_ERROR ("ATT - Rcvd L2CAP data, unknown cmd: 0x%x", op_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001027 }
1028 }
1029 else
1030 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001031 GATT_TRACE_ERROR ("invalid data length, ignore");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001032 }
1033
Pavlin Radoslavovabd70ab2016-02-05 13:54:43 -08001034 osi_free(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001035}
1036
1037/*******************************************************************************
1038**
1039** Function gatt_add_a_bonded_dev_for_srv_chg
1040**
1041** Description Add a bonded dev to the service changed client list
1042**
1043** Returns void
1044**
1045*******************************************************************************/
1046void gatt_add_a_bonded_dev_for_srv_chg (BD_ADDR bda)
1047{
The Android Open Source Project5738f832012-12-12 16:00:35 -08001048 tGATTS_SRV_CHG_REQ req;
1049 tGATTS_SRV_CHG srv_chg_clt;
1050
1051 memcpy(srv_chg_clt.bda, bda, BD_ADDR_LEN);
1052 srv_chg_clt.srv_changed = FALSE;
Satya Calloji444a8da2015-03-06 10:38:22 -08001053 if (gatt_add_srv_chg_clt(&srv_chg_clt) != NULL)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054 {
1055 memcpy(req.srv_chg.bda, bda, BD_ADDR_LEN);
1056 req.srv_chg.srv_changed = FALSE;
1057 if (gatt_cb.cb_info.p_srv_chg_callback)
1058 (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_ADD_CLIENT, &req, NULL);
1059 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060}
1061
1062/*******************************************************************************
1063**
1064** Function gatt_send_srv_chg_ind
1065**
1066** Description This function is called to send a service chnaged indication to
1067** the specified bd address
1068**
1069** Returns void
1070**
1071*******************************************************************************/
1072void gatt_send_srv_chg_ind (BD_ADDR peer_bda)
1073{
1074 UINT8 handle_range[GATT_SIZE_OF_SRV_CHG_HNDL_RANGE];
1075 UINT8 *p = handle_range;
1076 UINT16 conn_id;
1077
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001078 GATT_TRACE_DEBUG("gatt_send_srv_chg_ind");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001079
1080 if (gatt_cb.handle_of_h_r)
1081 {
1082 if ((conn_id = gatt_profile_find_conn_id_by_bd_addr(peer_bda)) != GATT_INVALID_CONN_ID)
1083 {
1084 UINT16_TO_STREAM (p, 1);
1085 UINT16_TO_STREAM (p, 0xFFFF);
1086 GATTS_HandleValueIndication (conn_id,
1087 gatt_cb.handle_of_h_r,
1088 GATT_SIZE_OF_SRV_CHG_HNDL_RANGE,
1089 handle_range);
1090 }
1091 else
1092 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001093 GATT_TRACE_ERROR("Unable to find conn_id for %08x%04x ",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001094 (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
1095 (peer_bda[4]<<8)+peer_bda[5] );
1096 }
1097 }
1098}
1099
1100/*******************************************************************************
1101**
1102** Function gatt_chk_srv_chg
1103**
1104** Description Check sending service chnaged Indication is required or not
1105** if required then send the Indication
1106**
1107** Returns void
1108**
1109*******************************************************************************/
1110void gatt_chk_srv_chg(tGATTS_SRV_CHG *p_srv_chg_clt)
1111{
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001112 GATT_TRACE_DEBUG("gatt_chk_srv_chg srv_changed=%d", p_srv_chg_clt->srv_changed );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001113
1114 if (p_srv_chg_clt->srv_changed)
1115 {
1116 gatt_send_srv_chg_ind(p_srv_chg_clt->bda);
1117 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118}
1119
1120/*******************************************************************************
1121**
1122** Function gatt_init_srv_chg
1123**
1124** Description This function is used to initialize the service changed
1125** attribute value
1126**
1127** Returns void
1128**
1129*******************************************************************************/
1130void gatt_init_srv_chg (void)
1131{
1132 tGATTS_SRV_CHG_REQ req;
1133 tGATTS_SRV_CHG_RSP rsp;
1134 BOOLEAN status;
1135 UINT8 num_clients,i;
1136 tGATTS_SRV_CHG srv_chg_clt;
1137
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001138 GATT_TRACE_DEBUG("gatt_init_srv_chg");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139 if (gatt_cb.cb_info.p_srv_chg_callback)
1140 {
1141 status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_NUM_CLENTS, NULL, &rsp);
1142
1143 if (status && rsp.num_clients)
1144 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001145 GATT_TRACE_DEBUG("gatt_init_srv_chg num_srv_chg_clt_clients=%d", rsp.num_clients);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001146 num_clients = rsp.num_clients;
1147 i = 1; /* use one based index */
1148 while ((i <= num_clients) && status)
1149 {
1150 req.client_read_index = i;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001151 if ((status = (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_READ_CLENT, &req, &rsp)) == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001152 {
1153 memcpy(&srv_chg_clt, &rsp.srv_chg ,sizeof(tGATTS_SRV_CHG));
1154 if (gatt_add_srv_chg_clt(&srv_chg_clt) == NULL)
1155 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001156 GATT_TRACE_ERROR("Unable to add a service change client");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001157 status = FALSE;
1158 }
1159 }
1160 i++;
1161 }
1162 }
1163 }
1164 else
1165 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001166 GATT_TRACE_DEBUG("gatt_init_srv_chg callback not registered yet");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001167 }
1168}
1169
1170/*******************************************************************************
1171**
1172** Function gatt_proc_srv_chg
1173**
1174** Description This function is process the service changed request
1175**
1176** Returns void
1177**
1178*******************************************************************************/
1179void gatt_proc_srv_chg (void)
1180{
1181 UINT8 start_idx, found_idx;
1182 BD_ADDR bda;
1183 BOOLEAN srv_chg_ind_pending=FALSE;
1184 tGATT_TCB *p_tcb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001185 tBT_TRANSPORT transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001186
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001187 GATT_TRACE_DEBUG ("gatt_proc_srv_chg");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001188
1189 if (gatt_cb.cb_info.p_srv_chg_callback && gatt_cb.handle_of_h_r)
1190 {
1191 gatt_set_srv_chg();
1192 start_idx =0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001193 while (gatt_find_the_connected_bda(start_idx, bda, &found_idx, &transport))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001194 {
1195 p_tcb = &gatt_cb.tcb[found_idx];;
1196 srv_chg_ind_pending = gatt_is_srv_chg_ind_pending(p_tcb);
1197
1198 if (!srv_chg_ind_pending)
1199 {
1200 gatt_send_srv_chg_ind(bda);
1201 }
1202 else
1203 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001204 GATT_TRACE_DEBUG ("discard srv chg - already has one in the queue");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001205 }
1206 start_idx = ++found_idx;
1207 }
1208 }
1209}
1210
1211/*******************************************************************************
1212**
1213** Function gatt_set_ch_state
1214**
1215** Description This function set the ch_state in tcb
1216**
1217** Returns none
1218**
1219*******************************************************************************/
1220void gatt_set_ch_state(tGATT_TCB *p_tcb, tGATT_CH_STATE ch_state)
1221{
1222 if (p_tcb)
1223 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001224 GATT_TRACE_DEBUG ("gatt_set_ch_state: old=%d new=%d", p_tcb->ch_state, ch_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001225 p_tcb->ch_state = ch_state;
1226 }
1227}
1228
1229/*******************************************************************************
1230**
1231** Function gatt_get_ch_state
1232**
1233** Description This function get the ch_state in tcb
1234**
1235** Returns none
1236**
1237*******************************************************************************/
1238tGATT_CH_STATE gatt_get_ch_state(tGATT_TCB *p_tcb)
1239{
1240 tGATT_CH_STATE ch_state = GATT_CH_CLOSE;
1241 if (p_tcb)
1242 {
Sharvil Nanavatib44cc592014-05-04 10:03:35 -07001243 GATT_TRACE_DEBUG ("gatt_get_ch_state: ch_state=%d", p_tcb->ch_state);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001244 ch_state = p_tcb->ch_state;
1245 }
1246 return ch_state;
1247}
1248
1249#endif /* BLE_INCLUDED */