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 #include "bta/bta_api.h"
29 #include "bta/bta_sys.h"
30 #include "common/bt_defs.h"
31 #include "bta/bta_hf_client_api.h"
32 #include "bta_hf_client_int.h"
33 #include "osi/allocator.h"
34 
35 #if (BTA_HF_INCLUDED == TRUE)
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 status)52 static void bta_hf_client_sdp_cback(UINT16 status)
53 {
54     tBTA_HF_CLIENT_DISC_RESULT *p_buf;
55     UINT16                     event;
56 
57     APPL_TRACE_DEBUG("bta_hf_client_sdp_cback status:0x%x", status);
58 
59     /* set event according to int/acp */
60     if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_ACP) {
61         event = BTA_HF_CLIENT_DISC_ACP_RES_EVT;
62     } else {
63         event = BTA_HF_CLIENT_DISC_INT_RES_EVT;
64     }
65 
66     if ((p_buf = (tBTA_HF_CLIENT_DISC_RESULT *) osi_malloc(sizeof(tBTA_HF_CLIENT_DISC_RESULT))) != NULL) {
67         p_buf->hdr.event = event;
68         p_buf->status = status;
69         bta_sys_sendmsg(p_buf);
70     }
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(char * p_service_name,UINT8 scn,tBTA_HF_CLIENT_FEAT features,UINT32 sdp_handle)86 BOOLEAN bta_hf_client_add_record(char *p_service_name, UINT8 scn,
87                                  tBTA_HF_CLIENT_FEAT features, UINT32 sdp_handle)
88 {
89     tSDP_PROTOCOL_ELEM  proto_elem_list[BTA_HF_CLIENT_NUM_PROTO_ELEMS];
90     UINT16              svc_class_id_list[BTA_HF_CLIENT_NUM_SVC_ELEMS];
91     UINT16              browse_list[] = {UUID_SERVCLASS_PUBLIC_BROWSE_GROUP};
92     UINT16              version;
93     UINT16              profile_uuid;
94     BOOLEAN             result = TRUE;
95     UINT8               buf[2];
96     UINT16              sdp_features = 0;
97 
98     APPL_TRACE_DEBUG("bta_hf_client_add_record");
99 
100     memset( proto_elem_list, 0 , BTA_HF_CLIENT_NUM_PROTO_ELEMS * sizeof(tSDP_PROTOCOL_ELEM));
101 
102     /* add the protocol element sequence */
103     proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
104     proto_elem_list[0].num_params = 0;
105     proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
106     proto_elem_list[1].num_params = 1;
107     proto_elem_list[1].params[0] = scn;
108     result &= SDP_AddProtocolList(sdp_handle, BTA_HF_CLIENT_NUM_PROTO_ELEMS, proto_elem_list);
109 
110     /* add service class id list */
111     svc_class_id_list[0] = UUID_SERVCLASS_HF_HANDSFREE;
112     svc_class_id_list[1] = UUID_SERVCLASS_GENERIC_AUDIO;
113     result &= SDP_AddServiceClassIdList(sdp_handle, BTA_HF_CLIENT_NUM_SVC_ELEMS, svc_class_id_list);
114 
115     /* add profile descriptor list */
116     profile_uuid = UUID_SERVCLASS_HF_HANDSFREE;
117     version = HFP_VERSION_1_8;
118 
119     result &= SDP_AddProfileDescriptorList(sdp_handle, profile_uuid, version);
120 
121     /* add service name */
122     if (p_service_name != NULL && p_service_name[0] != 0) {
123         result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME, TEXT_STR_DESC_TYPE,
124                                    (UINT32)(strlen(p_service_name) + 1), (UINT8 *) p_service_name);
125     }
126 
127     /* add features */
128     if (features & BTA_HF_CLIENT_FEAT_ECNR) {
129         sdp_features |= BTA_HF_CLIENT_FEAT_ECNR;
130     }
131 
132     if (features & BTA_HF_CLIENT_FEAT_3WAY) {
133         sdp_features |= BTA_HF_CLIENT_FEAT_3WAY;
134     }
135 
136     if (features & BTA_HF_CLIENT_FEAT_CLI) {
137         sdp_features |= BTA_HF_CLIENT_FEAT_CLI;
138     }
139 
140     if (features & BTA_HF_CLIENT_FEAT_VREC) {
141         sdp_features |= BTA_HF_CLIENT_FEAT_VREC;
142     }
143 
144     if (features & BTA_HF_CLIENT_FEAT_VOL) {
145         sdp_features |= BTA_HF_CLIENT_FEAT_VOL;
146     }
147 
148     /* Codec bit position is different in SDP (bit 5) and in BRSF (bit 7) */
149     if (features & BTA_HF_CLIENT_FEAT_CODEC) {
150         sdp_features |= 0x0020;
151     }
152 
153     UINT16_TO_BE_FIELD(buf, sdp_features);
154     result &= SDP_AddAttribute(sdp_handle, ATTR_ID_SUPPORTED_FEATURES, UINT_DESC_TYPE, 2, buf);
155 
156     /* add browse group list */
157     result &= SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, browse_list);
158 
159     return result;
160 }
161 
162 /*******************************************************************************
163 **
164 ** Function         bta_hf_client_create_record
165 **
166 ** Description      Create SDP record for registered service.
167 **
168 **
169 ** Returns          void
170 **
171 *******************************************************************************/
bta_hf_client_create_record(tBTA_HF_CLIENT_DATA * p_data)172 void bta_hf_client_create_record(tBTA_HF_CLIENT_DATA *p_data)
173 {
174     /* add sdp record if not already registered */
175     if (bta_hf_client_cb.sdp_handle == 0) {
176         bta_hf_client_cb.sdp_handle = SDP_CreateRecord();
177         bta_hf_client_cb.scn = BTM_AllocateSCN();
178         bta_hf_client_add_record(p_data->api_register.name,
179                                  bta_hf_client_cb.scn,
180                                  p_data->api_register.features,
181                                  bta_hf_client_cb.sdp_handle);
182 
183         bta_sys_add_uuid(UUID_SERVCLASS_HF_HANDSFREE);
184     }
185 
186 }
187 
188 /*******************************************************************************
189 **
190 ** Function         bta_hf_client_del_record
191 **
192 ** Description      Delete SDP record for registered service.
193 **
194 **
195 ** Returns          void
196 **
197 *******************************************************************************/
bta_hf_client_del_record(tBTA_HF_CLIENT_DATA * p_data)198 void bta_hf_client_del_record(tBTA_HF_CLIENT_DATA *p_data)
199 {
200     UNUSED(p_data);
201 
202     APPL_TRACE_DEBUG("bta_hf_client_del_record");
203 
204     if (bta_hf_client_cb.sdp_handle != 0) {
205         SDP_DeleteRecord(bta_hf_client_cb.sdp_handle);
206         bta_hf_client_cb.sdp_handle = 0;
207         BTM_FreeSCN(bta_hf_client_cb.scn);
208         BTM_SecClrService(BTM_SEC_SERVICE_HF_HANDSFREE);
209         bta_sys_remove_uuid(UUID_SERVCLASS_HF_HANDSFREE);
210     }
211 }
212 
213 /*******************************************************************************
214 **
215 ** Function         bta_hf_client_sdp_find_attr
216 **
217 ** Description      Process SDP discovery results to find requested attribute
218 **
219 **
220 ** Returns          TRUE if results found, FALSE otherwise.
221 **
222 *******************************************************************************/
bta_hf_client_sdp_find_attr(void)223 BOOLEAN bta_hf_client_sdp_find_attr(void)
224 {
225     tSDP_DISC_REC       *p_rec = NULL;
226     tSDP_DISC_ATTR      *p_attr;
227     tSDP_PROTOCOL_ELEM  pe;
228     BOOLEAN             result = FALSE;
229 
230     bta_hf_client_cb.scb.peer_version = HFP_VERSION_1_1;   /* Default version */
231 
232     /* loop through all records we found */
233     while (TRUE) {
234         /* get next record; if none found, we're done */
235         if ((p_rec = SDP_FindServiceInDb(bta_hf_client_cb.scb.p_disc_db, UUID_SERVCLASS_AG_HANDSFREE, p_rec)) == NULL) {
236             break;
237         }
238 
239         /* get scn from proto desc list if initiator */
240         if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_INT) {
241             if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
242                 bta_hf_client_cb.scb.peer_scn = (UINT8) pe.params[0];
243             } else {
244                 continue;
245             }
246         }
247 
248         /* get profile version (if failure, version parameter is not updated) */
249         SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_HF_HANDSFREE, &bta_hf_client_cb.scb.peer_version);
250 
251         /* get features */
252         if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES)) != NULL) {
253             /* Found attribute. Get value. */
254             /* There might be race condition between SDP and BRSF.  */
255             /* Do not update if we already received BRSF.           */
256             if (bta_hf_client_cb.scb.peer_features == 0) {
257                 bta_hf_client_cb.scb.peer_features = p_attr->attr_value.v.u16;
258 
259                 /* SDP and BRSF WBS bit are different, correct it if set */
260                 if (bta_hf_client_cb.scb.peer_features & 0x0020) {
261                     bta_hf_client_cb.scb.peer_features &= ~0x0020;
262                     bta_hf_client_cb.scb.peer_features |= BTA_HF_CLIENT_PEER_CODEC;
263                 }
264 
265                 /* get network for ability to reject calls */
266                 if ((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_NETWORK)) != NULL) {
267                     if (p_attr->attr_value.v.u16 == 0x01) {
268                         bta_hf_client_cb.scb.peer_features |= BTA_HF_CLIENT_PEER_REJECT;
269                     }
270                 }
271             }
272         }
273 
274         /* found what we needed */
275         result = TRUE;
276         break;
277     }
278 
279     APPL_TRACE_DEBUG("%s peer_version=0x%x peer_features=0x%x",
280                      __FUNCTION__, bta_hf_client_cb.scb.peer_version,
281                      bta_hf_client_cb.scb.peer_features);
282 
283     return result;
284 }
285 
286 /*******************************************************************************
287 **
288 ** Function         bta_hf_client_do_disc
289 **
290 ** Description      Do service discovery.
291 **
292 **
293 ** Returns          void
294 **
295 *******************************************************************************/
bta_hf_client_do_disc(void)296 void bta_hf_client_do_disc(void)
297 {
298     tSDP_UUID       uuid_list[2];
299     UINT16          num_uuid = 1;
300     UINT16          attr_list[4];
301     UINT8           num_attr;
302     BOOLEAN         db_inited = FALSE;
303 
304     /* initiator; get proto list and features */
305     if (bta_hf_client_cb.scb.role == BTA_HF_CLIENT_INT) {
306         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
307         attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
308         attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
309         attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
310         num_attr = 4;
311         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
312     }
313     /* acceptor; get features */
314     else {
315         attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
316         attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
317         attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
318         num_attr = 3;
319         uuid_list[0].uu.uuid16 = UUID_SERVCLASS_AG_HANDSFREE;
320     }
321 
322     /* allocate buffer for sdp database */
323     bta_hf_client_cb.scb.p_disc_db = (tSDP_DISCOVERY_DB *) osi_malloc(BT_DEFAULT_BUFFER_SIZE);
324 
325     if (bta_hf_client_cb.scb.p_disc_db) {
326         /* set up service discovery database; attr happens to be attr_list len */
327         uuid_list[0].len = LEN_UUID_16;
328         uuid_list[1].len = LEN_UUID_16;
329         db_inited = SDP_InitDiscoveryDb(bta_hf_client_cb.scb.p_disc_db, BT_DEFAULT_BUFFER_SIZE, num_uuid,
330                                         uuid_list, num_attr, attr_list);
331     }
332 
333     if (db_inited) {
334         /*Service discovery not initiated */
335         db_inited = SDP_ServiceSearchAttributeRequest(bta_hf_client_cb.scb.peer_addr,
336                     bta_hf_client_cb.scb.p_disc_db, bta_hf_client_sdp_cback);
337     }
338 
339     if (!db_inited) {
340         /*free discover db */
341         bta_hf_client_free_db(NULL);
342         /* sent failed event */
343         bta_hf_client_sm_execute(BTA_HF_CLIENT_DISC_FAIL_EVT, NULL);
344     }
345 
346 }
347 
348 /*******************************************************************************
349 **
350 ** Function         bta_hf_client_free_db
351 **
352 ** Description      Free discovery database.
353 **
354 **
355 ** Returns          void
356 **
357 *******************************************************************************/
bta_hf_client_free_db(tBTA_HF_CLIENT_DATA * p_data)358 void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA *p_data)
359 {
360     UNUSED(p_data);
361 
362     if (bta_hf_client_cb.scb.p_disc_db != NULL) {
363         osi_free(bta_hf_client_cb.scb.p_disc_db);
364         bta_hf_client_cb.scb.p_disc_db = NULL;
365     }
366 }
367 #endif /* #if (BTA_HF_INCLUDED == TRUE) */
368