blob: 054b2297e31a366b6ddd7a879bc14b8cc16ae419 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2009-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
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
Jack Hef2af1c42016-12-13 01:59:12 -080029#include <base/logging.h>
Ajay Panicker2b7e9052017-03-01 10:00:30 -080030#include <base/threading/thread.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070031#include <fcntl.h>
Chris Manton18023292014-08-29 09:12:06 -070032#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070033#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070034#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070035#include <time.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080036
Marie Janssendb554582015-06-26 14:53:46 -070037#include <hardware/bluetooth.h>
38
Myles Watson911d1ae2016-11-28 16:44:40 -080039#include "bt_common.h"
Matthew Xie66432dc2014-04-27 05:45:32 -070040#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070041#include "bt_utils.h"
Marie Janssendb554582015-06-26 14:53:46 -070042#include "bta_api.h"
Andre Eisenbachcae219f2015-05-18 09:41:06 -070043#include "btcore/include/module.h"
Marie Janssendb554582015-06-26 14:53:46 -070044#include "bte.h"
45#include "btif_common.h"
46#include "btsnoop.h"
47#include "btu.h"
Andre Eisenbache42d1d72016-01-14 02:00:24 -080048#include "device/include/interop.h"
Marie Janssendb554582015-06-26 14:53:46 -070049#include "hci_layer.h"
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070050#include "hcimsgs.h"
Marie Janssendb554582015-06-26 14:53:46 -070051#include "osi/include/alarm.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070052#include "osi/include/fixed_queue.h"
53#include "osi/include/future.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080054#include "osi/include/log.h"
Marie Janssendb554582015-06-26 14:53:46 -070055#include "osi/include/osi.h"
Chris Manton1bb0e512019-09-09 21:11:59 -070056#include "shim/shim.h"
Marie Janssendb554582015-06-26 14:53:46 -070057#include "stack_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080058
59/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080060 * Constants & Macros
61 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080062
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070063/* Run-time configuration file for BLE*/
64#ifndef BTE_BLE_STACK_CONF_FILE
Arman Ugurayf2d64342015-07-08 15:47:39 -070065// TODO(armansito): Find a better way than searching by a hardcoded path.
66#if defined(OS_GENERIC)
67#define BTE_BLE_STACK_CONF_FILE "ble_stack.conf"
68#else // !defined(OS_GENERIC)
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070069#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
Arman Ugurayf2d64342015-07-08 15:47:39 -070070#endif // defined(OS_GENERIC)
71#endif // BT_BLE_STACK_CONF_FILE
The Android Open Source Project5738f832012-12-12 16:00:35 -080072
The Android Open Source Project5738f832012-12-12 16:00:35 -080073/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080074 * Variables
75 *****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080076
The Android Open Source Project5738f832012-12-12 16:00:35 -080077/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080078 * Static variables
79 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -080080static const hci_t* hci;
The Android Open Source Project5738f832012-12-12 16:00:35 -080081
82/*******************************************************************************
Ajay Panicker2b7e9052017-03-01 10:00:30 -080083 * Externs
84 ******************************************************************************/
85extern void btu_hci_msg_process(BT_HDR* p_msg);
86
87/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080088 * Static functions
89 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
Ajay Panicker2b7e9052017-03-01 10:00:30 -080091/******************************************************************************
92 *
93 * Function post_to_hci_message_loop
94 *
Jack He96d16e32018-08-27 13:55:14 -070095 * Description Post an HCI event to the main thread
Ajay Panicker2b7e9052017-03-01 10:00:30 -080096 *
97 * Returns None
98 *
99 *****************************************************************************/
Jakub Pawlowski67f5f372018-07-23 10:00:25 -0700100void post_to_main_message_loop(const base::Location& from_here, BT_HDR* p_msg) {
Jack He96d16e32018-08-27 13:55:14 -0700101 if (do_in_main_thread(from_here, base::Bind(&btu_hci_msg_process, p_msg)) !=
102 BT_STATUS_SUCCESS) {
103 LOG(ERROR) << __func__ << ": do_in_main_thread failed from "
104 << from_here.ToString();
Ajay Panicker2b7e9052017-03-01 10:00:30 -0800105 }
Ajay Panicker2b7e9052017-03-01 10:00:30 -0800106}
Chris Manton307381e2014-09-04 19:48:49 -0700107
The Android Open Source Project5738f832012-12-12 16:00:35 -0800108/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800109 *
110 * Function bte_main_boot_entry
111 *
112 * Description BTE MAIN API - Entry point for BTE chip/stack initialization
113 *
114 * Returns None
115 *
116 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800117void bte_main_boot_entry(void) {
118 module_init(get_module(INTEROP_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119
Myles Watson911d1ae2016-11-28 16:44:40 -0800120 hci = hci_layer_get_interface();
Myles Watson5d700dd2017-03-21 10:24:20 -0700121 if (!hci) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800122 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
Myles Watson5d700dd2017-03-21 10:24:20 -0700123 return;
124 }
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700125
Jack He96d16e32018-08-27 13:55:14 -0700126 hci->set_data_cb(base::Bind(&post_to_main_message_loop));
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700127
Myles Watson911d1ae2016-11-28 16:44:40 -0800128 module_init(get_module(STACK_CONFIG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129}
130
131/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800132 *
133 * Function bte_main_cleanup
134 *
135 * Description BTE MAIN API - Cleanup code for BTE chip/stack
136 *
137 * Returns None
138 *
139 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800140void bte_main_cleanup() {
Myles Watson911d1ae2016-11-28 16:44:40 -0800141 module_clean_up(get_module(STACK_CONFIG_MODULE));
Zach Johnson9891f322014-09-22 22:11:55 -0700142
Myles Watson911d1ae2016-11-28 16:44:40 -0800143 module_clean_up(get_module(INTEROP_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144}
145
146/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800147 *
148 * Function bte_main_enable
149 *
150 * Description BTE MAIN API - Creates all the BTE tasks. Should be called
151 * part of the Bluetooth stack enable sequence
152 *
153 * Returns None
154 *
155 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800156void bte_main_enable() {
157 APPL_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158
Chris Manton1bb0e512019-09-09 21:11:59 -0700159 if (bluetooth::shim::is_gd_shim_enabled()) {
160 LOG_INFO(LOG_TAG, "%s Gd shim module enabled", __func__);
161 } else {
162 module_start_up(get_module(BTSNOOP_MODULE));
163 module_start_up(get_module(HCI_MODULE));
164 }
Zach Johnson093948a2014-09-23 18:30:45 -0700165
Myles Watson911d1ae2016-11-28 16:44:40 -0800166 BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700167}
168
169/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800170 *
171 * Function bte_main_disable
172 *
173 * Description BTE MAIN API - Destroys all the BTE tasks. Should be called
174 * part of the Bluetooth stack disable sequence
175 *
176 * Returns None
177 *
178 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800179void bte_main_disable(void) {
180 APPL_TRACE_DEBUG("%s", __func__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700181
Chris Manton1bb0e512019-09-09 21:11:59 -0700182 if (bluetooth::shim::is_gd_shim_enabled()) {
183 LOG_INFO(LOG_TAG, "%s Gd shim module enabled", __func__);
184 module_shut_down(get_module(GD_SHIM_MODULE));
185 } else {
186 module_shut_down(get_module(HCI_MODULE));
187 module_shut_down(get_module(BTSNOOP_MODULE));
188 }
Zach Johnson733a06e2014-09-08 18:31:39 -0700189
Myles Watson911d1ae2016-11-28 16:44:40 -0800190 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700191}
192
193/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800194 *
195 * Function bte_main_postload_cfg
196 *
197 * Description BTE MAIN API - Stack postload configuration
198 *
199 * Returns None
200 *
201 *****************************************************************************/
Andre Eisenbach796523d2016-11-10 16:11:00 -0800202void bte_main_postload_cfg(void) {
203 // TODO(eisenbach): [HIDL] DEPRECATE?
204}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800205
The Android Open Source Project5738f832012-12-12 16:00:35 -0800206/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800207 *
208 * Function bte_main_hci_send
209 *
210 * Description BTE MAIN API - This function is called by the upper stack to
211 * send an HCI message. The function displays a protocol trace
212 * message (if enabled), and then calls the 'transmit' function
213 * associated with the currently selected HCI transport
214 *
215 * Returns None
216 *
217 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800218void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) {
219 uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800220
Myles Watson911d1ae2016-11-28 16:44:40 -0800221 p_msg->event = event;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222
Myles Watson911d1ae2016-11-28 16:44:40 -0800223 if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) ||
224 (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
225 hci->transmit_downward(event, p_msg);
226 } else {
227 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
228 osi_free(p_msg);
229 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800230}