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             return GATT_WRITE_NOT_PERMIT;
303         }
304     }
305     return GATT_NOT_FOUND;
306 
307 }
308 
309 /******************************************************************************
310 **
311 ** Function         gap_ble_s_attr_request_cback
312 **
313 ** Description      GAP ATT server attribute access request callback.
314 **
315 ** Returns          void.
316 **
317 *******************************************************************************/
gap_ble_s_attr_request_cback(UINT16 conn_id,UINT32 trans_id,tGATTS_REQ_TYPE type,tGATTS_DATA * p_data)318 void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id,
319                                    tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
320 {
321     UINT8       status = GATT_INVALID_PDU;
322     tGATTS_RSP  rsp_msg;
323     BOOLEAN     ignore = FALSE;
324 
325     GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type);
326 
327     memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
328 
329     switch (type) {
330     case GATTS_REQ_TYPE_READ:
331         status = gap_proc_read(type, &p_data->read_req, &rsp_msg);
332         break;
333 
334     case GATTS_REQ_TYPE_WRITE:
335         if (!p_data->write_req.need_rsp) {
336             ignore = TRUE;
337         }
338 
339         status = gap_proc_write_req(type, &p_data->write_req);
340         break;
341 
342     case GATTS_REQ_TYPE_WRITE_EXEC:
343         ignore = TRUE;
344         GAP_TRACE_EVENT("Ignore GATTS_REQ_TYPE_WRITE_EXEC"  );
345         break;
346 
347     case GATTS_REQ_TYPE_MTU:
348         GAP_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
349         ignore = TRUE;
350         break;
351 
352     default:
353         GAP_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
354         break;
355     }
356 
357     if (!ignore) {
358         GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
359     }
360 }
361 
362 /*******************************************************************************
363 **
364 ** Function         btm_ble_att_db_init
365 **
366 ** Description      GAP ATT database initalization.
367 **
368 ** Returns          void.
369 **
370 *******************************************************************************/
gap_attr_db_init(void)371 void gap_attr_db_init(void)
372 {
373     tBT_UUID        app_uuid = {LEN_UUID_128, {0}};
374     tBT_UUID        uuid     = {LEN_UUID_16, {UUID_SERVCLASS_GAP_SERVER}};
375     UINT16          service_handle;
376     tGAP_ATTR       *p_db_attr = &gap_cb.gap_attr[0];
377     tGATT_STATUS    status;
378 
379     /* Fill our internal UUID with a fixed pattern 0x82 */
380     memset (&app_uuid.uu.uuid128, 0x82, LEN_UUID_128);
381     memset(gap_cb.gap_attr, 0, sizeof(tGAP_ATTR) *GAP_MAX_CHAR_NUM);
382 
383     gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback);
384 
385     GATT_StartIf(gap_cb.gatt_if);
386 
387     /* Create a GAP service */
388     service_handle = GATTS_CreateService (gap_cb.gatt_if, &uuid, 0, GAP_MAX_ATTR_NUM, TRUE);
389 
390     GAP_TRACE_EVENT ("gap_attr_db_init service_handle = %d", service_handle);
391 
392     /* add Device Name Characteristic
393     */
394     uuid.len = LEN_UUID_16;
395     uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_DEVICE_NAME;
396     p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
397 											NULL, NULL);
398     p_db_attr ++;
399 
400     /* add Icon characteristic
401     */
402     uuid.uu.uuid16   = p_db_attr->uuid = GATT_UUID_GAP_ICON;
403     p_db_attr->handle = GATTS_AddCharacteristic(service_handle,
404                         &uuid,
405                         GATT_PERM_READ,
406                         GATT_CHAR_PROP_BIT_READ,
407                         NULL, NULL);
408     p_db_attr ++;
409 
410 #if ((defined BTM_PERIPHERAL_ENABLED) && (BTM_PERIPHERAL_ENABLED == TRUE))
411     /* Only needed for peripheral testing */
412     /* add preferred connection parameter characteristic
413     */
414     uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
415     p_db_attr->attr_value.conn_param.int_max = GAP_PREFER_CONN_INT_MAX; /* 6 */
416     p_db_attr->attr_value.conn_param.int_min = GAP_PREFER_CONN_INT_MIN; /* 0 */
417     p_db_attr->attr_value.conn_param.latency = GAP_PREFER_CONN_LATENCY; /* 0 */
418     p_db_attr->attr_value.conn_param.sp_tout = GAP_PREFER_CONN_SP_TOUT; /* 2000 */
419     p_db_attr->handle = GATTS_AddCharacteristic(service_handle,
420                         &uuid,
421                         GATT_PERM_READ,
422                         GATT_CHAR_PROP_BIT_READ,
423                         NULL, NULL);
424     p_db_attr ++;
425 #endif
426 
427     /* add Central address resolution Characteristic */
428     uuid.len = LEN_UUID_16;
429     uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL;
430     p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid,
431                         GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
432                         NULL, NULL);
433     p_db_attr->attr_value.addr_resolution = 0;
434     p_db_attr++;
435 
436     /* start service now */
437     memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
438 
439     status = GATTS_StartService(gap_cb.gatt_if, service_handle, GAP_TRANSPORT_SUPPORTED );
440 #if (CONFIG_BT_STACK_NO_LOG)
441     (void) status;
442 #endif
443 
444     GAP_TRACE_EVENT ("GAP App gatt_if: %d  s_hdl = %d start_status=%d",
445                      gap_cb.gatt_if, service_handle, status);
446 
447 
448 
449 }
450 
451 /*******************************************************************************
452 **
453 ** Function         GAP_BleAttrDBUpdate
454 **
455 ** Description      GAP ATT database update.
456 **
457 ** Returns          void.
458 **
459 *******************************************************************************/
GAP_BleAttrDBUpdate(UINT16 attr_uuid,tGAP_BLE_ATTR_VALUE * p_value)460 void GAP_BleAttrDBUpdate(UINT16 attr_uuid, tGAP_BLE_ATTR_VALUE *p_value)
461 {
462     tGAP_ATTR  *p_db_attr = gap_cb.gap_attr;
463     UINT8       i = 0;
464 
465     GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x\n", attr_uuid);
466 
467     for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++) {
468         if (p_db_attr->uuid == attr_uuid) {
469             GAP_TRACE_EVENT("Found attr_uuid=0x%04x\n", attr_uuid);
470 
471             switch (attr_uuid) {
472             case GATT_UUID_GAP_ICON:
473                 p_db_attr->attr_value.icon  =  p_value->icon;
474                 break;
475 
476             case GATT_UUID_GAP_PREF_CONN_PARAM:
477                 memcpy((void *)&p_db_attr->attr_value.conn_param,
478                        (const void *)&p_value->conn_param, sizeof(tGAP_BLE_PREF_PARAM));
479                 break;
480 
481             case GATT_UUID_GAP_DEVICE_NAME:
482                 BTM_SetLocalDeviceName((char *)p_value->p_dev_name);
483                 break;
484 
485             case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
486                 p_db_attr->attr_value.addr_resolution = p_value->addr_resolution;
487                 break;
488 
489             }
490             break;
491         }
492     }
493 
494     return;
495 }
496 
497 /*******************************************************************************
498 **
499 ** Function         gap_ble_send_cl_read_request
500 **
501 ** Description      utility function to send a read request for a GAP charactersitic
502 **
503 ** Returns          TRUE if read started, else FALSE if GAP is busy
504 **
505 *******************************************************************************/
gap_ble_send_cl_read_request(tGAP_CLCB * p_clcb)506 BOOLEAN gap_ble_send_cl_read_request(tGAP_CLCB *p_clcb)
507 {
508     tGATT_READ_PARAM        param;
509     UINT16                  uuid = 0;
510     BOOLEAN                 started = FALSE;
511 
512     if (gap_ble_dequeue_request(p_clcb, &uuid, &p_clcb->p_cback)) {
513         memset(&param, 0, sizeof(tGATT_READ_PARAM));
514 
515         param.service.uuid.len       = LEN_UUID_16;
516         param.service.uuid.uu.uuid16 = uuid;
517         param.service.s_handle       = 1;
518         param.service.e_handle       = 0xFFFF;
519         param.service.auth_req       = 0;
520 #if (GATTC_INCLUDED == TRUE)
521         if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, &param) == GATT_SUCCESS) {
522             p_clcb->cl_op_uuid = uuid;
523             started = TRUE;
524         }
525 #endif  ///GATTC_INCLUDED == TRUE
526     }
527 
528     return started;
529 }
530 
531 /*******************************************************************************
532 **
533 ** Function         gap_ble_cl_op_cmpl
534 **
535 ** Description      GAP client operation complete callback
536 **
537 ** Returns          void
538 **
539 *******************************************************************************/
gap_ble_cl_op_cmpl(tGAP_CLCB * p_clcb,BOOLEAN status,UINT16 len,UINT8 * p_name)540 void gap_ble_cl_op_cmpl(tGAP_CLCB *p_clcb, BOOLEAN status, UINT16 len, UINT8 *p_name)
541 {
542     tGAP_BLE_CMPL_CBACK *p_cback = p_clcb->p_cback;
543     UINT16                  op = p_clcb->cl_op_uuid;
544 
545     GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status);
546 
547     p_clcb->cl_op_uuid = 0;
548     p_clcb->p_cback = NULL;
549 
550     if (p_cback && op) {
551         GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl");
552         (* p_cback)(status, p_clcb->bda, len, (char *)p_name);
553     }
554 
555     /* if no further activity is requested in callback, drop the link */
556     if (p_clcb->connected) {
557         if (!gap_ble_send_cl_read_request(p_clcb)) {
558             GATT_Disconnect(p_clcb->conn_id);
559             gap_ble_dealloc_clcb(p_clcb);
560         }
561     }
562 }
563 
564 /*******************************************************************************
565 **
566 ** Function         gap_ble_c_connect_cback
567 **
568 ** Description      Client connection callback.
569 **
570 ** Returns          void
571 **
572 *******************************************************************************/
gap_ble_c_connect_cback(tGATT_IF gatt_if,BD_ADDR bda,UINT16 conn_id,BOOLEAN connected,tGATT_DISCONN_REASON reason,tGATT_TRANSPORT transport)573 static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
574                                      BOOLEAN connected, tGATT_DISCONN_REASON reason,
575                                      tGATT_TRANSPORT transport)
576 {
577     tGAP_CLCB   *p_clcb = gap_find_clcb_by_bd_addr (bda);
578 
579     UNUSED(gatt_if);
580     UNUSED(transport);
581 
582     if (p_clcb != NULL) {
583         if (connected) {
584             p_clcb->conn_id = conn_id;
585             p_clcb->connected = TRUE;
586             /* start operation is pending */
587             gap_ble_send_cl_read_request(p_clcb);
588         } else {
589             p_clcb->connected = FALSE;
590             gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
591             /* clean up clcb */
592             gap_ble_dealloc_clcb(p_clcb);
593         }
594     }
595 }
596 
597 /*******************************************************************************
598 **
599 ** Function         gap_ble_c_cmpl_cback
600 **
601 ** Description      Client operation complete callback.
602 **
603 ** Returns          void
604 **
605 *******************************************************************************/
gap_ble_c_cmpl_cback(UINT16 conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)606 static 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 
619     op_type = p_clcb->cl_op_uuid;
620 
621     GAP_TRACE_EVENT ("gap_ble_c_cmpl_cback() - op_code: 0x%02x  status: 0x%02x  read_type: 0x%04x\n", op, status, op_type);
622     /* Currently we only issue read commands */
623     if (op != GATTC_OPTYPE_READ) {
624         return;
625     }
626 
627     if (status != GATT_SUCCESS) {
628         gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
629         return;
630     }
631 
632     pp = p_data->att_value.value;
633 
634     switch (op_type) {
635     case GATT_UUID_GAP_PREF_CONN_PARAM:
636         GAP_TRACE_EVENT ("GATT_UUID_GAP_PREF_CONN_PARAM");
637         /* 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:
650         GAP_TRACE_EVENT ("GATT_UUID_GAP_DEVICE_NAME\n");
651         len = (UINT16)strlen((char *)pp);
652         if (len > GAP_CHAR_DEV_NAME_SIZE) {
653             len = GAP_CHAR_DEV_NAME_SIZE;
654         }
655         gap_ble_cl_op_cmpl(p_clcb, TRUE, len, pp);
656         break;
657 
658     case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
659         gap_ble_cl_op_cmpl(p_clcb, TRUE, 1, pp);
660         break;
661     }
662 }
663 
664 
665 /*******************************************************************************
666 **
667 ** Function         gap_ble_accept_cl_operation
668 **
669 ** Description      Start a process to read peer address resolution capability
670 **
671 ** Returns          TRUE if request accepted
672 **
673 *******************************************************************************/
gap_ble_accept_cl_operation(BD_ADDR peer_bda,UINT16 uuid,tGAP_BLE_CMPL_CBACK * p_cback)674 BOOLEAN gap_ble_accept_cl_operation(BD_ADDR peer_bda, UINT16 uuid, tGAP_BLE_CMPL_CBACK *p_cback)
675 {
676     tGAP_CLCB *p_clcb;
677     BOOLEAN started = FALSE;
678 
679     if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM) {
680         return (started);
681     }
682 
683     if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL) {
684         if ((p_clcb = gap_clcb_alloc(peer_bda)) == NULL) {
685             GAP_TRACE_ERROR("gap_ble_accept_cl_operation max connection reached");
686             return started;
687         }
688     }
689 
690     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 
699     /* hold the link here */
700     if (!GATT_Connect(gap_cb.gatt_if, p_clcb->bda, BLE_ADDR_UNKNOWN_TYPE, TRUE, BT_TRANSPORT_LE, FALSE)) {
701         return started;
702     }
703 
704     /* enqueue the request */
705     gap_ble_enqueue_request(p_clcb, uuid, p_cback);
706 
707     if (p_clcb->connected && p_clcb->cl_op_uuid == 0) {
708         started = gap_ble_send_cl_read_request(p_clcb);
709     } else { /* wait for connection up or pending operation to finish */
710         started = TRUE;
711     }
712 
713     return started;
714 }
715 /*******************************************************************************
716 **
717 ** Function         GAP_BleReadPeerPrefConnParams
718 **
719 ** Description      Start a process to read a connected peripheral's preferred
720 **                  connection parameters
721 **
722 ** Returns          TRUE if read started, else FALSE if GAP is busy
723 **
724 *******************************************************************************/
GAP_BleReadPeerPrefConnParams(BD_ADDR peer_bda)725 BOOLEAN GAP_BleReadPeerPrefConnParams (BD_ADDR peer_bda)
726 {
727     return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM, NULL);
728 }
729 
730 /*******************************************************************************
731 **
732 ** Function         GAP_BleReadPeerDevName
733 **
734 ** Description      Start a process to read a connected peripheral's device name.
735 **
736 ** Returns          TRUE if request accepted
737 **
738 *******************************************************************************/
GAP_BleReadPeerDevName(BD_ADDR peer_bda,tGAP_BLE_CMPL_CBACK * p_cback)739 BOOLEAN GAP_BleReadPeerDevName (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
740 {
741     return gap_ble_accept_cl_operation (peer_bda, GATT_UUID_GAP_DEVICE_NAME, p_cback);
742 }
743 
744 /*******************************************************************************
745 **
746 ** Function         GAP_BleReadPeerAddressResolutionCap
747 **
748 ** Description      Start a process to read peer address resolution capability
749 **
750 ** Returns          TRUE if request accepted
751 **
752 *******************************************************************************/
GAP_BleReadPeerAddressResolutionCap(BD_ADDR peer_bda,tGAP_BLE_CMPL_CBACK * p_cback)753 BOOLEAN GAP_BleReadPeerAddressResolutionCap (BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK *p_cback)
754 {
755     return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_CENTRAL_ADDR_RESOL, p_cback);
756 }
757 
758 /*******************************************************************************
759 **
760 ** Function         GAP_BleCancelReadPeerDevName
761 **
762 ** Description      Cancel reading a peripheral's device name.
763 **
764 ** Returns          TRUE if request accepted
765 **
766 *******************************************************************************/
GAP_BleCancelReadPeerDevName(BD_ADDR peer_bda)767 BOOLEAN GAP_BleCancelReadPeerDevName (BD_ADDR peer_bda)
768 {
769     tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (peer_bda);
770 
771     GAP_TRACE_EVENT ("GAP_BleCancelReadPeerDevName() - BDA: %08x%04x  cl_op_uuid: 0x%04x",
772                      (peer_bda[0] << 24) + (peer_bda[1] << 16) + (peer_bda[2] << 8) + peer_bda[3],
773                      (peer_bda[4] << 8) + peer_bda[5], (p_clcb == NULL) ? 0 : p_clcb->cl_op_uuid);
774 
775     if (p_clcb == NULL) {
776         GAP_TRACE_ERROR ("Cannot cancel current op is not get dev name");
777         return FALSE;
778     }
779 
780     if (!p_clcb->connected) {
781         if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, TRUE)) {
782             GAP_TRACE_ERROR ("Cannot cancel where No connection id");
783             return FALSE;
784         }
785     }
786 
787     gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
788 
789     return (TRUE);
790 }
791 
792 #endif  /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE*/
793