Home | History | Annotate | Download | only in gatt
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2003-2012 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 /******************************************************************************
     20  *
     21  *  This file contains the GATT client utility function.
     22  *
     23  ******************************************************************************/
     24 
     25 #include "bt_target.h"
     26 
     27 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
     28 
     29 #include <string.h>
     30 #include "utl.h"
     31 #include "gki.h"
     32 #include "bta_sys.h"
     33 #include "bta_gatts_int.h"
     34 #include "bd.h"
     35 
     36 static const UINT8  base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
     37     0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     38 
     39 /*******************************************************************************
     40 **
     41 ** Function         bta_gatt_convert_uuid16_to_uuid128
     42 **
     43 ** Description      Convert a 16 bits UUID to be an standard 128 bits one.
     44 **
     45 ** Returns          TRUE if two uuid match; FALSE otherwise.
     46 **
     47 *******************************************************************************/
     48 static void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
     49 {
     50     UINT8   *p = &uuid_128[LEN_UUID_128 - 4];
     51 
     52     memcpy (uuid_128, base_uuid, LEN_UUID_128);
     53 
     54     UINT16_TO_STREAM(p, uuid_16);
     55 }
     56 /*******************************************************************************
     57 **
     58 ** Function         bta_gatts_alloc_srvc_cb
     59 **
     60 ** Description      allocate a service control block.
     61 **
     62 ** Returns          pointer to the control block, or otherwise NULL when failed.
     63 **
     64 *******************************************************************************/
     65 UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx)
     66 {
     67     UINT8 i;
     68 
     69     for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++)
     70     {
     71         if (!p_cb->srvc_cb[i].in_use)
     72         {
     73             p_cb->srvc_cb[i].in_use = TRUE;
     74             p_cb->srvc_cb[i].rcb_idx = rcb_idx;
     75             return i;
     76         }
     77     }
     78     return BTA_GATTS_INVALID_APP;
     79 }
     80 
     81 /*******************************************************************************
     82 **
     83 ** Function         bta_gatts_find_app_rcb_by_app_if
     84 **
     85 ** Description      find the index of the application control block by app ID.
     86 **
     87 ** Returns          pointer to the control block if success, otherwise NULL
     88 **
     89 *******************************************************************************/
     90 tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if)
     91 {
     92     UINT8 i;
     93     tBTA_GATTS_RCB *p_reg;
     94 
     95     for (i = 0, p_reg = bta_gatts_cb.rcb; i < BTA_GATTS_MAX_APP_NUM; i ++, p_reg++)
     96     {
     97         if (p_reg->in_use && p_reg->gatt_if == server_if)
     98             return p_reg;
     99     }
    100     return NULL;
    101 }
    102 
    103 /*******************************************************************************
    104 **
    105 ** Function         bta_gatts_find_app_rcb_idx_by_app_if
    106 **
    107 ** Description      find the index of the application control block by app ID.
    108 **
    109 ** Returns          index of the control block, or BTA_GATTS_INVALID_APP if failed.
    110 **
    111 *******************************************************************************/
    112 
    113 UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if)
    114 {
    115     UINT8 i;
    116 
    117     for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
    118     {
    119         if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == server_if)
    120             return i;
    121     }
    122     return BTA_GATTS_INVALID_APP;
    123 }
    124 /*******************************************************************************
    125 **
    126 ** Function         bta_gatts_find_srvc_cb_by_srvc_id
    127 **
    128 ** Description      find the service control block by service ID.
    129 **
    130 ** Returns          pointer to the rcb.
    131 **
    132 *******************************************************************************/
    133 tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id)
    134 {
    135     UINT8 i;
    136     APPL_TRACE_DEBUG1("bta_gatts_find_srvc_cb_by_srvc_id  service_id=%d", service_id);
    137     for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++)
    138     {
    139         if (p_cb->srvc_cb[i].in_use &&
    140             p_cb->srvc_cb[i].service_id == service_id)
    141         {
    142             APPL_TRACE_DEBUG1("bta_gatts_find_srvc_cb_by_srvc_id  found service cb index =%d", i);
    143             return &p_cb->srvc_cb[i];
    144         }
    145     }
    146     return NULL;
    147 }
    148 /*******************************************************************************
    149 **
    150 ** Function         bta_gatts_find_srvc_cb_by_attr_id
    151 **
    152 ** Description      find the service control block by attribute ID.
    153 **
    154 ** Returns          pointer to the rcb.
    155 **
    156 *******************************************************************************/
    157 tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id)
    158 {
    159     UINT8 i;
    160 
    161     for (i = 0; i < (BTA_GATTS_MAX_SRVC_NUM); i ++)
    162     {
    163         if (/* middle service */
    164             (i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
    165              p_cb->srvc_cb[i].in_use &&
    166              p_cb->srvc_cb[i + 1].in_use &&
    167              attr_id >= p_cb->srvc_cb[i].service_id &&
    168              attr_id < p_cb->srvc_cb[i + 1].service_id) ||
    169             /* last active service */
    170             (i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
    171              p_cb->srvc_cb[i].in_use &&
    172              !p_cb->srvc_cb[i + 1].in_use &&
    173              attr_id >= p_cb->srvc_cb[i].service_id)    ||
    174             /* last service incb */
    175             (i == (BTA_GATTS_MAX_SRVC_NUM - 1) &&
    176              attr_id >= p_cb->srvc_cb[i].service_id)
    177            )
    178         {
    179             return &p_cb->srvc_cb[i];
    180         }
    181     }
    182     return NULL;
    183 }
    184 /*******************************************************************************
    185 **
    186 ** Function         bta_gatts_uuid_compare
    187 **
    188 ** Description      Compare two UUID to see if they are the same.
    189 **
    190 ** Returns          TRUE if two uuid match; FALSE otherwise.
    191 **
    192 *******************************************************************************/
    193 BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src)
    194 {
    195     UINT8  su[LEN_UUID_128], tu[LEN_UUID_128];
    196     UINT8  *ps, *pt;
    197 
    198     /* any of the UUID is unspecified */
    199     if (src.len == 0 || tar.len == 0)
    200     {
    201         return TRUE;
    202     }
    203 
    204     /* If both are 16-bit, we can do a simple compare */
    205     if (src.len == 2 && tar.len == 2)
    206     {
    207         return src.uu.uuid16 == tar.uu.uuid16;
    208     }
    209 
    210     /* One or both of the UUIDs is 128-bit */
    211     if (src.len == LEN_UUID_16)
    212     {
    213         /* convert a 16 bits UUID to 128 bits value */
    214         bta_gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16);
    215         ps = su;
    216     }
    217     else
    218         ps = src.uu.uuid128;
    219 
    220     if (tar.len == LEN_UUID_16)
    221     {
    222         /* convert a 16 bits UUID to 128 bits value */
    223         bta_gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16);
    224         pt = tu;
    225     }
    226     else
    227         pt = tar.uu.uuid128;
    228 
    229     return(memcmp(ps, pt, LEN_UUID_128) == 0);
    230 }
    231 
    232 
    233 
    234 
    235 #endif
    236