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