blob: 470575662bf14ea224cbf77c80af96ccbe61fbb8 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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 *
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070021 * Filename: bte_main.cc
The Android Open Source Project5738f832012-12-12 16:00:35 -080022 *
23 * Description: Contains BTE core stack initialization and shutdown code
24 *
25 ******************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080026
Marie Janssen49120dc2015-07-07 16:47:20 -070027#define LOG_TAG "bt_main"
28
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include <assert.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070030#include <fcntl.h>
Chris Manton18023292014-08-29 09:12:06 -070031#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070032#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070033#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070034#include <time.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Marie Janssendb554582015-06-26 14:53:46 -070036#include <hardware/bluetooth.h>
37
Matthew Xie66432dc2014-04-27 05:45:32 -070038#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070039#include "bt_utils.h"
Marie Janssendb554582015-06-26 14:53:46 -070040#include "bta_api.h"
Andre Eisenbachcae219f2015-05-18 09:41:06 -070041#include "btcore/include/module.h"
Marie Janssendb554582015-06-26 14:53:46 -070042#include "bte.h"
43#include "btif_common.h"
44#include "btsnoop.h"
45#include "btu.h"
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070046#include "bt_common.h"
Andre Eisenbache42d1d72016-01-14 02:00:24 -080047#include "device/include/interop.h"
Marie Janssendb554582015-06-26 14:53:46 -070048#include "hci_layer.h"
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070049#include "hcimsgs.h"
Marie Janssendb554582015-06-26 14:53:46 -070050#include "osi/include/alarm.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070051#include "osi/include/fixed_queue.h"
52#include "osi/include/future.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080053#include "osi/include/log.h"
Marie Janssendb554582015-06-26 14:53:46 -070054#include "osi/include/osi.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070055#include "osi/include/thread.h"
Marie Janssendb554582015-06-26 14:53:46 -070056#include "stack_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080057
58/*******************************************************************************
59** Constants & Macros
60*******************************************************************************/
61
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070062/* Run-time configuration file for BLE*/
63#ifndef BTE_BLE_STACK_CONF_FILE
Arman Ugurayf2d64342015-07-08 15:47:39 -070064// 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)
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070068#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
Arman Ugurayf2d64342015-07-08 15:47:39 -070069#endif // defined(OS_GENERIC)
70#endif // BT_BLE_STACK_CONF_FILE
The Android Open Source Project5738f832012-12-12 16:00:35 -080071
The Android Open Source Project5738f832012-12-12 16:00:35 -080072/******************************************************************************
73** Variables
74******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080075
The Android Open Source Project5738f832012-12-12 16:00:35 -080076/*******************************************************************************
77** Static variables
78*******************************************************************************/
Zach Johnsonbf8193b2014-09-08 09:56:35 -070079static const hci_t *hci;
The Android Open Source Project5738f832012-12-12 16:00:35 -080080
81/*******************************************************************************
82** Static functions
83*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080084
85/*******************************************************************************
86** Externs
87*******************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -070088fixed_queue_t *btu_hci_msg_queue;
89
The Android Open Source Project5738f832012-12-12 16:00:35 -080090/******************************************************************************
91**
The Android Open Source Project5738f832012-12-12 16:00:35 -080092** Function bte_main_boot_entry
93**
94** Description BTE MAIN API - Entry point for BTE chip/stack initialization
95**
96** Returns None
97**
98******************************************************************************/
99void bte_main_boot_entry(void)
100{
Andre Eisenbache42d1d72016-01-14 02:00:24 -0800101 module_init(get_module(INTEROP_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800102
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700103 hci = hci_layer_get_interface();
104 if (!hci)
Marie Janssendb554582015-06-26 14:53:46 -0700105 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700106
Chris Manton307381e2014-09-04 19:48:49 -0700107 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
108 if (btu_hci_msg_queue == NULL) {
Marie Janssendb554582015-06-26 14:53:46 -0700109 LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__);
Chris Manton307381e2014-09-04 19:48:49 -0700110 return;
111 }
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700112
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800113 data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
114 hci->set_data_queue(btu_hci_msg_queue);
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700115
Zach Johnson9891f322014-09-22 22:11:55 -0700116 module_init(get_module(STACK_CONFIG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800117}
118
119/******************************************************************************
120**
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700121** Function bte_main_cleanup
The Android Open Source Project5738f832012-12-12 16:00:35 -0800122**
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700123** Description BTE MAIN API - Cleanup code for BTE chip/stack
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124**
125** Returns None
126**
127******************************************************************************/
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700128void bte_main_cleanup()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129{
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800130 data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
131 hci->set_data_queue(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700132 fixed_queue_free(btu_hci_msg_queue, NULL);
Zach Johnson733a06e2014-09-08 18:31:39 -0700133
Chris Manton307381e2014-09-04 19:48:49 -0700134 btu_hci_msg_queue = NULL;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700135
Zach Johnson9891f322014-09-22 22:11:55 -0700136 module_clean_up(get_module(STACK_CONFIG_MODULE));
137
Andre Eisenbache42d1d72016-01-14 02:00:24 -0800138 module_clean_up(get_module(INTEROP_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139}
140
141/******************************************************************************
142**
143** Function bte_main_enable
144**
145** Description BTE MAIN API - Creates all the BTE tasks. Should be called
146** part of the Bluetooth stack enable sequence
147**
148** Returns None
149**
150******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700151void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152{
Marie Janssend19e0782016-07-15 12:48:27 -0700153 APPL_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154
Zach Johnson9891f322014-09-22 22:11:55 -0700155 module_start_up(get_module(BTSNOOP_MODULE));
Zach Johnson093948a2014-09-23 18:30:45 -0700156 module_start_up(get_module(HCI_MODULE));
157
158 BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700159}
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******************************************************************************/
171void bte_main_disable(void)
172{
Marie Janssend19e0782016-07-15 12:48:27 -0700173 APPL_TRACE_DEBUG("%s", __func__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700174
Zach Johnson093948a2014-09-23 18:30:45 -0700175 module_shut_down(get_module(HCI_MODULE));
Zach Johnson9891f322014-09-22 22:11:55 -0700176 module_shut_down(get_module(BTSNOOP_MODULE));
Zach Johnson733a06e2014-09-08 18:31:39 -0700177
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700178 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700179}
180
181/******************************************************************************
182**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800183** Function bte_main_postload_cfg
184**
185** Description BTE MAIN API - Stack postload configuration
186**
187** Returns None
188**
189******************************************************************************/
190void bte_main_postload_cfg(void)
191{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700192 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193}
194
Marie Janssend19e0782016-07-15 12:48:27 -0700195#if (HCILP_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800196/******************************************************************************
197**
198** Function bte_main_enable_lpm
199**
200** Description BTE MAIN API - Enable/Disable low power mode operation
201**
202** Returns None
203**
204******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700205void bte_main_enable_lpm(bool enable)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800206{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700207 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800208}
209
210/******************************************************************************
211**
212** Function bte_main_lpm_allow_bt_device_sleep
213**
Miao Choufb685ef2015-08-11 11:41:58 -0700214** Description BTE MAIN API - Allow the BT controller to go to sleep
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215**
216** Returns None
217**
218******************************************************************************/
219void bte_main_lpm_allow_bt_device_sleep()
220{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700221 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222}
223
224/******************************************************************************
225**
226** Function bte_main_lpm_wake_bt_device
227**
228** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
229**
230** Returns None
231**
232******************************************************************************/
233void bte_main_lpm_wake_bt_device()
234{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700235 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800236}
237#endif // HCILP_INCLUDED
238
239/******************************************************************************
240**
241** Function bte_main_hci_send
242**
243** Description BTE MAIN API - This function is called by the upper stack to
244** send an HCI message. The function displays a protocol trace
245** message (if enabled), and then calls the 'transmit' function
246** associated with the currently selected HCI transport
247**
248** Returns None
249**
250******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700251void bte_main_hci_send (BT_HDR *p_msg, uint16_t event)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800252{
Marie Janssend19e0782016-07-15 12:48:27 -0700253 uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800254
255 p_msg->event = event;
256
257
258 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
259 (sub_event == LOCAL_BLE_CONTROLLER_ID))
260 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700261 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800262 }
263 else
264 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700265 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800266 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800267 }
268}