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 "common/bt_target.h"
26
27 #if defined(GATTS_INCLUDED) && (GATTS_INCLUDED == TRUE)
28
29 #include <string.h>
30 #include "bta/utl.h"
31 #include "bta/bta_sys.h"
32 #include "bta_gatts_int.h"
33
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
36 };
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 *******************************************************************************/
bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128],UINT16 uuid_16)47 static void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
48 {
49 UINT8 *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 *******************************************************************************/
bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB * p_cb,UINT8 rcb_idx)64 UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx)
65 {
66 UINT8 i;
67
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;
72 return i;
73 }
74 }
75 return BTA_GATTS_INVALID_APP;
76 }
77
78 /*******************************************************************************
79 **
80 ** Function bta_gatts_find_app_rcb_by_app_if
81 **
82 ** Description find the index of the application control block by app ID.
83 **
84 ** Returns pointer to the control block if success, otherwise NULL
85 **
86 *******************************************************************************/
bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if)87 tBTA_GATTS_RCB *bta_gatts_find_app_rcb_by_app_if(tBTA_GATTS_IF server_if)
88 {
89 UINT8 i;
90 tBTA_GATTS_RCB *p_reg;
91
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) {
94 return p_reg;
95 }
96 }
97 return NULL;
98 }
99
100 /*******************************************************************************
101 **
102 ** Function bta_gatts_find_app_rcb_idx_by_app_if
103 **
104 ** Description find the index of the application control block by app ID.
105 **
106 ** Returns index of the control block, or BTA_GATTS_INVALID_APP if failed.
107 **
108 *******************************************************************************/
109
bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB * p_cb,tBTA_GATTS_IF server_if)110 UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_IF server_if)
111 {
112 UINT8 i;
113
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) {
116 return i;
117 }
118 }
119 return BTA_GATTS_INVALID_APP;
120 }
121 /*******************************************************************************
122 **
123 ** Function bta_gatts_find_srvc_cb_by_srvc_id
124 **
125 ** Description find the service control block by service ID.
126 **
127 ** Returns pointer to the rcb.
128 **
129 *******************************************************************************/
bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB * p_cb,UINT16 service_id)130 tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id)
131 {
132 UINT8 i;
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];
139 }
140 }
141 return NULL;
142 }
143 /*******************************************************************************
144 **
145 ** Function bta_gatts_find_srvc_cb_by_attr_id
146 **
147 ** Description find the service control block by attribute ID.
148 **
149 ** Returns pointer to the rcb.
150 **
151 *******************************************************************************/
bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB * p_cb,UINT16 attr_id)152 tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id)
153 {
154 UINT8 i;
155
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)
171 ) {
172 return &p_cb->srvc_cb[i];
173 }
174 }
175 return NULL;
176 }
177 /*******************************************************************************
178 **
179 ** Function bta_gatts_uuid_compare
180 **
181 ** Description Compare two UUID to see if they are the same.
182 **
183 ** Returns TRUE if two uuid match; FALSE otherwise.
184 **
185 *******************************************************************************/
bta_gatts_uuid_compare(tBT_UUID tar,tBT_UUID src)186 BOOLEAN bta_gatts_uuid_compare(tBT_UUID tar, tBT_UUID src)
187 {
188 UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
189 UINT8 *ps, *pt;
190
191 /* any of the UUID is unspecified */
192 if (src.len == 0 || tar.len == 0) {
193 return TRUE;
194 }
195
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;
199 }
200
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);
205 ps = su;
206 } else {
207 ps = src.uu.uuid128;
208 }
209
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);
213 pt = tu;
214 } else {
215 pt = tar.uu.uuid128;
216 }
217
218 return (memcmp(ps, pt, LEN_UUID_128) == 0);
219 }
220
221
222
223
224 #endif /* GATTS_INCLUDED */
225