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