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