1 /******************************************************************************
2 *
3 * Copyright 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
19 #define LOG_TAG "bt_btif_gatt"
20
21 #include "btif_gatt_util.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <hardware/bluetooth.h>
29 #include <hardware/bt_gatt.h>
30
31 #include "bt_common.h"
32 #include "bta_api.h"
33 #include "bta_gatt_api.h"
34 #include "bta_jv_api.h"
35 #include "btif_common.h"
36 #include "btif_config.h"
37 #include "btif_dm.h"
38 #include "btif_gatt.h"
39 #include "btif_storage.h"
40 #include "btif_util.h"
41 #include "osi/include/osi.h"
42
43 using bluetooth::Uuid;
44
45 /*******************************************************************************
46 * BTIF -> BTA conversion functions
47 ******************************************************************************/
btif_to_bta_response(tGATTS_RSP * p_dest,btgatt_response_t * p_src)48 void btif_to_bta_response(tGATTS_RSP* p_dest, btgatt_response_t* p_src) {
49 p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
50 p_dest->attr_value.handle = p_src->attr_value.handle;
51 p_dest->attr_value.len = p_src->attr_value.len;
52 p_dest->attr_value.offset = p_src->attr_value.offset;
53 memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
54 }
55
56 /*******************************************************************************
57 * Encrypted link map handling
58 ******************************************************************************/
59
60 #if (BLE_DELAY_REQUEST_ENC == FALSE)
btif_gatt_is_link_encrypted(const RawAddress & bd_addr)61 static bool btif_gatt_is_link_encrypted(const RawAddress& bd_addr) {
62 return BTA_JvIsEncrypted(bd_addr);
63 }
64
btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress & bd_addr,UNUSED_ATTR tBTA_TRANSPORT transport,tBTA_STATUS result)65 static void btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress& bd_addr,
66 UNUSED_ATTR tBTA_TRANSPORT transport,
67 tBTA_STATUS result) {
68 if (result != BTA_SUCCESS && result != BTA_BUSY) {
69 BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result);
70 }
71 }
72 #endif
73
74 #if (BLE_DELAY_REQUEST_ENC == FALSE)
btif_gatt_check_encrypted_link(RawAddress bd_addr,tGATT_TRANSPORT transport_link)75 void btif_gatt_check_encrypted_link(RawAddress bd_addr,
76 tGATT_TRANSPORT transport_link) {
77 tBTM_LE_PENC_KEYS key;
78 if ((btif_storage_get_ble_bonding_key(
79 bd_addr, BTIF_DM_LE_KEY_PENC, (uint8_t*)&key,
80 sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) &&
81 !btif_gatt_is_link_encrypted(bd_addr)) {
82 BTIF_TRACE_DEBUG("%s: transport = %d", __func__, transport_link);
83 BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb,
84 BTM_BLE_SEC_ENCRYPT);
85 }
86 }
87 #else
btif_gatt_check_encrypted_link(UNUSED_ATTR RawAddress bd_addr,UNUSED_ATTR tGATT_TRANSPORT transport_link)88 void btif_gatt_check_encrypted_link(UNUSED_ATTR RawAddress bd_addr,
89 UNUSED_ATTR tGATT_TRANSPORT
90 transport_link) {}
91 #endif
92
btif_gatt_move_track_adv_data(btgatt_track_adv_info_t * p_dest,btgatt_track_adv_info_t * p_src)93 void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest,
94 btgatt_track_adv_info_t* p_src) {
95 memset(p_dest, 0, sizeof(btgatt_track_adv_info_t));
96
97 memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t));
98
99 if (p_src->adv_pkt_len > 0) {
100 p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len);
101 memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, p_src->adv_pkt_len);
102 osi_free_and_reset((void**)&p_src->p_adv_pkt_data);
103 }
104
105 if (p_src->scan_rsp_len > 0) {
106 p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len);
107 memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data,
108 p_src->scan_rsp_len);
109 osi_free_and_reset((void**)&p_src->p_scan_rsp_data);
110 }
111 }
112