1 /******************************************************************************
3 * Copyright (C) 2009-2013 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 #include <hardware/bluetooth.h>
20 #include <hardware/bt_gatt.h>
26 #define LOG_TAG "BtGatt.btif"
29 #include "bta_gatt_api.h"
30 #include "bta_jv_api.h"
32 #include "btif_storage.h"
34 #include "btif_common.h"
36 #include "btif_util.h"
37 #include "btif_gatt.h"
38 #include "btif_gatt_util.h"
39 #include "btif_config.h"
41 #if BTA_GATT_INCLUDED == TRUE
43 #define GATTC_READ_VALUE_TYPE_VALUE 0x0000 /* Attribute value itself */
44 #define GATTC_READ_VALUE_TYPE_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/
46 static unsigned char BASE_UUID[16] = {
47 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
48 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
51 extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);
53 int uuidType(unsigned char* p_uuid)
59 for(i = 0; i != 16; ++i)
61 if (i == 12 || i == 13)
64 if (p_uuid[i] == BASE_UUID[i])
79 /*******************************************************************************
80 * BTIF -> BTA conversion functions
81 *******************************************************************************/
83 void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src)
85 char *p_byte = (char*)p_src;
88 p_dest->len = uuidType(p_src->uu);
93 p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
97 p_dest->uu.uuid32 = (p_src->uu[13] << 8) + p_src->uu[12];
98 p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
102 for(i = 0; i != 16; ++i)
103 p_dest->uu.uuid128[i] = p_byte[i];
107 ALOGE("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
112 void btif_to_bta_gatt_id(tBTA_GATT_ID *p_dest, btgatt_gatt_id_t *p_src)
114 p_dest->inst_id = p_src->inst_id;
115 btif_to_bta_uuid(&p_dest->uuid, &p_src->uuid);
118 void btif_to_bta_srvc_id(tBTA_GATT_SRVC_ID *p_dest, btgatt_srvc_id_t *p_src)
120 p_dest->id.inst_id = p_src->id.inst_id;
121 btif_to_bta_uuid(&p_dest->id.uuid, &p_src->id.uuid);
122 p_dest->is_primary = p_src->is_primary;
125 void btif_to_bta_response(tBTA_GATTS_RSP *p_dest, btgatt_response_t* p_src)
127 p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
128 p_dest->attr_value.handle = p_src->attr_value.handle;
129 p_dest->attr_value.len = p_src->attr_value.len;
130 p_dest->attr_value.offset = p_src->attr_value.offset;
131 memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
134 void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK *p_mask, bt_uuid_t *p_src)
136 char *p_byte = (char*)p_src;
139 switch (uuidType(p_src->uu))
142 p_mask->uuid16_mask = (p_src->uu[13] << 8) + p_src->uu[12];
146 p_mask->uuid32_mask = (p_src->uu[13] << 8) + p_src->uu[12];
147 p_mask->uuid32_mask += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
151 for(i = 0; i != 16; ++i)
152 p_mask->uuid128_mask[i] = p_byte[i];
160 /*******************************************************************************
161 * BTA -> BTIF conversion functions
162 *******************************************************************************/
164 void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src)
168 if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32)
170 for(i=0; i != 16; ++i)
171 p_dest->uu[i] = BASE_UUID[i];
180 p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
181 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
185 p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
186 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
187 p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
188 p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
192 for(i=0; i != 16; ++i)
193 p_dest->uu[i] = p_src->uu.uuid128[i];
197 ALOGE("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len);
203 void bta_to_btif_gatt_id(btgatt_gatt_id_t *p_dest, tBTA_GATT_ID *p_src)
205 p_dest->inst_id = p_src->inst_id;
206 bta_to_btif_uuid(&p_dest->uuid, &p_src->uuid);
209 void bta_to_btif_srvc_id(btgatt_srvc_id_t *p_dest, tBTA_GATT_SRVC_ID *p_src)
211 p_dest->id.inst_id = p_src->id.inst_id;
212 bta_to_btif_uuid(&p_dest->id.uuid, &p_src->id.uuid);
213 p_dest->is_primary = p_src->is_primary;
217 /*******************************************************************************
219 *******************************************************************************/
221 uint16_t get_uuid16(tBT_UUID *p_uuid)
223 if (p_uuid->len == LEN_UUID_16)
225 return p_uuid->uu.uuid16;
227 else if (p_uuid->len == LEN_UUID_128)
230 UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
231 STREAM_TO_UINT16(u16, p);
234 else /* p_uuid->len == LEN_UUID_32 */
236 return(UINT16) p_uuid->uu.uuid32;
240 uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src)
243 uint16_t descr_type = 0;
246 p_dest->status = p_src->status;
247 bta_to_btif_srvc_id(&p_dest->srvc_id, &p_src->srvc_id);
248 bta_to_btif_gatt_id(&p_dest->char_id, &p_src->char_id);
249 bta_to_btif_gatt_id(&p_dest->descr_id, &p_src->descr_type);
251 descr_type = get_uuid16(&p_src->descr_type.uuid);
255 case GATT_UUID_CHAR_AGG_FORMAT:
257 p_dest->value_type = GATTC_READ_VALUE_TYPE_AGG_FORMAT;
261 if (( p_src->status == BTA_GATT_OK ) &&(p_src->p_value != NULL))
263 ALOGI("%s unformat.len = %d ", __FUNCTION__, p_src->p_value->unformat.len);
264 p_dest->value.len = p_src->p_value->unformat.len;
265 if ( p_src->p_value->unformat.len > 0 && p_src->p_value->unformat.p_value != NULL )
267 memcpy(p_dest->value.value, p_src->p_value->unformat.p_value,
268 p_src->p_value->unformat.len);
270 len += p_src->p_value->unformat.len;
274 p_dest->value.len = 0;
277 p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE;
284 /*******************************************************************************
285 * Encrypted link map handling
286 *******************************************************************************/
288 static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result);
290 static BOOLEAN btif_gatt_is_link_encrypted (BD_ADDR bd_addr)
295 return BTA_JvIsEncrypted(bd_addr);
298 static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result)
302 if (result != BTA_SUCCESS && result != BTA_BUSY)
305 bdcpy(bda.address, bd_addr);
307 btif_dm_remove_bond(&bda);
311 void btif_gatt_check_encrypted_link (BD_ADDR bd_addr)
316 bdcpy(bda.address, bd_addr);
320 #if (!defined(BLE_DELAY_REQUEST_ENC) || (BLE_DELAY_REQUEST_ENC == FALSE))
321 if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC,
322 buf, sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
323 && !btif_gatt_is_link_encrypted(bd_addr))
325 tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
327 btif_get_device_type(bd_addr, &addr_type, &device_type);
330 case BT_DEVICE_TYPE_BREDR:
331 transport = BTA_GATT_TRANSPORT_BR_EDR;
334 case BT_DEVICE_TYPE_BLE:
335 transport = BTA_GATT_TRANSPORT_LE;
338 case BT_DEVICE_TYPE_DUMO:
339 transport = BTA_GATT_TRANSPORT_LE_BR_EDR;
343 BTIF_TRACE_ERROR (" GATT Encrypt :Invalid device type %d",device_type);
346 BTA_DmSetEncryption(bd_addr,transport,
347 &btif_gatt_set_encryption_cb, BTM_BLE_SEC_ENCRYPT);
352 /*******************************************************************************
354 *******************************************************************************/
356 BOOLEAN btif_get_device_type(BD_ADDR bd_addr, int *addr_type, int *device_type)
358 if (device_type == NULL || addr_type == NULL)
362 bdcpy(bda.address, bd_addr);
364 char bd_addr_str[18] = {0};
365 bd2str(&bda, &bd_addr_str);
367 if (!btif_config_get_int("Remote", bd_addr_str, "DevType", device_type))
370 if (!btif_config_get_int("Remote", bd_addr_str, "AddrType", addr_type))
373 ALOGD("%s: Device [%s] type %d, addr. type %d", __FUNCTION__, bd_addr_str, *device_type, *addr_type);