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