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 audio gateway functions performing SDP
22 * operations.
23 *
24 ******************************************************************************/
25
26 #include <string.h>
27
28 #include "bt_common.h"
29 #include "bta_ag_api.h"
30 #include "bta_ag_int.h"
31 #include "bta_api.h"
32 #include "bta_sys.h"
33 #include "btm_api.h"
34 #include "osi/include/osi.h"
35 #include "sdp_api.h"
36 #include "utl.h"
37
38 /* Number of protocol elements in protocol element list. */
39 #define BTA_AG_NUM_PROTO_ELEMS 2
40
41 /* Number of elements in service class id list. */
42 #define BTA_AG_NUM_SVC_ELEMS 2
43
44 /* size of database for service discovery */
45 #ifndef BTA_AG_DISC_BUF_SIZE
46 #define BTA_AG_DISC_BUF_SIZE BT_DEFAULT_BUFFER_SIZE
47 #endif
48
49 /* declare sdp callback functions */
50 void bta_ag_sdp_cback_1(uint16_t status);
51 void bta_ag_sdp_cback_2(uint16_t status);
52 void bta_ag_sdp_cback_3(uint16_t status);
53
54 /* SDP callback function table */
55 typedef tSDP_DISC_CMPL_CB* tBTA_AG_SDP_CBACK;
56 const tBTA_AG_SDP_CBACK bta_ag_sdp_cback_tbl[] = {
57 bta_ag_sdp_cback_1, bta_ag_sdp_cback_2, bta_ag_sdp_cback_3};
58
59 /*******************************************************************************
60 *
61 * Function bta_ag_sdp_cback
62 *
63 * Description SDP callback function.
64 *
65 *
66 * Returns void
67 *
68 ******************************************************************************/
bta_ag_sdp_cback(uint16_t status,uint8_t idx)69 static void bta_ag_sdp_cback(uint16_t status, uint8_t idx) {
70 uint16_t event;
71 tBTA_AG_SCB* p_scb;
72
73 APPL_TRACE_DEBUG("%s status:0x%x", __func__, status);
74
75 p_scb = bta_ag_scb_by_idx(idx);
76 if (p_scb != NULL) {
77 /* set event according to int/acp */
78 if (p_scb->role == BTA_AG_ACP) {
79 event = BTA_AG_DISC_ACP_RES_EVT;
80 } else {
81 event = BTA_AG_DISC_INT_RES_EVT;
82 }
83
84 tBTA_AG_DISC_RESULT* p_buf =
85 (tBTA_AG_DISC_RESULT*)osi_malloc(sizeof(tBTA_AG_DISC_RESULT));
86 p_buf->hdr.event = event;
87 p_buf->hdr.layer_specific = idx;
88 p_buf->status = status;
89 bta_sys_sendmsg(p_buf);
90 }
91 }
92
93 /*******************************************************************************
94 *
95 * Function bta_ag_sdp_cback_1 to 3
96 *
97 * Description SDP callback functions. Since there is no way to
98 * distinguish scb from the callback we need separate
99 * callbacks for each scb.
100 *
101 *
102 * Returns void
103 *
104 ******************************************************************************/
bta_ag_sdp_cback_1(uint16_t status)105 void bta_ag_sdp_cback_1(uint16_t status) { bta_ag_sdp_cback(status, 1); }
bta_ag_sdp_cback_2(uint16_t status)106 void bta_ag_sdp_cback_2(uint16_t status) { bta_ag_sdp_cback(status, 2); }
bta_ag_sdp_cback_3(uint16_t status)107 void bta_ag_sdp_cback_3(uint16_t status) { bta_ag_sdp_cback(status, 3); }
108
109 /******************************************************************************
110 *
111 * Function bta_ag_add_record
112 *
113 * Description This function is called by a server application to add
114 * HSP or HFP information to an SDP record. Prior to
115 * calling this function the application must call
116 * SDP_CreateRecord() to create an SDP record.
117 *
118 * Returns true if function execution succeeded,
119 * false if function execution failed.
120 *
121 *****************************************************************************/
bta_ag_add_record(uint16_t service_uuid,char * p_service_name,uint8_t scn,tBTA_AG_FEAT features,uint32_t sdp_handle)122 bool bta_ag_add_record(uint16_t service_uuid, char* p_service_name, uint8_t scn,
123 tBTA_AG_FEAT features, uint32_t sdp_handle) {
124 tSDP_PROTOCOL_ELEM proto_elem_list[BTA_AG_NUM_PROTO_ELEMS];
125 uint16_t svc_class_id_list[BTA_AG_NUM_SVC_ELEMS];
126 uint16_t browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
127 uint16_t version;
128 uint16_t profile_uuid;
129 uint8_t network;
130 bool result = true;
131 bool codec_supported = false;
132 uint8_t buf[2];
133
134 APPL_TRACE_DEBUG("%s uuid: %x", __func__, service_uuid);
135
136 memset(proto_elem_list, 0,
137 BTA_AG_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
138
139 /* add the protocol element sequence */
140 proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
141 proto_elem_list[0].num_params = 0;
142 proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
143 proto_elem_list[1].num_params = 1;
144 proto_elem_list[1].params[0] = scn;
145 result &=
146 SDP_AddProtocolList(sdp_handle, BTA_AG_NUM_PROTO_ELEMS, proto_elem_list);
147
148 /* add service class id list */
149 svc_class_id_list[0] = service_uuid;
150 svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
151 result &= SDP_AddServiceClassIdList(sdp_handle, BTA_AG_NUM_SVC_ELEMS,
152 svc_class_id_list);
153
154 /* add profile descriptor list */
155 if (service_uuid == UUID_SERVCLASS_AG_HANDSFREE) {
156 profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
157 version = BTA_HFP_VERSION;
158 } else {
159 profile_uuid = UUID_SERVCLASS_HEADSET;
160 version = HSP_VERSION_1_2;
161 }
162 result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
163
164 /* add service name */
165 if (p_service_name != NULL && p_service_name[0] != 0) {
166 result &= SDP_AddAttribute(
167 sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
168 (uint32_t)(strlen(p_service_name) + 1), (uint8_t*)p_service_name);
169 }
170
171 /* add features and network */
172 if (service_uuid == UUID_SERVCLASS_AG_HANDSFREE) {
173 network = (features & BTA_AG_FEAT_REJECT) ? 1 : 0;
174 result &= SDP_AddAttribute(sdp_handle, ATTR_ID_DATA_STORES_OR_NETWORK,
175 UINT_DESC_TYPE, 1, &network);
176
177 if (features & BTA_AG_FEAT_CODEC) codec_supported = true;
178
179 features &= BTA_AG_SDP_FEAT_SPEC;
180
181 /* Codec bit position is different in SDP and in BRSF */
182 if (codec_supported) features |= 0x0020;
183
184 UINT16_TO_BE_FIELD(buf, features);
185 result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES,
186 UINT_DESC_TYPE, 2, buf);
187 }
188
189 /* add browse group list */
190 result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1,
191 browse_list);
192
193 return result;
194 }
195
196 /*******************************************************************************
197 *
198 * Function bta_ag_create_records
199 *
200 * Description Create SDP records for registered services.
201 *
202 *
203 * Returns void
204 *
205 ******************************************************************************/
bta_ag_create_records(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)206 void bta_ag_create_records(tBTA_AG_SCB* p_scb, tBTA_AG_DATA* p_data) {
207 int i;
208 tBTA_SERVICE_MASK services;
209
210 services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
211 for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1) {
212 /* if service is set in mask */
213 if (services & 1) {
214 /* add sdp record if not already registered */
215 if (bta_ag_cb.profile[i].sdp_handle == 0) {
216 bta_ag_cb.profile[i].sdp_handle = SDP_CreateRecord();
217 bta_ag_cb.profile[i].scn = BTM_AllocateSCN();
218 bta_ag_add_record(bta_ag_uuid[i], p_data->api_register.p_name[i],
219 bta_ag_cb.profile[i].scn,
220 p_data->api_register.features,
221 bta_ag_cb.profile[i].sdp_handle);
222 bta_sys_add_uuid(bta_ag_uuid[i]);
223 }
224 }
225 }
226
227 p_scb->hsp_version = HSP_VERSION_1_2;
228 }
229
230 /*******************************************************************************
231 *
232 * Function bta_ag_del_records
233 *
234 * Description Delete SDP records for any registered services.
235 *
236 *
237 * Returns void
238 *
239 ******************************************************************************/
bta_ag_del_records(tBTA_AG_SCB * p_scb,UNUSED_ATTR tBTA_AG_DATA * p_data)240 void bta_ag_del_records(tBTA_AG_SCB* p_scb, UNUSED_ATTR tBTA_AG_DATA* p_data) {
241 tBTA_AG_SCB* p = &bta_ag_cb.scb[0];
242 tBTA_SERVICE_MASK services;
243 tBTA_SERVICE_MASK others = 0;
244 int i;
245
246 /* get services of all other registered servers */
247 for (i = 0; i < BTA_AG_NUM_IDX; i++, p++) {
248 if (p_scb == p) {
249 continue;
250 }
251
252 if (p->in_use && p->dealloc == false) {
253 others |= p->reg_services;
254 }
255 }
256
257 others >>= BTA_HSP_SERVICE_ID;
258 services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
259 for (i = 0; i < BTA_AG_NUM_IDX && services != 0;
260 i++, services >>= 1, others >>= 1) {
261 /* if service registered for this scb and not registered for any other scb
262 */
263 if (((services & 1) == 1) && ((others & 1) == 0)) {
264 APPL_TRACE_DEBUG("bta_ag_del_records %d", i);
265 if (bta_ag_cb.profile[i].sdp_handle != 0) {
266 SDP_DeleteRecord(bta_ag_cb.profile[i].sdp_handle);
267 bta_ag_cb.profile[i].sdp_handle = 0;
268 }
269 BTM_FreeSCN(bta_ag_cb.profile[i].scn);
270 BTM_SecClrService(bta_ag_sec_id[i]);
271 bta_sys_remove_uuid(bta_ag_uuid[i]);
272 }
273 }
274 }
275
276 /*******************************************************************************
277 *
278 * Function bta_ag_sdp_find_attr
279 *
280 * Description Process SDP discovery results to find requested attributes
281 * for requested service.
282 *
283 *
284 * Returns true if results found, false otherwise.
285 *
286 ******************************************************************************/
bta_ag_sdp_find_attr(tBTA_AG_SCB * p_scb,tBTA_SERVICE_MASK service)287 bool bta_ag_sdp_find_attr(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) {
288 tSDP_DISC_REC* p_rec = NULL;
289 tSDP_DISC_ATTR* p_attr;
290 tSDP_PROTOCOL_ELEM pe;
291 uint16_t uuid;
292 bool result = false;
293
294 if (service & BTA_HFP_SERVICE_MASK) {
295 uuid = UUID_SERVCLASS_HF_HANDSFREE;
296 p_scb->peer_version = HFP_VERSION_1_1; /* Default version */
297 } else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT) {
298 uuid = UUID_SERVCLASS_HEADSET_HS;
299 p_scb->peer_version = HSP_VERSION_1_2; /* Default version */
300 } else {
301 return result;
302 }
303
304 /* loop through all records we found */
305 while (true) {
306 /* get next record; if none found, we're done */
307 p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec);
308 if (p_rec == NULL) {
309 if (uuid == UUID_SERVCLASS_HEADSET_HS) {
310 /* Search again in case the peer device uses the old HSP UUID */
311 uuid = UUID_SERVCLASS_HEADSET;
312 p_scb->peer_version = HSP_VERSION_1_0;
313 p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec);
314 if (p_rec == NULL) {
315 break;
316 }
317 } else
318 break;
319 }
320
321 /* get scn from proto desc list if initiator */
322 if (p_scb->role == BTA_AG_INT) {
323 if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
324 p_scb->peer_scn = (uint8_t)pe.params[0];
325 } else {
326 continue;
327 }
328 }
329
330 /* get profile version (if failure, version parameter is not updated) */
331 if (!SDP_FindProfileVersionInRec(p_rec, uuid, &p_scb->peer_version)) {
332 APPL_TRACE_WARNING("%s: Get peer_version failed, using default 0x%04x",
333 __func__, p_scb->peer_version);
334 }
335
336 /* get features if HFP */
337 if (service & BTA_HFP_SERVICE_MASK) {
338 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
339 if (p_attr != NULL) {
340 /* Found attribute. Get value. */
341 /* There might be race condition between SDP and BRSF. */
342 /* Do not update if we already received BRSF. */
343 if (p_scb->peer_features == 0)
344 p_scb->peer_features = p_attr->attr_value.v.u16;
345 }
346 } else /* HSP */
347 {
348 p_attr =
349 SDP_FindAttributeInRec(p_rec, ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL);
350 if (p_attr != NULL) {
351 /* Remote volume control of HSP */
352 if (p_attr->attr_value.v.u8)
353 p_scb->peer_features |= BTA_AG_PEER_FEAT_VOL;
354 else
355 p_scb->peer_features &= ~BTA_AG_PEER_FEAT_VOL;
356 }
357 }
358
359 /* found what we needed */
360 result = true;
361 break;
362 }
363 return result;
364 }
365
366 /*******************************************************************************
367 *
368 * Function bta_ag_do_disc
369 *
370 * Description Do service discovery.
371 *
372 *
373 * Returns void
374 *
375 ******************************************************************************/
bta_ag_do_disc(tBTA_AG_SCB * p_scb,tBTA_SERVICE_MASK service)376 void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service) {
377 tSDP_UUID uuid_list[1];
378 uint16_t num_uuid = 1;
379 uint16_t attr_list[4];
380 uint8_t num_attr;
381 bool db_inited = false;
382
383 /* HFP initiator; get proto list and features */
384 if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_INT) {
385 attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
386 attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
387 attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
388 attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
389 num_attr = 4;
390 uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
391 }
392 /* HFP acceptor; get features */
393 else if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_ACP) {
394 attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
395 attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
396 attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
397 num_attr = 3;
398 uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
399 }
400 /* HSP initiator; get proto list */
401 else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT) {
402 attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
403 attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
404 attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
405 attr_list[3] = ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL;
406 num_attr = 4;
407 // Although UUID_SERVCLASS_HEADSET_HS (0x1131) is to be used in HSP 1.2,
408 // some HSP 1.2 implementations, such as PTS, still use
409 // UUID_SERVCLASS_HEADSET (0x1108) to store its service record. However,
410 // most of such devices are HSP 1.0 devices.
411 if (p_scb->hsp_version >= HSP_VERSION_1_2) {
412 uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET_HS;
413 } else {
414 uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET;
415 }
416 }
417 /* HSP acceptor; no discovery */
418 else {
419 return;
420 }
421
422 /* allocate buffer for sdp database */
423 p_scb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BTA_AG_DISC_BUF_SIZE);
424 /* set up service discovery database; attr happens to be attr_list len */
425 uuid_list[0].len = LEN_UUID_16;
426 db_inited = SDP_InitDiscoveryDb(p_scb->p_disc_db, BTA_AG_DISC_BUF_SIZE,
427 num_uuid, uuid_list, num_attr, attr_list);
428
429 if (db_inited) {
430 /*Service discovery not initiated */
431 db_inited = SDP_ServiceSearchAttributeRequest(
432 p_scb->peer_addr, p_scb->p_disc_db,
433 bta_ag_sdp_cback_tbl[bta_ag_scb_to_idx(p_scb) - 1]);
434 }
435
436 if (!db_inited) {
437 /*free discover db */
438 bta_ag_free_db(p_scb, NULL);
439 /* sent failed event */
440 bta_ag_sm_execute(p_scb, BTA_AG_DISC_FAIL_EVT, NULL);
441 }
442 }
443
444 /*******************************************************************************
445 *
446 * Function bta_ag_free_db
447 *
448 * Description Free discovery database.
449 *
450 *
451 * Returns void
452 *
453 ******************************************************************************/
bta_ag_free_db(tBTA_AG_SCB * p_scb,UNUSED_ATTR tBTA_AG_DATA * p_data)454 void bta_ag_free_db(tBTA_AG_SCB* p_scb, UNUSED_ATTR tBTA_AG_DATA* p_data) {
455 osi_free_and_reset((void**)&p_scb->p_disc_db);
456 }
457