Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /*******************************************************************************
     20  *
     21  *  Filename:      btif_hf.c
     22  *
     23  *  Description:   Handsfree Profile Bluetooth Interface
     24  *
     25  *
     26  ******************************************************************************/
     27 
     28 #define LOG_TAG "bt_btif_hf"
     29 
     30 #include <cstdlib>
     31 #include <cstring>
     32 #include <ctime>
     33 
     34 #include <bta/include/bta_ag_api.h>
     35 #include <hardware/bluetooth.h>
     36 #include <hardware/bluetooth_headset_callbacks.h>
     37 #include <hardware/bluetooth_headset_interface.h>
     38 #include <hardware/bt_hf.h>
     39 
     40 #include "bta/include/utl.h"
     41 #include "bta_ag_api.h"
     42 #include "btif_common.h"
     43 #include "btif_hf.h"
     44 #include "btif_profile_queue.h"
     45 #include "btif_util.h"
     46 #include "osi/include/metrics.h"
     47 
     48 namespace bluetooth {
     49 namespace headset {
     50 
     51 /*******************************************************************************
     52  *  Constants & Macros
     53  ******************************************************************************/
     54 #ifndef BTIF_HSAG_SERVICE_NAME
     55 #define BTIF_HSAG_SERVICE_NAME ("Headset Gateway")
     56 #endif
     57 
     58 #ifndef BTIF_HFAG_SERVICE_NAME
     59 #define BTIF_HFAG_SERVICE_NAME ("Handsfree Gateway")
     60 #endif
     61 
     62 #ifndef BTIF_HF_SERVICES
     63 #define BTIF_HF_SERVICES (BTA_HSP_SERVICE_MASK | BTA_HFP_SERVICE_MASK)
     64 #endif
     65 
     66 #ifndef BTIF_HF_SERVICE_NAMES
     67 #define BTIF_HF_SERVICE_NAMES \
     68   { BTIF_HSAG_SERVICE_NAME, BTIF_HFAG_SERVICE_NAME }
     69 #endif
     70 
     71 #ifndef BTIF_HF_SECURITY
     72 #define BTIF_HF_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)
     73 #endif
     74 
     75 #ifndef BTIF_HF_FEATURES
     76 #define BTIF_HF_FEATURES                                       \
     77   (BTA_AG_FEAT_3WAY | BTA_AG_FEAT_ECNR | BTA_AG_FEAT_REJECT |  \
     78    BTA_AG_FEAT_ECS | BTA_AG_FEAT_EXTERR | BTA_AG_FEAT_VREC |   \
     79    BTA_AG_FEAT_CODEC | BTA_AG_FEAT_HF_IND | BTA_AG_FEAT_ESCO | \
     80    BTA_AG_FEAT_UNAT)
     81 #endif
     82 
     83 /* HF features supported at runtime */
     84 static uint32_t btif_hf_features = BTIF_HF_FEATURES;
     85 
     86 #define BTIF_HF_INVALID_IDX (-1)
     87 
     88 /* Max HF clients supported from App */
     89 static int btif_max_hf_clients = 1;
     90 static RawAddress active_bda = {};
     91 
     92 /*******************************************************************************
     93  *  Static variables
     94  ******************************************************************************/
     95 static Callbacks* bt_hf_callbacks = nullptr;
     96 
     97 #define CHECK_BTHF_INIT()                                             \
     98   do {                                                                \
     99     if (!bt_hf_callbacks) {                                           \
    100       BTIF_TRACE_WARNING("BTHF: %s: BTHF not initialized", __func__); \
    101       return BT_STATUS_NOT_READY;                                     \
    102     } else {                                                          \
    103       BTIF_TRACE_EVENT("BTHF: %s", __func__);                         \
    104     }                                                                 \
    105   } while (false)
    106 
    107 /* BTIF-HF control block to map bdaddr to BTA handle */
    108 struct btif_hf_cb_t {
    109   uint16_t handle;
    110   bool is_initiator;
    111   RawAddress connected_bda;
    112   bthf_connection_state_t state;
    113   tBTA_AG_PEER_FEAT peer_feat;
    114   int num_active;
    115   int num_held;
    116   bthf_call_state_t call_setup_state;
    117 };
    118 
    119 static btif_hf_cb_t btif_hf_cb[BTA_AG_MAX_NUM_CLIENTS];
    120 
    121 /* By default, even though codec negotiation is enabled, we will not use WBS as
    122  * the default
    123  * codec unless this variable is set to true.
    124  */
    125 #ifndef BTIF_HF_WBS_PREFERRED
    126 #define BTIF_HF_WBS_PREFERRED false
    127 #endif
    128 
    129 static bool btif_conf_hf_force_wbs = BTIF_HF_WBS_PREFERRED;
    130 
    131 static const char* dump_hf_call_state(bthf_call_state_t call_state) {
    132   switch (call_state) {
    133     CASE_RETURN_STR(BTHF_CALL_STATE_IDLE)
    134     CASE_RETURN_STR(BTHF_CALL_STATE_HELD)
    135     CASE_RETURN_STR(BTHF_CALL_STATE_DIALING)
    136     CASE_RETURN_STR(BTHF_CALL_STATE_ALERTING)
    137     CASE_RETURN_STR(BTHF_CALL_STATE_INCOMING)
    138     CASE_RETURN_STR(BTHF_CALL_STATE_WAITING)
    139     CASE_RETURN_STR(BTHF_CALL_STATE_ACTIVE)
    140     CASE_RETURN_STR(BTHF_CALL_STATE_DISCONNECTED)
    141     default:
    142       return "UNKNOWN CALL STATE";
    143   }
    144 }
    145 
    146 /**
    147  * Check if bd_addr is the current active device.
    148  *
    149  * @param bd_addr target device address
    150  * @return True if bd_addr is the current active device, False otherwise or if
    151  * no active device is set (i.e. active_device_addr is empty)
    152  */
    153 static bool is_active_device(const RawAddress& bd_addr) {
    154   return !active_bda.IsEmpty() && active_bda == bd_addr;
    155 }
    156 
    157 /*******************************************************************************
    158  *
    159  * Function         is_connected
    160  *
    161  * Description      Internal function to check if HF is connected
    162  *                  is_connected(nullptr) returns TRUE if one of the control
    163  *                  blocks is connected
    164  *
    165  * Returns          true if connected
    166  *
    167  ******************************************************************************/
    168 static bool is_connected(RawAddress* bd_addr) {
    169   for (int i = 0; i < btif_max_hf_clients; ++i) {
    170     if (((btif_hf_cb[i].state == BTHF_CONNECTION_STATE_CONNECTED) ||
    171          (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_SLC_CONNECTED)) &&
    172         (!bd_addr || *bd_addr == btif_hf_cb[i].connected_bda))
    173       return true;
    174   }
    175   return false;
    176 }
    177 
    178 /*******************************************************************************
    179  *
    180  * Function         btif_hf_idx_by_bdaddr
    181  *
    182  * Description      Internal function to get idx by bdaddr
    183  *
    184  * Returns          idx
    185  *
    186  ******************************************************************************/
    187 static int btif_hf_idx_by_bdaddr(RawAddress* bd_addr) {
    188   for (int i = 0; i < btif_max_hf_clients; ++i) {
    189     if (*bd_addr == btif_hf_cb[i].connected_bda) return i;
    190   }
    191   return BTIF_HF_INVALID_IDX;
    192 }
    193 
    194 /*******************************************************************************
    195  *
    196  * Function         callstate_to_callsetup
    197  *
    198  * Description      Converts HAL call state to BTA call setup indicator value
    199  *
    200  * Returns          BTA call indicator value
    201  *
    202  ******************************************************************************/
    203 static uint8_t callstate_to_callsetup(bthf_call_state_t call_state) {
    204   switch (call_state) {
    205     case BTHF_CALL_STATE_INCOMING:
    206       return 1;
    207     case BTHF_CALL_STATE_DIALING:
    208       return 2;
    209     case BTHF_CALL_STATE_ALERTING:
    210       return 3;
    211     default:
    212       return 0;
    213   }
    214 }
    215 
    216 /*******************************************************************************
    217  *
    218  * Function         send_at_result
    219  *
    220  * Description      Send AT result code (OK/ERROR)
    221  *
    222  * Returns          void
    223  *
    224  ******************************************************************************/
    225 static void send_at_result(uint8_t ok_flag, uint16_t errcode, int idx) {
    226   tBTA_AG_RES_DATA ag_res = {};
    227   ag_res.ok_flag = ok_flag;
    228   if (ok_flag == BTA_AG_OK_ERROR) {
    229     ag_res.errcode = errcode;
    230   }
    231   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_UNAT_RES, ag_res);
    232 }
    233 
    234 /*******************************************************************************
    235  *
    236  * Function         send_indicator_update
    237  *
    238  * Description      Send indicator update (CIEV)
    239  *
    240  * Returns          void
    241  *
    242  ******************************************************************************/
    243 static void send_indicator_update(const btif_hf_cb_t& control_block,
    244                                   uint16_t indicator, uint16_t value) {
    245   tBTA_AG_RES_DATA ag_res = {};
    246   ag_res.ind.id = indicator;
    247   ag_res.ind.value = value;
    248   BTA_AgResult(control_block.handle, BTA_AG_IND_RES, ag_res);
    249 }
    250 
    251 static bool is_nth_bit_enabled(uint32_t value, int n) {
    252   return (value & (static_cast<uint32_t>(1) << n)) != 0;
    253 }
    254 
    255 void clear_phone_state_multihf(btif_hf_cb_t* hf_cb) {
    256   hf_cb->call_setup_state = BTHF_CALL_STATE_IDLE;
    257   hf_cb->num_active = 0;
    258   hf_cb->num_held = 0;
    259 }
    260 
    261 static void reset_control_block(btif_hf_cb_t* hf_cb) {
    262   hf_cb->state = BTHF_CONNECTION_STATE_DISCONNECTED;
    263   hf_cb->is_initiator = false;
    264   hf_cb->connected_bda = RawAddress::kEmpty;
    265   hf_cb->peer_feat = 0;
    266   clear_phone_state_multihf(hf_cb);
    267 }
    268 
    269 /**
    270  * Check if Service Level Connection (SLC) is established for bd_addr
    271  *
    272  * @param bd_addr remote device address
    273  * @return true if SLC is established for bd_addr
    274  */
    275 static bool IsSlcConnected(RawAddress* bd_addr) {
    276   if (!bd_addr) {
    277     LOG(WARNING) << __func__ << ": bd_addr is null";
    278     return false;
    279   }
    280   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    281   if (idx < 0 || idx > BTA_AG_MAX_NUM_CLIENTS) {
    282     LOG(WARNING) << __func__ << ": invalid index " << idx << " for "
    283                  << *bd_addr;
    284     return false;
    285   }
    286   return btif_hf_cb[idx].state == BTHF_CONNECTION_STATE_SLC_CONNECTED;
    287 }
    288 
    289 /*******************************************************************************
    290  *
    291  * Function         btif_hf_upstreams_evt
    292  *
    293  * Description      Executes HF UPSTREAMS events in btif context
    294  *
    295  * Returns          void
    296  *
    297  ******************************************************************************/
    298 static void btif_hf_upstreams_evt(uint16_t event, char* p_param) {
    299   if (event == BTA_AG_ENABLE_EVT || event == BTA_AG_DISABLE_EVT) {
    300     LOG(INFO) << __func__ << ": AG enable/disable event " << event;
    301     return;
    302   }
    303   if (p_param == nullptr) {
    304     LOG(ERROR) << __func__ << ": parameter is null";
    305     return;
    306   }
    307   tBTA_AG* p_data = (tBTA_AG*)p_param;
    308   int idx = p_data->hdr.handle - 1;
    309 
    310   BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_hf_event(event));
    311 
    312   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    313     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    314     return;
    315   }
    316   if (!bt_hf_callbacks) {
    317     BTIF_TRACE_ERROR("%s: Headset callback is NULL", __func__);
    318     return;
    319   }
    320 
    321   switch (event) {
    322     case BTA_AG_REGISTER_EVT:
    323       btif_hf_cb[idx].handle = p_data->reg.hdr.handle;
    324       BTIF_TRACE_DEBUG("%s: BTA_AG_REGISTER_EVT, btif_hf_cb.handle = %d",
    325                        __func__, btif_hf_cb[idx].handle);
    326       break;
    327     // RFCOMM connected or failed to connect
    328     case BTA_AG_OPEN_EVT:
    329       // Check if an outoging connection is pending
    330       if (btif_hf_cb[idx].is_initiator) {
    331         CHECK_EQ(btif_hf_cb[idx].state, BTHF_CONNECTION_STATE_CONNECTING)
    332             << "Control block must be in connecting state when initiating";
    333         CHECK(!btif_hf_cb[idx].connected_bda.IsEmpty())
    334             << "Remote device address must not be empty when initiating";
    335         CHECK_EQ(btif_hf_cb[idx].connected_bda, p_data->open.bd_addr)
    336             << "Incoming message's address must match expected one";
    337       }
    338       if (p_data->open.status == BTA_AG_SUCCESS) {
    339         // In case this is an incoming connection
    340         btif_hf_cb[idx].connected_bda = p_data->open.bd_addr;
    341         btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_CONNECTED;
    342         btif_hf_cb[idx].peer_feat = 0;
    343         clear_phone_state_multihf(&btif_hf_cb[idx]);
    344         system_bt_osi::BluetoothMetricsLogger::GetInstance()
    345             ->LogHeadsetProfileRfcConnection(p_data->open.service_id);
    346         bt_hf_callbacks->ConnectionStateCallback(
    347             btif_hf_cb[idx].state, &btif_hf_cb[idx].connected_bda);
    348       } else {
    349         if (!btif_hf_cb[idx].is_initiator) {
    350           // Ignore remote initiated open failures
    351           LOG(WARNING) << __func__ << ": Unexpected AG open failure "
    352                        << std::to_string(p_data->open.status) << " for "
    353                        << p_data->open.bd_addr << " is ignored";
    354           break;
    355         }
    356         LOG(ERROR) << __func__ << ": self initiated AG open failed for "
    357                    << btif_hf_cb[idx].connected_bda << ", status "
    358                    << std::to_string(p_data->open.status);
    359         RawAddress connected_bda = btif_hf_cb[idx].connected_bda;
    360         reset_control_block(&btif_hf_cb[idx]);
    361         bt_hf_callbacks->ConnectionStateCallback(btif_hf_cb[idx].state,
    362                                                  &connected_bda);
    363         btif_queue_advance();
    364       }
    365       break;
    366     // SLC and RFCOMM both disconnected
    367     case BTA_AG_CLOSE_EVT: {
    368       BTIF_TRACE_DEBUG("%s: BTA_AG_CLOSE_EVT, idx = %d, btif_hf_cb.handle = %d",
    369                        __func__, idx, btif_hf_cb[idx].handle);
    370       // If AG_OPEN was received but SLC was not connected in time, then
    371       // AG_CLOSE may be received. We need to advance the queue here.
    372       bool failed_to_setup_slc =
    373           (btif_hf_cb[idx].state != BTHF_CONNECTION_STATE_SLC_CONNECTED) &&
    374           btif_hf_cb[idx].is_initiator;
    375       RawAddress connected_bda = btif_hf_cb[idx].connected_bda;
    376       reset_control_block(&btif_hf_cb[idx]);
    377       bt_hf_callbacks->ConnectionStateCallback(btif_hf_cb[idx].state,
    378                                                &connected_bda);
    379       if (failed_to_setup_slc) {
    380         LOG(ERROR) << __func__ << ": failed to setup SLC for " << connected_bda;
    381         btif_queue_advance();
    382       }
    383       break;
    384     }
    385     // SLC connected
    386     case BTA_AG_CONN_EVT:
    387       BTIF_TRACE_DEBUG("%s: BTA_AG_CONN_EVT, idx = %d ", __func__, idx);
    388       btif_hf_cb[idx].peer_feat = p_data->conn.peer_feat;
    389       btif_hf_cb[idx].state = BTHF_CONNECTION_STATE_SLC_CONNECTED;
    390       bt_hf_callbacks->ConnectionStateCallback(btif_hf_cb[idx].state,
    391                                                &btif_hf_cb[idx].connected_bda);
    392       if (btif_hf_cb[idx].is_initiator) {
    393         btif_queue_advance();
    394       }
    395       break;
    396 
    397     case BTA_AG_AUDIO_OPEN_EVT:
    398       bt_hf_callbacks->AudioStateCallback(BTHF_AUDIO_STATE_CONNECTED,
    399                                           &btif_hf_cb[idx].connected_bda);
    400       break;
    401 
    402     case BTA_AG_AUDIO_CLOSE_EVT:
    403       bt_hf_callbacks->AudioStateCallback(BTHF_AUDIO_STATE_DISCONNECTED,
    404                                           &btif_hf_cb[idx].connected_bda);
    405       break;
    406 
    407     /* BTA auto-responds, silently discard */
    408     case BTA_AG_SPK_EVT:
    409     case BTA_AG_MIC_EVT:
    410       bt_hf_callbacks->VolumeControlCallback(
    411           (event == BTA_AG_SPK_EVT) ? BTHF_VOLUME_TYPE_SPK
    412                                     : BTHF_VOLUME_TYPE_MIC,
    413           p_data->val.num, &btif_hf_cb[idx].connected_bda);
    414       break;
    415 
    416     case BTA_AG_AT_A_EVT:
    417       bt_hf_callbacks->AnswerCallCallback(&btif_hf_cb[idx].connected_bda);
    418       break;
    419 
    420     /* Java needs to send OK/ERROR for these commands */
    421     case BTA_AG_AT_BLDN_EVT:
    422     case BTA_AG_AT_D_EVT:
    423       bt_hf_callbacks->DialCallCallback(
    424           (event == BTA_AG_AT_D_EVT) ? p_data->val.str : nullptr,
    425           &btif_hf_cb[idx].connected_bda);
    426       break;
    427 
    428     case BTA_AG_AT_CHUP_EVT:
    429       bt_hf_callbacks->HangupCallCallback(&btif_hf_cb[idx].connected_bda);
    430       break;
    431 
    432     case BTA_AG_AT_CIND_EVT:
    433       bt_hf_callbacks->AtCindCallback(&btif_hf_cb[idx].connected_bda);
    434       break;
    435 
    436     case BTA_AG_AT_VTS_EVT:
    437       bt_hf_callbacks->DtmfCmdCallback(p_data->val.str[0],
    438                                        &btif_hf_cb[idx].connected_bda);
    439       break;
    440 
    441     case BTA_AG_AT_BVRA_EVT:
    442       bt_hf_callbacks->VoiceRecognitionCallback((p_data->val.num == 1)
    443                                                     ? BTHF_VR_STATE_STARTED
    444                                                     : BTHF_VR_STATE_STOPPED,
    445                                                 &btif_hf_cb[idx].connected_bda);
    446       break;
    447 
    448     case BTA_AG_AT_NREC_EVT:
    449       bt_hf_callbacks->NoiseReductionCallback(
    450           (p_data->val.num == 1) ? BTHF_NREC_START : BTHF_NREC_STOP,
    451           &btif_hf_cb[idx].connected_bda);
    452       break;
    453 
    454     /* TODO: Add a callback for CBC */
    455     case BTA_AG_AT_CBC_EVT:
    456       break;
    457 
    458     case BTA_AG_AT_CKPD_EVT:
    459       bt_hf_callbacks->KeyPressedCallback(&btif_hf_cb[idx].connected_bda);
    460       break;
    461 
    462     case BTA_AG_WBS_EVT:
    463       BTIF_TRACE_DEBUG(
    464           "BTA_AG_WBS_EVT Set codec status %d codec %d 1=CVSD 2=MSBC",
    465           p_data->val.hdr.status, p_data->val.num);
    466       if (p_data->val.num == BTA_AG_CODEC_CVSD) {
    467         bt_hf_callbacks->WbsCallback(BTHF_WBS_NO,
    468                                      &btif_hf_cb[idx].connected_bda);
    469       } else if (p_data->val.num == BTA_AG_CODEC_MSBC) {
    470         bt_hf_callbacks->WbsCallback(BTHF_WBS_YES,
    471                                      &btif_hf_cb[idx].connected_bda);
    472       } else {
    473         bt_hf_callbacks->WbsCallback(BTHF_WBS_NONE,
    474                                      &btif_hf_cb[idx].connected_bda);
    475       }
    476       break;
    477 
    478     /* Java needs to send OK/ERROR for these commands */
    479     case BTA_AG_AT_CHLD_EVT:
    480       bt_hf_callbacks->AtChldCallback((bthf_chld_type_t)atoi(p_data->val.str),
    481                                       &btif_hf_cb[idx].connected_bda);
    482       break;
    483 
    484     case BTA_AG_AT_CLCC_EVT:
    485       bt_hf_callbacks->AtClccCallback(&btif_hf_cb[idx].connected_bda);
    486       break;
    487 
    488     case BTA_AG_AT_COPS_EVT:
    489       bt_hf_callbacks->AtCopsCallback(&btif_hf_cb[idx].connected_bda);
    490       break;
    491 
    492     case BTA_AG_AT_UNAT_EVT:
    493       bt_hf_callbacks->UnknownAtCallback(p_data->val.str,
    494                                          &btif_hf_cb[idx].connected_bda);
    495       break;
    496 
    497     case BTA_AG_AT_CNUM_EVT:
    498       bt_hf_callbacks->AtCnumCallback(&btif_hf_cb[idx].connected_bda);
    499       break;
    500 
    501     /* TODO: Some of these commands may need to be sent to app. For now respond
    502      * with error */
    503     case BTA_AG_AT_BINP_EVT:
    504     case BTA_AG_AT_BTRH_EVT:
    505       send_at_result(BTA_AG_OK_ERROR, BTA_AG_ERR_OP_NOT_SUPPORTED, idx);
    506       break;
    507     case BTA_AG_AT_BAC_EVT:
    508       BTIF_TRACE_DEBUG("AG Bitmap of peer-codecs %d", p_data->val.num);
    509       /* If the peer supports mSBC and the BTIF preferred codec is also mSBC,
    510       then
    511       we should set the BTA AG Codec to mSBC. This would trigger a +BCS to mSBC
    512       at the time
    513       of SCO connection establishment */
    514       if ((btif_conf_hf_force_wbs) && (p_data->val.num & BTA_AG_CODEC_MSBC)) {
    515         BTIF_TRACE_EVENT("%s: btif_hf override-Preferred Codec to MSBC",
    516                          __func__);
    517         BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_MSBC);
    518       } else {
    519         BTIF_TRACE_EVENT("%s btif_hf override-Preferred Codec to CVSD",
    520                          __func__);
    521         BTA_AgSetCodec(btif_hf_cb[idx].handle, BTA_AG_CODEC_CVSD);
    522       }
    523       break;
    524     case BTA_AG_AT_BCS_EVT:
    525       BTIF_TRACE_DEBUG("%s: AG final selected codec is 0x%02x 1=CVSD 2=MSBC",
    526                        __func__, p_data->val.num);
    527       /* No BTHF_WBS_NONE case, because HF1.6 supported device can send BCS */
    528       /* Only CVSD is considered narrow band speech */
    529       bt_hf_callbacks->WbsCallback(
    530           (p_data->val.num == BTA_AG_CODEC_CVSD) ? BTHF_WBS_NO : BTHF_WBS_YES,
    531           &btif_hf_cb[idx].connected_bda);
    532       break;
    533 
    534     case BTA_AG_AT_BIND_EVT:
    535       if (p_data->val.hdr.status == BTA_AG_SUCCESS) {
    536         bt_hf_callbacks->AtBindCallback(p_data->val.str,
    537                                         &btif_hf_cb[idx].connected_bda);
    538       }
    539       break;
    540 
    541     case BTA_AG_AT_BIEV_EVT:
    542       if (p_data->val.hdr.status == BTA_AG_SUCCESS) {
    543         bt_hf_callbacks->AtBievCallback((bthf_hf_ind_type_t)p_data->val.lidx,
    544                                         (int)p_data->val.num,
    545                                         &btif_hf_cb[idx].connected_bda);
    546       }
    547       break;
    548     case BTA_AG_AT_BIA_EVT:
    549       if (p_data->val.hdr.status == BTA_AG_SUCCESS) {
    550         uint32_t bia_mask_out = p_data->val.num;
    551         bool service = !is_nth_bit_enabled(bia_mask_out, BTA_AG_IND_SERVICE);
    552         bool roam = !is_nth_bit_enabled(bia_mask_out, BTA_AG_IND_ROAM);
    553         bool signal = !is_nth_bit_enabled(bia_mask_out, BTA_AG_IND_SIGNAL);
    554         bool battery = !is_nth_bit_enabled(bia_mask_out, BTA_AG_IND_BATTCHG);
    555         bt_hf_callbacks->AtBiaCallback(service, roam, signal, battery,
    556                                        &btif_hf_cb[idx].connected_bda);
    557       }
    558       break;
    559     default:
    560       LOG(WARNING) << __func__ << ": unhandled event " << event;
    561       break;
    562   }
    563 }
    564 
    565 /*******************************************************************************
    566  *
    567  * Function         bte_hf_evt
    568  *
    569  * Description      Switches context from BTE to BTIF for all HF events
    570  *
    571  * Returns          void
    572  *
    573  ******************************************************************************/
    574 
    575 static void bte_hf_evt(tBTA_AG_EVT event, tBTA_AG* p_data) {
    576   bt_status_t status;
    577   int param_len = 0;
    578 
    579   /* TODO: BTA sends the union members and not tBTA_AG. If using
    580    * param_len=sizeof(tBTA_AG), we get a crash on memcpy */
    581   if (BTA_AG_REGISTER_EVT == event)
    582     param_len = sizeof(tBTA_AG_REGISTER);
    583   else if (BTA_AG_OPEN_EVT == event)
    584     param_len = sizeof(tBTA_AG_OPEN);
    585   else if (BTA_AG_CONN_EVT == event)
    586     param_len = sizeof(tBTA_AG_CONN);
    587   else if ((BTA_AG_CLOSE_EVT == event) || (BTA_AG_AUDIO_OPEN_EVT == event) ||
    588            (BTA_AG_AUDIO_CLOSE_EVT == event))
    589     param_len = sizeof(tBTA_AG_HDR);
    590   else if (p_data)
    591     param_len = sizeof(tBTA_AG_VAL);
    592 
    593   /* switch context to btif task context (copy full union size for convenience)
    594    */
    595   status = btif_transfer_context(btif_hf_upstreams_evt, (uint16_t)event,
    596                                  (char*)p_data, param_len, nullptr);
    597 
    598   /* catch any failed context transfers */
    599   ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
    600 }
    601 
    602 /*******************************************************************************
    603  *
    604  * Function         connect
    605  *
    606  * Description     connect to headset
    607  *
    608  * Returns         bt_status_t
    609  *
    610  ******************************************************************************/
    611 static bt_status_t connect_int(RawAddress* bd_addr, uint16_t uuid) {
    612   CHECK_BTHF_INIT();
    613   if (is_connected(bd_addr)) {
    614     BTIF_TRACE_WARNING("%s: device %s is already connected", __func__,
    615                        bd_addr->ToString().c_str());
    616     return BT_STATUS_BUSY;
    617   }
    618   btif_hf_cb_t* hf_cb = nullptr;
    619   for (int i = 0; i < btif_max_hf_clients; i++) {
    620     if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_DISCONNECTED) {
    621       hf_cb = &btif_hf_cb[i];
    622       break;
    623     }
    624     // Due to btif queue implementation, when connect_int is called, no btif
    625     // control block should be in connecting state
    626     // Crash here to prevent future code changes from breaking this mechanism
    627     if (btif_hf_cb[i].state == BTHF_CONNECTION_STATE_CONNECTING) {
    628       LOG(FATAL) << __func__ << ": " << btif_hf_cb[i].connected_bda
    629                  << ", handle " << btif_hf_cb[i].handle
    630                  << ", is still in connecting state " << btif_hf_cb[i].state;
    631     }
    632   }
    633   if (hf_cb == nullptr) {
    634     BTIF_TRACE_WARNING(
    635         "%s: Cannot connect %s: maximum %d clients already connected", __func__,
    636         bd_addr->ToString().c_str(), btif_max_hf_clients);
    637     return BT_STATUS_BUSY;
    638   }
    639   hf_cb->state = BTHF_CONNECTION_STATE_CONNECTING;
    640   hf_cb->connected_bda = *bd_addr;
    641   hf_cb->is_initiator = true;
    642   hf_cb->peer_feat = 0;
    643   BTA_AgOpen(hf_cb->handle, hf_cb->connected_bda, BTIF_HF_SECURITY);
    644   return BT_STATUS_SUCCESS;
    645 }
    646 
    647 static void UpdateCallStates(btif_hf_cb_t* control_block, int num_active,
    648                              int num_held, bthf_call_state_t call_setup_state) {
    649   control_block->num_active = num_active;
    650   control_block->num_held = num_held;
    651   control_block->call_setup_state = call_setup_state;
    652 }
    653 
    654 /*******************************************************************************
    655  *
    656  * Function         btif_hf_is_call_idle
    657  *
    658  * Description      returns true if no call is in progress
    659  *
    660  * Returns          bt_status_t
    661  *
    662  ******************************************************************************/
    663 bool IsCallIdle() {
    664   if (!bt_hf_callbacks) return true;
    665 
    666   for (int i = 0; i < btif_max_hf_clients; ++i) {
    667     if ((btif_hf_cb[i].call_setup_state != BTHF_CALL_STATE_IDLE) ||
    668         ((btif_hf_cb[i].num_held + btif_hf_cb[i].num_active) > 0))
    669       return false;
    670   }
    671 
    672   return true;
    673 }
    674 
    675 class HeadsetInterface : Interface {
    676  public:
    677   static Interface* GetInstance() {
    678     static Interface* instance = new HeadsetInterface();
    679     return instance;
    680   }
    681   bt_status_t Init(Callbacks* callbacks, int max_hf_clients,
    682                    bool inband_ringing_enabled) override;
    683   bt_status_t Connect(RawAddress* bd_addr) override;
    684   bt_status_t Disconnect(RawAddress* bd_addr) override;
    685   bt_status_t ConnectAudio(RawAddress* bd_addr) override;
    686   bt_status_t DisconnectAudio(RawAddress* bd_addr) override;
    687   bt_status_t StartVoiceRecognition(RawAddress* bd_addr) override;
    688   bt_status_t StopVoiceRecognition(RawAddress* bd_addr) override;
    689   bt_status_t VolumeControl(bthf_volume_type_t type, int volume,
    690                             RawAddress* bd_addr) override;
    691   bt_status_t DeviceStatusNotification(bthf_network_state_t ntk_state,
    692                                        bthf_service_type_t svc_type, int signal,
    693                                        int batt_chg,
    694                                        RawAddress* bd_addr) override;
    695   bt_status_t CopsResponse(const char* cops, RawAddress* bd_addr) override;
    696   bt_status_t CindResponse(int svc, int num_active, int num_held,
    697                            bthf_call_state_t call_setup_state, int signal,
    698                            int roam, int batt_chg,
    699                            RawAddress* bd_addr) override;
    700   bt_status_t FormattedAtResponse(const char* rsp,
    701                                   RawAddress* bd_addr) override;
    702   bt_status_t AtResponse(bthf_at_response_t response_code, int error_code,
    703                          RawAddress* bd_addr) override;
    704   bt_status_t ClccResponse(int index, bthf_call_direction_t dir,
    705                            bthf_call_state_t state, bthf_call_mode_t mode,
    706                            bthf_call_mpty_type_t mpty, const char* number,
    707                            bthf_call_addrtype_t type,
    708                            RawAddress* bd_addr) override;
    709   bt_status_t PhoneStateChange(int num_active, int num_held,
    710                                bthf_call_state_t call_setup_state,
    711                                const char* number, bthf_call_addrtype_t type,
    712                                RawAddress* bd_addr) override;
    713 
    714   void Cleanup() override;
    715   bt_status_t SetScoAllowed(bool value) override;
    716   bt_status_t SendBsir(bool value, RawAddress* bd_addr) override;
    717   bt_status_t SetActiveDevice(RawAddress* active_device_addr) override;
    718 };
    719 
    720 bt_status_t HeadsetInterface::Init(Callbacks* callbacks, int max_hf_clients,
    721                                    bool inband_ringing_enabled) {
    722   if (inband_ringing_enabled) {
    723     btif_hf_features |= BTA_AG_FEAT_INBAND;
    724   } else {
    725     btif_hf_features &= ~BTA_AG_FEAT_INBAND;
    726   }
    727   CHECK_LE(max_hf_clients, BTA_AG_MAX_NUM_CLIENTS)
    728       << __func__
    729       << "Too many HF clients,"
    730          " maximum is "
    731       << BTA_AG_MAX_NUM_CLIENTS << " was given " << max_hf_clients;
    732   btif_max_hf_clients = max_hf_clients;
    733   BTIF_TRACE_DEBUG(
    734       "%s: btif_hf_features=%zu, max_hf_clients=%d, inband_ringing_enabled=%d",
    735       __func__, btif_hf_features, btif_max_hf_clients, inband_ringing_enabled);
    736   bt_hf_callbacks = callbacks;
    737   for (btif_hf_cb_t& hf_cb : btif_hf_cb) {
    738     reset_control_block(&hf_cb);
    739   }
    740 
    741 // Invoke the enable service API to the core to set the appropriate service_id
    742 // Internally, the HSP_SERVICE_ID shall also be enabled if HFP is enabled
    743 // (phone) otherwise only HSP is enabled (tablet)
    744 #if (defined(BTIF_HF_SERVICES) && (BTIF_HF_SERVICES & BTA_HFP_SERVICE_MASK))
    745   btif_enable_service(BTA_HFP_SERVICE_ID);
    746 #else
    747   btif_enable_service(BTA_HSP_SERVICE_ID);
    748 #endif
    749 
    750   return BT_STATUS_SUCCESS;
    751 }
    752 
    753 bt_status_t HeadsetInterface::Connect(RawAddress* bd_addr) {
    754   CHECK_BTHF_INIT();
    755   return btif_queue_connect(UUID_SERVCLASS_AG_HANDSFREE, bd_addr, connect_int);
    756 }
    757 
    758 bt_status_t HeadsetInterface::Disconnect(RawAddress* bd_addr) {
    759   CHECK_BTHF_INIT();
    760   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    761   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    762     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    763     return BT_STATUS_FAIL;
    764   }
    765   if (!is_connected(bd_addr)) {
    766     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    767                      bd_addr->ToString().c_str());
    768     return BT_STATUS_FAIL;
    769   }
    770   BTA_AgClose(btif_hf_cb[idx].handle);
    771   return BT_STATUS_SUCCESS;
    772 }
    773 
    774 bt_status_t HeadsetInterface::ConnectAudio(RawAddress* bd_addr) {
    775   CHECK_BTHF_INIT();
    776   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    777   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    778     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    779     return BT_STATUS_FAIL;
    780   }
    781   /* Check if SLC is connected */
    782   if (!IsSlcConnected(bd_addr)) {
    783     LOG(ERROR) << ": SLC not connected for " << *bd_addr;
    784     return BT_STATUS_NOT_READY;
    785   }
    786   BTA_AgAudioOpen(btif_hf_cb[idx].handle);
    787   // Inform the application that the audio connection has been initiated
    788   // successfully
    789   do_in_jni_thread(base::Bind(&Callbacks::AudioStateCallback,
    790                               // Manual pointer management for now
    791                               base::Unretained(bt_hf_callbacks),
    792                               BTHF_AUDIO_STATE_CONNECTING,
    793                               &btif_hf_cb[idx].connected_bda));
    794   return BT_STATUS_SUCCESS;
    795 }
    796 
    797 bt_status_t HeadsetInterface::DisconnectAudio(RawAddress* bd_addr) {
    798   CHECK_BTHF_INIT();
    799   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    800   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    801     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    802     return BT_STATUS_FAIL;
    803   }
    804   if (!is_connected(bd_addr)) {
    805     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    806                      bd_addr->ToString().c_str());
    807     return BT_STATUS_FAIL;
    808   }
    809   BTA_AgAudioClose(btif_hf_cb[idx].handle);
    810   return BT_STATUS_SUCCESS;
    811 }
    812 
    813 bt_status_t HeadsetInterface::StartVoiceRecognition(RawAddress* bd_addr) {
    814   CHECK_BTHF_INIT();
    815   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    816   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    817     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    818     return BT_STATUS_FAIL;
    819   }
    820   if (!is_connected(bd_addr)) {
    821     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    822                      bd_addr->ToString().c_str());
    823     return BT_STATUS_NOT_READY;
    824   }
    825   if (!(btif_hf_cb[idx].peer_feat & BTA_AG_PEER_FEAT_VREC)) {
    826     BTIF_TRACE_ERROR("%s: voice recognition not supported, features=0x%x",
    827                      __func__, btif_hf_cb[idx].peer_feat);
    828     return BT_STATUS_UNSUPPORTED;
    829   }
    830   tBTA_AG_RES_DATA ag_res = {};
    831   ag_res.state = true;
    832   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_BVRA_RES, ag_res);
    833   return BT_STATUS_SUCCESS;
    834 }
    835 
    836 bt_status_t HeadsetInterface::StopVoiceRecognition(RawAddress* bd_addr) {
    837   CHECK_BTHF_INIT();
    838   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    839 
    840   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    841     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    842     return BT_STATUS_FAIL;
    843   }
    844   if (!is_connected(bd_addr)) {
    845     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    846                      bd_addr->ToString().c_str());
    847     return BT_STATUS_NOT_READY;
    848   }
    849   if (!(btif_hf_cb[idx].peer_feat & BTA_AG_PEER_FEAT_VREC)) {
    850     BTIF_TRACE_ERROR("%s: voice recognition not supported, features=0x%x",
    851                      __func__, btif_hf_cb[idx].peer_feat);
    852     return BT_STATUS_UNSUPPORTED;
    853   }
    854   tBTA_AG_RES_DATA ag_res = {};
    855   ag_res.state = false;
    856   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_BVRA_RES, ag_res);
    857   return BT_STATUS_SUCCESS;
    858 }
    859 
    860 bt_status_t HeadsetInterface::VolumeControl(bthf_volume_type_t type, int volume,
    861                                             RawAddress* bd_addr) {
    862   CHECK_BTHF_INIT();
    863   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    864   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    865     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    866     return BT_STATUS_FAIL;
    867   }
    868   if (!is_connected(bd_addr)) {
    869     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    870                      bd_addr->ToString().c_str());
    871     return BT_STATUS_FAIL;
    872   }
    873   tBTA_AG_RES_DATA ag_res = {};
    874   ag_res.num = static_cast<uint16_t>(volume);
    875   BTA_AgResult(btif_hf_cb[idx].handle,
    876                (type == BTHF_VOLUME_TYPE_SPK) ? BTA_AG_SPK_RES : BTA_AG_MIC_RES,
    877                ag_res);
    878   return BT_STATUS_SUCCESS;
    879 }
    880 
    881 bt_status_t HeadsetInterface::DeviceStatusNotification(
    882     bthf_network_state_t ntk_state, bthf_service_type_t svc_type, int signal,
    883     int batt_chg, RawAddress* bd_addr) {
    884   CHECK_BTHF_INIT();
    885   if (!bd_addr) {
    886     BTIF_TRACE_WARNING("%s: bd_addr is null", __func__);
    887     return BT_STATUS_FAIL;
    888   }
    889   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    890   if (idx < 0 || idx > BTA_AG_MAX_NUM_CLIENTS) {
    891     BTIF_TRACE_WARNING("%s: invalid index %d for %s", __func__, idx,
    892                        bd_addr->ToString().c_str());
    893     return BT_STATUS_FAIL;
    894   }
    895   const btif_hf_cb_t& control_block = btif_hf_cb[idx];
    896   // ok if no device is connected
    897   if (is_connected(nullptr)) {
    898     // send all indicators to BTA.
    899     // BTA will make sure no duplicates are sent out
    900     send_indicator_update(control_block, BTA_AG_IND_SERVICE,
    901                           (ntk_state == BTHF_NETWORK_STATE_AVAILABLE) ? 1 : 0);
    902     send_indicator_update(control_block, BTA_AG_IND_ROAM,
    903                           (svc_type == BTHF_SERVICE_TYPE_HOME) ? 0 : 1);
    904     send_indicator_update(control_block, BTA_AG_IND_SIGNAL, signal);
    905     send_indicator_update(control_block, BTA_AG_IND_BATTCHG, batt_chg);
    906   }
    907   return BT_STATUS_SUCCESS;
    908 }
    909 
    910 bt_status_t HeadsetInterface::CopsResponse(const char* cops,
    911                                            RawAddress* bd_addr) {
    912   CHECK_BTHF_INIT();
    913   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    914   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    915     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    916     return BT_STATUS_FAIL;
    917   }
    918   if (!is_connected(bd_addr)) {
    919     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    920                      bd_addr->ToString().c_str());
    921     return BT_STATUS_FAIL;
    922   }
    923   tBTA_AG_RES_DATA ag_res = {};
    924   /* Format the response */
    925   snprintf(ag_res.str, sizeof(ag_res.str), "0,0,\"%.16s\"", cops);
    926   ag_res.ok_flag = BTA_AG_OK_DONE;
    927   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_COPS_RES, ag_res);
    928   return BT_STATUS_SUCCESS;
    929 }
    930 
    931 bt_status_t HeadsetInterface::CindResponse(int svc, int num_active,
    932                                            int num_held,
    933                                            bthf_call_state_t call_setup_state,
    934                                            int signal, int roam, int batt_chg,
    935                                            RawAddress* bd_addr) {
    936   CHECK_BTHF_INIT();
    937   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    938   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    939     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    940     return BT_STATUS_FAIL;
    941   }
    942   if (!is_connected(bd_addr)) {
    943     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    944                      bd_addr->ToString().c_str());
    945     return BT_STATUS_FAIL;
    946   }
    947   tBTA_AG_RES_DATA ag_res = {};
    948   // per the errata 2043, call=1 implies atleast one call is in progress
    949   // (active/held), see:
    950   // https://www.bluetooth.org/errata/errata_view.cfm?errata_id=2043
    951   snprintf(ag_res.str, sizeof(ag_res.str), "%d,%d,%d,%d,%d,%d,%d",
    952            (num_active + num_held) ? 1 : 0,          /* Call state */
    953            callstate_to_callsetup(call_setup_state), /* Callsetup state */
    954            svc,                                      /* network service */
    955            signal,                                   /* Signal strength */
    956            roam,                                     /* Roaming indicator */
    957            batt_chg,                                 /* Battery level */
    958            ((num_held == 0) ? 0 : ((num_active == 0) ? 2 : 1))); /* Call held */
    959   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_CIND_RES, ag_res);
    960   return BT_STATUS_SUCCESS;
    961 }
    962 
    963 bt_status_t HeadsetInterface::FormattedAtResponse(const char* rsp,
    964                                                   RawAddress* bd_addr) {
    965   CHECK_BTHF_INIT();
    966   tBTA_AG_RES_DATA ag_res = {};
    967   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    968   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    969     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    970     return BT_STATUS_FAIL;
    971   }
    972   if (!is_connected(bd_addr)) {
    973     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    974                      bd_addr->ToString().c_str());
    975     return BT_STATUS_FAIL;
    976   }
    977   /* Format the response and send */
    978   strncpy(ag_res.str, rsp, BTA_AG_AT_MAX_LEN);
    979   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_UNAT_RES, ag_res);
    980   return BT_STATUS_SUCCESS;
    981 }
    982 
    983 bt_status_t HeadsetInterface::AtResponse(bthf_at_response_t response_code,
    984                                          int error_code, RawAddress* bd_addr) {
    985   CHECK_BTHF_INIT();
    986   int idx = btif_hf_idx_by_bdaddr(bd_addr);
    987   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
    988     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
    989     return BT_STATUS_FAIL;
    990   }
    991   if (!is_connected(bd_addr)) {
    992     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
    993                      bd_addr->ToString().c_str());
    994     return BT_STATUS_FAIL;
    995   }
    996   send_at_result(
    997       (response_code == BTHF_AT_RESPONSE_OK) ? BTA_AG_OK_DONE : BTA_AG_OK_ERROR,
    998       static_cast<uint16_t>(error_code), idx);
    999   return BT_STATUS_SUCCESS;
   1000 }
   1001 
   1002 bt_status_t HeadsetInterface::ClccResponse(
   1003     int index, bthf_call_direction_t dir, bthf_call_state_t state,
   1004     bthf_call_mode_t mode, bthf_call_mpty_type_t mpty, const char* number,
   1005     bthf_call_addrtype_t type, RawAddress* bd_addr) {
   1006   CHECK_BTHF_INIT();
   1007   int idx = btif_hf_idx_by_bdaddr(bd_addr);
   1008   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
   1009     BTIF_TRACE_ERROR("%s: Invalid index %d", __func__, idx);
   1010     return BT_STATUS_FAIL;
   1011   }
   1012   if (!is_connected(bd_addr)) {
   1013     BTIF_TRACE_ERROR("%s: %s is not connected", __func__,
   1014                      bd_addr->ToString().c_str());
   1015     return BT_STATUS_FAIL;
   1016   }
   1017   tBTA_AG_RES_DATA ag_res = {};
   1018   /* Format the response */
   1019   if (index == 0) {
   1020     ag_res.ok_flag = BTA_AG_OK_DONE;
   1021   } else {
   1022     BTIF_TRACE_EVENT(
   1023         "clcc_response: [%d] dir %d state %d mode %d number = %s type = %d",
   1024         index, dir, state, mode, number, type);
   1025     int res_strlen = snprintf(ag_res.str, sizeof(ag_res.str), "%d,%d,%d,%d,%d",
   1026                               index, dir, state, mode, mpty);
   1027     if (number) {
   1028       size_t rem_bytes = sizeof(ag_res.str) - res_strlen;
   1029       char dialnum[sizeof(ag_res.str)];
   1030       size_t newidx = 0;
   1031       if (type == BTHF_CALL_ADDRTYPE_INTERNATIONAL && *number != '+') {
   1032         dialnum[newidx++] = '+';
   1033       }
   1034       for (size_t i = 0; number[i] != 0; i++) {
   1035         if (utl_isdialchar(number[i])) {
   1036           dialnum[newidx++] = number[i];
   1037         }
   1038       }
   1039       dialnum[newidx] = 0;
   1040       snprintf(&ag_res.str[res_strlen], rem_bytes, ",\"%s\",%d", dialnum, type);
   1041     }
   1042   }
   1043   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_CLCC_RES, ag_res);
   1044   return BT_STATUS_SUCCESS;
   1045 }
   1046 
   1047 bt_status_t HeadsetInterface::PhoneStateChange(
   1048     int num_active, int num_held, bthf_call_state_t call_setup_state,
   1049     const char* number, bthf_call_addrtype_t type, RawAddress* bd_addr) {
   1050   CHECK_BTHF_INIT();
   1051   if (!bd_addr) {
   1052     BTIF_TRACE_WARNING("%s: bd_addr is null", __func__);
   1053     return BT_STATUS_FAIL;
   1054   }
   1055   int idx = btif_hf_idx_by_bdaddr(bd_addr);
   1056   if (idx < 0 || idx > BTA_AG_MAX_NUM_CLIENTS) {
   1057     BTIF_TRACE_WARNING("%s: invalid index %d for %s", __func__, idx,
   1058                        bd_addr->ToString().c_str());
   1059     return BT_STATUS_FAIL;
   1060   }
   1061   const btif_hf_cb_t& control_block = btif_hf_cb[idx];
   1062   if (!IsSlcConnected(bd_addr)) {
   1063     LOG(WARNING) << ": SLC not connected for " << *bd_addr;
   1064     return BT_STATUS_NOT_READY;
   1065   }
   1066   if (call_setup_state == BTHF_CALL_STATE_DISCONNECTED) {
   1067     // HFP spec does not handle cases when a call is being disconnected.
   1068     // Since DISCONNECTED state must lead to IDLE state, ignoring it here.s
   1069     LOG(INFO) << __func__
   1070               << ": Ignore call state change to DISCONNECTED, idx=" << idx
   1071               << ", addr=" << *bd_addr << ", num_active=" << num_active
   1072               << ", num_held=" << num_held;
   1073     return BT_STATUS_SUCCESS;
   1074   }
   1075   LOG(INFO) << __func__ << ": idx=" << idx << ", addr=" << *bd_addr
   1076             << ", active_bda=" << active_bda << ", num_active=" << num_active
   1077             << ", prev_num_active" << control_block.num_active
   1078             << ", num_held=" << num_held
   1079             << ", prev_num_held=" << control_block.num_held
   1080             << ", call_state=" << dump_hf_call_state(call_setup_state)
   1081             << ", prev_call_state="
   1082             << dump_hf_call_state(control_block.call_setup_state);
   1083   tBTA_AG_RES res = 0xFF;
   1084   bt_status_t status = BT_STATUS_SUCCESS;
   1085   bool active_call_updated = false;
   1086 
   1087   /* if all indicators are 0, send end call and return */
   1088   if (num_active == 0 && num_held == 0 &&
   1089       call_setup_state == BTHF_CALL_STATE_IDLE) {
   1090     VLOG(1) << __func__ << ": call ended";
   1091     BTA_AgResult(control_block.handle, BTA_AG_END_CALL_RES,
   1092                  tBTA_AG_RES_DATA::kEmpty);
   1093     /* if held call was present, reset that as well */
   1094     if (control_block.num_held) {
   1095       send_indicator_update(control_block, BTA_AG_IND_CALLHELD, 0);
   1096     }
   1097     UpdateCallStates(&btif_hf_cb[idx], num_active, num_held, call_setup_state);
   1098     return status;
   1099   }
   1100 
   1101   /* active state can change when:
   1102   ** 1. an outgoing/incoming call was answered
   1103   ** 2. an held was resumed
   1104   ** 3. without callsetup notifications, call became active
   1105   ** (3) can happen if call is active and a headset connects to us
   1106   **
   1107   ** In the case of (3), we will have to notify the stack of an active
   1108   ** call, instead of sending an indicator update. This will also
   1109   ** force the SCO to be setup. Handle this special case here prior to
   1110   ** call setup handling
   1111   */
   1112   if (((num_active + num_held) > 0) && (control_block.num_active == 0) &&
   1113       (control_block.num_held == 0) &&
   1114       (control_block.call_setup_state == BTHF_CALL_STATE_IDLE)) {
   1115     tBTA_AG_RES_DATA ag_res = {};
   1116     BTIF_TRACE_DEBUG(
   1117         "%s: Active/Held call notification received without call setup "
   1118         "update",
   1119         __func__);
   1120 
   1121     ag_res.audio_handle = BTA_AG_HANDLE_SCO_NO_CHANGE;
   1122     // Addition call setup with the Active call
   1123     // CIND response should have been updated.
   1124     // just open SCO connection.
   1125     if (call_setup_state != BTHF_CALL_STATE_IDLE) {
   1126       res = BTA_AG_MULTI_CALL_RES;
   1127     } else {
   1128       res = BTA_AG_OUT_CALL_CONN_RES;
   1129     }
   1130     BTA_AgResult(control_block.handle, res, ag_res);
   1131     active_call_updated = true;
   1132   }
   1133 
   1134   /* Ringing call changed? */
   1135   if (call_setup_state != control_block.call_setup_state) {
   1136     tBTA_AG_RES_DATA ag_res = {};
   1137     ag_res.audio_handle = BTA_AG_HANDLE_SCO_NO_CHANGE;
   1138     BTIF_TRACE_DEBUG("%s: Call setup states changed. old: %s new: %s", __func__,
   1139                      dump_hf_call_state(control_block.call_setup_state),
   1140                      dump_hf_call_state(call_setup_state));
   1141     switch (call_setup_state) {
   1142       case BTHF_CALL_STATE_IDLE: {
   1143         switch (control_block.call_setup_state) {
   1144           case BTHF_CALL_STATE_INCOMING:
   1145             if (num_active > control_block.num_active) {
   1146               res = BTA_AG_IN_CALL_CONN_RES;
   1147               if (is_active_device(*bd_addr)) {
   1148                 ag_res.audio_handle = control_block.handle;
   1149               }
   1150             } else if (num_held > control_block.num_held)
   1151               res = BTA_AG_IN_CALL_HELD_RES;
   1152             else
   1153               res = BTA_AG_CALL_CANCEL_RES;
   1154             break;
   1155           case BTHF_CALL_STATE_DIALING:
   1156           case BTHF_CALL_STATE_ALERTING:
   1157             if (num_active > control_block.num_active) {
   1158               res = BTA_AG_OUT_CALL_CONN_RES;
   1159             } else
   1160               res = BTA_AG_CALL_CANCEL_RES;
   1161             break;
   1162           default:
   1163             BTIF_TRACE_ERROR("%s: Incorrect call state prev=%d, now=%d",
   1164                              __func__, control_block.call_setup_state,
   1165                              call_setup_state);
   1166             status = BT_STATUS_PARM_INVALID;
   1167             break;
   1168         }
   1169       } break;
   1170 
   1171       case BTHF_CALL_STATE_INCOMING:
   1172         if (num_active || num_held) {
   1173           res = BTA_AG_CALL_WAIT_RES;
   1174         } else {
   1175           res = BTA_AG_IN_CALL_RES;
   1176           if (is_active_device(*bd_addr)) {
   1177             ag_res.audio_handle = control_block.handle;
   1178           }
   1179         }
   1180         if (number) {
   1181           int xx = 0;
   1182           if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+'))
   1183             xx = snprintf(ag_res.str, sizeof(ag_res.str), "\"+%s\"", number);
   1184           else
   1185             xx = snprintf(ag_res.str, sizeof(ag_res.str), "\"%s\"", number);
   1186           ag_res.num = type;
   1187 
   1188           if (res == BTA_AG_CALL_WAIT_RES)
   1189             snprintf(&ag_res.str[xx], sizeof(ag_res.str) - xx, ",%d", type);
   1190         }
   1191         break;
   1192       case BTHF_CALL_STATE_DIALING:
   1193         if (!(num_active + num_held) && is_active_device(*bd_addr)) {
   1194           ag_res.audio_handle = control_block.handle;
   1195         }
   1196         res = BTA_AG_OUT_CALL_ORIG_RES;
   1197         break;
   1198       case BTHF_CALL_STATE_ALERTING:
   1199         /* if we went from idle->alert, force SCO setup here. dialing usually
   1200          * triggers it */
   1201         if ((control_block.call_setup_state == BTHF_CALL_STATE_IDLE) &&
   1202             !(num_active + num_held) && is_active_device(*bd_addr)) {
   1203           ag_res.audio_handle = control_block.handle;
   1204         }
   1205         res = BTA_AG_OUT_CALL_ALERT_RES;
   1206         break;
   1207       default:
   1208         BTIF_TRACE_ERROR("%s: Incorrect call state prev=%d, now=%d", __func__,
   1209                          control_block.call_setup_state, call_setup_state);
   1210         status = BT_STATUS_PARM_INVALID;
   1211         break;
   1212     }
   1213     BTIF_TRACE_DEBUG("%s: Call setup state changed. res=%d, audio_handle=%d",
   1214                      __func__, res, ag_res.audio_handle);
   1215 
   1216     if (res != 0xFF) {
   1217       BTA_AgResult(control_block.handle, res, ag_res);
   1218     }
   1219 
   1220     /* if call setup is idle, we have already updated call indicator, jump out
   1221      */
   1222     if (call_setup_state == BTHF_CALL_STATE_IDLE) {
   1223       /* check & update callheld */
   1224       if ((num_held > 0) && (num_active > 0)) {
   1225         send_indicator_update(control_block, BTA_AG_IND_CALLHELD, 1);
   1226       }
   1227       UpdateCallStates(&btif_hf_cb[idx], num_active, num_held,
   1228                        call_setup_state);
   1229       return status;
   1230     }
   1231   }
   1232 
   1233   /**
   1234    * Handle call indicator change
   1235    *
   1236    * Per the errata 2043, call=1 implies at least one call is in progress
   1237    * (active or held)
   1238    * See: https://www.bluetooth.org/errata/errata_view.cfm?errata_id=2043
   1239    *
   1240    **/
   1241   if (!active_call_updated &&
   1242       ((num_active + num_held) !=
   1243        (control_block.num_active + control_block.num_held))) {
   1244     VLOG(1) << __func__ << ": in progress call states changed, active=["
   1245             << control_block.num_active << "->" << num_active << "], held=["
   1246             << control_block.num_held << "->" << num_held;
   1247     send_indicator_update(control_block, BTA_AG_IND_CALL,
   1248                           ((num_active + num_held) > 0) ? BTA_AG_CALL_ACTIVE
   1249                                                         : BTA_AG_CALL_INACTIVE);
   1250   }
   1251 
   1252   /* Held Changed? */
   1253   if (num_held != control_block.num_held ||
   1254       ((num_active == 0) && ((num_held + control_block.num_held) > 1))) {
   1255     BTIF_TRACE_DEBUG("%s: Held call states changed. old: %d new: %d", __func__,
   1256                      control_block.num_held, num_held);
   1257     send_indicator_update(control_block, BTA_AG_IND_CALLHELD,
   1258                           ((num_held == 0) ? 0 : ((num_active == 0) ? 2 : 1)));
   1259   }
   1260 
   1261   /* Calls Swapped? */
   1262   if ((call_setup_state == control_block.call_setup_state) &&
   1263       (num_active && num_held) && (num_active == control_block.num_active) &&
   1264       (num_held == control_block.num_held)) {
   1265     BTIF_TRACE_DEBUG("%s: Calls swapped", __func__);
   1266     send_indicator_update(control_block, BTA_AG_IND_CALLHELD, 1);
   1267   }
   1268 
   1269   /* When call is hung up and still there is another call is in active,
   1270    * some of the HF cannot acquire the call states by its own. If HF try
   1271    * to terminate a call, it may not send the command AT+CHUP because the
   1272    * call states are not updated properly. HF should get informed the call
   1273    * status forcibly.
   1274    */
   1275   if ((control_block.num_active == num_active && num_active != 0) &&
   1276       (control_block.num_held != num_held && num_held == 0)) {
   1277     tBTA_AG_RES_DATA ag_res = {};
   1278     ag_res.ind.id = BTA_AG_IND_CALL;
   1279     ag_res.ind.value = num_active;
   1280     BTA_AgResult(control_block.handle, BTA_AG_IND_RES_ON_DEMAND, ag_res);
   1281   }
   1282 
   1283   UpdateCallStates(&btif_hf_cb[idx], num_active, num_held, call_setup_state);
   1284   return status;
   1285 }
   1286 
   1287 void HeadsetInterface::Cleanup() {
   1288   BTIF_TRACE_EVENT("%s", __func__);
   1289 
   1290   btif_queue_cleanup(UUID_SERVCLASS_AG_HANDSFREE);
   1291   if (bt_hf_callbacks) {
   1292 #if (defined(BTIF_HF_SERVICES) && (BTIF_HF_SERVICES & BTA_HFP_SERVICE_MASK))
   1293     btif_disable_service(BTA_HFP_SERVICE_ID);
   1294 #else
   1295     btif_disable_service(BTA_HSP_SERVICE_ID);
   1296 #endif
   1297     bt_hf_callbacks = nullptr;
   1298   }
   1299 }
   1300 
   1301 bt_status_t HeadsetInterface::SetScoAllowed(bool value) {
   1302   CHECK_BTHF_INIT();
   1303   BTA_AgSetScoAllowed(value);
   1304   return BT_STATUS_SUCCESS;
   1305 }
   1306 
   1307 bt_status_t HeadsetInterface::SendBsir(bool value, RawAddress* bd_addr) {
   1308   CHECK_BTHF_INIT();
   1309   int idx = btif_hf_idx_by_bdaddr(bd_addr);
   1310   if ((idx < 0) || (idx >= BTA_AG_MAX_NUM_CLIENTS)) {
   1311     BTIF_TRACE_ERROR("%s: Invalid index %d for %s", __func__, idx,
   1312                      bd_addr->ToString().c_str());
   1313     return BT_STATUS_FAIL;
   1314   }
   1315   if (!is_connected(bd_addr)) {
   1316     BTIF_TRACE_ERROR("%s: %s not connected", __func__,
   1317                      bd_addr->ToString().c_str());
   1318     return BT_STATUS_FAIL;
   1319   }
   1320   tBTA_AG_RES_DATA ag_result = {};
   1321   ag_result.state = value;
   1322   BTA_AgResult(btif_hf_cb[idx].handle, BTA_AG_INBAND_RING_RES, ag_result);
   1323   return BT_STATUS_SUCCESS;
   1324 }
   1325 
   1326 bt_status_t HeadsetInterface::SetActiveDevice(RawAddress* active_device_addr) {
   1327   CHECK_BTHF_INIT();
   1328   active_bda = *active_device_addr;
   1329   BTA_AgSetActiveDevice(*active_device_addr);
   1330   return BT_STATUS_SUCCESS;
   1331 }
   1332 
   1333 /*******************************************************************************
   1334  *
   1335  * Function         btif_hf_execute_service
   1336  *
   1337  * Description      Initializes/Shuts down the service
   1338  *
   1339  * Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
   1340  *
   1341  ******************************************************************************/
   1342 bt_status_t ExecuteService(bool b_enable) {
   1343   const char* service_names_raw[] = BTIF_HF_SERVICE_NAMES;
   1344   std::vector<std::string> service_names;
   1345   for (const char* service_name_raw : service_names_raw) {
   1346     if (service_name_raw) {
   1347       service_names.emplace_back(service_name_raw);
   1348     }
   1349   }
   1350   if (b_enable) {
   1351     /* Enable and register with BTA-AG */
   1352     BTA_AgEnable(bte_hf_evt);
   1353     for (uint8_t app_id = 0; app_id < btif_max_hf_clients; app_id++) {
   1354       BTA_AgRegister(BTIF_HF_SERVICES, BTIF_HF_SECURITY, btif_hf_features,
   1355                      service_names, app_id);
   1356     }
   1357   } else {
   1358     /* De-register AG */
   1359     for (int i = 0; i < btif_max_hf_clients; i++) {
   1360       BTA_AgDeregister(btif_hf_cb[i].handle);
   1361     }
   1362     /* Disable AG */
   1363     BTA_AgDisable();
   1364   }
   1365   return BT_STATUS_SUCCESS;
   1366 }
   1367 
   1368 /*******************************************************************************
   1369  *
   1370  * Function         btif_hf_get_interface
   1371  *
   1372  * Description      Get the hf callback interface
   1373  *
   1374  * Returns          bthf_interface_t
   1375  *
   1376  ******************************************************************************/
   1377 Interface* GetInterface() {
   1378   VLOG(0) << __func__;
   1379   return HeadsetInterface::GetInstance();
   1380 }
   1381 
   1382 }  // namespace headset
   1383 }  // namespace bluetooth
   1384