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++, p++) {
247         if (p_scb == p) {
248             continue;
249         }
250         if (p->in_use && p->dealloc == FALSE) {
251             others |= p->reg_services;
252         }
253     }
254     others >>= BTA_HSP_SERVICE_ID;
255     services = p_scb->reg_services >> BTA_HSP_SERVICE_ID;
256     for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1, others >>= 1)
257     {
258         /* if service registered for this scb and not registered for any other scb */
259         if (((services & 1) == 1) && ((others & 1) == 0)) {
260             APPL_TRACE_DEBUG("bta_ag_del_records %d", i);
261             if (bta_ag_cb.profile[i].sdp_handle != 0) {
262                 SDP_DeleteRecord(bta_ag_cb.profile[i].sdp_handle);
263                 bta_ag_cb.profile[i].sdp_handle = 0;
264             }
265             BTM_FreeSCN(bta_ag_cb.profile[i].scn);
266             BTM_SecClrService(bta_ag_sec_id[i]);
267             bta_sys_remove_uuid(bta_ag_uuid[i]);
268         }
269     }
270 }
271 
272 /*******************************************************************************
273 **
274 ** Function         bta_ag_sdp_find_attr
275 **
276 ** Description      Process SDP discovery results to find requested attributes
277 **                  for requested service.
278 **
279 **
280 ** Returns          TRUE if results found, FALSE otherwise.
281 **
282 *******************************************************************************/
bta_ag_sdp_find_attr(tBTA_AG_SCB * p_scb,tBTA_SERVICE_MASK service)283 BOOLEAN bta_ag_sdp_find_attr(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK service)
284 {
285     tSDP_DISC_REC       *p_rec = NULL;
286     tSDP_DISC_ATTR      *p_attr;
287     tSDP_PROTOCOL_ELEM  pe;
288     UINT16              uuid;
289     BOOLEAN             result = FALSE;
290 
291     if (service & BTA_HFP_SERVICE_MASK) {
292         uuid = UUID_SERVCLASS_HF_HANDSFREE;
293         p_scb->peer_version = HFP_VERSION_1_1;   /* Default version */
294     } else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT) {
295         uuid = UUID_SERVCLASS_HEADSET_HS;
296         p_scb->peer_version = 0x0100;   /* Default version */
297     } else {
298         return result;
299     }
300 
301     /* loop through all records we found */
302     while (TRUE)
303     {
304         /* get next record; if none found, we're done */
305         if ((p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec)) == NULL) {
306             if (uuid == UUID_SERVCLASS_HEADSET_HS) {
307                 /* Search again in case the peer device is HSP v1.0 */
308                 uuid = UUID_SERVCLASS_HEADSET;
309                 if ((p_rec = SDP_FindServiceInDb(p_scb->p_disc_db, uuid, p_rec)) == NULL) {
310                     break;
311                 }
312             } else {
313                 break;
314             }
315         }
316 
317         /* get scn from proto desc list if initiator */
318         if (p_scb->role == BTA_AG_INT) {
319             if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
320                 p_scb->peer_scn = (UINT8) pe.params[0];
321             } else {
322                 continue;
323             }
324         }
325         /* get profile version (if failure, version parameter is not updated) */
326         SDP_FindProfileVersionInRec(p_rec, uuid, &p_scb->peer_version);
327         /* get features if HFP */
328         if (service & BTA_HFP_SERVICE_MASK) {
329             if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
330                 /* Found attribute. Get value. */
331                 /* There might be race condition between SDP and BRSF.  */
332                 /* Do not update if we already received BRSF.           */
333                 if (p_scb->peer_features == 0)
334                     p_scb->peer_features = p_attr->attr_value.v.u16;
335             }
336         } else {
337             /* HSP */
338             if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL)) != NULL) {
339                 /* Remote volume control of HSP */
340                 if (p_attr->attr_value.v.u8) {
341                     p_scb->peer_features |= BTA_AG_PEER_FEAT_VOL;
342                 } else {
343                     p_scb->peer_features &= ~BTA_AG_PEER_FEAT_VOL;
344                 }
345             }
346         }
347         /* found what we needed */
348         result = TRUE;
349         break;
350     }
351     return result;
352 }
353 
354 /*******************************************************************************
355 **
356 ** Function         bta_ag_do_disc
357 **
358 ** Description      Do service discovery.
359 **
360 **
361 ** Returns          void
362 **
363 *******************************************************************************/
bta_ag_do_disc(tBTA_AG_SCB * p_scb,tBTA_SERVICE_MASK service)364 void bta_ag_do_disc(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK service)
365 {
366     tSDP_UUID       uuid_list[2];
367     UINT16          num_uuid = 1;
368     UINT16          attr_list[4];
369     UINT8           num_attr;
370     BOOLEAN         db_inited = FALSE;
371 
372     /* HFP initiator; get proto list and features */
373     if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_INT) {
374         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
375         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
376         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
377         attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
378         num_attr = 4;
379         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
380     } else if (service & BTA_HFP_SERVICE_MASK && p_scb->role == BTA_AG_ACP) {
381         /* HFP acceptor; get features */
382         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
383         attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
384         attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
385         num_attr = 3;
386         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HF_HANDSFREE;
387     } else if (service & BTA_HSP_SERVICE_MASK && p_scb->role == BTA_AG_INT) {
388         /* HSP initiator; get proto list */
389         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
390         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
391         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
392         attr_list[3] = ATTR_ID_REMOTE_AUDIO_VOLUME_CONTROL;
393         num_attr = 4;
394         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_HEADSET;        /* Legacy from HSP v1.0 */
395         if (p_scb->hsp_version >= HSP_VERSION_1_2) {
396             uuid_list[1].uu.uuid16 = UUID_SERVCLASS_HEADSET_HS;
397             num_uuid = 2;
398         }
399     } else {
400         /* HSP acceptor; no discovery */
401         return;
402     }
403 
404     /* allocate buffer for sdp database */
405     p_scb->p_disc_db = (tSDP_DISCOVERY_DB *) osi_malloc(BTA_AG_DISC_BUF_SIZE);
406     if(p_scb->p_disc_db) {
407         /* set up service discovery database; attr happens to be attr_list len */
408         uuid_list[0].len = LEN_UUID_16;
409         uuid_list[1].len = LEN_UUID_16;
410         db_inited = SDP_InitDiscoveryDb(p_scb->p_disc_db, BTA_AG_DISC_BUF_SIZE, num_uuid,
411                             uuid_list, num_attr, attr_list);
412     }
413 
414     if(db_inited) {
415         /*Service discovery not initiated */
416         db_inited = SDP_ServiceSearchAttributeRequest(p_scb->peer_addr, p_scb->p_disc_db,
417                                       bta_ag_sdp_cback_tbl[bta_ag_scb_to_idx(p_scb) - 1]);
418     }
419     if(!db_inited) {
420         /*free discover db */
421         bta_ag_free_db(p_scb, NULL);
422         /* sent failed event */
423         bta_ag_sm_execute(p_scb, BTA_AG_DISC_FAIL_EVT, NULL);
424     }
425 }
426 
427 /*******************************************************************************
428 **
429 ** Function         bta_ag_free_db
430 **
431 ** Description      Free discovery database.
432 **
433 **
434 ** Returns          void
435 **
436 *******************************************************************************/
bta_ag_free_db(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)437 void bta_ag_free_db(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
438 {
439     UNUSED(p_data);
440     if (p_scb->p_disc_db != NULL) {
441         osi_free(p_scb->p_disc_db);
442         p_scb->p_disc_db = NULL;
443     }
444 }
445 
446 #endif /* #if (BTA_AG_INCLUDED == TRUE) */
447