1 /******************************************************************************
2  *
3  *  Copyright 2009-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  *  Filename:      bte_main.cc
22  *
23  *  Description:   Contains BTE core stack initialization and shutdown code
24  *
25  ******************************************************************************/
26 
27 #define LOG_TAG "bt_main"
28 
29 #include <base/logging.h>
30 #include <hardware/bluetooth.h>
31 
32 #include "bt_common.h"
33 #include "btcore/include/module.h"
34 #include "bte.h"
35 #include "btif/include/btif_config.h"
36 #include "btsnoop.h"
37 #include "btu.h"
38 #include "device/include/interop.h"
39 #include "hci_layer.h"
40 #include "osi/include/log.h"
41 #include "osi/include/osi.h"
42 #include "shim/hci_layer.h"
43 #include "shim/shim.h"
44 #include "stack_config.h"
45 
46 /*******************************************************************************
47  *  Static variables
48  ******************************************************************************/
49 static const hci_t* hci;
50 
51 /*******************************************************************************
52  *  Externs
53  ******************************************************************************/
54 extern void btu_hci_msg_process(BT_HDR* p_msg);
55 
56 /*******************************************************************************
57  *  Static functions
58  ******************************************************************************/
59 
60 /******************************************************************************
61  *
62  * Function         post_to_hci_message_loop
63  *
64  * Description      Post an HCI event to the main thread
65  *
66  * Returns          None
67  *
68  *****************************************************************************/
post_to_main_message_loop(const base::Location & from_here,BT_HDR * p_msg)69 void post_to_main_message_loop(const base::Location& from_here, BT_HDR* p_msg) {
70   if (do_in_main_thread(from_here, base::Bind(&btu_hci_msg_process, p_msg)) !=
71       BT_STATUS_SUCCESS) {
72     LOG(ERROR) << __func__ << ": do_in_main_thread failed from "
73                << from_here.ToString();
74   }
75 }
76 
bte_main_init(void)77 void bte_main_init(void) {
78   hci = hci_layer_get_interface();
79   if (!hci) {
80     LOG_ERROR("%s could not get hci layer interface.", __func__);
81     return;
82   }
83 
84   hci->set_data_cb(base::Bind(&post_to_main_message_loop));
85 }
86 
87 /******************************************************************************
88  *
89  * Function         bte_main_hci_send
90  *
91  * Description      BTE MAIN API - This function is called by the upper stack to
92  *                  send an HCI message. The function displays a protocol trace
93  *                  message (if enabled), and then calls the 'transmit' function
94  *                  associated with the currently selected HCI transport
95  *
96  * Returns          None
97  *
98  *****************************************************************************/
bte_main_hci_send(BT_HDR * p_msg,uint16_t event)99 void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) {
100   uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
101 
102   p_msg->event = event;
103 
104   if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) ||
105       (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
106     hci->transmit_downward(event, p_msg);
107   } else {
108     APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
109     osi_free(p_msg);
110   }
111 }
112