1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * Filename: bte_main.c
23 * Description: Contains BTE core stack initialization and shutdown code
25 ******************************************************************************/
34 #include <hardware/bluetooth.h>
36 #include "bt_hci_bdroid.h"
39 #include "btcore/include/counter.h"
40 #include "btcore/include/module.h"
42 #include "btif_common.h"
46 #include "hci_layer.h"
47 #include "osi/include/alarm.h"
48 #include "osi/include/fixed_queue.h"
49 #include "osi/include/future.h"
50 #include "osi/include/hash_functions.h"
51 #include "osi/include/hash_map.h"
52 #include "osi/include/log.h"
53 #include "osi/include/osi.h"
54 #include "osi/include/thread.h"
55 #include "stack_config.h"
57 /*******************************************************************************
59 *******************************************************************************/
61 #define LOG_TAG "bt_main"
63 /* Run-time configuration file for BLE*/
64 #ifndef BTE_BLE_STACK_CONF_FILE
65 #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
68 /******************************************************************************
70 ******************************************************************************/
72 /*******************************************************************************
74 *******************************************************************************/
75 static const hci_t *hci;
77 /*******************************************************************************
79 *******************************************************************************/
81 /*******************************************************************************
83 *******************************************************************************/
84 extern void bte_load_ble_conf(const char *p_path);
85 fixed_queue_t *btu_hci_msg_queue;
87 /******************************************************************************
89 ** Function bte_main_boot_entry
91 ** Description BTE MAIN API - Entry point for BTE chip/stack initialization
95 ******************************************************************************/
96 void bte_main_boot_entry(void)
98 module_init(get_module(GKI_MODULE));
99 module_init(get_module(COUNTER_MODULE));
101 hci = hci_layer_get_interface();
103 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
105 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
106 if (btu_hci_msg_queue == NULL) {
107 LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__);
111 data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
112 hci->set_data_queue(btu_hci_msg_queue);
114 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
115 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
117 module_init(get_module(STACK_CONFIG_MODULE));
120 /******************************************************************************
122 ** Function bte_main_shutdown
124 ** Description BTE MAIN API - Shutdown code for BTE chip/stack
128 ******************************************************************************/
129 void bte_main_shutdown()
131 data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
132 hci->set_data_queue(NULL);
133 fixed_queue_free(btu_hci_msg_queue, NULL);
135 btu_hci_msg_queue = NULL;
137 module_clean_up(get_module(STACK_CONFIG_MODULE));
139 module_clean_up(get_module(COUNTER_MODULE));
140 module_clean_up(get_module(GKI_MODULE));
143 /******************************************************************************
145 ** Function bte_main_enable
147 ** Description BTE MAIN API - Creates all the BTE tasks. Should be called
148 ** part of the Bluetooth stack enable sequence
152 ******************************************************************************/
153 void bte_main_enable()
155 APPL_TRACE_DEBUG("%s", __FUNCTION__);
157 module_start_up(get_module(BTSNOOP_MODULE));
158 module_start_up(get_module(HCI_MODULE));
163 /******************************************************************************
165 ** Function bte_main_disable
167 ** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
168 ** part of the Bluetooth stack disable sequence
172 ******************************************************************************/
173 void bte_main_disable(void)
175 APPL_TRACE_DEBUG("%s", __FUNCTION__);
177 module_shut_down(get_module(HCI_MODULE));
178 module_shut_down(get_module(BTSNOOP_MODULE));
183 /******************************************************************************
185 ** Function bte_main_postload_cfg
187 ** Description BTE MAIN API - Stack postload configuration
191 ******************************************************************************/
192 void bte_main_postload_cfg(void)
197 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
198 /******************************************************************************
200 ** Function bte_main_enable_lpm
202 ** Description BTE MAIN API - Enable/Disable low power mode operation
206 ******************************************************************************/
207 void bte_main_enable_lpm(BOOLEAN enable)
209 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
212 /******************************************************************************
214 ** Function bte_main_lpm_allow_bt_device_sleep
216 ** Description BTE MAIN API - Allow BT controller goest to sleep
220 ******************************************************************************/
221 void bte_main_lpm_allow_bt_device_sleep()
223 hci->send_low_power_command(LPM_WAKE_DEASSERT);
226 /******************************************************************************
228 ** Function bte_main_lpm_wake_bt_device
230 ** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
234 ******************************************************************************/
235 void bte_main_lpm_wake_bt_device()
237 hci->send_low_power_command(LPM_WAKE_ASSERT);
239 #endif // HCILP_INCLUDED
241 /******************************************************************************
243 ** Function bte_main_hci_send
245 ** Description BTE MAIN API - This function is called by the upper stack to
246 ** send an HCI message. The function displays a protocol trace
247 ** message (if enabled), and then calls the 'transmit' function
248 ** associated with the currently selected HCI transport
252 ******************************************************************************/
253 void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
255 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
257 p_msg->event = event;
259 counter_add("main.tx.packets", 1);
260 counter_add("main.tx.bytes", p_msg->len);
262 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
263 (sub_event == LOCAL_BLE_CONTROLLER_ID))
265 hci->transmit_downward(event, p_msg);
269 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");