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 #include <stdlib.h>
     29 #include <string.h>
     30 #include <stdio.h>
     31 
     32 #include "gki.h"
     33 #include "bt_types.h"
     34 #include "hcimsgs.h"
     35 #include "btu.h"
     36 #include "l2c_int.h"
     37 #include "btm_api.h"
     38 #include "btm_int.h"
     39 
     40 extern void btm_process_cancel_complete(UINT8 status, UINT8 mode);
     41 extern void btm_ble_test_command_complete(UINT8 *p);
     42 
     43 // btla-specific ++
     44 #define LOG_TAG "BTLD"
     45 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE) && (!defined(LINUX_NATIVE)) )
     46 #include <cutils/log.h>
     47 #else
     48 #define LOGV(format, ...)  fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__)
     49 #define LOGE(format, ...)  fprintf (stderr, LOG_TAG format"\n", ## __VA_ARGS__)
     50 #define LOGI(format, ...)  fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__)
     51 #endif
     52 
     53 // btla-specific ++
     54 /* BTE application task */
     55 #if APPL_INCLUDED == TRUE
     56 #include "bte_appl.h"
     57 #endif
     58 // btla-specific --
     59 
     60 //Counter to track number of HCI command timeout
     61 static int num_hci_cmds_timed_out;
     62 
     63 /********************************************************************************/
     64 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
     65 /********************************************************************************/
     66 static void btu_hcif_inquiry_comp_evt (UINT8 *p);
     67 static void btu_hcif_inquiry_result_evt (UINT8 *p);
     68 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p);
     69 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
     70 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p);
     71 #endif
     72 
     73 static void btu_hcif_connection_comp_evt (UINT8 *p);
     74 static void btu_hcif_connection_request_evt (UINT8 *p);
     75 static void btu_hcif_disconnection_comp_evt (UINT8 *p);
     76 static void btu_hcif_authentication_comp_evt (UINT8 *p);
     77 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len);
     78 static void btu_hcif_encryption_change_evt (UINT8 *p);
     79 static void btu_hcif_change_conn_link_key_evt (UINT8 *p);
     80 static void btu_hcif_master_link_key_comp_evt (UINT8 *p);
     81 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p);
     82 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p);
     83 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p);
     84 static void btu_hcif_qos_setup_comp_evt (UINT8 *p);
     85 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len);
     86 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p);
     87 static void btu_hcif_hardware_error_evt (UINT8 *p);
     88 static void btu_hcif_flush_occured_evt (void);
     89 static void btu_hcif_role_change_evt (UINT8 *p);
     90 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p);
     91 static void btu_hcif_mode_change_evt (UINT8 *p);
     92 static void btu_hcif_return_link_keys_evt (UINT8 *p);
     93 static void btu_hcif_pin_code_request_evt (UINT8 *p);
     94 static void btu_hcif_link_key_request_evt (UINT8 *p);
     95 static void btu_hcif_link_key_notification_evt (UINT8 *p);
     96 static void btu_hcif_loopback_command_evt (void);
     97 static void btu_hcif_data_buf_overflow_evt (void);
     98 static void btu_hcif_max_slots_changed_evt (void);
     99 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p);
    100 static void btu_hcif_conn_pkt_type_change_evt (void);
    101 static void btu_hcif_qos_violation_evt (UINT8 *p);
    102 static void btu_hcif_page_scan_mode_change_evt (void);
    103 static void btu_hcif_page_scan_rep_mode_chng_evt (void);
    104 static void btu_hcif_esco_connection_comp_evt(UINT8 *p);
    105 static void btu_hcif_esco_connection_chg_evt(UINT8 *p);
    106 
    107 /* Simple Pairing Events */
    108 static void btu_hcif_host_support_evt (UINT8 *p);
    109 static void btu_hcif_io_cap_request_evt (UINT8 *p);
    110 static void btu_hcif_io_cap_response_evt (UINT8 *p);
    111 static void btu_hcif_user_conf_request_evt (UINT8 *p);
    112 static void btu_hcif_user_passkey_request_evt (UINT8 *p);
    113 static void btu_hcif_user_passkey_notif_evt (UINT8 *p);
    114 static void btu_hcif_keypress_notif_evt (UINT8 *p);
    115 static void btu_hcif_link_super_tout_evt (UINT8 *p);
    116 
    117     #if BTM_OOB_INCLUDED == TRUE
    118 static void btu_hcif_rem_oob_request_evt (UINT8 *p);
    119     #endif
    120 
    121 static void btu_hcif_simple_pair_complete_evt (UINT8 *p);
    122     #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
    123 static void btu_hcif_enhanced_flush_complete_evt (void);
    124     #endif
    125 
    126     #if (BTM_SSR_INCLUDED == TRUE)
    127 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len);
    128     #endif /* BTM_SSR_INCLUDED == TRUE */
    129 
    130     #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
    131 extern void hidd_pm_proc_mode_change( UINT8 hci_status, UINT8 mode, UINT16 interval );
    132     #endif
    133 
    134 
    135     #if BLE_INCLUDED == TRUE
    136 static void btu_ble_ll_conn_complete_evt (UINT8 *p, UINT16 evt_len);
    137 static void btu_ble_process_adv_pkt (UINT8 *p);
    138 static void btu_ble_read_remote_feat_evt (UINT8 *p);
    139 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p, UINT16 evt_len);
    140 static void btu_ble_proc_ltk_req (UINT8 *p);
    141 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p);
    142 #if (BLE_LLT_INCLUDED == TRUE)
    143 static void btu_ble_rc_param_req_evt(UINT8 *p);
    144 #endif
    145     #endif
    146 /*******************************************************************************
    147 **
    148 ** Function         btu_hcif_store_cmd
    149 **
    150 ** Description      This function stores a copy of an outgoing command and
    151 **                  and sets a timer waiting for a event in response to the
    152 **                  command.
    153 **
    154 ** Returns          void
    155 **
    156 *******************************************************************************/
    157 static void btu_hcif_store_cmd (UINT8 controller_id, BT_HDR *p_buf)
    158 {
    159     tHCI_CMD_CB *p_hci_cmd_cb;
    160     UINT16  opcode;
    161     BT_HDR  *p_cmd;
    162     UINT8   *p;
    163 
    164     /* Validate controller ID */
    165     if (controller_id >= BTU_MAX_LOCAL_CTRLS)
    166         return;
    167 
    168     p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
    169     p = (UINT8 *)(p_buf + 1) + p_buf->offset;
    170 
    171     /* get command opcode */
    172     STREAM_TO_UINT16 (opcode, p);
    173 
    174     /* don't do anything for certain commands */
    175     if ((opcode == HCI_RESET) || (opcode == HCI_HOST_NUM_PACKETS_DONE))
    176     {
    177         return;
    178     }
    179 
    180     /* allocate buffer (HCI_GET_CMD_BUF will either get a buffer from HCI_CMD_POOL or from 'best-fit' pool) */
    181     if ((p_cmd = HCI_GET_CMD_BUF(p_buf->len + p_buf->offset - HCIC_PREAMBLE_SIZE)) == NULL)
    182     {
    183         return;
    184     }
    185 
    186     /* copy buffer */
    187     memcpy (p_cmd, p_buf, sizeof(BT_HDR));
    188 
    189     /* If vendor specific save the callback function */
    190     if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
    191 #if BLE_INCLUDED == TRUE
    192         || (opcode == HCI_BLE_RAND )
    193         || (opcode == HCI_BLE_ENCRYPT)
    194 #endif
    195        )
    196     {
    197         memcpy ((UINT8 *)(p_cmd + 1), (UINT8 *)(p_buf + 1), sizeof(void *));
    198     }
    199 
    200     memcpy ((UINT8 *)(p_cmd + 1) + p_cmd->offset,
    201             (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
    202 
    203     /* queue copy of cmd */
    204     GKI_enqueue(&(p_hci_cmd_cb->cmd_cmpl_q), p_cmd);
    205 
    206     /* start timer */
    207     if (BTU_CMD_CMPL_TIMEOUT > 0)
    208     {
    209 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
    210         p_hci_cmd_cb->checked_hcisu = FALSE;
    211 #endif
    212         btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
    213                          (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
    214                          BTU_CMD_CMPL_TIMEOUT);
    215     }
    216 }
    217 
    218 /*******************************************************************************
    219 **
    220 ** Function         btu_hcif_process_event
    221 **
    222 ** Description      This function is called when an event is received from
    223 **                  the Host Controller.
    224 **
    225 ** Returns          void
    226 **
    227 *******************************************************************************/
    228 void btu_hcif_process_event (UINT8 controller_id, BT_HDR *p_msg)
    229 {
    230     UINT8   *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
    231     UINT8   hci_evt_code, hci_evt_len;
    232 #if BLE_INCLUDED == TRUE
    233     UINT8   ble_sub_code;
    234 #endif
    235     STREAM_TO_UINT8  (hci_evt_code, p);
    236     STREAM_TO_UINT8  (hci_evt_len, p);
    237 
    238     switch (hci_evt_code)
    239     {
    240         case HCI_INQUIRY_COMP_EVT:
    241             btu_hcif_inquiry_comp_evt (p);
    242             break;
    243         case HCI_INQUIRY_RESULT_EVT:
    244             btu_hcif_inquiry_result_evt (p);
    245             break;
    246         case HCI_INQUIRY_RSSI_RESULT_EVT:
    247             btu_hcif_inquiry_rssi_result_evt (p);
    248             break;
    249 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
    250         case HCI_EXTENDED_INQUIRY_RESULT_EVT:
    251             btu_hcif_extended_inquiry_result_evt (p);
    252             break;
    253 #endif
    254         case HCI_CONNECTION_COMP_EVT:
    255             btu_hcif_connection_comp_evt (p);
    256             break;
    257         case HCI_CONNECTION_REQUEST_EVT:
    258             btu_hcif_connection_request_evt (p);
    259             break;
    260         case HCI_DISCONNECTION_COMP_EVT:
    261             btu_hcif_disconnection_comp_evt (p);
    262             break;
    263         case HCI_AUTHENTICATION_COMP_EVT:
    264             btu_hcif_authentication_comp_evt (p);
    265             break;
    266         case HCI_RMT_NAME_REQUEST_COMP_EVT:
    267             btu_hcif_rmt_name_request_comp_evt (p, hci_evt_len);
    268             break;
    269         case HCI_ENCRYPTION_CHANGE_EVT:
    270             btu_hcif_encryption_change_evt (p);
    271             break;
    272 #if BLE_INCLUDED == TRUE
    273         case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
    274             btu_hcif_encryption_key_refresh_cmpl_evt(p);
    275             break;
    276 #endif
    277         case HCI_CHANGE_CONN_LINK_KEY_EVT:
    278             btu_hcif_change_conn_link_key_evt (p);
    279             break;
    280         case HCI_MASTER_LINK_KEY_COMP_EVT:
    281             btu_hcif_master_link_key_comp_evt (p);
    282             break;
    283         case HCI_READ_RMT_FEATURES_COMP_EVT:
    284             btu_hcif_read_rmt_features_comp_evt (p);
    285             break;
    286         case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
    287             btu_hcif_read_rmt_ext_features_comp_evt (p);
    288             break;
    289         case HCI_READ_RMT_VERSION_COMP_EVT:
    290             btu_hcif_read_rmt_version_comp_evt (p);
    291             break;
    292         case HCI_QOS_SETUP_COMP_EVT:
    293             btu_hcif_qos_setup_comp_evt (p);
    294             break;
    295         case HCI_COMMAND_COMPLETE_EVT:
    296             btu_hcif_command_complete_evt (controller_id, p, hci_evt_len);
    297             break;
    298         case HCI_COMMAND_STATUS_EVT:
    299             btu_hcif_command_status_evt (controller_id, p);
    300             break;
    301         case HCI_HARDWARE_ERROR_EVT:
    302             btu_hcif_hardware_error_evt (p);
    303             break;
    304         case HCI_FLUSH_OCCURED_EVT:
    305             btu_hcif_flush_occured_evt ();
    306             break;
    307         case HCI_ROLE_CHANGE_EVT:
    308             btu_hcif_role_change_evt (p);
    309             break;
    310         case HCI_NUM_COMPL_DATA_PKTS_EVT:
    311             btu_hcif_num_compl_data_pkts_evt (p);
    312             break;
    313         case HCI_MODE_CHANGE_EVT:
    314             btu_hcif_mode_change_evt (p);
    315             break;
    316         case HCI_RETURN_LINK_KEYS_EVT:
    317             btu_hcif_return_link_keys_evt (p);
    318             break;
    319         case HCI_PIN_CODE_REQUEST_EVT:
    320             btu_hcif_pin_code_request_evt (p);
    321             break;
    322         case HCI_LINK_KEY_REQUEST_EVT:
    323             btu_hcif_link_key_request_evt (p);
    324             break;
    325         case HCI_LINK_KEY_NOTIFICATION_EVT:
    326             btu_hcif_link_key_notification_evt (p);
    327             break;
    328         case HCI_LOOPBACK_COMMAND_EVT:
    329             btu_hcif_loopback_command_evt ();
    330             break;
    331         case HCI_DATA_BUF_OVERFLOW_EVT:
    332             btu_hcif_data_buf_overflow_evt ();
    333             break;
    334         case HCI_MAX_SLOTS_CHANGED_EVT:
    335             btu_hcif_max_slots_changed_evt ();
    336             break;
    337         case HCI_READ_CLOCK_OFF_COMP_EVT:
    338             btu_hcif_read_clock_off_comp_evt (p);
    339             break;
    340         case HCI_CONN_PKT_TYPE_CHANGE_EVT:
    341             btu_hcif_conn_pkt_type_change_evt ();
    342             break;
    343         case HCI_QOS_VIOLATION_EVT:
    344             btu_hcif_qos_violation_evt (p);
    345             break;
    346         case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
    347             btu_hcif_page_scan_mode_change_evt ();
    348             break;
    349         case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
    350             btu_hcif_page_scan_rep_mode_chng_evt ();
    351             break;
    352         case HCI_ESCO_CONNECTION_COMP_EVT:
    353             btu_hcif_esco_connection_comp_evt (p);
    354             break;
    355         case HCI_ESCO_CONNECTION_CHANGED_EVT:
    356             btu_hcif_esco_connection_chg_evt (p);
    357             break;
    358 #if (BTM_SSR_INCLUDED == TRUE)
    359         case HCI_SNIFF_SUB_RATE_EVT:
    360             btu_hcif_ssr_evt (p, hci_evt_len);
    361             break;
    362 #endif  /* BTM_SSR_INCLUDED == TRUE */
    363         case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
    364             btu_hcif_host_support_evt (p);
    365             break;
    366         case HCI_IO_CAPABILITY_REQUEST_EVT:
    367             btu_hcif_io_cap_request_evt (p);
    368             break;
    369         case HCI_IO_CAPABILITY_RESPONSE_EVT:
    370             btu_hcif_io_cap_response_evt (p);
    371             break;
    372         case HCI_USER_CONFIRMATION_REQUEST_EVT:
    373             btu_hcif_user_conf_request_evt (p);
    374             break;
    375         case HCI_USER_PASSKEY_REQUEST_EVT:
    376             btu_hcif_user_passkey_request_evt (p);
    377             break;
    378 #if BTM_OOB_INCLUDED == TRUE
    379         case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
    380             btu_hcif_rem_oob_request_evt (p);
    381             break;
    382 #endif
    383         case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
    384             btu_hcif_simple_pair_complete_evt (p);
    385             break;
    386         case HCI_USER_PASSKEY_NOTIFY_EVT:
    387             btu_hcif_user_passkey_notif_evt (p);
    388             break;
    389         case HCI_KEYPRESS_NOTIFY_EVT:
    390             btu_hcif_keypress_notif_evt (p);
    391             break;
    392         case HCI_LINK_SUPER_TOUT_CHANGED_EVT:
    393             btu_hcif_link_super_tout_evt (p);
    394             break;
    395 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
    396         case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
    397             btu_hcif_enhanced_flush_complete_evt ();
    398             break;
    399 #endif
    400 
    401 #if (BLE_INCLUDED == TRUE)
    402         case HCI_BLE_EVENT:
    403             STREAM_TO_UINT8  (ble_sub_code, p);
    404 
    405             HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code,  ble_sub_code);
    406 
    407             switch (ble_sub_code)
    408             {
    409                 case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
    410                     btu_ble_process_adv_pkt(p);
    411                     break;
    412                 case HCI_BLE_CONN_COMPLETE_EVT:
    413                     btu_ble_ll_conn_complete_evt(p, hci_evt_len);
    414                     break;
    415                 case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
    416                     btu_ble_ll_conn_param_upd_evt(p, hci_evt_len);
    417                     break;
    418                 case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
    419                     btu_ble_read_remote_feat_evt(p);
    420                     break;
    421                 case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
    422                     btu_ble_proc_ltk_req(p);
    423                     break;
    424 #if (BLE_LLT_INCLUDED == TRUE)
    425                case HCI_BLE_RC_PARAM_REQ_EVT:
    426                     btu_ble_rc_param_req_evt(p);
    427                     break;
    428 #endif
    429 
    430             }
    431             break;
    432 #endif /* BLE_INCLUDED */
    433         case HCI_VENDOR_SPECIFIC_EVT:
    434                 btm_vendor_specific_evt (p, hci_evt_len);
    435             break;
    436     }
    437     // reset the  num_hci_cmds_timed_out upon receving any event from controller.
    438     num_hci_cmds_timed_out = 0;
    439 }
    440 
    441 
    442 /*******************************************************************************
    443 **
    444 ** Function         btu_hcif_send_cmd
    445 **
    446 ** Description      This function is called to check if it can send commands
    447 **                  to the Host Controller. It may be passed the address of
    448 **                  a packet to send.
    449 **
    450 ** Returns          void
    451 **
    452 *******************************************************************************/
    453 void btu_hcif_send_cmd (UINT8 controller_id, BT_HDR *p_buf)
    454 {
    455     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
    456 
    457 #if ((L2CAP_HOST_FLOW_CTRL == TRUE)||defined(HCI_TESTER))
    458     UINT8 *pp;
    459     UINT16 code;
    460 #endif
    461 
    462     /* If there are already commands in the queue, then enqueue this command */
    463     if ((p_buf) && (p_hci_cmd_cb->cmd_xmit_q.count))
    464     {
    465         GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
    466         p_buf = NULL;
    467     }
    468 
    469     /* Allow for startup case, where no acks may be received */
    470     if ( ((controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
    471          && (p_hci_cmd_cb->cmd_window == 0)
    472          && (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT)) )
    473     {
    474         p_hci_cmd_cb->cmd_window = p_hci_cmd_cb->cmd_xmit_q.count + 1;
    475     }
    476 
    477     /* See if we can send anything */
    478     while (p_hci_cmd_cb->cmd_window != 0)
    479     {
    480         if (!p_buf)
    481             p_buf = (BT_HDR *)GKI_dequeue (&(p_hci_cmd_cb->cmd_xmit_q));
    482 
    483         if (p_buf)
    484         {
    485             btu_hcif_store_cmd(controller_id, p_buf);
    486 
    487 #if ((L2CAP_HOST_FLOW_CTRL == TRUE)||defined(HCI_TESTER))
    488             pp = (UINT8 *)(p_buf + 1) + p_buf->offset;
    489 
    490             STREAM_TO_UINT16 (code, pp);
    491 
    492             /*
    493              * We do not need to decrease window for host flow control,
    494              * host flow control does not receive an event back from controller
    495              */
    496             if (code != HCI_HOST_NUM_PACKETS_DONE)
    497 #endif
    498                 p_hci_cmd_cb->cmd_window--;
    499 
    500             if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
    501             {
    502                 HCI_CMD_TO_LOWER(p_buf);
    503             }
    504             else
    505             {
    506                 /* Unknown controller */
    507                 HCI_TRACE_WARNING("BTU HCI(ctrl id=%d) controller ID not recognized", controller_id);
    508                 GKI_freebuf(p_buf);;
    509             }
    510 
    511             p_buf = NULL;
    512         }
    513         else
    514             break;
    515     }
    516 
    517     if (p_buf)
    518         GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
    519 
    520 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
    521     if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
    522     {
    523         /* check if controller can go to sleep */
    524         btu_check_bt_sleep ();
    525     }
    526 #endif
    527 
    528 }
    529 
    530 
    531 /*******************************************************************************
    532 **
    533 ** Function         btu_hcif_send_host_rdy_for_data
    534 **
    535 ** Description      This function is called to check if it can send commands
    536 **                  to the Host Controller. It may be passed the address of
    537 **                  a packet to send.
    538 **
    539 ** Returns          void
    540 **
    541 *******************************************************************************/
    542 void btu_hcif_send_host_rdy_for_data(void)
    543 {
    544     UINT16      num_pkts[MAX_L2CAP_LINKS + 4];      /* 3 SCO connections */
    545     UINT16      handles[MAX_L2CAP_LINKS + 4];
    546     UINT8       num_ents;
    547 
    548     /* Get the L2CAP numbers */
    549     num_ents = l2c_link_pkts_rcvd (num_pkts, handles);
    550 
    551     /* Get the SCO numbers */
    552     /* No SCO for now ?? */
    553 
    554     if (num_ents)
    555     {
    556         btsnd_hcic_host_num_xmitted_pkts (num_ents, handles, num_pkts);
    557     }
    558 }
    559 
    560 /*******************************************************************************
    561 **
    562 ** Function         btu_hcif_inquiry_comp_evt
    563 **
    564 ** Description      Process event HCI_INQUIRY_COMP_EVT
    565 **
    566 ** Returns          void
    567 **
    568 *******************************************************************************/
    569 static void btu_hcif_inquiry_comp_evt (UINT8 *p)
    570 {
    571     UINT8   status;
    572 
    573     STREAM_TO_UINT8    (status, p);
    574 
    575     /* Tell inquiry processing that we are done */
    576     btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
    577 }
    578 
    579 
    580 /*******************************************************************************
    581 **
    582 ** Function         btu_hcif_inquiry_result_evt
    583 **
    584 ** Description      Process event HCI_INQUIRY_RESULT_EVT
    585 **
    586 ** Returns          void
    587 **
    588 *******************************************************************************/
    589 static void btu_hcif_inquiry_result_evt (UINT8 *p)
    590 {
    591     /* Store results in the cache */
    592     btm_process_inq_results (p, BTM_INQ_RESULT_STANDARD);
    593 }
    594 
    595 /*******************************************************************************
    596 **
    597 ** Function         btu_hcif_inquiry_rssi_result_evt
    598 **
    599 ** Description      Process event HCI_INQUIRY_RSSI_RESULT_EVT
    600 **
    601 ** Returns          void
    602 **
    603 *******************************************************************************/
    604 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p)
    605 {
    606     /* Store results in the cache */
    607     btm_process_inq_results (p, BTM_INQ_RESULT_WITH_RSSI);
    608 }
    609 
    610 /*******************************************************************************
    611 **
    612 ** Function         btu_hcif_extended_inquiry_result_evt
    613 **
    614 ** Description      Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
    615 **
    616 ** Returns          void
    617 **
    618 *******************************************************************************/
    619 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
    620 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p)
    621 {
    622     /* Store results in the cache */
    623     btm_process_inq_results (p, BTM_INQ_RESULT_EXTENDED);
    624 }
    625 #endif
    626 
    627 /*******************************************************************************
    628 **
    629 ** Function         btu_hcif_connection_comp_evt
    630 **
    631 ** Description      Process event HCI_CONNECTION_COMP_EVT
    632 **
    633 ** Returns          void
    634 **
    635 *******************************************************************************/
    636 static void btu_hcif_connection_comp_evt (UINT8 *p)
    637 {
    638     UINT8       status;
    639     UINT16      handle;
    640     BD_ADDR     bda;
    641     UINT8       link_type;
    642     UINT8       enc_mode;
    643 #if BTM_SCO_INCLUDED == TRUE
    644     tBTM_ESCO_DATA  esco_data;
    645 #endif
    646 
    647     STREAM_TO_UINT8    (status, p);
    648     STREAM_TO_UINT16   (handle, p);
    649     STREAM_TO_BDADDR   (bda, p);
    650     STREAM_TO_UINT8    (link_type, p);
    651     STREAM_TO_UINT8    (enc_mode, p);
    652 
    653     handle = HCID_GET_HANDLE (handle);
    654 
    655     if (link_type == HCI_LINK_TYPE_ACL)
    656     {
    657         btm_sec_connected (bda, handle, status, enc_mode);
    658 
    659         l2c_link_hci_conn_comp (status, handle, bda);
    660     }
    661 #if BTM_SCO_INCLUDED == TRUE
    662     else
    663     {
    664         memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
    665         /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
    666         memcpy (esco_data.bd_addr, bda, BD_ADDR_LEN);
    667         btm_sco_connected (status, bda, handle, &esco_data);
    668     }
    669 #endif /* BTM_SCO_INCLUDED */
    670 }
    671 
    672 
    673 /*******************************************************************************
    674 **
    675 ** Function         btu_hcif_connection_request_evt
    676 **
    677 ** Description      Process event HCI_CONNECTION_REQUEST_EVT
    678 **
    679 ** Returns          void
    680 **
    681 *******************************************************************************/
    682 static void btu_hcif_connection_request_evt (UINT8 *p)
    683 {
    684     BD_ADDR     bda;
    685     DEV_CLASS   dc;
    686     UINT8       link_type;
    687 
    688     STREAM_TO_BDADDR   (bda, p);
    689     STREAM_TO_DEVCLASS (dc, p);
    690     STREAM_TO_UINT8    (link_type, p);
    691 
    692     /* Pass request to security manager to check connect filters before */
    693     /* passing request to l2cap */
    694     if (link_type == HCI_LINK_TYPE_ACL)
    695     {
    696         btm_sec_conn_req (bda, dc);
    697     }
    698 #if BTM_SCO_INCLUDED == TRUE
    699     else
    700     {
    701         btm_sco_conn_req (bda, dc, link_type);
    702     }
    703 #endif /* BTM_SCO_INCLUDED */
    704 }
    705 
    706 
    707 /*******************************************************************************
    708 **
    709 ** Function         btu_hcif_disconnection_comp_evt
    710 **
    711 ** Description      Process event HCI_DISCONNECTION_COMP_EVT
    712 **
    713 ** Returns          void
    714 **
    715 *******************************************************************************/
    716 static void btu_hcif_disconnection_comp_evt (UINT8 *p)
    717 {
    718     UINT8   status;
    719     UINT16  handle;
    720     UINT8   reason;
    721 
    722     STREAM_TO_UINT8  (status, p);
    723     STREAM_TO_UINT16 (handle, p);
    724     STREAM_TO_UINT8  (reason, p);
    725 
    726     handle = HCID_GET_HANDLE (handle);
    727 
    728 #if BTM_SCO_INCLUDED == TRUE
    729     /* If L2CAP doesn't know about it, send it to SCO */
    730     if (!l2c_link_hci_disc_comp (handle, reason))
    731         btm_sco_removed (handle, reason);
    732 #else
    733     l2c_link_hci_disc_comp (handle, reason);
    734 #endif /* BTM_SCO_INCLUDED */
    735 
    736     /* Notify security manager */
    737     btm_sec_disconnected (handle, reason);
    738 }
    739 
    740 /*******************************************************************************
    741 **
    742 ** Function         btu_hcif_authentication_comp_evt
    743 **
    744 ** Description      Process event HCI_AUTHENTICATION_COMP_EVT
    745 **
    746 ** Returns          void
    747 **
    748 *******************************************************************************/
    749 static void btu_hcif_authentication_comp_evt (UINT8 *p)
    750 {
    751     UINT8   status;
    752     UINT16  handle;
    753 
    754     STREAM_TO_UINT8  (status, p);
    755     STREAM_TO_UINT16 (handle, p);
    756 
    757     btm_sec_auth_complete (handle, status);
    758 }
    759 
    760 
    761 /*******************************************************************************
    762 **
    763 ** Function         btu_hcif_rmt_name_request_comp_evt
    764 **
    765 ** Description      Process event HCI_RMT_NAME_REQUEST_COMP_EVT
    766 **
    767 ** Returns          void
    768 **
    769 *******************************************************************************/
    770 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len)
    771 {
    772     UINT8   status;
    773     BD_ADDR bd_addr;
    774 
    775     STREAM_TO_UINT8 (status, p);
    776     STREAM_TO_BDADDR (bd_addr, p);
    777 
    778     evt_len -= (1 + BD_ADDR_LEN);
    779 
    780     btm_process_remote_name (bd_addr, p, evt_len, status);
    781 
    782     btm_sec_rmt_name_request_complete (bd_addr, p, status);
    783 }
    784 
    785 
    786 /*******************************************************************************
    787 **
    788 ** Function         btu_hcif_encryption_change_evt
    789 **
    790 ** Description      Process event HCI_ENCRYPTION_CHANGE_EVT
    791 **
    792 ** Returns          void
    793 **
    794 *******************************************************************************/
    795 static void btu_hcif_encryption_change_evt (UINT8 *p)
    796 {
    797     UINT8   status;
    798     UINT16  handle;
    799     UINT8   encr_enable;
    800 
    801     STREAM_TO_UINT8  (status, p);
    802     STREAM_TO_UINT16 (handle, p);
    803     STREAM_TO_UINT8  (encr_enable, p);
    804 
    805     btm_acl_encrypt_change (handle, status, encr_enable);
    806     btm_sec_encrypt_change (handle, status, encr_enable);
    807 }
    808 
    809 
    810 /*******************************************************************************
    811 **
    812 ** Function         btu_hcif_change_conn_link_key_evt
    813 **
    814 ** Description      Process event HCI_CHANGE_CONN_LINK_KEY_EVT
    815 **
    816 ** Returns          void
    817 **
    818 *******************************************************************************/
    819 static void btu_hcif_change_conn_link_key_evt (UINT8 *p)
    820 {
    821     UINT8   status;
    822     UINT16  handle;
    823 
    824     STREAM_TO_UINT8  (status, p);
    825     STREAM_TO_UINT16 (handle, p);
    826 
    827     btm_acl_link_key_change (handle, status);
    828 }
    829 
    830 
    831 /*******************************************************************************
    832 **
    833 ** Function         btu_hcif_master_link_key_comp_evt
    834 **
    835 ** Description      Process event HCI_MASTER_LINK_KEY_COMP_EVT
    836 **
    837 ** Returns          void
    838 **
    839 *******************************************************************************/
    840 static void btu_hcif_master_link_key_comp_evt (UINT8 *p)
    841 {
    842     UINT8   status;
    843     UINT16  handle;
    844     UINT8   key_flg;
    845 
    846     STREAM_TO_UINT8  (status, p);
    847     STREAM_TO_UINT16 (handle, p);
    848     STREAM_TO_UINT8  (key_flg, p);
    849 
    850     btm_sec_mkey_comp_event (handle, status, key_flg);
    851 }
    852 
    853 
    854 /*******************************************************************************
    855 **
    856 ** Function         btu_hcif_read_rmt_features_comp_evt
    857 **
    858 ** Description      Process event HCI_READ_RMT_FEATURES_COMP_EVT
    859 **
    860 ** Returns          void
    861 **
    862 *******************************************************************************/
    863 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p)
    864 {
    865     btm_read_remote_features_complete(p);
    866 }
    867 
    868 /*******************************************************************************
    869 **
    870 ** Function         btu_hcif_read_rmt_ext_features_comp_evt
    871 **
    872 ** Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
    873 **
    874 ** Returns          void
    875 **
    876 *******************************************************************************/
    877 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p)
    878 {
    879     UINT8 *p_cur = p;
    880     UINT8 status;
    881     UINT16 handle;
    882 
    883     STREAM_TO_UINT8 (status, p_cur);
    884 
    885     if (status == HCI_SUCCESS)
    886         btm_read_remote_ext_features_complete(p);
    887     else
    888     {
    889         STREAM_TO_UINT16 (handle, p_cur);
    890         btm_read_remote_ext_features_failed(status, handle);
    891     }
    892 }
    893 
    894 /*******************************************************************************
    895 **
    896 ** Function         btu_hcif_read_rmt_version_comp_evt
    897 **
    898 ** Description      Process event HCI_READ_RMT_VERSION_COMP_EVT
    899 **
    900 ** Returns          void
    901 **
    902 *******************************************************************************/
    903 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p)
    904 {
    905     btm_read_remote_version_complete (p);
    906 }
    907 
    908 
    909 /*******************************************************************************
    910 **
    911 ** Function         btu_hcif_qos_setup_comp_evt
    912 **
    913 ** Description      Process event HCI_QOS_SETUP_COMP_EVT
    914 **
    915 ** Returns          void
    916 **
    917 *******************************************************************************/
    918 static void btu_hcif_qos_setup_comp_evt (UINT8 *p)
    919 {
    920     UINT8 status;
    921     UINT16 handle;
    922     FLOW_SPEC flow;
    923 
    924     STREAM_TO_UINT8 (status, p);
    925     STREAM_TO_UINT16 (handle, p);
    926     STREAM_TO_UINT8 (flow.qos_flags, p);
    927     STREAM_TO_UINT8 (flow.service_type, p);
    928     STREAM_TO_UINT32 (flow.token_rate, p);
    929     STREAM_TO_UINT32 (flow.peak_bandwidth, p);
    930     STREAM_TO_UINT32 (flow.latency, p);
    931     STREAM_TO_UINT32 (flow.delay_variation, p);
    932 
    933     btm_qos_setup_complete(status, handle, &flow);
    934 }
    935 
    936 
    937 /*******************************************************************************
    938 **
    939 ** Function         btu_hcif_esco_connection_comp_evt
    940 **
    941 ** Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
    942 **
    943 ** Returns          void
    944 **
    945 *******************************************************************************/
    946 static void btu_hcif_esco_connection_comp_evt (UINT8 *p)
    947 {
    948 #if BTM_SCO_INCLUDED == TRUE
    949     tBTM_ESCO_DATA  data;
    950     UINT16          handle;
    951     BD_ADDR         bda;
    952     UINT8           status;
    953 
    954     STREAM_TO_UINT8 (status, p);
    955     STREAM_TO_UINT16 (handle, p);
    956     STREAM_TO_BDADDR (bda, p);
    957 
    958     STREAM_TO_UINT8 (data.link_type, p);
    959     STREAM_TO_UINT8 (data.tx_interval, p);
    960     STREAM_TO_UINT8 (data.retrans_window, p);
    961     STREAM_TO_UINT16 (data.rx_pkt_len, p);
    962     STREAM_TO_UINT16 (data.tx_pkt_len, p);
    963     STREAM_TO_UINT8 (data.air_mode, p);
    964 
    965     memcpy (data.bd_addr, bda, BD_ADDR_LEN);
    966     btm_sco_connected (status, bda, handle, &data);
    967 #endif
    968 }
    969 
    970 
    971 /*******************************************************************************
    972 **
    973 ** Function         btu_hcif_esco_connection_chg_evt
    974 **
    975 ** Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
    976 **
    977 ** Returns          void
    978 **
    979 *******************************************************************************/
    980 static void btu_hcif_esco_connection_chg_evt (UINT8 *p)
    981 {
    982 #if BTM_SCO_INCLUDED == TRUE
    983     UINT16  handle;
    984     UINT16  tx_pkt_len;
    985     UINT16  rx_pkt_len;
    986     UINT8   status;
    987     UINT8   tx_interval;
    988     UINT8   retrans_window;
    989 
    990     STREAM_TO_UINT8 (status, p);
    991     STREAM_TO_UINT16 (handle, p);
    992 
    993     STREAM_TO_UINT8 (tx_interval, p);
    994     STREAM_TO_UINT8 (retrans_window, p);
    995     STREAM_TO_UINT16 (rx_pkt_len, p);
    996     STREAM_TO_UINT16 (tx_pkt_len, p);
    997 
    998     btm_esco_proc_conn_chg (status, handle, tx_interval, retrans_window,
    999                             rx_pkt_len, tx_pkt_len);
   1000 #endif
   1001 }
   1002 
   1003 /*******************************************************************************
   1004 **
   1005 ** Function         btu_hcif_hdl_command_complete
   1006 **
   1007 ** Description      Handle command complete event
   1008 **
   1009 ** Returns          void
   1010 **
   1011 *******************************************************************************/
   1012 static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_len,
   1013                                            void *p_cplt_cback)
   1014 {
   1015     switch (opcode)
   1016     {
   1017         case HCI_RESET:
   1018             btm_reset_complete ();  /* BR/EDR */
   1019             break;
   1020 
   1021         case HCI_INQUIRY_CANCEL:
   1022             /* Tell inquiry processing that we are done */
   1023             btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
   1024             break;
   1025         case HCI_SET_EVENT_FILTER:
   1026             btm_event_filter_complete (p);
   1027             break;
   1028 
   1029         case HCI_READ_STORED_LINK_KEY:
   1030             btm_read_stored_link_key_complete (p);
   1031             break;
   1032 
   1033         case HCI_WRITE_STORED_LINK_KEY:
   1034             btm_write_stored_link_key_complete (p);
   1035             break;
   1036 
   1037         case HCI_DELETE_STORED_LINK_KEY:
   1038             btm_delete_stored_link_key_complete (p);
   1039             break;
   1040 
   1041         case HCI_READ_LOCAL_VERSION_INFO:
   1042             btm_read_local_version_complete (p, evt_len);
   1043             break;
   1044 
   1045         case HCI_READ_POLICY_SETTINGS:
   1046             btm_read_link_policy_complete (p);
   1047             break;
   1048 
   1049         case HCI_READ_BUFFER_SIZE:
   1050             btm_read_hci_buf_size_complete (p, evt_len);
   1051             break;
   1052 
   1053         case HCI_READ_LOCAL_SUPPORTED_CMDS:
   1054             btm_read_local_supported_cmds_complete (p);
   1055             break;
   1056 
   1057         case HCI_READ_LOCAL_FEATURES:
   1058             btm_read_local_features_complete (p, evt_len);
   1059             break;
   1060 
   1061         case HCI_READ_LOCAL_EXT_FEATURES:
   1062             btm_read_local_ext_features_complete (p, evt_len);
   1063             break;
   1064 
   1065         case HCI_READ_LOCAL_NAME:
   1066             btm_read_local_name_complete (p, evt_len);
   1067             break;
   1068 
   1069         case HCI_READ_BD_ADDR:
   1070             btm_read_local_addr_complete (p, evt_len);
   1071             break;
   1072 
   1073         case HCI_GET_LINK_QUALITY:
   1074             btm_read_link_quality_complete (p);
   1075             break;
   1076 
   1077         case HCI_READ_RSSI:
   1078             btm_read_rssi_complete (p);
   1079             break;
   1080 
   1081         case HCI_READ_TRANSMIT_POWER_LEVEL:
   1082             btm_read_tx_power_complete(p, FALSE);
   1083             break;
   1084 
   1085         case HCI_CREATE_CONNECTION_CANCEL:
   1086             btm_create_conn_cancel_complete(p);
   1087             break;
   1088 
   1089         case HCI_READ_LOCAL_OOB_DATA:
   1090 #if BTM_OOB_INCLUDED == TRUE
   1091             btm_read_local_oob_complete(p);
   1092 #endif
   1093             break;
   1094 
   1095 
   1096         case HCI_READ_INQ_TX_POWER_LEVEL:
   1097             btm_read_linq_tx_power_complete (p);
   1098             break;
   1099 
   1100         case HCI_WRITE_SIMPLE_PAIRING_MODE:
   1101             btm_write_simple_paring_mode_complete (p);
   1102             break;
   1103 
   1104         case HCI_WRITE_LE_HOST_SUPPORTED:
   1105             btm_write_le_host_supported_complete (p);
   1106             break;
   1107 
   1108 #if (BLE_INCLUDED == TRUE)
   1109 /* BLE Commands sComplete*/
   1110         case HCI_BLE_READ_WHITE_LIST_SIZE :
   1111             btm_read_white_list_size_complete(p, evt_len);
   1112             break;
   1113 
   1114         case HCI_BLE_ADD_WHITE_LIST:
   1115             btm_ble_add_2_white_list_complete(*p);
   1116             break;
   1117 
   1118         case HCI_BLE_CLEAR_WHITE_LIST:
   1119             btm_ble_clear_white_list_complete(p, evt_len);
   1120             break;
   1121 
   1122         case HCI_BLE_REMOVE_WHITE_LIST:
   1123             btm_ble_remove_from_white_list_complete(p, evt_len);
   1124             break;
   1125 
   1126         case HCI_BLE_RAND:
   1127         case HCI_BLE_ENCRYPT:
   1128             btm_ble_rand_enc_complete (p, opcode, (tBTM_RAND_ENC_CB *)p_cplt_cback);
   1129             break;
   1130 
   1131         case HCI_BLE_READ_BUFFER_SIZE:
   1132             btm_read_ble_buf_size_complete(p, evt_len);
   1133             break;
   1134 
   1135         case HCI_BLE_READ_LOCAL_SPT_FEAT:
   1136             btm_read_ble_local_supported_features_complete(p, evt_len);
   1137             break;
   1138 
   1139         case HCI_BLE_READ_ADV_CHNL_TX_POWER:
   1140             btm_read_tx_power_complete(p, TRUE);
   1141             break;
   1142 
   1143         case HCI_BLE_WRITE_ADV_ENABLE:
   1144             btm_ble_write_adv_enable_complete(p);
   1145             break;
   1146 
   1147         case HCI_BLE_READ_SUPPORTED_STATES:
   1148             btm_read_ble_local_supported_states_complete(p, evt_len);
   1149             break;
   1150 
   1151         case HCI_BLE_CREATE_LL_CONN:
   1152             btm_ble_create_ll_conn_complete(*p);
   1153             break;
   1154 
   1155         case HCI_BLE_TRANSMITTER_TEST:
   1156         case HCI_BLE_RECEIVER_TEST:
   1157         case HCI_BLE_TEST_END:
   1158             btm_ble_test_command_complete(p);
   1159             break;
   1160 #endif /* (BLE_INCLUDED == TRUE) */
   1161 
   1162         default:
   1163             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
   1164                 btm_vsc_complete (p, opcode, evt_len, (tBTM_CMPL_CB *)p_cplt_cback);
   1165             break;
   1166     }
   1167 }
   1168 
   1169 /*******************************************************************************
   1170 **
   1171 ** Function         btu_hcif_command_complete_evt
   1172 **
   1173 ** Description      Process event HCI_COMMAND_COMPLETE_EVT
   1174 **
   1175 ** Returns          void
   1176 **
   1177 *******************************************************************************/
   1178 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len)
   1179 {
   1180     tHCI_CMD_CB *p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
   1181     UINT16      cc_opcode;
   1182     BT_HDR      *p_cmd;
   1183     void        *p_cplt_cback = NULL;
   1184 
   1185     STREAM_TO_UINT8  (p_hci_cmd_cb->cmd_window, p);
   1186 
   1187 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
   1188     if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
   1189         p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
   1190 #endif
   1191 
   1192     STREAM_TO_UINT16 (cc_opcode, p);
   1193 
   1194     evt_len -= 3;
   1195 
   1196     /* only do this for certain commands */
   1197     if ((cc_opcode != HCI_RESET) && (cc_opcode != HCI_HOST_NUM_PACKETS_DONE) &&
   1198         (cc_opcode != HCI_COMMAND_NONE))
   1199     {
   1200         /* dequeue and free stored command */
   1201 
   1202 /* always use cmd code check, when one cmd timeout waiting for cmd_cmpl,
   1203    it'll cause the rest of the command goes in wrong order                  */
   1204         p_cmd = (BT_HDR *) GKI_getfirst (&p_hci_cmd_cb->cmd_cmpl_q);
   1205 
   1206         while (p_cmd)
   1207         {
   1208             UINT16 opcode_dequeued;
   1209             UINT8  *p_dequeued;
   1210 
   1211             /* Make sure dequeued command is for the command_cplt received */
   1212             p_dequeued = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
   1213             STREAM_TO_UINT16 (opcode_dequeued, p_dequeued);
   1214 
   1215             if (opcode_dequeued != cc_opcode)
   1216             {
   1217                 /* opcode does not match, check next command in the queue */
   1218                 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
   1219                 continue;
   1220             }
   1221             GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
   1222 
   1223             /* If command was a VSC, then extract command_complete callback */
   1224             if ((cc_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
   1225 #if BLE_INCLUDED == TRUE
   1226                 || (cc_opcode == HCI_BLE_RAND )
   1227                 || (cc_opcode == HCI_BLE_ENCRYPT)
   1228 #endif
   1229                )
   1230             {
   1231                 p_cplt_cback = *((void **)(p_cmd + 1));
   1232             }
   1233 
   1234             GKI_freebuf (p_cmd);
   1235 
   1236             break;
   1237         }
   1238 
   1239         /* if more commands in queue restart timer */
   1240         if (BTU_CMD_CMPL_TIMEOUT > 0)
   1241         {
   1242             if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
   1243             {
   1244 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
   1245                 p_hci_cmd_cb->checked_hcisu = FALSE;
   1246 #endif
   1247                 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
   1248                                  (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
   1249                                  BTU_CMD_CMPL_TIMEOUT);
   1250             }
   1251             else
   1252             {
   1253                 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
   1254             }
   1255         }
   1256     }
   1257 
   1258     /* handle event */
   1259     btu_hcif_hdl_command_complete (cc_opcode, p, evt_len, p_cplt_cback);
   1260 
   1261     /* see if we can send more commands */
   1262     btu_hcif_send_cmd (controller_id, NULL);
   1263 }
   1264 
   1265 
   1266 /*******************************************************************************
   1267 **
   1268 ** Function         btu_hcif_hdl_command_status
   1269 **
   1270 ** Description      Handle a command status event
   1271 **
   1272 ** Returns          void
   1273 **
   1274 *******************************************************************************/
   1275 static void btu_hcif_hdl_command_status (UINT16 opcode, UINT8 status, UINT8 *p_cmd,
   1276                                          void *p_vsc_status_cback)
   1277 {
   1278     BD_ADDR         bd_addr;
   1279     UINT16          handle;
   1280 #if BTM_SCO_INCLUDED == TRUE
   1281     tBTM_ESCO_DATA  esco_data;
   1282 #endif
   1283 
   1284 #if BTM_PWR_MGR_INCLUDED == TRUE
   1285     switch (opcode)
   1286     {
   1287         case HCI_EXIT_SNIFF_MODE:
   1288         case HCI_EXIT_PARK_MODE:
   1289 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
   1290             if (status != HCI_SUCCESS)
   1291             {
   1292                 /* Allow SCO initiation to continue if waiting for change mode event */
   1293                 if (p_cmd != NULL)
   1294                 {
   1295                     p_cmd++;    /* bypass length field */
   1296                     STREAM_TO_UINT16 (handle, p_cmd);
   1297                     btm_sco_chk_pend_unpark (status, handle);
   1298                 }
   1299             }
   1300 #endif
   1301             /* Case Falls Through */
   1302 
   1303         case HCI_HOLD_MODE:
   1304         case HCI_SNIFF_MODE:
   1305         case HCI_PARK_MODE:
   1306             btm_pm_proc_cmd_status(status);
   1307             break;
   1308 
   1309         default:
   1310 #endif  /* BTM_PWR_MGR_INCLUDED */
   1311             /* If command failed to start, we may need to tell BTM */
   1312             if (status != HCI_SUCCESS)
   1313             {
   1314                 switch (opcode)
   1315                 {
   1316                     case HCI_INQUIRY:
   1317                         /* Tell inquiry processing that we are done */
   1318                         btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
   1319                         break;
   1320 
   1321                     case HCI_RMT_NAME_REQUEST:
   1322                         /* Tell inquiry processing that we are done */
   1323                         btm_process_remote_name (NULL, NULL, 0, status);
   1324 
   1325                         btm_sec_rmt_name_request_complete (NULL, NULL, status);
   1326                         break;
   1327 
   1328                     case HCI_CHANGE_CONN_LINK_KEY:
   1329                         /* Let host know we're done with error */
   1330                         /* read handle out of stored command */
   1331                         if (p_cmd != NULL)
   1332                         {
   1333                             p_cmd++;
   1334                             STREAM_TO_UINT16 (handle, p_cmd);
   1335 
   1336                             btm_acl_link_key_change (handle, status);
   1337                         }
   1338                         break;
   1339 
   1340                     case HCI_QOS_SETUP_COMP_EVT:
   1341                         /* Tell qos setup that we are done */
   1342                         btm_qos_setup_complete(status,0,NULL);
   1343                         break;
   1344 
   1345                     case HCI_SWITCH_ROLE:
   1346                         /* Tell BTM that the command failed */
   1347                         /* read bd addr out of stored command */
   1348                         if (p_cmd != NULL)
   1349                         {
   1350                             p_cmd++;
   1351                             STREAM_TO_BDADDR (bd_addr, p_cmd);
   1352                             btm_acl_role_changed(status, bd_addr, BTM_ROLE_UNDEFINED);
   1353                         }
   1354                         else
   1355                             btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
   1356                         l2c_link_role_changed (NULL, BTM_ROLE_UNDEFINED, HCI_ERR_COMMAND_DISALLOWED);
   1357                         break;
   1358 
   1359                     case HCI_CREATE_CONNECTION:
   1360                         /* read bd addr out of stored command */
   1361                         if (p_cmd != NULL)
   1362                         {
   1363                             p_cmd++;
   1364                             STREAM_TO_BDADDR (bd_addr, p_cmd);
   1365                             btm_sec_connected (bd_addr, HCI_INVALID_HANDLE, status, 0);
   1366                             l2c_link_hci_conn_comp (status, HCI_INVALID_HANDLE, bd_addr);
   1367                         }
   1368                         break;
   1369 
   1370                     case HCI_READ_RMT_EXT_FEATURES:
   1371                         if (p_cmd != NULL)
   1372                         {
   1373                             p_cmd++; /* skip command length */
   1374                             STREAM_TO_UINT16 (handle, p_cmd);
   1375                         }
   1376                         else
   1377                             handle = HCI_INVALID_HANDLE;
   1378 
   1379                         btm_read_remote_ext_features_failed(status, handle);
   1380                         break;
   1381 
   1382                     case HCI_AUTHENTICATION_REQUESTED:
   1383                         /* Device refused to start authentication.  That should be treated as authentication failure. */
   1384                         btm_sec_auth_complete (BTM_INVALID_HCI_HANDLE, status);
   1385                         break;
   1386 
   1387                     case HCI_SET_CONN_ENCRYPTION:
   1388                         /* Device refused to start encryption.  That should be treated as encryption failure. */
   1389                         btm_sec_encrypt_change (BTM_INVALID_HCI_HANDLE, status, FALSE);
   1390                         break;
   1391 
   1392 #if BLE_INCLUDED == TRUE
   1393                     case HCI_BLE_CREATE_LL_CONN:
   1394                         btm_ble_create_ll_conn_complete(status);
   1395                         break;
   1396 #endif
   1397 
   1398 #if BTM_SCO_INCLUDED == TRUE
   1399                     case HCI_SETUP_ESCO_CONNECTION:
   1400                         /* read handle out of stored command */
   1401                         if (p_cmd != NULL)
   1402                         {
   1403                             p_cmd++;
   1404                             STREAM_TO_UINT16 (handle, p_cmd);
   1405 
   1406                             /* Determine if initial connection failed or is a change of setup */
   1407                             if (btm_is_sco_active(handle))
   1408                                 btm_esco_proc_conn_chg (status, handle, 0, 0, 0, 0);
   1409                             else
   1410                                 btm_sco_connected (status, NULL, handle, &esco_data);
   1411                         }
   1412                         break;
   1413 #endif
   1414 
   1415 /* This is commented out until an upper layer cares about returning event
   1416 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
   1417             case HCI_ENHANCED_FLUSH:
   1418                 break;
   1419 #endif
   1420 */
   1421                     default:
   1422                         if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
   1423                             btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
   1424                         break;
   1425                 }
   1426 
   1427             }
   1428             else
   1429             {
   1430                 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
   1431                     btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
   1432             }
   1433 #if BTM_PWR_MGR_INCLUDED == TRUE
   1434     }
   1435 #endif
   1436 }
   1437 
   1438 /*******************************************************************************
   1439 **
   1440 ** Function         btu_hcif_command_status_evt
   1441 **
   1442 ** Description      Process event HCI_COMMAND_STATUS_EVT
   1443 **
   1444 ** Returns          void
   1445 **
   1446 *******************************************************************************/
   1447 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p)
   1448 {
   1449     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
   1450     UINT8       status;
   1451     UINT16      opcode;
   1452     UINT16      cmd_opcode;
   1453     BT_HDR      *p_cmd = NULL;
   1454     UINT8       *p_data = NULL;
   1455     void        *p_vsc_status_cback = NULL;
   1456 
   1457     STREAM_TO_UINT8  (status, p);
   1458     STREAM_TO_UINT8  (p_hci_cmd_cb->cmd_window, p);
   1459 
   1460 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
   1461     if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
   1462         p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
   1463 #endif
   1464 
   1465     STREAM_TO_UINT16 (opcode, p);
   1466 
   1467     /* only do this for certain commands */
   1468     if ((opcode != HCI_RESET) && (opcode != HCI_HOST_NUM_PACKETS_DONE) &&
   1469         (opcode != HCI_COMMAND_NONE))
   1470     {
   1471         /*look for corresponding command in cmd_queue*/
   1472         p_cmd = (BT_HDR *) GKI_getfirst(&(p_hci_cmd_cb->cmd_cmpl_q));
   1473         while (p_cmd)
   1474         {
   1475             p_data = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
   1476             STREAM_TO_UINT16 (cmd_opcode, p_data);
   1477 
   1478             /* Make sure this  command is for the command_status received */
   1479             if (cmd_opcode != opcode)
   1480             {
   1481                 /* opcode does not match, check next command in the queue */
   1482                 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
   1483                 continue;
   1484             }
   1485             else
   1486             {
   1487                 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
   1488 
   1489                 /* If command was a VSC, then extract command_status callback */
   1490                  if ((cmd_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
   1491                 {
   1492                     p_vsc_status_cback = *((void **)(p_cmd + 1));
   1493                 }
   1494                 break;
   1495             }
   1496         }
   1497 
   1498         /* if more commands in queue restart timer */
   1499         if (BTU_CMD_CMPL_TIMEOUT > 0)
   1500         {
   1501             if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
   1502             {
   1503 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
   1504                 p_hci_cmd_cb->checked_hcisu = FALSE;
   1505 #endif
   1506                 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
   1507                                  (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
   1508                                  BTU_CMD_CMPL_TIMEOUT);
   1509             }
   1510             else
   1511             {
   1512                 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
   1513             }
   1514         }
   1515     }
   1516 
   1517     /* handle command */
   1518     btu_hcif_hdl_command_status (opcode, status, p_data, p_vsc_status_cback);
   1519 
   1520     /* free stored command */
   1521     if (p_cmd != NULL)
   1522     {
   1523         GKI_freebuf (p_cmd);
   1524     }
   1525     else
   1526     {
   1527         HCI_TRACE_WARNING("No command in queue matching opcode %d", opcode);
   1528     }
   1529 
   1530     /* See if we can forward any more commands */
   1531     btu_hcif_send_cmd (controller_id, NULL);
   1532 }
   1533 
   1534 /*******************************************************************************
   1535 **
   1536 ** Function         btu_hcif_cmd_timeout
   1537 **
   1538 ** Description      Handle a command timeout
   1539 **
   1540 ** Returns          void
   1541 **
   1542 *******************************************************************************/
   1543 void btu_hcif_cmd_timeout (UINT8 controller_id)
   1544 {
   1545     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
   1546     BT_HDR  *p_cmd;
   1547     UINT8   *p;
   1548     void    *p_cplt_cback = NULL;
   1549     UINT16  opcode;
   1550     UINT16  event;
   1551 
   1552 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
   1553     if (!(p_hci_cmd_cb->checked_hcisu))
   1554     {
   1555         HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout - double check HCISU", controller_id);
   1556 
   1557         /* trigger HCISU to read any pending data in transport buffer */
   1558         GKI_send_event(HCISU_TASK, HCISU_EVT_MASK);
   1559 
   1560         btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
   1561                          (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
   1562                          2); /* start short timer, if timer is set to 1 then it could expire before HCISU checks. */
   1563 
   1564         p_hci_cmd_cb->checked_hcisu = TRUE;
   1565 
   1566         return;
   1567     }
   1568 #endif
   1569 
   1570     /* set the controller cmd window to 1, as if we received a response, so
   1571     ** the flow of commands from the stack doesn't hang */
   1572     p_hci_cmd_cb->cmd_window = 1;
   1573 
   1574     /* get queued command */
   1575     if ((p_cmd = (BT_HDR *) GKI_dequeue (&(p_hci_cmd_cb->cmd_cmpl_q))) == NULL)
   1576     {
   1577         HCI_TRACE_WARNING("Cmd timeout; no cmd in queue");
   1578         return;
   1579     }
   1580 
   1581     /* if more commands in queue restart timer */
   1582     if (BTU_CMD_CMPL_TIMEOUT > 0)
   1583     {
   1584         if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
   1585         {
   1586 #if (defined(BTU_CMD_CMPL_TOUT_DOUBLE_CHECK) && BTU_CMD_CMPL_TOUT_DOUBLE_CHECK == TRUE)
   1587             p_hci_cmd_cb->checked_hcisu = FALSE;
   1588 #endif
   1589             btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
   1590                              (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
   1591                              BTU_CMD_CMPL_TIMEOUT);
   1592         }
   1593     }
   1594 
   1595     p = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
   1596 #if (NFC_INCLUDED == TRUE)
   1597     if (controller_id == NFC_CONTROLLER_ID)
   1598     {
   1599         //TODO call nfc_ncif_cmd_timeout
   1600         HCI_TRACE_WARNING("BTU NCI command timeout - header 0x%02x%02x", p[0], p[1]);
   1601         return;
   1602     }
   1603 #endif
   1604 
   1605     /* get opcode from stored command */
   1606     STREAM_TO_UINT16 (opcode, p);
   1607 
   1608 // btla-specific ++
   1609 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE))
   1610     ALOGE("######################################################################");
   1611     ALOGE("#");
   1612     ALOGE("# WARNING : BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
   1613     ALOGE("#");
   1614     ALOGE("######################################################################");
   1615 #else
   1616     HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
   1617 #endif
   1618 // btla-specific ++
   1619 
   1620     /* send stack a fake command complete or command status, but first determine
   1621     ** which to send
   1622     */
   1623     switch (opcode)
   1624     {
   1625         case HCI_HOLD_MODE:
   1626         case HCI_SNIFF_MODE:
   1627         case HCI_EXIT_SNIFF_MODE:
   1628         case HCI_PARK_MODE:
   1629         case HCI_EXIT_PARK_MODE:
   1630         case HCI_INQUIRY:
   1631         case HCI_RMT_NAME_REQUEST:
   1632         case HCI_QOS_SETUP_COMP_EVT:
   1633         case HCI_CREATE_CONNECTION:
   1634         case HCI_CHANGE_CONN_LINK_KEY:
   1635         case HCI_SWITCH_ROLE:
   1636         case HCI_READ_RMT_EXT_FEATURES:
   1637         case HCI_AUTHENTICATION_REQUESTED:
   1638         case HCI_SET_CONN_ENCRYPTION:
   1639 #if BTM_SCO_INCLUDED == TRUE
   1640         case HCI_SETUP_ESCO_CONNECTION:
   1641 #endif
   1642             /* fake a command status */
   1643             btu_hcif_hdl_command_status (opcode, HCI_ERR_UNSPECIFIED, p, NULL);
   1644             break;
   1645 
   1646         default:
   1647             /* If vendor specific restore the callback function */
   1648             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
   1649 #if BLE_INCLUDED == TRUE
   1650                 || (opcode == HCI_BLE_RAND ) ||
   1651                 (opcode == HCI_BLE_ENCRYPT)
   1652 #endif
   1653                )
   1654             {
   1655                 p_cplt_cback = *((void **)(p_cmd + 1));
   1656             }
   1657 
   1658             /* fake a command complete; first create a fake event */
   1659             event = HCI_ERR_UNSPECIFIED;
   1660             btu_hcif_hdl_command_complete (opcode, (UINT8 *)&event, 1, p_cplt_cback);
   1661             break;
   1662     }
   1663 
   1664     /* free stored command */
   1665     GKI_freebuf(p_cmd);
   1666 
   1667     num_hci_cmds_timed_out++;
   1668     /* When we receive consecutive HCI cmd timeouts for >=BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART
   1669      times, Bluetooth process will be killed and restarted */
   1670     if (num_hci_cmds_timed_out >= BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART)
   1671     {
   1672         HCI_TRACE_ERROR("Num consecutive HCI Cmd tout =%d Restarting BT process",num_hci_cmds_timed_out);
   1673 
   1674         usleep(10000); /* 10 milliseconds */
   1675         /* Killing the process to force a restart as part of fault tolerance */
   1676         kill(getpid(), SIGKILL);
   1677     }
   1678     else
   1679     {
   1680         HCI_TRACE_WARNING("HCI Cmd timeout counter %d", num_hci_cmds_timed_out);
   1681 
   1682         /* If anyone wants device status notifications, give him one */
   1683         btm_report_device_status (BTM_DEV_STATUS_CMD_TOUT);
   1684     }
   1685     /* See if we can forward any more commands */
   1686     btu_hcif_send_cmd (controller_id, NULL);
   1687 }
   1688 
   1689 /*******************************************************************************
   1690 **
   1691 ** Function         btu_hcif_hardware_error_evt
   1692 **
   1693 ** Description      Process event HCI_HARDWARE_ERROR_EVT
   1694 **
   1695 ** Returns          void
   1696 **
   1697 *******************************************************************************/
   1698 static void btu_hcif_hardware_error_evt (UINT8 *p)
   1699 {
   1700     HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
   1701 
   1702     /* If anyone wants device status notifications, give him one. */
   1703     btm_report_device_status (BTM_DEV_STATUS_DOWN);
   1704 
   1705     /* Reset the controller */
   1706     if (BTM_IsDeviceUp())
   1707         BTM_DeviceReset (NULL);
   1708 }
   1709 
   1710 
   1711 /*******************************************************************************
   1712 **
   1713 ** Function         btu_hcif_flush_occured_evt
   1714 **
   1715 ** Description      Process event HCI_FLUSH_OCCURED_EVT
   1716 **
   1717 ** Returns          void
   1718 **
   1719 *******************************************************************************/
   1720 static void btu_hcif_flush_occured_evt (void)
   1721 {
   1722 }
   1723 
   1724 
   1725 /*******************************************************************************
   1726 **
   1727 ** Function         btu_hcif_role_change_evt
   1728 **
   1729 ** Description      Process event HCI_ROLE_CHANGE_EVT
   1730 **
   1731 ** Returns          void
   1732 **
   1733 *******************************************************************************/
   1734 static void btu_hcif_role_change_evt (UINT8 *p)
   1735 {
   1736     UINT8       status;
   1737     BD_ADDR     bda;
   1738     UINT8       role;
   1739 
   1740     STREAM_TO_UINT8 (status, p);
   1741     STREAM_TO_BDADDR (bda, p);
   1742     STREAM_TO_UINT8  (role, p);
   1743 
   1744     l2c_link_role_changed (bda, role, status);
   1745     btm_acl_role_changed(status, bda, role);
   1746 }
   1747 
   1748 
   1749 /*******************************************************************************
   1750 **
   1751 ** Function         btu_hcif_num_compl_data_pkts_evt
   1752 **
   1753 ** Description      Process event HCI_NUM_COMPL_DATA_PKTS_EVT
   1754 **
   1755 ** Returns          void
   1756 **
   1757 *******************************************************************************/
   1758 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p)
   1759 {
   1760     /* Process for L2CAP and SCO */
   1761     l2c_link_process_num_completed_pkts (p);
   1762 
   1763     /* Send on to SCO */
   1764     /*?? No SCO for now */
   1765 }
   1766 
   1767 /*******************************************************************************
   1768 **
   1769 ** Function         btu_hcif_mode_change_evt
   1770 **
   1771 ** Description      Process event HCI_MODE_CHANGE_EVT
   1772 **
   1773 ** Returns          void
   1774 **
   1775 *******************************************************************************/
   1776 static void btu_hcif_mode_change_evt (UINT8 *p)
   1777 {
   1778     UINT8       status;
   1779     UINT16      handle;
   1780     UINT8       current_mode;
   1781     UINT16      interval;
   1782 
   1783     STREAM_TO_UINT8 (status, p);
   1784 
   1785     STREAM_TO_UINT16 (handle, p);
   1786     STREAM_TO_UINT8 (current_mode, p);
   1787     STREAM_TO_UINT16 (interval, p);
   1788 #if BTM_PWR_MGR_INCLUDED == TRUE
   1789 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
   1790     btm_sco_chk_pend_unpark (status, handle);
   1791 #endif
   1792     btm_pm_proc_mode_change (status, handle, current_mode, interval);
   1793 #else
   1794     btm_process_mode_change (status, handle, current_mode, interval);
   1795 #endif /* BTM_PWR_MGR_INCLUDED == TRUE */
   1796 
   1797 #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
   1798     hidd_pm_proc_mode_change( status, current_mode, interval ) ;
   1799 #endif
   1800 }
   1801 
   1802 /*******************************************************************************
   1803 **
   1804 ** Function         btu_hcif_ssr_evt
   1805 **
   1806 ** Description      Process event HCI_SNIFF_SUB_RATE_EVT
   1807 **
   1808 ** Returns          void
   1809 **
   1810 *******************************************************************************/
   1811     #if (BTM_SSR_INCLUDED == TRUE)
   1812 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len)
   1813 {
   1814 #if (BTM_PWR_MGR_INCLUDED == TRUE)
   1815     btm_pm_proc_ssr_evt(p, evt_len);
   1816 #endif
   1817 }
   1818     #endif
   1819 
   1820 
   1821 /*******************************************************************************
   1822 **
   1823 ** Function         btu_hcif_return_link_keys_evt
   1824 **
   1825 ** Description      Process event HCI_RETURN_LINK_KEYS_EVT
   1826 **
   1827 ** Returns          void
   1828 **
   1829 *******************************************************************************/
   1830 
   1831 static void btu_hcif_return_link_keys_evt (UINT8 *p)
   1832 {
   1833     UINT8                       num_keys;
   1834     tBTM_RETURN_LINK_KEYS_EVT   *result;
   1835 
   1836     /* get the number of link keys */
   1837     num_keys = *p;
   1838 
   1839     /* If there are no link keys don't call the call back */
   1840     if (!num_keys)
   1841         return;
   1842 
   1843     /* Take one extra byte at the beginning to specify event */
   1844     result = (tBTM_RETURN_LINK_KEYS_EVT *)(--p);
   1845     result->event = BTM_CB_EVT_RETURN_LINK_KEYS;
   1846 
   1847     /* Call the BTM function to pass the link keys to application */
   1848     btm_return_link_keys_evt (result);
   1849 }
   1850 
   1851 
   1852 /*******************************************************************************
   1853 **
   1854 ** Function         btu_hcif_pin_code_request_evt
   1855 **
   1856 ** Description      Process event HCI_PIN_CODE_REQUEST_EVT
   1857 **
   1858 ** Returns          void
   1859 **
   1860 *******************************************************************************/
   1861 static void btu_hcif_pin_code_request_evt (UINT8 *p)
   1862 {
   1863     BD_ADDR  bda;
   1864 
   1865     STREAM_TO_BDADDR (bda, p);
   1866 
   1867     /* Tell L2CAP that there was a PIN code request,  */
   1868     /* it may need to stretch timeouts                */
   1869     l2c_pin_code_request (bda);
   1870 
   1871     btm_sec_pin_code_request (bda);
   1872 }
   1873 
   1874 
   1875 /*******************************************************************************
   1876 **
   1877 ** Function         btu_hcif_link_key_request_evt
   1878 **
   1879 ** Description      Process event HCI_LINK_KEY_REQUEST_EVT
   1880 **
   1881 ** Returns          void
   1882 **
   1883 *******************************************************************************/
   1884 static void btu_hcif_link_key_request_evt (UINT8 *p)
   1885 {
   1886     BD_ADDR  bda;
   1887 
   1888     STREAM_TO_BDADDR (bda, p);
   1889     btm_sec_link_key_request (bda);
   1890 }
   1891 
   1892 
   1893 /*******************************************************************************
   1894 **
   1895 ** Function         btu_hcif_link_key_notification_evt
   1896 **
   1897 ** Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
   1898 **
   1899 ** Returns          void
   1900 **
   1901 *******************************************************************************/
   1902 static void btu_hcif_link_key_notification_evt (UINT8 *p)
   1903 {
   1904     BD_ADDR  bda;
   1905     LINK_KEY key;
   1906     UINT8    key_type;
   1907 
   1908     STREAM_TO_BDADDR (bda, p);
   1909     STREAM_TO_ARRAY16 (key, p);
   1910     STREAM_TO_UINT8 (key_type, p);
   1911 
   1912     btm_sec_link_key_notification (bda, key, key_type);
   1913 }
   1914 
   1915 
   1916 /*******************************************************************************
   1917 **
   1918 ** Function         btu_hcif_loopback_command_evt
   1919 **
   1920 ** Description      Process event HCI_LOOPBACK_COMMAND_EVT
   1921 **
   1922 ** Returns          void
   1923 **
   1924 *******************************************************************************/
   1925 static void btu_hcif_loopback_command_evt (void)
   1926 {
   1927 }
   1928 
   1929 
   1930 /*******************************************************************************
   1931 **
   1932 ** Function         btu_hcif_data_buf_overflow_evt
   1933 **
   1934 ** Description      Process event HCI_DATA_BUF_OVERFLOW_EVT
   1935 **
   1936 ** Returns          void
   1937 **
   1938 *******************************************************************************/
   1939 static void btu_hcif_data_buf_overflow_evt (void)
   1940 {
   1941 }
   1942 
   1943 
   1944 /*******************************************************************************
   1945 **
   1946 ** Function         btu_hcif_max_slots_changed_evt
   1947 **
   1948 ** Description      Process event HCI_MAX_SLOTS_CHANGED_EVT
   1949 **
   1950 ** Returns          void
   1951 **
   1952 *******************************************************************************/
   1953 static void btu_hcif_max_slots_changed_evt (void)
   1954 {
   1955 }
   1956 
   1957 
   1958 /*******************************************************************************
   1959 **
   1960 ** Function         btu_hcif_read_clock_off_comp_evt
   1961 **
   1962 ** Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
   1963 **
   1964 ** Returns          void
   1965 **
   1966 *******************************************************************************/
   1967 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p)
   1968 {
   1969     UINT8       status;
   1970     UINT16      handle;
   1971     UINT16      clock_offset;
   1972 
   1973     STREAM_TO_UINT8  (status, p);
   1974 
   1975     /* If failed to get clock offset just drop the result */
   1976     if (status != HCI_SUCCESS)
   1977         return;
   1978 
   1979     STREAM_TO_UINT16 (handle, p);
   1980     STREAM_TO_UINT16 (clock_offset, p);
   1981 
   1982     handle = HCID_GET_HANDLE (handle);
   1983 
   1984     btm_process_clk_off_comp_evt (handle, clock_offset);
   1985     btm_sec_update_clock_offset (handle, clock_offset);
   1986 }
   1987 
   1988 
   1989 /*******************************************************************************
   1990 **
   1991 ** Function         btu_hcif_conn_pkt_type_change_evt
   1992 **
   1993 ** Description      Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
   1994 **
   1995 ** Returns          void
   1996 **
   1997 *******************************************************************************/
   1998 static void btu_hcif_conn_pkt_type_change_evt (void)
   1999 {
   2000 }
   2001 
   2002 
   2003 /*******************************************************************************
   2004 **
   2005 ** Function         btu_hcif_qos_violation_evt
   2006 **
   2007 ** Description      Process event HCI_QOS_VIOLATION_EVT
   2008 **
   2009 ** Returns          void
   2010 **
   2011 *******************************************************************************/
   2012 static void btu_hcif_qos_violation_evt (UINT8 *p)
   2013 {
   2014     UINT16   handle;
   2015 
   2016     STREAM_TO_UINT16 (handle, p);
   2017 
   2018     handle = HCID_GET_HANDLE (handle);
   2019 
   2020 
   2021     l2c_link_hci_qos_violation (handle);
   2022 }
   2023 
   2024 
   2025 /*******************************************************************************
   2026 **
   2027 ** Function         btu_hcif_page_scan_mode_change_evt
   2028 **
   2029 ** Description      Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
   2030 **
   2031 ** Returns          void
   2032 **
   2033 *******************************************************************************/
   2034 static void btu_hcif_page_scan_mode_change_evt (void)
   2035 {
   2036 }
   2037 
   2038 
   2039 /*******************************************************************************
   2040 **
   2041 ** Function         btu_hcif_page_scan_rep_mode_chng_evt
   2042 **
   2043 ** Description      Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
   2044 **
   2045 ** Returns          void
   2046 **
   2047 *******************************************************************************/
   2048 static void btu_hcif_page_scan_rep_mode_chng_evt (void)
   2049 {
   2050 }
   2051 
   2052 /**********************************************
   2053 ** Simple Pairing Events
   2054 ***********************************************/
   2055 
   2056 /*******************************************************************************
   2057 **
   2058 ** Function         btu_hcif_host_support_evt
   2059 **
   2060 ** Description      Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
   2061 **
   2062 ** Returns          void
   2063 **
   2064 *******************************************************************************/
   2065 static void btu_hcif_host_support_evt (UINT8 *p)
   2066 {
   2067     btm_sec_rmt_host_support_feat_evt(p);
   2068 }
   2069 
   2070 /*******************************************************************************
   2071 **
   2072 ** Function         btu_hcif_io_cap_request_evt
   2073 **
   2074 ** Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
   2075 **
   2076 ** Returns          void
   2077 **
   2078 *******************************************************************************/
   2079 static void btu_hcif_io_cap_request_evt (UINT8 *p)
   2080 {
   2081     btm_io_capabilities_req(p);
   2082 }
   2083 
   2084 
   2085 /*******************************************************************************
   2086 **
   2087 ** Function         btu_hcif_io_cap_response_evt
   2088 **
   2089 ** Description      Process event HCI_IO_CAPABILITY_RESPONSE_EVT
   2090 **
   2091 ** Returns          void
   2092 **
   2093 *******************************************************************************/
   2094 static void btu_hcif_io_cap_response_evt (UINT8 *p)
   2095 {
   2096     btm_io_capabilities_rsp(p);
   2097 }
   2098 
   2099 
   2100 /*******************************************************************************
   2101 **
   2102 ** Function         btu_hcif_user_conf_request_evt
   2103 **
   2104 ** Description      Process event HCI_USER_CONFIRMATION_REQUEST_EVT
   2105 **
   2106 ** Returns          void
   2107 **
   2108 *******************************************************************************/
   2109 static void btu_hcif_user_conf_request_evt (UINT8 *p)
   2110 {
   2111     btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
   2112 }
   2113 
   2114 
   2115 /*******************************************************************************
   2116 **
   2117 ** Function         btu_hcif_user_passkey_request_evt
   2118 **
   2119 ** Description      Process event HCI_USER_PASSKEY_REQUEST_EVT
   2120 **
   2121 ** Returns          void
   2122 **
   2123 *******************************************************************************/
   2124 static void btu_hcif_user_passkey_request_evt (UINT8 *p)
   2125 {
   2126     btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
   2127 }
   2128 
   2129 /*******************************************************************************
   2130 **
   2131 ** Function         btu_hcif_user_passkey_notif_evt
   2132 **
   2133 ** Description      Process event HCI_USER_PASSKEY_NOTIFY_EVT
   2134 **
   2135 ** Returns          void
   2136 **
   2137 *******************************************************************************/
   2138 static void btu_hcif_user_passkey_notif_evt (UINT8 *p)
   2139 {
   2140     btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
   2141 }
   2142 
   2143 /*******************************************************************************
   2144 **
   2145 ** Function         btu_hcif_keypress_notif_evt
   2146 **
   2147 ** Description      Process event HCI_KEYPRESS_NOTIFY_EVT
   2148 **
   2149 ** Returns          void
   2150 **
   2151 *******************************************************************************/
   2152 static void btu_hcif_keypress_notif_evt (UINT8 *p)
   2153 {
   2154     btm_keypress_notif_evt(p);
   2155 }
   2156 
   2157 /*******************************************************************************
   2158 **
   2159 ** Function         btu_hcif_link_super_tout_evt
   2160 **
   2161 ** Description      Process event HCI_LINK_SUPER_TOUT_CHANGED_EVT
   2162 **
   2163 ** Returns          void
   2164 **
   2165 *******************************************************************************/
   2166 static void btu_hcif_link_super_tout_evt (UINT8 *p)
   2167 {
   2168     UINT16 handle, timeout;
   2169     STREAM_TO_UINT16 (handle, p);
   2170     STREAM_TO_UINT16 (timeout, p);
   2171 
   2172     btm_proc_lsto_evt(handle, timeout);
   2173 }
   2174 
   2175 /*******************************************************************************
   2176 **
   2177 ** Function         btu_hcif_rem_oob_request_evt
   2178 **
   2179 ** Description      Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
   2180 **
   2181 ** Returns          void
   2182 **
   2183 *******************************************************************************/
   2184     #if BTM_OOB_INCLUDED == TRUE
   2185 static void btu_hcif_rem_oob_request_evt (UINT8 *p)
   2186 {
   2187     btm_rem_oob_req(p);
   2188 }
   2189     #endif
   2190 
   2191 /*******************************************************************************
   2192 **
   2193 ** Function         btu_hcif_simple_pair_complete_evt
   2194 **
   2195 ** Description      Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
   2196 **
   2197 ** Returns          void
   2198 **
   2199 *******************************************************************************/
   2200 static void btu_hcif_simple_pair_complete_evt (UINT8 *p)
   2201 {
   2202     btm_simple_pair_complete(p);
   2203 }
   2204 /*******************************************************************************
   2205 **
   2206 ** Function         btu_hcif_flush_cmd_queue
   2207 **
   2208 ** Description      Flush the HCI command complete queue and transmit queue when
   2209 **                  needed.
   2210 **
   2211 ** Returns          void
   2212 **
   2213 *******************************************************************************/
   2214 void btu_hcif_flush_cmd_queue(void)
   2215 {
   2216     BT_HDR *p_cmd;
   2217 
   2218     btu_cb.hci_cmd_cb[0].cmd_window = 0;
   2219     while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_cmpl_q)) != NULL)
   2220     {
   2221         GKI_freebuf (p_cmd);
   2222     }
   2223     while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_xmit_q)) != NULL)
   2224     {
   2225         GKI_freebuf (p_cmd);
   2226     }
   2227 }
   2228 
   2229 /*******************************************************************************
   2230 **
   2231 ** Function         btu_hcif_enhanced_flush_complete_evt
   2232 **
   2233 ** Description      Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
   2234 **
   2235 ** Returns          void
   2236 **
   2237 *******************************************************************************/
   2238 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
   2239 static void btu_hcif_enhanced_flush_complete_evt (void)
   2240 {
   2241 /* This is empty until an upper layer cares about returning event */
   2242 }
   2243 #endif
   2244 /**********************************************
   2245 ** End of Simple Pairing Events
   2246 ***********************************************/
   2247 
   2248 
   2249 /**********************************************
   2250 ** BLE Events
   2251 ***********************************************/
   2252 #if (defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)
   2253 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p)
   2254 {
   2255     UINT8   status;
   2256     UINT8   enc_enable = 0;
   2257     UINT16  handle;
   2258 
   2259     STREAM_TO_UINT8  (status, p);
   2260     STREAM_TO_UINT16 (handle, p);
   2261 
   2262     if (status == HCI_SUCCESS) enc_enable = 1;
   2263 
   2264     btm_sec_encrypt_change (handle, status, enc_enable);
   2265 }
   2266 
   2267 static void btu_ble_process_adv_pkt (UINT8 *p)
   2268 {
   2269     HCI_TRACE_EVENT("btu_ble_process_adv_pkt");
   2270 
   2271     btm_ble_process_adv_pkt(p);
   2272 }
   2273 
   2274 static void btu_ble_ll_conn_complete_evt ( UINT8 *p, UINT16 evt_len)
   2275 {
   2276     btm_ble_conn_complete(p, evt_len);
   2277 }
   2278 
   2279 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p, UINT16 evt_len)
   2280 {
   2281     /* LE connection update has completed successfully as a master. */
   2282     /* We can enable the update request if the result is a success. */
   2283     /* extract the HCI handle first */
   2284     UINT8   status;
   2285     UINT16  handle;
   2286 
   2287     STREAM_TO_UINT8  (status, p);
   2288     STREAM_TO_UINT16 (handle, p);
   2289     l2cble_process_conn_update_evt(handle, status);
   2290 }
   2291 
   2292 static void btu_ble_read_remote_feat_evt (UINT8 *p)
   2293 {
   2294     btm_ble_read_remote_features_complete(p);
   2295 }
   2296 
   2297 static void btu_ble_proc_ltk_req (UINT8 *p)
   2298 {
   2299     UINT16 ediv, handle;
   2300     UINT8   *pp;
   2301 
   2302     STREAM_TO_UINT16(handle, p);
   2303     pp = p + 8;
   2304     STREAM_TO_UINT16(ediv, pp);
   2305 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
   2306     btm_ble_ltk_request(handle, p, ediv);
   2307 #endif
   2308     /* This is empty until an upper layer cares about returning event */
   2309 }
   2310 /**********************************************
   2311 ** End of BLE Events Handler
   2312 ***********************************************/
   2313 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
   2314 static void btu_ble_rc_param_req_evt(UINT8 *p)
   2315 {
   2316     UINT16 handle;
   2317     UINT16  int_min, int_max, latency, timeout;
   2318 
   2319     STREAM_TO_UINT16(handle, p);
   2320     STREAM_TO_UINT16(int_min, p);
   2321     STREAM_TO_UINT16(int_max, p);
   2322     STREAM_TO_UINT16(latency, p);
   2323     STREAM_TO_UINT16(timeout, p);
   2324 
   2325     l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency, timeout);
   2326 }
   2327 #endif /* BLE_LLT_INCLUDED */
   2328 
   2329 #endif /* BLE_INCLUDED */
   2330 
   2331