1 /******************************************************************************
3 * Copyright (C) 2003-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the GATT client utility function.
23 ******************************************************************************/
25 #include "bt_target.h"
27 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
31 #include "bt_common.h"
33 #include "bta_gatts_int.h"
35 static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
36 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
38 /*******************************************************************************
40 ** Function bta_gatt_convert_uuid16_to_uuid128
42 ** Description Convert a 16 bits UUID to be an standard 128 bits one.
44 ** Returns TRUE if two uuid match; FALSE otherwise.
46 *******************************************************************************/
47 static void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
49 UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
51 memcpy (uuid_128, base_uuid, LEN_UUID_128);
53 UINT16_TO_STREAM(p, uuid_16);
55 /*******************************************************************************
57 ** Function bta_gatts_alloc_srvc_cb
59 ** Description allocate a service control block.
61 ** Returns pointer to the control block, or otherwise NULL when failed.
63 *******************************************************************************/
64 UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx)
68 for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++)
70 if (!p_cb->srvc_cb[i].in_use)
72 p_cb->srvc_cb[i].in_use = TRUE;
73 p_cb->srvc_cb[i].rcb_idx = rcb_idx;
77 return BTA_GATTS_INVALID_APP;
80 /*******************************************************************************
82 ** Function bta_gatts_find_app_rcb_by_app_if
84 ** Description find the index of the application control block by app ID.
86 ** Returns pointer to the control block if success, otherwise NULL
88 *******************************************************************************/
89 tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if)
92 tBTA_GATTS_RCB *p_reg;
94 for (i = 0, p_reg = bta_gatts_cb.rcb; i < BTA_GATTS_MAX_APP_NUM; i ++, p_reg++)
96 if (p_reg->in_use && p_reg->gatt_if == server_if)
102 /*******************************************************************************
104 ** Function bta_gatts_find_app_rcb_idx_by_app_if
106 ** Description find the index of the application control block by app ID.
108 ** Returns index of the control block, or BTA_GATTS_INVALID_APP if failed.
110 *******************************************************************************/
112 UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if)
116 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
118 if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == server_if)
121 return BTA_GATTS_INVALID_APP;
123 /*******************************************************************************
125 ** Function bta_gatts_find_srvc_cb_by_srvc_id
127 ** Description find the service control block by service ID.
129 ** Returns pointer to the rcb.
131 *******************************************************************************/
132 tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id)
135 APPL_TRACE_DEBUG("bta_gatts_find_srvc_cb_by_srvc_id service_id=%d", service_id);
136 for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++)
138 if (p_cb->srvc_cb[i].in_use &&
139 p_cb->srvc_cb[i].service_id == service_id)
141 APPL_TRACE_DEBUG("bta_gatts_find_srvc_cb_by_srvc_id found service cb index =%d", i);
142 return &p_cb->srvc_cb[i];
147 /*******************************************************************************
149 ** Function bta_gatts_find_srvc_cb_by_attr_id
151 ** Description find the service control block by attribute ID.
153 ** Returns pointer to the rcb.
155 *******************************************************************************/
156 tBTA_GATTS_SRVC_CB * bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id)
160 for (i = 0; i < (BTA_GATTS_MAX_SRVC_NUM); i ++)
162 if (/* middle service */
163 (i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
164 p_cb->srvc_cb[i].in_use &&
165 p_cb->srvc_cb[i + 1].in_use &&
166 attr_id >= p_cb->srvc_cb[i].service_id &&
167 attr_id < p_cb->srvc_cb[i + 1].service_id) ||
168 /* last active service */
169 (i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
170 p_cb->srvc_cb[i].in_use &&
171 !p_cb->srvc_cb[i + 1].in_use &&
172 attr_id >= p_cb->srvc_cb[i].service_id) ||
173 /* last service incb */
174 (i == (BTA_GATTS_MAX_SRVC_NUM - 1) &&
175 attr_id >= p_cb->srvc_cb[i].service_id)
178 return &p_cb->srvc_cb[i];
183 /*******************************************************************************
185 ** Function bta_gatts_uuid_compare
187 ** Description Compare two UUID to see if they are the same.
189 ** Returns TRUE if two uuid match; FALSE otherwise.
191 *******************************************************************************/
192 BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src)
194 UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
197 /* any of the UUID is unspecified */
198 if (src.len == 0 || tar.len == 0)
203 /* If both are 16-bit, we can do a simple compare */
204 if (src.len == 2 && tar.len == 2)
206 return src.uu.uuid16 == tar.uu.uuid16;
209 /* One or both of the UUIDs is 128-bit */
210 if (src.len == LEN_UUID_16)
212 /* convert a 16 bits UUID to 128 bits value */
213 bta_gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16);
219 if (tar.len == LEN_UUID_16)
221 /* convert a 16 bits UUID to 128 bits value */
222 bta_gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16);
228 return(memcmp(ps, pt, LEN_UUID_128) == 0);