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 "BtGatt.btif"
27
28#include "bta_api.h"
29#include "bta_gatt_api.h"
30#include "bta_jv_api.h"
31#include "bd.h"
32#include "btif_storage.h"
33
34#include "btif_common.h"
35#include "btif_dm.h"
36#include "btif_util.h"
37#include "btif_gatt.h"
38#include "btif_gatt_util.h"
39
40#if BTA_GATT_INCLUDED == TRUE
41
42#define GATTC_READ_VALUE_TYPE_VALUE          0x0000  /* Attribute value itself */
43#define GATTC_READ_VALUE_TYPE_AGG_FORMAT     0x2905  /* Characteristic Aggregate Format*/
44
45static char BASE_UUID[16] = {
46    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
47    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
48};
49
50extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);
51
52int 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
82void 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            ALOGE("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
107            break;
108    }
109}
110
111void 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
117void 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
124void 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/*******************************************************************************
134 * BTA -> BTIF conversion functions
135 *******************************************************************************/
136
137void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src)
138{
139    int i = 0;
140
141    if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32)
142    {
143        for(i=0; i != 16; ++i)
144            p_dest->uu[i] = BASE_UUID[i];
145    }
146
147    switch (p_src->len)
148    {
149        case 0:
150            break;
151
152        case LEN_UUID_16:
153            p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
154            p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
155            break;
156
157        case LEN_UUID_32:
158            p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
159            p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
160            p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
161            p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
162            break;
163
164        case LEN_UUID_128:
165            for(i=0; i != 16; ++i)
166                p_dest->uu[i] = p_src->uu.uuid128[i];
167            break;
168
169        default:
170            ALOGE("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len);
171            break;
172    }
173}
174
175
176void bta_to_btif_gatt_id(btgatt_gatt_id_t *p_dest, tBTA_GATT_ID *p_src)
177{
178    p_dest->inst_id = p_src->inst_id;
179    bta_to_btif_uuid(&p_dest->uuid, &p_src->uuid);
180}
181
182void bta_to_btif_srvc_id(btgatt_srvc_id_t *p_dest, tBTA_GATT_SRVC_ID *p_src)
183{
184    p_dest->id.inst_id = p_src->id.inst_id;
185    bta_to_btif_uuid(&p_dest->id.uuid, &p_src->id.uuid);
186    p_dest->is_primary = p_src->is_primary;
187}
188
189
190/*******************************************************************************
191 * Utility functions
192 *******************************************************************************/
193
194uint16_t get_uuid16(tBT_UUID *p_uuid)
195{
196    if (p_uuid->len == LEN_UUID_16)
197    {
198        return p_uuid->uu.uuid16;
199    }
200    else if (p_uuid->len == LEN_UUID_128)
201    {
202        UINT16 u16;
203        UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
204        STREAM_TO_UINT16(u16, p);
205        return u16;
206    }
207    else  /* p_uuid->len == LEN_UUID_32 */
208    {
209        return(UINT16) p_uuid->uu.uuid32;
210    }
211}
212
213uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src)
214{
215    int i = 0;
216    uint16_t descr_type = 0;
217    uint16_t len = 0;
218
219    p_dest->status = p_src->status;
220    bta_to_btif_srvc_id(&p_dest->srvc_id, &p_src->srvc_id);
221    bta_to_btif_gatt_id(&p_dest->char_id, &p_src->char_id);
222    bta_to_btif_gatt_id(&p_dest->descr_id, &p_src->descr_type);
223
224    descr_type = get_uuid16(&p_src->descr_type.uuid);
225
226    switch (descr_type)
227    {
228        case GATT_UUID_CHAR_AGG_FORMAT:
229            /* not supported */
230            p_dest->value_type = GATTC_READ_VALUE_TYPE_AGG_FORMAT;
231            break;
232
233        default:
234            if (( p_src->status == BTA_GATT_OK ) &&(p_src->p_value != NULL))
235            {
236                ALOGI("%s unformat.len = %d ", __FUNCTION__, p_src->p_value->unformat.len);
237                p_dest->value.len = p_src->p_value->unformat.len;
238                if ( p_src->p_value->unformat.len > 0  && p_src->p_value->unformat.p_value != NULL )
239                {
240                    memcpy(p_dest->value.value, p_src->p_value->unformat.p_value,
241                           p_src->p_value->unformat.len);
242                }
243                len += p_src->p_value->unformat.len;
244            }
245            else
246            {
247                p_dest->value.len = 0;
248            }
249
250            p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE;
251            break;
252    }
253
254    return len;
255}
256
257/*******************************************************************************
258 * Encrypted link map handling
259 *******************************************************************************/
260
261static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_STATUS result);
262
263static BOOLEAN btif_gatt_is_link_encrypted (BD_ADDR bd_addr)
264{
265    if (bd_addr == NULL)
266        return FALSE;
267
268    return BTA_JvIsEncrypted(bd_addr);
269}
270
271static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_STATUS result)
272{
273    if (result != BTA_SUCCESS)
274    {
275        bt_bdaddr_t bda;
276        bdcpy(bda.address, bd_addr);
277
278        btif_dm_remove_bond(&bda);
279    }
280}
281
282void btif_gatt_check_encrypted_link (BD_ADDR bd_addr)
283{
284    char buf[100];
285
286    bt_bdaddr_t bda;
287    bdcpy(bda.address, bd_addr);
288
289    if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC,
290                    buf, sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
291        && !btif_gatt_is_link_encrypted(bd_addr))
292    {
293        BTA_DmSetEncryption(bd_addr,
294                            &btif_gatt_set_encryption_cb, BTM_BLE_SEC_ENCRYPT);
295    }
296}
297
298#endif
299