OSDN Git Service

Remove unused #define BTU_CMD_CMPL_TOUT_DOUBLE_CHECK
[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 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "gki.h"
33 #include "bt_types.h"
34 #include "hcimsgs.h"
35 #include "btu.h"
36 #include "l2c_int.h"
37 #include "btm_api.h"
38 #include "btm_int.h"
39
40 extern void btm_process_cancel_complete(UINT8 status, UINT8 mode);
41 extern void btm_ble_test_command_complete(UINT8 *p);
42
43 // btla-specific ++
44 #define LOG_TAG "BTLD"
45 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE) && (!defined(LINUX_NATIVE)) )
46 #include <cutils/log.h>
47 #else
48 #define LOGV(format, ...)  fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__)
49 #define LOGE(format, ...)  fprintf (stderr, LOG_TAG format"\n", ## __VA_ARGS__)
50 #define LOGI(format, ...)  fprintf (stdout, LOG_TAG format"\n", ## __VA_ARGS__)
51 #endif
52
53 // btla-specific ++
54 /* BTE application task */
55 #if APPL_INCLUDED == TRUE
56 #include "bte_appl.h"
57 #endif
58 // btla-specific --
59
60 //Counter to track number of HCI command timeout
61 static int num_hci_cmds_timed_out;
62
63 /********************************************************************************/
64 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
65 /********************************************************************************/
66 static void btu_hcif_inquiry_comp_evt (UINT8 *p);
67 static void btu_hcif_inquiry_result_evt (UINT8 *p);
68 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p);
69 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
70 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p);
71 #endif
72
73 static void btu_hcif_connection_comp_evt (UINT8 *p);
74 static void btu_hcif_connection_request_evt (UINT8 *p);
75 static void btu_hcif_disconnection_comp_evt (UINT8 *p);
76 static void btu_hcif_authentication_comp_evt (UINT8 *p);
77 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len);
78 static void btu_hcif_encryption_change_evt (UINT8 *p);
79 static void btu_hcif_change_conn_link_key_evt (UINT8 *p);
80 static void btu_hcif_master_link_key_comp_evt (UINT8 *p);
81 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p);
82 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p);
83 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p);
84 static void btu_hcif_qos_setup_comp_evt (UINT8 *p);
85 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len);
86 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p);
87 static void btu_hcif_hardware_error_evt (UINT8 *p);
88 static void btu_hcif_flush_occured_evt (void);
89 static void btu_hcif_role_change_evt (UINT8 *p);
90 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p);
91 static void btu_hcif_mode_change_evt (UINT8 *p);
92 static void btu_hcif_return_link_keys_evt (UINT8 *p);
93 static void btu_hcif_pin_code_request_evt (UINT8 *p);
94 static void btu_hcif_link_key_request_evt (UINT8 *p);
95 static void btu_hcif_link_key_notification_evt (UINT8 *p);
96 static void btu_hcif_loopback_command_evt (void);
97 static void btu_hcif_data_buf_overflow_evt (void);
98 static void btu_hcif_max_slots_changed_evt (void);
99 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p);
100 static void btu_hcif_conn_pkt_type_change_evt (void);
101 static void btu_hcif_qos_violation_evt (UINT8 *p);
102 static void btu_hcif_page_scan_mode_change_evt (void);
103 static void btu_hcif_page_scan_rep_mode_chng_evt (void);
104 static void btu_hcif_esco_connection_comp_evt(UINT8 *p);
105 static void btu_hcif_esco_connection_chg_evt(UINT8 *p);
106
107 /* Simple Pairing Events */
108 static void btu_hcif_host_support_evt (UINT8 *p);
109 static void btu_hcif_io_cap_request_evt (UINT8 *p);
110 static void btu_hcif_io_cap_response_evt (UINT8 *p);
111 static void btu_hcif_user_conf_request_evt (UINT8 *p);
112 static void btu_hcif_user_passkey_request_evt (UINT8 *p);
113 static void btu_hcif_user_passkey_notif_evt (UINT8 *p);
114 static void btu_hcif_keypress_notif_evt (UINT8 *p);
115 static void btu_hcif_link_super_tout_evt (UINT8 *p);
116
117     #if BTM_OOB_INCLUDED == TRUE
118 static void btu_hcif_rem_oob_request_evt (UINT8 *p);
119     #endif
120
121 static void btu_hcif_simple_pair_complete_evt (UINT8 *p);
122     #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
123 static void btu_hcif_enhanced_flush_complete_evt (void);
124     #endif
125
126     #if (BTM_SSR_INCLUDED == TRUE)
127 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len);
128     #endif /* BTM_SSR_INCLUDED == TRUE */
129
130     #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
131 extern void hidd_pm_proc_mode_change( UINT8 hci_status, UINT8 mode, UINT16 interval );
132     #endif
133
134
135     #if BLE_INCLUDED == TRUE
136 static void btu_ble_ll_conn_complete_evt (UINT8 *p, UINT16 evt_len);
137 static void btu_ble_process_adv_pkt (UINT8 *p);
138 static void btu_ble_read_remote_feat_evt (UINT8 *p);
139 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p, UINT16 evt_len);
140 static void btu_ble_proc_ltk_req (UINT8 *p);
141 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p);
142 #if (BLE_LLT_INCLUDED == TRUE)
143 static void btu_ble_rc_param_req_evt(UINT8 *p);
144 #endif
145     #endif
146 /*******************************************************************************
147 **
148 ** Function         btu_hcif_store_cmd
149 **
150 ** Description      This function stores a copy of an outgoing command and
151 **                  and sets a timer waiting for a event in response to the
152 **                  command.
153 **
154 ** Returns          void
155 **
156 *******************************************************************************/
157 static void btu_hcif_store_cmd (UINT8 controller_id, BT_HDR *p_buf)
158 {
159     tHCI_CMD_CB *p_hci_cmd_cb;
160     UINT16  opcode;
161     BT_HDR  *p_cmd;
162     UINT8   *p;
163
164     /* Validate controller ID */
165     if (controller_id >= BTU_MAX_LOCAL_CTRLS)
166         return;
167
168     p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
169     p = (UINT8 *)(p_buf + 1) + p_buf->offset;
170
171     /* get command opcode */
172     STREAM_TO_UINT16 (opcode, p);
173
174     /* don't do anything for certain commands */
175     if ((opcode == HCI_RESET) || (opcode == HCI_HOST_NUM_PACKETS_DONE))
176     {
177         return;
178     }
179
180     /* allocate buffer (HCI_GET_CMD_BUF will either get a buffer from HCI_CMD_POOL or from 'best-fit' pool) */
181     if ((p_cmd = HCI_GET_CMD_BUF(p_buf->len + p_buf->offset - HCIC_PREAMBLE_SIZE)) == NULL)
182     {
183         return;
184     }
185
186     /* copy buffer */
187     memcpy (p_cmd, p_buf, sizeof(BT_HDR));
188
189     /* If vendor specific save the callback function */
190     if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
191 #if BLE_INCLUDED == TRUE
192         || (opcode == HCI_BLE_RAND )
193         || (opcode == HCI_BLE_ENCRYPT)
194 #endif
195        )
196     {
197         memcpy ((UINT8 *)(p_cmd + 1), (UINT8 *)(p_buf + 1), sizeof(void *));
198     }
199
200     memcpy ((UINT8 *)(p_cmd + 1) + p_cmd->offset,
201             (UINT8 *)(p_buf + 1) + p_buf->offset, p_buf->len);
202
203     /* queue copy of cmd */
204     GKI_enqueue(&(p_hci_cmd_cb->cmd_cmpl_q), p_cmd);
205
206     /* start timer */
207     if (BTU_CMD_CMPL_TIMEOUT > 0)
208     {
209         btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
210                          (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
211                          BTU_CMD_CMPL_TIMEOUT);
212     }
213 }
214
215 /*******************************************************************************
216 **
217 ** Function         btu_hcif_process_event
218 **
219 ** Description      This function is called when an event is received from
220 **                  the Host Controller.
221 **
222 ** Returns          void
223 **
224 *******************************************************************************/
225 void btu_hcif_process_event (UINT8 controller_id, BT_HDR *p_msg)
226 {
227     UINT8   *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
228     UINT8   hci_evt_code, hci_evt_len;
229 #if BLE_INCLUDED == TRUE
230     UINT8   ble_sub_code;
231 #endif
232     STREAM_TO_UINT8  (hci_evt_code, p);
233     STREAM_TO_UINT8  (hci_evt_len, p);
234
235     switch (hci_evt_code)
236     {
237         case HCI_INQUIRY_COMP_EVT:
238             btu_hcif_inquiry_comp_evt (p);
239             break;
240         case HCI_INQUIRY_RESULT_EVT:
241             btu_hcif_inquiry_result_evt (p);
242             break;
243         case HCI_INQUIRY_RSSI_RESULT_EVT:
244             btu_hcif_inquiry_rssi_result_evt (p);
245             break;
246 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
247         case HCI_EXTENDED_INQUIRY_RESULT_EVT:
248             btu_hcif_extended_inquiry_result_evt (p);
249             break;
250 #endif
251         case HCI_CONNECTION_COMP_EVT:
252             btu_hcif_connection_comp_evt (p);
253             break;
254         case HCI_CONNECTION_REQUEST_EVT:
255             btu_hcif_connection_request_evt (p);
256             break;
257         case HCI_DISCONNECTION_COMP_EVT:
258             btu_hcif_disconnection_comp_evt (p);
259             break;
260         case HCI_AUTHENTICATION_COMP_EVT:
261             btu_hcif_authentication_comp_evt (p);
262             break;
263         case HCI_RMT_NAME_REQUEST_COMP_EVT:
264             btu_hcif_rmt_name_request_comp_evt (p, hci_evt_len);
265             break;
266         case HCI_ENCRYPTION_CHANGE_EVT:
267             btu_hcif_encryption_change_evt (p);
268             break;
269 #if BLE_INCLUDED == TRUE
270         case HCI_ENCRYPTION_KEY_REFRESH_COMP_EVT:
271             btu_hcif_encryption_key_refresh_cmpl_evt(p);
272             break;
273 #endif
274         case HCI_CHANGE_CONN_LINK_KEY_EVT:
275             btu_hcif_change_conn_link_key_evt (p);
276             break;
277         case HCI_MASTER_LINK_KEY_COMP_EVT:
278             btu_hcif_master_link_key_comp_evt (p);
279             break;
280         case HCI_READ_RMT_FEATURES_COMP_EVT:
281             btu_hcif_read_rmt_features_comp_evt (p);
282             break;
283         case HCI_READ_RMT_EXT_FEATURES_COMP_EVT:
284             btu_hcif_read_rmt_ext_features_comp_evt (p);
285             break;
286         case HCI_READ_RMT_VERSION_COMP_EVT:
287             btu_hcif_read_rmt_version_comp_evt (p);
288             break;
289         case HCI_QOS_SETUP_COMP_EVT:
290             btu_hcif_qos_setup_comp_evt (p);
291             break;
292         case HCI_COMMAND_COMPLETE_EVT:
293             btu_hcif_command_complete_evt (controller_id, p, hci_evt_len);
294             break;
295         case HCI_COMMAND_STATUS_EVT:
296             btu_hcif_command_status_evt (controller_id, p);
297             break;
298         case HCI_HARDWARE_ERROR_EVT:
299             btu_hcif_hardware_error_evt (p);
300             break;
301         case HCI_FLUSH_OCCURED_EVT:
302             btu_hcif_flush_occured_evt ();
303             break;
304         case HCI_ROLE_CHANGE_EVT:
305             btu_hcif_role_change_evt (p);
306             break;
307         case HCI_NUM_COMPL_DATA_PKTS_EVT:
308             btu_hcif_num_compl_data_pkts_evt (p);
309             break;
310         case HCI_MODE_CHANGE_EVT:
311             btu_hcif_mode_change_evt (p);
312             break;
313         case HCI_RETURN_LINK_KEYS_EVT:
314             btu_hcif_return_link_keys_evt (p);
315             break;
316         case HCI_PIN_CODE_REQUEST_EVT:
317             btu_hcif_pin_code_request_evt (p);
318             break;
319         case HCI_LINK_KEY_REQUEST_EVT:
320             btu_hcif_link_key_request_evt (p);
321             break;
322         case HCI_LINK_KEY_NOTIFICATION_EVT:
323             btu_hcif_link_key_notification_evt (p);
324             break;
325         case HCI_LOOPBACK_COMMAND_EVT:
326             btu_hcif_loopback_command_evt ();
327             break;
328         case HCI_DATA_BUF_OVERFLOW_EVT:
329             btu_hcif_data_buf_overflow_evt ();
330             break;
331         case HCI_MAX_SLOTS_CHANGED_EVT:
332             btu_hcif_max_slots_changed_evt ();
333             break;
334         case HCI_READ_CLOCK_OFF_COMP_EVT:
335             btu_hcif_read_clock_off_comp_evt (p);
336             break;
337         case HCI_CONN_PKT_TYPE_CHANGE_EVT:
338             btu_hcif_conn_pkt_type_change_evt ();
339             break;
340         case HCI_QOS_VIOLATION_EVT:
341             btu_hcif_qos_violation_evt (p);
342             break;
343         case HCI_PAGE_SCAN_MODE_CHANGE_EVT:
344             btu_hcif_page_scan_mode_change_evt ();
345             break;
346         case HCI_PAGE_SCAN_REP_MODE_CHNG_EVT:
347             btu_hcif_page_scan_rep_mode_chng_evt ();
348             break;
349         case HCI_ESCO_CONNECTION_COMP_EVT:
350             btu_hcif_esco_connection_comp_evt (p);
351             break;
352         case HCI_ESCO_CONNECTION_CHANGED_EVT:
353             btu_hcif_esco_connection_chg_evt (p);
354             break;
355 #if (BTM_SSR_INCLUDED == TRUE)
356         case HCI_SNIFF_SUB_RATE_EVT:
357             btu_hcif_ssr_evt (p, hci_evt_len);
358             break;
359 #endif  /* BTM_SSR_INCLUDED == TRUE */
360         case HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT:
361             btu_hcif_host_support_evt (p);
362             break;
363         case HCI_IO_CAPABILITY_REQUEST_EVT:
364             btu_hcif_io_cap_request_evt (p);
365             break;
366         case HCI_IO_CAPABILITY_RESPONSE_EVT:
367             btu_hcif_io_cap_response_evt (p);
368             break;
369         case HCI_USER_CONFIRMATION_REQUEST_EVT:
370             btu_hcif_user_conf_request_evt (p);
371             break;
372         case HCI_USER_PASSKEY_REQUEST_EVT:
373             btu_hcif_user_passkey_request_evt (p);
374             break;
375 #if BTM_OOB_INCLUDED == TRUE
376         case HCI_REMOTE_OOB_DATA_REQUEST_EVT:
377             btu_hcif_rem_oob_request_evt (p);
378             break;
379 #endif
380         case HCI_SIMPLE_PAIRING_COMPLETE_EVT:
381             btu_hcif_simple_pair_complete_evt (p);
382             break;
383         case HCI_USER_PASSKEY_NOTIFY_EVT:
384             btu_hcif_user_passkey_notif_evt (p);
385             break;
386         case HCI_KEYPRESS_NOTIFY_EVT:
387             btu_hcif_keypress_notif_evt (p);
388             break;
389         case HCI_LINK_SUPER_TOUT_CHANGED_EVT:
390             btu_hcif_link_super_tout_evt (p);
391             break;
392 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
393         case HCI_ENHANCED_FLUSH_COMPLETE_EVT:
394             btu_hcif_enhanced_flush_complete_evt ();
395             break;
396 #endif
397
398 #if (BLE_INCLUDED == TRUE)
399         case HCI_BLE_EVENT:
400             STREAM_TO_UINT8  (ble_sub_code, p);
401
402             HCI_TRACE_EVENT("BLE HCI(id=%d) event = 0x%02x)", hci_evt_code,  ble_sub_code);
403
404             switch (ble_sub_code)
405             {
406                 case HCI_BLE_ADV_PKT_RPT_EVT: /* result of inquiry */
407                     btu_ble_process_adv_pkt(p);
408                     break;
409                 case HCI_BLE_CONN_COMPLETE_EVT:
410                     btu_ble_ll_conn_complete_evt(p, hci_evt_len);
411                     break;
412                 case HCI_BLE_LL_CONN_PARAM_UPD_EVT:
413                     btu_ble_ll_conn_param_upd_evt(p, hci_evt_len);
414                     break;
415                 case HCI_BLE_READ_REMOTE_FEAT_CMPL_EVT:
416                     btu_ble_read_remote_feat_evt(p);
417                     break;
418                 case HCI_BLE_LTK_REQ_EVT: /* received only at slave device */
419                     btu_ble_proc_ltk_req(p);
420                     break;
421 #if (BLE_LLT_INCLUDED == TRUE)
422                case HCI_BLE_RC_PARAM_REQ_EVT:
423                     btu_ble_rc_param_req_evt(p);
424                     break;
425 #endif
426
427             }
428             break;
429 #endif /* BLE_INCLUDED */
430         case HCI_VENDOR_SPECIFIC_EVT:
431                 btm_vendor_specific_evt (p, hci_evt_len);
432             break;
433     }
434     // reset the  num_hci_cmds_timed_out upon receving any event from controller.
435     num_hci_cmds_timed_out = 0;
436 }
437
438
439 /*******************************************************************************
440 **
441 ** Function         btu_hcif_send_cmd
442 **
443 ** Description      This function is called to check if it can send commands
444 **                  to the Host Controller. It may be passed the address of
445 **                  a packet to send.
446 **
447 ** Returns          void
448 **
449 *******************************************************************************/
450 void btu_hcif_send_cmd (UINT8 controller_id, BT_HDR *p_buf)
451 {
452     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
453
454     /* If there are already commands in the queue, then enqueue this command */
455     if ((p_buf) && (!GKI_queue_is_empty(&p_hci_cmd_cb->cmd_xmit_q)))
456     {
457         GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
458         p_buf = NULL;
459     }
460
461     /* Allow for startup case, where no acks may be received */
462     if ( ((controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
463          && (p_hci_cmd_cb->cmd_window == 0)
464          && (btm_cb.devcb.state == BTM_DEV_STATE_WAIT_RESET_CMPLT)) )
465     {
466         p_hci_cmd_cb->cmd_window = GKI_queue_length(&p_hci_cmd_cb->cmd_xmit_q) + 1;
467     }
468
469     /* See if we can send anything */
470     while (p_hci_cmd_cb->cmd_window != 0)
471     {
472         if (!p_buf)
473             p_buf = (BT_HDR *)GKI_dequeue (&(p_hci_cmd_cb->cmd_xmit_q));
474
475         if (p_buf)
476         {
477             btu_hcif_store_cmd(controller_id, p_buf);
478             p_hci_cmd_cb->cmd_window--;
479
480             if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
481             {
482                 HCI_CMD_TO_LOWER(p_buf);
483             }
484             else
485             {
486                 /* Unknown controller */
487                 HCI_TRACE_WARNING("BTU HCI(ctrl id=%d) controller ID not recognized", controller_id);
488                 GKI_freebuf(p_buf);;
489             }
490
491             p_buf = NULL;
492         }
493         else
494             break;
495     }
496
497     if (p_buf)
498         GKI_enqueue (&(p_hci_cmd_cb->cmd_xmit_q), p_buf);
499
500 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
501     if (controller_id == LOCAL_BR_EDR_CONTROLLER_ID)
502     {
503         /* check if controller can go to sleep */
504         btu_check_bt_sleep ();
505     }
506 #endif
507
508 }
509
510
511 /*******************************************************************************
512 **
513 ** Function         btu_hcif_send_host_rdy_for_data
514 **
515 ** Description      This function is called to check if it can send commands
516 **                  to the Host Controller. It may be passed the address of
517 **                  a packet to send.
518 **
519 ** Returns          void
520 **
521 *******************************************************************************/
522 void btu_hcif_send_host_rdy_for_data(void)
523 {
524     UINT16      num_pkts[MAX_L2CAP_LINKS + 4];      /* 3 SCO connections */
525     UINT16      handles[MAX_L2CAP_LINKS + 4];
526     UINT8       num_ents;
527
528     /* Get the L2CAP numbers */
529     num_ents = l2c_link_pkts_rcvd (num_pkts, handles);
530
531     /* Get the SCO numbers */
532     /* No SCO for now ?? */
533
534     if (num_ents)
535     {
536         btsnd_hcic_host_num_xmitted_pkts (num_ents, handles, num_pkts);
537     }
538 }
539
540 /*******************************************************************************
541 **
542 ** Function         btu_hcif_inquiry_comp_evt
543 **
544 ** Description      Process event HCI_INQUIRY_COMP_EVT
545 **
546 ** Returns          void
547 **
548 *******************************************************************************/
549 static void btu_hcif_inquiry_comp_evt (UINT8 *p)
550 {
551     UINT8   status;
552
553     STREAM_TO_UINT8    (status, p);
554
555     /* Tell inquiry processing that we are done */
556     btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
557 }
558
559
560 /*******************************************************************************
561 **
562 ** Function         btu_hcif_inquiry_result_evt
563 **
564 ** Description      Process event HCI_INQUIRY_RESULT_EVT
565 **
566 ** Returns          void
567 **
568 *******************************************************************************/
569 static void btu_hcif_inquiry_result_evt (UINT8 *p)
570 {
571     /* Store results in the cache */
572     btm_process_inq_results (p, BTM_INQ_RESULT_STANDARD);
573 }
574
575 /*******************************************************************************
576 **
577 ** Function         btu_hcif_inquiry_rssi_result_evt
578 **
579 ** Description      Process event HCI_INQUIRY_RSSI_RESULT_EVT
580 **
581 ** Returns          void
582 **
583 *******************************************************************************/
584 static void btu_hcif_inquiry_rssi_result_evt (UINT8 *p)
585 {
586     /* Store results in the cache */
587     btm_process_inq_results (p, BTM_INQ_RESULT_WITH_RSSI);
588 }
589
590 /*******************************************************************************
591 **
592 ** Function         btu_hcif_extended_inquiry_result_evt
593 **
594 ** Description      Process event HCI_EXTENDED_INQUIRY_RESULT_EVT
595 **
596 ** Returns          void
597 **
598 *******************************************************************************/
599 #if (BTM_EIR_CLIENT_INCLUDED == TRUE)
600 static void btu_hcif_extended_inquiry_result_evt (UINT8 *p)
601 {
602     /* Store results in the cache */
603     btm_process_inq_results (p, BTM_INQ_RESULT_EXTENDED);
604 }
605 #endif
606
607 /*******************************************************************************
608 **
609 ** Function         btu_hcif_connection_comp_evt
610 **
611 ** Description      Process event HCI_CONNECTION_COMP_EVT
612 **
613 ** Returns          void
614 **
615 *******************************************************************************/
616 static void btu_hcif_connection_comp_evt (UINT8 *p)
617 {
618     UINT8       status;
619     UINT16      handle;
620     BD_ADDR     bda;
621     UINT8       link_type;
622     UINT8       enc_mode;
623 #if BTM_SCO_INCLUDED == TRUE
624     tBTM_ESCO_DATA  esco_data;
625 #endif
626
627     STREAM_TO_UINT8    (status, p);
628     STREAM_TO_UINT16   (handle, p);
629     STREAM_TO_BDADDR   (bda, p);
630     STREAM_TO_UINT8    (link_type, p);
631     STREAM_TO_UINT8    (enc_mode, p);
632
633     handle = HCID_GET_HANDLE (handle);
634
635     if (link_type == HCI_LINK_TYPE_ACL)
636     {
637         btm_sec_connected (bda, handle, status, enc_mode);
638
639         l2c_link_hci_conn_comp (status, handle, bda);
640     }
641 #if BTM_SCO_INCLUDED == TRUE
642     else
643     {
644         memset(&esco_data, 0, sizeof(tBTM_ESCO_DATA));
645         /* esco_data.link_type = HCI_LINK_TYPE_SCO; already zero */
646         memcpy (esco_data.bd_addr, bda, BD_ADDR_LEN);
647         btm_sco_connected (status, bda, handle, &esco_data);
648     }
649 #endif /* BTM_SCO_INCLUDED */
650 }
651
652
653 /*******************************************************************************
654 **
655 ** Function         btu_hcif_connection_request_evt
656 **
657 ** Description      Process event HCI_CONNECTION_REQUEST_EVT
658 **
659 ** Returns          void
660 **
661 *******************************************************************************/
662 static void btu_hcif_connection_request_evt (UINT8 *p)
663 {
664     BD_ADDR     bda;
665     DEV_CLASS   dc;
666     UINT8       link_type;
667
668     STREAM_TO_BDADDR   (bda, p);
669     STREAM_TO_DEVCLASS (dc, p);
670     STREAM_TO_UINT8    (link_type, p);
671
672     /* Pass request to security manager to check connect filters before */
673     /* passing request to l2cap */
674     if (link_type == HCI_LINK_TYPE_ACL)
675     {
676         btm_sec_conn_req (bda, dc);
677     }
678 #if BTM_SCO_INCLUDED == TRUE
679     else
680     {
681         btm_sco_conn_req (bda, dc, link_type);
682     }
683 #endif /* BTM_SCO_INCLUDED */
684 }
685
686
687 /*******************************************************************************
688 **
689 ** Function         btu_hcif_disconnection_comp_evt
690 **
691 ** Description      Process event HCI_DISCONNECTION_COMP_EVT
692 **
693 ** Returns          void
694 **
695 *******************************************************************************/
696 static void btu_hcif_disconnection_comp_evt (UINT8 *p)
697 {
698     UINT8   status;
699     UINT16  handle;
700     UINT8   reason;
701
702     STREAM_TO_UINT8  (status, p);
703     STREAM_TO_UINT16 (handle, p);
704     STREAM_TO_UINT8  (reason, p);
705
706     handle = HCID_GET_HANDLE (handle);
707
708 #if BTM_SCO_INCLUDED == TRUE
709     /* If L2CAP doesn't know about it, send it to SCO */
710     if (!l2c_link_hci_disc_comp (handle, reason))
711         btm_sco_removed (handle, reason);
712 #else
713     l2c_link_hci_disc_comp (handle, reason);
714 #endif /* BTM_SCO_INCLUDED */
715
716     /* Notify security manager */
717     btm_sec_disconnected (handle, reason);
718 }
719
720 /*******************************************************************************
721 **
722 ** Function         btu_hcif_authentication_comp_evt
723 **
724 ** Description      Process event HCI_AUTHENTICATION_COMP_EVT
725 **
726 ** Returns          void
727 **
728 *******************************************************************************/
729 static void btu_hcif_authentication_comp_evt (UINT8 *p)
730 {
731     UINT8   status;
732     UINT16  handle;
733
734     STREAM_TO_UINT8  (status, p);
735     STREAM_TO_UINT16 (handle, p);
736
737     btm_sec_auth_complete (handle, status);
738 }
739
740
741 /*******************************************************************************
742 **
743 ** Function         btu_hcif_rmt_name_request_comp_evt
744 **
745 ** Description      Process event HCI_RMT_NAME_REQUEST_COMP_EVT
746 **
747 ** Returns          void
748 **
749 *******************************************************************************/
750 static void btu_hcif_rmt_name_request_comp_evt (UINT8 *p, UINT16 evt_len)
751 {
752     UINT8   status;
753     BD_ADDR bd_addr;
754
755     STREAM_TO_UINT8 (status, p);
756     STREAM_TO_BDADDR (bd_addr, p);
757
758     evt_len -= (1 + BD_ADDR_LEN);
759
760     btm_process_remote_name (bd_addr, p, evt_len, status);
761
762     btm_sec_rmt_name_request_complete (bd_addr, p, status);
763 }
764
765
766 /*******************************************************************************
767 **
768 ** Function         btu_hcif_encryption_change_evt
769 **
770 ** Description      Process event HCI_ENCRYPTION_CHANGE_EVT
771 **
772 ** Returns          void
773 **
774 *******************************************************************************/
775 static void btu_hcif_encryption_change_evt (UINT8 *p)
776 {
777     UINT8   status;
778     UINT16  handle;
779     UINT8   encr_enable;
780
781     STREAM_TO_UINT8  (status, p);
782     STREAM_TO_UINT16 (handle, p);
783     STREAM_TO_UINT8  (encr_enable, p);
784
785     btm_acl_encrypt_change (handle, status, encr_enable);
786     btm_sec_encrypt_change (handle, status, encr_enable);
787 }
788
789
790 /*******************************************************************************
791 **
792 ** Function         btu_hcif_change_conn_link_key_evt
793 **
794 ** Description      Process event HCI_CHANGE_CONN_LINK_KEY_EVT
795 **
796 ** Returns          void
797 **
798 *******************************************************************************/
799 static void btu_hcif_change_conn_link_key_evt (UINT8 *p)
800 {
801     UINT8   status;
802     UINT16  handle;
803
804     STREAM_TO_UINT8  (status, p);
805     STREAM_TO_UINT16 (handle, p);
806
807     btm_acl_link_key_change (handle, status);
808 }
809
810
811 /*******************************************************************************
812 **
813 ** Function         btu_hcif_master_link_key_comp_evt
814 **
815 ** Description      Process event HCI_MASTER_LINK_KEY_COMP_EVT
816 **
817 ** Returns          void
818 **
819 *******************************************************************************/
820 static void btu_hcif_master_link_key_comp_evt (UINT8 *p)
821 {
822     UINT8   status;
823     UINT16  handle;
824     UINT8   key_flg;
825
826     STREAM_TO_UINT8  (status, p);
827     STREAM_TO_UINT16 (handle, p);
828     STREAM_TO_UINT8  (key_flg, p);
829
830     btm_sec_mkey_comp_event (handle, status, key_flg);
831 }
832
833
834 /*******************************************************************************
835 **
836 ** Function         btu_hcif_read_rmt_features_comp_evt
837 **
838 ** Description      Process event HCI_READ_RMT_FEATURES_COMP_EVT
839 **
840 ** Returns          void
841 **
842 *******************************************************************************/
843 static void btu_hcif_read_rmt_features_comp_evt (UINT8 *p)
844 {
845     btm_read_remote_features_complete(p);
846 }
847
848 /*******************************************************************************
849 **
850 ** Function         btu_hcif_read_rmt_ext_features_comp_evt
851 **
852 ** Description      Process event HCI_READ_RMT_EXT_FEATURES_COMP_EVT
853 **
854 ** Returns          void
855 **
856 *******************************************************************************/
857 static void btu_hcif_read_rmt_ext_features_comp_evt (UINT8 *p)
858 {
859     UINT8 *p_cur = p;
860     UINT8 status;
861     UINT16 handle;
862
863     STREAM_TO_UINT8 (status, p_cur);
864
865     if (status == HCI_SUCCESS)
866         btm_read_remote_ext_features_complete(p);
867     else
868     {
869         STREAM_TO_UINT16 (handle, p_cur);
870         btm_read_remote_ext_features_failed(status, handle);
871     }
872 }
873
874 /*******************************************************************************
875 **
876 ** Function         btu_hcif_read_rmt_version_comp_evt
877 **
878 ** Description      Process event HCI_READ_RMT_VERSION_COMP_EVT
879 **
880 ** Returns          void
881 **
882 *******************************************************************************/
883 static void btu_hcif_read_rmt_version_comp_evt (UINT8 *p)
884 {
885     btm_read_remote_version_complete (p);
886 }
887
888
889 /*******************************************************************************
890 **
891 ** Function         btu_hcif_qos_setup_comp_evt
892 **
893 ** Description      Process event HCI_QOS_SETUP_COMP_EVT
894 **
895 ** Returns          void
896 **
897 *******************************************************************************/
898 static void btu_hcif_qos_setup_comp_evt (UINT8 *p)
899 {
900     UINT8 status;
901     UINT16 handle;
902     FLOW_SPEC flow;
903
904     STREAM_TO_UINT8 (status, p);
905     STREAM_TO_UINT16 (handle, p);
906     STREAM_TO_UINT8 (flow.qos_flags, p);
907     STREAM_TO_UINT8 (flow.service_type, p);
908     STREAM_TO_UINT32 (flow.token_rate, p);
909     STREAM_TO_UINT32 (flow.peak_bandwidth, p);
910     STREAM_TO_UINT32 (flow.latency, p);
911     STREAM_TO_UINT32 (flow.delay_variation, p);
912
913     btm_qos_setup_complete(status, handle, &flow);
914 }
915
916
917 /*******************************************************************************
918 **
919 ** Function         btu_hcif_esco_connection_comp_evt
920 **
921 ** Description      Process event HCI_ESCO_CONNECTION_COMP_EVT
922 **
923 ** Returns          void
924 **
925 *******************************************************************************/
926 static void btu_hcif_esco_connection_comp_evt (UINT8 *p)
927 {
928 #if BTM_SCO_INCLUDED == TRUE
929     tBTM_ESCO_DATA  data;
930     UINT16          handle;
931     BD_ADDR         bda;
932     UINT8           status;
933
934     STREAM_TO_UINT8 (status, p);
935     STREAM_TO_UINT16 (handle, p);
936     STREAM_TO_BDADDR (bda, p);
937
938     STREAM_TO_UINT8 (data.link_type, p);
939     STREAM_TO_UINT8 (data.tx_interval, p);
940     STREAM_TO_UINT8 (data.retrans_window, p);
941     STREAM_TO_UINT16 (data.rx_pkt_len, p);
942     STREAM_TO_UINT16 (data.tx_pkt_len, p);
943     STREAM_TO_UINT8 (data.air_mode, p);
944
945     memcpy (data.bd_addr, bda, BD_ADDR_LEN);
946     btm_sco_connected (status, bda, handle, &data);
947 #endif
948 }
949
950
951 /*******************************************************************************
952 **
953 ** Function         btu_hcif_esco_connection_chg_evt
954 **
955 ** Description      Process event HCI_ESCO_CONNECTION_CHANGED_EVT
956 **
957 ** Returns          void
958 **
959 *******************************************************************************/
960 static void btu_hcif_esco_connection_chg_evt (UINT8 *p)
961 {
962 #if BTM_SCO_INCLUDED == TRUE
963     UINT16  handle;
964     UINT16  tx_pkt_len;
965     UINT16  rx_pkt_len;
966     UINT8   status;
967     UINT8   tx_interval;
968     UINT8   retrans_window;
969
970     STREAM_TO_UINT8 (status, p);
971     STREAM_TO_UINT16 (handle, p);
972
973     STREAM_TO_UINT8 (tx_interval, p);
974     STREAM_TO_UINT8 (retrans_window, p);
975     STREAM_TO_UINT16 (rx_pkt_len, p);
976     STREAM_TO_UINT16 (tx_pkt_len, p);
977
978     btm_esco_proc_conn_chg (status, handle, tx_interval, retrans_window,
979                             rx_pkt_len, tx_pkt_len);
980 #endif
981 }
982
983 /*******************************************************************************
984 **
985 ** Function         btu_hcif_hdl_command_complete
986 **
987 ** Description      Handle command complete event
988 **
989 ** Returns          void
990 **
991 *******************************************************************************/
992 static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_len,
993                                            void *p_cplt_cback)
994 {
995     switch (opcode)
996     {
997         case HCI_RESET:
998             btm_reset_complete ();  /* BR/EDR */
999             break;
1000
1001         case HCI_INQUIRY_CANCEL:
1002             /* Tell inquiry processing that we are done */
1003             btm_process_cancel_complete(HCI_SUCCESS, BTM_BR_INQUIRY_MASK);
1004             break;
1005         case HCI_SET_EVENT_FILTER:
1006             btm_event_filter_complete (p);
1007             break;
1008
1009         case HCI_READ_STORED_LINK_KEY:
1010             btm_read_stored_link_key_complete (p);
1011             break;
1012
1013         case HCI_WRITE_STORED_LINK_KEY:
1014             btm_write_stored_link_key_complete (p);
1015             break;
1016
1017         case HCI_DELETE_STORED_LINK_KEY:
1018             btm_delete_stored_link_key_complete (p);
1019             break;
1020
1021         case HCI_READ_LOCAL_VERSION_INFO:
1022             btm_read_local_version_complete (p, evt_len);
1023             break;
1024
1025         case HCI_READ_POLICY_SETTINGS:
1026             btm_read_link_policy_complete (p);
1027             break;
1028
1029         case HCI_READ_BUFFER_SIZE:
1030             btm_read_hci_buf_size_complete (p, evt_len);
1031             break;
1032
1033         case HCI_READ_LOCAL_SUPPORTED_CMDS:
1034             btm_read_local_supported_cmds_complete (p);
1035             break;
1036
1037         case HCI_READ_LOCAL_FEATURES:
1038             btm_read_local_features_complete (p, evt_len);
1039             break;
1040
1041         case HCI_READ_LOCAL_EXT_FEATURES:
1042             btm_read_local_ext_features_complete (p, evt_len);
1043             break;
1044
1045         case HCI_READ_LOCAL_NAME:
1046             btm_read_local_name_complete (p, evt_len);
1047             break;
1048
1049         case HCI_READ_BD_ADDR:
1050             btm_read_local_addr_complete (p, evt_len);
1051             break;
1052
1053         case HCI_GET_LINK_QUALITY:
1054             btm_read_link_quality_complete (p);
1055             break;
1056
1057         case HCI_READ_RSSI:
1058             btm_read_rssi_complete (p);
1059             break;
1060
1061         case HCI_READ_TRANSMIT_POWER_LEVEL:
1062             btm_read_tx_power_complete(p, FALSE);
1063             break;
1064
1065         case HCI_CREATE_CONNECTION_CANCEL:
1066             btm_create_conn_cancel_complete(p);
1067             break;
1068
1069         case HCI_READ_LOCAL_OOB_DATA:
1070 #if BTM_OOB_INCLUDED == TRUE
1071             btm_read_local_oob_complete(p);
1072 #endif
1073             break;
1074
1075
1076         case HCI_READ_INQ_TX_POWER_LEVEL:
1077             btm_read_linq_tx_power_complete (p);
1078             break;
1079
1080         case HCI_WRITE_SIMPLE_PAIRING_MODE:
1081             btm_write_simple_paring_mode_complete (p);
1082             break;
1083
1084         case HCI_WRITE_LE_HOST_SUPPORTED:
1085             btm_write_le_host_supported_complete (p);
1086             break;
1087
1088 #if (BLE_INCLUDED == TRUE)
1089 /* BLE Commands sComplete*/
1090         case HCI_BLE_READ_WHITE_LIST_SIZE :
1091             btm_read_white_list_size_complete(p, evt_len);
1092             break;
1093
1094         case HCI_BLE_ADD_WHITE_LIST:
1095             btm_ble_add_2_white_list_complete(*p);
1096             break;
1097
1098         case HCI_BLE_CLEAR_WHITE_LIST:
1099             btm_ble_clear_white_list_complete(p, evt_len);
1100             break;
1101
1102         case HCI_BLE_REMOVE_WHITE_LIST:
1103             btm_ble_remove_from_white_list_complete(p, evt_len);
1104             break;
1105
1106         case HCI_BLE_RAND:
1107         case HCI_BLE_ENCRYPT:
1108             btm_ble_rand_enc_complete (p, opcode, (tBTM_RAND_ENC_CB *)p_cplt_cback);
1109             break;
1110
1111         case HCI_BLE_READ_BUFFER_SIZE:
1112             btm_read_ble_buf_size_complete(p, evt_len);
1113             break;
1114
1115         case HCI_BLE_READ_LOCAL_SPT_FEAT:
1116             btm_read_ble_local_supported_features_complete(p, evt_len);
1117             break;
1118
1119         case HCI_BLE_READ_ADV_CHNL_TX_POWER:
1120             btm_read_tx_power_complete(p, TRUE);
1121             break;
1122
1123         case HCI_BLE_WRITE_ADV_ENABLE:
1124             btm_ble_write_adv_enable_complete(p);
1125             break;
1126
1127         case HCI_BLE_READ_SUPPORTED_STATES:
1128             btm_read_ble_local_supported_states_complete(p, evt_len);
1129             break;
1130
1131         case HCI_BLE_CREATE_LL_CONN:
1132             btm_ble_create_ll_conn_complete(*p);
1133             break;
1134
1135         case HCI_BLE_TRANSMITTER_TEST:
1136         case HCI_BLE_RECEIVER_TEST:
1137         case HCI_BLE_TEST_END:
1138             btm_ble_test_command_complete(p);
1139             break;
1140 #endif /* (BLE_INCLUDED == TRUE) */
1141
1142         default:
1143             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1144                 btm_vsc_complete (p, opcode, evt_len, (tBTM_CMPL_CB *)p_cplt_cback);
1145             break;
1146     }
1147 }
1148
1149 /*******************************************************************************
1150 **
1151 ** Function         btu_hcif_command_complete_evt
1152 **
1153 ** Description      Process event HCI_COMMAND_COMPLETE_EVT
1154 **
1155 ** Returns          void
1156 **
1157 *******************************************************************************/
1158 static void btu_hcif_command_complete_evt (UINT8 controller_id, UINT8 *p, UINT16 evt_len)
1159 {
1160     tHCI_CMD_CB *p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1161     UINT16      cc_opcode;
1162     BT_HDR      *p_cmd;
1163     void        *p_cplt_cback = NULL;
1164
1165     STREAM_TO_UINT8  (p_hci_cmd_cb->cmd_window, p);
1166
1167 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
1168     if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
1169         p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
1170 #endif
1171
1172     STREAM_TO_UINT16 (cc_opcode, p);
1173
1174     evt_len -= 3;
1175
1176     /* only do this for certain commands */
1177     if ((cc_opcode != HCI_RESET) && (cc_opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1178         (cc_opcode != HCI_COMMAND_NONE))
1179     {
1180         /* dequeue and free stored command */
1181
1182 /* always use cmd code check, when one cmd timeout waiting for cmd_cmpl,
1183    it'll cause the rest of the command goes in wrong order                  */
1184         p_cmd = (BT_HDR *) GKI_getfirst (&p_hci_cmd_cb->cmd_cmpl_q);
1185
1186         while (p_cmd)
1187         {
1188             UINT16 opcode_dequeued;
1189             UINT8  *p_dequeued;
1190
1191             /* Make sure dequeued command is for the command_cplt received */
1192             p_dequeued = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1193             STREAM_TO_UINT16 (opcode_dequeued, p_dequeued);
1194
1195             if (opcode_dequeued != cc_opcode)
1196             {
1197                 /* opcode does not match, check next command in the queue */
1198                 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1199                 continue;
1200             }
1201             GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1202
1203             /* If command was a VSC, then extract command_complete callback */
1204             if ((cc_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
1205 #if BLE_INCLUDED == TRUE
1206                 || (cc_opcode == HCI_BLE_RAND )
1207                 || (cc_opcode == HCI_BLE_ENCRYPT)
1208 #endif
1209                )
1210             {
1211                 p_cplt_cback = *((void **)(p_cmd + 1));
1212             }
1213
1214             GKI_freebuf (p_cmd);
1215
1216             break;
1217         }
1218
1219         /* if more commands in queue restart timer */
1220         if (BTU_CMD_CMPL_TIMEOUT > 0)
1221         {
1222             if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1223             {
1224                 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1225                                  (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1226                                  BTU_CMD_CMPL_TIMEOUT);
1227             }
1228             else
1229             {
1230                 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1231             }
1232         }
1233     }
1234
1235     /* handle event */
1236     btu_hcif_hdl_command_complete (cc_opcode, p, evt_len, p_cplt_cback);
1237
1238     /* see if we can send more commands */
1239     btu_hcif_send_cmd (controller_id, NULL);
1240 }
1241
1242
1243 /*******************************************************************************
1244 **
1245 ** Function         btu_hcif_hdl_command_status
1246 **
1247 ** Description      Handle a command status event
1248 **
1249 ** Returns          void
1250 **
1251 *******************************************************************************/
1252 static void btu_hcif_hdl_command_status (UINT16 opcode, UINT8 status, UINT8 *p_cmd,
1253                                          void *p_vsc_status_cback)
1254 {
1255     BD_ADDR         bd_addr;
1256     UINT16          handle;
1257 #if BTM_SCO_INCLUDED == TRUE
1258     tBTM_ESCO_DATA  esco_data;
1259 #endif
1260
1261 #if BTM_PWR_MGR_INCLUDED == TRUE
1262     switch (opcode)
1263     {
1264         case HCI_EXIT_SNIFF_MODE:
1265         case HCI_EXIT_PARK_MODE:
1266 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
1267             if (status != HCI_SUCCESS)
1268             {
1269                 /* Allow SCO initiation to continue if waiting for change mode event */
1270                 if (p_cmd != NULL)
1271                 {
1272                     p_cmd++;    /* bypass length field */
1273                     STREAM_TO_UINT16 (handle, p_cmd);
1274                     btm_sco_chk_pend_unpark (status, handle);
1275                 }
1276             }
1277 #endif
1278             /* Case Falls Through */
1279
1280         case HCI_HOLD_MODE:
1281         case HCI_SNIFF_MODE:
1282         case HCI_PARK_MODE:
1283             btm_pm_proc_cmd_status(status);
1284             break;
1285
1286         default:
1287 #endif  /* BTM_PWR_MGR_INCLUDED */
1288             /* If command failed to start, we may need to tell BTM */
1289             if (status != HCI_SUCCESS)
1290             {
1291                 switch (opcode)
1292                 {
1293                     case HCI_INQUIRY:
1294                         /* Tell inquiry processing that we are done */
1295                         btm_process_inq_complete(status, BTM_BR_INQUIRY_MASK);
1296                         break;
1297
1298                     case HCI_RMT_NAME_REQUEST:
1299                         /* Tell inquiry processing that we are done */
1300                         btm_process_remote_name (NULL, NULL, 0, status);
1301
1302                         btm_sec_rmt_name_request_complete (NULL, NULL, status);
1303                         break;
1304
1305                     case HCI_CHANGE_CONN_LINK_KEY:
1306                         /* Let host know we're done with error */
1307                         /* read handle out of stored command */
1308                         if (p_cmd != NULL)
1309                         {
1310                             p_cmd++;
1311                             STREAM_TO_UINT16 (handle, p_cmd);
1312
1313                             btm_acl_link_key_change (handle, status);
1314                         }
1315                         break;
1316
1317                     case HCI_QOS_SETUP_COMP_EVT:
1318                         /* Tell qos setup that we are done */
1319                         btm_qos_setup_complete(status,0,NULL);
1320                         break;
1321
1322                     case HCI_SWITCH_ROLE:
1323                         /* Tell BTM that the command failed */
1324                         /* read bd addr out of stored command */
1325                         if (p_cmd != NULL)
1326                         {
1327                             p_cmd++;
1328                             STREAM_TO_BDADDR (bd_addr, p_cmd);
1329                             btm_acl_role_changed(status, bd_addr, BTM_ROLE_UNDEFINED);
1330                         }
1331                         else
1332                             btm_acl_role_changed(status, NULL, BTM_ROLE_UNDEFINED);
1333                         l2c_link_role_changed (NULL, BTM_ROLE_UNDEFINED, HCI_ERR_COMMAND_DISALLOWED);
1334                         break;
1335
1336                     case HCI_CREATE_CONNECTION:
1337                         /* read bd addr out of stored command */
1338                         if (p_cmd != NULL)
1339                         {
1340                             p_cmd++;
1341                             STREAM_TO_BDADDR (bd_addr, p_cmd);
1342                             btm_sec_connected (bd_addr, HCI_INVALID_HANDLE, status, 0);
1343                             l2c_link_hci_conn_comp (status, HCI_INVALID_HANDLE, bd_addr);
1344                         }
1345                         break;
1346
1347                     case HCI_READ_RMT_EXT_FEATURES:
1348                         if (p_cmd != NULL)
1349                         {
1350                             p_cmd++; /* skip command length */
1351                             STREAM_TO_UINT16 (handle, p_cmd);
1352                         }
1353                         else
1354                             handle = HCI_INVALID_HANDLE;
1355
1356                         btm_read_remote_ext_features_failed(status, handle);
1357                         break;
1358
1359                     case HCI_AUTHENTICATION_REQUESTED:
1360                         /* Device refused to start authentication.  That should be treated as authentication failure. */
1361                         btm_sec_auth_complete (BTM_INVALID_HCI_HANDLE, status);
1362                         break;
1363
1364                     case HCI_SET_CONN_ENCRYPTION:
1365                         /* Device refused to start encryption.  That should be treated as encryption failure. */
1366                         btm_sec_encrypt_change (BTM_INVALID_HCI_HANDLE, status, FALSE);
1367                         break;
1368
1369 #if BLE_INCLUDED == TRUE
1370                     case HCI_BLE_CREATE_LL_CONN:
1371                         btm_ble_create_ll_conn_complete(status);
1372                         break;
1373 #endif
1374
1375 #if BTM_SCO_INCLUDED == TRUE
1376                     case HCI_SETUP_ESCO_CONNECTION:
1377                         /* read handle out of stored command */
1378                         if (p_cmd != NULL)
1379                         {
1380                             p_cmd++;
1381                             STREAM_TO_UINT16 (handle, p_cmd);
1382
1383                             /* Determine if initial connection failed or is a change of setup */
1384                             if (btm_is_sco_active(handle))
1385                                 btm_esco_proc_conn_chg (status, handle, 0, 0, 0, 0);
1386                             else
1387                                 btm_sco_connected (status, NULL, handle, &esco_data);
1388                         }
1389                         break;
1390 #endif
1391
1392 /* This is commented out until an upper layer cares about returning event
1393 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
1394             case HCI_ENHANCED_FLUSH:
1395                 break;
1396 #endif
1397 */
1398                     default:
1399                         if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1400                             btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
1401                         break;
1402                 }
1403
1404             }
1405             else
1406             {
1407                 if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1408                     btm_vsc_complete (&status, opcode, 1, (tBTM_CMPL_CB *)p_vsc_status_cback);
1409             }
1410 #if BTM_PWR_MGR_INCLUDED == TRUE
1411     }
1412 #endif
1413 }
1414
1415 /*******************************************************************************
1416 **
1417 ** Function         btu_hcif_command_status_evt
1418 **
1419 ** Description      Process event HCI_COMMAND_STATUS_EVT
1420 **
1421 ** Returns          void
1422 **
1423 *******************************************************************************/
1424 static void btu_hcif_command_status_evt (UINT8 controller_id, UINT8 *p)
1425 {
1426     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1427     UINT8       status;
1428     UINT16      opcode;
1429     UINT16      cmd_opcode;
1430     BT_HDR      *p_cmd = NULL;
1431     UINT8       *p_data = NULL;
1432     void        *p_vsc_status_cback = NULL;
1433
1434     STREAM_TO_UINT8  (status, p);
1435     STREAM_TO_UINT8  (p_hci_cmd_cb->cmd_window, p);
1436
1437 #if (defined(HCI_MAX_SIMUL_CMDS) && (HCI_MAX_SIMUL_CMDS > 0))
1438     if (p_hci_cmd_cb->cmd_window > HCI_MAX_SIMUL_CMDS)
1439         p_hci_cmd_cb->cmd_window = HCI_MAX_SIMUL_CMDS;
1440 #endif
1441
1442     STREAM_TO_UINT16 (opcode, p);
1443
1444     /* only do this for certain commands */
1445     if ((opcode != HCI_RESET) && (opcode != HCI_HOST_NUM_PACKETS_DONE) &&
1446         (opcode != HCI_COMMAND_NONE))
1447     {
1448         /*look for corresponding command in cmd_queue*/
1449         p_cmd = (BT_HDR *) GKI_getfirst(&(p_hci_cmd_cb->cmd_cmpl_q));
1450         while (p_cmd)
1451         {
1452             p_data = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1453             STREAM_TO_UINT16 (cmd_opcode, p_data);
1454
1455             /* Make sure this  command is for the command_status received */
1456             if (cmd_opcode != opcode)
1457             {
1458                 /* opcode does not match, check next command in the queue */
1459                 p_cmd = (BT_HDR *) GKI_getnext(p_cmd);
1460                 continue;
1461             }
1462             else
1463             {
1464                 GKI_remove_from_queue(&p_hci_cmd_cb->cmd_cmpl_q, p_cmd);
1465
1466                 /* If command was a VSC, then extract command_status callback */
1467                  if ((cmd_opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC)
1468                 {
1469                     p_vsc_status_cback = *((void **)(p_cmd + 1));
1470                 }
1471                 break;
1472             }
1473         }
1474
1475         /* if more commands in queue restart timer */
1476         if (BTU_CMD_CMPL_TIMEOUT > 0)
1477         {
1478             if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1479             {
1480                 btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1481                                  (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1482                                  BTU_CMD_CMPL_TIMEOUT);
1483             }
1484             else
1485             {
1486                 btu_stop_timer (&(p_hci_cmd_cb->cmd_cmpl_timer));
1487             }
1488         }
1489     }
1490
1491     /* handle command */
1492     btu_hcif_hdl_command_status (opcode, status, p_data, p_vsc_status_cback);
1493
1494     /* free stored command */
1495     if (p_cmd != NULL)
1496     {
1497         GKI_freebuf (p_cmd);
1498     }
1499     else
1500     {
1501         HCI_TRACE_WARNING("No command in queue matching opcode %d", opcode);
1502     }
1503
1504     /* See if we can forward any more commands */
1505     btu_hcif_send_cmd (controller_id, NULL);
1506 }
1507
1508 /*******************************************************************************
1509 **
1510 ** Function         btu_hcif_cmd_timeout
1511 **
1512 ** Description      Handle a command timeout
1513 **
1514 ** Returns          void
1515 **
1516 *******************************************************************************/
1517 void btu_hcif_cmd_timeout (UINT8 controller_id)
1518 {
1519     tHCI_CMD_CB * p_hci_cmd_cb = &(btu_cb.hci_cmd_cb[controller_id]);
1520     BT_HDR  *p_cmd;
1521     UINT8   *p;
1522     void    *p_cplt_cback = NULL;
1523     UINT16  opcode;
1524     UINT16  event;
1525
1526     /* set the controller cmd window to 1, as if we received a response, so
1527     ** the flow of commands from the stack doesn't hang */
1528     p_hci_cmd_cb->cmd_window = 1;
1529
1530     /* get queued command */
1531     if ((p_cmd = (BT_HDR *) GKI_dequeue (&(p_hci_cmd_cb->cmd_cmpl_q))) == NULL)
1532     {
1533         HCI_TRACE_WARNING("Cmd timeout; no cmd in queue");
1534         return;
1535     }
1536
1537     /* if more commands in queue restart timer */
1538     if (BTU_CMD_CMPL_TIMEOUT > 0)
1539     {
1540         if (!GKI_queue_is_empty (&(p_hci_cmd_cb->cmd_cmpl_q)))
1541         {
1542             btu_start_timer (&(p_hci_cmd_cb->cmd_cmpl_timer),
1543                              (UINT16)(BTU_TTYPE_BTU_CMD_CMPL + controller_id),
1544                              BTU_CMD_CMPL_TIMEOUT);
1545         }
1546     }
1547
1548     p = (UINT8 *)(p_cmd + 1) + p_cmd->offset;
1549 #if (NFC_INCLUDED == TRUE)
1550     if (controller_id == NFC_CONTROLLER_ID)
1551     {
1552         //TODO call nfc_ncif_cmd_timeout
1553         HCI_TRACE_WARNING("BTU NCI command timeout - header 0x%02x%02x", p[0], p[1]);
1554         return;
1555     }
1556 #endif
1557
1558     /* get opcode from stored command */
1559     STREAM_TO_UINT16 (opcode, p);
1560
1561 // btla-specific ++
1562 #if (defined(ANDROID_APP_INCLUDED) && (ANDROID_APP_INCLUDED == TRUE))
1563     ALOGE("######################################################################");
1564     ALOGE("#");
1565     ALOGE("# WARNING : BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1566     ALOGE("#");
1567     ALOGE("######################################################################");
1568 #else
1569     HCI_TRACE_WARNING("BTU HCI(id=%d) command timeout. opcode=0x%x", controller_id, opcode);
1570 #endif
1571 // btla-specific ++
1572
1573     /* send stack a fake command complete or command status, but first determine
1574     ** which to send
1575     */
1576     switch (opcode)
1577     {
1578         case HCI_HOLD_MODE:
1579         case HCI_SNIFF_MODE:
1580         case HCI_EXIT_SNIFF_MODE:
1581         case HCI_PARK_MODE:
1582         case HCI_EXIT_PARK_MODE:
1583         case HCI_INQUIRY:
1584         case HCI_RMT_NAME_REQUEST:
1585         case HCI_QOS_SETUP_COMP_EVT:
1586         case HCI_CREATE_CONNECTION:
1587         case HCI_CHANGE_CONN_LINK_KEY:
1588         case HCI_SWITCH_ROLE:
1589         case HCI_READ_RMT_EXT_FEATURES:
1590         case HCI_AUTHENTICATION_REQUESTED:
1591         case HCI_SET_CONN_ENCRYPTION:
1592 #if BTM_SCO_INCLUDED == TRUE
1593         case HCI_SETUP_ESCO_CONNECTION:
1594 #endif
1595             /* fake a command status */
1596             btu_hcif_hdl_command_status (opcode, HCI_ERR_UNSPECIFIED, p, NULL);
1597             break;
1598
1599         default:
1600             /* If vendor specific restore the callback function */
1601             if ((opcode & HCI_GRP_VENDOR_SPECIFIC) == HCI_GRP_VENDOR_SPECIFIC
1602 #if BLE_INCLUDED == TRUE
1603                 || (opcode == HCI_BLE_RAND ) ||
1604                 (opcode == HCI_BLE_ENCRYPT)
1605 #endif
1606                )
1607             {
1608                 p_cplt_cback = *((void **)(p_cmd + 1));
1609             }
1610
1611             /* fake a command complete; first create a fake event */
1612             event = HCI_ERR_UNSPECIFIED;
1613             btu_hcif_hdl_command_complete (opcode, (UINT8 *)&event, 1, p_cplt_cback);
1614             break;
1615     }
1616
1617     /* free stored command */
1618     GKI_freebuf(p_cmd);
1619
1620     num_hci_cmds_timed_out++;
1621     /* When we receive consecutive HCI cmd timeouts for >=BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART
1622      times, Bluetooth process will be killed and restarted */
1623     if (num_hci_cmds_timed_out >= BTM_MAX_HCI_CMD_TOUT_BEFORE_RESTART)
1624     {
1625         HCI_TRACE_ERROR("Num consecutive HCI Cmd tout =%d Restarting BT process",num_hci_cmds_timed_out);
1626
1627         usleep(10000); /* 10 milliseconds */
1628         /* Killing the process to force a restart as part of fault tolerance */
1629         kill(getpid(), SIGKILL);
1630     }
1631     else
1632     {
1633         HCI_TRACE_WARNING("HCI Cmd timeout counter %d", num_hci_cmds_timed_out);
1634
1635         /* If anyone wants device status notifications, give him one */
1636         btm_report_device_status (BTM_DEV_STATUS_CMD_TOUT);
1637     }
1638     /* See if we can forward any more commands */
1639     btu_hcif_send_cmd (controller_id, NULL);
1640 }
1641
1642 /*******************************************************************************
1643 **
1644 ** Function         btu_hcif_hardware_error_evt
1645 **
1646 ** Description      Process event HCI_HARDWARE_ERROR_EVT
1647 **
1648 ** Returns          void
1649 **
1650 *******************************************************************************/
1651 static void btu_hcif_hardware_error_evt (UINT8 *p)
1652 {
1653     HCI_TRACE_ERROR("Ctlr H/w error event - code:0x%x", *p);
1654
1655     /* If anyone wants device status notifications, give him one. */
1656     btm_report_device_status (BTM_DEV_STATUS_DOWN);
1657
1658     /* Reset the controller */
1659     if (BTM_IsDeviceUp())
1660         BTM_DeviceReset (NULL);
1661 }
1662
1663
1664 /*******************************************************************************
1665 **
1666 ** Function         btu_hcif_flush_occured_evt
1667 **
1668 ** Description      Process event HCI_FLUSH_OCCURED_EVT
1669 **
1670 ** Returns          void
1671 **
1672 *******************************************************************************/
1673 static void btu_hcif_flush_occured_evt (void)
1674 {
1675 }
1676
1677
1678 /*******************************************************************************
1679 **
1680 ** Function         btu_hcif_role_change_evt
1681 **
1682 ** Description      Process event HCI_ROLE_CHANGE_EVT
1683 **
1684 ** Returns          void
1685 **
1686 *******************************************************************************/
1687 static void btu_hcif_role_change_evt (UINT8 *p)
1688 {
1689     UINT8       status;
1690     BD_ADDR     bda;
1691     UINT8       role;
1692
1693     STREAM_TO_UINT8 (status, p);
1694     STREAM_TO_BDADDR (bda, p);
1695     STREAM_TO_UINT8  (role, p);
1696
1697     l2c_link_role_changed (bda, role, status);
1698     btm_acl_role_changed(status, bda, role);
1699 }
1700
1701
1702 /*******************************************************************************
1703 **
1704 ** Function         btu_hcif_num_compl_data_pkts_evt
1705 **
1706 ** Description      Process event HCI_NUM_COMPL_DATA_PKTS_EVT
1707 **
1708 ** Returns          void
1709 **
1710 *******************************************************************************/
1711 static void btu_hcif_num_compl_data_pkts_evt (UINT8 *p)
1712 {
1713     /* Process for L2CAP and SCO */
1714     l2c_link_process_num_completed_pkts (p);
1715
1716     /* Send on to SCO */
1717     /*?? No SCO for now */
1718 }
1719
1720 /*******************************************************************************
1721 **
1722 ** Function         btu_hcif_mode_change_evt
1723 **
1724 ** Description      Process event HCI_MODE_CHANGE_EVT
1725 **
1726 ** Returns          void
1727 **
1728 *******************************************************************************/
1729 static void btu_hcif_mode_change_evt (UINT8 *p)
1730 {
1731     UINT8       status;
1732     UINT16      handle;
1733     UINT8       current_mode;
1734     UINT16      interval;
1735
1736     STREAM_TO_UINT8 (status, p);
1737
1738     STREAM_TO_UINT16 (handle, p);
1739     STREAM_TO_UINT8 (current_mode, p);
1740     STREAM_TO_UINT16 (interval, p);
1741 #if BTM_PWR_MGR_INCLUDED == TRUE
1742 #if BTM_SCO_WAKE_PARKED_LINK == TRUE
1743     btm_sco_chk_pend_unpark (status, handle);
1744 #endif
1745     btm_pm_proc_mode_change (status, handle, current_mode, interval);
1746 #else
1747     btm_process_mode_change (status, handle, current_mode, interval);
1748 #endif /* BTM_PWR_MGR_INCLUDED == TRUE */
1749
1750 #if (HID_DEV_INCLUDED == TRUE) && (HID_DEV_PM_INCLUDED == TRUE)
1751     hidd_pm_proc_mode_change( status, current_mode, interval ) ;
1752 #endif
1753 }
1754
1755 /*******************************************************************************
1756 **
1757 ** Function         btu_hcif_ssr_evt
1758 **
1759 ** Description      Process event HCI_SNIFF_SUB_RATE_EVT
1760 **
1761 ** Returns          void
1762 **
1763 *******************************************************************************/
1764     #if (BTM_SSR_INCLUDED == TRUE)
1765 static void btu_hcif_ssr_evt (UINT8 *p, UINT16 evt_len)
1766 {
1767 #if (BTM_PWR_MGR_INCLUDED == TRUE)
1768     btm_pm_proc_ssr_evt(p, evt_len);
1769 #endif
1770 }
1771     #endif
1772
1773
1774 /*******************************************************************************
1775 **
1776 ** Function         btu_hcif_return_link_keys_evt
1777 **
1778 ** Description      Process event HCI_RETURN_LINK_KEYS_EVT
1779 **
1780 ** Returns          void
1781 **
1782 *******************************************************************************/
1783
1784 static void btu_hcif_return_link_keys_evt (UINT8 *p)
1785 {
1786     UINT8                       num_keys;
1787     tBTM_RETURN_LINK_KEYS_EVT   *result;
1788
1789     /* get the number of link keys */
1790     num_keys = *p;
1791
1792     /* If there are no link keys don't call the call back */
1793     if (!num_keys)
1794         return;
1795
1796     /* Take one extra byte at the beginning to specify event */
1797     result = (tBTM_RETURN_LINK_KEYS_EVT *)(--p);
1798     result->event = BTM_CB_EVT_RETURN_LINK_KEYS;
1799
1800     /* Call the BTM function to pass the link keys to application */
1801     btm_return_link_keys_evt (result);
1802 }
1803
1804
1805 /*******************************************************************************
1806 **
1807 ** Function         btu_hcif_pin_code_request_evt
1808 **
1809 ** Description      Process event HCI_PIN_CODE_REQUEST_EVT
1810 **
1811 ** Returns          void
1812 **
1813 *******************************************************************************/
1814 static void btu_hcif_pin_code_request_evt (UINT8 *p)
1815 {
1816     BD_ADDR  bda;
1817
1818     STREAM_TO_BDADDR (bda, p);
1819
1820     /* Tell L2CAP that there was a PIN code request,  */
1821     /* it may need to stretch timeouts                */
1822     l2c_pin_code_request (bda);
1823
1824     btm_sec_pin_code_request (bda);
1825 }
1826
1827
1828 /*******************************************************************************
1829 **
1830 ** Function         btu_hcif_link_key_request_evt
1831 **
1832 ** Description      Process event HCI_LINK_KEY_REQUEST_EVT
1833 **
1834 ** Returns          void
1835 **
1836 *******************************************************************************/
1837 static void btu_hcif_link_key_request_evt (UINT8 *p)
1838 {
1839     BD_ADDR  bda;
1840
1841     STREAM_TO_BDADDR (bda, p);
1842     btm_sec_link_key_request (bda);
1843 }
1844
1845
1846 /*******************************************************************************
1847 **
1848 ** Function         btu_hcif_link_key_notification_evt
1849 **
1850 ** Description      Process event HCI_LINK_KEY_NOTIFICATION_EVT
1851 **
1852 ** Returns          void
1853 **
1854 *******************************************************************************/
1855 static void btu_hcif_link_key_notification_evt (UINT8 *p)
1856 {
1857     BD_ADDR  bda;
1858     LINK_KEY key;
1859     UINT8    key_type;
1860
1861     STREAM_TO_BDADDR (bda, p);
1862     STREAM_TO_ARRAY16 (key, p);
1863     STREAM_TO_UINT8 (key_type, p);
1864
1865     btm_sec_link_key_notification (bda, key, key_type);
1866 }
1867
1868
1869 /*******************************************************************************
1870 **
1871 ** Function         btu_hcif_loopback_command_evt
1872 **
1873 ** Description      Process event HCI_LOOPBACK_COMMAND_EVT
1874 **
1875 ** Returns          void
1876 **
1877 *******************************************************************************/
1878 static void btu_hcif_loopback_command_evt (void)
1879 {
1880 }
1881
1882
1883 /*******************************************************************************
1884 **
1885 ** Function         btu_hcif_data_buf_overflow_evt
1886 **
1887 ** Description      Process event HCI_DATA_BUF_OVERFLOW_EVT
1888 **
1889 ** Returns          void
1890 **
1891 *******************************************************************************/
1892 static void btu_hcif_data_buf_overflow_evt (void)
1893 {
1894 }
1895
1896
1897 /*******************************************************************************
1898 **
1899 ** Function         btu_hcif_max_slots_changed_evt
1900 **
1901 ** Description      Process event HCI_MAX_SLOTS_CHANGED_EVT
1902 **
1903 ** Returns          void
1904 **
1905 *******************************************************************************/
1906 static void btu_hcif_max_slots_changed_evt (void)
1907 {
1908 }
1909
1910
1911 /*******************************************************************************
1912 **
1913 ** Function         btu_hcif_read_clock_off_comp_evt
1914 **
1915 ** Description      Process event HCI_READ_CLOCK_OFF_COMP_EVT
1916 **
1917 ** Returns          void
1918 **
1919 *******************************************************************************/
1920 static void btu_hcif_read_clock_off_comp_evt (UINT8 *p)
1921 {
1922     UINT8       status;
1923     UINT16      handle;
1924     UINT16      clock_offset;
1925
1926     STREAM_TO_UINT8  (status, p);
1927
1928     /* If failed to get clock offset just drop the result */
1929     if (status != HCI_SUCCESS)
1930         return;
1931
1932     STREAM_TO_UINT16 (handle, p);
1933     STREAM_TO_UINT16 (clock_offset, p);
1934
1935     handle = HCID_GET_HANDLE (handle);
1936
1937     btm_process_clk_off_comp_evt (handle, clock_offset);
1938     btm_sec_update_clock_offset (handle, clock_offset);
1939 }
1940
1941
1942 /*******************************************************************************
1943 **
1944 ** Function         btu_hcif_conn_pkt_type_change_evt
1945 **
1946 ** Description      Process event HCI_CONN_PKT_TYPE_CHANGE_EVT
1947 **
1948 ** Returns          void
1949 **
1950 *******************************************************************************/
1951 static void btu_hcif_conn_pkt_type_change_evt (void)
1952 {
1953 }
1954
1955
1956 /*******************************************************************************
1957 **
1958 ** Function         btu_hcif_qos_violation_evt
1959 **
1960 ** Description      Process event HCI_QOS_VIOLATION_EVT
1961 **
1962 ** Returns          void
1963 **
1964 *******************************************************************************/
1965 static void btu_hcif_qos_violation_evt (UINT8 *p)
1966 {
1967     UINT16   handle;
1968
1969     STREAM_TO_UINT16 (handle, p);
1970
1971     handle = HCID_GET_HANDLE (handle);
1972
1973
1974     l2c_link_hci_qos_violation (handle);
1975 }
1976
1977
1978 /*******************************************************************************
1979 **
1980 ** Function         btu_hcif_page_scan_mode_change_evt
1981 **
1982 ** Description      Process event HCI_PAGE_SCAN_MODE_CHANGE_EVT
1983 **
1984 ** Returns          void
1985 **
1986 *******************************************************************************/
1987 static void btu_hcif_page_scan_mode_change_evt (void)
1988 {
1989 }
1990
1991
1992 /*******************************************************************************
1993 **
1994 ** Function         btu_hcif_page_scan_rep_mode_chng_evt
1995 **
1996 ** Description      Process event HCI_PAGE_SCAN_REP_MODE_CHNG_EVT
1997 **
1998 ** Returns          void
1999 **
2000 *******************************************************************************/
2001 static void btu_hcif_page_scan_rep_mode_chng_evt (void)
2002 {
2003 }
2004
2005 /**********************************************
2006 ** Simple Pairing Events
2007 ***********************************************/
2008
2009 /*******************************************************************************
2010 **
2011 ** Function         btu_hcif_host_support_evt
2012 **
2013 ** Description      Process event HCI_RMT_HOST_SUP_FEAT_NOTIFY_EVT
2014 **
2015 ** Returns          void
2016 **
2017 *******************************************************************************/
2018 static void btu_hcif_host_support_evt (UINT8 *p)
2019 {
2020     btm_sec_rmt_host_support_feat_evt(p);
2021 }
2022
2023 /*******************************************************************************
2024 **
2025 ** Function         btu_hcif_io_cap_request_evt
2026 **
2027 ** Description      Process event HCI_IO_CAPABILITY_REQUEST_EVT
2028 **
2029 ** Returns          void
2030 **
2031 *******************************************************************************/
2032 static void btu_hcif_io_cap_request_evt (UINT8 *p)
2033 {
2034     btm_io_capabilities_req(p);
2035 }
2036
2037
2038 /*******************************************************************************
2039 **
2040 ** Function         btu_hcif_io_cap_response_evt
2041 **
2042 ** Description      Process event HCI_IO_CAPABILITY_RESPONSE_EVT
2043 **
2044 ** Returns          void
2045 **
2046 *******************************************************************************/
2047 static void btu_hcif_io_cap_response_evt (UINT8 *p)
2048 {
2049     btm_io_capabilities_rsp(p);
2050 }
2051
2052
2053 /*******************************************************************************
2054 **
2055 ** Function         btu_hcif_user_conf_request_evt
2056 **
2057 ** Description      Process event HCI_USER_CONFIRMATION_REQUEST_EVT
2058 **
2059 ** Returns          void
2060 **
2061 *******************************************************************************/
2062 static void btu_hcif_user_conf_request_evt (UINT8 *p)
2063 {
2064     btm_proc_sp_req_evt(BTM_SP_CFM_REQ_EVT, p);
2065 }
2066
2067
2068 /*******************************************************************************
2069 **
2070 ** Function         btu_hcif_user_passkey_request_evt
2071 **
2072 ** Description      Process event HCI_USER_PASSKEY_REQUEST_EVT
2073 **
2074 ** Returns          void
2075 **
2076 *******************************************************************************/
2077 static void btu_hcif_user_passkey_request_evt (UINT8 *p)
2078 {
2079     btm_proc_sp_req_evt(BTM_SP_KEY_REQ_EVT, p);
2080 }
2081
2082 /*******************************************************************************
2083 **
2084 ** Function         btu_hcif_user_passkey_notif_evt
2085 **
2086 ** Description      Process event HCI_USER_PASSKEY_NOTIFY_EVT
2087 **
2088 ** Returns          void
2089 **
2090 *******************************************************************************/
2091 static void btu_hcif_user_passkey_notif_evt (UINT8 *p)
2092 {
2093     btm_proc_sp_req_evt(BTM_SP_KEY_NOTIF_EVT, p);
2094 }
2095
2096 /*******************************************************************************
2097 **
2098 ** Function         btu_hcif_keypress_notif_evt
2099 **
2100 ** Description      Process event HCI_KEYPRESS_NOTIFY_EVT
2101 **
2102 ** Returns          void
2103 **
2104 *******************************************************************************/
2105 static void btu_hcif_keypress_notif_evt (UINT8 *p)
2106 {
2107     btm_keypress_notif_evt(p);
2108 }
2109
2110 /*******************************************************************************
2111 **
2112 ** Function         btu_hcif_link_super_tout_evt
2113 **
2114 ** Description      Process event HCI_LINK_SUPER_TOUT_CHANGED_EVT
2115 **
2116 ** Returns          void
2117 **
2118 *******************************************************************************/
2119 static void btu_hcif_link_super_tout_evt (UINT8 *p)
2120 {
2121     UINT16 handle, timeout;
2122     STREAM_TO_UINT16 (handle, p);
2123     STREAM_TO_UINT16 (timeout, p);
2124
2125     btm_proc_lsto_evt(handle, timeout);
2126 }
2127
2128 /*******************************************************************************
2129 **
2130 ** Function         btu_hcif_rem_oob_request_evt
2131 **
2132 ** Description      Process event HCI_REMOTE_OOB_DATA_REQUEST_EVT
2133 **
2134 ** Returns          void
2135 **
2136 *******************************************************************************/
2137     #if BTM_OOB_INCLUDED == TRUE
2138 static void btu_hcif_rem_oob_request_evt (UINT8 *p)
2139 {
2140     btm_rem_oob_req(p);
2141 }
2142     #endif
2143
2144 /*******************************************************************************
2145 **
2146 ** Function         btu_hcif_simple_pair_complete_evt
2147 **
2148 ** Description      Process event HCI_SIMPLE_PAIRING_COMPLETE_EVT
2149 **
2150 ** Returns          void
2151 **
2152 *******************************************************************************/
2153 static void btu_hcif_simple_pair_complete_evt (UINT8 *p)
2154 {
2155     btm_simple_pair_complete(p);
2156 }
2157 /*******************************************************************************
2158 **
2159 ** Function         btu_hcif_flush_cmd_queue
2160 **
2161 ** Description      Flush the HCI command complete queue and transmit queue when
2162 **                  needed.
2163 **
2164 ** Returns          void
2165 **
2166 *******************************************************************************/
2167 void btu_hcif_flush_cmd_queue(void)
2168 {
2169     BT_HDR *p_cmd;
2170
2171     btu_cb.hci_cmd_cb[0].cmd_window = 0;
2172     while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_cmpl_q)) != NULL)
2173     {
2174         GKI_freebuf (p_cmd);
2175     }
2176     while ((p_cmd = (BT_HDR *) GKI_dequeue (&btu_cb.hci_cmd_cb[0].cmd_xmit_q)) != NULL)
2177     {
2178         GKI_freebuf (p_cmd);
2179     }
2180 }
2181
2182 /*******************************************************************************
2183 **
2184 ** Function         btu_hcif_enhanced_flush_complete_evt
2185 **
2186 ** Description      Process event HCI_ENHANCED_FLUSH_COMPLETE_EVT
2187 **
2188 ** Returns          void
2189 **
2190 *******************************************************************************/
2191 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
2192 static void btu_hcif_enhanced_flush_complete_evt (void)
2193 {
2194 /* This is empty until an upper layer cares about returning event */
2195 }
2196 #endif
2197 /**********************************************
2198 ** End of Simple Pairing Events
2199 ***********************************************/
2200
2201
2202 /**********************************************
2203 ** BLE Events
2204 ***********************************************/
2205 #if (defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)
2206 static void btu_hcif_encryption_key_refresh_cmpl_evt (UINT8 *p)
2207 {
2208     UINT8   status;
2209     UINT8   enc_enable = 0;
2210     UINT16  handle;
2211
2212     STREAM_TO_UINT8  (status, p);
2213     STREAM_TO_UINT16 (handle, p);
2214
2215     if (status == HCI_SUCCESS) enc_enable = 1;
2216
2217     btm_sec_encrypt_change (handle, status, enc_enable);
2218 }
2219
2220 static void btu_ble_process_adv_pkt (UINT8 *p)
2221 {
2222     HCI_TRACE_EVENT("btu_ble_process_adv_pkt");
2223
2224     btm_ble_process_adv_pkt(p);
2225 }
2226
2227 static void btu_ble_ll_conn_complete_evt ( UINT8 *p, UINT16 evt_len)
2228 {
2229     btm_ble_conn_complete(p, evt_len);
2230 }
2231
2232 static void btu_ble_ll_conn_param_upd_evt (UINT8 *p, UINT16 evt_len)
2233 {
2234     /* LE connection update has completed successfully as a master. */
2235     /* We can enable the update request if the result is a success. */
2236     /* extract the HCI handle first */
2237     UINT8   status;
2238     UINT16  handle;
2239
2240     STREAM_TO_UINT8  (status, p);
2241     STREAM_TO_UINT16 (handle, p);
2242     l2cble_process_conn_update_evt(handle, status);
2243 }
2244
2245 static void btu_ble_read_remote_feat_evt (UINT8 *p)
2246 {
2247     btm_ble_read_remote_features_complete(p);
2248 }
2249
2250 static void btu_ble_proc_ltk_req (UINT8 *p)
2251 {
2252     UINT16 ediv, handle;
2253     UINT8   *pp;
2254
2255     STREAM_TO_UINT16(handle, p);
2256     pp = p + 8;
2257     STREAM_TO_UINT16(ediv, pp);
2258 #if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
2259     btm_ble_ltk_request(handle, p, ediv);
2260 #endif
2261     /* This is empty until an upper layer cares about returning event */
2262 }
2263 /**********************************************
2264 ** End of BLE Events Handler
2265 ***********************************************/
2266 #if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
2267 static void btu_ble_rc_param_req_evt(UINT8 *p)
2268 {
2269     UINT16 handle;
2270     UINT16  int_min, int_max, latency, timeout;
2271
2272     STREAM_TO_UINT16(handle, p);
2273     STREAM_TO_UINT16(int_min, p);
2274     STREAM_TO_UINT16(int_max, p);
2275     STREAM_TO_UINT16(latency, p);
2276     STREAM_TO_UINT16(timeout, p);
2277
2278     l2cble_process_rc_param_request_evt(handle, int_min, int_max, latency, timeout);
2279 }
2280 #endif /* BLE_LLT_INCLUDED */
2281
2282 #endif /* BLE_INCLUDED */
2283