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