OSDN Git Service

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