1 /******************************************************************************
2 *
3 * Copyright (c) 2014 The Android Open Source Project
4 * Copyright (C) 2003-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /******************************************************************************
21 *
22 * This file contains the audio gateway functions performing SDP
23 * operations.
24 *
25 ******************************************************************************/
26
27 #include <string.h>
28
29 #include "bt_utils.h"
30 #include "bta_api.h"
31 #include "bta_hf_client_api.h"
32 #include "bta_hf_client_int.h"
33 #include "bta_sys.h"
34 #include "osi/include/osi.h"
35
36 /* Number of protocol elements in protocol element list. */
37 #define BTA_HF_CLIENT_NUM_PROTO_ELEMS 2
38
39 /* Number of elements in service class id list. */
40 #define BTA_HF_CLIENT_NUM_SVC_ELEMS 2
41
42 /*******************************************************************************
43 *
44 * Function bta_hf_client_sdp_cback
45 *
46 * Description SDP callback function.
47 *
48 *
49 * Returns void
50 *
51 ******************************************************************************/
bta_hf_client_sdp_cback(uint16_t status,void * data)52 static void bta_hf_client_sdp_cback(uint16_t status, void* data) {
53 uint16_t event;
54 tBTA_HF_CLIENT_DISC_RESULT* p_buf = (tBTA_HF_CLIENT_DISC_RESULT*)osi_malloc(
55 sizeof(tBTA_HF_CLIENT_DISC_RESULT));
56
57 APPL_TRACE_DEBUG("bta_hf_client_sdp_cback status:0x%x", status);
58 tBTA_HF_CLIENT_CB* client_cb = (tBTA_HF_CLIENT_CB*)data;
59
60 /* set event according to int/acp */
61 if (client_cb->role == BTA_HF_CLIENT_ACP)
62 event = BTA_HF_CLIENT_DISC_ACP_RES_EVT;
63 else
64 event = BTA_HF_CLIENT_DISC_INT_RES_EVT;
65
66 p_buf->hdr.event = event;
67 p_buf->hdr.layer_specific = client_cb->handle;
68 p_buf->status = status;
69
70 bta_sys_sendmsg(p_buf);
71 }
72
73 /******************************************************************************
74 *
75 * Function bta_hf_client_add_record
76 *
77 * Description This function is called by a server application to add
78 * HFP Client information to an SDP record. Prior to
79 * calling this function the application must call
80 * SDP_CreateRecord() to create an SDP record.
81 *
82 * Returns true if function execution succeeded,
83 * false if function execution failed.
84 *
85 *****************************************************************************/
bta_hf_client_add_record(const char * p_service_name,uint8_t scn,tBTA_HF_CLIENT_FEAT features,uint32_t sdp_handle)86 bool bta_hf_client_add_record(const char* p_service_name, uint8_t scn,
87 tBTA_HF_CLIENT_FEAT features,
88 uint32_t sdp_handle) {
89 tSDP_PROTOCOL_ELEM proto_elem_list[BTA_HF_CLIENT_NUM_PROTO_ELEMS];
90 uint16_t svc_class_id_list[BTA_HF_CLIENT_NUM_SVC_ELEMS];
91 uint16_t browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
92 uint16_t version;
93 uint16_t profile_uuid;
94 bool result = true;
95 uint8_t buf[2];
96 uint16_t sdp_features = 0;
97
98 APPL_TRACE_DEBUG("bta_hf_client_add_record");
99
100 memset(proto_elem_list, 0,
101 BTA_HF_CLIENT_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
102
103 /* add the protocol element sequence */
104 proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
105 proto_elem_list[0].num_params = 0;
106 proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
107 proto_elem_list[1].num_params = 1;
108 proto_elem_list[1].params[0] = scn;
109 result &= SDP_AddProtocolList(sdp_handle, BTA_HF_CLIENT_NUM_PROTO_ELEMS,
110 proto_elem_list);
111
112 /* add service class id list */
113 svc_class_id_list[0] = UUID_SERVCLASS_HF_HANDSFREE;
114 svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
115 result &= SDP_AddServiceClassIdList(sdp_handle, BTA_HF_CLIENT_NUM_SVC_ELEMS,
116 svc_class_id_list);
117
118 /* add profile descriptor list */
119 profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
120 version = HFP_VERSION_1_6;
121
122 result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
123
124 /* add service name */
125 if (p_service_name != NULL && p_service_name[0] != 0) {
126 result &= SDP_AddAttribute(
127 sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
128 (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
129 }
130
131 /* add features */
132 if (features & BTA_HF_CLIENT_FEAT_ECNR)
133 sdp_features |= BTA_HF_CLIENT_FEAT_ECNR;
134
135 if (features & BTA_HF_CLIENT_FEAT_3WAY)
136 sdp_features |= BTA_HF_CLIENT_FEAT_3WAY;
137
138 if (features & BTA_HF_CLIENT_FEAT_CLI) sdp_features |= BTA_HF_CLIENT_FEAT_CLI;
139
140 if (features & BTA_HF_CLIENT_FEAT_VREC)
141 sdp_features |= BTA_HF_CLIENT_FEAT_VREC;
142
143 if (features & BTA_HF_CLIENT_FEAT_VOL) sdp_features |= BTA_HF_CLIENT_FEAT_VOL;
144
145 /* Codec bit position is different in SDP (bit 5) and in BRSF (bit 7) */
146 if (features & BTA_HF_CLIENT_FEAT_CODEC) sdp_features |= 0x0020;
147
148 UINT16_TO_BE_FIELD(buf, sdp_features);
149 result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES,
150 UINT_DESC_TYPE, 2, buf);
151
152 /* add browse group list */
153 result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1,
154 browse_list);
155
156 return result;
157 }
158
159 /*******************************************************************************
160 *
161 * Function bta_hf_client_create_record
162 *
163 * Description Create SDP record for registered service.
164 *
165 *
166 * Returns void
167 *
168 ******************************************************************************/
bta_hf_client_create_record(tBTA_HF_CLIENT_CB_ARR * client_cb_arr,const char * p_service_name)169 void bta_hf_client_create_record(tBTA_HF_CLIENT_CB_ARR* client_cb_arr,
170 const char* p_service_name) {
171 /* add sdp record if not already registered */
172 if (client_cb_arr->sdp_handle == 0) {
173 client_cb_arr->sdp_handle = SDP_CreateRecord();
174 client_cb_arr->scn = BTM_AllocateSCN();
175 bta_hf_client_add_record(p_service_name, client_cb_arr->scn,
176 client_cb_arr->features,
177 client_cb_arr->sdp_handle);
178
179 bta_sys_add_uuid(UUID_SERVCLASS_HF_HANDSFREE);
180 }
181 }
182
183 /*******************************************************************************
184 *
185 * Function bta_hf_client_del_record
186 *
187 * Description Delete SDP record for registered service.
188 *
189 *
190 * Returns void
191 *
192 ******************************************************************************/
bta_hf_client_del_record(tBTA_HF_CLIENT_CB_ARR * client_cb)193 void bta_hf_client_del_record(tBTA_HF_CLIENT_CB_ARR* client_cb) {
194 APPL_TRACE_DEBUG("%s", __func__);
195
196 if (client_cb->sdp_handle != 0) {
197 SDP_DeleteRecord(client_cb->sdp_handle);
198 client_cb->sdp_handle = 0;
199 BTM_FreeSCN(client_cb->scn);
200 BTM_SecClrService(BTM_SEC_SERVICE_HF_HANDSFREE);
201 bta_sys_remove_uuid(UUID_SERVCLASS_HF_HANDSFREE);
202 }
203 }
204
205 /*******************************************************************************
206 *
207 * Function bta_hf_client_sdp_find_attr
208 *
209 * Description Process SDP discovery results to find requested attribute
210 *
211 *
212 * Returns true if results found, false otherwise.
213 *
214 ******************************************************************************/
bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB * client_cb)215 bool bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB* client_cb) {
216 tSDP_DISC_REC* p_rec = NULL;
217 tSDP_DISC_ATTR* p_attr;
218 tSDP_PROTOCOL_ELEM pe;
219 bool result = false;
220
221 client_cb->peer_version = HFP_VERSION_1_1; /* Default version */
222
223 /* loop through all records we found */
224 while (true) {
225 /* get next record; if none found, we're done */
226 p_rec = SDP_FindServiceInDb(client_cb->p_disc_db,
227 UUID_SERVCLASS_AG_HANDSFREE, p_rec);
228 if (p_rec == NULL) {
229 break;
230 }
231
232 /* get scn from proto desc list if initiator */
233 if (client_cb->role == BTA_HF_CLIENT_INT) {
234 if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
235 client_cb->peer_scn = (uint8_t)pe.params[0];
236 } else {
237 continue;
238 }
239 }
240
241 /* get profile version (if failure, version parameter is not updated) */
242 SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_HF_HANDSFREE,
243 &client_cb->peer_version);
244
245 /* get features */
246 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
247 if (p_attr != NULL) {
248 /* Found attribute. Get value. */
249 /* There might be race condition between SDP and BRSF. */
250 /* Do not update if we already received BRSF. */
251 if (client_cb->peer_features == 0) {
252 client_cb->peer_features = p_attr->attr_value.v.u16;
253
254 /* SDP and BRSF WBS bit are different, correct it if set */
255 if (client_cb->peer_features & 0x0020) {
256 client_cb->peer_features &= ~0x0020;
257 client_cb->peer_features |= BTA_HF_CLIENT_PEER_CODEC;
258 }
259
260 /* get network for ability to reject calls */
261 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_NETWORK);
262 if (p_attr != NULL) {
263 if (p_attr->attr_value.v.u16 == 0x01) {
264 client_cb->peer_features |= BTA_HF_CLIENT_PEER_REJECT;
265 }
266 }
267 }
268 }
269
270 /* found what we needed */
271 result = true;
272 break;
273 }
274
275 APPL_TRACE_DEBUG("%s: peer_version=0x%x peer_features=0x%x", __func__,
276 client_cb->peer_version, client_cb->peer_features);
277
278 return result;
279 }
280
281 /*******************************************************************************
282 *
283 * Function bta_hf_client_do_disc
284 *
285 * Description Do service discovery.
286 *
287 *
288 * Returns void
289 *
290 ******************************************************************************/
bta_hf_client_do_disc(tBTA_HF_CLIENT_CB * client_cb)291 void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb) {
292 tSDP_UUID uuid_list[2];
293 uint16_t num_uuid = 1;
294 uint16_t attr_list[4];
295 uint8_t num_attr;
296 bool db_inited = false;
297
298 /* initiator; get proto list and features */
299 if (client_cb->role == BTA_HF_CLIENT_INT) {
300 attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
301 attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
302 attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
303 attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
304 num_attr = 4;
305 uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
306 }
307 /* acceptor; get features */
308 else {
309 attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
310 attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
311 attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
312 num_attr = 3;
313 uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
314 }
315
316 /* allocate buffer for sdp database */
317 client_cb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
318
319 /* set up service discovery database; attr happens to be attr_list len */
320 uuid_list[0].len = LEN_UUID_16;
321 uuid_list[1].len = LEN_UUID_16;
322 db_inited = SDP_InitDiscoveryDb(client_cb->p_disc_db, BT_DEFAULT_BUFFER_SIZE,
323 num_uuid, uuid_list, num_attr, attr_list);
324
325 if (db_inited) {
326 /*Service discovery not initiated */
327 db_inited = SDP_ServiceSearchAttributeRequest2(
328 client_cb->peer_addr, client_cb->p_disc_db, bta_hf_client_sdp_cback,
329 (void*)client_cb);
330 }
331
332 if (!db_inited) {
333 /*free discover db */
334 bta_hf_client_free_db(NULL);
335 /* sent failed event */
336 tBTA_HF_CLIENT_DATA msg;
337 msg.hdr.layer_specific = client_cb->handle;
338 bta_hf_client_sm_execute(BTA_HF_CLIENT_DISC_FAIL_EVT, &msg);
339 }
340 }
341
342 /*******************************************************************************
343 *
344 * Function bta_hf_client_free_db
345 *
346 * Description Free discovery database.
347 *
348 *
349 * Returns void
350 *
351 ******************************************************************************/
bta_hf_client_free_db(tBTA_HF_CLIENT_DATA * p_data)352 void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA* p_data) {
353 tBTA_HF_CLIENT_CB* client_cb =
354 bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
355 if (client_cb == NULL) {
356 APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
357 p_data->hdr.layer_specific);
358 return;
359 }
360
361 osi_free_and_reset((void**)&client_cb->p_disc_db);
362 }
363