summaryrefslogtreecommitdiffstats
path: root/btif/src/btif_gatt_util.c
blob: a778597f73bf5f2bdcb40e9c409c038414e56bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/******************************************************************************
 *
 *  Copyright (C) 2009-2013 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include <hardware/bluetooth.h>
#include <hardware/bt_gatt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define LOG_TAG "bt_btif_gatt"

#include "btcore/include/bdaddr.h"
#include "bta_api.h"
#include "bta_gatt_api.h"
#include "bta_jv_api.h"
#include "btif_storage.h"
#include "btif_config.h"

#include "btif_common.h"
#include "btif_dm.h"
#include "btif_util.h"
#include "btif_gatt.h"
#include "btif_gatt_util.h"
#include "gki.h"

#if BTA_GATT_INCLUDED == TRUE

#define GATTC_READ_VALUE_TYPE_VALUE          0x0000  /* Attribute value itself */
#define GATTC_READ_VALUE_TYPE_AGG_FORMAT     0x2905  /* Characteristic Aggregate Format*/

static unsigned char BASE_UUID[16] = {
    0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);

int uuidType(unsigned char* p_uuid)
{
    int i = 0;
    int match = 0;
    int all_zero = 1;

    for(i = 0; i != 16; ++i)
    {
        if (i == 12 || i == 13)
            continue;

        if (p_uuid[i] == BASE_UUID[i])
            ++match;

        if (p_uuid[i] != 0)
            all_zero = 0;
    }
    if (all_zero)
        return 0;
    if (match == 12)
        return LEN_UUID_32;
    if (match == 14)
        return LEN_UUID_16;
    return LEN_UUID_128;
}

/*******************************************************************************
 * BTIF -> BTA conversion functions
 *******************************************************************************/

void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src)
{
    char *p_byte = (char*)p_src;
    int i = 0;

    p_dest->len = uuidType(p_src->uu);

    switch (p_dest->len)
    {
        case LEN_UUID_16:
            p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
            break;

        case LEN_UUID_32:
            p_dest->uu.uuid32  = (p_src->uu[13] <<  8) + p_src->uu[12];
            p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
            break;

        case LEN_UUID_128:
            for(i = 0; i != 16; ++i)
                p_dest->uu.uuid128[i] = p_byte[i];
            break;

        default:
            LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
            break;
    }
}

void btif_to_bta_gatt_id(tBTA_GATT_ID *p_dest, btgatt_gatt_id_t *p_src)
{
    p_dest->inst_id = p_src->inst_id;
    btif_to_bta_uuid(&p_dest->uuid, &p_src->uuid);
}

void btif_to_bta_srvc_id(tBTA_GATT_SRVC_ID *p_dest, btgatt_srvc_id_t *p_src)
{
    p_dest->id.inst_id = p_src->id.inst_id;
    btif_to_bta_uuid(&p_dest->id.uuid, &p_src->id.uuid);
    p_dest->is_primary = p_src->is_primary;
}

void btif_to_bta_response(tBTA_GATTS_RSP *p_dest, btgatt_response_t* p_src)
{
    p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
    p_dest->attr_value.handle   = p_src->attr_value.handle;
    p_dest->attr_value.len      = p_src->attr_value.len;
    p_dest->attr_value.offset   = p_src->attr_value.offset;
    memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
}

void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK *p_mask, bt_uuid_t *uuid_mask, bt_uuid_t *svc_uuid)
{
    char *p_byte = (char*)uuid_mask;
    int uuid_len = uuidType(svc_uuid->uu);
    int i = 0;

    switch (uuid_len)
    {
        case LEN_UUID_16:
            p_mask->uuid16_mask = (uuid_mask->uu[13] << 8) + uuid_mask->uu[12];
            break;

        case LEN_UUID_32:
            p_mask->uuid32_mask = (uuid_mask->uu[13] <<  8) + uuid_mask->uu[12];
            p_mask->uuid32_mask += (uuid_mask->uu[15] << 24) + (uuid_mask->uu[14] << 16);
            break;

        case LEN_UUID_128:
            for(i = 0; i != 16; ++i)
                p_mask->uuid128_mask[i] = p_byte[i];
            break;

        default:
            break;
    }
}

/*******************************************************************************
 * BTA -> BTIF conversion functions
 *******************************************************************************/

void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src)
{
    int i = 0;

    if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32)
    {
        for(i=0; i != 16; ++i)
            p_dest->uu[i] = BASE_UUID[i];
    }

    switch (p_src->len)
    {
        case 0:
            break;

        case LEN_UUID_16:
            p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
            p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
            break;

        case LEN_UUID_32:
            p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
            p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
            p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
            p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
            break;

        case LEN_UUID_128:
            for(i=0; i != 16; ++i)
                p_dest->uu[i] = p_src->uu.uuid128[i];
            break;

        default:
            LOG_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len);
            break;
    }
}


void bta_to_btif_gatt_id(btgatt_gatt_id_t *p_dest, tBTA_GATT_ID *p_src)
{
    p_dest->inst_id = p_src->inst_id;
    bta_to_btif_uuid(&p_dest->uuid, &p_src->uuid);
}

