blob: 5dc6f2d9e2218173f4ebbd54e711056cf2fba863 [file] [log] [blame]
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2013 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 ******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080018#include "bt_target.h"
19
20#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
21
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080022#include "bt_utils.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080023#include <string.h>
24#include "gap_int.h"
25#include "gap_api.h"
26#include "gattdefs.h"
27#include "gatt_api.h"
28#include "gatt_int.h"
29#include "btm_int.h"
30#include "hcimsgs.h"
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -070031#include "btcore/include/uuid.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080032
33#define GAP_CHAR_ICON_SIZE 2
34#define GAP_CHAR_DEV_NAME_SIZE 248
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080035#define GAP_MAX_NUM_INC_SVR 0
36#define GAP_MAX_ATTR_NUM (2 * GAP_MAX_CHAR_NUM + GAP_MAX_NUM_INC_SVR + 1)
37#define GAP_MAX_CHAR_VALUE_SIZE (30 + GAP_CHAR_DEV_NAME_SIZE)
38
39
40#ifndef GAP_ATTR_DB_SIZE
41#define GAP_ATTR_DB_SIZE GATT_DB_MEM_SIZE(GAP_MAX_NUM_INC_SVR, GAP_MAX_CHAR_NUM, GAP_MAX_CHAR_VALUE_SIZE)
42#endif
43
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080044static void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE op_code, tGATTS_DATA *p_data);
45
46/* client connection callback */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070047static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected,
48 tGATT_DISCONN_REASON reason, tGATT_TRANSPORT transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080049static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data);
50
51static tGATT_CBACK gap_cback =
52{
53 gap_ble_c_connect_cback,
54 gap_ble_c_cmpl_cback,
55 NULL,
56 NULL,
Zhihai Xu7051db32013-11-12 20:18:37 -080057 gap_ble_s_attr_request_cback,
Andre Eisenbach17b04bd2014-03-28 14:54:22 -070058 NULL,
Zhihai Xu7051db32013-11-12 20:18:37 -080059 NULL
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080060};
61
62
63
64/*******************************************************************************
65**
66** Function gap_find_clcb_by_bd_addr
67**
68** Description The function searches all LCB with macthing bd address
69**
70** Returns total number of clcb found.
71**
72*******************************************************************************/
73tGAP_CLCB *gap_find_clcb_by_bd_addr(BD_ADDR bda)
74{
75 UINT8 i_clcb;
76 tGAP_CLCB *p_clcb = NULL;
77
78 for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
79 {
80 if (p_clcb->in_use && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN))
81 {
82 return p_clcb;
83 }
84 }
85
86 return NULL;
87}
88
89/*******************************************************************************
90**
91** Function gap_ble_find_clcb_by_conn_id
92**
93** Description The function searches all LCB with macthing connection ID
94**
95** Returns total number of clcb found.
96**
97*******************************************************************************/
98tGAP_CLCB *gap_ble_find_clcb_by_conn_id(UINT16 conn_id)
99{
100 UINT8 i_clcb;
101 tGAP_CLCB *p_clcb = NULL;
102
103 for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
104 {
105 if (p_clcb->in_use && p_clcb->connected && p_clcb->conn_id == conn_id)
106 {
107 return p_clcb;
108 }
109 }
110
111 return p_clcb;
112}
113
114/*******************************************************************************
115**
116** Function gap_clcb_alloc
117**
118** Description The function allocates a GAP connection link control block
119**
120** Returns NULL if not found. Otherwise pointer to the connection link block.
121**
122*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800123tGAP_CLCB *gap_clcb_alloc (BD_ADDR bda)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800124{
125 UINT8 i_clcb = 0;
126 tGAP_CLCB *p_clcb = NULL;
127
128 for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
129 {
130 if (!p_clcb->in_use)
131 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700132 fixed_queue_free(p_clcb->pending_req_q, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800133 memset(p_clcb, 0, sizeof(tGAP_CLCB));
134 p_clcb->in_use = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800135 memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700136 p_clcb->pending_req_q = fixed_queue_new(SIZE_MAX);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800137 break;
138 }
139 }
140 return p_clcb;
141}
142
143/*******************************************************************************
144**
Satya Calloji444a8da2015-03-06 10:38:22 -0800145** Function gap_ble_dealloc_clcb
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800146**
Satya Calloji444a8da2015-03-06 10:38:22 -0800147** Description The function clean up the pending request queue in GAP
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800148**
Satya Calloji444a8da2015-03-06 10:38:22 -0800149** Returns none
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800150**
151*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800152void gap_ble_dealloc_clcb(tGAP_CLCB *p_clcb)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800153{
Satya Calloji444a8da2015-03-06 10:38:22 -0800154 tGAP_BLE_REQ *p_q;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800155
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700156 while ((p_q = (tGAP_BLE_REQ *)fixed_queue_try_dequeue(p_clcb->pending_req_q)) != NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800157 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800158 /* send callback to all pending requests if being removed*/
159 if (p_q->p_cback != NULL)
160 (*p_q->p_cback)(FALSE, p_clcb->bda, 0, NULL);
161
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800162 osi_free(p_q);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800163 }
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700164 fixed_queue_free(p_clcb->pending_req_q, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800165
166 memset(p_clcb, 0, sizeof(tGAP_CLCB));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800167}
168
169/*******************************************************************************
170**
Satya Calloji444a8da2015-03-06 10:38:22 -0800171** Function gap_ble_enqueue_request
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800172**
Satya Calloji444a8da2015-03-06 10:38:22 -0800173** Description The function enqueue a GAP client request
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800174**
Satya Calloji444a8da2015-03-06 10:38:22 -0800175** Returns TRUE is successul; FALSE otherwise
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800176**
177*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800178BOOLEAN gap_ble_enqueue_request (tGAP_CLCB *p_clcb, UINT16 uuid, tGAP_BLE_CMPL_CBACK *p_cback)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800179{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800180 tGAP_BLE_REQ *p_q = (tGAP_BLE_REQ *)osi_malloc(sizeof(tGAP_BLE_REQ));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800181
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800182 p_q->p_cback = p_cback;
183 p_q->uuid = uuid;
184 fixed_queue_enqueue(p_clcb->pending_req_q, p_q);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800185
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800186 return TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800187}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800188
Satya Calloji444a8da2015-03-06 10:38:22 -0800189/*******************************************************************************
190**
191** Function gap_ble_dequeue_request
192**
193** Description The function dequeue a GAP client request if any
194**
195** Returns TRUE is successul; FALSE otherwise
196**
197*******************************************************************************/
198BOOLEAN gap_ble_dequeue_request (tGAP_CLCB *p_clcb, UINT16 * p_uuid, tGAP_BLE_CMPL_CBACK **p_cback)
199{
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700200 tGAP_BLE_REQ *p_q = (tGAP_BLE_REQ *)fixed_queue_try_dequeue(p_clcb->pending_req_q);;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800201
Satya Calloji444a8da2015-03-06 10:38:22 -0800202 if (p_q != NULL)
203 {
204 *p_cback = p_q->p_cback;
205 *p_uuid = p_q->uuid;
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800206 osi_free(p_q);
Satya Calloji444a8da2015-03-06 10:38:22 -0800207 return TRUE;
208 }
209
210 return FALSE;
211}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800212
213/*******************************************************************************
214** GAP Attributes Database Request callback
215*******************************************************************************/
216tGATT_STATUS gap_read_attr_value (UINT16 handle, tGATT_VALUE *p_value, BOOLEAN is_long)
217{
218 tGAP_ATTR *p_db_attr = gap_cb.gatt_attr;
219 UINT8 *p = p_value->value, i;
220 UINT16 offset = p_value->offset;
221 UINT8 *p_dev_name = NULL;
222
223 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
224 {
225 if (handle == p_db_attr->handle)
226 {
227 if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME &&
228 is_long == TRUE)
229 return GATT_NOT_LONG;
230
231 switch (p_db_attr->uuid)
232 {
233 case GATT_UUID_GAP_DEVICE_NAME:
234 BTM_ReadLocalDeviceName((char **)&p_dev_name);
235 if (strlen ((char *)p_dev_name) > GATT_MAX_ATTR_LEN)
236 p_value->len = GATT_MAX_ATTR_LEN;
237 else
238 p_value->len = (UINT16)strlen ((char *)p_dev_name);
239
240 if (offset > p_value->len)
241 return GATT_INVALID_OFFSET;
242 else
243 {
244 p_value->len -= offset;
245 p_dev_name += offset;
246 ARRAY_TO_STREAM(p, p_dev_name, p_value->len);
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700247 GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME len=0x%04x", p_value->len);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800248 }
249 break;
250
251 case GATT_UUID_GAP_ICON:
252 UINT16_TO_STREAM(p, p_db_attr->attr_value.icon);
253 p_value->len = 2;
254 break;
255
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800256 case GATT_UUID_GAP_PREF_CONN_PARAM:
257 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_min); /* int_min */
258 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_max); /* int_max */
259 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.latency); /* latency */
260 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.sp_tout); /* sp_tout */
261 p_value->len =8;
262 break;
Satya Calloji444a8da2015-03-06 10:38:22 -0800263
264 /* address resolution */
265 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
266 UINT8_TO_STREAM(p, p_db_attr->attr_value.addr_resolution);
267 p_value->len =1;
268 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800269 }
270 return GATT_SUCCESS;
271 }
272 }
273 return GATT_NOT_FOUND;
274}
275
276/*******************************************************************************
277** GAP Attributes Database Read/Read Blob Request process
278*******************************************************************************/
279tGATT_STATUS gap_proc_read (tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp)
280{
281 tGATT_STATUS status = GATT_NO_RESOURCES;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800282 UNUSED(type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800283
284 if (p_data->is_long)
285 p_rsp->attr_value.offset = p_data->offset;
286
287 p_rsp->attr_value.handle = p_data->handle;
288
289 status = gap_read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long);
290
291 return status;
292}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800293
294/******************************************************************************
295**
296** Function gap_proc_write_req
297**
298** Description GAP ATT server process a write request.
299**
300** Returns void.
301**
302*******************************************************************************/
303UINT8 gap_proc_write_req( tGATTS_REQ_TYPE type, tGATT_WRITE_REQ *p_data)
304{
305 tGAP_ATTR *p_db_attr = gap_cb.gatt_attr;
306 UINT8 i;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800307 UNUSED(type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800308
309 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
310 {
311 if (p_data-> handle == p_db_attr->handle)
312 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700313 return GATT_WRITE_NOT_PERMIT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800314 }
315 }
316 return GATT_NOT_FOUND;
317
318}
319
320/******************************************************************************
321**
322** Function gap_ble_s_attr_request_cback
323**
324** Description GAP ATT server attribute access request callback.
325**
326** Returns void.
327**
328*******************************************************************************/
329void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id,
330 tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
331{
332 UINT8 status = GATT_INVALID_PDU;
333 tGATTS_RSP rsp_msg;
334 BOOLEAN ignore = FALSE;
335
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700336 GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800337
338 memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
339
340 switch (type)
341 {
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700342 case GATTS_REQ_TYPE_READ_CHARACTERISTIC:
343 case GATTS_REQ_TYPE_READ_DESCRIPTOR:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800344 status = gap_proc_read(type, &p_data->read_req, &rsp_msg);
345 break;
346
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700347 case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC:
348 case GATTS_REQ_TYPE_WRITE_DESCRIPTOR:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800349 if (!p_data->write_req.need_rsp)
350 ignore = TRUE;
351
352 status = gap_proc_write_req(type, &p_data->write_req);
353 break;
354
355 case GATTS_REQ_TYPE_WRITE_EXEC:
356 ignore = TRUE;
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700357 GAP_TRACE_EVENT("Ignore GATTS_REQ_TYPE_WRITE_EXEC" );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800358 break;
359
360 case GATTS_REQ_TYPE_MTU:
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700361 GAP_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800362 ignore = TRUE;
363 break;
364
365 default:
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700366 GAP_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800367 break;
368 }
369
370 if (!ignore)
371 GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
372}
373
374/*******************************************************************************
375**
376** Function btm_ble_att_db_init
377**
378** Description GAP ATT database initalization.
379**
380** Returns void.
381**
382*******************************************************************************/
383void gap_attr_db_init(void)
384{
385 tBT_UUID app_uuid = {LEN_UUID_128,{0}};
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800386 UINT16 service_handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800387
388 /* Fill our internal UUID with a fixed pattern 0x82 */
389 memset (&app_uuid.uu.uuid128, 0x82, LEN_UUID_128);
390 memset(gap_cb.gatt_attr, 0, sizeof(tGAP_ATTR) *GAP_MAX_CHAR_NUM);
391
392 gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback);
393
394 GATT_StartIf(gap_cb.gatt_if);
395
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700396 bt_uuid_t svc_uuid, name_uuid, icon_uuid, pref_uuid, addr_res_uuid;
397 uuid_128_from_16(&svc_uuid, UUID_SERVCLASS_GAP_SERVER);
398 uuid_128_from_16(&name_uuid, GATT_UUID_GAP_DEVICE_NAME);
399 uuid_128_from_16(&icon_uuid, GATT_UUID_GAP_ICON);
400 uuid_128_from_16(&pref_uuid, GATT_UUID_GAP_PREF_CONN_PARAM);
401 uuid_128_from_16(&addr_res_uuid, GATT_UUID_GAP_CENTRAL_ADDR_RESOL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800402
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700403 btgatt_db_element_t service[] = {
404 {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = svc_uuid},
405 {.type = BTGATT_DB_CHARACTERISTIC, .uuid = name_uuid, .properties = GATT_CHAR_PROP_BIT_READ, .permissions = GATT_PERM_READ},
406 {.type = BTGATT_DB_CHARACTERISTIC, .uuid = icon_uuid, .properties = GATT_CHAR_PROP_BIT_READ, .permissions = GATT_PERM_READ},
407 {.type = BTGATT_DB_CHARACTERISTIC, .uuid = addr_res_uuid, .properties = GATT_CHAR_PROP_BIT_READ, .permissions = GATT_PERM_READ}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700408#if BTM_PERIPHERAL_ENABLED == TRUE /* Only needed for peripheral testing */
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700409 ,{.type = BTGATT_DB_CHARACTERISTIC, .uuid = pref_uuid, .properties = GATT_CHAR_PROP_BIT_READ, .permissions = GATT_PERM_READ}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700410#endif
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700411 };
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700412
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700413 /* Add a GAP service */
414 GATTS_AddService(gap_cb.gatt_if, service, sizeof(service)/sizeof(btgatt_db_element_t));
415 service_handle = service[0].attribute_handle;
Satya Calloji444a8da2015-03-06 10:38:22 -0800416
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700417 GAP_TRACE_EVENT("%s: service_handle = %d",__func__, service_handle);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800418
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700419 gap_cb.gatt_attr[0].uuid = GATT_UUID_GAP_DEVICE_NAME;
420 gap_cb.gatt_attr[0].handle = service[1].attribute_handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800421
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700422 gap_cb.gatt_attr[1].uuid = GATT_UUID_GAP_ICON;
423 gap_cb.gatt_attr[1].handle = service[2].attribute_handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800424
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700425 gap_cb.gatt_attr[2].uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL;
426 gap_cb.gatt_attr[2].handle = service[3].attribute_handle;
427 gap_cb.gatt_attr[2].attr_value.addr_resolution = 0;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800428
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700429#if BTM_PERIPHERAL_ENABLED == TRUE /* Only needed for peripheral testing */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800430
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700431 gap_cb.gatt_attr[3].uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
432 gap_cb.gatt_attr[3].attr_value.conn_param.int_max = GAP_PREFER_CONN_INT_MAX; /* 6 */
433 gap_cb.gatt_attr[3].attr_value.conn_param.int_min = GAP_PREFER_CONN_INT_MIN; /* 0 */
434 gap_cb.gatt_attr[3].attr_value.conn_param.latency = GAP_PREFER_CONN_LATENCY; /* 0 */
435 gap_cb.gatt_attr[3].attr_value.conn_param.sp_tout = GAP_PREFER_CONN_SP_TOUT; /* 2000 */
436 gap_cb.gatt_attr[3].handle = service[4].attribute_handle;
437#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800438}
439
440/*******************************************************************************
441**
442** Function GAP_BleAttrDBUpdate
443**
444** Description GAP ATT database update.
445**
446** Returns void.
447**
448*******************************************************************************/
449void GAP_BleAttrDBUpdate(UINT16 attr_uuid, tGAP_BLE_ATTR_VALUE *p_value)
450{
451 tGAP_ATTR *p_db_attr = gap_cb.gatt_attr;
452 UINT8 i = 0;
453
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700454 GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x", attr_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800455
456 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
457 {
458 if (p_db_attr->uuid == attr_uuid)
459 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700460 GAP_TRACE_EVENT("Found attr_uuid=0x%04x", attr_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800461
462 switch (attr_uuid)
463 {
464 case GATT_UUID_GAP_ICON:
465 p_db_attr->attr_value.icon = p_value->icon;
466 break;
467
468 case GATT_UUID_GAP_PREF_CONN_PARAM:
Satya Calloji444a8da2015-03-06 10:38:22 -0800469 memcpy((void *)&p_db_attr->attr_value.conn_param,
470 (const void *)&p_value->conn_param, sizeof(tGAP_BLE_PREF_PARAM));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800471 break;
472
473 case GATT_UUID_GAP_DEVICE_NAME:
474 BTM_SetLocalDeviceName((char *)p_value->p_dev_name);
475 break;
476
Satya Calloji444a8da2015-03-06 10:38:22 -0800477 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
478 p_db_attr->attr_value.addr_resolution = p_value->addr_resolution;
479 break;
480
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800481 }
482 break;
483 }
484 }
485
486 return;
487}
488
489/*******************************************************************************
490**
Satya Calloji444a8da2015-03-06 10:38:22 -0800491** Function gap_ble_send_cl_read_request
492**
493** Description utility function to send a read request for a GAP charactersitic
494**
495** Returns TRUE if read started, else FALSE if GAP is busy
496**
497*******************************************************************************/
498BOOLEAN gap_ble_send_cl_read_request(tGAP_CLCB *p_clcb)
499{
500 tGATT_READ_PARAM param;
501 UINT16 uuid = 0;
502 BOOLEAN started = FALSE;
503
504 if (gap_ble_dequeue_request(p_clcb, &uuid, &p_clcb->p_cback))
505 {
506 memset(&param, 0, sizeof(tGATT_READ_PARAM));
507
508 param.service.uuid.len = LEN_UUID_16;
509 param.service.uuid.uu.uuid16 = uuid;
510 param.service.s_handle = 1;
511 param.service.e_handle = 0xFFFF;
512 param.service.auth_req = 0;
513
514 if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, &param) == GATT_SUCCESS)
515 {
516 p_clcb->cl_op_uuid = uuid;
517 started = TRUE;
518 }
519 }
520
521 return started;
522}
523
524/*******************************************************************************
525**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800526** Function gap_ble_cl_op_cmpl
527**
528** Description GAP client operation complete callback
529**
530** Returns void
531**
532*******************************************************************************/
533void gap_ble_cl_op_cmpl(tGAP_CLCB *p_clcb, BOOLEAN status, UINT16 len, UINT8 *p_name)
534{
Satya Calloji444a8da2015-03-06 10:38:22 -0800535 tGAP_BLE_CMPL_CBACK *p_cback = p_clcb->p_cback;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800536 UINT16 op = p_clcb->cl_op_uuid;
537
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700538 GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800539
540 p_clcb->cl_op_uuid = 0;
541 p_clcb->p_cback=NULL;
542
Satya Calloji444a8da2015-03-06 10:38:22 -0800543 if (p_cback && op)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800544 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700545 GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl");
Satya Calloji444a8da2015-03-06 10:38:22 -0800546 (* p_cback)(status, p_clcb->bda, len, (char *)p_name);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800547 }
548
Satya Calloji444a8da2015-03-06 10:38:22 -0800549 /* if no further activity is requested in callback, drop the link */
550 if (p_clcb->connected)
551 {
552 if (!gap_ble_send_cl_read_request(p_clcb))
553 {
554 GATT_Disconnect(p_clcb->conn_id);
555 gap_ble_dealloc_clcb(p_clcb);
556 }
557 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800558}
559
560/*******************************************************************************
561**
562** Function gap_ble_c_connect_cback
563**
564** Description Client connection callback.
565**
566** Returns void
567**
568*******************************************************************************/
569static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700570 BOOLEAN connected, tGATT_DISCONN_REASON reason,
571 tGATT_TRANSPORT transport)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800572{
573 tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (bda);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700574
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800575 UNUSED(gatt_if);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700576 UNUSED(transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800577
Satya Calloji444a8da2015-03-06 10:38:22 -0800578 if (p_clcb != NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800579 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800580 if (connected)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800581 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800582 p_clcb->conn_id = conn_id;
583 p_clcb->connected = TRUE;
584 /* start operation is pending */
585 gap_ble_send_cl_read_request(p_clcb);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800586 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800587 else
588 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800589 p_clcb->connected = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800590 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800591 /* clean up clcb */
592 gap_ble_dealloc_clcb(p_clcb);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800593 }
594 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800595}
596
597/*******************************************************************************
598**
599** Function gap_ble_c_cmpl_cback
600**
601** Description Client operation complete callback.
602**
603** Returns void
604**
605*******************************************************************************/
606static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
607
608{
609 tGAP_CLCB *p_clcb = gap_ble_find_clcb_by_conn_id(conn_id);
610 UINT16 op_type;
611 UINT16 min, max, latency, tout;
612 UINT16 len;
613 UINT8 *pp;
614
615 if (p_clcb == NULL)
616 return;
617
618 op_type = p_clcb->cl_op_uuid;
619
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700620 GAP_TRACE_EVENT ("gap_ble_c_cmpl_cback() - op_code: 0x%02x status: 0x%02x read_type: 0x%04x", op, status, op_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800621 /* Currently we only issue read commands */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700622 if (op != GATTC_OPTYPE_READ)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800623 return;
624
625 if (status != GATT_SUCCESS)
626 {
627 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
628 return;
629 }
630
631 pp = p_data->att_value.value;
632
633 switch (op_type)
634 {
635 case GATT_UUID_GAP_PREF_CONN_PARAM:
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700636 GAP_TRACE_EVENT ("GATT_UUID_GAP_PREF_CONN_PARAM");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800637 /* Extract the peripheral preferred connection parameters and save them */
638
639 STREAM_TO_UINT16 (min, pp);
640 STREAM_TO_UINT16 (max, pp);
641 STREAM_TO_UINT16 (latency, pp);
642 STREAM_TO_UINT16 (tout, pp);
643
644 BTM_BleSetPrefConnParams (p_clcb->bda, min, max, latency, tout);
645 /* release the connection here */
646 gap_ble_cl_op_cmpl(p_clcb, TRUE, 0, NULL);
647 break;
648
649 case GATT_UUID_GAP_DEVICE_NAME:
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700650 GAP_TRACE_EVENT ("GATT_UUID_GAP_DEVICE_NAME");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800651 len = (UINT16)strlen((char *)pp);
652 if (len > GAP_CHAR_DEV_NAME_SIZE)
653 len = GAP_CHAR_DEV_NAME_SIZE;
654 gap_ble_cl_op_cmpl(p_clcb, TRUE, len, pp);
655 break;
Satya Calloji444a8da2015-03-06 10:38:22 -0800656
657 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
658 gap_ble_cl_op_cmpl(p_clcb, TRUE, 1, pp);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800659 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800660 }
661}
662
Satya Calloji444a8da2015-03-06 10:38:22 -0800663
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800664/*******************************************************************************
665**
Satya Calloji444a8da2015-03-06 10:38:22 -0800666** Function gap_ble_accept_cl_operation
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800667**
Satya Calloji444a8da2015-03-06 10:38:22 -0800668** Description Start a process to read peer address resolution capability
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800669**
Satya Calloji444a8da2015-03-06 10:38:22 -0800670** Returns TRUE if request accepted
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800671**
672*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800673BOOLEAN gap_ble_accept_cl_operation(BD_ADDR peer_bda, UINT16 uuid, tGAP_BLE_CMPL_CBACK *p_cback)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800674{
Satya Calloji444a8da2015-03-06 10:38:22 -0800675 tGAP_CLCB *p_clcb;
676 BOOLEAN started = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800677
Satya Calloji444a8da2015-03-06 10:38:22 -0800678 if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM)
679 return(started);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800680
Satya Calloji444a8da2015-03-06 10:38:22 -0800681 if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800682 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800683 if ((p_clcb = gap_clcb_alloc(peer_bda)) == NULL)
684 {
685 GAP_TRACE_ERROR("gap_ble_accept_cl_operation max connection reached");
686 return started;
687 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800688 }
689
Satya Calloji444a8da2015-03-06 10:38:22 -0800690 GAP_TRACE_EVENT ("%s() - BDA: %08x%04x cl_op_uuid: 0x%04x",
691 __FUNCTION__,
692 (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
693 (peer_bda[4]<<8)+peer_bda[5], uuid);
694
695 if (GATT_GetConnIdIfConnected(gap_cb.gatt_if, peer_bda, &p_clcb->conn_id, BT_TRANSPORT_LE))
696 p_clcb->connected = TRUE;
697
698 /* hold the link here */
699 if (!GATT_Connect(gap_cb.gatt_if, p_clcb->bda, TRUE, BT_TRANSPORT_LE))
700 return started;
701
702 /* enqueue the request */
703 gap_ble_enqueue_request(p_clcb, uuid, p_cback);
704
705 if (p_clcb->connected && p_clcb->cl_op_uuid == 0)
706 started = gap_ble_send_cl_read_request(p_clcb);
707 else /* wait for connection up or pending operation to finish */
708 started = TRUE;
709
710 return started;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800711}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800712/*******************************************************************************
713**
714** Function GAP_BleReadPeerPrefConnParams
715**
716** Description Start a process to read a connected peripheral's preferred
717** connection parameters
718**
719** Returns TRUE if read started, else FALSE if GAP is busy
720**
721*******************************************************************************/
722BOOLEAN GAP_BleReadPeerPrefConnParams (BD_ADDR peer_bda)
723{
Satya Calloji444a8da2015-03-06 10:38:22 -0800724 return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM, NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800725}
726
727/*******************************************************************************
728**
729** Function GAP_BleReadPeerDevName
730**
731** Description Start a process to read a connected peripheral's device name.
732**
733** Returns TRUE if request accepted
734**
735*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800736BOOLEAN GAP_BleReadPeerDevName (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800737{
Satya Calloji444a8da2015-03-06 10:38:22 -0800738 return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_DEVICE_NAME, p_cback);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800739}
740
Satya Calloji444a8da2015-03-06 10:38:22 -0800741/*******************************************************************************
742**
743** Function GAP_BleReadPeerAddressResolutionCap
744**
745** Description Start a process to read peer address resolution capability
746**
747** Returns TRUE if request accepted
748**
749*******************************************************************************/
750BOOLEAN GAP_BleReadPeerAddressResolutionCap (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
751{
752 return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_CENTRAL_ADDR_RESOL, p_cback);
753}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700754
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800755/*******************************************************************************
756**
757** Function GAP_BleCancelReadPeerDevName
758**
759** Description Cancel reading a peripheral's device name.
760**
761** Returns TRUE if request accepted
762**
763*******************************************************************************/
764BOOLEAN GAP_BleCancelReadPeerDevName (BD_ADDR peer_bda)
765{
766 tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (peer_bda);
767
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700768 GAP_TRACE_EVENT ("GAP_BleCancelReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800769 (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
770 (peer_bda[4]<<8)+peer_bda[5], (p_clcb == NULL)? 0 : p_clcb->cl_op_uuid);
771
Satya Calloji444a8da2015-03-06 10:38:22 -0800772 if (p_clcb == NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800773 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700774 GAP_TRACE_ERROR ("Cannot cancel current op is not get dev name");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800775 return FALSE;
776 }
777
778 if (!p_clcb->connected)
779 {
780 if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, TRUE))
781 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700782 GAP_TRACE_ERROR ("Cannot cancel where No connection id");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800783 return FALSE;
784 }
785 }
786
787 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
788
789 return(TRUE);
790}
791
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800792#endif /* BLE_INCLUDED */
793
794
795
796
797