OSDN Git Service

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