1 /******************************************************************************
2 *
3 * Copyright (C) 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 <fcntl.h>
31 #include <pthread.h>
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <time.h>
35
36 #include <hardware/bluetooth.h>
37
38 #include "bt_common.h"
39 #include "bt_hci_bdroid.h"
40 #include "bt_utils.h"
41 #include "bta_api.h"
42 #include "btcore/include/module.h"
43 #include "bte.h"
44 #include "btif_common.h"
45 #include "btsnoop.h"
46 #include "btu.h"
47 #include "device/include/interop.h"
48 #include "hci_layer.h"
49 #include "hcimsgs.h"
50 #include "osi/include/alarm.h"
51 #include "osi/include/fixed_queue.h"
52 #include "osi/include/future.h"
53 #include "osi/include/log.h"
54 #include "osi/include/osi.h"
55 #include "osi/include/thread.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 * Static functions
83 ******************************************************************************/
84
85 /*******************************************************************************
86 * Externs
87 ******************************************************************************/
88 fixed_queue_t* btu_hci_msg_queue;
89
90 /******************************************************************************
91 *
92 * Function bte_main_boot_entry
93 *
94 * Description BTE MAIN API - Entry point for BTE chip/stack initialization
95 *
96 * Returns None
97 *
98 *****************************************************************************/
bte_main_boot_entry(void)99 void bte_main_boot_entry(void) {
100 module_init(get_module(INTEROP_MODULE));
101
102 hci = hci_layer_get_interface();
103 if (!hci) {
104 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
105 return;
106 }
107
108 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
109 if (btu_hci_msg_queue == NULL) {
110 LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__);
111 return;
112 }
113
114 data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
115 hci->set_data_queue(btu_hci_msg_queue);
116
117 module_init(get_module(STACK_CONFIG_MODULE));
118 }
119
120 /******************************************************************************
121 *
122 * Function bte_main_cleanup
123 *
124 * Description BTE MAIN API - Cleanup code for BTE chip/stack
125 *
126 * Returns None
127 *
128 *****************************************************************************/
bte_main_cleanup()129 void bte_main_cleanup() {
130 data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher,
131 NULL);
132 hci->set_data_queue(NULL);
133 fixed_queue_free(btu_hci_msg_queue, NULL);
134
135 btu_hci_msg_queue = NULL;
136
137 module_clean_up(get_module(STACK_CONFIG_MODULE));
138
139 module_clean_up(get_module(INTEROP_MODULE));
140 }
141
142 /******************************************************************************
143 *
144 * Function bte_main_enable
145 *
146 * Description BTE MAIN API - Creates all the BTE tasks. Should be called
147 * part of the Bluetooth stack enable sequence
148 *
149 * Returns None
150 *
151 *****************************************************************************/
bte_main_enable()152 void bte_main_enable() {
153 APPL_TRACE_DEBUG("%s", __func__);
154
155 module_start_up(get_module(BTSNOOP_MODULE));
156 module_start_up(get_module(HCI_MODULE));
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 module_shut_down(get_module(HCI_MODULE));
175 module_shut_down(get_module(BTSNOOP_MODULE));
176
177 BTU_ShutDown();
178 }
179
180 /******************************************************************************
181 *
182 * Function bte_main_postload_cfg
183 *
184 * Description BTE MAIN API - Stack postload configuration
185 *
186 * Returns None
187 *
188 *****************************************************************************/
bte_main_postload_cfg(void)189 void bte_main_postload_cfg(void) {
190 // TODO(eisenbach): [HIDL] DEPRECATE?
191 }
192
193 /******************************************************************************
194 *
195 * Function bte_main_hci_send
196 *
197 * Description BTE MAIN API - This function is called by the upper stack to
198 * send an HCI message. The function displays a protocol trace
199 * message (if enabled), and then calls the 'transmit' function
200 * associated with the currently selected HCI transport
201 *
202 * Returns None
203 *
204 *****************************************************************************/
bte_main_hci_send(BT_HDR * p_msg,uint16_t event)205 void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) {
206 uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
207
208 p_msg->event = event;
209
210 if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) ||
211 (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
212 hci->transmit_downward(event, p_msg);
213 } else {
214 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
215 osi_free(p_msg);
216 }
217 }
218