1/****************************************************************************** 2 * 3 * Copyright (C) 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#define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */ 44#define GATTC_READ_VALUE_TYPE_AGG_FORMAT \ 45 0x2905 /* Characteristic Aggregate Format*/ 46 47static unsigned char BASE_UUID[16] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 48 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 49 0x00, 0x00, 0x00, 0x00}; 50 51int uuidType(const unsigned char* p_uuid) { 52 int i = 0; 53 int match = 0; 54 int all_zero = 1; 55 56 for (i = 0; i != 16; ++i) { 57 if (i == 12 || i == 13) continue; 58 59 if (p_uuid[i] == BASE_UUID[i]) ++match; 60 61 if (p_uuid[i] != 0) all_zero = 0; 62 } 63 if (all_zero) return 0; 64 if (match == 12) return LEN_UUID_32; 65 if (match == 14) return LEN_UUID_16; 66 return LEN_UUID_128; 67} 68 69/******************************************************************************* 70 * BTIF -> BTA conversion functions 71 ******************************************************************************/ 72 73void btif_to_bta_uuid(tBT_UUID* p_dest, const bt_uuid_t* p_src) { 74 char* p_byte = (char*)p_src; 75 int i = 0; 76 77 p_dest->len = uuidType(p_src->uu); 78 79 switch (p_dest->len) { 80 case LEN_UUID_16: 81 p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12]; 82 break; 83 84 case LEN_UUID_32: 85 p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12]; 86 p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16); 87 break; 88 89 case LEN_UUID_128: 90 for (i = 0; i != 16; ++i) p_dest->uu.uuid128[i] = p_byte[i]; 91 break; 92 93 default: 94 LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_dest->len); 95 break; 96 } 97} 98 99void btif_to_bta_response(tBTA_GATTS_RSP* p_dest, btgatt_response_t* p_src) { 100 p_dest->attr_value.auth_req = p_src->attr_value.auth_req; 101 p_dest->attr_value.handle = p_src->attr_value.handle; 102 p_dest->attr_value.len = p_src->attr_value.len; 103 p_dest->attr_value.offset = p_src->attr_value.offset; 104 memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN); 105} 106 107void btif_to_bta_uuid_mask(tBTM_BLE_PF_COND_MASK* p_mask, 108 const bt_uuid_t* uuid_mask, 109 const bt_uuid_t* svc_uuid) { 110 char* p_byte = (char*)uuid_mask; 111 int uuid_len = uuidType(svc_uuid->uu); 112 int i = 0; 113 114 switch (uuid_len) { 115 case LEN_UUID_16: 116 p_mask->uuid16_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; 117 break; 118 119 case LEN_UUID_32: 120 p_mask->uuid32_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12]; 121 p_mask->uuid32_mask += 122 (uuid_mask->uu[15] << 24) + (uuid_mask->uu[14] << 16); 123 break; 124 125 case LEN_UUID_128: 126 for (i = 0; i != 16; ++i) p_mask->uuid128_mask[i] = p_byte[i]; 127 break; 128 129 default: 130 break; 131 } 132} 133 134/******************************************************************************* 135 * BTA -> BTIF conversion functions 136 ******************************************************************************/ 137 138void bta_to_btif_uuid(bt_uuid_t* p_dest, tBT_UUID* p_src) { 139 int i = 0; 140 141 if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32) { 142 for (i = 0; i != 16; ++i) p_dest->uu[i] = BASE_UUID[i]; 143 } 144 145 switch (p_src->len) { 146 case 0: 147 break; 148 149 case LEN_UUID_16: 150 p_dest->uu[12] = p_src->uu.uuid16 & 0xff; 151 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; 152 break; 153 154 case LEN_UUID_32: 155 p_dest->uu[12] = p_src->uu.uuid16 & 0xff; 156 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; 157 p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff; 158 p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff; 159 break; 160 161 case LEN_UUID_128: 162 for (i = 0; i != 16; ++i) p_dest->uu[i] = p_src->uu.uuid128[i]; 163 break; 164 165 default: 166 LOG_ERROR(LOG_TAG, "%s: Unknown UUID length %d!", __func__, p_src->len); 167 break; 168 } 169} 170 171/******************************************************************************* 172 * Utility functions 173 ******************************************************************************/ 174 175uint16_t get_uuid16(tBT_UUID* p_uuid) { 176 if (p_uuid->len == LEN_UUID_16) { 177 return p_uuid->uu.uuid16; 178 } else if (p_uuid->len == LEN_UUID_128) { 179 uint16_t u16; 180 uint8_t* p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4]; 181 STREAM_TO_UINT16(u16, p); 182 return u16; 183 } else /* p_uuid->len == LEN_UUID_32 */ 184 { 185 return (uint16_t)p_uuid->uu.uuid32; 186 } 187} 188 189uint16_t set_read_value(btgatt_read_params_t* p_dest, tBTA_GATTC_READ* p_src) { 190 uint16_t len = 0; 191 192 p_dest->status = p_src->status; 193 p_dest->handle = p_src->handle; 194 195 if ((p_src->status == BTA_GATT_OK) && (p_src->len != 0)) { 196 LOG_INFO(LOG_TAG, "%s len = %d ", __func__, p_src->len); 197 p_dest->value.len = p_src->len; 198 memcpy(p_dest->value.value, p_src->value, p_src->len); 199 200 len += p_src->len; 201 } else { 202 p_dest->value.len = 0; 203 } 204 205 p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE; 206 return len; 207} 208 209/******************************************************************************* 210 * Encrypted link map handling 211 ******************************************************************************/ 212 213#if (BLE_DELAY_REQUEST_ENC == FALSE) 214static bool btif_gatt_is_link_encrypted(const RawAddress& bd_addr) { 215 return BTA_JvIsEncrypted(bd_addr); 216} 217 218static void btif_gatt_set_encryption_cb(UNUSED_ATTR const RawAddress& bd_addr, 219 UNUSED_ATTR tBTA_TRANSPORT transport, 220 tBTA_STATUS result) { 221 if (result != BTA_SUCCESS && result != BTA_BUSY) { 222 BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __func__, result); 223 } 224} 225#endif 226 227#if (BLE_DELAY_REQUEST_ENC == FALSE) 228void btif_gatt_check_encrypted_link(RawAddress bd_addr, 229 tBTA_GATT_TRANSPORT transport_link) { 230 char buf[100]; 231 232 if ((btif_storage_get_ble_bonding_key(&bd_addr, BTIF_DM_LE_KEY_PENC, buf, 233 sizeof(tBTM_LE_PENC_KEYS)) == 234 BT_STATUS_SUCCESS) && 235 !btif_gatt_is_link_encrypted(bd_addr)) { 236 BTIF_TRACE_DEBUG("%s: transport = %d", __func__, transport_link); 237 BTA_DmSetEncryption(bd_addr, transport_link, &btif_gatt_set_encryption_cb, 238 BTM_BLE_SEC_ENCRYPT); 239 } 240} 241#else 242void btif_gatt_check_encrypted_link(UNUSED_ATTR RawAddress bd_addr, 243 UNUSED_ATTR tBTA_GATT_TRANSPORT 244 transport_link) {} 245#endif 246 247void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t* p_dest, 248 btgatt_track_adv_info_t* p_src) { 249 memset(p_dest, 0, sizeof(btgatt_track_adv_info_t)); 250 251 memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t)); 252 253 if (p_src->adv_pkt_len > 0) { 254 p_dest->p_adv_pkt_data = (uint8_t*)osi_malloc(p_src->adv_pkt_len); 255 memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, p_src->adv_pkt_len); 256 osi_free_and_reset((void**)&p_src->p_adv_pkt_data); 257 } 258 259 if (p_src->scan_rsp_len > 0) { 260 p_dest->p_scan_rsp_data = (uint8_t*)osi_malloc(p_src->scan_rsp_len); 261 memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data, 262 p_src->scan_rsp_len); 263 osi_free_and_reset((void**)&p_src->p_scan_rsp_data); 264 } 265} 266