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 "common/bt_target.h"
27 #if defined(GATTS_INCLUDED) && (GATTS_INCLUDED == TRUE)
31 #include "bta/bta_sys.h"
32 #include "bta_gatts_int.h"
34 static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
35 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 ++) {
69 if (!p_cb->srvc_cb[i].in_use) {
70 p_cb->srvc_cb[i].in_use = TRUE;
71 p_cb->srvc_cb[i].rcb_idx = rcb_idx;
75 return BTA_GATTS_INVALID_APP;
78 /*******************************************************************************
80 ** Function bta_gatts_find_app_rcb_by_app_if
82 ** Description find the index of the application control block by app ID.
84 ** Returns pointer to the control block if success, otherwise NULL
86 *******************************************************************************/
87 tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if)
90 tBTA_GATTS_RCB *p_reg;
92 for (i = 0, p_reg = bta_gatts_cb.rcb; i < BTA_GATTS_MAX_APP_NUM; i ++, p_reg++) {
93 if (p_reg->in_use && p_reg->gatt_if == server_if) {
100 /*******************************************************************************
102 ** Function bta_gatts_find_app_rcb_idx_by_app_if
104 ** Description find the index of the application control block by app ID.
106 ** Returns index of the control block, or BTA_GATTS_INVALID_APP if failed.
108 *******************************************************************************/
110 UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if)
114 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++) {
115 if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == server_if) {
119 return BTA_GATTS_INVALID_APP;
121 /*******************************************************************************
123 ** Function bta_gatts_find_srvc_cb_by_srvc_id
125 ** Description find the service control block by service ID.
127 ** Returns pointer to the rcb.
129 *******************************************************************************/
130 tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id)
133 APPL_TRACE_DEBUG("bta_gatts_find_srvc_cb_by_srvc_id service_id=%d", service_id);
134 for (i = 0; i < BTA_GATTS_MAX_SRVC_NUM; i ++) {
135 if (p_cb->srvc_cb[i].in_use &&
136 p_cb->srvc_cb[i].service_id == service_id) {
137 APPL_TRACE_DEBUG("bta_gatts_find_srvc_cb_by_srvc_id found service cb index =%d", i);
138 return &p_cb->srvc_cb[i];
143 /*******************************************************************************
145 ** Function bta_gatts_find_srvc_cb_by_attr_id
147 ** Description find the service control block by attribute ID.
149 ** Returns pointer to the rcb.
151 *******************************************************************************/
152 tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id)
156 for (i = 0; i < (BTA_GATTS_MAX_SRVC_NUM); i ++) {
157 if (/* middle service */
158 (i < (BTA_GATTS_MAX_SRVC_NUM - 1) &&
159 p_cb->srvc_cb[i].in_use &&
160 p_cb->srvc_cb[i + 1].in_use &&
161 attr_id >= p_cb->srvc_cb[i].service_id &&
162 attr_id < p_cb->srvc_cb[i + 1].service_id) ||
163 /* last active 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 /* last service incb */
169 (i == (BTA_GATTS_MAX_SRVC_NUM - 1) &&
170 attr_id >= p_cb->srvc_cb[i].service_id)
172 return &p_cb->srvc_cb[i];
177 /*******************************************************************************
179 ** Function bta_gatts_uuid_compare
181 ** Description Compare two UUID to see if they are the same.
183 ** Returns TRUE if two uuid match; FALSE otherwise.
185 *******************************************************************************/
186 BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src)
188 UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
191 /* any of the UUID is unspecified */
192 if (src.len == 0 || tar.len == 0) {
196 /* If both are 16-bit, we can do a simple compare */
197 if (src.len == 2 && tar.len == 2) {
198 return src.uu.uuid16 == tar.uu.uuid16;
201 /* One or both of the UUIDs is 128-bit */
202 if (src.len == LEN_UUID_16) {
203 /* convert a 16 bits UUID to 128 bits value */
204 bta_gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16);
210 if (tar.len == LEN_UUID_16) {
211 /* convert a 16 bits UUID to 128 bits value */
212 bta_gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16);
218 return (memcmp(ps, pt, LEN_UUID_128) == 0);
224 #endif /* GATTS_INCLUDED */