void bta_to_btif_srvc_id(btgatt_srvc_id_t *p_dest, tBTA_GATT_SRVC_ID *p_src)
{
    p_dest->id.inst_id = p_src->id.inst_id;
    bta_to_btif_uuid(&p_dest->id.uuid, &p_src->id.uuid);
    p_dest->is_primary = p_src->is_primary;
}


/*******************************************************************************
 * Utility functions
 *******************************************************************************/

uint16_t get_uuid16(tBT_UUID *p_uuid)
{
    if (p_uuid->len == LEN_UUID_16)
    {
        return p_uuid->uu.uuid16;
    }
    else if (p_uuid->len == LEN_UUID_128)
    {
        UINT16 u16;
        UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
        STREAM_TO_UINT16(u16, p);
        return u16;
    }
    else  /* p_uuid->len == LEN_UUID_32 */
    {
        return(UINT16) p_uuid->uu.uuid32;
    }
}

uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src)
{
    uint16_t descr_type = 0;
    uint16_t len = 0;

    p_dest->status = p_src->status;
    bta_to_btif_srvc_id(&p_dest->srvc_id, &p_src->srvc_id);
    bta_to_btif_gatt_id(&p_dest->char_id, &p_src->char_id);
    bta_to_btif_gatt_id(&p_dest->descr_id, &p_src->descr_type);

    descr_type = get_uuid16(&p_src->descr_type.uuid);

    switch (descr_type)
    {
        case GATT_UUID_CHAR_AGG_FORMAT:
            /* not supported */
            p_dest->value_type = GATTC_READ_VALUE_TYPE_AGG_FORMAT;
            break;

        default:
            if (( p_src->status == BTA_GATT_OK ) &&(p_src->p_value != NULL))
            {
                LOG_INFO("%s unformat.len = %d ", __FUNCTION__, p_src->p_value->unformat.len);
                p_dest->value.len = p_src->p_value->unformat.len;
                if ( p_src->p_value->unformat.len > 0  && p_src->p_value->unformat.p_value != NULL )
                {
                    memcpy(p_dest->value.value, p_src->p_value->unformat.p_value,
                           p_src->p_value->unformat.len);
                }
                len += p_src->p_value->unformat.len;
            }
            else
            {
                p_dest->value.len = 0;
            }

            p_dest->value_type = GATTC_READ_VALUE_TYPE_VALUE;
            break;
    }

    return len;
}

/*******************************************************************************
 * Encrypted link map handling
 *******************************************************************************/

static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result);

static BOOLEAN btif_gatt_is_link_encrypted (BD_ADDR bd_addr, tBTA_GATT_TRANSPORT transport)
{
    if (bd_addr == NULL)
        return FALSE;

    return BTA_JvIsEncrypted(bd_addr, transport);
}

static void btif_gatt_set_encryption_cb (BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_STATUS result)
{
    UNUSED(bd_addr);
    UNUSED(transport);
    BTIF_TRACE_WARNING("%s() - Encryption failed (%d)", __FUNCTION__, result);

    if (result == BTA_ERR_KEY_MISSING)
    {
        bt_bdaddr_t bda;
        bdcpy(bda.address, bd_addr);
        btif_dm_remove_bond(&bda);
    }
}

void btif_gatt_check_encrypted_link (BD_ADDR bd_addr, tBTA_GATT_TRANSPORT transport_link)
{
    char buf[100];

    bt_bdaddr_t bda;
    bdcpy(bda.address, bd_addr);

#if (!defined(BLE_DELAY_REQUEST_ENC) || (BLE_DELAY_REQUEST_ENC == FALSE))
    if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC,
                    buf, sizeof(tBTM_LE_PENC_KEYS)) == BT_STATUS_SUCCESS)
        && !btif_gatt_is_link_encrypted(bd_addr, transport_link))
    {
        BTIF_TRACE_DEBUG ("%s: transport = %d", __func__, transport_link);
        BTA_DmSetEncryption(bd_addr,transport_link,
                            &btif_gatt_set_encryption_cb, BTM_BLE_SEC_ENCRYPT);
    }
#endif
}

#endif

void btif_gatt_move_track_adv_data(btgatt_track_adv_info_t *p_dest,
                              btgatt_track_adv_info_t *p_src)
{
    memset(p_dest, 0, sizeof(btgatt_track_adv_info_t));

    memcpy(p_dest, p_src, sizeof(btgatt_track_adv_info_t));

    if (p_src->adv_pkt_len > 0)
    {
        p_dest->p_adv_pkt_data = GKI_getbuf(p_src->adv_pkt_len);
        memcpy(p_dest->p_adv_pkt_data, p_src->p_adv_pkt_data,
               p_src->adv_pkt_len);
        GKI_freebuf(p_src->p_adv_pkt_data);
    }

    if (p_src->scan_rsp_len > 0)
    {
        p_dest->p_scan_rsp_data = GKI_getbuf(p_src->scan_rsp_len);
        memcpy(p_dest->p_scan_rsp_data, p_src->p_scan_rsp_data,
               p_src->scan_rsp_len);
        GKI_freebuf(p_src->p_scan_rsp_data);
    }
}