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 
31 #include <hardware/bluetooth.h>
32 
33 #include "bt_common.h"
34 #include "btcore/include/module.h"
35 #include "bte.h"
36 #include "btif/include/btif_config.h"
37 #include "btsnoop.h"
38 #include "btu.h"
39 #include "device/include/interop.h"
40 #include "hci_layer.h"
41 #include "osi/include/log.h"
42 #include "osi/include/osi.h"
43 #include "shim/hci_layer.h"
44 #include "shim/shim.h"
45 #include "stack_config.h"
46 
47 /*******************************************************************************
48  *  Constants & Macros
49  ******************************************************************************/
50 
51 /* Run-time configuration file for BLE*/
52 #ifndef BTE_BLE_STACK_CONF_FILE
53 // TODO(armansito): Find a better way than searching by a hardcoded path.
54 #if defined(OS_GENERIC)
55 #define BTE_BLE_STACK_CONF_FILE "ble_stack.conf"
56 #else  // !defined(OS_GENERIC)
57 #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
58 #endif  // defined(OS_GENERIC)
59 #endif  // BT_BLE_STACK_CONF_FILE
60 
61 /******************************************************************************
62  *  Variables
63  *****************************************************************************/
64 
65 /*******************************************************************************
66  *  Static variables
67  ******************************************************************************/
68 static const hci_t* hci;
69 
70 /*******************************************************************************
71  *  Externs
72  ******************************************************************************/
73 extern void btu_hci_msg_process(BT_HDR* p_msg);
74 
75 /*******************************************************************************
76  *  Static functions
77  ******************************************************************************/
78 
79 /******************************************************************************
80  *
81  * Function         post_to_hci_message_loop
82  *
83  * Description      Post an HCI event to the main thread
84  *
85  * Returns          None
86  *
87  *****************************************************************************/
post_to_main_message_loop(const base::Location & from_here,BT_HDR * p_msg)88 void post_to_main_message_loop(const base::Location& from_here, BT_HDR* p_msg) {
89   if (do_in_main_thread(from_here, base::Bind(&btu_hci_msg_process, p_msg)) !=
90       BT_STATUS_SUCCESS) {
91     LOG(ERROR) << __func__ << ": do_in_main_thread failed from "
92                << from_here.ToString();
93   }
94 }
95 
96 /******************************************************************************
97  *
98  * Function         bte_main_boot_entry
99  *
100  * Description      BTE MAIN API - Entry point for BTE chip/stack initialization
101  *
102  * Returns          None
103  *
104  *****************************************************************************/
bte_main_boot_entry(void)105 void bte_main_boot_entry(void) {
106   module_init(get_module(INTEROP_MODULE));
107 
108   hci = hci_layer_get_interface();
109   if (!hci) {
110     LOG_ERROR("%s could not get hci layer interface.", __func__);
111     return;
112   }
113 
114   hci->set_data_cb(base::Bind(&post_to_main_message_loop));
115 
116   module_init(get_module(STACK_CONFIG_MODULE));
117 }
118 
119 /******************************************************************************
120  *
121  * Function         bte_main_cleanup
122  *
123  * Description      BTE MAIN API - Cleanup code for BTE chip/stack
124  *
125  * Returns          None
126  *
127  *****************************************************************************/
bte_main_cleanup()128 void bte_main_cleanup() {
129   module_clean_up(get_module(STACK_CONFIG_MODULE));
130 
131   module_clean_up(get_module(INTEROP_MODULE));
132 }
133 
134 /******************************************************************************
135  *
136  * Function         bte_main_enable
137  *
138  * Description      BTE MAIN API - Creates all the BTE tasks. Should be called
139  *                  part of the Bluetooth stack enable sequence
140  *
141  * Returns          None
142  *
143  *****************************************************************************/
bte_main_enable()144 void bte_main_enable() {
145   APPL_TRACE_DEBUG("%s", __func__);
146 
147   if (bluetooth::shim::is_gd_shim_enabled()) {
148     LOG_INFO("%s Gd shim module enabled", __func__);
149     module_shut_down(get_module(GD_IDLE_MODULE));
150     module_start_up(get_module(GD_SHIM_MODULE));
151     module_start_up(get_module(BTIF_CONFIG_MODULE));
152   } else {
153     module_start_up(get_module(BTIF_CONFIG_MODULE));
154     module_start_up(get_module(BTSNOOP_MODULE));
155     module_start_up(get_module(HCI_MODULE));
156   }
157 
158   BTU_StartUp();
159 }
160 
161 /******************************************************************************
162  *
163  * Function         bte_main_disable
164  *
165  * Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
166  *                  part of the Bluetooth stack disable sequence
167  *
168  * Returns          None
169  *
170  *****************************************************************************/
bte_main_disable(void)171 void bte_main_disable(void) {
172   APPL_TRACE_DEBUG("%s", __func__);
173 
174   if (bluetooth::shim::is_gd_shim_enabled()) {
175     LOG_INFO("%s Gd shim module enabled", __func__);
176     module_shut_down(get_module(GD_SHIM_MODULE));
177     module_start_up(get_module(GD_IDLE_MODULE));
178   } else {
179     module_shut_down(get_module(HCI_MODULE));
180     module_shut_down(get_module(BTSNOOP_MODULE));
181   }
182 
183   BTU_ShutDown();
184 }
185 
186 /******************************************************************************
187  *
188  * Function         bte_main_postload_cfg
189  *
190  * Description      BTE MAIN API - Stack postload configuration
191  *
192  * Returns          None
193  *
194  *****************************************************************************/
bte_main_postload_cfg(void)195 void bte_main_postload_cfg(void) {
196   // TODO(eisenbach): [HIDL] DEPRECATE?
197 }
198 
199 /******************************************************************************
200  *
201  * Function         bte_main_hci_send
202  *
203  * Description      BTE MAIN API - This function is called by the upper stack to
204  *                  send an HCI message. The function displays a protocol trace
205  *                  message (if enabled), and then calls the 'transmit' function
206  *                  associated with the currently selected HCI transport
207  *
208  * Returns          None
209  *
210  *****************************************************************************/
bte_main_hci_send(BT_HDR * p_msg,uint16_t event)211 void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) {
212   uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
213 
214   p_msg->event = event;
215 
216   if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) ||
217       (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
218     hci->transmit_downward(event, p_msg);
219   } else {
220     APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
221     osi_free(p_msg);
222   }
223 }
224