1 /****************************************************************************** 2 * 3 * Copyright (c) 2014 The Android Open Source Project 4 * Copyright (C) 2009-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 * Filename: btif_hf_client.c 23 * 24 * Description: Handsfree Profile (HF role) Bluetooth Interface 25 * 26 * Notes: 27 * a) Lifecycle of a control block 28 * Control block handles the lifecycle for a particular remote device's 29 * connection. The connection can go via the classic phases but more 30 * importantly there's only two messages from BTA that affect this. 31 * BTA_HF_CLIENT_OPEN_EVT and BTA_HF_CLIENT_CLOSE_EVT. Since the API between 32 * BTIF and BTA is controlled entirely by handles it's important to know where 33 * the handles are created and destroyed. Handles can be created at two 34 * locations: 35 * -- While connect() is called from BTIF. This is an outgoing connection 36 * -- While accepting an incoming connection (see BTA_HF_CLIENT_OPEN_EVT 37 * handling). 38 * 39 * The destruction or rather reuse of handles can be done when 40 * BTA_HF_CLIENT_CLOSE_EVT is called. Refer to the event handling for details 41 * of this. 42 * 43 ******************************************************************************/ 44 45 #define LOG_TAG "bt_btif_hfc" 46 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include <hardware/bluetooth.h> 51 #include <hardware/bt_hf_client.h> 52 53 #include "bt_utils.h" 54 #include "bta_hf_client_api.h" 55 #include "btif_common.h" 56 #include "btif_profile_queue.h" 57 #include "btif_util.h" 58 #include "osi/include/osi.h" 59 #include "osi/include/properties.h" 60 61 /******************************************************************************* 62 * Constants & Macros 63 ******************************************************************************/ 64 65 #ifndef BTIF_HF_CLIENT_SERVICE_NAME 66 #define BTIF_HF_CLIENT_SERVICE_NAME ("Handsfree") 67 #endif 68 69 #ifndef BTIF_HF_CLIENT_SECURITY 70 #define BTIF_HF_CLIENT_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT) 71 #endif 72 73 #ifndef BTIF_HF_CLIENT_FEATURES 74 #define BTIF_HF_CLIENT_FEATURES \ 75 (BTA_HF_CLIENT_FEAT_ECNR | BTA_HF_CLIENT_FEAT_3WAY | \ 76 BTA_HF_CLIENT_FEAT_CLI | BTA_HF_CLIENT_FEAT_VREC | BTA_HF_CLIENT_FEAT_VOL | \ 77 BTA_HF_CLIENT_FEAT_ECS | BTA_HF_CLIENT_FEAT_ECC | BTA_HF_CLIENT_FEAT_CODEC) 78 #endif 79 80 /******************************************************************************* 81 * Local type definitions 82 ******************************************************************************/ 83 /* BTIF-HF control block to map bdaddr to BTA handle */ 84 typedef struct { 85 uint16_t handle; // Handle obtained frm the BTA 86 RawAddress peer_bda; // Device corresponding to handle 87 bthf_client_connection_state_t state; // State of current connection 88 tBTA_HF_CLIENT_PEER_FEAT peer_feat; // HF features 89 tBTA_HF_CLIENT_CHLD_FEAT chld_feat; // AT+CHLD=<> command features 90 } btif_hf_client_cb_t; 91 92 /* Max devices supported by BTIF (useful to match the value in BTA) */ 93 #define HF_CLIENT_MAX_DEVICES 10 94 typedef struct { 95 btif_hf_client_cb_t cb[HF_CLIENT_MAX_DEVICES]; 96 } btif_hf_client_cb_arr_t; 97 98 /****************************************************************************** 99 * Local function declarations 100 ******************************************************************************/ 101 btif_hf_client_cb_t* btif_hf_client_get_cb_by_handle(uint16_t handle); 102 btif_hf_client_cb_t* btif_hf_client_get_cb_by_bda(const RawAddress& addr); 103 bool is_connected(const btif_hf_client_cb_t* cb); 104 105 /******************************************************************************* 106 * Static variables 107 ******************************************************************************/ 108 static bthf_client_callbacks_t* bt_hf_client_callbacks = NULL; 109 110 char btif_hf_client_version[PROPERTY_VALUE_MAX]; 111 112 #define CHECK_BTHF_CLIENT_INIT() \ 113 do { \ 114 if (bt_hf_client_callbacks == NULL) { \ 115 BTIF_TRACE_WARNING("BTHF CLIENT: %s: not initialized", __func__); \ 116 return BT_STATUS_NOT_READY; \ 117 } else { \ 118 BTIF_TRACE_EVENT("BTHF CLIENT: %s", __func__); \ 119 } \ 120 } while (0) 121 122 #define CHECK_BTHF_CLIENT_SLC_CONNECTED(cb) \ 123 do { \ 124 if (bt_hf_client_callbacks == NULL) { \ 125 BTIF_TRACE_WARNING("BTHF CLIENT: %s: not initialized", __func__); \ 126 return BT_STATUS_NOT_READY; \ 127 } else if ((cb)->state != BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED) { \ 128 BTIF_TRACE_WARNING("BTHF CLIENT: %s: SLC connection not up. state=%s", \ 129 __func__, dump_hf_conn_state((cb)->state)); \ 130 return BT_STATUS_NOT_READY; \ 131 } else { \ 132 BTIF_TRACE_EVENT("BTHF CLIENT: %s", __func__); \ 133 } \ 134 } while (0) 135 136 static btif_hf_client_cb_arr_t btif_hf_client_cb_arr; 137 138 /******************************************************************************* 139 * Static functions 140 ******************************************************************************/ 141 142 /******************************************************************************* 143 * 144 * Function btif_in_hf_client_generic_evt 145 * 146 * Description Processes generic events to be sent to JNI that are not 147 * triggered from the BTA. 148 * Always runs in BTIF context 149 * 150 * Returns void 151 * 152 ******************************************************************************/ 153 static void btif_in_hf_client_generic_evt(uint16_t event, char* p_param) { 154 BTIF_TRACE_DEBUG("%s", __func__); 155 RawAddress* bd_addr = (RawAddress*)p_param; 156 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 157 if (cb == NULL || !is_connected(cb)) { 158 BTIF_TRACE_ERROR("%s: failed to find block for bda", __func__); 159 } 160 161 BTIF_TRACE_EVENT("%s: event=%d", __func__, event); 162 switch (event) { 163 case BTIF_HF_CLIENT_CB_AUDIO_CONNECTING: { 164 HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, &cb->peer_bda, 165 (bthf_client_audio_state_t)BTHF_AUDIO_STATE_CONNECTING); 166 } break; 167 default: { 168 BTIF_TRACE_WARNING("%s: : Unknown event 0x%x", __func__, event); 169 } break; 170 } 171 } 172 173 /******************************************************************************* 174 * Functions 175 ******************************************************************************/ 176 bool is_connected(const btif_hf_client_cb_t* cb) { 177 if ((cb->state == BTHF_CLIENT_CONNECTION_STATE_CONNECTED) || 178 (cb->state == BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED)) 179 return true; 180 181 BTIF_TRACE_ERROR("%s: not connected!", __func__); 182 return false; 183 } 184 185 /******************************************************************************* 186 * 187 * Function btif_hf_client_get_cb_by_handle 188 * 189 * Description Get control block by handle 190 * 191 * Returns btif_hf_client_cb_t pointer if available NULL otherwise 192 * 193 ******************************************************************************/ 194 btif_hf_client_cb_t* btif_hf_client_get_cb_by_handle(uint16_t handle) { 195 BTIF_TRACE_DEBUG("%s: cb by handle %d", __func__, handle); 196 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) { 197 // Block is valid only if it is allocated i.e. state is not DISCONNECTED 198 if (btif_hf_client_cb_arr.cb[i].state != 199 BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED && 200 btif_hf_client_cb_arr.cb[i].handle == handle) { 201 return &btif_hf_client_cb_arr.cb[i]; 202 } 203 } 204 BTIF_TRACE_ERROR("%s: could not find block for handle %d", __func__, handle); 205 return NULL; 206 } 207 208 /******************************************************************************* 209 * 210 * Function btif_hf_client_get_cb_by_bda 211 * 212 * Description Get control block by bda 213 * 214 * Returns btif_hf_client_cb_t pointer if available NULL otherwise 215 * 216 ******************************************************************************/ 217 btif_hf_client_cb_t* btif_hf_client_get_cb_by_bda(const RawAddress& bd_addr) { 218 VLOG(1) << __func__ << " incoming addr " << bd_addr; 219 220 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) { 221 // Block is valid only if it is allocated i.e. state is not DISCONNECTED 222 if (btif_hf_client_cb_arr.cb[i].state != 223 BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED && 224 btif_hf_client_cb_arr.cb[i].peer_bda == bd_addr) { 225 return &btif_hf_client_cb_arr.cb[i]; 226 } 227 } 228 BTIF_TRACE_ERROR("%s: could not find block for bdaddr", __func__); 229 return NULL; 230 } 231 232 /******************************************************************************* 233 * 234 * Function btif_hf_client_allocate_cb 235 * 236 * Description Get control block by bda 237 * 238 * Returns btif_hf_client_cb_t pointer if available NULL otherwise 239 * 240 ******************************************************************************/ 241 btif_hf_client_cb_t* btif_hf_client_allocate_cb() { 242 for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) { 243 btif_hf_client_cb_t* cb = &btif_hf_client_cb_arr.cb[i]; 244 if (cb->state == BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED) { 245 return cb; 246 } 247 } 248 BTIF_TRACE_ERROR("%s: unable to allocate control block", __func__); 249 return NULL; 250 } 251 252 /***************************************************************************** 253 * 254 * btif hf api functions (no context switch) 255 * 256 ****************************************************************************/ 257 258 /******************************************************************************* 259 * 260 * Function btif_hf_client_init 261 * 262 * Description initializes the hf interface 263 * 264 * Returns bt_status_t 265 * 266 ******************************************************************************/ 267 static bt_status_t init(bthf_client_callbacks_t* callbacks) { 268 BTIF_TRACE_EVENT("%s", __func__); 269 270 bt_hf_client_callbacks = callbacks; 271 272 btif_enable_service(BTA_HFP_HS_SERVICE_ID); 273 274 memset(&btif_hf_client_cb_arr, 0, sizeof(btif_hf_client_cb_arr_t)); 275 276 return BT_STATUS_SUCCESS; 277 } 278 279 /******************************************************************************* 280 * 281 * Function connect 282 * 283 * Description connect to audio gateway 284 * 285 * Returns bt_status_t 286 * 287 ******************************************************************************/ 288 static bt_status_t connect_int(RawAddress* bd_addr, uint16_t uuid) { 289 btif_hf_client_cb_t* cb = btif_hf_client_allocate_cb(); 290 if (cb == NULL) { 291 BTIF_TRACE_ERROR("%s: could not allocate block!", __func__); 292 return BT_STATUS_BUSY; 293 } 294 295 cb->peer_bda = *bd_addr; 296 if (is_connected(cb)) return BT_STATUS_BUSY; 297 298 cb->state = BTHF_CLIENT_CONNECTION_STATE_CONNECTING; 299 cb->peer_bda = *bd_addr; 300 301 /* Open HF connection to remote device and get the relevant handle. 302 * The handle is valid until we have called BTA_HfClientClose or the LL 303 * has notified us of channel close due to remote closing, error etc. 304 */ 305 BTA_HfClientOpen(cb->peer_bda, BTIF_HF_CLIENT_SECURITY, &cb->handle); 306 307 return BT_STATUS_SUCCESS; 308 } 309 310 static bt_status_t connect(RawAddress* bd_addr) { 311 BTIF_TRACE_EVENT("HFP Client version is %s", btif_hf_client_version); 312 CHECK_BTHF_CLIENT_INIT(); 313 return btif_queue_connect(UUID_SERVCLASS_HF_HANDSFREE, bd_addr, connect_int); 314 } 315 316 /******************************************************************************* 317 * 318 * Function disconnect 319 * 320 * Description disconnect from audio gateway 321 * 322 * Returns bt_status_t 323 * 324 ******************************************************************************/ 325 static bt_status_t disconnect(const RawAddress* bd_addr) { 326 CHECK_BTHF_CLIENT_INIT(); 327 328 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 329 if (cb != NULL) { 330 BTA_HfClientClose(cb->handle); 331 return BT_STATUS_SUCCESS; 332 } else { 333 return BT_STATUS_BUSY; 334 } 335 } 336 337 /******************************************************************************* 338 * 339 * Function connect_audio 340 * 341 * Description create an audio connection 342 * 343 * Returns bt_status_t 344 * 345 ******************************************************************************/ 346 static bt_status_t connect_audio(const RawAddress* bd_addr) { 347 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 348 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 349 350 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 351 352 if ((BTIF_HF_CLIENT_FEATURES & BTA_HF_CLIENT_FEAT_CODEC) && 353 (cb->peer_feat & BTA_HF_CLIENT_PEER_CODEC)) { 354 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BCC, 0, 0, NULL); 355 } else { 356 BTA_HfClientAudioOpen(cb->handle); 357 } 358 359 /* Inform the application that the audio connection has been initiated 360 * successfully */ 361 btif_transfer_context(btif_in_hf_client_generic_evt, 362 BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, (char*)bd_addr, 363 sizeof(RawAddress), NULL); 364 return BT_STATUS_SUCCESS; 365 } 366 367 /******************************************************************************* 368 * 369 * Function disconnect_audio 370 * 371 * Description close the audio connection 372 * 373 * Returns bt_status_t 374 * 375 ******************************************************************************/ 376 static bt_status_t disconnect_audio(const RawAddress* bd_addr) { 377 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 378 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 379 380 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 381 382 BTA_HfClientAudioClose(cb->handle); 383 return BT_STATUS_SUCCESS; 384 } 385 386 /******************************************************************************* 387 * 388 * Function start_voice_recognition 389 * 390 * Description start voice recognition 391 * 392 * Returns bt_status_t 393 * 394 ******************************************************************************/ 395 static bt_status_t start_voice_recognition(const RawAddress* bd_addr) { 396 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 397 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 398 399 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 400 401 if (cb->peer_feat & BTA_HF_CLIENT_PEER_FEAT_VREC) { 402 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BVRA, 1, 0, NULL); 403 return BT_STATUS_SUCCESS; 404 } 405 return BT_STATUS_UNSUPPORTED; 406 } 407 408 /******************************************************************************* 409 * 410 * Function stop_voice_recognition 411 * 412 * Description stop voice recognition 413 * 414 * Returns bt_status_t 415 * 416 ******************************************************************************/ 417 static bt_status_t stop_voice_recognition(const RawAddress* bd_addr) { 418 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 419 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 420 421 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 422 423 if (cb->peer_feat & BTA_HF_CLIENT_PEER_FEAT_VREC) { 424 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BVRA, 0, 0, NULL); 425 return BT_STATUS_SUCCESS; 426 } 427 return BT_STATUS_UNSUPPORTED; 428 } 429 430 /******************************************************************************* 431 * 432 * Function volume_control 433 * 434 * Description volume control 435 * 436 * Returns bt_status_t 437 * 438 ******************************************************************************/ 439 static bt_status_t volume_control(const RawAddress* bd_addr, 440 bthf_client_volume_type_t type, int volume) { 441 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 442 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 443 444 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 445 446 switch (type) { 447 case BTHF_CLIENT_VOLUME_TYPE_SPK: 448 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_VGS, volume, 0, NULL); 449 break; 450 case BTHF_CLIENT_VOLUME_TYPE_MIC: 451 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_VGM, volume, 0, NULL); 452 break; 453 default: 454 return BT_STATUS_UNSUPPORTED; 455 } 456 457 return BT_STATUS_SUCCESS; 458 } 459 460 /******************************************************************************* 461 * 462 * Function dial 463 * 464 * Description place a call 465 * 466 * Returns bt_status_t 467 * 468 ******************************************************************************/ 469 static bt_status_t dial(UNUSED_ATTR const RawAddress* bd_addr, 470 const char* number) { 471 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 472 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 473 474 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 475 476 if (number) { 477 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_ATD, 0, 0, number); 478 } else { 479 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BLDN, 0, 0, NULL); 480 } 481 return BT_STATUS_SUCCESS; 482 } 483 484 /******************************************************************************* 485 * 486 * Function dial_memory 487 * 488 * Description place a call with number specified by location (speed dial) 489 * 490 * Returns bt_status_t 491 * 492 ******************************************************************************/ 493 static bt_status_t dial_memory(const RawAddress* bd_addr, int location) { 494 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 495 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 496 497 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 498 499 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_ATD, location, 0, NULL); 500 return BT_STATUS_SUCCESS; 501 } 502 503 /******************************************************************************* 504 * 505 * Function handle_call_action 506 * 507 * Description handle specified call related action 508 * 509 * Returns bt_status_t 510 * 511 ******************************************************************************/ 512 static bt_status_t handle_call_action(const RawAddress* bd_addr, 513 bthf_client_call_action_t action, 514 int idx) { 515 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 516 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 517 518 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 519 520 switch (action) { 521 case BTHF_CLIENT_CALL_ACTION_CHLD_0: 522 if (cb->chld_feat & BTA_HF_CLIENT_CHLD_REL) { 523 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 0, 0, NULL); 524 break; 525 } 526 return BT_STATUS_UNSUPPORTED; 527 case BTHF_CLIENT_CALL_ACTION_CHLD_1: 528 // CHLD 1 is mandatory for 3 way calling 529 if (cb->peer_feat & BTA_HF_CLIENT_PEER_FEAT_3WAY) { 530 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 1, 0, NULL); 531 break; 532 } 533 return BT_STATUS_UNSUPPORTED; 534 case BTHF_CLIENT_CALL_ACTION_CHLD_2: 535 // CHLD 2 is mandatory for 3 way calling 536 if (cb->peer_feat & BTA_HF_CLIENT_PEER_FEAT_3WAY) { 537 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 2, 0, NULL); 538 break; 539 } 540 return BT_STATUS_UNSUPPORTED; 541 case BTHF_CLIENT_CALL_ACTION_CHLD_3: 542 if (cb->chld_feat & BTA_HF_CLIENT_CHLD_MERGE) { 543 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 3, 0, NULL); 544 break; 545 } 546 return BT_STATUS_UNSUPPORTED; 547 case BTHF_CLIENT_CALL_ACTION_CHLD_4: 548 if (cb->chld_feat & BTA_HF_CLIENT_CHLD_MERGE_DETACH) { 549 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 4, 0, NULL); 550 break; 551 } 552 return BT_STATUS_UNSUPPORTED; 553 case BTHF_CLIENT_CALL_ACTION_CHLD_1x: 554 if (cb->peer_feat & BTA_HF_CLIENT_PEER_ECC) { 555 if (idx < 1) { 556 return BT_STATUS_FAIL; 557 } 558 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 1, idx, NULL); 559 break; 560 } 561 return BT_STATUS_UNSUPPORTED; 562 case BTHF_CLIENT_CALL_ACTION_CHLD_2x: 563 if (cb->peer_feat & BTA_HF_CLIENT_PEER_ECC) { 564 if (idx < 1) { 565 return BT_STATUS_FAIL; 566 } 567 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHLD, 2, idx, NULL); 568 break; 569 } 570 return BT_STATUS_UNSUPPORTED; 571 case BTHF_CLIENT_CALL_ACTION_ATA: 572 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_ATA, 0, 0, NULL); 573 break; 574 case BTHF_CLIENT_CALL_ACTION_CHUP: 575 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CHUP, 0, 0, NULL); 576 break; 577 case BTHF_CLIENT_CALL_ACTION_BTRH_0: 578 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BTRH, 0, 0, NULL); 579 break; 580 case BTHF_CLIENT_CALL_ACTION_BTRH_1: 581 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BTRH, 1, 0, NULL); 582 break; 583 case BTHF_CLIENT_CALL_ACTION_BTRH_2: 584 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BTRH, 2, 0, NULL); 585 break; 586 default: 587 return BT_STATUS_FAIL; 588 } 589 590 return BT_STATUS_SUCCESS; 591 } 592 593 /******************************************************************************* 594 * 595 * Function query_current_calls 596 * 597 * Description query list of current calls 598 * 599 * Returns bt_status_t 600 * 601 ******************************************************************************/ 602 static bt_status_t query_current_calls(UNUSED_ATTR const RawAddress* bd_addr) { 603 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 604 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 605 606 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 607 608 if (cb->peer_feat & BTA_HF_CLIENT_PEER_ECS) { 609 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CLCC, 0, 0, NULL); 610 return BT_STATUS_SUCCESS; 611 } 612 613 return BT_STATUS_UNSUPPORTED; 614 } 615 616 /******************************************************************************* 617 * 618 * Function query_current_operator_name 619 * 620 * Description query current selected operator name 621 * 622 * Returns bt_status_t 623 * 624 ******************************************************************************/ 625 static bt_status_t query_current_operator_name(const RawAddress* bd_addr) { 626 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 627 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 628 629 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 630 631 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_COPS, 0, 0, NULL); 632 return BT_STATUS_SUCCESS; 633 } 634 635 /******************************************************************************* 636 * 637 * Function retieve_subscriber_info 638 * 639 * Description retrieve subscriber number information 640 * 641 * Returns bt_status_t 642 * 643 ******************************************************************************/ 644 static bt_status_t retrieve_subscriber_info(const RawAddress* bd_addr) { 645 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 646 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 647 648 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 649 650 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_CNUM, 0, 0, NULL); 651 return BT_STATUS_SUCCESS; 652 } 653 654 /******************************************************************************* 655 * 656 * Function send_dtmf 657 * 658 * Description send dtmf 659 * 660 * Returns bt_status_t 661 * 662 ******************************************************************************/ 663 static bt_status_t send_dtmf(const RawAddress* bd_addr, char code) { 664 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 665 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 666 667 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 668 669 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_VTS, code, 0, NULL); 670 return BT_STATUS_SUCCESS; 671 } 672 673 /******************************************************************************* 674 * 675 * Function request_last_voice_tag_number 676 * 677 * Description Request number from AG for VR purposes 678 * 679 * Returns bt_status_t 680 * 681 ******************************************************************************/ 682 static bt_status_t request_last_voice_tag_number(const RawAddress* bd_addr) { 683 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 684 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 685 686 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 687 688 if (cb->peer_feat & BTA_HF_CLIENT_PEER_VTAG) { 689 BTA_HfClientSendAT(cb->handle, BTA_HF_CLIENT_AT_CMD_BINP, 1, 0, NULL); 690 return BT_STATUS_SUCCESS; 691 } 692 return BT_STATUS_UNSUPPORTED; 693 } 694 695 /******************************************************************************* 696 * 697 * Function cleanup 698 * 699 * Description Closes the HF interface 700 * 701 * Returns bt_status_t 702 * 703 ******************************************************************************/ 704 static void cleanup(void) { 705 BTIF_TRACE_EVENT("%s", __func__); 706 707 btif_queue_cleanup(UUID_SERVCLASS_HF_HANDSFREE); 708 if (bt_hf_client_callbacks) { 709 btif_disable_service(BTA_HFP_HS_SERVICE_ID); 710 bt_hf_client_callbacks = NULL; 711 } 712 } 713 714 /******************************************************************************* 715 * 716 * Function send_at_cmd 717 * 718 * Description Send requested AT command to rempte device. 719 * 720 * Returns bt_status_t 721 * 722 ******************************************************************************/ 723 static bt_status_t send_at_cmd(const RawAddress* bd_addr, int cmd, int val1, 724 int val2, const char* arg) { 725 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(*bd_addr); 726 if (cb == NULL || !is_connected(cb)) return BT_STATUS_FAIL; 727 728 CHECK_BTHF_CLIENT_SLC_CONNECTED(cb); 729 730 BTIF_TRACE_EVENT("%s: Cmd %d val1 %d val2 %d arg %s", __func__, cmd, val1, 731 val2, (arg != NULL) ? arg : "<null>"); 732 BTA_HfClientSendAT(cb->handle, cmd, val1, val2, arg); 733 734 return BT_STATUS_SUCCESS; 735 } 736 737 static const bthf_client_interface_t bthfClientInterface = { 738 sizeof(bthf_client_interface_t), 739 .init = init, 740 .connect = connect, 741 .disconnect = disconnect, 742 .connect_audio = connect_audio, 743 .disconnect_audio = disconnect_audio, 744 .start_voice_recognition = start_voice_recognition, 745 .stop_voice_recognition = stop_voice_recognition, 746 .volume_control = volume_control, 747 .dial = dial, 748 .dial_memory = dial_memory, 749 .handle_call_action = handle_call_action, 750 .query_current_calls = query_current_calls, 751 .query_current_operator_name = query_current_operator_name, 752 .retrieve_subscriber_info = retrieve_subscriber_info, 753 .send_dtmf = send_dtmf, 754 .request_last_voice_tag_number = request_last_voice_tag_number, 755 .cleanup = cleanup, 756 .send_at_cmd = send_at_cmd, 757 }; 758 759 static void process_ind_evt(tBTA_HF_CLIENT_IND* ind) { 760 BTIF_TRACE_DEBUG("%s", __func__); 761 762 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(ind->bd_addr); 763 if (cb == NULL || !is_connected(cb)) return; 764 765 switch (ind->type) { 766 case BTA_HF_CLIENT_IND_CALL: 767 HAL_CBACK(bt_hf_client_callbacks, call_cb, &cb->peer_bda, 768 (bthf_client_call_t)ind->value); 769 break; 770 771 case BTA_HF_CLIENT_IND_CALLSETUP: 772 HAL_CBACK(bt_hf_client_callbacks, callsetup_cb, &cb->peer_bda, 773 (bthf_client_callsetup_t)ind->value); 774 break; 775 case BTA_HF_CLIENT_IND_CALLHELD: 776 HAL_CBACK(bt_hf_client_callbacks, callheld_cb, &cb->peer_bda, 777 (bthf_client_callheld_t)ind->value); 778 break; 779 780 case BTA_HF_CLIENT_IND_SERVICE: 781 HAL_CBACK(bt_hf_client_callbacks, network_state_cb, &cb->peer_bda, 782 (bthf_client_network_state_t)ind->value); 783 break; 784 785 case BTA_HF_CLIENT_IND_SIGNAL: 786 HAL_CBACK(bt_hf_client_callbacks, network_signal_cb, &cb->peer_bda, 787 ind->value); 788 break; 789 790 case BTA_HF_CLIENT_IND_ROAM: 791 HAL_CBACK(bt_hf_client_callbacks, network_roaming_cb, &cb->peer_bda, 792 (bthf_client_service_type_t)ind->value); 793 break; 794 795 case BTA_HF_CLIENT_IND_BATTCH: 796 HAL_CBACK(bt_hf_client_callbacks, battery_level_cb, &cb->peer_bda, 797 ind->value); 798 break; 799 800 default: 801 break; 802 } 803 } 804 805 /******************************************************************************* 806 * 807 * Function btif_hf_client_upstreams_evt 808 * 809 * Description Executes HF CLIENT UPSTREAMS events in btif context 810 * 811 * Returns void 812 * 813 ******************************************************************************/ 814 static void btif_hf_client_upstreams_evt(uint16_t event, char* p_param) { 815 tBTA_HF_CLIENT* p_data = (tBTA_HF_CLIENT*)p_param; 816 817 btif_hf_client_cb_t* cb = btif_hf_client_get_cb_by_bda(p_data->bd_addr); 818 if (cb == NULL && event == BTA_HF_CLIENT_OPEN_EVT) { 819 BTIF_TRACE_DEBUG("%s: event BTA_HF_CLIENT_OPEN_EVT allocating block", 820 __func__); 821 cb = btif_hf_client_allocate_cb(); 822 cb->handle = p_data->open.handle; 823 cb->peer_bda = p_data->open.bd_addr; 824 } else if (cb == NULL) { 825 BTIF_TRACE_ERROR("%s: event %d but not allocating block: cb not found", 826 __func__, event); 827 return; 828 } 829 830 BTIF_TRACE_DEBUG("%s: event=%s (%u)", __func__, dump_hf_client_event(event), 831 event); 832 833 switch (event) { 834 case BTA_HF_CLIENT_OPEN_EVT: 835 if (p_data->open.status == BTA_HF_CLIENT_SUCCESS) { 836 cb->state = BTHF_CLIENT_CONNECTION_STATE_CONNECTED; 837 cb->peer_feat = 0; 838 cb->chld_feat = 0; 839 } else if (cb->state == BTHF_CLIENT_CONNECTION_STATE_CONNECTING) { 840 cb->state = BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED; 841 } else { 842 BTIF_TRACE_WARNING( 843 "%s: HF CLient open failed, but another device connected. " 844 "status=%d state=%d connected device=%s", 845 __func__, p_data->open.status, cb->state, 846 cb->peer_bda.ToString().c_str()); 847 break; 848 } 849 850 HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, &cb->peer_bda, 851 cb->state, 0, /* peer feat */ 852 0 /* AT+CHLD feat */); 853 854 if (cb->state == BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED) 855 cb->peer_bda = RawAddress::kAny; 856 857 if (p_data->open.status != BTA_HF_CLIENT_SUCCESS) btif_queue_advance(); 858 break; 859 860 case BTA_HF_CLIENT_CONN_EVT: 861 cb->peer_feat = p_data->conn.peer_feat; 862 cb->chld_feat = p_data->conn.chld_feat; 863 cb->state = BTHF_CLIENT_CONNECTION_STATE_SLC_CONNECTED; 864 865 HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, &cb->peer_bda, 866 cb->state, cb->peer_feat, cb->chld_feat); 867 868 /* Inform the application about in-band ringtone */ 869 if (cb->peer_feat & BTA_HF_CLIENT_PEER_INBAND) { 870 HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, &cb->peer_bda, 871 BTHF_CLIENT_IN_BAND_RINGTONE_PROVIDED); 872 } 873 874 btif_queue_advance(); 875 break; 876 877 case BTA_HF_CLIENT_CLOSE_EVT: 878 cb->state = BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED; 879 HAL_CBACK(bt_hf_client_callbacks, connection_state_cb, &cb->peer_bda, 880 cb->state, 0, 0); 881 cb->peer_bda = RawAddress::kAny; 882 cb->peer_feat = 0; 883 cb->chld_feat = 0; 884 btif_queue_advance(); 885 break; 886 887 case BTA_HF_CLIENT_IND_EVT: 888 process_ind_evt(&p_data->ind); 889 break; 890 891 case BTA_HF_CLIENT_MIC_EVT: 892 HAL_CBACK(bt_hf_client_callbacks, volume_change_cb, &cb->peer_bda, 893 BTHF_CLIENT_VOLUME_TYPE_MIC, p_data->val.value); 894 break; 895 896 case BTA_HF_CLIENT_SPK_EVT: 897 HAL_CBACK(bt_hf_client_callbacks, volume_change_cb, &cb->peer_bda, 898 BTHF_CLIENT_VOLUME_TYPE_SPK, p_data->val.value); 899 break; 900 901 case BTA_HF_CLIENT_VOICE_REC_EVT: 902 HAL_CBACK(bt_hf_client_callbacks, vr_cmd_cb, &cb->peer_bda, 903 (bthf_client_vr_state_t)p_data->val.value); 904 break; 905 906 case BTA_HF_CLIENT_OPERATOR_NAME_EVT: 907 HAL_CBACK(bt_hf_client_callbacks, current_operator_cb, &cb->peer_bda, 908 p_data->operator_name.name); 909 break; 910 911 case BTA_HF_CLIENT_CLIP_EVT: 912 HAL_CBACK(bt_hf_client_callbacks, clip_cb, &cb->peer_bda, 913 p_data->number.number); 914 break; 915 916 case BTA_HF_CLIENT_BINP_EVT: 917 HAL_CBACK(bt_hf_client_callbacks, last_voice_tag_number_callback, 918 &cb->peer_bda, p_data->number.number); 919 break; 920 921 case BTA_HF_CLIENT_CCWA_EVT: 922 HAL_CBACK(bt_hf_client_callbacks, call_waiting_cb, &cb->peer_bda, 923 p_data->number.number); 924 break; 925 926 case BTA_HF_CLIENT_AT_RESULT_EVT: 927 HAL_CBACK(bt_hf_client_callbacks, cmd_complete_cb, &cb->peer_bda, 928 (bthf_client_cmd_complete_t)p_data->result.type, 929 p_data->result.cme); 930 break; 931 932 case BTA_HF_CLIENT_CLCC_EVT: 933 HAL_CBACK(bt_hf_client_callbacks, current_calls_cb, &cb->peer_bda, 934 p_data->clcc.idx, 935 p_data->clcc.inc ? BTHF_CLIENT_CALL_DIRECTION_INCOMING 936 : BTHF_CLIENT_CALL_DIRECTION_OUTGOING, 937 (bthf_client_call_state_t)p_data->clcc.status, 938 p_data->clcc.mpty ? BTHF_CLIENT_CALL_MPTY_TYPE_MULTI 939 : BTHF_CLIENT_CALL_MPTY_TYPE_SINGLE, 940 p_data->clcc.number_present ? p_data->clcc.number : NULL); 941 break; 942 943 case BTA_HF_CLIENT_CNUM_EVT: 944 if (p_data->cnum.service == 4) { 945 HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, &cb->peer_bda, 946 p_data->cnum.number, BTHF_CLIENT_SERVICE_VOICE); 947 } else if (p_data->cnum.service == 5) { 948 HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, &cb->peer_bda, 949 p_data->cnum.number, BTHF_CLIENT_SERVICE_FAX); 950 } else { 951 HAL_CBACK(bt_hf_client_callbacks, subscriber_info_cb, &cb->peer_bda, 952 p_data->cnum.number, BTHF_CLIENT_SERVICE_UNKNOWN); 953 } 954 break; 955 956 case BTA_HF_CLIENT_BTRH_EVT: 957 if (p_data->val.value <= BTRH_CLIENT_RESP_AND_HOLD_REJECT) { 958 HAL_CBACK(bt_hf_client_callbacks, resp_and_hold_cb, &cb->peer_bda, 959 (bthf_client_resp_and_hold_t)p_data->val.value); 960 } 961 break; 962 963 case BTA_HF_CLIENT_BSIR_EVT: 964 if (p_data->val.value != 0) { 965 HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, &cb->peer_bda, 966 BTHF_CLIENT_IN_BAND_RINGTONE_PROVIDED); 967 } else { 968 HAL_CBACK(bt_hf_client_callbacks, in_band_ring_tone_cb, &cb->peer_bda, 969 BTHF_CLIENT_IN_BAND_RINGTONE_NOT_PROVIDED); 970 } 971 break; 972 973 case BTA_HF_CLIENT_AUDIO_OPEN_EVT: 974 HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, &cb->peer_bda, 975 BTHF_CLIENT_AUDIO_STATE_CONNECTED); 976 break; 977 978 case BTA_HF_CLIENT_AUDIO_MSBC_OPEN_EVT: 979 HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, &cb->peer_bda, 980 BTHF_CLIENT_AUDIO_STATE_CONNECTED_MSBC); 981 break; 982 983 case BTA_HF_CLIENT_AUDIO_CLOSE_EVT: 984 HAL_CBACK(bt_hf_client_callbacks, audio_state_cb, &cb->peer_bda, 985 BTHF_CLIENT_AUDIO_STATE_DISCONNECTED); 986 break; 987 case BTA_HF_CLIENT_RING_INDICATION: 988 HAL_CBACK(bt_hf_client_callbacks, ring_indication_cb, &cb->peer_bda); 989 break; 990 default: 991 BTIF_TRACE_WARNING("%s: Unhandled event: %d", __func__, event); 992 break; 993 } 994 } 995 996 /******************************************************************************* 997 * 998 * Function bta_hf_client_evt 999 * 1000 * Description Switches context from BTA to BTIF for all HF Client events 1001 * 1002 * Returns void 1003 * 1004 ******************************************************************************/ 1005 1006 static void bta_hf_client_evt(tBTA_HF_CLIENT_EVT event, 1007 tBTA_HF_CLIENT* p_data) { 1008 bt_status_t status; 1009 1010 /* switch context to btif task context (copy full union size for convenience) 1011 */ 1012 status = btif_transfer_context(btif_hf_client_upstreams_evt, (uint16_t)event, 1013 (char*)p_data, sizeof(*p_data), NULL); 1014 1015 /* catch any failed context transfers */ 1016 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status); 1017 } 1018 1019 /******************************************************************************* 1020 * 1021 * Function btif_hf_client_execute_service 1022 * 1023 * Description Initializes/Shuts down the service 1024 * 1025 * Returns BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise 1026 * 1027 ******************************************************************************/ 1028 bt_status_t btif_hf_client_execute_service(bool b_enable) { 1029 BTIF_TRACE_EVENT("%s: enable: %d", __func__, b_enable); 1030 1031 if (b_enable) { 1032 /* Enable and register with BTA-HFClient */ 1033 BTIF_TRACE_EVENT("%s: support codec negotiation %d ", __func__, 1034 BTIF_HF_CLIENT_FEATURES); 1035 BTA_HfClientEnable(bta_hf_client_evt, BTIF_HF_CLIENT_SECURITY, 1036 BTIF_HF_CLIENT_FEATURES, BTIF_HF_CLIENT_SERVICE_NAME); 1037 } else { 1038 BTA_HfClientDisable(); 1039 } 1040 return BT_STATUS_SUCCESS; 1041 } 1042 1043 /******************************************************************************* 1044 * 1045 * Function btif_hf_get_interface 1046 * 1047 * Description Get the hf callback interface 1048 * 1049 * Returns bthf_interface_t 1050 * 1051 ******************************************************************************/ 1052 const bthf_client_interface_t* btif_hf_client_get_interface(void) { 1053 BTIF_TRACE_EVENT("%s", __func__); 1054 return &bthfClientInterface; 1055 } 1056