Home | History | Annotate | Download | only in btu
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-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  *  This file contains functions that interface with the HCI transport. On
     22  *  the receive side, it routes events to the appropriate handler, e.g.
     23  *  L2CAP, ScoMgr. On the transmit side, it manages the command
     24  *  transmission.
     25  *
     26  ******************************************************************************/
     27 
     28 #define LOG_TAG "bt_btu_hcif"
     29 
     30 #include <base/bind.h>
     31 #include <base/callback.h>
     32 #include <base/location.h>
     33 #include <base/logging.h>
     34 #include <base/threading/thread.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 
     39 #include "bt_common.h"
     40 #include "bt_types.h"
     41 #include "bt_utils.h"
     42 #include "btm_api.h"
     43 #include "btm_int.h"
     44 #include "btu.h"
     45 #include "device/include/controller.h"
     46 #include "hci_layer.h"
     47 #include "hcimsgs.h"
     48 #include "l2c_int.h"
     49 #include "osi/include/log.h"
     50 #include "osi/include/osi.h"
     51 
     52 using tracked_objects::Location;
     53 
     54 extern void btm_process_cancel_complete(uint8_t status, uint8_t mode);
     55 extern void btm_ble_test_command_complete(uint8_t* p);
     56 
     57 /******************************************************************************/
     58 /*            L O C A L    F U N C T I O N     P R O T O T Y P E S            */
     59 /******************************************************************************/
     60 static void btu_hcif_inquiry_comp_evt(uint8_t* p);
     61 static void btu_hcif_inquiry_result_evt(uint8_t* p);
     62 static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p);
     63 static void btu_hcif_extended_inquiry_result_evt(uint8_t* p);
     64 
     65 static void btu_hcif_connection_comp_evt(uint8_t* p);
     66 static void btu_hcif_connection_request_evt(uint8_t* p);
     67 static void btu_hcif_disconnection_comp_evt(uint8_t* p);
     68 static void btu_hcif_authentication_comp_evt(uint8_t* p);
     69 static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len);
     70 static void btu_hcif_encryption_change_evt(uint8_t* p);
     71 static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p);
     72 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p);
     73 static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p);
     74 static void btu_hcif_qos_setup_comp_evt(uint8_t* p);
     75 static void btu_hcif_command_complete_evt(BT_HDR* response, void* context);
     76 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
     77                                         void* context);
     78 static void btu_hcif_hardware_error_evt(uint8_t* p);
     79 static void btu_hcif_flush_occured_evt(void);
     80 static void btu_hcif_role_change_evt(uint8_t* p);
     81 static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p);
     82 static void btu_hcif_mode_change_evt(uint8_t* p);
     83 static void btu_hcif_pin_code_request_evt(uint8_t* p);
     84 static void btu_hcif_link_key_request_evt(uint8_t* p);
     85 static void btu_hcif_link_key_notification_evt(uint8_t* p);
     86 static void btu_hcif_loopback_command_evt(void);
     87 static void btu_hcif_data_buf_overflow_evt(void);
     88 static void btu_hcif_max_slots_changed_evt(void);
     89 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p);
     90 static void btu_hcif_conn_pkt_type_change_evt(void);
     91 static void btu_hcif_qos_violation_evt(uint8_t* p);
     92 static void btu_hcif_page_scan_mode_change_evt(void);
     93 static void btu_hcif_page_scan_rep_mode_chng_evt(void);
     94 static void btu_hcif_esco_connection_comp_evt(uint8_t* p);
     95 static void btu_hcif_esco_connection_chg_evt(uint8_t* p);
     96 
     97 /* Simple Pairing Events */
     98 static void btu_hcif_host_support_evt(uint8_t* p);
     99 static void btu_hcif_io_cap_request_evt(uint8_t* p);
    100 static void btu_hcif_io_cap_response_evt(uint8_t* p);
    101 static void btu_hcif_user_conf_request_evt(uint8_t* p);
    102 static void btu_hcif_user_passkey_request_evt(uint8_t* p);
    103 static void btu_hcif_user_passkey_notif_evt(uint8_t* p);
    104 static void btu_hcif_keypress_notif_evt(uint8_t* p);
    105 static void btu_hcif_rem_oob_request_evt(uint8_t* p);
    106 
    107 static void btu_hcif_simple_pair_complete_evt(uint8_t* p);
    108 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
    109 static void btu_hcif_enhanced_flush_complete_evt(void);
    110 #endif
    111 
    112 #if (BTM_SSR_INCLUDED == TRUE)
    113 static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len);
    114 #endif /* BTM_SSR_INCLUDED == TRUE */
    115 
    116 static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len);
    117 static void btu_ble_read_remote_feat_evt(uint8_t* p);
    118 static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len);
    119 static void btu_ble_proc_ltk_req(uint8_t* p);
    120 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p);
    121 static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len);
    122 #if (BLE_LLT_INCLUDED == TRUE)
    123 static void btu_ble_rc_param_req_evt(uint8_t* p);
    124 #endif
    125 #if (BLE_PRIVACY_SPT == TRUE)
    126 static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len);
    127 #endif
    128 
    129 static void do_in_hci_thread(const tracked_objects::Location& from_here,
    130                              const base::Closure& task) {
    131   base::MessageLoop* hci_message_loop = get_message_loop();
    132   if (!hci_message_loop || !hci_message_loop->task_runner().get()) {
    133     LOG_ERROR(LOG_TAG, "%s: HCI message loop not running, accessed from %s",
    134               __func__, from_here.ToString().c_str());
    135     return;
    136   }
    137 
    138   hci_message_loop->task_runner()->PostTask(from_here, task);
    139 }
    140 
    141 /*******************************************************************************
    142  *
    143  * Function         btu_hcif_process_event
    144  *
    145  * Description      This function is called when an event is received from
    146  *                  the Host Controller.
    147  *
    148  * Returns          void
    149  *
    150  ******************************************************************************/
    151 void btu_hcif_process_event(UNUSED_ATTR uint8_t controller_id, BT_HDR* p_msg) {
    152   uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
    153   uint8_t hci_evt_code, hci_evt_len;
    154   uint8_t ble_sub_code;
    155   STREAM_TO_UINT8(hci_evt_code, p);
    156   STREAM_TO_UINT8(hci_evt_len, p);
    157 
    158   switch (hci_evt_code) {
    159     case HCI_INQUIRY_COMP_EVT:
    160       btu_hcif_inquiry_comp_evt(p);
    161       break;
    162     case HCI_INQUIRY_RESULT_EVT:
    163       btu_hcif_inquiry_result_evt(p);
    164       break;
    165     case HCI_INQUIRY_RSSI_RESULT_EVT:
    166       btu_hcif_inquiry_rssi_result_evt(p);
    167       break;
    168     case HCI_EXTENDED_INQUIRY_RESULT_EVT:
    169       btu_hcif_extended_inquiry_result_evt(p);
    170       break;
    171     case HCI_CONNECTION_COMP_EVT:
    172       btu_hcif_connection_comp_evt(p);
    173       break;
    174     case HCI_CONNECTION_REQUEST_EVT:
    175       btu_hcif_connection_request_evt(p);
    176       break;
    177     case HCI_DISCONNECTION_COMP_EVT:
    178       btu_hcif_disconnection_comp_evt(p);
    179       break;
    180     case HCI_AUTHENTICATION_COMP_EVT:
    181       btu_hcif_authentication_comp_evt(p);
    182       break;
    183     case HCI_RMT_NAME_REQUEST_COMP_EVT:
    184       btu_hcif_rmt_name_request_comp_evt(p, hci_evt_len);
    185       break;
    186     case HCI_ENCRYPTION_CHANGE_EVT:
    187       btu_hcif_encryption_change_evt(p);
    188       break;
    189     case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
    190       btu_hcif_encryption_key_refresh_cmpl_evt(p);
    191       break;
    192     case HCI_READ_RMT_FEATURES_COMP_EVT:
    193       btu_hcif_read_rmt_features_comp_evt(p);
    194       break;
    195     case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
    196       btu_hcif_read_rmt_ext_features_comp_evt(p);
    197       break;
    198     case HCI_READ_RMT_VERSION_COMP_EVT:
    199       btu_hcif_read_rmt_version_comp_evt(p);
    200       break;
    201     case HCI_QOS_SETUP_COMP_EVT:
    202       btu_hcif_qos_setup_comp_evt(p);
    203       break;
    204     case HCI_COMMAND_COMPLETE_EVT:
    205       LOG_ERROR(LOG_TAG,
    206                 "%s should not have received a command complete event. "
    207                 "Someone didn't go through the hci transmit_command function.",
    208                 __func__);
    209       break;
    210     case HCI_COMMAND_STATUS_EVT:
    211       LOG_ERROR(LOG_TAG,
    212                 "%s should not have received a command status event. "
    213                 "Someone didn't go through the hci transmit_command function.",
    214                 __func__);
    215       break;
    216     case HCI_HARDWARE_ERROR_EVT:
    217       btu_hcif_hardware_error_evt(p);
    218       break;
    219     case HCI_FLUSH_OCCURED_EVT:
    220       btu_hcif_flush_occured_evt();
    221       break;
    222     case HCI_ROLE_CHANGE_EVT:
    223       btu_hcif_role_change_evt(p);
    224       break;
    225     case HCI_NUM_COMPL_DATA_PKTS_EVT:
    226       btu_hcif_num_compl_data_pkts_evt(p);
    227       break;
    228     case HCI_MODE_CHANGE_EVT:
    229       btu_hcif_mode_change_evt(p);
    230       break;
    231     case HCI_PIN_CODE_REQUEST_EVT:
    232       btu_hcif_pin_code_request_evt(p);
    233       break;
    234     case HCI_LINK_KEY_REQUEST_EVT:
    235       btu_hcif_link_key_request_evt(p);
    236       break;
    237     case HCI_LINK_KEY_NOTIFICATION_EVT:
    238       btu_hcif_link_key_notification_evt(p);
    239       break;
    240     case HCI_LOOPBACK_COMMAND_EVT:
    241       btu_hcif_loopback_command_evt();
    242       break;
    243     case HCI_DATA_BUF_OVERFLOW_EVT:
    244       btu_hcif_data_buf_overflow_evt();
    245       break;
    246     case HCI_MAX_SLOTS_CHANGED_EVT:
    247       btu_hcif_max_slots_changed_evt();
    248       break;
    249     case HCI_READ_CLOCK_OFF_COMP_EVT:
    250       btu_hcif_read_clock_off_comp_evt(p);
    251       break;
    252     case HCI_CONN_PKT_TYPE_CHANGE_EVT:
    253       btu_hcif_conn_pkt_type_change_evt();
    254       break;
    255     case HCI_QOS_VIOLATION_EVT:
    256       btu_hcif_qos_violation_evt(p);
    257       break;
    258     case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
    259       btu_hcif_page_scan_mode_change_evt();
    260       break;
    261     case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
    262       btu_hcif_page_scan_rep_mode_chng_evt();
    263       break;
    264     case HCI_ESCO_CONNECTION_COMP_EVT:
    265       btu_hcif_esco_connection_comp_evt(p);
    266       break;
    267     case HCI_ESCO_CONNECTION_CHANGED_EVT:
    268       btu_hcif_esco_connection_chg_evt(p);
    269       break;
    270 #if (BTM_SSR_INCLUDED == TRUE)
    271     case HCI_SNIFF_SUB_RATE_EVT:
    272       btu_hcif_ssr_evt(p, hci_evt_len);
    273       break;
    274 #endif /* BTM_SSR_INCLUDED == TRUE */
    275     case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
    276       btu_hcif_host_support_evt(p);
    277       break;
    278     case HCI_IO_CAPABILITY_REQUEST_EVT:
    279       btu_hcif_io_cap_request_evt(p);
    280       break;
    281     case HCI_IO_CAPABILITY_RESPONSE_EVT:
    282       btu_hcif_io_cap_response_evt(p);
    283       break;
    284     case HCI_USER_CONFIRMATION_REQUEST_EVT:
    285       btu_hcif_user_conf_request_evt(p);
    286       break;
    287     case HCI_USER_PASSKEY_REQUEST_EVT:
    288       btu_hcif_user_passkey_request_evt(p);
    289       break;
    290     case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
    291       btu_hcif_rem_oob_request_evt(p);
    292       break;
    293     case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
    294       btu_hcif_simple_pair_complete_evt(p);
    295       break;
    296     case HCI_USER_PASSKEY_NOTIFY_EVT:
    297       btu_hcif_user_passkey_notif_evt(p);
    298       break;
    299     case HCI_KEYPRESS_NOTIFY_EVT:
    300       btu_hcif_keypress_notif_evt(p);
    301       break;
    302 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
    303     case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
    304       btu_hcif_enhanced_flush_complete_evt();
    305       break;
    306 #endif
    307 
    308     case HCI_BLE_EVENT: {
    309       STREAM_TO_UINT8(ble_sub_code, p);
    310 
    311       HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code,
    312                       ble_sub_code);
    313 
    314       uint8_t ble_evt_len = hci_evt_len - 1;
    315       switch (ble_sub_code) {
    316         case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
    317           HCI_TRACE_EVENT("HCI_BLE_ADV_PKT_RPT_EVT");
    318           btm_ble_process_adv_pkt(ble_evt_len, p);
    319           break;
    320         case HCI_BLE_CONN_COMPLETE_EVT:
    321           btu_ble_ll_conn_complete_evt(p, hci_evt_len);
    322           break;
    323         case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
    324           btu_ble_ll_conn_param_upd_evt(p, hci_evt_len);
    325           break;
    326         case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
    327           btu_ble_read_remote_feat_evt(p);
    328           break;
    329         case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
    330           btu_ble_proc_ltk_req(p);
    331           break;
    332 #if (BLE_PRIVACY_SPT == TRUE)
    333         case HCI_BLE_ENHANCED_CONN_COMPLETE_EVT:
    334           btu_ble_proc_enhanced_conn_cmpl(p, hci_evt_len);
    335           break;
    336 #endif
    337 #if (BLE_LLT_INCLUDED == TRUE)
    338         case HCI_BLE_RC_PARAM_REQ_EVT:
    339           btu_ble_rc_param_req_evt(p);
    340           break;
    341 #endif
    342         case HCI_BLE_DATA_LENGTH_CHANGE_EVT:
    343           btu_ble_data_length_change_evt(p, hci_evt_len);
    344           break;
    345 
    346         case HCI_BLE_PHY_UPDATE_COMPLETE_EVT:
    347           btm_ble_process_phy_update_pkt(ble_evt_len, p);
    348           break;
    349 
    350         case HCI_LE_EXTENDED_ADVERTISING_REPORT_EVT:
    351           btm_ble_process_ext_adv_pkt(hci_evt_len, p);
    352           break;
    353 
    354         case HCI_LE_ADVERTISING_SET_TERMINATED_EVT:
    355           btm_le_on_advertising_set_terminated(p, hci_evt_len);
    356           break;
    357       }
    358       break;
    359     }
    360 
    361     case HCI_VENDOR_SPECIFIC_EVT:
    362       btm_vendor_specific_evt(p, hci_evt_len);
    363       break;
    364   }
    365 }
    366 
    367 /*******************************************************************************
    368  *
    369  * Function         btu_hcif_send_cmd
    370  *
    371  * Description      This function is called to send commands to the Host
    372  *                  Controller.
    373  *
    374  * Returns          void
    375  *
    376  ******************************************************************************/
    377 void btu_hcif_send_cmd(UNUSED_ATTR uint8_t controller_id, BT_HDR* p_buf) {
    378   if (!p_buf) return;
    379 
    380   uint16_t opcode;
    381   uint8_t* stream = p_buf->data + p_buf->offset;
    382   void* vsc_callback = NULL;
    383 
    384   STREAM_TO_UINT16(opcode, stream);
    385 
    386   // Eww...horrible hackery here
    387   /* If command was a VSC, then extract command_complete callback */
    388   if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC ||
    389       (opcode == HCI_BLE_RAND) || (opcode == HCI_BLE_ENCRYPT)) {
    390     vsc_callback = *((void**)(p_buf + 1));
    391   }
    392 
    393   hci_layer_get_interface()->transmit_command(
    394       p_buf, btu_hcif_command_complete_evt, btu_hcif_command_status_evt,
    395       vsc_callback);
    396 }
    397 
    398 using hci_cmd_cb = base::Callback<void(uint8_t* /* return_parameters */,
    399                                        uint16_t /* return_parameters_length*/)>;
    400 
    401 struct cmd_with_cb_data {
    402   hci_cmd_cb cb;
    403   Location posted_from;
    404 };
    405 
    406 void cmd_with_cb_data_init(cmd_with_cb_data* cb_wrapper) {
    407   new (&cb_wrapper->cb) hci_cmd_cb;
    408   new (&cb_wrapper->posted_from) Location;
    409 }
    410 
    411 void cmd_with_cb_data_cleanup(cmd_with_cb_data* cb_wrapper) {
    412   cb_wrapper->cb.~hci_cmd_cb();
    413   cb_wrapper->posted_from.~Location();
    414 }
    415 
    416 static void btu_hcif_command_complete_evt_with_cb_on_task(BT_HDR* event,
    417                                                           void* context) {
    418   command_opcode_t opcode;
    419   uint8_t* stream =
    420       event->data + event->offset +
    421       3;  // 2 to skip the event headers, 1 to skip the command credits
    422   STREAM_TO_UINT16(opcode, stream);
    423 
    424   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
    425   HCI_TRACE_DEBUG("command complete for: %s",
    426                   cb_wrapper->posted_from.ToString().c_str());
    427   cb_wrapper->cb.Run(stream, event->len - 5);
    428   cmd_with_cb_data_cleanup(cb_wrapper);
    429   osi_free(cb_wrapper);
    430 
    431   osi_free(event);
    432 }
    433 
    434 static void btu_hcif_command_complete_evt_with_cb(BT_HDR* response,
    435                                                   void* context) {
    436   do_in_hci_thread(FROM_HERE,
    437                    base::Bind(btu_hcif_command_complete_evt_with_cb_on_task,
    438                               response, context));
    439 }
    440 
    441 static void btu_hcif_command_status_evt_with_cb_on_task(uint8_t status,
    442                                                         BT_HDR* event,
    443                                                         void* context) {
    444   command_opcode_t opcode;
    445   uint8_t* stream = event->data + event->offset;
    446   STREAM_TO_UINT16(opcode, stream);
    447 
    448   CHECK(status != 0);
    449 
    450   // report command status error
    451   cmd_with_cb_data* cb_wrapper = (cmd_with_cb_data*)context;
    452   HCI_TRACE_DEBUG("command status for: %s",
    453                   cb_wrapper->posted_from.ToString().c_str());
    454   cb_wrapper->cb.Run(&status, sizeof(uint16_t));
    455   cmd_with_cb_data_cleanup(cb_wrapper);
    456   osi_free(cb_wrapper);
    457 
    458   osi_free(event);
    459 }
    460 
    461 static void btu_hcif_command_status_evt_with_cb(uint8_t status, BT_HDR* command,
    462                                                 void* context) {
    463   // Command is pending, we  report only error.
    464   if (!status) {
    465     osi_free(command);
    466     return;
    467   }
    468 
    469   do_in_hci_thread(
    470       FROM_HERE, base::Bind(btu_hcif_command_status_evt_with_cb_on_task, status,
    471                             command, context));
    472 }
    473 
    474 /* This function is called to send commands to the Host Controller. |cb| is
    475  * called when command status event is called with error code, or when the
    476  * command complete event is received. */
    477 void btu_hcif_send_cmd_with_cb(const tracked_objects::Location& posted_from,
    478                                uint16_t opcode, uint8_t* params,
    479                                uint8_t params_len, hci_cmd_cb cb) {
    480   BT_HDR* p = (BT_HDR*)osi_malloc(HCI_CMD_BUF_SIZE);
    481   uint8_t* pp = (uint8_t*)(p + 1);
    482 
    483   p->len = HCIC_PREAMBLE_SIZE + params_len;
    484   p->offset = 0;
    485 
    486   UINT16_TO_STREAM(pp, opcode);
    487   UINT8_TO_STREAM(pp, params_len);
    488   if (params) {
    489     memcpy(pp, params, params_len);
    490   }
    491 
    492   cmd_with_cb_data* cb_wrapper =
    493       (cmd_with_cb_data*)osi_malloc(sizeof(cmd_with_cb_data));
    494 
    495   cmd_with_cb_data_init(cb_wrapper);
    496   cb_wrapper->cb = cb;
    497   cb_wrapper->posted_from = posted_from;
    498 
    499   hci_layer_get_interface()->transmit_command(
    500       p, btu_hcif_command_complete_evt_with_cb,
    501       btu_hcif_command_status_evt_with_cb, (void*)cb_wrapper);
    502 }
    503 
    504 /*******************************************************************************
    505  *
    506  * Function         btu_hcif_inquiry_comp_evt
    507  *
    508  * Description      Process event HCI_INQUIRY_COMP_EVT
    509  *
    510  * Returns          void
    511  *
    512  ******************************************************************************/
    513 static void btu_hcif_inquiry_comp_evt(uint8_t* p) {
    514   uint8_t status;
    515 
    516   STREAM_TO_UINT8(status, p);
    517 
    518   /* Tell inquiry processing that we are done */
    519   btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
    520 }
    521 
    522 /*******************************************************************************
    523  *
    524  * Function         btu_hcif_inquiry_result_evt
    525  *
    526  * Description      Process event HCI_INQUIRY_RESULT_EVT
    527  *
    528  * Returns          void
    529  *
    530  ******************************************************************************/
    531 static void btu_hcif_inquiry_result_evt(uint8_t* p) {
    532   /* Store results in the cache */
    533   btm_process_inq_results(p, BTM_INQ_RESULT_STANDARD);
    534 }
    535 
    536 /*******************************************************************************
    537  *
    538  * Function         btu_hcif_inquiry_rssi_result_evt
    539  *
    540  * Description      Process event HCI_INQUIRY_RSSI_RESULT_EVT
    541  *
    542  * Returns          void
    543  *
    544  ******************************************************************************/
    545 static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p) {
    546   /* Store results in the cache */
    547   btm_process_inq_results(p, BTM_INQ_RESULT_WITH_RSSI);
    548 }
    549 
    550 /*******************************************************************************
    551  *
    552  * Function         btu_hcif_extended_inquiry_result_evt
    553  *
    554  * Description      Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
    555  *
    556  * Returns          void
    557  *
    558  ******************************************************************************/
    559 static void btu_hcif_extended_inquiry_result_evt(uint8_t* p) {
    560   /* Store results in the cache */
    561   btm_process_inq_results(p, BTM_INQ_RESULT_EXTENDED);
    562 }
    563 
    564 /*******************************************************************************
    565  *
    566  * Function         btu_hcif_connection_comp_evt
    567  *
    568  * Description      Process event HCI_CONNECTION_COMP_EVT
    569  *
    570  * Returns          void
    571  *
    572  ******************************************************************************/
    573 static void btu_hcif_connection_comp_evt(uint8_t* p) {
    574   uint8_t status;
    575   uint16_t handle;
    576   RawAddress bda;
    577   uint8_t link_type;
    578   uint8_t enc_mode;
    579 #if (BTM_SCO_INCLUDED == TRUE)
    580   tBTM_ESCO_DATA esco_data;
    581 #endif
    582 
    583   STREAM_TO_UINT8(status, p);
    584   STREAM_TO_UINT16(handle, p);
    585   STREAM_TO_BDADDR(bda, p);
    586   STREAM_TO_UINT8(link_type, p);
    587   STREAM_TO_UINT8(enc_mode, p);
    588 
    589   handle = HCID_GET_HANDLE(handle);
    590 
    591   if (link_type == HCI_LINK_TYPE_ACL) {
    592     btm_sec_connected(bda, handle, status, enc_mode);
    593 
    594     l2c_link_hci_conn_comp(status, handle, bda);
    595   }
    596 #if (BTM_SCO_INCLUDED == TRUE)
    597   else {
    598     memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
    599     /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
    600     esco_data.bd_addr = bda;
    601     btm_sco_connected(status, &bda, handle, &esco_data);
    602   }
    603 #endif /* BTM_SCO_INCLUDED */
    604 }
    605 
    606 /*******************************************************************************
    607  *
    608  * Function         btu_hcif_connection_request_evt
    609  *
    610  * Description      Process event HCI_CONNECTION_REQUEST_EVT
    611  *
    612  * Returns          void
    613  *
    614  ******************************************************************************/
    615 static void btu_hcif_connection_request_evt(uint8_t* p) {
    616   RawAddress bda;
    617   DEV_CLASS dc;
    618   uint8_t link_type;
    619 
    620   STREAM_TO_BDADDR(bda, p);
    621   STREAM_TO_DEVCLASS(dc, p);
    622   STREAM_TO_UINT8(link_type, p);
    623 
    624   /* Pass request to security manager to check connect filters before */
    625   /* passing request to l2cap */
    626   if (link_type == HCI_LINK_TYPE_ACL) {
    627     btm_sec_conn_req(bda, dc);
    628   }
    629 #if (BTM_SCO_INCLUDED == TRUE)
    630   else {
    631     btm_sco_conn_req(bda, dc, link_type);
    632   }
    633 #endif /* BTM_SCO_INCLUDED */
    634 }
    635 
    636 /*******************************************************************************
    637  *
    638  * Function         btu_hcif_disconnection_comp_evt
    639  *
    640  * Description      Process event HCI_DISCONNECTION_COMP_EVT
    641  *
    642  * Returns          void
    643  *
    644  ******************************************************************************/
    645 static void btu_hcif_disconnection_comp_evt(uint8_t* p) {
    646   uint16_t handle;
    647   uint8_t reason;
    648 
    649   ++p;
    650   STREAM_TO_UINT16(handle, p);
    651   STREAM_TO_UINT8(reason, p);
    652 
    653   handle = HCID_GET_HANDLE(handle);
    654 
    655 #if (BTM_SCO_INCLUDED == TRUE)
    656   /* If L2CAP doesn't know about it, send it to SCO */
    657   if (!l2c_link_hci_disc_comp(handle, reason)) btm_sco_removed(handle, reason);
    658 #else
    659   l2c_link_hci_disc_comp(handle, reason);
    660 #endif /* BTM_SCO_INCLUDED */
    661 
    662   /* Notify security manager */
    663   btm_sec_disconnected(handle, reason);
    664 }
    665 
    666 /*******************************************************************************
    667  *
    668  * Function         btu_hcif_authentication_comp_evt
    669  *
    670  * Description      Process event HCI_AUTHENTICATION_COMP_EVT
    671  *
    672  * Returns          void
    673  *
    674  ******************************************************************************/
    675 static void btu_hcif_authentication_comp_evt(uint8_t* p) {
    676   uint8_t status;
    677   uint16_t handle;
    678 
    679   STREAM_TO_UINT8(status, p);
    680   STREAM_TO_UINT16(handle, p);
    681 
    682   btm_sec_auth_complete(handle, status);
    683 }
    684 
    685 /*******************************************************************************
    686  *
    687  * Function         btu_hcif_rmt_name_request_comp_evt
    688  *
    689  * Description      Process event HCI_RMT_NAME_REQUEST_COMP_EVT
    690  *
    691  * Returns          void
    692  *
    693  ******************************************************************************/
    694 static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len) {
    695   uint8_t status;
    696   RawAddress bd_addr;
    697 
    698   STREAM_TO_UINT8(status, p);
    699   STREAM_TO_BDADDR(bd_addr, p);
    700 
    701   evt_len -= (1 + BD_ADDR_LEN);
    702 
    703   btm_process_remote_name(&bd_addr, p, evt_len, status);
    704 
    705   btm_sec_rmt_name_request_complete(&bd_addr, p, status);
    706 }
    707 
    708 /*******************************************************************************
    709  *
    710  * Function         btu_hcif_encryption_change_evt
    711  *
    712  * Description      Process event HCI_ENCRYPTION_CHANGE_EVT
    713  *
    714  * Returns          void
    715  *
    716  ******************************************************************************/
    717 static void btu_hcif_encryption_change_evt(uint8_t* p) {
    718   uint8_t status;
    719   uint16_t handle;
    720   uint8_t encr_enable;
    721 
    722   STREAM_TO_UINT8(status, p);
    723   STREAM_TO_UINT16(handle, p);
    724   STREAM_TO_UINT8(encr_enable, p);
    725 
    726   btm_acl_encrypt_change(handle, status, encr_enable);
    727   btm_sec_encrypt_change(handle, status, encr_enable);
    728 }
    729 
    730 /*******************************************************************************
    731  *
    732  * Function         btu_hcif_read_rmt_features_comp_evt
    733  *
    734  * Description      Process event HCI_READ_RMT_FEATURES_COMP_EVT
    735  *
    736  * Returns          void
    737  *
    738  ******************************************************************************/
    739 static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p) {
    740   btm_read_remote_features_complete(p);
    741 }
    742 
    743 /*******************************************************************************
    744  *
    745  * Function         btu_hcif_read_rmt_ext_features_comp_evt
    746  *
    747  * Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
    748  *
    749  * Returns          void
    750  *
    751  ******************************************************************************/
    752 static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p) {
    753   uint8_t* p_cur = p;
    754   uint8_t status;
    755   uint16_t handle;
    756 
    757   STREAM_TO_UINT8(status, p_cur);
    758 
    759   if (status == HCI_SUCCESS)
    760     btm_read_remote_ext_features_complete(p);
    761   else {
    762     STREAM_TO_UINT16(handle, p_cur);
    763     btm_read_remote_ext_features_failed(status, handle);
    764   }
    765 }
    766 
    767 /*******************************************************************************
    768  *
    769  * Function         btu_hcif_read_rmt_version_comp_evt
    770  *
    771  * Description      Process event HCI_READ_RMT_VERSION_COMP_EVT
    772  *
    773  * Returns          void
    774  *
    775  ******************************************************************************/
    776 static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p) {
    777   btm_read_remote_version_complete(p);
    778 }
    779 
    780 /*******************************************************************************
    781  *
    782  * Function         btu_hcif_qos_setup_comp_evt
    783  *
    784  * Description      Process event HCI_QOS_SETUP_COMP_EVT
    785  *
    786  * Returns          void
    787  *
    788  ******************************************************************************/
    789 static void btu_hcif_qos_setup_comp_evt(uint8_t* p) {
    790   uint8_t status;
    791   uint16_t handle;
    792   FLOW_SPEC flow;
    793 
    794   STREAM_TO_UINT8(status, p);
    795   STREAM_TO_UINT16(handle, p);
    796   STREAM_TO_UINT8(flow.qos_flags, p);
    797   STREAM_TO_UINT8(flow.service_type, p);
    798   STREAM_TO_UINT32(flow.token_rate, p);
    799   STREAM_TO_UINT32(flow.peak_bandwidth, p);
    800   STREAM_TO_UINT32(flow.latency, p);
    801   STREAM_TO_UINT32(flow.delay_variation, p);
    802 
    803   btm_qos_setup_complete(status, handle, &flow);
    804 }
    805 
    806 /*******************************************************************************
    807  *
    808  * Function         btu_hcif_esco_connection_comp_evt
    809  *
    810  * Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
    811  *
    812  * Returns          void
    813  *
    814  ******************************************************************************/
    815 static void btu_hcif_esco_connection_comp_evt(uint8_t* p) {
    816 #if (BTM_SCO_INCLUDED == TRUE)
    817   tBTM_ESCO_DATA data;
    818   uint16_t handle;
    819   RawAddress bda;
    820   uint8_t status;
    821 
    822   STREAM_TO_UINT8(status, p);
    823   STREAM_TO_UINT16(handle, p);
    824   STREAM_TO_BDADDR(bda, p);
    825 
    826   STREAM_TO_UINT8(data.link_type, p);
    827   STREAM_TO_UINT8(data.tx_interval, p);
    828   STREAM_TO_UINT8(data.retrans_window, p);
    829   STREAM_TO_UINT16(data.rx_pkt_len, p);
    830   STREAM_TO_UINT16(data.tx_pkt_len, p);
    831   STREAM_TO_UINT8(data.air_mode, p);
    832 
    833   data.bd_addr = bda;
    834   btm_sco_connected(status, &bda, handle, &data);
    835 #endif
    836 }
    837 
    838 /*******************************************************************************
    839  *
    840  * Function         btu_hcif_esco_connection_chg_evt
    841  *
    842  * Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
    843  *
    844  * Returns          void
    845  *
    846  ******************************************************************************/
    847 static void btu_hcif_esco_connection_chg_evt(uint8_t* p) {
    848 #if (BTM_SCO_INCLUDED == TRUE)
    849   uint16_t handle;
    850   uint16_t tx_pkt_len;
    851   uint16_t rx_pkt_len;
    852   uint8_t status;
    853   uint8_t tx_interval;
    854   uint8_t retrans_window;
    855 
    856   STREAM_TO_UINT8(status, p);
    857   STREAM_TO_UINT16(handle, p);
    858 
    859   STREAM_TO_UINT8(tx_interval, p);
    860   STREAM_TO_UINT8(retrans_window, p);
    861   STREAM_TO_UINT16(rx_pkt_len, p);
    862   STREAM_TO_UINT16(tx_pkt_len, p);
    863 
    864   btm_esco_proc_conn_chg(status, handle, tx_interval, retrans_window,
    865                          rx_pkt_len, tx_pkt_len);
    866 #endif
    867 }
    868 
    869 /*******************************************************************************
    870  *
    871  * Function         btu_hcif_hdl_command_complete
    872  *
    873  * Description      Handle command complete event
    874  *
    875  * Returns          void
    876  *
    877  ******************************************************************************/
    878 static void btu_hcif_hdl_command_complete(uint16_t opcode, uint8_t* p,
    879                                           uint16_t evt_len,
    880                                           void* p_cplt_cback) {
    881   switch (opcode) {
    882     case HCI_INQUIRY_CANCEL:
    883       /* Tell inquiry processing that we are done */
    884       btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
    885       break;
    886     case HCI_SET_EVENT_FILTER:
    887       btm_event_filter_complete(p);
    888       break;
    889 
    890     case HCI_DELETE_STORED_LINK_KEY:
    891       btm_delete_stored_link_key_complete(p);
    892       break;
    893 
    894     case HCI_READ_LOCAL_NAME:
    895       btm_read_local_name_complete(p, evt_len);
    896       break;
    897 
    898     case HCI_GET_LINK_QUALITY:
    899       btm_read_link_quality_complete(p);
    900       break;
    901 
    902     case HCI_READ_RSSI:
    903       btm_read_rssi_complete(p);
    904       break;
    905 
    906     case HCI_READ_FAILED_CONTACT_COUNTER:
    907       btm_read_failed_contact_counter_complete(p);
    908       break;
    909 
    910     case HCI_READ_AUTOMATIC_FLUSH_TIMEOUT:
    911       btm_read_automatic_flush_timeout_complete(p);
    912       break;
    913 
    914     case HCI_READ_TRANSMIT_POWER_LEVEL:
    915       btm_read_tx_power_complete(p, false);
    916       break;
    917 
    918     case HCI_CREATE_CONNECTION_CANCEL:
    919       btm_create_conn_cancel_complete(p);
    920       break;
    921 
    922     case HCI_READ_LOCAL_OOB_DATA:
    923       btm_read_local_oob_complete(p);
    924       break;
    925 
    926     case HCI_READ_INQ_TX_POWER_LEVEL:
    927       btm_read_inq_tx_power_complete(p);
    928       break;
    929 
    930     /* BLE Commands sComplete*/
    931     case HCI_BLE_ADD_WHITE_LIST:
    932       btm_ble_add_2_white_list_complete(*p);
    933       break;
    934 
    935     case HCI_BLE_CLEAR_WHITE_LIST:
    936       btm_ble_clear_white_list_complete(p, evt_len);
    937       break;
    938 
    939     case HCI_BLE_REMOVE_WHITE_LIST:
    940       btm_ble_remove_from_white_list_complete(p, evt_len);
    941       break;
    942 
    943     case HCI_BLE_RAND:
    944     case HCI_BLE_ENCRYPT:
    945       btm_ble_rand_enc_complete(p, opcode, (tBTM_RAND_ENC_CB*)p_cplt_cback);
    946       break;
    947 
    948     case HCI_BLE_READ_ADV_CHNL_TX_POWER:
    949       btm_read_tx_power_complete(p, true);
    950       break;
    951 
    952     case HCI_BLE_WRITE_ADV_ENABLE:
    953       btm_ble_write_adv_enable_complete(p);
    954       break;
    955 
    956     case HCI_BLE_CREATE_LL_CONN:
    957       btm_ble_create_ll_conn_complete(*p);
    958       break;
    959 
    960     case HCI_BLE_TRANSMITTER_TEST:
    961     case HCI_BLE_RECEIVER_TEST:
    962     case HCI_BLE_TEST_END:
    963       btm_ble_test_command_complete(p);
    964       break;
    965 
    966 #if (BLE_PRIVACY_SPT == TRUE)
    967     case HCI_BLE_ADD_DEV_RESOLVING_LIST:
    968       btm_ble_add_resolving_list_entry_complete(p, evt_len);
    969       break;
    970 
    971     case HCI_BLE_RM_DEV_RESOLVING_LIST:
    972       btm_ble_remove_resolving_list_entry_complete(p, evt_len);
    973       break;
    974 
    975     case HCI_BLE_CLEAR_RESOLVING_LIST:
    976       btm_ble_clear_resolving_list_complete(p, evt_len);
    977       break;
    978 
    979     case HCI_BLE_READ_RESOLVABLE_ADDR_PEER:
    980       btm_ble_read_resolving_list_entry_complete(p, evt_len);
    981       break;
    982 
    983     case HCI_BLE_READ_RESOLVABLE_ADDR_LOCAL:
    984     case HCI_BLE_SET_ADDR_RESOLUTION_ENABLE:
    985     case HCI_BLE_SET_RAND_PRIV_ADDR_TIMOUT:
    986       break;
    987 #endif
    988     default:
    989       if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
    990         btm_vsc_complete(p, opcode, evt_len, (tBTM_CMPL_CB*)p_cplt_cback);
    991       break;
    992   }
    993 }
    994 
    995 /*******************************************************************************
    996  *
    997  * Function         btu_hcif_command_complete_evt
    998  *
    999  * Description      Process event HCI_COMMAND_COMPLETE_EVT
   1000  *
   1001  * Returns          void
   1002  *
   1003  ******************************************************************************/
   1004 static void btu_hcif_command_complete_evt_on_task(BT_HDR* event,
   1005                                                   void* context) {
   1006   command_opcode_t opcode;
   1007   uint8_t* stream =
   1008       event->data + event->offset +
   1009       3;  // 2 to skip the event headers, 1 to skip the command credits
   1010   STREAM_TO_UINT16(opcode, stream);
   1011 
   1012   btu_hcif_hdl_command_complete(
   1013       opcode, stream,
   1014       event->len -
   1015           5,  // 3 for the command complete headers, 2 for the event headers
   1016       context);
   1017 
   1018   osi_free(event);
   1019 }
   1020 
   1021 static void btu_hcif_command_complete_evt(BT_HDR* response, void* context) {
   1022   do_in_hci_thread(FROM_HERE, base::Bind(btu_hcif_command_complete_evt_on_task,
   1023                                          response, context));
   1024 }
   1025 
   1026 /*******************************************************************************
   1027  *
   1028  * Function         btu_hcif_hdl_command_status
   1029  *
   1030  * Description      Handle a command status event
   1031  *
   1032  * Returns          void
   1033  *
   1034  ******************************************************************************/
   1035 static void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status,
   1036                                         uint8_t* p_cmd,
   1037                                         void* p_vsc_status_cback) {
   1038   RawAddress bd_addr;
   1039   uint16_t handle;
   1040 #if (BTM_SCO_INCLUDED == TRUE)
   1041   tBTM_ESCO_DATA esco_data;
   1042 #endif
   1043 
   1044   switch (opcode) {
   1045     case HCI_EXIT_SNIFF_MODE:
   1046     case HCI_EXIT_PARK_MODE:
   1047 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
   1048       if (status != HCI_SUCCESS) {
   1049         /* Allow SCO initiation to continue if waiting for change mode event */
   1050         if (p_cmd != NULL) {
   1051           p_cmd++; /* bypass length field */
   1052           STREAM_TO_UINT16(handle, p_cmd);
   1053           btm_sco_chk_pend_unpark(status, handle);
   1054         }
   1055       }
   1056 #endif
   1057     /* Case Falls Through */
   1058 
   1059     case HCI_HOLD_MODE:
   1060     case HCI_SNIFF_MODE:
   1061     case HCI_PARK_MODE:
   1062       btm_pm_proc_cmd_status(status);
   1063       break;
   1064 
   1065     default:
   1066       /* If command failed to start, we may need to tell BTM */
   1067       if (status != HCI_SUCCESS) {
   1068         switch (opcode) {
   1069           case HCI_INQUIRY:
   1070             /* Tell inquiry processing that we are done */
   1071             btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
   1072             break;
   1073 
   1074           case HCI_RMT_NAME_REQUEST:
   1075             /* Tell inquiry processing that we are done */
   1076             btm_process_remote_name(NULL, NULL, 0, status);
   1077 
   1078             btm_sec_rmt_name_request_complete(NULL, NULL, status);
   1079             break;
   1080 
   1081           case HCI_QOS_SETUP_COMP_EVT:
   1082             /* Tell qos setup that we are done */
   1083             btm_qos_setup_complete(status, 0, NULL);
   1084             break;
   1085 
   1086           case HCI_SWITCH_ROLE:
   1087             /* Tell BTM that the command failed */
   1088             /* read bd addr out of stored command */
   1089             if (p_cmd != NULL) {
   1090               p_cmd++;
   1091               STREAM_TO_BDADDR(bd_addr, p_cmd);
   1092               btm_acl_role_changed(status, &bd_addr, BTM_ROLE_UNDEFINED);
   1093             } else
   1094               btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
   1095             l2c_link_role_changed(nullptr, BTM_ROLE_UNDEFINED,
   1096                                   HCI_ERR_COMMAND_DISALLOWED);
   1097             break;
   1098 
   1099           case HCI_CREATE_CONNECTION:
   1100             /* read bd addr out of stored command */
   1101             if (p_cmd != NULL) {
   1102               p_cmd++;
   1103               STREAM_TO_BDADDR(bd_addr, p_cmd);
   1104               btm_sec_connected(bd_addr, HCI_INVALID_HANDLE, status, 0);
   1105               l2c_link_hci_conn_comp(status, HCI_INVALID_HANDLE, bd_addr);
   1106             }
   1107             break;
   1108 
   1109           case HCI_READ_RMT_EXT_FEATURES:
   1110             if (p_cmd != NULL) {
   1111               p_cmd++; /* skip command length */
   1112               STREAM_TO_UINT16(handle, p_cmd);
   1113             } else
   1114               handle = HCI_INVALID_HANDLE;
   1115 
   1116             btm_read_remote_ext_features_failed(status, handle);
   1117             break;
   1118 
   1119           case HCI_AUTHENTICATION_REQUESTED:
   1120             /* Device refused to start authentication.  That should be treated
   1121              * as authentication failure. */
   1122             btm_sec_auth_complete(BTM_INVALID_HCI_HANDLE, status);
   1123             break;
   1124 
   1125           case HCI_SET_CONN_ENCRYPTION:
   1126             /* Device refused to start encryption.  That should be treated as
   1127              * encryption failure. */
   1128             btm_sec_encrypt_change(BTM_INVALID_HCI_HANDLE, status, false);
   1129             break;
   1130 
   1131           case HCI_BLE_CREATE_LL_CONN:
   1132             btm_ble_create_ll_conn_complete(status);
   1133             break;
   1134 
   1135 #if (BTM_SCO_INCLUDED == TRUE)
   1136           case HCI_SETUP_ESCO_CONNECTION:
   1137           case HCI_ENH_SETUP_ESCO_CONNECTION:
   1138             /* read handle out of stored command */
   1139             if (p_cmd != NULL) {
   1140               p_cmd++;
   1141               STREAM_TO_UINT16(handle, p_cmd);
   1142 
   1143               /* Determine if initial connection failed or is a change
   1144                * of setup */
   1145               if (btm_is_sco_active(handle))
   1146                 btm_esco_proc_conn_chg(status, handle, 0, 0, 0, 0);
   1147               else
   1148                 btm_sco_connected(status, NULL, handle, &esco_data);
   1149             }
   1150             break;
   1151 #endif
   1152 
   1153           /* This is commented out until an upper layer cares about returning
   1154           event
   1155           #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
   1156                       case HCI_ENHANCED_FLUSH:
   1157                           break;
   1158           #endif
   1159           */
   1160           default:
   1161             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
   1162               btm_vsc_complete(&status, opcode, 1,
   1163                                (tBTM_CMPL_CB*)p_vsc_status_cback);
   1164             break;
   1165         }
   1166 
   1167       } else {
   1168         if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
   1169           btm_vsc_complete(&status, opcode, 1,
   1170                            (tBTM_CMPL_CB*)p_vsc_status_cback);
   1171       }
   1172   }
   1173 }
   1174 
   1175 /*******************************************************************************
   1176  *
   1177  * Function         btu_hcif_command_status_evt
   1178  *
   1179  * Description      Process event HCI_COMMAND_STATUS_EVT
   1180  *
   1181  * Returns          void
   1182  *
   1183  ******************************************************************************/
   1184 static void btu_hcif_command_status_evt_on_task(uint8_t status, BT_HDR* event,
   1185                                                 void* context) {
   1186   command_opcode_t opcode;
   1187   uint8_t* stream = event->data + event->offset;
   1188   STREAM_TO_UINT16(opcode, stream);
   1189 
   1190   btu_hcif_hdl_command_status(opcode, status, stream, context);
   1191   osi_free(event);
   1192 }
   1193 
   1194 static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
   1195                                         void* context) {
   1196   do_in_hci_thread(FROM_HERE, base::Bind(btu_hcif_command_status_evt_on_task,
   1197                                          status, command, context));
   1198 }
   1199 
   1200 /*******************************************************************************
   1201  *
   1202  * Function         btu_hcif_hardware_error_evt
   1203  *
   1204  * Description      Process event HCI_HARDWARE_ERROR_EVT
   1205  *
   1206  * Returns          void
   1207  *
   1208  ******************************************************************************/
   1209 static void btu_hcif_hardware_error_evt(uint8_t* p) {
   1210   HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
   1211 
   1212   /* If anyone wants device status notifications, give him one. */
   1213   btm_report_device_status(BTM_DEV_STATUS_DOWN);
   1214 
   1215   /* Reset the controller */
   1216   if (BTM_IsDeviceUp()) BTM_DeviceReset(NULL);
   1217 }
   1218 
   1219 /*******************************************************************************
   1220  *
   1221  * Function         btu_hcif_flush_occured_evt
   1222  *
   1223  * Description      Process event HCI_FLUSH_OCCURED_EVT
   1224  *
   1225  * Returns          void
   1226  *
   1227  ******************************************************************************/
   1228 static void btu_hcif_flush_occured_evt(void) {}
   1229 
   1230 /*******************************************************************************
   1231  *
   1232  * Function         btu_hcif_role_change_evt
   1233  *
   1234  * Description      Process event HCI_ROLE_CHANGE_EVT
   1235  *
   1236  * Returns          void
   1237  *
   1238  ******************************************************************************/
   1239 static void btu_hcif_role_change_evt(uint8_t* p) {
   1240   uint8_t status;
   1241   RawAddress bda;
   1242   uint8_t role;
   1243 
   1244   STREAM_TO_UINT8(status, p);
   1245   STREAM_TO_BDADDR(bda, p);
   1246   STREAM_TO_UINT8(role, p);
   1247 
   1248   btm_blacklist_role_change_device(bda, status);
   1249   l2c_link_role_changed(&bda, role, status);
   1250   btm_acl_role_changed(status, &bda, role);
   1251 }
   1252 
   1253 /*******************************************************************************
   1254  *
   1255  * Function         btu_hcif_num_compl_data_pkts_evt
   1256  *
   1257  * Description      Process event HCI_NUM_COMPL_DATA_PKTS_EVT
   1258  *
   1259  * Returns          void
   1260  *
   1261  ******************************************************************************/
   1262 static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p) {
   1263   /* Process for L2CAP and SCO */
   1264   l2c_link_process_num_completed_pkts(p);
   1265 
   1266   /* Send on to SCO */
   1267   /*?? No SCO for now */
   1268 }
   1269 
   1270 /*******************************************************************************
   1271  *
   1272  * Function         btu_hcif_mode_change_evt
   1273  *
   1274  * Description      Process event HCI_MODE_CHANGE_EVT
   1275  *
   1276  * Returns          void
   1277  *
   1278  ******************************************************************************/
   1279 static void btu_hcif_mode_change_evt(uint8_t* p) {
   1280   uint8_t status;
   1281   uint16_t handle;
   1282   uint8_t current_mode;
   1283   uint16_t interval;
   1284 
   1285   STREAM_TO_UINT8(status, p);
   1286 
   1287   STREAM_TO_UINT16(handle, p);
   1288   STREAM_TO_UINT8(current_mode, p);
   1289   STREAM_TO_UINT16(interval, p);
   1290 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
   1291   btm_sco_chk_pend_unpark(status, handle);
   1292 #endif
   1293   btm_pm_proc_mode_change(status, handle, current_mode, interval);
   1294 
   1295 #if (HID_DEV_INCLUDED == TRUE && HID_DEV_PM_INCLUDED == TRUE)
   1296   hidd_pm_proc_mode_change(status, current_mode, interval);
   1297 #endif
   1298 }
   1299 
   1300 /*******************************************************************************
   1301  *
   1302  * Function         btu_hcif_ssr_evt
   1303  *
   1304  * Description      Process event HCI_SNIFF_SUB_RATE_EVT
   1305  *
   1306  * Returns          void
   1307  *
   1308  ******************************************************************************/
   1309 #if (BTM_SSR_INCLUDED == TRUE)
   1310 static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len) {
   1311   btm_pm_proc_ssr_evt(p, evt_len);
   1312 }
   1313 #endif
   1314 
   1315 /*******************************************************************************
   1316  *
   1317  * Function         btu_hcif_pin_code_request_evt
   1318  *
   1319  * Description      Process event HCI_PIN_CODE_REQUEST_EVT
   1320  *
   1321  * Returns          void
   1322  *
   1323  ******************************************************************************/
   1324 static void btu_hcif_pin_code_request_evt(uint8_t* p) {
   1325   RawAddress bda;
   1326 
   1327   STREAM_TO_BDADDR(bda, p);
   1328 
   1329   /* Tell L2CAP that there was a PIN code request,  */
   1330   /* it may need to stretch timeouts                */
   1331   l2c_pin_code_request(bda);
   1332 
   1333   btm_sec_pin_code_request(bda);
   1334 }
   1335 
   1336 /*******************************************************************************
   1337  *
   1338  * Function         btu_hcif_link_key_request_evt
   1339  *
   1340  * Description      Process event HCI_LINK_KEY_REQUEST_EVT
   1341  *
   1342  * Returns          void
   1343  *
   1344  ******************************************************************************/
   1345 static void btu_hcif_link_key_request_evt(uint8_t* p) {
   1346   RawAddress bda;
   1347 
   1348   STREAM_TO_BDADDR(bda, p);
   1349   btm_sec_link_key_request(bda);
   1350 }
   1351 
   1352 /*******************************************************************************
   1353  *
   1354  * Function         btu_hcif_link_key_notification_evt
   1355  *
   1356  * Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
   1357  *
   1358  * Returns          void
   1359  *
   1360  ******************************************************************************/
   1361 static void btu_hcif_link_key_notification_evt(uint8_t* p) {
   1362   RawAddress bda;
   1363   LINK_KEY key;
   1364   uint8_t key_type;
   1365 
   1366   STREAM_TO_BDADDR(bda, p);
   1367   STREAM_TO_ARRAY16(key, p);
   1368   STREAM_TO_UINT8(key_type, p);
   1369 
   1370   btm_sec_link_key_notification(bda, key, key_type);
   1371 }
   1372 
   1373 /*******************************************************************************
   1374  *
   1375  * Function         btu_hcif_loopback_command_evt
   1376  *
   1377  * Description      Process event HCI_LOOPBACK_COMMAND_EVT
   1378  *
   1379  * Returns          void
   1380  *
   1381  ******************************************************************************/
   1382 static void btu_hcif_loopback_command_evt(void) {}
   1383 
   1384 /*******************************************************************************
   1385  *
   1386  * Function         btu_hcif_data_buf_overflow_evt
   1387  *
   1388  * Description      Process event HCI_DATA_BUF_OVERFLOW_EVT
   1389  *
   1390  * Returns          void
   1391  *
   1392  ******************************************************************************/
   1393 static void btu_hcif_data_buf_overflow_evt(void) {}
   1394 
   1395 /*******************************************************************************
   1396  *
   1397  * Function         btu_hcif_max_slots_changed_evt
   1398  *
   1399  * Description      Process event HCI_MAX_SLOTS_CHANGED_EVT
   1400  *
   1401  * Returns          void
   1402  *
   1403  ******************************************************************************/
   1404 static void btu_hcif_max_slots_changed_evt(void) {}
   1405 
   1406 /*******************************************************************************
   1407  *
   1408  * Function         btu_hcif_read_clock_off_comp_evt
   1409  *
   1410  * Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
   1411  *
   1412  * Returns          void
   1413  *
   1414  ******************************************************************************/
   1415 static void btu_hcif_read_clock_off_comp_evt(uint8_t* p) {
   1416   uint8_t status;
   1417   uint16_t handle;
   1418   uint16_t clock_offset;
   1419 
   1420   STREAM_TO_UINT8(status, p);
   1421 
   1422   /* If failed to get clock offset just drop the result */
   1423   if (status != HCI_SUCCESS) return;
   1424 
   1425   STREAM_TO_UINT16(handle, p);
   1426   STREAM_TO_UINT16(clock_offset, p);
   1427 
   1428   handle = HCID_GET_HANDLE(handle);
   1429 
   1430   btm_process_clk_off_comp_evt(handle, clock_offset);
   1431   btm_sec_update_clock_offset(handle, clock_offset);
   1432 }
   1433 
   1434 /*******************************************************************************
   1435  *
   1436  * Function         btu_hcif_conn_pkt_type_change_evt
   1437  *
   1438  * Description      Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
   1439  *
   1440  * Returns          void
   1441  *
   1442  ******************************************************************************/
   1443 static void btu_hcif_conn_pkt_type_change_evt(void) {}
   1444 
   1445 /*******************************************************************************
   1446  *
   1447  * Function         btu_hcif_qos_violation_evt
   1448  *
   1449  * Description      Process event HCI_QOS_VIOLATION_EVT
   1450  *
   1451  * Returns          void
   1452  *
   1453  ******************************************************************************/
   1454 static void btu_hcif_qos_violation_evt(uint8_t* p) {
   1455   uint16_t handle;
   1456 
   1457   STREAM_TO_UINT16(handle, p);
   1458 
   1459   handle = HCID_GET_HANDLE(handle);
   1460 
   1461   l2c_link_hci_qos_violation(handle);
   1462 }
   1463 
   1464 /*******************************************************************************
   1465  *
   1466  * Function         btu_hcif_page_scan_mode_change_evt
   1467  *
   1468  * Description      Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
   1469  *
   1470  * Returns          void
   1471  *
   1472  ******************************************************************************/
   1473 static void btu_hcif_page_scan_mode_change_evt(void) {}
   1474 
   1475 /*******************************************************************************
   1476  *
   1477  * Function         btu_hcif_page_scan_rep_mode_chng_evt
   1478  *
   1479  * Description      Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
   1480  *
   1481  * Returns          void
   1482  *
   1483  ******************************************************************************/
   1484 static void btu_hcif_page_scan_rep_mode_chng_evt(void) {}
   1485 
   1486 /**********************************************
   1487  * Simple Pairing Events
   1488  **********************************************/
   1489 
   1490 /*******************************************************************************
   1491  *
   1492  * Function         btu_hcif_host_support_evt
   1493  *
   1494  * Description      Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
   1495  *
   1496  * Returns          void
   1497  *
   1498  ******************************************************************************/
   1499 static void btu_hcif_host_support_evt(uint8_t* p) {
   1500   btm_sec_rmt_host_support_feat_evt(p);
   1501 }
   1502 
   1503 /*******************************************************************************
   1504  *
   1505  * Function         btu_hcif_io_cap_request_evt
   1506  *
   1507  * Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
   1508  *
   1509  * Returns          void
   1510  *
   1511  ******************************************************************************/
   1512 static void btu_hcif_io_cap_request_evt(uint8_t* p) {
   1513   RawAddress bda;
   1514   STREAM_TO_BDADDR(bda, p);
   1515   btm_io_capabilities_req(bda);
   1516 }
   1517 
   1518 /*******************************************************************************
   1519  *
   1520  * Function         btu_hcif_io_cap_response_evt
   1521  *
   1522  * Description      Process event HCI_IO_CAPABILITY_RESPONSE_EVT
   1523  *
   1524  * Returns          void
   1525  *
   1526  ******************************************************************************/
   1527 static void btu_hcif_io_cap_response_evt(uint8_t* p) {
   1528   btm_io_capabilities_rsp(p);
   1529 }
   1530 
   1531 /*******************************************************************************
   1532  *
   1533  * Function         btu_hcif_user_conf_request_evt
   1534  *
   1535  * Description      Process event HCI_USER_CONFIRMATION_REQUEST_EVT
   1536  *
   1537  * Returns          void
   1538  *
   1539  ******************************************************************************/
   1540 static void btu_hcif_user_conf_request_evt(uint8_t* p) {
   1541   btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
   1542 }
   1543 
   1544 /*******************************************************************************
   1545  *
   1546  * Function         btu_hcif_user_passkey_request_evt
   1547  *
   1548  * Description      Process event HCI_USER_PASSKEY_REQUEST_EVT
   1549  *
   1550  * Returns          void
   1551  *
   1552  ******************************************************************************/
   1553 static void btu_hcif_user_passkey_request_evt(uint8_t* p) {
   1554   btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
   1555 }
   1556 
   1557 /*******************************************************************************
   1558  *
   1559  * Function         btu_hcif_user_passkey_notif_evt
   1560  *
   1561  * Description      Process event HCI_USER_PASSKEY_NOTIFY_EVT
   1562  *
   1563  * Returns          void
   1564  *
   1565  ******************************************************************************/
   1566 static void btu_hcif_user_passkey_notif_evt(uint8_t* p) {
   1567   btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
   1568 }
   1569 
   1570 /*******************************************************************************
   1571  *
   1572  * Function         btu_hcif_keypress_notif_evt
   1573  *
   1574  * Description      Process event HCI_KEYPRESS_NOTIFY_EVT
   1575  *
   1576  * Returns          void
   1577  *
   1578  ******************************************************************************/
   1579 static void btu_hcif_keypress_notif_evt(uint8_t* p) {
   1580   btm_keypress_notif_evt(p);
   1581 }
   1582 
   1583 /*******************************************************************************
   1584  *
   1585  * Function         btu_hcif_rem_oob_request_evt
   1586  *
   1587  * Description      Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
   1588  *
   1589  * Returns          void
   1590  *
   1591  ******************************************************************************/
   1592 static void btu_hcif_rem_oob_request_evt(uint8_t* p) { btm_rem_oob_req(p); }
   1593 
   1594 /*******************************************************************************
   1595  *
   1596  * Function         btu_hcif_simple_pair_complete_evt
   1597  *
   1598  * Description      Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
   1599  *
   1600  * Returns          void
   1601  *
   1602  ******************************************************************************/
   1603 static void btu_hcif_simple_pair_complete_evt(uint8_t* p) {
   1604   btm_simple_pair_complete(p);
   1605 }
   1606 
   1607 /*******************************************************************************
   1608  *
   1609  * Function         btu_hcif_enhanced_flush_complete_evt
   1610  *
   1611  * Description      Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
   1612  *
   1613  * Returns          void
   1614  *
   1615  ******************************************************************************/
   1616 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
   1617 static void btu_hcif_enhanced_flush_complete_evt(void) {
   1618   /* This is empty until an upper layer cares about returning event */
   1619 }
   1620 #endif
   1621 /**********************************************
   1622  * End of Simple Pairing Events
   1623  **********************************************/
   1624 
   1625 /**********************************************
   1626  * BLE Events
   1627  **********************************************/
   1628 static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p) {
   1629   uint8_t status;
   1630   uint8_t enc_enable = 0;
   1631   uint16_t handle;
   1632 
   1633   STREAM_TO_UINT8(status, p);
   1634   STREAM_TO_UINT16(handle, p);
   1635 
   1636   if (status == HCI_SUCCESS) enc_enable = 1;
   1637 
   1638   btm_sec_encrypt_change(handle, status, enc_enable);
   1639 }
   1640 
   1641 static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len) {
   1642   btm_ble_conn_complete(p, evt_len, false);
   1643 }
   1644 #if (BLE_PRIVACY_SPT == TRUE)
   1645 static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len) {
   1646   btm_ble_conn_complete(p, evt_len, true);
   1647 }
   1648 #endif
   1649 
   1650 extern void gatt_notify_conn_update(uint16_t handle, uint16_t interval,
   1651                                     uint16_t latency, uint16_t timeout,
   1652                                     uint8_t status);
   1653 
   1654 static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len) {
   1655   /* LE connection update has completed successfully as a master. */
   1656   /* We can enable the update request if the result is a success. */
   1657   /* extract the HCI handle first */
   1658   uint8_t status;
   1659   uint16_t handle;
   1660   uint16_t interval;
   1661   uint16_t latency;
   1662   uint16_t timeout;
   1663 
   1664   STREAM_TO_UINT8(status, p);
   1665   STREAM_TO_UINT16(handle, p);
   1666   STREAM_TO_UINT16(interval, p);
   1667   STREAM_TO_UINT16(latency, p);
   1668   STREAM_TO_UINT16(timeout, p);
   1669 
   1670   l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
   1671 
   1672   gatt_notify_conn_update(handle & 0x0FFF, interval, latency, timeout, status);
   1673 }
   1674 
   1675 static void btu_ble_read_remote_feat_evt(uint8_t* p) {
   1676   btm_ble_read_remote_features_complete(p);
   1677 }
   1678 
   1679 static void btu_ble_proc_ltk_req(uint8_t* p) {
   1680   uint16_t ediv, handle;
   1681   uint8_t* pp;
   1682 
   1683   STREAM_TO_UINT16(handle, p);
   1684   pp = p + 8;
   1685   STREAM_TO_UINT16(ediv, pp);
   1686   btm_ble_ltk_request(handle, p, ediv);
   1687   /* This is empty until an upper layer cares about returning event */
   1688 }
   1689 
   1690 static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len) {
   1691   uint16_t handle;
   1692   uint16_t tx_data_len;
   1693   uint16_t rx_data_len;
   1694 
   1695   if (!controller_get_interface()->supports_ble_packet_extension()) {
   1696     HCI_TRACE_WARNING("%s, request not supported", __func__);
   1697     return;
   1698   }
   1699 
   1700   STREAM_TO_UINT16(handle, p);
   1701   STREAM_TO_UINT16(tx_data_len, p);
   1702   p += 2; /* Skip the TxTimer */
   1703   STREAM_TO_UINT16(rx_data_len, p);
   1704 
   1705   l2cble_process_data_length_change_event(handle, tx_data_len, rx_data_len);
   1706 }
   1707 
   1708 /**********************************************
   1709  * End of BLE Events Handler
   1710  **********************************************/
   1711 #if (BLE_LLT_INCLUDED == TRUE)
   1712 static void btu_ble_rc_param_req_evt(uint8_t* p) {
   1713   uint16_t handle;
   1714   uint16_t int_min, int_max, latency, timeout;
   1715 
   1716   STREAM_TO_UINT16(handle, p);
   1717   STREAM_TO_UINT16(int_min, p);
   1718   STREAM_TO_UINT16(int_max, p);
   1719   STREAM_TO_UINT16(latency, p);
   1720   STREAM_TO_UINT16(timeout, p);
   1721 
   1722   l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency,
   1723                                       timeout);
   1724 }
   1725 #endif /* BLE_LLT_INCLUDED */
   1726