OSDN Git Service

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