• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <hardware/bluetooth.h>
25 #include <hardware/bt_gatt.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "bta_api.h"
31 #include "bta_gatt_api.h"
32 #include "bta_jv_api.h"
33 #include "btif_common.h"
34 #include "btif_config.h"
35 #include "btif_dm.h"
36 #include "btif_gatt.h"
37 #include "btif_storage.h"
38 #include "btif_util.h"
39 #include "gd/os/system_properties.h"
40 #include "osi/include/allocator.h"
41 #include "osi/include/osi.h"
42 #include "stack/btm/btm_sec.h"
43 #include "types/bluetooth/uuid.h"
44 #include "types/bt_transport.h"
45 #include "types/raw_address.h"
46 
47 using bluetooth::Uuid;
48 
49 /*******************************************************************************
50  * BTIF -> BTA conversion functions
51  ******************************************************************************/
btif_to_bta_response(tGATTS_RSP * p_dest,btgatt_response_t * p_src)52 void btif_to_bta_response(tGATTS_RSP* p_dest, btgatt_response_t* p_src) {
53   p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
54   p_dest->attr_value.handle = p_src->attr_value.handle;
55   p_dest->attr_value.len = p_src->attr_value.len;
56   p_dest->attr_value.offset = p_src->attr_value.offset;
57   memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
58 }
59 
60 /*******************************************************************************
61  * Encrypted link map handling
62  ******************************************************************************/
63 
btif_gatt_is_link_encrypted(const RawAddress & bd_addr)64 static bool btif_gatt_is_link_encrypted(const RawAddress& bd_addr) {
65   return BTM_IsEncrypted(bd_addr, BT_TRANSPORT_BR_EDR) ||
66          BTM_IsEncrypted(bd_addr, BT_TRANSPORT_LE);
67 }
68 
btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress & bd_addr,UNUSED_ATTR tBT_TRANSPORT transport,tBTA_STATUS result)69 static void btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress& bd_addr,
70                                         UNUSED_ATTR tBT_TRANSPORT transport,
71                                         tBTA_STATUS result) {
72   if (result != BTA_SUCCESS && result != BTA_BUSY) {
73     BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result);
74   }
75 }
76 
btif_gatt_check_encrypted_link(RawAddress bd_addr,tBT_TRANSPORT transport_link)77 void btif_gatt_check_encrypted_link(RawAddress bd_addr,
78                                     tBT_TRANSPORT transport_link) {
79   static const bool check_encrypted = bluetooth::os::GetSystemPropertyBool(
80       "bluetooth.gatt.check_encrypted_link.enabled", true);
81   if (!check_encrypted) {
82     LOG_DEBUG("Check skipped due to system config");
83     return;
84   }
85   tBTM_LE_PENC_KEYS key;
86   if ((btif_storage_get_ble_bonding_key(
87            bd_addr, BTM_LE_KEY_PENC, (uint8_t*)&key,
88            sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) &&
89       !btif_gatt_is_link_encrypted(bd_addr)) {
90     LOG_DEBUG("Checking gatt link peer:%s transport:%s",
91               ADDRESS_TO_LOGGABLE_CSTR(bd_addr),
92               bt_transport_text(transport_link).c_str());
93     BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb,
94                         BTM_BLE_SEC_ENCRYPT);
95   }
96 }
97 
btif_gatt_move_track_adv_data(btgatt_track_adv_info_t * p_dest,btgatt_track_adv_info_t * p_src)98 void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest,
99                                    btgatt_track_adv_info_t* p_src) {
100   memset(p_dest, 0, sizeof(btgatt_track_adv_info_t));
101 
102   memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t));
103 
104   if (p_src->adv_pkt_len > 0) {
105     p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len);
106     memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, p_src->adv_pkt_len);
107     osi_free_and_reset((void**)&p_src->p_adv_pkt_data);
108   }
109 
110   if (p_src->scan_rsp_len > 0) {
111     p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len);
112     memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data,
113            p_src->scan_rsp_len);
114     osi_free_and_reset((void**)&p_src->p_scan_rsp_data);
115   }
116 }
117