1 /******************************************************************************
2  *
3  *  Copyright (C) 2008-2012 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 
19 /******************************************************************************
20  *
21  *  this file contains the main GATT server attributes access request
22  *  handling functions.
23  *
24  ******************************************************************************/
25 
26 #include "common/bt_target.h"
27 //#include "bt_utils.h"
28 
29 #include "stack/gatt_api.h"
30 #include "gatt_int.h"
31 #include "stack/sdpdefs.h"
32 #include "bta/bta_gatts_co.h"
33 
34 #if (BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
35 
36 #define BLE_GATT_SR_SUPP_FEAT_EATT_BITMASK 0x01
37 #define BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK 0x01
38 #define BLE_GATT_CL_SUPP_FEAT_EATT_BITMASK 0x02
39 #define BLE_GATT_CL_SUPP_FEAT_MULTI_NOTIF_BITMASK 0x04
40 #define BLE_GATT_CL_SUPP_FEAT_BITMASK 0x07
41 
42 #define GATTP_MAX_NUM_INC_SVR       0
43 
44 #if GATTS_ROBUST_CACHING_ENABLED
45 #define GATTP_MAX_CHAR_NUM          5
46 #else
47 #define GATTP_MAX_CHAR_NUM          2
48 #endif /* GATTS_ROBUST_CACHING_ENABLED */
49 
50 #define GATTP_MAX_ATTR_NUM          (GATTP_MAX_CHAR_NUM * 2 + GATTP_MAX_NUM_INC_SVR + 1)
51 #define GATTP_MAX_CHAR_VALUE_SIZE   50
52 
53 #ifndef GATTP_ATTR_DB_SIZE
54 #define GATTP_ATTR_DB_SIZE      GATT_DB_MEM_SIZE(GATTP_MAX_NUM_INC_SVR, GATTP_MAX_CHAR_NUM, GATTP_MAX_CHAR_VALUE_SIZE)
55 #endif
56 
57 static void gatt_request_cback(UINT16 conn_id, UINT32 trans_id, UINT8 op_code, tGATTS_DATA *p_data);
58 static void gatt_connect_cback(tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected,
59                                tGATT_DISCONN_REASON reason, tBT_TRANSPORT transport);
60 static void gatt_disc_res_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data);
61 static void gatt_disc_cmpl_cback(UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
62 static void gatt_cl_op_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
63                                   tGATT_CL_COMPLETE *p_data);
64 
65 static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB *p_clcb);
66 
67 
68 static const tGATT_CBACK gatt_profile_cback = {
69     gatt_connect_cback,
70     gatt_cl_op_cmpl_cback,
71     gatt_disc_res_cback,
72     gatt_disc_cmpl_cback,
73     gatt_request_cback,
74     NULL,
75     NULL
76 } ;
77 
78 /*******************************************************************************
79 **
80 ** Function         gatt_profile_find_conn_id_by_bd_addr
81 **
82 ** Description      Find the connection ID by remote address
83 **
84 ** Returns          Connection ID
85 **
86 *******************************************************************************/
87 #if (GATTS_INCLUDED == TRUE)
gatt_profile_find_conn_id_by_bd_addr(BD_ADDR remote_bda)88 UINT16 gatt_profile_find_conn_id_by_bd_addr(BD_ADDR remote_bda)
89 {
90     UINT16 conn_id = GATT_INVALID_CONN_ID;
91     GATT_GetConnIdIfConnected (gatt_cb.gatt_if, remote_bda, &conn_id, BT_TRANSPORT_LE);
92     return conn_id;
93 }
94 #endif  ///GATTS_INCLUDED == TRUE
95 /*******************************************************************************
96 **
97 ** Function         gatt_profile_find_clcb_by_conn_id
98 **
99 ** Description      find clcb by Connection ID
100 **
101 ** Returns          Pointer to the found link conenction control block.
102 **
103 *******************************************************************************/
gatt_profile_find_clcb_by_conn_id(UINT16 conn_id)104 static tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_conn_id(UINT16 conn_id)
105 {
106     UINT8 i_clcb;
107     tGATT_PROFILE_CLCB    *p_clcb = NULL;
108 
109     for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
110         if (p_clcb->in_use && p_clcb->conn_id == conn_id) {
111             return p_clcb;
112         }
113     }
114 
115     return NULL;
116 }
117 
118 /*******************************************************************************
119 **
120 ** Function         gatt_profile_find_clcb_by_bd_addr
121 **
122 ** Description      The function searches all LCBs with macthing bd address.
123 **
124 ** Returns          Pointer to the found link conenction control block.
125 **
126 *******************************************************************************/
gatt_profile_find_clcb_by_bd_addr(BD_ADDR bda,tBT_TRANSPORT transport)127 static tGATT_PROFILE_CLCB *gatt_profile_find_clcb_by_bd_addr(BD_ADDR bda, tBT_TRANSPORT transport)
128 {
129     UINT8 i_clcb;
130     tGATT_PROFILE_CLCB    *p_clcb = NULL;
131 
132     for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
133         if (p_clcb->in_use && p_clcb->transport == transport &&
134                 p_clcb->connected && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN)) {
135             return p_clcb;
136         }
137     }
138 
139     return NULL;
140 }
141 
142 /*******************************************************************************
143 **
144 ** Function         gatt_profile_clcb_alloc
145 **
146 ** Description      The function allocates a GATT profile  connection link control block
147 **
148 ** Returns           NULL if not found. Otherwise pointer to the connection link block.
149 **
150 *******************************************************************************/
gatt_profile_clcb_alloc(UINT16 conn_id,BD_ADDR bda,tBT_TRANSPORT tranport)151 tGATT_PROFILE_CLCB *gatt_profile_clcb_alloc (UINT16 conn_id, BD_ADDR bda, tBT_TRANSPORT tranport)
152 {
153     UINT8                   i_clcb = 0;
154     tGATT_PROFILE_CLCB      *p_clcb = NULL;
155 
156     for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
157         if (!p_clcb->in_use) {
158             p_clcb->in_use      = TRUE;
159             p_clcb->conn_id     = conn_id;
160             p_clcb->connected   = TRUE;
161             p_clcb->transport   = tranport;
162             memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
163             break;
164         }
165     }
166     if (i_clcb < GATT_MAX_APPS) {
167         return p_clcb;
168     }
169 
170     return NULL;
171 }
172 
173 /*******************************************************************************
174 **
175 ** Function         gatt_profile_clcb_dealloc
176 **
177 ** Description      The function deallocates a GATT profile  connection link control block
178 **
179 ** Returns          void
180 **
181 *******************************************************************************/
gatt_profile_clcb_dealloc(tGATT_PROFILE_CLCB * p_clcb)182 void gatt_profile_clcb_dealloc (tGATT_PROFILE_CLCB *p_clcb)
183 {
184     memset(p_clcb, 0, sizeof(tGATT_PROFILE_CLCB));
185 }
186 
187 /*******************************************************************************
188 **
189 ** Function         gatt_proc_read
190 **
191 ** Description      GATT Attributes Database Read/Read Blob Request process
192 **
193 ** Returns          GATT_SUCCESS if successfully sent; otherwise error code.
194 **
195 *******************************************************************************/
gatt_proc_read(UINT16 conn_id,tGATTS_REQ_TYPE type,tGATT_READ_REQ * p_data,tGATTS_RSP * p_rsp)196 tGATT_STATUS gatt_proc_read (UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp)
197 {
198     tGATT_STATUS    status = GATT_NO_RESOURCES;
199     UINT16 len = 0;
200     UINT8 *value;
201     UNUSED(type);
202 
203     GATT_TRACE_DEBUG("%s handle %x", __func__, p_data->handle);
204 
205     if (p_data->is_long) {
206         p_rsp->attr_value.offset = p_data->offset;
207     }
208 
209     p_rsp->attr_value.handle = p_data->handle;
210 #if GATTS_ROBUST_CACHING_ENABLED
211 
212     UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
213     tGATT_TCB *tcb = gatt_get_tcb_by_idx(tcb_idx);
214 
215     /* handle request for reading client supported features */
216     if (p_data->handle == gatt_cb.handle_of_cl_supported_feat) {
217         if (tcb == NULL) {
218             return GATT_INSUF_RESOURCE;
219         }
220         p_rsp->attr_value.len = 1;
221         memcpy(p_rsp->attr_value.value, &tcb->cl_supp_feat, 1);
222         return GATT_SUCCESS;
223     }
224 
225     /* handle request for reading database hash */
226     if (p_data->handle == gatt_cb.handle_of_database_hash) {
227         p_rsp->attr_value.len = BT_OCTET16_LEN;
228         memcpy(p_rsp->attr_value.value, gatt_cb.database_hash, BT_OCTET16_LEN);
229         gatt_sr_update_cl_status(tcb, true);
230         return GATT_SUCCESS;
231     }
232 
233     /* handle request for reading server supported features */
234     if (p_data->handle == gatt_cb.handle_of_sr_supported_feat) {
235         p_rsp->attr_value.len = 1;
236         memcpy(p_rsp->attr_value.value, &gatt_cb.gatt_sr_supported_feat_mask, 1);
237         return GATT_SUCCESS;
238     }
239 #endif /* GATTS_ROBUST_CACHING_ENABLED */
240     /* handle request for reading service changed des and the others */
241     status = GATTS_GetAttributeValue(p_data->handle, &len, &value);
242     if(status == GATT_SUCCESS && len > 0 && value) {
243         if(len > GATT_MAX_ATTR_LEN) {
244             len = GATT_MAX_ATTR_LEN;
245         }
246         p_rsp->attr_value.len = len;
247         memcpy(p_rsp->attr_value.value, value, len);
248     }
249     return status;
250 }
251 #if GATTS_ROBUST_CACHING_ENABLED
gatt_sr_write_cl_supp_feat(UINT16 conn_id,tGATT_WRITE_REQ * p_data)252 static tGATT_STATUS gatt_sr_write_cl_supp_feat(UINT16 conn_id, tGATT_WRITE_REQ *p_data)
253 {
254     UINT8 val_new;
255     UINT8 val_old;
256     UINT8 val_xor;
257     UINT8 val_and;
258     UINT8 *p = p_data->value;
259     UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
260     tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
261 
262     GATT_TRACE_DEBUG("%s len %u, feat %x", __func__, p_data->len, *p);
263 
264     if (p_tcb == NULL) {
265         GATT_TRACE_ERROR("%s no conn", __func__);
266         return GATT_NOT_FOUND;
267     }
268 
269     if (p_data->len != 1) {
270         GATT_TRACE_ERROR("%s len %u", __func__, p_data->len);
271         return GATT_INVALID_PDU;
272     }
273 
274     STREAM_TO_UINT8(val_new, p);
275     val_new = (val_new & BLE_GATT_CL_SUPP_FEAT_BITMASK);
276 
277     if (val_new == 0) {
278         GATT_TRACE_ERROR("%s bit cannot be all zero", __func__);
279         return GATT_VALUE_NOT_ALLOWED;
280     }
281 
282     val_old = p_tcb->cl_supp_feat;
283     val_xor = val_old ^ val_new;
284     val_and = val_xor & val_new;
285     if (val_and != val_xor) {
286         GATT_TRACE_ERROR("%s bit cannot be reset", __func__);
287         return GATT_VALUE_NOT_ALLOWED;
288     }
289 
290     p_tcb->cl_supp_feat = val_new;
291 #if (SMP_INCLUDED == TRUE)
292     bta_gatts_co_cl_feat_save(p_tcb->peer_bda, &p_tcb->cl_supp_feat);
293 #endif
294     return GATT_SUCCESS;
295 }
296 #endif /* GATTS_ROBUST_CACHING_ENABLED */
297 /******************************************************************************
298 **
299 ** Function         gatt_proc_write_req
300 **
301 ** Description      GATT server process a write request.
302 **
303 ** Returns          GATT_SUCCESS if successfully sent; otherwise error code.
304 **
305 *******************************************************************************/
gatt_proc_write_req(UINT16 conn_id,tGATTS_REQ_TYPE type,tGATT_WRITE_REQ * p_data)306 tGATT_STATUS gatt_proc_write_req(UINT16 conn_id, tGATTS_REQ_TYPE type, tGATT_WRITE_REQ *p_data)
307 {
308     if(p_data->len > GATT_MAX_ATTR_LEN) {
309         p_data->len = GATT_MAX_ATTR_LEN;
310     }
311 #if GATTS_ROBUST_CACHING_ENABLED
312     if (p_data->handle == gatt_cb.handle_of_h_r) {
313         return GATT_WRITE_NOT_PERMIT;
314     }
315 
316     if (p_data->handle == gatt_cb.handle_of_cl_supported_feat) {
317         return gatt_sr_write_cl_supp_feat(conn_id, p_data);
318     }
319 
320     if (p_data->handle == gatt_cb.handle_of_database_hash) {
321         return GATT_WRITE_NOT_PERMIT;
322     }
323 
324     if (p_data->handle == gatt_cb.handle_of_sr_supported_feat) {
325         return GATT_WRITE_NOT_PERMIT;
326     }
327 #endif /* GATTS_ROBUST_CACHING_ENABLED */
328     return GATTS_SetAttributeValue(p_data->handle,
329                            p_data->len,
330                            p_data->value);
331 
332 }
333 
334 /*******************************************************************************
335 **
336 ** Function         gatt_request_cback
337 **
338 ** Description      GATT profile attribute access request callback.
339 **
340 ** Returns          void.
341 **
342 *******************************************************************************/
gatt_request_cback(UINT16 conn_id,UINT32 trans_id,tGATTS_REQ_TYPE type,tGATTS_DATA * p_data)343 static void gatt_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type,
344                                 tGATTS_DATA *p_data)
345 {
346     UINT8       status = GATT_INVALID_PDU;
347     tGATTS_RSP   rsp_msg ;
348     BOOLEAN     ignore = FALSE;
349     GATT_TRACE_DEBUG("%s",__func__);
350     memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
351 
352     switch (type) {
353     case GATTS_REQ_TYPE_READ:
354         status = gatt_proc_read(conn_id, type, &p_data->read_req, &rsp_msg);
355         break;
356 
357     case GATTS_REQ_TYPE_WRITE:
358         if (!p_data->write_req.need_rsp) {
359             ignore = TRUE;
360         }
361         status = gatt_proc_write_req(conn_id, type, &p_data->write_req);
362         break;
363 
364     case GATTS_REQ_TYPE_WRITE_EXEC:
365     case GATT_CMD_WRITE:
366         ignore = TRUE;
367         GATT_TRACE_EVENT("Ignore GATT_REQ_EXEC_WRITE/WRITE_CMD" );
368         break;
369 
370     case GATTS_REQ_TYPE_MTU:
371         GATT_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
372         ignore = TRUE;
373         break;
374 
375     default:
376         GATT_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
377         break;
378     }
379 
380     if (!ignore) {
381         GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
382     }
383 
384 }
385 
386 /*******************************************************************************
387 **
388 ** Function         gatt_connect_cback
389 **
390 ** Description      Gatt profile connection callback.
391 **
392 ** Returns          void
393 **
394 *******************************************************************************/
gatt_connect_cback(tGATT_IF gatt_if,BD_ADDR bda,UINT16 conn_id,BOOLEAN connected,tGATT_DISCONN_REASON reason,tBT_TRANSPORT transport)395 static void gatt_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
396                                 BOOLEAN connected, tGATT_DISCONN_REASON reason,
397                                 tBT_TRANSPORT transport)
398 {
399     UNUSED(gatt_if);
400 
401     GATT_TRACE_DEBUG ("%s: from %08x%04x connected:%d conn_id=%d reason = 0x%04x", __FUNCTION__,
402                       (bda[0] << 24) + (bda[1] << 16) + (bda[2] << 8) + bda[3],
403                       (bda[4] << 8) + bda[5], connected, conn_id, reason);
404 
405     tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_bd_addr(bda, transport);
406     if (p_clcb == NULL) {
407         p_clcb = gatt_profile_clcb_alloc (conn_id, bda, transport);
408     }
409 
410     if (p_clcb == NULL) {
411         return;
412     }
413 
414     if (GATT_GetConnIdIfConnected (gatt_cb.gatt_if, bda, &p_clcb->conn_id, transport)) {
415         p_clcb->connected = TRUE;
416         p_clcb->conn_id = conn_id;
417     }
418 
419 
420     if (!p_clcb->connected) {
421         /* wait for connection */
422         return;
423     }
424 
425     if (connected) {
426         p_clcb->conn_id = conn_id;
427         p_clcb->connected = TRUE;
428 
429     } else {
430         gatt_profile_clcb_dealloc(p_clcb);
431     }
432 }
433 
434 /*******************************************************************************
435 **
436 ** Function         gatt_profile_db_init
437 **
438 ** Description      Initializa the GATT profile attribute database.
439 **
440 *******************************************************************************/
gatt_profile_db_init(void)441 void gatt_profile_db_init (void)
442 {
443     tBT_UUID          app_uuid = {LEN_UUID_128, {0}};
444     tBT_UUID          uuid = {LEN_UUID_16, {UUID_SERVCLASS_GATT_SERVER}};
445     UINT16            service_handle = 0;
446     tGATT_STATUS      status;
447 
448     /* Fill our internal UUID with a fixed pattern 0x81 */
449     memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
450 
451 
452     /* Create a GATT profile service */
453     gatt_cb.gatt_if = GATT_Register(&app_uuid, &gatt_profile_cback);
454     GATT_StartIf(gatt_cb.gatt_if);
455 
456     service_handle = GATTS_CreateService (gatt_cb.gatt_if , &uuid, 0, GATTP_MAX_ATTR_NUM, TRUE);
457     GATT_TRACE_DEBUG ("GATTS_CreateService:  handle of service handle%x", service_handle);
458 
459     /* add Service Changed characteristic
460     */
461     uuid.uu.uuid16 = gatt_cb.gattp_attr.uuid = GATT_UUID_GATT_SRV_CHGD;
462     gatt_cb.gattp_attr.service_change = 0;
463     gatt_cb.gattp_attr.handle   =
464     gatt_cb.handle_of_h_r       = GATTS_AddCharacteristic(service_handle, &uuid, 0, GATT_CHAR_PROP_BIT_INDICATE,
465     												    NULL, NULL);
466 
467     GATT_TRACE_DEBUG ("gatt_profile_db_init:  handle of service changed%d\n",
468                       gatt_cb.handle_of_h_r);
469 
470     tBT_UUID descr_uuid = {LEN_UUID_16, {GATT_UUID_CHAR_CLIENT_CONFIG}};
471     uint8_t ccc_value[2] ={ 0x00, 0x00};
472 
473     tGATT_ATTR_VAL  attr_val = {
474         .attr_max_len = sizeof(UINT16),
475         .attr_len = sizeof(UINT16),
476         .attr_val = ccc_value,
477     };
478 
479     GATTS_AddCharDescriptor (service_handle, GATT_PERM_READ | GATT_PERM_WRITE , &descr_uuid, &attr_val, NULL);
480 #if GATTS_ROBUST_CACHING_ENABLED
481     /* add Client Supported Features characteristic */
482     uuid.uu.uuid16 = GATT_UUID_CLIENT_SUP_FEAT;
483     gatt_cb.handle_of_cl_supported_feat = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ | GATT_PERM_WRITE,
484         GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE, NULL, NULL);
485 
486     /* add Database Hash characteristic */
487     uuid.uu.uuid16 = GATT_UUID_GATT_DATABASE_HASH;
488     gatt_cb.handle_of_database_hash = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ, NULL, NULL);
489 
490     /* add Server Supported Features characteristic */
491     uuid.uu.uuid16 = GATT_UUID_SERVER_SUP_FEAT;
492     gatt_cb.handle_of_sr_supported_feat = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ, NULL, NULL);
493 #endif /* GATTS_ROBUST_CACHING_ENABLED */
494     /* start service */
495     status = GATTS_StartService (gatt_cb.gatt_if, service_handle, GATTP_TRANSPORT_SUPPORTED );
496 
497 #if (CONFIG_BT_STACK_NO_LOG)
498     (void) status;
499 #endif
500 
501     GATT_TRACE_DEBUG ("gatt_profile_db_init:  gatt_if=%d   start status%d\n",
502                       gatt_cb.gatt_if,  status);
503 }
504 
505 /*******************************************************************************
506 **
507 ** Function         gatt_disc_res_cback
508 **
509 ** Description      Gatt profile discovery result callback
510 **
511 ** Returns          void
512 **
513 *******************************************************************************/
gatt_disc_res_cback(UINT16 conn_id,tGATT_DISC_TYPE disc_type,tGATT_DISC_RES * p_data)514 static void gatt_disc_res_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_DISC_RES *p_data)
515 {
516     GATT_TRACE_DEBUG("%s, disc_type = %d",__func__, disc_type);
517     tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
518 
519     if (p_clcb == NULL) {
520         return;
521     }
522 
523     switch (disc_type) {
524     case GATT_DISC_SRVC_BY_UUID:/* stage 1 */
525         p_clcb->e_handle = p_data->value.group_value.e_handle;
526         p_clcb->ccc_result ++;
527         break;
528 
529     case GATT_DISC_CHAR:/* stage 2 */
530         p_clcb->s_handle = p_data->value.dclr_value.val_handle;
531         p_clcb->ccc_result ++;
532         break;
533 
534     case GATT_DISC_CHAR_DSCPT: /* stage 3 */
535         if (p_data->type.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG) {
536             p_clcb->s_handle = p_data->handle;
537             p_clcb->ccc_result ++;
538         }
539         break;
540     }
541 }
542 
543 /*******************************************************************************
544 **
545 ** Function         gatt_disc_cmpl_cback
546 **
547 ** Description      Gatt profile discovery complete callback
548 **
549 ** Returns          void
550 **
551 *******************************************************************************/
gatt_disc_cmpl_cback(UINT16 conn_id,tGATT_DISC_TYPE disc_type,tGATT_STATUS status)552 static void gatt_disc_cmpl_cback (UINT16 conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status)
553 {
554     GATT_TRACE_DEBUG("%s",__func__);
555     tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
556 
557     if (p_clcb == NULL) {
558         return;
559     }
560 
561     if (status == GATT_SUCCESS && p_clcb->ccc_result > 0) {
562         p_clcb->ccc_result = 0;
563         p_clcb->ccc_stage ++;
564         gatt_cl_start_config_ccc(p_clcb);
565     } else {
566         GATT_TRACE_ERROR("%s() - Register for service changed indication failure", __FUNCTION__);
567     }
568 }
569 
570 /*******************************************************************************
571 **
572 ** Function         gatt_cl_op_cmpl_cback
573 **
574 ** Description      Gatt profile client operation complete callback
575 **
576 ** Returns          void
577 **
578 *******************************************************************************/
gatt_cl_op_cmpl_cback(UINT16 conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)579 static void gatt_cl_op_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op,
580                                    tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
581 {
582     GATT_TRACE_DEBUG("%s",__func__);
583     tGATT_PROFILE_CLCB *p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
584 
585     if (p_clcb == NULL) {
586         return;
587     }
588 
589     if (op == GATTC_OPTYPE_WRITE) {
590         GATT_TRACE_DEBUG("%s() - ccc write status : %d", __FUNCTION__, status);
591     }
592 
593 }
594 
595 /*******************************************************************************
596 **
597 ** Function         gatt_cl_start_config_ccc
598 **
599 ** Description      Gatt profile start configure service change CCC
600 **
601 ** Returns          void
602 **
603 *******************************************************************************/
gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB * p_clcb)604 static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB *p_clcb)
605 {
606     tGATT_DISC_PARAM    srvc_disc_param;
607     tGATT_VALUE         ccc_value;
608 
609     GATT_TRACE_DEBUG("%s() - stage: %d", __FUNCTION__, p_clcb->ccc_stage);
610 
611     memset (&srvc_disc_param, 0 , sizeof(tGATT_DISC_PARAM));
612     memset (&ccc_value, 0 , sizeof(tGATT_VALUE));
613 
614     switch (p_clcb->ccc_stage) {
615     case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */
616         srvc_disc_param.s_handle = 1;
617         srvc_disc_param.e_handle = 0xffff;
618         srvc_disc_param.service.len = 2;
619         srvc_disc_param.service.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
620 #if (GATTC_INCLUDED == TRUE)
621         if (GATTC_Discover (p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, &srvc_disc_param) != GATT_SUCCESS) {
622             GATT_TRACE_ERROR("%s() - ccc service error", __FUNCTION__);
623         }
624 #endif  ///GATTC_INCLUDED == TRUE
625         break;
626 
627     case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */
628         srvc_disc_param.s_handle = 1;
629         srvc_disc_param.e_handle = p_clcb->e_handle;
630         srvc_disc_param.service.len = 2;
631         srvc_disc_param.service.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
632 #if (GATTC_INCLUDED == TRUE)
633         if (GATTC_Discover (p_clcb->conn_id, GATT_DISC_CHAR, &srvc_disc_param) != GATT_SUCCESS) {
634             GATT_TRACE_ERROR("%s() - ccc char error", __FUNCTION__);
635         }
636 #endif  ///GATTC_INCLUDED == TRUE
637         break;
638 
639     case GATT_SVC_CHANGED_DESCRIPTOR: /* discover service change ccc */
640         srvc_disc_param.s_handle = p_clcb->s_handle;
641         srvc_disc_param.e_handle = p_clcb->e_handle;
642 #if (GATTC_INCLUDED == TRUE)
643         if (GATTC_Discover (p_clcb->conn_id, GATT_DISC_CHAR_DSCPT, &srvc_disc_param) != GATT_SUCCESS) {
644             GATT_TRACE_ERROR("%s() - ccc char descriptor error", __FUNCTION__);
645         }
646 #endif  ///GATTC_INCLUDED == TRUE
647         break;
648 
649     case GATT_SVC_CHANGED_CONFIGURE_CCCD: /* write ccc */
650         ccc_value.handle = p_clcb->s_handle;
651         ccc_value.len = 2;
652         ccc_value.value[0] = GATT_CLT_CONFIG_INDICATION;
653 #if (GATTC_INCLUDED == TRUE)
654         if (GATTC_Write (p_clcb->conn_id, GATT_WRITE, &ccc_value) != GATT_SUCCESS) {
655             GATT_TRACE_ERROR("%s() - write ccc error", __FUNCTION__);
656         }
657 #endif  ///GATTC_INCLUDED == TRUE
658         break;
659     }
660 }
661 
662 /*******************************************************************************
663 **
664 ** Function         GATT_ConfigServiceChangeCCC
665 **
666 ** Description      Configure service change indication on remote device
667 **
668 ** Returns          none
669 **
670 *******************************************************************************/
GATT_ConfigServiceChangeCCC(BD_ADDR remote_bda,BOOLEAN enable,tBT_TRANSPORT transport)671 void GATT_ConfigServiceChangeCCC (BD_ADDR remote_bda, BOOLEAN enable, tBT_TRANSPORT transport)
672 {
673     tGATT_PROFILE_CLCB   *p_clcb = gatt_profile_find_clcb_by_bd_addr (remote_bda, transport);
674 
675     if (p_clcb == NULL) {
676         p_clcb = gatt_profile_clcb_alloc (0, remote_bda, transport);
677     }
678 
679     if (p_clcb == NULL) {
680         return;
681     }
682 
683     if (GATT_GetConnIdIfConnected (gatt_cb.gatt_if, remote_bda, &p_clcb->conn_id, transport)) {
684         p_clcb->connected = TRUE;
685     }
686     /* hold the link here */
687     GATT_Connect(gatt_cb.gatt_if, remote_bda, BLE_ADDR_UNKNOWN_TYPE, TRUE, transport, FALSE);
688     p_clcb->ccc_stage = GATT_SVC_CHANGED_CONNECTING;
689 
690     if (!p_clcb->connected) {
691         /* wait for connection */
692         return;
693     }
694 
695     p_clcb->ccc_stage ++;
696     gatt_cl_start_config_ccc(p_clcb);
697 }
698 
699 #if GATTS_ROBUST_CACHING_ENABLED
700 /*******************************************************************************
701 **
702 ** Function         gatt_sr_is_cl_robust_caching_supported
703 **
704 ** Description      Check if Robust Caching is supported for the connection
705 **
706 ** Returns          true if enabled by client side, otherwise false
707 **
708 *******************************************************************************/
gatt_sr_is_cl_robust_caching_supported(tGATT_TCB * p_tcb)709 static BOOLEAN gatt_sr_is_cl_robust_caching_supported(tGATT_TCB *p_tcb)
710 {
711     return (p_tcb->cl_supp_feat & BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK);
712 }
713 /*******************************************************************************
714 **
715 ** Function         gatt_sr_is_cl_change_aware
716 **
717 ** Description      Check if the connection is change-aware
718 **
719 ** Returns          true if change aware, otherwise false
720 **
721 *******************************************************************************/
gatt_sr_is_cl_change_aware(tGATT_TCB * p_tcb)722 BOOLEAN gatt_sr_is_cl_change_aware(tGATT_TCB *p_tcb)
723 {
724     // If robust caching is not supported, should always return true by default
725     if (!gatt_sr_is_cl_robust_caching_supported(p_tcb)) {
726         return true;
727     }
728 
729     return p_tcb->is_robust_cache_change_aware;
730 }
731 
732 /*******************************************************************************
733 **
734 ** Function         gatt_sr_init_cl_status
735 **
736 ** Description      Restore status for trusted device
737 **
738 ** Returns          none
739 **
740 *******************************************************************************/
gatt_sr_init_cl_status(tGATT_TCB * p_tcb)741 void gatt_sr_init_cl_status(tGATT_TCB *p_tcb)
742 {
743 #if (SMP_INCLUDED == TRUE)
744     bta_gatts_co_cl_feat_load(p_tcb->peer_bda, &p_tcb->cl_supp_feat);
745 #endif
746 
747     // This is used to reset bit when robust caching is disabled
748     if (!GATTS_ROBUST_CACHING_ENABLED) {
749         p_tcb->cl_supp_feat &= ~BLE_GATT_CL_SUPP_FEAT_ROBUST_CACHING_BITMASK;
750     }
751 
752     if (gatt_sr_is_cl_robust_caching_supported(p_tcb)) {
753         BT_OCTET16 stored_hash = {0};
754 #if (SMP_INCLUDED == TRUE)
755         bta_gatts_co_db_hash_load(p_tcb->peer_bda, stored_hash);
756 #endif
757         p_tcb->is_robust_cache_change_aware = (memcmp(stored_hash, gatt_cb.database_hash, BT_OCTET16_LEN) == 0);
758     } else {
759         p_tcb->is_robust_cache_change_aware = true;
760     }
761 
762     GATT_TRACE_DEBUG("%s feat %x aware %d", __func__, p_tcb->cl_supp_feat, p_tcb->is_robust_cache_change_aware);
763 }
764 
765 /*******************************************************************************
766 **
767 ** Function         gatt_sr_update_cl_status
768 **
769 ** Description      Update change-aware status for the remote device
770 **
771 ** Returns          none
772 **
773 *******************************************************************************/
gatt_sr_update_cl_status(tGATT_TCB * p_tcb,BOOLEAN chg_aware)774 void gatt_sr_update_cl_status(tGATT_TCB *p_tcb, BOOLEAN chg_aware)
775 {
776     if (p_tcb == NULL) {
777         return;
778     }
779 
780     // if robust caching is not supported, do nothing
781     if (!gatt_sr_is_cl_robust_caching_supported(p_tcb)) {
782         return;
783     }
784 
785     // only when client status is changed from unaware to aware, we should store database hash
786     if (!p_tcb->is_robust_cache_change_aware && chg_aware) {
787 #if (SMP_INCLUDED == TRUE)
788         bta_gatts_co_db_hash_save(p_tcb->peer_bda, gatt_cb.database_hash);
789 #endif
790     }
791 
792     p_tcb->is_robust_cache_change_aware = chg_aware;
793 
794     GATT_TRACE_DEBUG("%s status %d", __func__, chg_aware);
795 }
796 #endif /* GATTS_ROBUST_CACHING_ENABLED */
797 #endif  /* BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */
798