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 #include "stack/btm/btm_sec.h"
43 #include "types/bt_transport.h"
44
45 using bluetooth::Uuid;
46
47 /*******************************************************************************
48 * BTIF -> BTA conversion functions
49 ******************************************************************************/
btif_to_bta_response(tGATTS_RSP * p_dest,btgatt_response_t * p_src)50 void btif_to_bta_response(tGATTS_RSP* p_dest, btgatt_response_t* p_src) {
51 p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
52 p_dest->attr_value.handle = p_src->attr_value.handle;
53 p_dest->attr_value.len = p_src->attr_value.len;
54 p_dest->attr_value.offset = p_src->attr_value.offset;
55 memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
56 }
57
58 /*******************************************************************************
59 * Encrypted link map handling
60 ******************************************************************************/
61
btif_gatt_is_link_encrypted(const RawAddress & bd_addr)62 static bool btif_gatt_is_link_encrypted(const RawAddress& bd_addr) {
63 return BTM_IsEncrypted(bd_addr, BT_TRANSPORT_BR_EDR) ||
64 BTM_IsEncrypted(bd_addr, BT_TRANSPORT_LE);
65 }
66
btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress & bd_addr,UNUSED_ATTR tBT_TRANSPORT transport,tBTA_STATUS result)67 static void btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress& bd_addr,
68 UNUSED_ATTR tBT_TRANSPORT transport,
69 tBTA_STATUS result) {
70 if (result != BTA_SUCCESS && result != BTA_BUSY) {
71 BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result);
72 }
73 }
74
btif_gatt_check_encrypted_link(RawAddress bd_addr,tBT_TRANSPORT transport_link)75 void btif_gatt_check_encrypted_link(RawAddress bd_addr,
76 tBT_TRANSPORT transport_link) {
77 tBTM_LE_PENC_KEYS key;
78 if ((btif_storage_get_ble_bonding_key(
79 bd_addr, BTM_LE_KEY_PENC, (uint8_t*)&key,
80 sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) &&
81 !btif_gatt_is_link_encrypted(bd_addr)) {
82 LOG_DEBUG("Checking gatt link peer:%s transport:%s",
83 PRIVATE_ADDRESS(bd_addr),
84 bt_transport_text(transport_link).c_str());
85 BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb,
86 BTM_BLE_SEC_ENCRYPT);
87 }
88 }
89
btif_gatt_move_track_adv_data(btgatt_track_adv_info_t * p_dest,btgatt_track_adv_info_t * p_src)90 void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest,
91 btgatt_track_adv_info_t* p_src) {
92 memset(p_dest, 0, sizeof(btgatt_track_adv_info_t));
93
94 memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t));
95
96 if (p_src->adv_pkt_len > 0) {
97 p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len);
98 memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, p_src->adv_pkt_len);
99 osi_free_and_reset((void**)&p_src->p_adv_pkt_data);
100 }
101
102 if (p_src->scan_rsp_len > 0) {
103 p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len);
104 memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data,
105 p_src->scan_rsp_len);
106 osi_free_and_reset((void**)&p_src->p_scan_rsp_data);
107 }
108 }
109