blob: 84103f0e80de0952773fb3cb9ff5b672f9b43ac0 [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"
31
32#define GAP_CHAR_ICON_SIZE 2
33#define GAP_CHAR_DEV_NAME_SIZE 248
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080034#define GAP_MAX_NUM_INC_SVR 0
35#define GAP_MAX_ATTR_NUM (2 * GAP_MAX_CHAR_NUM + GAP_MAX_NUM_INC_SVR + 1)
36#define GAP_MAX_CHAR_VALUE_SIZE (30 + GAP_CHAR_DEV_NAME_SIZE)
37
38
39#ifndef GAP_ATTR_DB_SIZE
40#define GAP_ATTR_DB_SIZE GATT_DB_MEM_SIZE(GAP_MAX_NUM_INC_SVR, GAP_MAX_CHAR_NUM, GAP_MAX_CHAR_VALUE_SIZE)
41#endif
42
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080043static void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE op_code, tGATTS_DATA *p_data);
44
45/* client connection callback */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070046static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected,
47 tGATT_DISCONN_REASON reason, tGATT_TRANSPORT transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080048static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data);
49
50static tGATT_CBACK gap_cback =
51{
52 gap_ble_c_connect_cback,
53 gap_ble_c_cmpl_cback,
54 NULL,
55 NULL,
Zhihai Xu7051db32013-11-12 20:18:37 -080056 gap_ble_s_attr_request_cback,
Andre Eisenbach17b04bd2014-03-28 14:54:22 -070057 NULL,
Zhihai Xu7051db32013-11-12 20:18:37 -080058 NULL
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080059};
60
61
62
63/*******************************************************************************
64**
65** Function gap_find_clcb_by_bd_addr
66**
67** Description The function searches all LCB with macthing bd address
68**
69** Returns total number of clcb found.
70**
71*******************************************************************************/
72tGAP_CLCB *gap_find_clcb_by_bd_addr(BD_ADDR bda)
73{
74 UINT8 i_clcb;
75 tGAP_CLCB *p_clcb = NULL;
76
77 for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
78 {
79 if (p_clcb->in_use && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN))
80 {
81 return p_clcb;
82 }
83 }
84
85 return NULL;
86}
87
88/*******************************************************************************
89**
90** Function gap_ble_find_clcb_by_conn_id
91**
92** Description The function searches all LCB with macthing connection ID
93**
94** Returns total number of clcb found.
95**
96*******************************************************************************/
97tGAP_CLCB *gap_ble_find_clcb_by_conn_id(UINT16 conn_id)
98{
99 UINT8 i_clcb;
100 tGAP_CLCB *p_clcb = NULL;
101
102 for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
103 {
104 if (p_clcb->in_use && p_clcb->connected && p_clcb->conn_id == conn_id)
105 {
106 return p_clcb;
107 }
108 }
109
110 return p_clcb;
111}
112
113/*******************************************************************************
114**
115** Function gap_clcb_alloc
116**
117** Description The function allocates a GAP connection link control block
118**
119** Returns NULL if not found. Otherwise pointer to the connection link block.
120**
121*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800122tGAP_CLCB *gap_clcb_alloc (BD_ADDR bda)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800123{
124 UINT8 i_clcb = 0;
125 tGAP_CLCB *p_clcb = NULL;
126
127 for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
128 {
129 if (!p_clcb->in_use)
130 {
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700131 fixed_queue_free(p_clcb->pending_req_q, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800132 memset(p_clcb, 0, sizeof(tGAP_CLCB));
133 p_clcb->in_use = TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800134 memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700135 p_clcb->pending_req_q = fixed_queue_new(SIZE_MAX);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800136 break;
137 }
138 }
139 return p_clcb;
140}
141
142/*******************************************************************************
143**
Satya Calloji444a8da2015-03-06 10:38:22 -0800144** Function gap_ble_dealloc_clcb
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800145**
Satya Calloji444a8da2015-03-06 10:38:22 -0800146** Description The function clean up the pending request queue in GAP
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800147**
Satya Calloji444a8da2015-03-06 10:38:22 -0800148** Returns none
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800149**
150*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800151void gap_ble_dealloc_clcb(tGAP_CLCB *p_clcb)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800152{
Satya Calloji444a8da2015-03-06 10:38:22 -0800153 tGAP_BLE_REQ *p_q;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800154
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700155 while ((p_q = (tGAP_BLE_REQ *)fixed_queue_try_dequeue(p_clcb->pending_req_q)) != NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800156 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800157 /* send callback to all pending requests if being removed*/
158 if (p_q->p_cback != NULL)
159 (*p_q->p_cback)(FALSE, p_clcb->bda, 0, NULL);
160
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700161 osi_freebuf (p_q);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800162 }
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700163 fixed_queue_free(p_clcb->pending_req_q, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800164
165 memset(p_clcb, 0, sizeof(tGAP_CLCB));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800166}
167
168/*******************************************************************************
169**
Satya Calloji444a8da2015-03-06 10:38:22 -0800170** Function gap_ble_enqueue_request
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800171**
Satya Calloji444a8da2015-03-06 10:38:22 -0800172** Description The function enqueue a GAP client request
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800173**
Satya Calloji444a8da2015-03-06 10:38:22 -0800174** Returns TRUE is successul; FALSE otherwise
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800175**
176*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800177BOOLEAN gap_ble_enqueue_request (tGAP_CLCB *p_clcb, UINT16 uuid, tGAP_BLE_CMPL_CBACK *p_cback)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800178{
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700179 tGAP_BLE_REQ *p_q = (tGAP_BLE_REQ *)osi_getbuf(sizeof(tGAP_BLE_REQ));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800180
Satya Calloji444a8da2015-03-06 10:38:22 -0800181 if (p_q != NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800182 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800183 p_q->p_cback = p_cback;
184 p_q->uuid = uuid;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700185 fixed_queue_enqueue(p_clcb->pending_req_q, p_q);
Satya Calloji444a8da2015-03-06 10:38:22 -0800186 return TRUE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800187 }
188
Satya Calloji444a8da2015-03-06 10:38:22 -0800189 return FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800190}
Satya Calloji444a8da2015-03-06 10:38:22 -0800191/*******************************************************************************
192**
193** Function gap_ble_dequeue_request
194**
195** Description The function dequeue a GAP client request if any
196**
197** Returns TRUE is successul; FALSE otherwise
198**
199*******************************************************************************/
200BOOLEAN gap_ble_dequeue_request (tGAP_CLCB *p_clcb, UINT16 * p_uuid, tGAP_BLE_CMPL_CBACK **p_cback)
201{
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700202 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 -0800203
Satya Calloji444a8da2015-03-06 10:38:22 -0800204 if (p_q != NULL)
205 {
206 *p_cback = p_q->p_cback;
207 *p_uuid = p_q->uuid;
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700208 osi_freebuf((void *)p_q);
Satya Calloji444a8da2015-03-06 10:38:22 -0800209 return TRUE;
210 }
211
212 return FALSE;
213}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800214
215/*******************************************************************************
216** GAP Attributes Database Request callback
217*******************************************************************************/
218tGATT_STATUS gap_read_attr_value (UINT16 handle, tGATT_VALUE *p_value, BOOLEAN is_long)
219{
220 tGAP_ATTR *p_db_attr = gap_cb.gatt_attr;
221 UINT8 *p = p_value->value, i;
222 UINT16 offset = p_value->offset;
223 UINT8 *p_dev_name = NULL;
224
225 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
226 {
227 if (handle == p_db_attr->handle)
228 {
229 if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME &&
230 is_long == TRUE)
231 return GATT_NOT_LONG;
232
233 switch (p_db_attr->uuid)
234 {
235 case GATT_UUID_GAP_DEVICE_NAME:
236 BTM_ReadLocalDeviceName((char **)&p_dev_name);
237 if (strlen ((char *)p_dev_name) > GATT_MAX_ATTR_LEN)
238 p_value->len = GATT_MAX_ATTR_LEN;
239 else
240 p_value->len = (UINT16)strlen ((char *)p_dev_name);
241
242 if (offset > p_value->len)
243 return GATT_INVALID_OFFSET;
244 else
245 {
246 p_value->len -= offset;
247 p_dev_name += offset;
248 ARRAY_TO_STREAM(p, p_dev_name, p_value->len);
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700249 GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME len=0x%04x", p_value->len);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800250 }
251 break;
252
253 case GATT_UUID_GAP_ICON:
254 UINT16_TO_STREAM(p, p_db_attr->attr_value.icon);
255 p_value->len = 2;
256 break;
257
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800258 case GATT_UUID_GAP_PREF_CONN_PARAM:
259 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_min); /* int_min */
260 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_max); /* int_max */
261 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.latency); /* latency */
262 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.sp_tout); /* sp_tout */
263 p_value->len =8;
264 break;
Satya Calloji444a8da2015-03-06 10:38:22 -0800265
266 /* address resolution */
267 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
268 UINT8_TO_STREAM(p, p_db_attr->attr_value.addr_resolution);
269 p_value->len =1;
270 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800271 }
272 return GATT_SUCCESS;
273 }
274 }
275 return GATT_NOT_FOUND;
276}
277
278/*******************************************************************************
279** GAP Attributes Database Read/Read Blob Request process
280*******************************************************************************/
281tGATT_STATUS gap_proc_read (tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp)
282{
283 tGATT_STATUS status = GATT_NO_RESOURCES;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800284 UNUSED(type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800285
286 if (p_data->is_long)
287 p_rsp->attr_value.offset = p_data->offset;
288
289 p_rsp->attr_value.handle = p_data->handle;
290
291 status = gap_read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long);
292
293 return status;
294}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800295
296/******************************************************************************
297**
298** Function gap_proc_write_req
299**
300** Description GAP ATT server process a write request.
301**
302** Returns void.
303**
304*******************************************************************************/
305UINT8 gap_proc_write_req( tGATTS_REQ_TYPE type, tGATT_WRITE_REQ *p_data)
306{
307 tGAP_ATTR *p_db_attr = gap_cb.gatt_attr;
308 UINT8 i;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800309 UNUSED(type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800310
311 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
312 {
313 if (p_data-> handle == p_db_attr->handle)
314 {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700315 return GATT_WRITE_NOT_PERMIT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800316 }
317 }
318 return GATT_NOT_FOUND;
319
320}
321
322/******************************************************************************
323**
324** Function gap_ble_s_attr_request_cback
325**
326** Description GAP ATT server attribute access request callback.
327**
328** Returns void.
329**
330*******************************************************************************/
331void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id,
332 tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
333{
334 UINT8 status = GATT_INVALID_PDU;
335 tGATTS_RSP rsp_msg;
336 BOOLEAN ignore = FALSE;
337
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700338 GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800339
340 memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
341
342 switch (type)
343 {
344 case GATTS_REQ_TYPE_READ:
345 status = gap_proc_read(type, &p_data->read_req, &rsp_msg);
346 break;
347
348 case GATTS_REQ_TYPE_WRITE:
349 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}};
386 tBT_UUID uuid = {LEN_UUID_16,{UUID_SERVCLASS_GAP_SERVER}};
387 UINT16 service_handle;
388 tGAP_ATTR *p_db_attr = &gap_cb.gatt_attr[0];
389 tGATT_STATUS status;
390
391 /* Fill our internal UUID with a fixed pattern 0x82 */
392 memset (&app_uuid.uu.uuid128, 0x82, LEN_UUID_128);
393 memset(gap_cb.gatt_attr, 0, sizeof(tGAP_ATTR) *GAP_MAX_CHAR_NUM);
394
395 gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback);
396
397 GATT_StartIf(gap_cb.gatt_if);
398
399 /* Create a GAP service */
400 service_handle = GATTS_CreateService (gap_cb.gatt_if, &uuid, 0, GAP_MAX_ATTR_NUM, TRUE);
401
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700402 GAP_TRACE_EVENT ("gap_attr_db_init service_handle = %d", service_handle);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800403
404 /* add Device Name Characteristic
405 */
406 uuid.len = LEN_UUID_16;
407 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_DEVICE_NAME;
408 p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ);
409 p_db_attr ++;
410
411 /* add Icon characteristic
412 */
413 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_ICON;
414 p_db_attr->handle = GATTS_AddCharacteristic(service_handle,
415 &uuid,
416 GATT_PERM_READ,
417 GATT_CHAR_PROP_BIT_READ);
418 p_db_attr ++;
419
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700420#if BTM_PERIPHERAL_ENABLED == TRUE /* Only needed for peripheral testing */
421 /* add preferred connection parameter characteristic
422 */
423 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
424 p_db_attr->attr_value.conn_param.int_max = GAP_PREFER_CONN_INT_MAX; /* 6 */
425 p_db_attr->attr_value.conn_param.int_min = GAP_PREFER_CONN_INT_MIN; /* 0 */
426 p_db_attr->attr_value.conn_param.latency = GAP_PREFER_CONN_LATENCY; /* 0 */
427 p_db_attr->attr_value.conn_param.sp_tout = GAP_PREFER_CONN_SP_TOUT; /* 2000 */
428 p_db_attr->handle = GATTS_AddCharacteristic(service_handle,
429 &uuid,
430 GATT_PERM_READ,
431 GATT_CHAR_PROP_BIT_READ);
432 p_db_attr ++;
433#endif
434
Satya Calloji444a8da2015-03-06 10:38:22 -0800435 /* add Central address resolution Characteristic */
436 uuid.len = LEN_UUID_16;
437 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL;
438 p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid,
439 GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ);
440 p_db_attr->attr_value.addr_resolution = 0;
441 p_db_attr++;
442
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800443 /* start service now */
444 memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
445
446 status = GATTS_StartService(gap_cb.gatt_if, service_handle, GAP_TRANSPORT_SUPPORTED );
447
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700448 GAP_TRACE_EVENT ("GAP App gatt_if: %d s_hdl = %d start_status=%d",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800449 gap_cb.gatt_if, service_handle, status);
450
451
452
453}
454
455/*******************************************************************************
456**
457** Function GAP_BleAttrDBUpdate
458**
459** Description GAP ATT database update.
460**
461** Returns void.
462**
463*******************************************************************************/
464void GAP_BleAttrDBUpdate(UINT16 attr_uuid, tGAP_BLE_ATTR_VALUE *p_value)
465{
466 tGAP_ATTR *p_db_attr = gap_cb.gatt_attr;
467 UINT8 i = 0;
468
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700469 GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x", attr_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800470
471 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
472 {
473 if (p_db_attr->uuid == attr_uuid)
474 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700475 GAP_TRACE_EVENT("Found attr_uuid=0x%04x", attr_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800476
477 switch (attr_uuid)
478 {
479 case GATT_UUID_GAP_ICON:
480 p_db_attr->attr_value.icon = p_value->icon;
481 break;
482
483 case GATT_UUID_GAP_PREF_CONN_PARAM:
Satya Calloji444a8da2015-03-06 10:38:22 -0800484 memcpy((void *)&p_db_attr->attr_value.conn_param,
485 (const void *)&p_value->conn_param, sizeof(tGAP_BLE_PREF_PARAM));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800486 break;
487
488 case GATT_UUID_GAP_DEVICE_NAME:
489 BTM_SetLocalDeviceName((char *)p_value->p_dev_name);
490 break;
491
Satya Calloji444a8da2015-03-06 10:38:22 -0800492 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
493 p_db_attr->attr_value.addr_resolution = p_value->addr_resolution;
494 break;
495
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800496 }
497 break;
498 }
499 }
500
501 return;
502}
503
504/*******************************************************************************
505**
Satya Calloji444a8da2015-03-06 10:38:22 -0800506** Function gap_ble_send_cl_read_request
507**
508** Description utility function to send a read request for a GAP charactersitic
509**
510** Returns TRUE if read started, else FALSE if GAP is busy
511**
512*******************************************************************************/
513BOOLEAN gap_ble_send_cl_read_request(tGAP_CLCB *p_clcb)
514{
515 tGATT_READ_PARAM param;
516 UINT16 uuid = 0;
517 BOOLEAN started = FALSE;
518
519 if (gap_ble_dequeue_request(p_clcb, &uuid, &p_clcb->p_cback))
520 {
521 memset(&param, 0, sizeof(tGATT_READ_PARAM));
522
523 param.service.uuid.len = LEN_UUID_16;
524 param.service.uuid.uu.uuid16 = uuid;
525 param.service.s_handle = 1;
526 param.service.e_handle = 0xFFFF;
527 param.service.auth_req = 0;
528
529 if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, &param) == GATT_SUCCESS)
530 {
531 p_clcb->cl_op_uuid = uuid;
532 started = TRUE;
533 }
534 }
535
536 return started;
537}
538
539/*******************************************************************************
540**
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800541** Function gap_ble_cl_op_cmpl
542**
543** Description GAP client operation complete callback
544**
545** Returns void
546**
547*******************************************************************************/
548void gap_ble_cl_op_cmpl(tGAP_CLCB *p_clcb, BOOLEAN status, UINT16 len, UINT8 *p_name)
549{
Satya Calloji444a8da2015-03-06 10:38:22 -0800550 tGAP_BLE_CMPL_CBACK *p_cback = p_clcb->p_cback;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800551 UINT16 op = p_clcb->cl_op_uuid;
552
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700553 GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800554
555 p_clcb->cl_op_uuid = 0;
556 p_clcb->p_cback=NULL;
557
Satya Calloji444a8da2015-03-06 10:38:22 -0800558 if (p_cback && op)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800559 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700560 GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl");
Satya Calloji444a8da2015-03-06 10:38:22 -0800561 (* p_cback)(status, p_clcb->bda, len, (char *)p_name);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800562 }
563
Satya Calloji444a8da2015-03-06 10:38:22 -0800564 /* if no further activity is requested in callback, drop the link */
565 if (p_clcb->connected)
566 {
567 if (!gap_ble_send_cl_read_request(p_clcb))
568 {
569 GATT_Disconnect(p_clcb->conn_id);
570 gap_ble_dealloc_clcb(p_clcb);
571 }
572 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800573}
574
575/*******************************************************************************
576**
577** Function gap_ble_c_connect_cback
578**
579** Description Client connection callback.
580**
581** Returns void
582**
583*******************************************************************************/
584static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700585 BOOLEAN connected, tGATT_DISCONN_REASON reason,
586 tGATT_TRANSPORT transport)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800587{
588 tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (bda);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700589
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800590 UNUSED(gatt_if);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700591 UNUSED(transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800592
Satya Calloji444a8da2015-03-06 10:38:22 -0800593 if (p_clcb != NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800594 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800595 if (connected)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800596 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800597 p_clcb->conn_id = conn_id;
598 p_clcb->connected = TRUE;
599 /* start operation is pending */
600 gap_ble_send_cl_read_request(p_clcb);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800601 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800602 else
603 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800604 p_clcb->connected = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800605 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800606 /* clean up clcb */
607 gap_ble_dealloc_clcb(p_clcb);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800608 }
609 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800610}
611
612/*******************************************************************************
613**
614** Function gap_ble_c_cmpl_cback
615**
616** Description Client operation complete callback.
617**
618** Returns void
619**
620*******************************************************************************/
621static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
622
623{
624 tGAP_CLCB *p_clcb = gap_ble_find_clcb_by_conn_id(conn_id);
625 UINT16 op_type;
626 UINT16 min, max, latency, tout;
627 UINT16 len;
628 UINT8 *pp;
629
630 if (p_clcb == NULL)
631 return;
632
633 op_type = p_clcb->cl_op_uuid;
634
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700635 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 -0800636 /* Currently we only issue read commands */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700637 if (op != GATTC_OPTYPE_READ)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800638 return;
639
640 if (status != GATT_SUCCESS)
641 {
642 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
643 return;
644 }
645
646 pp = p_data->att_value.value;
647
648 switch (op_type)
649 {
650 case GATT_UUID_GAP_PREF_CONN_PARAM:
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700651 GAP_TRACE_EVENT ("GATT_UUID_GAP_PREF_CONN_PARAM");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800652 /* Extract the peripheral preferred connection parameters and save them */
653
654 STREAM_TO_UINT16 (min, pp);
655 STREAM_TO_UINT16 (max, pp);
656 STREAM_TO_UINT16 (latency, pp);
657 STREAM_TO_UINT16 (tout, pp);
658
659 BTM_BleSetPrefConnParams (p_clcb->bda, min, max, latency, tout);
660 /* release the connection here */
661 gap_ble_cl_op_cmpl(p_clcb, TRUE, 0, NULL);
662 break;
663
664 case GATT_UUID_GAP_DEVICE_NAME:
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700665 GAP_TRACE_EVENT ("GATT_UUID_GAP_DEVICE_NAME");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800666 len = (UINT16)strlen((char *)pp);
667 if (len > GAP_CHAR_DEV_NAME_SIZE)
668 len = GAP_CHAR_DEV_NAME_SIZE;
669 gap_ble_cl_op_cmpl(p_clcb, TRUE, len, pp);
670 break;
Satya Calloji444a8da2015-03-06 10:38:22 -0800671
672 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
673 gap_ble_cl_op_cmpl(p_clcb, TRUE, 1, pp);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800674 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800675 }
676}
677
Satya Calloji444a8da2015-03-06 10:38:22 -0800678
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800679/*******************************************************************************
680**
Satya Calloji444a8da2015-03-06 10:38:22 -0800681** Function gap_ble_accept_cl_operation
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800682**
Satya Calloji444a8da2015-03-06 10:38:22 -0800683** Description Start a process to read peer address resolution capability
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800684**
Satya Calloji444a8da2015-03-06 10:38:22 -0800685** Returns TRUE if request accepted
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800686**
687*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800688BOOLEAN 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 -0800689{
Satya Calloji444a8da2015-03-06 10:38:22 -0800690 tGAP_CLCB *p_clcb;
691 BOOLEAN started = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800692
Satya Calloji444a8da2015-03-06 10:38:22 -0800693 if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM)
694 return(started);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800695
Satya Calloji444a8da2015-03-06 10:38:22 -0800696 if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800697 {
Satya Calloji444a8da2015-03-06 10:38:22 -0800698 if ((p_clcb = gap_clcb_alloc(peer_bda)) == NULL)
699 {
700 GAP_TRACE_ERROR("gap_ble_accept_cl_operation max connection reached");
701 return started;
702 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800703 }
704
Satya Calloji444a8da2015-03-06 10:38:22 -0800705 GAP_TRACE_EVENT ("%s() - BDA: %08x%04x cl_op_uuid: 0x%04x",
706 __FUNCTION__,
707 (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
708 (peer_bda[4]<<8)+peer_bda[5], uuid);
709
710 if (GATT_GetConnIdIfConnected(gap_cb.gatt_if, peer_bda, &p_clcb->conn_id, BT_TRANSPORT_LE))
711 p_clcb->connected = TRUE;
712
713 /* hold the link here */
714 if (!GATT_Connect(gap_cb.gatt_if, p_clcb->bda, TRUE, BT_TRANSPORT_LE))
715 return started;
716
717 /* enqueue the request */
718 gap_ble_enqueue_request(p_clcb, uuid, p_cback);
719
720 if (p_clcb->connected && p_clcb->cl_op_uuid == 0)
721 started = gap_ble_send_cl_read_request(p_clcb);
722 else /* wait for connection up or pending operation to finish */
723 started = TRUE;
724
725 return started;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800726}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800727/*******************************************************************************
728**
729** Function GAP_BleReadPeerPrefConnParams
730**
731** Description Start a process to read a connected peripheral's preferred
732** connection parameters
733**
734** Returns TRUE if read started, else FALSE if GAP is busy
735**
736*******************************************************************************/
737BOOLEAN GAP_BleReadPeerPrefConnParams (BD_ADDR peer_bda)
738{
Satya Calloji444a8da2015-03-06 10:38:22 -0800739 return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM, NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800740}
741
742/*******************************************************************************
743**
744** Function GAP_BleReadPeerDevName
745**
746** Description Start a process to read a connected peripheral's device name.
747**
748** Returns TRUE if request accepted
749**
750*******************************************************************************/
Satya Calloji444a8da2015-03-06 10:38:22 -0800751BOOLEAN GAP_BleReadPeerDevName (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800752{
Satya Calloji444a8da2015-03-06 10:38:22 -0800753 return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_DEVICE_NAME, p_cback);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800754}
755
Satya Calloji444a8da2015-03-06 10:38:22 -0800756/*******************************************************************************
757**
758** Function GAP_BleReadPeerAddressResolutionCap
759**
760** Description Start a process to read peer address resolution capability
761**
762** Returns TRUE if request accepted
763**
764*******************************************************************************/
765BOOLEAN GAP_BleReadPeerAddressResolutionCap (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
766{
767 return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_CENTRAL_ADDR_RESOL, p_cback);
768}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700769
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800770/*******************************************************************************
771**
772** Function GAP_BleCancelReadPeerDevName
773**
774** Description Cancel reading a peripheral's device name.
775**
776** Returns TRUE if request accepted
777**
778*******************************************************************************/
779BOOLEAN GAP_BleCancelReadPeerDevName (BD_ADDR peer_bda)
780{
781 tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (peer_bda);
782
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700783 GAP_TRACE_EVENT ("GAP_BleCancelReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x",
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800784 (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
785 (peer_bda[4]<<8)+peer_bda[5], (p_clcb == NULL)? 0 : p_clcb->cl_op_uuid);
786
Satya Calloji444a8da2015-03-06 10:38:22 -0800787 if (p_clcb == NULL)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800788 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700789 GAP_TRACE_ERROR ("Cannot cancel current op is not get dev name");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800790 return FALSE;
791 }
792
793 if (!p_clcb->connected)
794 {
795 if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, TRUE))
796 {
Sharvil Nanavatid5bba902014-05-04 01:33:15 -0700797 GAP_TRACE_ERROR ("Cannot cancel where No connection id");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800798 return FALSE;
799 }
800 }
801
802 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
803
804 return(TRUE);
805}
806
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800807#endif /* BLE_INCLUDED */
808
809
810
811
812