Home | History | Annotate | Download | only in src
      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 #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 
     41 #if BTA_GATT_INCLUDED == TRUE
     42 
     43 #define GATTC_READ_VALUE_TYPE_VALUE          0x0000  /* Attribute value itself */
     44 #define GATTC_READ_VALUE_TYPE_AGG_FORMAT     0x2905  /* Characteristic Aggregate Format*/
     45 
     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
     49 };
     50 
     51 int uuidType(unsigned char* p_uuid)
     52 {
     53     int i = 0;
     54     int match = 0;
     55     int all_zero = 1;
     56 
     57     for(i = 0; i != 16; ++i)
     58     {
     59         if (i == 12 || i == 13)
     60             continue;
     61 
     62         if (p_uuid[i] == BASE_UUID[i])
     63             ++match;
     64 
     65         if (p_uuid[i] != 0)
     66             all_zero = 0;
     67     }
     68     if (all_zero)
     69         return 0;
     70     if (match == 12)
     71         return LEN_UUID_32;
     72     if (match == 14)
     73         return LEN_UUID_16;
     74     return LEN_UUID_128;
     75 }
     76 
     77 /*******************************************************************************
     78  * BTIF -> BTA conversion functions
     79  *******************************************************************************/
     80 
     81 void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src)
     82 {
     83     char *p_byte = (char*)p_src;
     84     int i = 0;
     85 
     86     p_dest->len = uuidType(p_src->uu);
     87 
     88     switch (p_dest->len)
     89     {
     90         case LEN_UUID_16:
     91             p_dest->uu.uuid16 = (p_src->uu[13] << 8) + p_src->uu[12];
     92             break;
     93 
     94         case LEN_UUID_32:
     95             p_dest->uu.uuid32  = (p_src->uu[13] <<  8) + p_src->uu[12];
     96             p_dest->uu.uuid32 += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
     97             break;
     98 
     99         case LEN_UUID_128:
    100             for(i = 0; i != 16; ++i)
    101                 p_dest->uu.uuid128[i] = p_byte[i];
    102             break;
    103 
    104         default:
    105             ALOGE("%s: Unknown UUID length %d!", __FUNCTION__, p_dest->len);
    106             break;
    107     }
    108 }
    109 
    110 void btif_to_bta_gatt_id(tBTA_GATT_ID *p_dest, btgatt_gatt_id_t *p_src)
    111 {
    112     p_dest->inst_id = p_src->inst_id;
    113     btif_to_bta_uuid(&p_dest->uuid, &p_src->uuid);
    114 }
    115 
    116 void btif_to_bta_srvc_id(tBTA_GATT_SRVC_ID *p_dest, btgatt_srvc_id_t *p_src)
    117 {
    118     p_dest->id.inst_id = p_src->id.inst_id;
    119     btif_to_bta_uuid(&p_dest->id.uuid, &p_src->id.uuid);
    120     p_dest->is_primary = p_src->is_primary;
    121 }
    122 
    123 void btif_to_bta_response(tBTA_GATTS_RSP *p_dest, btgatt_response_t* p_src)
    124 {
    125     p_dest->attr_value.auth_req = p_src->attr_value.auth_req;
    126     p_dest->attr_value.handle   = p_src->attr_value.handle;
    127     p_dest->attr_value.len      = p_src->attr_value.len;
    128     p_dest->attr_value.offset   = p_src->attr_value.offset;
    129     memcpy(p_dest->attr_value.value, p_src->attr_value.value, GATT_MAX_ATTR_LEN);
    130 }
    131 
    132 void btif_to_bta_uuid_mask(tBTA_DM_BLE_PF_COND_MASK *p_mask, bt_uuid_t *p_src)
    133 {
    134     char *p_byte = (char*)p_src;
    135     int i = 0;
    136 
    137     switch (uuidType(p_src->uu))
    138     {
    139         case LEN_UUID_16:
    140             p_mask->uuid16_mask = (p_src->uu[13] << 8) + p_src->uu[12];
    141             break;
    142 
    143         case LEN_UUID_32:
    144             p_mask->uuid32_mask = (p_src->uu[13] <<  8) + p_src->uu[12];
    145             p_mask->uuid32_mask += (p_src->uu[15] << 24) + (p_src->uu[14] << 16);
    146             break;
    147 
    148         case LEN_UUID_128:
    149             for(i = 0; i != 16; ++i)
    150                 p_mask->uuid128_mask[i] = p_byte[i];
    151             break;
    152 
    153         default:
    154             break;
    155     }
    156 }
    157 
    158 /*******************************************************************************
    159  * BTA -> BTIF conversion functions
    160  *******************************************************************************/
    161 
    162 void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src)
    163 {
    164     int i = 0;
    165 
    166     if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32)
    167     {
    168         for(i=0; i != 16; ++i)
    169             p_dest->uu[i] = BASE_UUID[i];
    170     }
    171 
    172     switch (p_src->len)
    173     {
    174         case 0:
    175             break;
    176 
    177         case LEN_UUID_16:
    178             p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
    179             p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
    180             break;
    181 
    182         case LEN_UUID_32:
    183             p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
    184             p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
    185             p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
    186             p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
    187             break;
    188 
    189         case LEN_UUID_128:
    190             for(i=0; i != 16; ++i)
    191                 p_dest->uu[i] = p_src->uu.uuid128[i];
    192             break;
    193 
    194         default:
    195             ALOGE("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len);
    196             break;
    197     }
    198 }
    199 
    200 
    201 void bta_to_btif_gatt_id(btgatt_gatt_id_t *p_dest, tBTA_GATT_ID *p_src)
    202 {
    203     p_dest->inst_id = p_src->inst_id;
    204     bta_to_btif_uuid(&p_dest->uuid, &p_src->uuid);
    205 }
    206 
    207 void bta_to_btif_srvc_id(btgatt_srvc_id_t *p_dest, tBTA_GATT_SRVC_ID *p_src)
    208 {
    209     p_dest->id.inst_id = p_src->id.inst_id;
    210     bta_to_btif_uuid(&p_dest->id.uuid, &p_src->id.uuid);
    211     p_dest->is_primary = p_src->is_primary;
    212 }
    213 
    214 
    215 /*******************************************************************************
    216  * Utility functions
    217  *******************************************************************************/
    218 
    219 uint16_t get_uuid16(tBT_UUID *p_uuid)
    220 {
    221     if (p_uuid->len == LEN_UUID_16)
    222     {
    223         return p_uuid->uu.uuid16;
    224     }
    225     else if (p_uuid->len == LEN_UUID_128)
    226     {
    227         UINT16 u16;
    228         UINT8 *p = &p_uuid->uu.uuid128[LEN_UUID_128 - 4];
    229         STREAM_TO_UINT16(u16, p);
    230         return u16;
    231     }
    232     else  /* p_uuid->len == LEN_UUID_32 */
    233     {
    234         return(UINT16) p_uuid->uu.uuid32;
    235     }
    236 }
    237 
    238 uint16_t set_read_value(btgatt_read_params_t *p_dest, tBTA_GATTC_READ *p_src)
    239 {
    240     int i = 0;
    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                 ALOGI("%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)
    308 {
    309     char buf[100];
    310 
    311     bt_bdaddr_t bda;
    312     bdcpy(bda.address, bd_addr);
    313     int device_type = 0;
    314 
    315 #if (!defined(BLE_DELAY_REQUEST_ENC) || (BLE_DELAY_REQUEST_ENC == FALSE))
    316     if ((btif_storage_get_ble_bonding_key(&bda, BTIF_DM_LE_KEY_PENC,
    317                     buf, sizeof(btif_dm_ble_penc_keys_t)) == BT_STATUS_SUCCESS)
    318         && !btif_gatt_is_link_encrypted(bd_addr))
    319     {
    320         tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
    321 
    322         btif_get_device_type(bd_addr, &device_type);
    323         switch(device_type)
    324         {
    325             case BT_DEVICE_TYPE_BREDR:
    326                 transport = BTA_GATT_TRANSPORT_BR_EDR;
    327                 break;
    328 
    329             case BT_DEVICE_TYPE_BLE:
    330                 transport = BTA_GATT_TRANSPORT_LE;
    331                 break;
    332 
    333             case BT_DEVICE_TYPE_DUMO:
    334                 transport = BTA_GATT_TRANSPORT_LE_BR_EDR;
    335                 break;
    336 
    337             default:
    338                 BTIF_TRACE_ERROR (" GATT Encrypt :Invalid device type %d",device_type);
    339                 return;
    340         }
    341         BTA_DmSetEncryption(bd_addr,transport,
    342                             &btif_gatt_set_encryption_cb, BTM_BLE_SEC_ENCRYPT);
    343     }
    344 #endif
    345 }
    346 
    347 #endif
    348