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 GATT server attributes access request
24 ******************************************************************************/
26 #include "bt_target.h"
32 #if BLE_INCLUDED == TRUE
34 #define GATTP_MAX_NUM_INC_SVR 0
35 #define GATTP_MAX_CHAR_NUM 2
36 #define GATTP_MAX_ATTR_NUM (GATTP_MAX_CHAR_NUM * 2 + GATTP_MAX_NUM_INC_SVR + 1)
37 #define GATTP_MAX_CHAR_VALUE_SIZE 50
39 #ifndef GATTP_ATTR_DB_SIZE
40 #define GATTP_ATTR_DB_SIZE GATT_DB_MEM_SIZE(GATTP_MAX_NUM_INC_SVR, GATTP_MAX_CHAR_NUM, GATTP_MAX_CHAR_VALUE_SIZE)
43 static void gatt_request_cback(UINT16 conn_id, UINT32 trans_id, UINT8 op_code, tGATTS_DATA *p_data);
44 static void gatt_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected,
45 tGATT_DISCONN_REASON reason, tBT_TRANSPORT transport);
46 static void gatt_disc_res_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data);
47 static void gatt_disc_cmpl_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
48 static void gatt_cl_op_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
49 tGATT_CL_COMPLETE *p_data);
51 static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB *p_clcb);
54 static tGATT_CBACK gatt_profile_cback =
57 gatt_cl_op_cmpl_cback,
65 /*******************************************************************************
67 ** Function gatt_profile_find_conn_id_by_bd_addr
69 ** Description Find the connection ID by remote address
71 ** Returns Connection ID
73 *******************************************************************************/
74 UINT16 gatt_profile_find_conn_id_by_bd_addr(BD_ADDR remote_bda)
76 UINT16 conn_id = GATT_INVALID_CONN_ID;
77 GATT_GetConnIdIfConnected (gatt_cb.gatt_if, remote_bda, &conn_id, BT_TRANSPORT_LE);
81 /*******************************************************************************
83 ** Function gatt_profile_find_clcb_by_conn_id
85 ** Description find clcb by Connection ID
87 ** Returns Pointer to the found link conenction control block.
89 *******************************************************************************/
90 static tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_conn_id(UINT16 conn_id)
93 tGATT_PROFILE_CLCB *p_clcb = NULL;
95 for (i_clcb = 0, p_clcb= gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++)
97 if (p_clcb->in_use && p_clcb->conn_id == conn_id)
104 /*******************************************************************************
106 ** Function gatt_profile_find_clcb_by_bd_addr
108 ** Description The function searches all LCBs with macthing bd address.
110 ** Returns Pointer to the found link conenction control block.
112 *******************************************************************************/
113 static tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_bd_addr(BD_ADDR bda, tBT_TRANSPORT transport)
116 tGATT_PROFILE_CLCB *p_clcb = NULL;
118 for (i_clcb = 0, p_clcb= gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++)
120 if (p_clcb->in_use && p_clcb->transport == transport &&
121 p_clcb->connected && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN))
128 /*******************************************************************************
130 ** Function gatt_profile_clcb_alloc
132 ** Description The function allocates a GATT profile connection link control block
134 ** Returns NULL if not found. Otherwise pointer to the connection link block.
136 *******************************************************************************/
137 tGATT_PROFILE_CLCB *gatt_profile_clcb_alloc (UINT16 conn_id, BD_ADDR bda, tBT_TRANSPORT tranport)
140 tGATT_PROFILE_CLCB *p_clcb = NULL;
142 for (i_clcb = 0, p_clcb= gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++)
146 p_clcb->in_use = TRUE;
147 p_clcb->conn_id = conn_id;
148 p_clcb->connected = TRUE;
149 p_clcb->transport = tranport;
150 memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
154 if(i_clcb < GATT_MAX_APPS)
160 /*******************************************************************************
162 ** Function gatt_profile_clcb_dealloc
164 ** Description The function deallocates a GATT profile connection link control block
168 *******************************************************************************/
169 void gatt_profile_clcb_dealloc (tGATT_PROFILE_CLCB *p_clcb)
171 memset(p_clcb, 0, sizeof(tGATT_PROFILE_CLCB));
174 /*******************************************************************************
176 ** Function gatt_request_cback
178 ** Description GATT profile attribute access request callback.
182 *******************************************************************************/
183 static void gatt_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type,
186 UINT8 status = GATT_INVALID_PDU;
188 BOOLEAN ignore = FALSE;
190 memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
194 case GATTS_REQ_TYPE_READ:
195 status = GATT_READ_NOT_PERMIT;
198 case GATTS_REQ_TYPE_WRITE:
199 status = GATT_WRITE_NOT_PERMIT;
202 case GATTS_REQ_TYPE_WRITE_EXEC:
205 GATT_TRACE_EVENT("Ignore GATT_REQ_EXEC_WRITE/WRITE_CMD" );
208 case GATTS_REQ_TYPE_MTU:
209 GATT_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
214 GATT_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
219 GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
223 /*******************************************************************************
225 ** Function gatt_connect_cback
227 ** Description Gatt profile connection callback.
231 *******************************************************************************/
232 static void gatt_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
233 BOOLEAN connected, tGATT_DISCONN_REASON reason,
234 tBT_TRANSPORT transport)
238 GATT_TRACE_EVENT ("%s: from %08x%04x connected:%d conn_id=%d reason = 0x%04x", __FUNCTION__,
239 (bda[0]<<24)+(bda[1]<<16)+(bda[2]<<8)+bda[3],
240 (bda[4]<<8)+bda[5], connected, conn_id, reason);
242 tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_bd_addr(bda, transport);
248 p_clcb->conn_id = conn_id;
249 p_clcb->connected = TRUE;
251 if (p_clcb->ccc_stage == GATT_SVC_CHANGED_CONNECTING)
253 p_clcb->ccc_stage ++;
254 gatt_cl_start_config_ccc(p_clcb);
257 gatt_profile_clcb_dealloc(p_clcb);
261 /*******************************************************************************
263 ** Function gatt_profile_db_init
265 ** Description Initializa the GATT profile attribute database.
267 *******************************************************************************/
268 void gatt_profile_db_init (void)
270 tBT_UUID app_uuid = {LEN_UUID_128, {0}};
271 tBT_UUID uuid = {LEN_UUID_16, {UUID_SERVCLASS_GATT_SERVER}};
272 UINT16 service_handle = 0;
275 /* Fill our internal UUID with a fixed pattern 0x81 */
276 memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
279 /* Create a GATT profile service */
280 gatt_cb.gatt_if = GATT_Register(&app_uuid, &gatt_profile_cback);
281 GATT_StartIf(gatt_cb.gatt_if);
283 service_handle = GATTS_CreateService (gatt_cb.gatt_if , &uuid, 0, GATTP_MAX_ATTR_NUM, TRUE);
284 /* add Service Changed characteristic
286 uuid.uu.uuid16 = gatt_cb.gattp_attr.uuid = GATT_UUID_GATT_SRV_CHGD;
287 gatt_cb.gattp_attr.service_change = 0;
288 gatt_cb.gattp_attr.handle =
289 gatt_cb.handle_of_h_r = GATTS_AddCharacteristic(service_handle, &uuid, 0, GATT_CHAR_PROP_BIT_INDICATE);
291 GATT_TRACE_DEBUG ("gatt_profile_db_init: handle of service changed%d",
292 gatt_cb.handle_of_h_r );
296 status = GATTS_StartService (gatt_cb.gatt_if, service_handle, GATTP_TRANSPORT_SUPPORTED );
298 GATT_TRACE_DEBUG ("gatt_profile_db_init: gatt_if=%d start status%d",
299 gatt_cb.gatt_if, status);
302 /*******************************************************************************
304 ** Function gatt_disc_res_cback
306 ** Description Gatt profile discovery result callback
310 *******************************************************************************/
311 static void gatt_disc_res_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data)
313 tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
320 case GATT_DISC_SRVC_BY_UUID:/* stage 1 */
321 p_clcb->e_handle = p_data->value.group_value.e_handle;
322 p_clcb->ccc_result ++;
325 case GATT_DISC_CHAR:/* stage 2 */
326 p_clcb->s_handle = p_data->value.dclr_value.val_handle;
327 p_clcb->ccc_result ++;
330 case GATT_DISC_CHAR_DSCPT: /* stage 3 */
331 if (p_data->type.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG)
333 p_clcb->s_handle = p_data->handle;
334 p_clcb->ccc_result ++;
340 /*******************************************************************************
342 ** Function gatt_disc_cmpl_cback
344 ** Description Gatt profile discovery complete callback
348 *******************************************************************************/
349 static void gatt_disc_cmpl_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status)
351 tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
356 if (status == GATT_SUCCESS && p_clcb->ccc_result > 0)
358 p_clcb->ccc_result = 0;
359 p_clcb->ccc_stage ++;
360 gatt_cl_start_config_ccc(p_clcb);
362 GATT_TRACE_ERROR("%s() - Unable to register for service changed indication", __func__);
366 /*******************************************************************************
368 ** Function gatt_cl_op_cmpl_cback
370 ** Description Gatt profile client operation complete callback
374 *******************************************************************************/
375 static void gatt_cl_op_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op,
376 tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
384 /*******************************************************************************
386 ** Function gatt_cl_start_config_ccc
388 ** Description Gatt profile start configure service change CCC
392 *******************************************************************************/
393 static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB *p_clcb)
395 tGATT_DISC_PARAM srvc_disc_param;
396 tGATT_VALUE ccc_value;
398 GATT_TRACE_DEBUG("%s() - stage: %d", __FUNCTION__, p_clcb->ccc_stage);
400 memset (&srvc_disc_param, 0 , sizeof(tGATT_DISC_PARAM));
401 memset (&ccc_value, 0 , sizeof(tGATT_VALUE));
403 switch(p_clcb->ccc_stage)
405 case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */
406 srvc_disc_param.s_handle = 1;
407 srvc_disc_param.e_handle = 0xffff;
408 srvc_disc_param.service.len = 2;
409 srvc_disc_param.service.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
410 GATTC_Discover(p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, &srvc_disc_param);
413 case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */
414 srvc_disc_param.s_handle = 1;
415 srvc_disc_param.e_handle = p_clcb->e_handle;
416 srvc_disc_param.service.len = 2;
417 srvc_disc_param.service.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
418 GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR, &srvc_disc_param);
421 case GATT_SVC_CHANGED_DESCRIPTOR: /* discover service change ccc */
422 srvc_disc_param.s_handle = p_clcb->s_handle;
423 srvc_disc_param.e_handle = p_clcb->e_handle;
424 GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR_DSCPT, &srvc_disc_param);
427 case GATT_SVC_CHANGED_CONFIGURE_CCCD: /* write ccc */
428 ccc_value.handle = p_clcb->s_handle;
430 ccc_value.value[0] = GATT_CLT_CONFIG_INDICATION;
431 GATTC_Write(p_clcb->conn_id, GATT_WRITE, &ccc_value);
436 /*******************************************************************************
438 ** Function GATT_ConfigServiceChangeCCC
440 ** Description Configure service change indication on remote device
444 *******************************************************************************/
445 void GATT_ConfigServiceChangeCCC (BD_ADDR remote_bda, BOOLEAN enable, tBT_TRANSPORT transport)
447 tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_bd_addr (remote_bda, transport);
450 p_clcb = gatt_profile_clcb_alloc (0, remote_bda, transport);
455 if (GATT_GetConnIdIfConnected (gatt_cb.gatt_if, remote_bda, &p_clcb->conn_id, transport))
457 p_clcb->connected = TRUE;
459 /* hold the link here */
460 GATT_Connect(gatt_cb.gatt_if, remote_bda, TRUE, transport, true);
461 p_clcb->ccc_stage = GATT_SVC_CHANGED_CONNECTING;
463 if (!p_clcb->connected)
465 /* wait for connection */
469 p_clcb->ccc_stage ++;
470 gatt_cl_start_config_ccc(p_clcb);
473 #endif /* BLE_INCLUDED */