• 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,void * data)59 static void bta_hf_client_sdp_cback(tSDP_STATUS status, 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 = BTA_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     RFCOMM_ClearSecurityRecord(client_cb->scn);
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(tBTA_HF_CLIENT_CB * client_cb)222 bool bta_hf_client_sdp_find_attr(tBTA_HF_CLIENT_CB* client_cb) {
223   tSDP_DISC_REC* p_rec = NULL;
224   tSDP_DISC_ATTR* p_attr;
225   tSDP_PROTOCOL_ELEM pe;
226   bool result = false;
227 
228   client_cb->peer_version = HFP_VERSION_1_1; /* Default version */
229 
230   /* loop through all records we found */
231   while (true) {
232     /* get next record; if none found, we're done */
233     p_rec = SDP_FindServiceInDb(client_cb->p_disc_db,
234                                 UUID_SERVCLASS_AG_HANDSFREE, p_rec);
235     if (p_rec == NULL) {
236       break;
237     }
238 
239     /* get scn from proto desc list if initiator */
240     if (client_cb->role == BTA_HF_CLIENT_INT) {
241       if (SDP_FindProtocolListElemInRec(p_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
242         client_cb->peer_scn = (uint8_t)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,
250                                 &client_cb->peer_version);
251 
252     /* get features */
253     p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
254     if (p_attr != NULL) {
255       /* Found attribute. Get value. */
256       /* There might be race condition between SDP and BRSF.  */
257       /* Do not update if we already received BRSF.           */
258       if (client_cb->peer_features == 0) {
259         client_cb->peer_features = p_attr->attr_value.v.u16;
260 
261         /* SDP and BRSF WBS bit are different, correct it if set */
262         if (client_cb->peer_features & 0x0020) {
263           client_cb->peer_features &= ~0x0020;
264           client_cb->peer_features |= BTA_HF_CLIENT_PEER_CODEC;
265         }
266 
267         /* get network for ability to reject calls */
268         p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_NETWORK);
269         if (p_attr != NULL) {
270           if (p_attr->attr_value.v.u16 == 0x01) {
271             client_cb->peer_features |= BTA_HF_CLIENT_PEER_REJECT;
272           }
273         }
274       }
275     }
276 
277     /* found what we needed */
278     result = true;
279     break;
280   }
281 
282   APPL_TRACE_DEBUG("%s: peer_version=0x%x peer_features=0x%x", __func__,
283                    client_cb->peer_version, client_cb->peer_features);
284 
285   return result;
286 }
287 
288 /*******************************************************************************
289  *
290  * Function         bta_hf_client_do_disc
291  *
292  * Description      Do service discovery.
293  *
294  *
295  * Returns          void
296  *
297  ******************************************************************************/
bta_hf_client_do_disc(tBTA_HF_CLIENT_CB * client_cb)298 void bta_hf_client_do_disc(tBTA_HF_CLIENT_CB* client_cb) {
299   Uuid uuid_list[1];
300   uint16_t num_uuid = 1;
301   uint16_t attr_list[4];
302   uint8_t num_attr;
303   bool db_inited = false;
304 
305   /* initiator; get proto list and features */
306   if (client_cb->role == BTA_HF_CLIENT_INT) {
307     attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
308     attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST;
309     attr_list[2] = ATTR_ID_BT_PROFILE_DESC_LIST;
310     attr_list[3] = ATTR_ID_SUPPORTED_FEATURES;
311     num_attr = 4;
312     uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_AG_HANDSFREE);
313   }
314   /* acceptor; get features */
315   else {
316     attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST;
317     attr_list[1] = ATTR_ID_BT_PROFILE_DESC_LIST;
318     attr_list[2] = ATTR_ID_SUPPORTED_FEATURES;
319     num_attr = 3;
320     uuid_list[0] = Uuid::From16Bit(UUID_SERVCLASS_AG_HANDSFREE);
321   }
322 
323   /* allocate buffer for sdp database */
324   client_cb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
325 
326   /* set up service discovery database; attr happens to be attr_list len */
327   db_inited = SDP_InitDiscoveryDb(client_cb->p_disc_db, BT_DEFAULT_BUFFER_SIZE,
328                                   num_uuid, uuid_list, num_attr, attr_list);
329 
330   if (db_inited) {
331     /*Service discovery not initiated */
332     db_inited = SDP_ServiceSearchAttributeRequest2(
333         client_cb->peer_addr, client_cb->p_disc_db, bta_hf_client_sdp_cback,
334         (void*)client_cb);
335   }
336 
337   if (!db_inited) {
338     /*free discover db */
339     osi_free_and_reset((void**)&client_cb->p_disc_db);
340     /* sent failed event */
341     tBTA_HF_CLIENT_DATA msg;
342     msg.hdr.layer_specific = client_cb->handle;
343     bta_hf_client_sm_execute(BTA_HF_CLIENT_DISC_FAIL_EVT, &msg);
344   }
345 }
346 
347 /*******************************************************************************
348  *
349  * Function         bta_hf_client_free_db
350  *
351  * Description      Free discovery database.
352  *
353  *
354  * Returns          void
355  *
356  ******************************************************************************/
bta_hf_client_free_db(tBTA_HF_CLIENT_DATA * p_data)357 void bta_hf_client_free_db(tBTA_HF_CLIENT_DATA* p_data) {
358   CHECK(p_data != NULL);
359   tBTA_HF_CLIENT_CB* client_cb =
360       bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
361   if (client_cb == NULL) {
362     APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
363                      p_data->hdr.layer_specific);
364     return;
365   }
366 
367   osi_free_and_reset((void**)&client_cb->p_disc_db);
368 }
369