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 #include <hardware/bluetooth.h> 20 #include <hardware/bt_gatt.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <errno.h> 24 #include <string.h> 25 26 #define LOG_TAG "bt_btif_gatt" 27 28 #include "btcore/include/bdaddr.h" 29 #include "bta_api.h" 30 #include "bta_gatt_api.h" 31 #include "bta_jv_api.h" 32 #include "btif_storage.h" 33 #include "btif_config.h" 34 35 #include "btif_common.h" 36 #include "btif_dm.h" 37 #include "btif_util.h" 38 #include "btif_gatt.h" 39 #include "btif_gatt_util.h" 40 #include "gki.h" 41 42 #if BTA_GATT_INCLUDED == TRUE 43 44 #define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */ 45 #define GATTC_READ_VALUE_TYPE_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/ 46 47 static unsigned char BASE_UUID[16] = { 48 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 49 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 50 }; 51 52 int uuidType(unsigned char* p_uuid) 53 { 54 int i = 0; 55 int match = 0; 56 int all_zero = 1; 57 58 for(i = 0; i != 16; ++i) 59 { 60 if (i == 12 || i == 13) 61 continue; 62 63 if (p_uuid[i] == BASE_UUID[i]) 64 ++match; 65 66 if (p_uuid[i] != 0) 67 all_zero = 0; 68 } 69 if (all_zero) 70 return 0; 71 if (match == 12) 72 return LEN_UUID_32; 73 if (match == 14) 74 return LEN_UUID_16; 75 return LEN_UUID_128; 76 } 77 78 /******************************************************************************* 79 * BTIF -> BTA conversion functions 80 *******************************************************************************/ 81 82 void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src) 83 { 84 char *p_byte = (char*)p_src; 85 int i = 0; 86 87 p_dest->len = uuidType(p_src->uu); 88 89 switch (p_dest->len) 90 { 91 case LEN_UUID_16: 92 p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12]; 93 break; 94 95 case LEN_UUID_32: 96 p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12]; 97 p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16); 98 break; 99 100 case LEN_UUID_128: 101 for(i = 0; i != 16; ++i) 102 p_dest->uu.uuid128[i] = p_byte[i]; 103 break; 104 105 default: 106 LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len); 107 break; 108 } 109 } 110 111 void btif_to_bta_gatt_id(tBTA_GATT_ID *p_dest, btgatt_gatt_id_t *p_src) 112 { 113 p_dest->inst_id = p_src->inst_id; 114 btif_to_bta_uuid(&p_dest->uuid, &p_src->uuid); 115 } 116 117 void btif_to_bta_srvc_id(tBTA_GATT_SRVC_ID *p_dest, btgatt_srvc_id_t *p_src) 118 { 119 p_dest->id.inst_id = p_src->id.inst_id; 120 btif_to_bta_uuid(&p_dest->id.uuid, &p_src->id.uuid); 121 p_dest->is_primary = p_src->is_primary; 122 } 123 124 void btif_to_bta_response(tBTA_GATTS_RSP *p_dest, btgatt_response_t* p_src) 125 { 126 p_dest->attr_value.auth_req = p_src->attr_value.auth_req; 127 p_dest->attr_value.handle = p_src->attr_value.handle; 128 p_dest->attr_value.len = p_src->attr_value.len; 129 p_dest->attr_value.offset = p_src->attr_value.offset; 130 memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN); 131 } 132 133 void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK *p_mask, bt_uuid_t *p_src) 134 { 135 char *p_byte = (char*)p_src; 136 int i = 0; 137 138 switch (uuidType(p_src->uu)) 139 { 140 case LEN_UUID_16: 141 p_mask->uuid16_mask = (p_src->uu[13] << 8) + p_src->uu[12]; 142 break; 143 144 case LEN_UUID_32: 145 p_mask->uuid32_mask = (p_src->uu[13] << 8) + p_src->uu[12]; 146 p_mask->uuid32_mask += (p_src->uu[15] << 24) + (p_src->uu[14] << 16); 147 break; 148 149 case LEN_UUID_128: 150 for(i = 0; i != 16; ++i) 151 p_mask->uuid128_mask[i] = p_byte[i]; 152 break; 153 154 default: 155 break; 156 } 157 } 158 159 /******************************************************************************* 160 * BTA -> BTIF conversion functions 161 *******************************************************************************/ 162 163 void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src) 164 { 165 int i = 0; 166 167 if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32) 168 { 169 for(i=0; i != 16; ++i) 170 p_dest->uu[i] = BASE_UUID[i]; 171 } 172 173 switch (p_src->len) 174 { 175 case 0: 176 break; 177 178 case LEN_UUID_16: 179 p_dest->uu[12] = p_src->uu.uuid16 & 0xff; 180 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; 181 break; 182 183 case LEN_UUID_32: 184 p_dest->uu[12] = p_src->uu.uuid16 & 0xff; 185 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff; 186 p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff; 187 p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff; 188 break; 189 190 case LEN_UUID_128: 191 for(i=0; i != 16; ++i) 192 p_dest->uu[i] = p_src->uu.uuid128[i]; 193 break; 194 195 default: 196 LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len); 197 break; 198 } 199 } 200 201 202 void bta_to_btif_gatt_id(btgatt_gatt_id_t *p_dest, tBTA_GATT_ID *p_src) 203 { 204 p_dest->inst_id = p_src->inst_id; 205 bta_to_btif_uuid(&p_dest->uuid, &p_src->uuid); 206 } 207 208 void bta_to_btif_srvc_id(btgatt_srvc_id_t *p_dest, tBTA_GATT_SRVC_ID *p_src) 209 { 210 p_dest->id.inst_id = p_src->id.inst_id; 211 bta_to_btif_uuid(&p_dest->id.uuid, &p_src->id.uuid); 212 p_dest->is_primary = p_src->is_primary; 213 } 214 215 216 /******************************************************************************* 217 * Utility functions 218 *******************************************************************************/ 219 220 uint16_t get_uuid16(tBT_UUID *p_uuid) 221 { 222 if (p_uuid->len == LEN_UUID_16) 223 { 224 return p_uuid->uu.uuid16; 225 } 226 else if (p_uuid->len == LEN_UUID_128) 227 { 228 UINT16 u16; 229 UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4]; 230 STREAM_TO_UINT16(u16, p); 231 return u16; 232 } 233 else /* p_uuid->len == LEN_UUID_32 */ 234 { 235 return(UINT16) p_uuid->uu.uuid32; 236 } 237 } 238 239 uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src) 240 { 241 uint16_t descr_type = 0; 242 uint16_t len = 0; 243 244 p_dest->status = p_src->status; 245 bta_to_btif_srvc_id(&p_dest->srvc_id, &p_src->srvc_id); 246 bta_to_btif_gatt_id(&p_dest->char_id, &p_src->char_id); 247 bta_to_btif_gatt_id(&p_dest->descr_id, &p_src->descr_type); 248 249 descr_type = get_uuid16(&p_src->descr_type.uuid); 250 251 switch (descr_type) 252 { 253 case GATT_UUID_CHAR_AGG_FORMAT: 254 /* not supported */ 255 p_dest->value_type = GATTC_READ_VALUE_TYPE_AGG_FORMAT; 256 break; 257 258 default: 259 if (( p_src->status == BTA_GATT_OK ) &&(p_src->p_value != NULL)) 260 { 261 LOG_INFO("%s unformat.len = %d ", __FUNCTION__, p_src->p_value->unformat.len); 262 p_dest->value.len = p_src->p_value->unformat.len; 263 if ( p_src->p_value->unformat.len > 0 && p_src->p_value->unformat.p_value != NULL ) 264 { 265 memcpy(p_dest->value.value, p_src->p_value->unformat.p_value, 266 p_src->p_value->unformat.len); 267 } 268 len += p_src->p_value->unformat.len; 269 } 270 else 271 { 272 p_dest->value.len = 0; 273 } 274 275 p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE; 276 break; 277 } 278 279 return len; 280 } 281 282 /******************************************************************************* 283 * Encrypted link map handling 284 *******************************************************************************/ 285 286 static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result); 287 288 static BOOLEAN btif_gatt_is_link_encrypted (BD_ADDR bd_addr) 289 { 290 if (bd_addr == NULL) 291 return FALSE; 292 293 return BTA_JvIsEncrypted(bd_addr); 294 } 295 296 static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result) 297 { 298 UNUSED(bd_addr); 299 UNUSED(transport); 300 301 if (result != BTA_SUCCESS && result != BTA_BUSY) 302 { 303 BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __FUNCTION__, result); 304 } 305 } 306 307 void btif_gatt_check_encrypted_link (BD_ADDR bd_addr, tBTA_GATT_TRANSPORT transport_link) 308 { 309 char buf[100]; 310 311 bt_bdaddr_t bda; 312 bdcpy(bda.address, bd_addr); 313 314 #if (!defined(BLE_DELAY_REQUEST_ENC) || (BLE_DELAY_REQUEST_ENC == FALSE)) 315 if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC, 316 buf, sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS) 317 && !btif_gatt_is_link_encrypted(bd_addr)) 318 { 319 BTIF_TRACE_DEBUG ("%s: transport = %d", __func__, transport_link); 320 BTA_DmSetEncryption(bd_addr,transport_link, 321 &btif_gatt_set_encryption_cb, BTM_BLE_SEC_ENCRYPT); 322 } 323 #endif 324 } 325 326 #endif 327 328 void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t *p_dest, 329 btgatt_track_adv_info_t *p_src) 330 { 331 memset(p_dest, 0, sizeof(btgatt_track_adv_info_t)); 332 333 memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t)); 334 335 if (p_src->adv_pkt_len > 0) 336 { 337 p_dest->p_adv_pkt_data = GKI_getbuf(p_src->adv_pkt_len); 338 memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data, 339 p_src->adv_pkt_len); 340 GKI_freebuf(p_src->p_adv_pkt_data); 341 } 342 343 if (p_src->scan_rsp_len > 0) 344 { 345 p_dest->p_scan_rsp_data = GKI_getbuf(p_src->scan_rsp_len); 346 memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data, 347 p_src->scan_rsp_len); 348 GKI_freebuf(p_src->p_scan_rsp_data); 349 } 350 } 351 352