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