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 action functions for the handsfree client. 23 * 24 ******************************************************************************/ 25 26#include "bta_api.h" 27#include "bta_hf_client_api.h" 28#include "bta_hf_client_int.h" 29#include "bta_dm_int.h" 30#include "l2c_api.h" 31#include "port_api.h" 32#include "bta_sys.h" 33#include "utl.h" 34#include "bt_utils.h" 35#include "osi/include/compat.h" 36#include <string.h> 37 38/***************************************************************************** 39** Constants 40*****************************************************************************/ 41 42/* maximum length of data to read from RFCOMM */ 43#define BTA_HF_CLIENT_RFC_READ_MAX 512 44 45/******************************************************************************* 46** 47** Function bta_hf_client_register 48** 49** Description This function initializes values of the scb and sets up 50** the SDP record for the services. 51** 52** 53** Returns void 54** 55*******************************************************************************/ 56void bta_hf_client_register(tBTA_HF_CLIENT_DATA *p_data) 57{ 58 tBTA_HF_CLIENT evt; 59 tBTA_UTL_COD cod; 60 61 memset(&evt, 0, sizeof(evt)); 62 63 /* initialize control block */ 64 bta_hf_client_scb_init(); 65 66 bta_hf_client_cb.scb.serv_sec_mask = p_data->api_register.sec_mask; 67 bta_hf_client_cb.scb.features = p_data->api_register.features; 68 69 /* initialize AT control block */ 70 bta_hf_client_at_init(); 71 72 /* create SDP records */ 73 bta_hf_client_create_record(p_data); 74 75 /* Set the Audio service class bit */ 76 cod.service = BTM_COD_SERVICE_AUDIO; 77 utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS); 78 79 /* start RFCOMM server */ 80 bta_hf_client_start_server(); 81 82 /* call app callback with register event */ 83 evt.reg.status = BTA_HF_CLIENT_SUCCESS; 84 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_REGISTER_EVT, &evt); 85} 86 87/******************************************************************************* 88** 89** Function bta_hf_client_deregister 90** 91** Description This function removes the sdp records, closes the RFCOMM 92** servers, and deallocates the service control block. 93** 94** 95** Returns void 96** 97*******************************************************************************/ 98void bta_hf_client_deregister(tBTA_HF_CLIENT_DATA *p_data) 99{ 100 bta_hf_client_cb.scb.deregister = TRUE; 101 102 /* remove sdp record */ 103 bta_hf_client_del_record(p_data); 104 105 /* remove rfcomm server */ 106 bta_hf_client_close_server(); 107 108 /* disable */ 109 bta_hf_client_scb_disable(); 110} 111 112/******************************************************************************* 113** 114** Function bta_hf_client_start_dereg 115** 116** Description Start a deregister event. 117** 118** 119** Returns void 120** 121*******************************************************************************/ 122void bta_hf_client_start_dereg(tBTA_HF_CLIENT_DATA *p_data) 123{ 124 bta_hf_client_cb.scb.deregister = TRUE; 125 126 /* remove sdp record */ 127 bta_hf_client_del_record(p_data); 128} 129 130/******************************************************************************* 131** 132** Function bta_hf_client_start_close 133** 134** Description Start the process of closing SCO and RFCOMM connection. 135** 136** 137** Returns void 138** 139*******************************************************************************/ 140void bta_hf_client_start_close(tBTA_HF_CLIENT_DATA *p_data) 141{ 142 /* Take the link out of sniff and set L2C idle time to 0 */ 143 bta_dm_pm_active(bta_hf_client_cb.scb.peer_addr); 144 L2CA_SetIdleTimeoutByBdAddr(bta_hf_client_cb.scb.peer_addr, 0, BT_TRANSPORT_BR_EDR); 145 146 /* if SCO is open close SCO and wait on RFCOMM close */ 147 if (bta_hf_client_cb.scb.sco_state == BTA_HF_CLIENT_SCO_OPEN_ST) 148 { 149 bta_hf_client_cb.scb.sco_close_rfc = TRUE; 150 } 151 else 152 { 153 bta_hf_client_rfc_do_close(p_data); 154 } 155 156 /* always do SCO shutdown to handle all SCO corner cases */ 157 bta_hf_client_sco_shutdown(NULL); 158} 159 160/******************************************************************************* 161** 162** Function bta_hf_client_start_open 163** 164** Description This starts an HF Client open. 165** 166** 167** Returns void 168** 169*******************************************************************************/ 170void bta_hf_client_start_open(tBTA_HF_CLIENT_DATA *p_data) 171{ 172 BD_ADDR pending_bd_addr; 173 174 /* store parameters */ 175 if (p_data) 176 { 177 bdcpy(bta_hf_client_cb.scb.peer_addr, p_data->api_open.bd_addr); 178 bta_hf_client_cb.scb.cli_sec_mask = p_data->api_open.sec_mask; 179 } 180 181 /* Check if RFCOMM has any incoming connection to avoid collision. */ 182 if (PORT_IsOpening (pending_bd_addr)) 183 { 184 /* Let the incoming connection goes through. */ 185 /* Issue collision for now. */ 186 /* We will decide what to do when we find incoming connection later.*/ 187 bta_hf_client_collision_cback (0, BTA_ID_HS, 0, bta_hf_client_cb.scb.peer_addr); 188 return; 189 } 190 191 /* close server */ 192 bta_hf_client_close_server(); 193 194 /* set role */ 195 bta_hf_client_cb.scb.role = BTA_HF_CLIENT_INT; 196 197 /* do service search */ 198 bta_hf_client_do_disc(); 199} 200 201/******************************************************************************* 202** 203** Function bta_hf_client_cback_open 204** 205** Description Send open callback event to application. 206** 207** 208** Returns void 209** 210*******************************************************************************/ 211static void bta_hf_client_cback_open(tBTA_HF_CLIENT_DATA *p_data, tBTA_HF_CLIENT_STATUS status) 212{ 213 tBTA_HF_CLIENT evt; 214 215 memset(&evt, 0, sizeof(evt)); 216 217 /* call app callback with open event */ 218 evt.open.status = status; 219 if(p_data) 220 { 221 /* if p_data is provided then we need to pick the bd address from the open api structure */ 222 bdcpy(evt.open.bd_addr, p_data->api_open.bd_addr); 223 } 224 else 225 { 226 bdcpy(evt.open.bd_addr, bta_hf_client_cb.scb.peer_addr); 227 } 228 229 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_OPEN_EVT, &evt); 230} 231 232/******************************************************************************* 233** 234** Function bta_hf_client_rfc_open 235** 236** Description Handle RFCOMM channel open. 237** 238** 239** Returns void 240** 241*******************************************************************************/ 242void bta_hf_client_rfc_open(tBTA_HF_CLIENT_DATA *p_data) 243{ 244 UNUSED(p_data); 245 246 bta_sys_conn_open(BTA_ID_HS, 1, bta_hf_client_cb.scb.peer_addr); 247 248 bta_hf_client_cback_open(NULL, BTA_HF_CLIENT_SUCCESS); 249 250 /* start SLC procedure */ 251 bta_hf_client_slc_seq(FALSE); 252} 253 254/******************************************************************************* 255** 256** Function bta_hf_client_rfc_acp_open 257** 258** Description Handle RFCOMM channel open when accepting connection. 259** 260** 261** Returns void 262** 263*******************************************************************************/ 264void bta_hf_client_rfc_acp_open(tBTA_HF_CLIENT_DATA *p_data) 265{ 266 UINT16 lcid; 267 BD_ADDR dev_addr; 268 int status; 269 270 /* set role */ 271 bta_hf_client_cb.scb.role = BTA_HF_CLIENT_ACP; 272 273 APPL_TRACE_DEBUG ("bta_hf_client_rfc_acp_open: serv_handle = %d rfc.port_handle = %d", 274 bta_hf_client_cb.scb.serv_handle, p_data->rfc.port_handle); 275 276 /* get bd addr of peer */ 277 if (PORT_SUCCESS != (status=PORT_CheckConnection(p_data->rfc.port_handle, dev_addr, &lcid))) 278 { 279 APPL_TRACE_DEBUG ("bta_hf_client_rfc_acp_open error PORT_CheckConnection returned status %d", status); 280 } 281 282 /* Collision Handling */ 283 if (alarm_is_scheduled(bta_hf_client_cb.scb.collision_timer)) { 284 alarm_cancel(bta_hf_client_cb.scb.collision_timer); 285 286 if (bdcmp (dev_addr, bta_hf_client_cb.scb.peer_addr) == 0) 287 { 288 /* If incoming and outgoing device are same, nothing more to do. */ 289 /* Outgoing conn will be aborted because we have successful incoming conn. */ 290 } 291 else 292 { 293 /* Resume outgoing connection. */ 294 bta_hf_client_resume_open (); 295 } 296 } 297 298 bdcpy (bta_hf_client_cb.scb.peer_addr, dev_addr); 299 bta_hf_client_cb.scb.conn_handle = p_data->rfc.port_handle; 300 301 /* do service discovery to get features */ 302 bta_hf_client_do_disc(); 303 304 /* continue with open processing */ 305 bta_hf_client_rfc_open(p_data); 306} 307 308/******************************************************************************* 309** 310** Function bta_hf_client_rfc_fail 311** 312** Description RFCOMM connection failed. 313** 314** 315** Returns void 316** 317*******************************************************************************/ 318void bta_hf_client_rfc_fail(tBTA_HF_CLIENT_DATA *p_data) 319{ 320 UNUSED(p_data); 321 322 /* reinitialize stuff */ 323 bta_hf_client_cb.scb.conn_handle = 0; 324 bta_hf_client_cb.scb.peer_features = 0; 325 bta_hf_client_cb.scb.chld_features = 0; 326 bta_hf_client_cb.scb.role = BTA_HF_CLIENT_ACP; 327 bta_hf_client_cb.scb.svc_conn = FALSE; 328 bta_hf_client_cb.scb.send_at_reply = FALSE; 329 bta_hf_client_cb.scb.negotiated_codec = BTM_SCO_CODEC_CVSD; 330 331 bta_hf_client_at_reset(); 332 333 /* reopen server */ 334 bta_hf_client_start_server(); 335 336 /* call open cback w. failure */ 337 bta_hf_client_cback_open(NULL, BTA_HF_CLIENT_FAIL_RFCOMM); 338} 339 340/******************************************************************************* 341** 342** Function bta_hf_client_disc_fail 343** 344** Description This function handles a discovery failure. 345** 346** 347** Returns void 348** 349*******************************************************************************/ 350void bta_hf_client_disc_fail(tBTA_HF_CLIENT_DATA *p_data) 351{ 352 UNUSED(p_data); 353 354 /* reopen server */ 355 bta_hf_client_start_server(); 356 357 /* reinitialize stuff */ 358 359 /* call open cback w. failure */ 360 bta_hf_client_cback_open(NULL, BTA_HF_CLIENT_FAIL_SDP); 361} 362 363/******************************************************************************* 364** 365** Function bta_hf_client_open_fail 366** 367** Description open connection failed. 368** 369** 370** Returns void 371** 372*******************************************************************************/ 373void bta_hf_client_open_fail(tBTA_HF_CLIENT_DATA *p_data) 374{ 375 /* call open cback w. failure */ 376 bta_hf_client_cback_open(p_data, BTA_HF_CLIENT_FAIL_RESOURCES); 377} 378 379/******************************************************************************* 380** 381** Function bta_hf_client_rfc_close 382** 383** Description RFCOMM connection closed. 384** 385** 386** Returns void 387** 388*******************************************************************************/ 389void bta_hf_client_rfc_close(tBTA_HF_CLIENT_DATA *p_data) 390{ 391 UNUSED(p_data); 392 393 /* reinitialize stuff */ 394 bta_hf_client_cb.scb.peer_features = 0; 395 bta_hf_client_cb.scb.chld_features = 0; 396 bta_hf_client_cb.scb.role = BTA_HF_CLIENT_ACP; 397 bta_hf_client_cb.scb.svc_conn = FALSE; 398 bta_hf_client_cb.scb.send_at_reply = FALSE; 399 bta_hf_client_cb.scb.negotiated_codec = BTM_SCO_CODEC_CVSD; 400 401 bta_hf_client_at_reset(); 402 403 bta_sys_conn_close(BTA_ID_HS, 1, bta_hf_client_cb.scb.peer_addr); 404 405 /* call close cback */ 406 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_CLOSE_EVT, NULL); 407 408 /* if not deregistering reopen server */ 409 if (bta_hf_client_cb.scb.deregister == FALSE) 410 { 411 /* Clear peer bd_addr so instance can be reused */ 412 bdcpy(bta_hf_client_cb.scb.peer_addr, bd_addr_null); 413 414 /* start server as it might got closed on open*/ 415 bta_hf_client_start_server(); 416 417 bta_hf_client_cb.scb.conn_handle = 0; 418 419 /* Make sure SCO is shutdown */ 420 bta_hf_client_sco_shutdown(NULL); 421 422 bta_sys_sco_unuse(BTA_ID_HS, 1, bta_hf_client_cb.scb.peer_addr); 423 } 424 /* else close port and deallocate scb */ 425 else 426 { 427 bta_hf_client_close_server(); 428 bta_hf_client_scb_disable(); 429 } 430} 431 432/******************************************************************************* 433** 434** Function bta_hf_client_disc_int_res 435** 436** Description This function handles a discovery result when initiator. 437** 438** 439** Returns void 440** 441*******************************************************************************/ 442void bta_hf_client_disc_int_res(tBTA_HF_CLIENT_DATA *p_data) 443{ 444 UINT16 event = BTA_HF_CLIENT_DISC_FAIL_EVT; 445 446 APPL_TRACE_DEBUG ("bta_hf_client_disc_int_res: Status: %d", p_data->disc_result.status); 447 448 /* if found service */ 449 if (p_data->disc_result.status == SDP_SUCCESS || 450 p_data->disc_result.status == SDP_DB_FULL) 451 { 452 /* get attributes */ 453 if (bta_hf_client_sdp_find_attr()) 454 { 455 event = BTA_HF_CLIENT_DISC_OK_EVT; 456 } 457 } 458 459 /* free discovery db */ 460 bta_hf_client_free_db(p_data); 461 462 /* send ourselves sdp ok/fail event */ 463 bta_hf_client_sm_execute(event, p_data); 464} 465 466/******************************************************************************* 467** 468** Function bta_hf_client_disc_acp_res 469** 470** Description This function handles a discovery result when acceptor. 471** 472** 473** Returns void 474** 475*******************************************************************************/ 476void bta_hf_client_disc_acp_res(tBTA_HF_CLIENT_DATA *p_data) 477{ 478 /* if found service */ 479 if (p_data->disc_result.status == SDP_SUCCESS || 480 p_data->disc_result.status == SDP_DB_FULL) 481 { 482 /* get attributes */ 483 bta_hf_client_sdp_find_attr(); 484 } 485 486 /* free discovery db */ 487 bta_hf_client_free_db(p_data); 488} 489 490/******************************************************************************* 491** 492** Function bta_hf_client_rfc_data 493** 494** Description Read and process data from RFCOMM. 495** 496** 497** Returns void 498** 499*******************************************************************************/ 500void bta_hf_client_rfc_data(tBTA_HF_CLIENT_DATA *p_data) 501{ 502 UINT16 len; 503 char buf[BTA_HF_CLIENT_RFC_READ_MAX]; 504 UNUSED(p_data); 505 506 memset(buf, 0, sizeof(buf)); 507 508 /* read data from rfcomm; if bad status, we're done */ 509 while (PORT_ReadData(bta_hf_client_cb.scb.conn_handle, buf, BTA_HF_CLIENT_RFC_READ_MAX, &len) == PORT_SUCCESS) 510 { 511 /* if no data, we're done */ 512 if (len == 0) 513 { 514 break; 515 } 516 517 bta_hf_client_at_parse(buf, len); 518 519 /* no more data to read, we're done */ 520 if (len < BTA_HF_CLIENT_RFC_READ_MAX) 521 { 522 break; 523 } 524 } 525} 526 527/******************************************************************************* 528** 529** Function bta_hf_client_svc_conn_open 530** 531** Description Service level connection opened 532** 533** 534** Returns void 535** 536*******************************************************************************/ 537void bta_hf_client_svc_conn_open(tBTA_HF_CLIENT_DATA *p_data) 538{ 539 tBTA_HF_CLIENT evt; 540 UNUSED(p_data); 541 542 memset(&evt, 0, sizeof(evt)); 543 544 if (!bta_hf_client_cb.scb.svc_conn) 545 { 546 /* set state variable */ 547 bta_hf_client_cb.scb.svc_conn = TRUE; 548 549 /* call callback */ 550 evt.conn.peer_feat = bta_hf_client_cb.scb.peer_features; 551 evt.conn.chld_feat = bta_hf_client_cb.scb.chld_features; 552 553 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_CONN_EVT, &evt); 554 } 555} 556 557/******************************************************************************* 558** 559** Function bta_hf_client_cback_ind 560** 561** Description Send indicator callback event to application. 562** 563** Returns void 564** 565*******************************************************************************/ 566void bta_hf_client_ind(tBTA_HF_CLIENT_IND_TYPE type, UINT16 value) 567{ 568 tBTA_HF_CLIENT evt; 569 570 memset(&evt, 0, sizeof(evt)); 571 572 evt.ind.type = type; 573 evt.ind.value = value; 574 575 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_IND_EVT, &evt); 576} 577 578/******************************************************************************* 579** 580** Function bta_hf_client_evt_val 581** 582** Description Send event to application. 583** This is a generic helper for events with common data. 584** 585** 586** Returns void 587** 588*******************************************************************************/ 589void bta_hf_client_evt_val(tBTA_HF_CLIENT_EVT type, UINT16 value) 590{ 591 tBTA_HF_CLIENT evt; 592 593 memset(&evt, 0, sizeof(evt)); 594 595 evt.val.value = value; 596 597 (*bta_hf_client_cb.p_cback)(type, &evt); 598} 599 600/******************************************************************************* 601** 602** Function bta_hf_client_operator_name 603** 604** Description Send operator name event to application. 605** 606** 607** Returns void 608** 609*******************************************************************************/ 610void bta_hf_client_operator_name(char *name) 611{ 612 tBTA_HF_CLIENT evt; 613 614 memset(&evt, 0, sizeof(evt)); 615 616 strlcpy(evt.operator.name, name, BTA_HF_CLIENT_OPERATOR_NAME_LEN + 1); 617 evt.operator.name[BTA_HF_CLIENT_OPERATOR_NAME_LEN] = '\0'; 618 619 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_OPERATOR_NAME_EVT, &evt); 620} 621 622 623/******************************************************************************* 624** 625** Function bta_hf_client_clip 626** 627** Description Send CLIP event to application. 628** 629** 630** Returns void 631** 632*******************************************************************************/ 633void bta_hf_client_clip(char *number) 634{ 635 tBTA_HF_CLIENT evt; 636 637 memset(&evt, 0, sizeof(evt)); 638 639 strlcpy(evt.number.number, number, BTA_HF_CLIENT_NUMBER_LEN + 1); 640 evt.number.number[BTA_HF_CLIENT_NUMBER_LEN] = '\0'; 641 642 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_CLIP_EVT, &evt); 643} 644 645/******************************************************************************* 646** 647** Function bta_hf_client_ccwa 648** 649** Description Send CLIP event to application. 650** 651** 652** Returns void 653** 654*******************************************************************************/ 655void bta_hf_client_ccwa(char *number) 656{ 657 tBTA_HF_CLIENT evt; 658 659 memset(&evt, 0, sizeof(evt)); 660 661 strlcpy(evt.number.number, number, BTA_HF_CLIENT_NUMBER_LEN + 1); 662 evt.number.number[BTA_HF_CLIENT_NUMBER_LEN] = '\0'; 663 664 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_CCWA_EVT, &evt); 665} 666 667/******************************************************************************* 668** 669** Function bta_hf_client_at_result 670** 671** Description Send AT result event to application. 672** 673** 674** Returns void 675** 676*******************************************************************************/ 677void bta_hf_client_at_result(tBTA_HF_CLIENT_AT_RESULT_TYPE type, UINT16 cme) 678{ 679 tBTA_HF_CLIENT evt; 680 681 memset(&evt, 0, sizeof(evt)); 682 683 evt.result.type = type; 684 evt.result.cme = cme; 685 686 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_AT_RESULT_EVT, &evt); 687} 688 689/******************************************************************************* 690** 691** Function bta_hf_client_clcc 692** 693** Description Send clcc event to application. 694** 695** 696** Returns void 697** 698*******************************************************************************/ 699void bta_hf_client_clcc(UINT32 idx, BOOLEAN incoming, UINT8 status, BOOLEAN mpty, char *number) 700{ 701 tBTA_HF_CLIENT evt; 702 703 memset(&evt, 0, sizeof(evt)); 704 705 evt.clcc.idx = idx; 706 evt.clcc.inc = incoming; 707 evt.clcc.status = status; 708 evt.clcc.mpty = mpty; 709 710 if (number) 711 { 712 evt.clcc.number_present = TRUE; 713 strlcpy(evt.clcc.number, number, BTA_HF_CLIENT_NUMBER_LEN + 1); 714 evt.clcc.number[BTA_HF_CLIENT_NUMBER_LEN] = '\0'; 715 } 716 717 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_CLCC_EVT, &evt); 718} 719 720/******************************************************************************* 721** 722** Function bta_hf_client_cnum 723** 724** Description Send cnum event to application. 725** 726** 727** Returns void 728** 729*******************************************************************************/ 730void bta_hf_client_cnum(char *number, UINT16 service) 731{ 732 tBTA_HF_CLIENT evt; 733 734 memset(&evt, 0, sizeof(evt)); 735 736 evt.cnum.service = service; 737 strlcpy(evt.cnum.number, number, BTA_HF_CLIENT_NUMBER_LEN + 1); 738 evt.cnum.number[BTA_HF_CLIENT_NUMBER_LEN] = '\0'; 739 740 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_CNUM_EVT, &evt); 741} 742 743/******************************************************************************* 744** 745** Function bta_hf_client_binp 746** 747** Description Send BINP event to application. 748** 749** 750** Returns void 751** 752*******************************************************************************/ 753void bta_hf_client_binp(char *number) 754{ 755 tBTA_HF_CLIENT evt; 756 757 memset(&evt, 0, sizeof(evt)); 758 759 strlcpy(evt.number.number, number, BTA_HF_CLIENT_NUMBER_LEN + 1); 760 evt.number.number[BTA_HF_CLIENT_NUMBER_LEN] = '\0'; 761 762 (*bta_hf_client_cb.p_cback)(BTA_HF_CLIENT_BINP_EVT, &evt); 763} 764