1/******************************************************************************
2 *
3 *  Copyright 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
52using tracked_objects::Location;
53
54extern void btm_process_cancel_complete(uint8_t status, uint8_t mode);
55extern 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/******************************************************************************/
60static void btu_hcif_inquiry_comp_evt(uint8_t* p);
61static void btu_hcif_inquiry_result_evt(uint8_t* p);
62static void btu_hcif_inquiry_rssi_result_evt(uint8_t* p);
63static void btu_hcif_extended_inquiry_result_evt(uint8_t* p);
64
65static void btu_hcif_connection_comp_evt(uint8_t* p);
66static void btu_hcif_connection_request_evt(uint8_t* p);
67static void btu_hcif_disconnection_comp_evt(uint8_t* p);
68static void btu_hcif_authentication_comp_evt(uint8_t* p);
69static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len);
70static void btu_hcif_encryption_change_evt(uint8_t* p);
71static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p);
72static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p);
73static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p);
74static void btu_hcif_qos_setup_comp_evt(uint8_t* p);
75static void btu_hcif_command_complete_evt(BT_HDR* response, void* context);
76static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
77                                        void* context);
78static void btu_hcif_hardware_error_evt(uint8_t* p);
79static void btu_hcif_flush_occured_evt(void);
80static void btu_hcif_role_change_evt(uint8_t* p);
81static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p);
82static void btu_hcif_mode_change_evt(uint8_t* p);
83static void btu_hcif_pin_code_request_evt(uint8_t* p);
84static void btu_hcif_link_key_request_evt(uint8_t* p);
85static void btu_hcif_link_key_notification_evt(uint8_t* p);
86static void btu_hcif_loopback_command_evt(void);
87static void btu_hcif_data_buf_overflow_evt(void);
88static void btu_hcif_max_slots_changed_evt(void);
89static void btu_hcif_read_clock_off_comp_evt(uint8_t* p);
90static void btu_hcif_conn_pkt_type_change_evt(void);
91static void btu_hcif_qos_violation_evt(uint8_t* p);
92static void btu_hcif_page_scan_mode_change_evt(void);
93static void btu_hcif_page_scan_rep_mode_chng_evt(void);
94static void btu_hcif_esco_connection_comp_evt(uint8_t* p);
95static void btu_hcif_esco_connection_chg_evt(uint8_t* p);
96
97/* Simple Pairing Events */
98static void btu_hcif_host_support_evt(uint8_t* p);
99static void btu_hcif_io_cap_request_evt(uint8_t* p);
100static void btu_hcif_io_cap_response_evt(uint8_t* p);
101static void btu_hcif_user_conf_request_evt(uint8_t* p);
102static void btu_hcif_user_passkey_request_evt(uint8_t* p);
103static void btu_hcif_user_passkey_notif_evt(uint8_t* p);
104static void btu_hcif_keypress_notif_evt(uint8_t* p);
105static void btu_hcif_rem_oob_request_evt(uint8_t* p);
106
107static void btu_hcif_simple_pair_complete_evt(uint8_t* p);
108#if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
109static void btu_hcif_enhanced_flush_complete_evt(void);
110#endif
111
112#if (BTM_SSR_INCLUDED == TRUE)
113static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len);
114#endif /* BTM_SSR_INCLUDED == TRUE */
115
116static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len);
117static void btu_ble_read_remote_feat_evt(uint8_t* p);
118static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len);
119static void btu_ble_proc_ltk_req(uint8_t* p);
120static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p);
121static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len);
122#if (BLE_LLT_INCLUDED == TRUE)
123static void btu_ble_rc_param_req_evt(uint8_t* p);
124#endif
125#if (BLE_PRIVACY_SPT == TRUE)
126static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len);
127#endif
128
129static 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 ******************************************************************************/
151void 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 ******************************************************************************/
377void 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
398using hci_cmd_cb = base::Callback<void(uint8_t* /* return_parameters */,
399                                       uint16_t /* return_parameters_length*/)>;
400
401struct cmd_with_cb_data {
402  hci_cmd_cb cb;
403  Location posted_from;
404};
405
406void 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
411void 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
416static 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
434static 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
441static 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
461static 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. */
477void 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 ******************************************************************************/
513static 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 ******************************************************************************/
531static 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 ******************************************************************************/
545static 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 ******************************************************************************/
559static 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 ******************************************************************************/
573static 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 (status != HCI_SUCCESS) {
592    HCI_TRACE_DEBUG(
593        "%s: Connection failed: status=%d, handle=%d, link_type=%d, "
594        "enc_mode=%d",
595        __func__, status, handle, link_type, enc_mode);
596  }
597
598  if (link_type == HCI_LINK_TYPE_ACL) {
599    btm_sec_connected(bda, handle, status, enc_mode);
600
601    l2c_link_hci_conn_comp(status, handle, bda);
602  }
603#if (BTM_SCO_INCLUDED == TRUE)
604  else {
605    memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
606    /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
607    esco_data.bd_addr = bda;
608    btm_sco_connected(status, &bda, handle, &esco_data);
609  }
610#endif /* BTM_SCO_INCLUDED */
611}
612
613/*******************************************************************************
614 *
615 * Function         btu_hcif_connection_request_evt
616 *
617 * Description      Process event HCI_CONNECTION_REQUEST_EVT
618 *
619 * Returns          void
620 *
621 ******************************************************************************/
622static void btu_hcif_connection_request_evt(uint8_t* p) {
623  RawAddress bda;
624  DEV_CLASS dc;
625  uint8_t link_type;
626
627  STREAM_TO_BDADDR(bda, p);
628  STREAM_TO_DEVCLASS(dc, p);
629  STREAM_TO_UINT8(link_type, p);
630
631  /* Pass request to security manager to check connect filters before */
632  /* passing request to l2cap */
633  if (link_type == HCI_LINK_TYPE_ACL) {
634    btm_sec_conn_req(bda, dc);
635  }
636#if (BTM_SCO_INCLUDED == TRUE)
637  else {
638    btm_sco_conn_req(bda, dc, link_type);
639  }
640#endif /* BTM_SCO_INCLUDED */
641}
642
643/*******************************************************************************
644 *
645 * Function         btu_hcif_disconnection_comp_evt
646 *
647 * Description      Process event HCI_DISCONNECTION_COMP_EVT
648 *
649 * Returns          void
650 *
651 ******************************************************************************/
652static void btu_hcif_disconnection_comp_evt(uint8_t* p) {
653  uint16_t handle;
654  uint8_t reason;
655
656  ++p;
657  STREAM_TO_UINT16(handle, p);
658  STREAM_TO_UINT8(reason, p);
659
660  handle = HCID_GET_HANDLE(handle);
661
662  if ((reason != HCI_ERR_CONN_CAUSE_LOCAL_HOST) &&
663      (reason != HCI_ERR_PEER_USER)) {
664    /* Uncommon disconnection reasons */
665    HCI_TRACE_DEBUG("%s: Got Disconn Complete Event: reason=%d, handle=%d",
666                    __func__, reason, handle);
667  }
668
669#if (BTM_SCO_INCLUDED == TRUE)
670  /* If L2CAP doesn't know about it, send it to SCO */
671  if (!l2c_link_hci_disc_comp(handle, reason)) btm_sco_removed(handle, reason);
672#else
673  l2c_link_hci_disc_comp(handle, reason);
674#endif /* BTM_SCO_INCLUDED */
675
676  /* Notify security manager */
677  btm_sec_disconnected(handle, reason);
678}
679
680/*******************************************************************************
681 *
682 * Function         btu_hcif_authentication_comp_evt
683 *
684 * Description      Process event HCI_AUTHENTICATION_COMP_EVT
685 *
686 * Returns          void
687 *
688 ******************************************************************************/
689static void btu_hcif_authentication_comp_evt(uint8_t* p) {
690  uint8_t status;
691  uint16_t handle;
692
693  STREAM_TO_UINT8(status, p);
694  STREAM_TO_UINT16(handle, p);
695
696  btm_sec_auth_complete(handle, status);
697}
698
699/*******************************************************************************
700 *
701 * Function         btu_hcif_rmt_name_request_comp_evt
702 *
703 * Description      Process event HCI_RMT_NAME_REQUEST_COMP_EVT
704 *
705 * Returns          void
706 *
707 ******************************************************************************/
708static void btu_hcif_rmt_name_request_comp_evt(uint8_t* p, uint16_t evt_len) {
709  uint8_t status;
710  RawAddress bd_addr;
711
712  STREAM_TO_UINT8(status, p);
713  STREAM_TO_BDADDR(bd_addr, p);
714
715  evt_len -= (1 + BD_ADDR_LEN);
716
717  btm_process_remote_name(&bd_addr, p, evt_len, status);
718
719  btm_sec_rmt_name_request_complete(&bd_addr, p, status);
720}
721
722/*******************************************************************************
723 *
724 * Function         btu_hcif_encryption_change_evt
725 *
726 * Description      Process event HCI_ENCRYPTION_CHANGE_EVT
727 *
728 * Returns          void
729 *
730 ******************************************************************************/
731static void btu_hcif_encryption_change_evt(uint8_t* p) {
732  uint8_t status;
733  uint16_t handle;
734  uint8_t encr_enable;
735
736  STREAM_TO_UINT8(status, p);
737  STREAM_TO_UINT16(handle, p);
738  STREAM_TO_UINT8(encr_enable, p);
739
740  btm_acl_encrypt_change(handle, status, encr_enable);
741  btm_sec_encrypt_change(handle, status, encr_enable);
742}
743
744/*******************************************************************************
745 *
746 * Function         btu_hcif_read_rmt_features_comp_evt
747 *
748 * Description      Process event HCI_READ_RMT_FEATURES_COMP_EVT
749 *
750 * Returns          void
751 *
752 ******************************************************************************/
753static void btu_hcif_read_rmt_features_comp_evt(uint8_t* p) {
754  btm_read_remote_features_complete(p);
755}
756
757/*******************************************************************************
758 *
759 * Function         btu_hcif_read_rmt_ext_features_comp_evt
760 *
761 * Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
762 *
763 * Returns          void
764 *
765 ******************************************************************************/
766static void btu_hcif_read_rmt_ext_features_comp_evt(uint8_t* p) {
767  uint8_t* p_cur = p;
768  uint8_t status;
769  uint16_t handle;
770
771  STREAM_TO_UINT8(status, p_cur);
772
773  if (status == HCI_SUCCESS)
774    btm_read_remote_ext_features_complete(p);
775  else {
776    STREAM_TO_UINT16(handle, p_cur);
777    btm_read_remote_ext_features_failed(status, handle);
778  }
779}
780
781/*******************************************************************************
782 *
783 * Function         btu_hcif_read_rmt_version_comp_evt
784 *
785 * Description      Process event HCI_READ_RMT_VERSION_COMP_EVT
786 *
787 * Returns          void
788 *
789 ******************************************************************************/
790static void btu_hcif_read_rmt_version_comp_evt(uint8_t* p) {
791  btm_read_remote_version_complete(p);
792}
793
794/*******************************************************************************
795 *
796 * Function         btu_hcif_qos_setup_comp_evt
797 *
798 * Description      Process event HCI_QOS_SETUP_COMP_EVT
799 *
800 * Returns          void
801 *
802 ******************************************************************************/
803static void btu_hcif_qos_setup_comp_evt(uint8_t* p) {
804  uint8_t status;
805  uint16_t handle;
806  FLOW_SPEC flow;
807
808  STREAM_TO_UINT8(status, p);
809  STREAM_TO_UINT16(handle, p);
810  STREAM_TO_UINT8(flow.qos_flags, p);
811  STREAM_TO_UINT8(flow.service_type, p);
812  STREAM_TO_UINT32(flow.token_rate, p);
813  STREAM_TO_UINT32(flow.peak_bandwidth, p);
814  STREAM_TO_UINT32(flow.latency, p);
815  STREAM_TO_UINT32(flow.delay_variation, p);
816
817  btm_qos_setup_complete(status, handle, &flow);
818}
819
820/*******************************************************************************
821 *
822 * Function         btu_hcif_esco_connection_comp_evt
823 *
824 * Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
825 *
826 * Returns          void
827 *
828 ******************************************************************************/
829static void btu_hcif_esco_connection_comp_evt(uint8_t* p) {
830#if (BTM_SCO_INCLUDED == TRUE)
831  tBTM_ESCO_DATA data;
832  uint16_t handle;
833  RawAddress bda;
834  uint8_t status;
835
836  STREAM_TO_UINT8(status, p);
837  STREAM_TO_UINT16(handle, p);
838  STREAM_TO_BDADDR(bda, p);
839
840  STREAM_TO_UINT8(data.link_type, p);
841  STREAM_TO_UINT8(data.tx_interval, p);
842  STREAM_TO_UINT8(data.retrans_window, p);
843  STREAM_TO_UINT16(data.rx_pkt_len, p);
844  STREAM_TO_UINT16(data.tx_pkt_len, p);
845  STREAM_TO_UINT8(data.air_mode, p);
846
847  data.bd_addr = bda;
848  btm_sco_connected(status, &bda, handle, &data);
849#endif
850}
851
852/*******************************************************************************
853 *
854 * Function         btu_hcif_esco_connection_chg_evt
855 *
856 * Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
857 *
858 * Returns          void
859 *
860 ******************************************************************************/
861static void btu_hcif_esco_connection_chg_evt(uint8_t* p) {
862#if (BTM_SCO_INCLUDED == TRUE)
863  uint16_t handle;
864  uint16_t tx_pkt_len;
865  uint16_t rx_pkt_len;
866  uint8_t status;
867  uint8_t tx_interval;
868  uint8_t retrans_window;
869
870  STREAM_TO_UINT8(status, p);
871  STREAM_TO_UINT16(handle, p);
872
873  STREAM_TO_UINT8(tx_interval, p);
874  STREAM_TO_UINT8(retrans_window, p);
875  STREAM_TO_UINT16(rx_pkt_len, p);
876  STREAM_TO_UINT16(tx_pkt_len, p);
877
878  btm_esco_proc_conn_chg(status, handle, tx_interval, retrans_window,
879                         rx_pkt_len, tx_pkt_len);
880#endif
881}
882
883/*******************************************************************************
884 *
885 * Function         btu_hcif_hdl_command_complete
886 *
887 * Description      Handle command complete event
888 *
889 * Returns          void
890 *
891 ******************************************************************************/
892static void btu_hcif_hdl_command_complete(uint16_t opcode, uint8_t* p,
893                                          uint16_t evt_len,
894                                          void* p_cplt_cback) {
895  switch (opcode) {
896    case HCI_INQUIRY_CANCEL:
897      /* Tell inquiry processing that we are done */
898      btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
899      break;
900    case HCI_SET_EVENT_FILTER:
901      btm_event_filter_complete(p);
902      break;
903
904    case HCI_DELETE_STORED_LINK_KEY:
905      btm_delete_stored_link_key_complete(p);
906      break;
907
908    case HCI_READ_LOCAL_NAME:
909      btm_read_local_name_complete(p, evt_len);
910      break;
911
912    case HCI_GET_LINK_QUALITY:
913      btm_read_link_quality_complete(p);
914      break;
915
916    case HCI_READ_RSSI:
917      btm_read_rssi_complete(p);
918      break;
919
920    case HCI_READ_FAILED_CONTACT_COUNTER:
921      btm_read_failed_contact_counter_complete(p);
922      break;
923
924    case HCI_READ_AUTOMATIC_FLUSH_TIMEOUT:
925      btm_read_automatic_flush_timeout_complete(p);
926      break;
927
928    case HCI_READ_TRANSMIT_POWER_LEVEL:
929      btm_read_tx_power_complete(p, false);
930      break;
931
932    case HCI_CREATE_CONNECTION_CANCEL:
933      btm_create_conn_cancel_complete(p);
934      break;
935
936    case HCI_READ_LOCAL_OOB_DATA:
937      btm_read_local_oob_complete(p);
938      break;
939
940    case HCI_READ_INQ_TX_POWER_LEVEL:
941      btm_read_inq_tx_power_complete(p);
942      break;
943
944    /* BLE Commands sComplete*/
945    case HCI_BLE_ADD_WHITE_LIST:
946      btm_ble_add_2_white_list_complete(*p);
947      break;
948
949    case HCI_BLE_CLEAR_WHITE_LIST:
950      btm_ble_clear_white_list_complete(p, evt_len);
951      break;
952
953    case HCI_BLE_REMOVE_WHITE_LIST:
954      btm_ble_remove_from_white_list_complete(p, evt_len);
955      break;
956
957    case HCI_BLE_RAND:
958    case HCI_BLE_ENCRYPT:
959      btm_ble_rand_enc_complete(p, opcode, (tBTM_RAND_ENC_CB*)p_cplt_cback);
960      break;
961
962    case HCI_BLE_READ_ADV_CHNL_TX_POWER:
963      btm_read_tx_power_complete(p, true);
964      break;
965
966    case HCI_BLE_WRITE_ADV_ENABLE:
967      btm_ble_write_adv_enable_complete(p);
968      break;
969
970    case HCI_BLE_CREATE_LL_CONN:
971    case HCI_LE_EXTENDED_CREATE_CONNECTION:
972      // No command complete event for those commands according to spec
973      LOG(ERROR) << "No command complete expected, but received!";
974      break;
975
976    case HCI_BLE_CREATE_CONN_CANCEL:
977      btm_ble_create_conn_cancel_complete(p);
978      break;
979
980    case HCI_BLE_TRANSMITTER_TEST:
981    case HCI_BLE_RECEIVER_TEST:
982    case HCI_BLE_TEST_END:
983      btm_ble_test_command_complete(p);
984      break;
985
986#if (BLE_PRIVACY_SPT == TRUE)
987    case HCI_BLE_ADD_DEV_RESOLVING_LIST:
988      btm_ble_add_resolving_list_entry_complete(p, evt_len);
989      break;
990
991    case HCI_BLE_RM_DEV_RESOLVING_LIST:
992      btm_ble_remove_resolving_list_entry_complete(p, evt_len);
993      break;
994
995    case HCI_BLE_CLEAR_RESOLVING_LIST:
996      btm_ble_clear_resolving_list_complete(p, evt_len);
997      break;
998
999    case HCI_BLE_READ_RESOLVABLE_ADDR_PEER:
1000      btm_ble_read_resolving_list_entry_complete(p, evt_len);
1001      break;
1002
1003    case HCI_BLE_READ_RESOLVABLE_ADDR_LOCAL:
1004    case HCI_BLE_SET_ADDR_RESOLUTION_ENABLE:
1005    case HCI_BLE_SET_RAND_PRIV_ADDR_TIMOUT:
1006      break;
1007#endif
1008    default:
1009      if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1010        btm_vsc_complete(p, opcode, evt_len, (tBTM_VSC_CMPL_CB*)p_cplt_cback);
1011      break;
1012  }
1013}
1014
1015/*******************************************************************************
1016 *
1017 * Function         btu_hcif_command_complete_evt
1018 *
1019 * Description      Process event HCI_COMMAND_COMPLETE_EVT
1020 *
1021 * Returns          void
1022 *
1023 ******************************************************************************/
1024static void btu_hcif_command_complete_evt_on_task(BT_HDR* event,
1025                                                  void* context) {
1026  command_opcode_t opcode;
1027  uint8_t* stream =
1028      event->data + event->offset +
1029      3;  // 2 to skip the event headers, 1 to skip the command credits
1030  STREAM_TO_UINT16(opcode, stream);
1031
1032  btu_hcif_hdl_command_complete(
1033      opcode, stream,
1034      event->len -
1035          5,  // 3 for the command complete headers, 2 for the event headers
1036      context);
1037
1038  osi_free(event);
1039}
1040
1041static void btu_hcif_command_complete_evt(BT_HDR* response, void* context) {
1042  do_in_hci_thread(FROM_HERE, base::Bind(btu_hcif_command_complete_evt_on_task,
1043                                         response, context));
1044}
1045
1046/*******************************************************************************
1047 *
1048 * Function         btu_hcif_hdl_command_status
1049 *
1050 * Description      Handle a command status event
1051 *
1052 * Returns          void
1053 *
1054 ******************************************************************************/
1055static void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status,
1056                                        uint8_t* p_cmd,
1057                                        void* p_vsc_status_cback) {
1058  RawAddress bd_addr;
1059  uint16_t handle;
1060#if (BTM_SCO_INCLUDED == TRUE)
1061  tBTM_ESCO_DATA esco_data;
1062#endif
1063
1064  switch (opcode) {
1065    case HCI_EXIT_SNIFF_MODE:
1066    case HCI_EXIT_PARK_MODE:
1067#if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
1068      if (status != HCI_SUCCESS) {
1069        /* Allow SCO initiation to continue if waiting for change mode event */
1070        if (p_cmd != NULL) {
1071          p_cmd++; /* bypass length field */
1072          STREAM_TO_UINT16(handle, p_cmd);
1073          btm_sco_chk_pend_unpark(status, handle);
1074        }
1075      }
1076#endif
1077    /* Case Falls Through */
1078
1079    case HCI_HOLD_MODE:
1080    case HCI_SNIFF_MODE:
1081    case HCI_PARK_MODE:
1082      btm_pm_proc_cmd_status(status);
1083      break;
1084
1085    default:
1086      /* If command failed to start, we may need to tell BTM */
1087      if (status != HCI_SUCCESS) {
1088        switch (opcode) {
1089          case HCI_INQUIRY:
1090            /* Tell inquiry processing that we are done */
1091            btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1092            break;
1093
1094          case HCI_RMT_NAME_REQUEST:
1095            /* Tell inquiry processing that we are done */
1096            btm_process_remote_name(NULL, NULL, 0, status);
1097
1098            btm_sec_rmt_name_request_complete(NULL, NULL, status);
1099            break;
1100
1101          case HCI_QOS_SETUP_COMP_EVT:
1102            /* Tell qos setup that we are done */
1103            btm_qos_setup_complete(status, 0, NULL);
1104            break;
1105
1106          case HCI_SWITCH_ROLE:
1107            /* Tell BTM that the command failed */
1108            /* read bd addr out of stored command */
1109            if (p_cmd != NULL) {
1110              p_cmd++;
1111              STREAM_TO_BDADDR(bd_addr, p_cmd);
1112              btm_acl_role_changed(status, &bd_addr, BTM_ROLE_UNDEFINED);
1113            } else
1114              btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
1115            l2c_link_role_changed(nullptr, BTM_ROLE_UNDEFINED,
1116                                  HCI_ERR_COMMAND_DISALLOWED);
1117            break;
1118
1119          case HCI_CREATE_CONNECTION:
1120            /* read bd addr out of stored command */
1121            if (p_cmd != NULL) {
1122              p_cmd++;
1123              STREAM_TO_BDADDR(bd_addr, p_cmd);
1124              btm_sec_connected(bd_addr, HCI_INVALID_HANDLE, status, 0);
1125              l2c_link_hci_conn_comp(status, HCI_INVALID_HANDLE, bd_addr);
1126            }
1127            break;
1128
1129          case HCI_READ_RMT_EXT_FEATURES:
1130            if (p_cmd != NULL) {
1131              p_cmd++; /* skip command length */
1132              STREAM_TO_UINT16(handle, p_cmd);
1133            } else
1134              handle = HCI_INVALID_HANDLE;
1135
1136            btm_read_remote_ext_features_failed(status, handle);
1137            break;
1138
1139          case HCI_AUTHENTICATION_REQUESTED:
1140            /* Device refused to start authentication.  That should be treated
1141             * as authentication failure. */
1142            btm_sec_auth_complete(BTM_INVALID_HCI_HANDLE, status);
1143            break;
1144
1145          case HCI_SET_CONN_ENCRYPTION:
1146            /* Device refused to start encryption.  That should be treated as
1147             * encryption failure. */
1148            btm_sec_encrypt_change(BTM_INVALID_HCI_HANDLE, status, false);
1149            break;
1150
1151          case HCI_BLE_CREATE_LL_CONN:
1152          case HCI_LE_EXTENDED_CREATE_CONNECTION:
1153            btm_ble_create_ll_conn_complete(status);
1154            break;
1155
1156#if (BTM_SCO_INCLUDED == TRUE)
1157          case HCI_SETUP_ESCO_CONNECTION:
1158          case HCI_ENH_SETUP_ESCO_CONNECTION:
1159            /* read handle out of stored command */
1160            if (p_cmd != NULL) {
1161              p_cmd++;
1162              STREAM_TO_UINT16(handle, p_cmd);
1163
1164              /* Determine if initial connection failed or is a change
1165               * of setup */
1166              if (btm_is_sco_active(handle))
1167                btm_esco_proc_conn_chg(status, handle, 0, 0, 0, 0);
1168              else
1169                btm_sco_connected(status, NULL, handle, &esco_data);
1170            }
1171            break;
1172#endif
1173
1174          /* This is commented out until an upper layer cares about returning
1175          event
1176          #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
1177                      case HCI_ENHANCED_FLUSH:
1178                          break;
1179          #endif
1180          */
1181          default:
1182            if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1183              btm_vsc_complete(&status, opcode, 1,
1184                               (tBTM_VSC_CMPL_CB*)p_vsc_status_cback);
1185            break;
1186        }
1187
1188      } else {
1189        if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1190          btm_vsc_complete(&status, opcode, 1,
1191                           (tBTM_VSC_CMPL_CB*)p_vsc_status_cback);
1192      }
1193  }
1194}
1195
1196/*******************************************************************************
1197 *
1198 * Function         btu_hcif_command_status_evt
1199 *
1200 * Description      Process event HCI_COMMAND_STATUS_EVT
1201 *
1202 * Returns          void
1203 *
1204 ******************************************************************************/
1205static void btu_hcif_command_status_evt_on_task(uint8_t status, BT_HDR* event,
1206                                                void* context) {
1207  command_opcode_t opcode;
1208  uint8_t* stream = event->data + event->offset;
1209  STREAM_TO_UINT16(opcode, stream);
1210
1211  btu_hcif_hdl_command_status(opcode, status, stream, context);
1212  osi_free(event);
1213}
1214
1215static void btu_hcif_command_status_evt(uint8_t status, BT_HDR* command,
1216                                        void* context) {
1217  do_in_hci_thread(FROM_HERE, base::Bind(btu_hcif_command_status_evt_on_task,
1218                                         status, command, context));
1219}
1220
1221/*******************************************************************************
1222 *
1223 * Function         btu_hcif_hardware_error_evt
1224 *
1225 * Description      Process event HCI_HARDWARE_ERROR_EVT
1226 *
1227 * Returns          void
1228 *
1229 ******************************************************************************/
1230static void btu_hcif_hardware_error_evt(uint8_t* p) {
1231  HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1232
1233  /* If anyone wants device status notifications, give him one. */
1234  btm_report_device_status(BTM_DEV_STATUS_DOWN);
1235
1236  /* Reset the controller */
1237  if (BTM_IsDeviceUp()) BTM_DeviceReset(NULL);
1238}
1239
1240/*******************************************************************************
1241 *
1242 * Function         btu_hcif_flush_occured_evt
1243 *
1244 * Description      Process event HCI_FLUSH_OCCURED_EVT
1245 *
1246 * Returns          void
1247 *
1248 ******************************************************************************/
1249static void btu_hcif_flush_occured_evt(void) {}
1250
1251/*******************************************************************************
1252 *
1253 * Function         btu_hcif_role_change_evt
1254 *
1255 * Description      Process event HCI_ROLE_CHANGE_EVT
1256 *
1257 * Returns          void
1258 *
1259 ******************************************************************************/
1260static void btu_hcif_role_change_evt(uint8_t* p) {
1261  uint8_t status;
1262  RawAddress bda;
1263  uint8_t role;
1264
1265  STREAM_TO_UINT8(status, p);
1266  STREAM_TO_BDADDR(bda, p);
1267  STREAM_TO_UINT8(role, p);
1268
1269  btm_blacklist_role_change_device(bda, status);
1270  l2c_link_role_changed(&bda, role, status);
1271  btm_acl_role_changed(status, &bda, role);
1272}
1273
1274/*******************************************************************************
1275 *
1276 * Function         btu_hcif_num_compl_data_pkts_evt
1277 *
1278 * Description      Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1279 *
1280 * Returns          void
1281 *
1282 ******************************************************************************/
1283static void btu_hcif_num_compl_data_pkts_evt(uint8_t* p) {
1284  /* Process for L2CAP and SCO */
1285  l2c_link_process_num_completed_pkts(p);
1286
1287  /* Send on to SCO */
1288  /*?? No SCO for now */
1289}
1290
1291/*******************************************************************************
1292 *
1293 * Function         btu_hcif_mode_change_evt
1294 *
1295 * Description      Process event HCI_MODE_CHANGE_EVT
1296 *
1297 * Returns          void
1298 *
1299 ******************************************************************************/
1300static void btu_hcif_mode_change_evt(uint8_t* p) {
1301  uint8_t status;
1302  uint16_t handle;
1303  uint8_t current_mode;
1304  uint16_t interval;
1305
1306  STREAM_TO_UINT8(status, p);
1307
1308  STREAM_TO_UINT16(handle, p);
1309  STREAM_TO_UINT8(current_mode, p);
1310  STREAM_TO_UINT16(interval, p);
1311#if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
1312  btm_sco_chk_pend_unpark(status, handle);
1313#endif
1314  btm_pm_proc_mode_change(status, handle, current_mode, interval);
1315
1316#if (HID_DEV_INCLUDED == TRUE && HID_DEV_PM_INCLUDED == TRUE)
1317  hidd_pm_proc_mode_change(status, current_mode, interval);
1318#endif
1319}
1320
1321/*******************************************************************************
1322 *
1323 * Function         btu_hcif_ssr_evt
1324 *
1325 * Description      Process event HCI_SNIFF_SUB_RATE_EVT
1326 *
1327 * Returns          void
1328 *
1329 ******************************************************************************/
1330#if (BTM_SSR_INCLUDED == TRUE)
1331static void btu_hcif_ssr_evt(uint8_t* p, uint16_t evt_len) {
1332  btm_pm_proc_ssr_evt(p, evt_len);
1333}
1334#endif
1335
1336/*******************************************************************************
1337 *
1338 * Function         btu_hcif_pin_code_request_evt
1339 *
1340 * Description      Process event HCI_PIN_CODE_REQUEST_EVT
1341 *
1342 * Returns          void
1343 *
1344 ******************************************************************************/
1345static void btu_hcif_pin_code_request_evt(uint8_t* p) {
1346  RawAddress bda;
1347
1348  STREAM_TO_BDADDR(bda, p);
1349
1350  /* Tell L2CAP that there was a PIN code request,  */
1351  /* it may need to stretch timeouts                */
1352  l2c_pin_code_request(bda);
1353
1354  btm_sec_pin_code_request(bda);
1355}
1356
1357/*******************************************************************************
1358 *
1359 * Function         btu_hcif_link_key_request_evt
1360 *
1361 * Description      Process event HCI_LINK_KEY_REQUEST_EVT
1362 *
1363 * Returns          void
1364 *
1365 ******************************************************************************/
1366static void btu_hcif_link_key_request_evt(uint8_t* p) {
1367  RawAddress bda;
1368
1369  STREAM_TO_BDADDR(bda, p);
1370  btm_sec_link_key_request(bda);
1371}
1372
1373/*******************************************************************************
1374 *
1375 * Function         btu_hcif_link_key_notification_evt
1376 *
1377 * Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
1378 *
1379 * Returns          void
1380 *
1381 ******************************************************************************/
1382static void btu_hcif_link_key_notification_evt(uint8_t* p) {
1383  RawAddress bda;
1384  LINK_KEY key;
1385  uint8_t key_type;
1386
1387  STREAM_TO_BDADDR(bda, p);
1388  STREAM_TO_ARRAY16(key, p);
1389  STREAM_TO_UINT8(key_type, p);
1390
1391  btm_sec_link_key_notification(bda, key, key_type);
1392}
1393
1394/*******************************************************************************
1395 *
1396 * Function         btu_hcif_loopback_command_evt
1397 *
1398 * Description      Process event HCI_LOOPBACK_COMMAND_EVT
1399 *
1400 * Returns          void
1401 *
1402 ******************************************************************************/
1403static void btu_hcif_loopback_command_evt(void) {}
1404
1405/*******************************************************************************
1406 *
1407 * Function         btu_hcif_data_buf_overflow_evt
1408 *
1409 * Description      Process event HCI_DATA_BUF_OVERFLOW_EVT
1410 *
1411 * Returns          void
1412 *
1413 ******************************************************************************/
1414static void btu_hcif_data_buf_overflow_evt(void) {}
1415
1416/*******************************************************************************
1417 *
1418 * Function         btu_hcif_max_slots_changed_evt
1419 *
1420 * Description      Process event HCI_MAX_SLOTS_CHANGED_EVT
1421 *
1422 * Returns          void
1423 *
1424 ******************************************************************************/
1425static void btu_hcif_max_slots_changed_evt(void) {}
1426
1427/*******************************************************************************
1428 *
1429 * Function         btu_hcif_read_clock_off_comp_evt
1430 *
1431 * Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
1432 *
1433 * Returns          void
1434 *
1435 ******************************************************************************/
1436static void btu_hcif_read_clock_off_comp_evt(uint8_t* p) {
1437  uint8_t status;
1438  uint16_t handle;
1439  uint16_t clock_offset;
1440
1441  STREAM_TO_UINT8(status, p);
1442
1443  /* If failed to get clock offset just drop the result */
1444  if (status != HCI_SUCCESS) return;
1445
1446  STREAM_TO_UINT16(handle, p);
1447  STREAM_TO_UINT16(clock_offset, p);
1448
1449  handle = HCID_GET_HANDLE(handle);
1450
1451  btm_process_clk_off_comp_evt(handle, clock_offset);
1452  btm_sec_update_clock_offset(handle, clock_offset);
1453}
1454
1455/*******************************************************************************
1456 *
1457 * Function         btu_hcif_conn_pkt_type_change_evt
1458 *
1459 * Description      Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1460 *
1461 * Returns          void
1462 *
1463 ******************************************************************************/
1464static void btu_hcif_conn_pkt_type_change_evt(void) {}
1465
1466/*******************************************************************************
1467 *
1468 * Function         btu_hcif_qos_violation_evt
1469 *
1470 * Description      Process event HCI_QOS_VIOLATION_EVT
1471 *
1472 * Returns          void
1473 *
1474 ******************************************************************************/
1475static void btu_hcif_qos_violation_evt(uint8_t* p) {
1476  uint16_t handle;
1477
1478  STREAM_TO_UINT16(handle, p);
1479
1480  handle = HCID_GET_HANDLE(handle);
1481
1482  l2c_link_hci_qos_violation(handle);
1483}
1484
1485/*******************************************************************************
1486 *
1487 * Function         btu_hcif_page_scan_mode_change_evt
1488 *
1489 * Description      Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
1490 *
1491 * Returns          void
1492 *
1493 ******************************************************************************/
1494static void btu_hcif_page_scan_mode_change_evt(void) {}
1495
1496/*******************************************************************************
1497 *
1498 * Function         btu_hcif_page_scan_rep_mode_chng_evt
1499 *
1500 * Description      Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
1501 *
1502 * Returns          void
1503 *
1504 ******************************************************************************/
1505static void btu_hcif_page_scan_rep_mode_chng_evt(void) {}
1506
1507/**********************************************
1508 * Simple Pairing Events
1509 **********************************************/
1510
1511/*******************************************************************************
1512 *
1513 * Function         btu_hcif_host_support_evt
1514 *
1515 * Description      Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
1516 *
1517 * Returns          void
1518 *
1519 ******************************************************************************/
1520static void btu_hcif_host_support_evt(uint8_t* p) {
1521  btm_sec_rmt_host_support_feat_evt(p);
1522}
1523
1524/*******************************************************************************
1525 *
1526 * Function         btu_hcif_io_cap_request_evt
1527 *
1528 * Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
1529 *
1530 * Returns          void
1531 *
1532 ******************************************************************************/
1533static void btu_hcif_io_cap_request_evt(uint8_t* p) {
1534  RawAddress bda;
1535  STREAM_TO_BDADDR(bda, p);
1536  btm_io_capabilities_req(bda);
1537}
1538
1539/*******************************************************************************
1540 *
1541 * Function         btu_hcif_io_cap_response_evt
1542 *
1543 * Description      Process event HCI_IO_CAPABILITY_RESPONSE_EVT
1544 *
1545 * Returns          void
1546 *
1547 ******************************************************************************/
1548static void btu_hcif_io_cap_response_evt(uint8_t* p) {
1549  btm_io_capabilities_rsp(p);
1550}
1551
1552/*******************************************************************************
1553 *
1554 * Function         btu_hcif_user_conf_request_evt
1555 *
1556 * Description      Process event HCI_USER_CONFIRMATION_REQUEST_EVT
1557 *
1558 * Returns          void
1559 *
1560 ******************************************************************************/
1561static void btu_hcif_user_conf_request_evt(uint8_t* p) {
1562  btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
1563}
1564
1565/*******************************************************************************
1566 *
1567 * Function         btu_hcif_user_passkey_request_evt
1568 *
1569 * Description      Process event HCI_USER_PASSKEY_REQUEST_EVT
1570 *
1571 * Returns          void
1572 *
1573 ******************************************************************************/
1574static void btu_hcif_user_passkey_request_evt(uint8_t* p) {
1575  btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
1576}
1577
1578/*******************************************************************************
1579 *
1580 * Function         btu_hcif_user_passkey_notif_evt
1581 *
1582 * Description      Process event HCI_USER_PASSKEY_NOTIFY_EVT
1583 *
1584 * Returns          void
1585 *
1586 ******************************************************************************/
1587static void btu_hcif_user_passkey_notif_evt(uint8_t* p) {
1588  btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
1589}
1590
1591/*******************************************************************************
1592 *
1593 * Function         btu_hcif_keypress_notif_evt
1594 *
1595 * Description      Process event HCI_KEYPRESS_NOTIFY_EVT
1596 *
1597 * Returns          void
1598 *
1599 ******************************************************************************/
1600static void btu_hcif_keypress_notif_evt(uint8_t* p) {
1601  btm_keypress_notif_evt(p);
1602}
1603
1604/*******************************************************************************
1605 *
1606 * Function         btu_hcif_rem_oob_request_evt
1607 *
1608 * Description      Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
1609 *
1610 * Returns          void
1611 *
1612 ******************************************************************************/
1613static void btu_hcif_rem_oob_request_evt(uint8_t* p) { btm_rem_oob_req(p); }
1614
1615/*******************************************************************************
1616 *
1617 * Function         btu_hcif_simple_pair_complete_evt
1618 *
1619 * Description      Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
1620 *
1621 * Returns          void
1622 *
1623 ******************************************************************************/
1624static void btu_hcif_simple_pair_complete_evt(uint8_t* p) {
1625  btm_simple_pair_complete(p);
1626}
1627
1628/*******************************************************************************
1629 *
1630 * Function         btu_hcif_enhanced_flush_complete_evt
1631 *
1632 * Description      Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
1633 *
1634 * Returns          void
1635 *
1636 ******************************************************************************/
1637#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
1638static void btu_hcif_enhanced_flush_complete_evt(void) {
1639  /* This is empty until an upper layer cares about returning event */
1640}
1641#endif
1642/**********************************************
1643 * End of Simple Pairing Events
1644 **********************************************/
1645
1646/**********************************************
1647 * BLE Events
1648 **********************************************/
1649static void btu_hcif_encryption_key_refresh_cmpl_evt(uint8_t* p) {
1650  uint8_t status;
1651  uint8_t enc_enable = 0;
1652  uint16_t handle;
1653
1654  STREAM_TO_UINT8(status, p);
1655  STREAM_TO_UINT16(handle, p);
1656
1657  if (status == HCI_SUCCESS) enc_enable = 1;
1658
1659  btm_sec_encrypt_change(handle, status, enc_enable);
1660}
1661
1662static void btu_ble_ll_conn_complete_evt(uint8_t* p, uint16_t evt_len) {
1663  btm_ble_conn_complete(p, evt_len, false);
1664}
1665#if (BLE_PRIVACY_SPT == TRUE)
1666static void btu_ble_proc_enhanced_conn_cmpl(uint8_t* p, uint16_t evt_len) {
1667  btm_ble_conn_complete(p, evt_len, true);
1668}
1669#endif
1670
1671extern void gatt_notify_conn_update(uint16_t handle, uint16_t interval,
1672                                    uint16_t latency, uint16_t timeout,
1673                                    uint8_t status);
1674
1675static void btu_ble_ll_conn_param_upd_evt(uint8_t* p, uint16_t evt_len) {
1676  /* LE connection update has completed successfully as a master. */
1677  /* We can enable the update request if the result is a success. */
1678  /* extract the HCI handle first */
1679  uint8_t status;
1680  uint16_t handle;
1681  uint16_t interval;
1682  uint16_t latency;
1683  uint16_t timeout;
1684
1685  STREAM_TO_UINT8(status, p);
1686  STREAM_TO_UINT16(handle, p);
1687  STREAM_TO_UINT16(interval, p);
1688  STREAM_TO_UINT16(latency, p);
1689  STREAM_TO_UINT16(timeout, p);
1690
1691  l2cble_process_conn_update_evt(handle, status, interval, latency, timeout);
1692
1693  gatt_notify_conn_update(handle & 0x0FFF, interval, latency, timeout, status);
1694}
1695
1696static void btu_ble_read_remote_feat_evt(uint8_t* p) {
1697  btm_ble_read_remote_features_complete(p);
1698}
1699
1700static void btu_ble_proc_ltk_req(uint8_t* p) {
1701  uint16_t ediv, handle;
1702  uint8_t* pp;
1703
1704  STREAM_TO_UINT16(handle, p);
1705  pp = p + 8;
1706  STREAM_TO_UINT16(ediv, pp);
1707  btm_ble_ltk_request(handle, p, ediv);
1708  /* This is empty until an upper layer cares about returning event */
1709}
1710
1711static void btu_ble_data_length_change_evt(uint8_t* p, uint16_t evt_len) {
1712  uint16_t handle;
1713  uint16_t tx_data_len;
1714  uint16_t rx_data_len;
1715
1716  if (!controller_get_interface()->supports_ble_packet_extension()) {
1717    HCI_TRACE_WARNING("%s, request not supported", __func__);
1718    return;
1719  }
1720
1721  STREAM_TO_UINT16(handle, p);
1722  STREAM_TO_UINT16(tx_data_len, p);
1723  p += 2; /* Skip the TxTimer */
1724  STREAM_TO_UINT16(rx_data_len, p);
1725
1726  l2cble_process_data_length_change_event(handle, tx_data_len, rx_data_len);
1727}
1728
1729/**********************************************
1730 * End of BLE Events Handler
1731 **********************************************/
1732#if (BLE_LLT_INCLUDED == TRUE)
1733static void btu_ble_rc_param_req_evt(uint8_t* p) {
1734  uint16_t handle;
1735  uint16_t int_min, int_max, latency, timeout;
1736
1737  STREAM_TO_UINT16(handle, p);
1738  STREAM_TO_UINT16(int_min, p);
1739  STREAM_TO_UINT16(int_max, p);
1740  STREAM_TO_UINT16(latency, p);
1741  STREAM_TO_UINT16(timeout, p);
1742
1743  l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency,
1744                                      timeout);
1745}
1746#endif /* BLE_LLT_INCLUDED */
1747