1 /******************************************************************************
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 ******************************************************************************/
18 #include "common/bt_target.h"
19
20 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
21
22 #include "common/bt_defs.h"
23 #include "osi/allocator.h"
24 #include <string.h>
25 #include "gap_int.h"
26 #include "stack/gap_api.h"
27 #include "stack/gattdefs.h"
28 #include "stack/gatt_api.h"
29 #include "gatt_int.h"
30 #include "btm_int.h"
31 #include "stack/hcimsgs.h"
32 #include "stack/sdpdefs.h"
33
34 #define GAP_CHAR_ICON_SIZE 2
35 #define GAP_CHAR_DEV_NAME_SIZE 248
36 #define GAP_MAX_NUM_INC_SVR 0
37 #define GAP_MAX_ATTR_NUM (2 * GAP_MAX_CHAR_NUM + GAP_MAX_NUM_INC_SVR + 1)
38 #define GAP_MAX_CHAR_VALUE_SIZE (30 + GAP_CHAR_DEV_NAME_SIZE)
39
40
41 #ifndef GAP_ATTR_DB_SIZE
42 #define GAP_ATTR_DB_SIZE GATT_DB_MEM_SIZE(GAP_MAX_NUM_INC_SVR, GAP_MAX_CHAR_NUM, GAP_MAX_CHAR_VALUE_SIZE)
43 #endif
44
45 static void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE op_code, tGATTS_DATA *p_data);
46
47 /* client connection callback */
48 static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected,
49 tGATT_DISCONN_REASON reason, tGATT_TRANSPORT transport);
50 static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data);
51
52 static const tGATT_CBACK gap_cback = {
53 gap_ble_c_connect_cback,
54 gap_ble_c_cmpl_cback,
55 NULL,
56 NULL,
57 gap_ble_s_attr_request_cback,
58 NULL,
59 NULL
60 };
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 *******************************************************************************/
gap_find_clcb_by_bd_addr(BD_ADDR bda)73 tGAP_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 if (p_clcb->in_use && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN)) {
80 return p_clcb;
81 }
82 }
83
84 return NULL;
85 }
86
87 /*******************************************************************************
88 **
89 ** Function gap_ble_find_clcb_by_conn_id
90 **
91 ** Description The function searches all LCB with macthing connection ID
92 **
93 ** Returns total number of clcb found.
94 **
95 *******************************************************************************/
gap_ble_find_clcb_by_conn_id(UINT16 conn_id)96 tGAP_CLCB *gap_ble_find_clcb_by_conn_id(UINT16 conn_id)
97 {
98 UINT8 i_clcb;
99 tGAP_CLCB *p_clcb = NULL;
100
101 for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++) {
102 if (p_clcb->in_use && p_clcb->connected && p_clcb->conn_id == conn_id) {
103 return p_clcb;
104 }
105 }
106
107 return p_clcb;
108 }
109
110 /*******************************************************************************
111 **
112 ** Function gap_clcb_alloc
113 **
114 ** Description The function allocates a GAP connection link control block
115 **
116 ** Returns NULL if not found. Otherwise pointer to the connection link block.
117 **
118 *******************************************************************************/
gap_clcb_alloc(BD_ADDR bda)119 tGAP_CLCB *gap_clcb_alloc (BD_ADDR bda)
120 {
121 UINT8 i_clcb = 0;
122 tGAP_CLCB *p_clcb = NULL;
123
124 for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++) {
125 if (!p_clcb->in_use) {
126 memset(p_clcb, 0, sizeof(tGAP_CLCB));
127 p_clcb->in_use = TRUE;
128 memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
129 break;
130 }
131 }
132 return p_clcb;
133 }
134
135 /*******************************************************************************
136 **
137 ** Function gap_ble_dealloc_clcb
138 **
139 ** Description The function clean up the pending request queue in GAP
140 **
141 ** Returns none
142 **
143 *******************************************************************************/
gap_ble_dealloc_clcb(tGAP_CLCB * p_clcb)144 void gap_ble_dealloc_clcb(tGAP_CLCB *p_clcb)
145 {
146 tGAP_BLE_REQ *p_q;
147
148 while ((p_q = (tGAP_BLE_REQ *)fixed_queue_dequeue(p_clcb->pending_req_q, 0)) != NULL) {
149 /* send callback to all pending requests if being removed*/
150 if (p_q->p_cback != NULL) {
151 (*p_q->p_cback)(FALSE, p_clcb->bda, 0, NULL);
152 }
153
154 osi_free (p_q);
155 }
156
157 memset(p_clcb, 0, sizeof(tGAP_CLCB));
158 }
159
160 /*******************************************************************************
161 **
162 ** Function gap_ble_enqueue_request
163 **
164 ** Description The function enqueue a GAP client request
165 **
166 ** Returns TRUE is successul; FALSE otherwise
167 **
168 *******************************************************************************/
gap_ble_enqueue_request(tGAP_CLCB * p_clcb,UINT16 uuid,tGAP_BLE_CMPL_CBACK * p_cback)169 BOOLEAN gap_ble_enqueue_request (tGAP_CLCB *p_clcb, UINT16 uuid, tGAP_BLE_CMPL_CBACK *p_cback)
170 {
171 tGAP_BLE_REQ *p_q = (tGAP_BLE_REQ *)osi_malloc(sizeof(tGAP_BLE_REQ));
172
173 if (p_q != NULL) {
174 p_q->p_cback = p_cback;
175 p_q->uuid = uuid;
176 fixed_queue_enqueue(p_clcb->pending_req_q, p_q, FIXED_QUEUE_MAX_TIMEOUT);
177 return TRUE;
178 }
179
180 return FALSE;
181 }
182 /*******************************************************************************
183 **
184 ** Function gap_ble_dequeue_request
185 **
186 ** Description The function dequeue a GAP client request if any
187 **
188 ** Returns TRUE is successul; FALSE otherwise
189 **
190 *******************************************************************************/
gap_ble_dequeue_request(tGAP_CLCB * p_clcb,UINT16 * p_uuid,tGAP_BLE_CMPL_CBACK ** p_cback)191 BOOLEAN gap_ble_dequeue_request (tGAP_CLCB *p_clcb, UINT16 *p_uuid, tGAP_BLE_CMPL_CBACK **p_cback)
192 {
193 tGAP_BLE_REQ *p_q = (tGAP_BLE_REQ *)fixed_queue_dequeue(p_clcb->pending_req_q, 0);;
194
195 if (p_q != NULL) {
196 *p_cback = p_q->p_cback;
197 *p_uuid = p_q->uuid;
198 osi_free((void *)p_q);
199 return TRUE;
200 }
201
202 return FALSE;
203 }
204
205 /*******************************************************************************
206 ** GAP Attributes Database Request callback
207 *******************************************************************************/
gap_read_attr_value(UINT16 handle,tGATT_VALUE * p_value,BOOLEAN is_long)208 tGATT_STATUS gap_read_attr_value (UINT16 handle, tGATT_VALUE *p_value, BOOLEAN is_long)
209 {
210 tGAP_ATTR *p_db_attr = gap_cb.gap_attr;
211 UINT8 *p = p_value->value, i;
212 UINT16 offset = p_value->offset;
213 UINT8 *p_dev_name = NULL;
214
215 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++) {
216 if (handle == p_db_attr->handle) {
217 if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME &&
218 is_long == TRUE) {
219 return GATT_NOT_LONG;
220 }
221
222 switch (p_db_attr->uuid) {
223 case GATT_UUID_GAP_DEVICE_NAME:
224 BTM_ReadLocalDeviceName((char **)&p_dev_name);
225 if (strlen ((char *)p_dev_name) > GATT_MAX_ATTR_LEN) {
226 p_value->len = GATT_MAX_ATTR_LEN;
227 } else {
228 p_value->len = (UINT16)strlen ((char *)p_dev_name);
229 }
230
231 if (offset > p_value->len) {
232 return GATT_INVALID_OFFSET;
233 } else {
234 p_value->len -= offset;
235 p_dev_name += offset;
236 ARRAY_TO_STREAM(p, p_dev_name, p_value->len);
237 GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME len=0x%04x", p_value->len);
238 }
239 break;
240
241 case GATT_UUID_GAP_ICON:
242 UINT16_TO_STREAM(p, p_db_attr->attr_value.icon);
243 p_value->len = 2;
244 break;
245
246 case GATT_UUID_GAP_PREF_CONN_PARAM:
247 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_min); /* int_min */
248 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_max); /* int_max */
249 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.latency); /* latency */
250 UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.sp_tout); /* sp_tout */
251 p_value->len = 8;
252 break;
253
254 /* address resolution */
255 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
256 UINT8_TO_STREAM(p, p_db_attr->attr_value.addr_resolution);
257 p_value->len = 1;
258 break;
259 }
260 return GATT_SUCCESS;
261 }
262 }
263 return GATT_NOT_FOUND;
264 }
265
266 /*******************************************************************************
267 ** GAP Attributes Database Read/Read Blob Request process
268 *******************************************************************************/
gap_proc_read(tGATTS_REQ_TYPE type,tGATT_READ_REQ * p_data,tGATTS_RSP * p_rsp)269 tGATT_STATUS gap_proc_read (tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp)
270 {
271 tGATT_STATUS status = GATT_NO_RESOURCES;
272 UNUSED(type);
273
274 if (p_data->is_long) {
275 p_rsp->attr_value.offset = p_data->offset;
276 }
277
278 p_rsp->attr_value.handle = p_data->handle;
279
280 status = gap_read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long);
281
282 return status;
283 }
284
285 /******************************************************************************
286 **
287 ** Function gap_proc_write_req
288 **
289 ** Description GAP ATT server process a write request.
290 **
291 ** Returns void.
292 **
293 *******************************************************************************/
gap_proc_write_req(tGATTS_REQ_TYPE type,tGATT_WRITE_REQ * p_data)294 UINT8 gap_proc_write_req( tGATTS_REQ_TYPE type, tGATT_WRITE_REQ *p_data)
295 {
296 tGAP_ATTR *p_db_attr = gap_cb.gap_attr;
297 UINT8 i;
298 UNUSED(type);
299
300 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++) {
301 if (p_data->handle == p_db_attr->handle) {
302 switch (p_db_attr->uuid) {
303 #if (GATTS_DEVICE_NAME_WRITABLE == TRUE)
304 case GATT_UUID_GAP_DEVICE_NAME: {
305 UINT8 *p_val = p_data->value;
306 p_val[p_data->len] = '\0';
307 BTM_SetLocalDeviceName((char *)p_val);
308 return GATT_SUCCESS;
309 }
310 #endif
311 #if (GATTS_APPEARANCE_WRITABLE == TRUE)
312 case GATT_UUID_GAP_ICON: {
313 UINT8 *p_val = p_data->value;
314 if (p_data->len != sizeof(UINT16)) {
315 return GATT_INVALID_ATTR_LEN;
316 }
317 STREAM_TO_UINT16(p_db_attr->attr_value.icon, p_val);
318 return GATT_SUCCESS;
319 }
320 #endif
321 default:
322 break;
323 }
324 return GATT_WRITE_NOT_PERMIT;
325 }
326 }
327
328 return GATT_NOT_FOUND;
329 }
330
331 /******************************************************************************
332 **
333 ** Function gap_ble_s_attr_request_cback
334 **
335 ** Description GAP ATT server attribute access request callback.
336 **
337 ** Returns void.
338 **
339 *******************************************************************************/
gap_ble_s_attr_request_cback(UINT16 conn_id,UINT32 trans_id,tGATTS_REQ_TYPE type,tGATTS_DATA * p_data)340 void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id,
341 tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
342 {
343 UINT8 status = GATT_INVALID_PDU;
344 tGATTS_RSP rsp_msg;
345 BOOLEAN ignore = FALSE;
346
347 GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type);
348
349 memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
350
351 switch (type) {
352 case GATTS_REQ_TYPE_READ:
353 status = gap_proc_read(type, &p_data->read_req, &rsp_msg);
354 break;
355
356 case GATTS_REQ_TYPE_WRITE:
357 if (!p_data->write_req.need_rsp) {
358 ignore = TRUE;
359 }
360
361 status = gap_proc_write_req(type, &p_data->write_req);
362 break;
363
364 case GATTS_REQ_TYPE_WRITE_EXEC:
365 ignore = TRUE;
366 GAP_TRACE_EVENT("Ignore GATTS_REQ_TYPE_WRITE_EXEC" );
367 break;
368
369 case GATTS_REQ_TYPE_MTU:
370 GAP_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
371 ignore = TRUE;
372 break;
373
374 default:
375 GAP_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
376 break;
377 }
378
379 if (!ignore) {
380 GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
381 }
382 }
383
384 /*******************************************************************************
385 **
386 ** Function btm_ble_att_db_init
387 **
388 ** Description GAP ATT database initalization.
389 **
390 ** Returns void.
391 **
392 *******************************************************************************/
gap_attr_db_init(void)393 void gap_attr_db_init(void)
394 {
395 tBT_UUID app_uuid = {LEN_UUID_128, {0}};
396 tBT_UUID uuid = {LEN_UUID_16, {UUID_SERVCLASS_GAP_SERVER}};
397 UINT16 service_handle;
398 tGAP_ATTR *p_db_attr = &gap_cb.gap_attr[0];
399 tGATT_STATUS status;
400
401 /* Fill our internal UUID with a fixed pattern 0x82 */
402 memset (&app_uuid.uu.uuid128, 0x82, LEN_UUID_128);
403 memset(gap_cb.gap_attr, 0, sizeof(tGAP_ATTR) *GAP_MAX_CHAR_NUM);
404
405 gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback);
406
407 GATT_StartIf(gap_cb.gatt_if);
408
409 /* Create a GAP service */
410 service_handle = GATTS_CreateService (gap_cb.gatt_if, &uuid, 0, GAP_MAX_ATTR_NUM, TRUE);
411
412 GAP_TRACE_EVENT ("gap_attr_db_init service_handle = %d", service_handle);
413
414 /* add Device Name Characteristic
415 */
416 uuid.len = LEN_UUID_16;
417 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_DEVICE_NAME;
418 p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid,
419 #if (GATTS_DEVICE_NAME_WRITABLE == TRUE)
420 GATT_PERM_READ | GATT_PERM_WRITE,
421 GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE_NR,
422 #else
423 GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
424 #endif
425 NULL, NULL);
426 p_db_attr ++;
427
428 /* add Icon characteristic
429 */
430 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_ICON;
431 p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid,
432 #if (GATTS_APPEARANCE_WRITABLE == TRUE)
433 GATT_PERM_READ | GATT_PERM_WRITE,
434 GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE_NR,
435 #else
436 GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
437 #endif
438 NULL, NULL);
439 p_db_attr ++;
440
441 #if ((defined BTM_PERIPHERAL_ENABLED) && (BTM_PERIPHERAL_ENABLED == TRUE))
442 /* Only needed for peripheral testing */
443 /* add preferred connection parameter characteristic
444 */
445 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
446 p_db_attr->attr_value.conn_param.int_max = GAP_PREFER_CONN_INT_MAX; /* 6 */
447 p_db_attr->attr_value.conn_param.int_min = GAP_PREFER_CONN_INT_MIN; /* 0 */
448 p_db_attr->attr_value.conn_param.latency = GAP_PREFER_CONN_LATENCY; /* 0 */
449 p_db_attr->attr_value.conn_param.sp_tout = GAP_PREFER_CONN_SP_TOUT; /* 2000 */
450 p_db_attr->handle = GATTS_AddCharacteristic(service_handle,
451 &uuid,
452 GATT_PERM_READ,
453 GATT_CHAR_PROP_BIT_READ,
454 NULL, NULL);
455 p_db_attr ++;
456 #endif
457
458 /* add Central address resolution Characteristic */
459 uuid.len = LEN_UUID_16;
460 uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL;
461 p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid,
462 GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
463 NULL, NULL);
464 p_db_attr->attr_value.addr_resolution = 0;
465 p_db_attr++;
466
467 /* start service now */
468 memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
469
470 status = GATTS_StartService(gap_cb.gatt_if, service_handle, GAP_TRANSPORT_SUPPORTED );
471 #if (CONFIG_BT_STACK_NO_LOG)
472 (void) status;
473 #endif
474
475 GAP_TRACE_EVENT ("GAP App gatt_if: %d s_hdl = %d start_status=%d",
476 gap_cb.gatt_if, service_handle, status);
477
478
479
480 }
481
482 /*******************************************************************************
483 **
484 ** Function GAP_BleAttrDBUpdate
485 **
486 ** Description GAP ATT database update.
487 **
488 ** Returns void.
489 **
490 *******************************************************************************/
GAP_BleAttrDBUpdate(UINT16 attr_uuid,tGAP_BLE_ATTR_VALUE * p_value)491 void GAP_BleAttrDBUpdate(UINT16 attr_uuid, tGAP_BLE_ATTR_VALUE *p_value)
492 {
493 tGAP_ATTR *p_db_attr = gap_cb.gap_attr;
494 UINT8 i = 0;
495
496 GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x\n", attr_uuid);
497
498 for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++) {
499 if (p_db_attr->uuid == attr_uuid) {
500 GAP_TRACE_EVENT("Found attr_uuid=0x%04x\n", attr_uuid);
501
502 switch (attr_uuid) {
503 case GATT_UUID_GAP_ICON:
504 p_db_attr->attr_value.icon = p_value->icon;
505 break;
506
507 case GATT_UUID_GAP_PREF_CONN_PARAM:
508 memcpy((void *)&p_db_attr->attr_value.conn_param,
509 (const void *)&p_value->conn_param, sizeof(tGAP_BLE_PREF_PARAM));
510 break;
511
512 case GATT_UUID_GAP_DEVICE_NAME:
513 BTM_SetLocalDeviceName((char *)p_value->p_dev_name);
514 break;
515
516 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
517 p_db_attr->attr_value.addr_resolution = p_value->addr_resolution;
518 break;
519
520 }
521 break;
522 }
523 }
524
525 return;
526 }
527
528 /*******************************************************************************
529 **
530 ** Function gap_ble_send_cl_read_request
531 **
532 ** Description utility function to send a read request for a GAP charactersitic
533 **
534 ** Returns TRUE if read started, else FALSE if GAP is busy
535 **
536 *******************************************************************************/
gap_ble_send_cl_read_request(tGAP_CLCB * p_clcb)537 BOOLEAN gap_ble_send_cl_read_request(tGAP_CLCB *p_clcb)
538 {
539 tGATT_READ_PARAM param;
540 UINT16 uuid = 0;
541 BOOLEAN started = FALSE;
542
543 if (gap_ble_dequeue_request(p_clcb, &uuid, &p_clcb->p_cback)) {
544 memset(¶m, 0, sizeof(tGATT_READ_PARAM));
545
546 param.service.uuid.len = LEN_UUID_16;
547 param.service.uuid.uu.uuid16 = uuid;
548 param.service.s_handle = 1;
549 param.service.e_handle = 0xFFFF;
550 param.service.auth_req = 0;
551 #if (GATTC_INCLUDED == TRUE)
552 if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, ¶m) == GATT_SUCCESS) {
553 p_clcb->cl_op_uuid = uuid;
554 started = TRUE;
555 }
556 #endif ///GATTC_INCLUDED == TRUE
557 }
558
559 return started;
560 }
561
562 /*******************************************************************************
563 **
564 ** Function gap_ble_cl_op_cmpl
565 **
566 ** Description GAP client operation complete callback
567 **
568 ** Returns void
569 **
570 *******************************************************************************/
gap_ble_cl_op_cmpl(tGAP_CLCB * p_clcb,BOOLEAN status,UINT16 len,UINT8 * p_name)571 void gap_ble_cl_op_cmpl(tGAP_CLCB *p_clcb, BOOLEAN status, UINT16 len, UINT8 *p_name)
572 {
573 tGAP_BLE_CMPL_CBACK *p_cback = p_clcb->p_cback;
574 UINT16 op = p_clcb->cl_op_uuid;
575
576 GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status);
577
578 p_clcb->cl_op_uuid = 0;
579 p_clcb->p_cback = NULL;
580
581 if (p_cback && op) {
582 GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl");
583 (* p_cback)(status, p_clcb->bda, len, (char *)p_name);
584 }
585
586 /* if no further activity is requested in callback, drop the link */
587 if (p_clcb->connected) {
588 if (!gap_ble_send_cl_read_request(p_clcb)) {
589 GATT_Disconnect(p_clcb->conn_id);
590 gap_ble_dealloc_clcb(p_clcb);
591 }
592 }
593 }
594
595 /*******************************************************************************
596 **
597 ** Function gap_ble_c_connect_cback
598 **
599 ** Description Client connection callback.
600 **
601 ** Returns void
602 **
603 *******************************************************************************/
gap_ble_c_connect_cback(tGATT_IF gatt_if,BD_ADDR bda,UINT16 conn_id,BOOLEAN connected,tGATT_DISCONN_REASON reason,tGATT_TRANSPORT transport)604 static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
605 BOOLEAN connected, tGATT_DISCONN_REASON reason,
606 tGATT_TRANSPORT transport)
607 {
608 tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (bda);
609
610 UNUSED(gatt_if);
611 UNUSED(transport);
612
613 if (p_clcb != NULL) {
614 if (connected) {
615 p_clcb->conn_id = conn_id;
616 p_clcb->connected = TRUE;
617 /* start operation is pending */
618 gap_ble_send_cl_read_request(p_clcb);
619 } else {
620 p_clcb->connected = FALSE;
621 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
622 /* clean up clcb */
623 gap_ble_dealloc_clcb(p_clcb);
624 }
625 }
626 }
627
628 /*******************************************************************************
629 **
630 ** Function gap_ble_c_cmpl_cback
631 **
632 ** Description Client operation complete callback.
633 **
634 ** Returns void
635 **
636 *******************************************************************************/
gap_ble_c_cmpl_cback(UINT16 conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)637 static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
638
639 {
640 tGAP_CLCB *p_clcb = gap_ble_find_clcb_by_conn_id(conn_id);
641 UINT16 op_type;
642 UINT16 min, max, latency, tout;
643 UINT16 len;
644 UINT8 *pp;
645
646 if (p_clcb == NULL) {
647 return;
648 }
649
650 op_type = p_clcb->cl_op_uuid;
651
652 GAP_TRACE_EVENT ("gap_ble_c_cmpl_cback() - op_code: 0x%02x status: 0x%02x read_type: 0x%04x\n", op, status, op_type);
653 /* Currently we only issue read commands */
654 if (op != GATTC_OPTYPE_READ) {
655 return;
656 }
657
658 if (status != GATT_SUCCESS) {
659 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
660 return;
661 }
662
663 pp = p_data->att_value.value;
664
665 switch (op_type) {
666 case GATT_UUID_GAP_PREF_CONN_PARAM:
667 GAP_TRACE_EVENT ("GATT_UUID_GAP_PREF_CONN_PARAM");
668 /* Extract the peripheral preferred connection parameters and save them */
669
670 STREAM_TO_UINT16 (min, pp);
671 STREAM_TO_UINT16 (max, pp);
672 STREAM_TO_UINT16 (latency, pp);
673 STREAM_TO_UINT16 (tout, pp);
674
675 BTM_BleSetPrefConnParams (p_clcb->bda, min, max, latency, tout);
676 /* release the connection here */
677 gap_ble_cl_op_cmpl(p_clcb, TRUE, 0, NULL);
678 break;
679
680 case GATT_UUID_GAP_DEVICE_NAME:
681 GAP_TRACE_EVENT ("GATT_UUID_GAP_DEVICE_NAME\n");
682 len = (UINT16)strlen((char *)pp);
683 if (len > GAP_CHAR_DEV_NAME_SIZE) {
684 len = GAP_CHAR_DEV_NAME_SIZE;
685 }
686 gap_ble_cl_op_cmpl(p_clcb, TRUE, len, pp);
687 break;
688
689 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
690 gap_ble_cl_op_cmpl(p_clcb, TRUE, 1, pp);
691 break;
692 }
693 }
694
695
696 /*******************************************************************************
697 **
698 ** Function gap_ble_accept_cl_operation
699 **
700 ** Description Start a process to read peer address resolution capability
701 **
702 ** Returns TRUE if request accepted
703 **
704 *******************************************************************************/
gap_ble_accept_cl_operation(BD_ADDR peer_bda,UINT16 uuid,tGAP_BLE_CMPL_CBACK * p_cback)705 BOOLEAN gap_ble_accept_cl_operation(BD_ADDR peer_bda, UINT16 uuid, tGAP_BLE_CMPL_CBACK *p_cback)
706 {
707 tGAP_CLCB *p_clcb;
708 BOOLEAN started = FALSE;
709
710 if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM) {
711 return (started);
712 }
713
714 if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL) {
715 if ((p_clcb = gap_clcb_alloc(peer_bda)) == NULL) {
716 GAP_TRACE_ERROR("gap_ble_accept_cl_operation max connection reached");
717 return started;
718 }
719 }
720
721 GAP_TRACE_EVENT ("%s() - BDA: %08x%04x cl_op_uuid: 0x%04x",
722 __FUNCTION__,
723 (peer_bda[0] << 24) + (peer_bda[1] << 16) + (peer_bda[2] << 8) + peer_bda[3],
724 (peer_bda[4] << 8) + peer_bda[5], uuid);
725
726 if (GATT_GetConnIdIfConnected(gap_cb.gatt_if, peer_bda, &p_clcb->conn_id, BT_TRANSPORT_LE)) {
727 p_clcb->connected = TRUE;
728 }
729
730 /* hold the link here */
731 if (!GATT_Connect(gap_cb.gatt_if, p_clcb->bda, BLE_ADDR_UNKNOWN_TYPE, TRUE, BT_TRANSPORT_LE, FALSE)) {
732 return started;
733 }
734
735 /* enqueue the request */
736 gap_ble_enqueue_request(p_clcb, uuid, p_cback);
737
738 if (p_clcb->connected && p_clcb->cl_op_uuid == 0) {
739 started = gap_ble_send_cl_read_request(p_clcb);
740 } else { /* wait for connection up or pending operation to finish */
741 started = TRUE;
742 }
743
744 return started;
745 }
746 /*******************************************************************************
747 **
748 ** Function GAP_BleReadPeerPrefConnParams
749 **
750 ** Description Start a process to read a connected peripheral's preferred
751 ** connection parameters
752 **
753 ** Returns TRUE if read started, else FALSE if GAP is busy
754 **
755 *******************************************************************************/
GAP_BleReadPeerPrefConnParams(BD_ADDR peer_bda)756 BOOLEAN GAP_BleReadPeerPrefConnParams (BD_ADDR peer_bda)
757 {
758 return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM, NULL);
759 }
760
761 /*******************************************************************************
762 **
763 ** Function GAP_BleReadPeerDevName
764 **
765 ** Description Start a process to read a connected peripheral's device name.
766 **
767 ** Returns TRUE if request accepted
768 **
769 *******************************************************************************/
GAP_BleReadPeerDevName(BD_ADDR peer_bda,tGAP_BLE_CMPL_CBACK * p_cback)770 BOOLEAN GAP_BleReadPeerDevName (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
771 {
772 return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_DEVICE_NAME, p_cback);
773 }
774
775 /*******************************************************************************
776 **
777 ** Function GAP_BleReadPeerAddressResolutionCap
778 **
779 ** Description Start a process to read peer address resolution capability
780 **
781 ** Returns TRUE if request accepted
782 **
783 *******************************************************************************/
GAP_BleReadPeerAddressResolutionCap(BD_ADDR peer_bda,tGAP_BLE_CMPL_CBACK * p_cback)784 BOOLEAN GAP_BleReadPeerAddressResolutionCap (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
785 {
786 return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_CENTRAL_ADDR_RESOL, p_cback);
787 }
788
789 /*******************************************************************************
790 **
791 ** Function GAP_BleCancelReadPeerDevName
792 **
793 ** Description Cancel reading a peripheral's device name.
794 **
795 ** Returns TRUE if request accepted
796 **
797 *******************************************************************************/
GAP_BleCancelReadPeerDevName(BD_ADDR peer_bda)798 BOOLEAN GAP_BleCancelReadPeerDevName (BD_ADDR peer_bda)
799 {
800 tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (peer_bda);
801
802 GAP_TRACE_EVENT ("GAP_BleCancelReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x",
803 (peer_bda[0] << 24) + (peer_bda[1] << 16) + (peer_bda[2] << 8) + peer_bda[3],
804 (peer_bda[4] << 8) + peer_bda[5], (p_clcb == NULL) ? 0 : p_clcb->cl_op_uuid);
805
806 if (p_clcb == NULL) {
807 GAP_TRACE_ERROR ("Cannot cancel current op is not get dev name");
808 return FALSE;
809 }
810
811 if (!p_clcb->connected) {
812 if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, TRUE)) {
813 GAP_TRACE_ERROR ("Cannot cancel where No connection id");
814 return FALSE;
815 }
816 }
817
818 gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
819
820 return (TRUE);
821 }
822
823 #endif /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE*/
824