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