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