The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2009-2012 Broadcom Corporation |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 21 | * Filename: bte_main.cc |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 22 | * |
| 23 | * Description: Contains BTE core stack initialization and shutdown code |
| 24 | * |
| 25 | ******************************************************************************/ |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 26 | |
Marie Janssen | 49120dc | 2015-07-07 16:47:20 -0700 | [diff] [blame] | 27 | #define LOG_TAG "bt_main" |
| 28 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 29 | #include <base/logging.h> |
Ajay Panicker | 2b7e905 | 2017-03-01 10:00:30 -0800 | [diff] [blame] | 30 | #include <base/threading/thread.h> |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 31 | #include <fcntl.h> |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 32 | #include <pthread.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 33 | #include <signal.h> |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 34 | #include <stdlib.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 35 | #include <time.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 36 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 37 | #include <hardware/bluetooth.h> |
| 38 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 39 | #include "bt_common.h" |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 40 | #include "bt_hci_bdroid.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 41 | #include "bt_utils.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 42 | #include "bta_api.h" |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 43 | #include "btcore/include/module.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 44 | #include "bte.h" |
| 45 | #include "btif_common.h" |
| 46 | #include "btsnoop.h" |
| 47 | #include "btu.h" |
Andre Eisenbach | e42d1d7 | 2016-01-14 02:00:24 -0800 | [diff] [blame] | 48 | #include "device/include/interop.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 49 | #include "hci_layer.h" |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 50 | #include "hcimsgs.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 51 | #include "osi/include/alarm.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 52 | #include "osi/include/fixed_queue.h" |
| 53 | #include "osi/include/future.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 54 | #include "osi/include/log.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 55 | #include "osi/include/osi.h" |
Chris Manton | d434863 | 2019-09-11 09:28:41 -0700 | [diff] [blame] | 56 | #include "shim/hci_layer.h" |
Chris Manton | 1bb0e51 | 2019-09-09 21:11:59 -0700 | [diff] [blame] | 57 | #include "shim/shim.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 58 | #include "stack_config.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 59 | |
| 60 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 61 | * Constants & Macros |
| 62 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 63 | |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 64 | /* Run-time configuration file for BLE*/ |
| 65 | #ifndef BTE_BLE_STACK_CONF_FILE |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 66 | // TODO(armansito): Find a better way than searching by a hardcoded path. |
| 67 | #if defined(OS_GENERIC) |
| 68 | #define BTE_BLE_STACK_CONF_FILE "ble_stack.conf" |
| 69 | #else // !defined(OS_GENERIC) |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 70 | #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf" |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 71 | #endif // defined(OS_GENERIC) |
| 72 | #endif // BT_BLE_STACK_CONF_FILE |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 73 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 74 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 75 | * Variables |
| 76 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 77 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 78 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 79 | * Static variables |
| 80 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 81 | static const hci_t* hci; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 82 | |
| 83 | /******************************************************************************* |
Ajay Panicker | 2b7e905 | 2017-03-01 10:00:30 -0800 | [diff] [blame] | 84 | * Externs |
| 85 | ******************************************************************************/ |
| 86 | extern void btu_hci_msg_process(BT_HDR* p_msg); |
| 87 | |
| 88 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 89 | * Static functions |
| 90 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 91 | |
Ajay Panicker | 2b7e905 | 2017-03-01 10:00:30 -0800 | [diff] [blame] | 92 | /****************************************************************************** |
| 93 | * |
| 94 | * Function post_to_hci_message_loop |
| 95 | * |
Jack He | 96d16e3 | 2018-08-27 13:55:14 -0700 | [diff] [blame] | 96 | * Description Post an HCI event to the main thread |
Ajay Panicker | 2b7e905 | 2017-03-01 10:00:30 -0800 | [diff] [blame] | 97 | * |
| 98 | * Returns None |
| 99 | * |
| 100 | *****************************************************************************/ |
Jakub Pawlowski | 67f5f37 | 2018-07-23 10:00:25 -0700 | [diff] [blame] | 101 | void post_to_main_message_loop(const base::Location& from_here, BT_HDR* p_msg) { |
Jack He | 96d16e3 | 2018-08-27 13:55:14 -0700 | [diff] [blame] | 102 | if (do_in_main_thread(from_here, base::Bind(&btu_hci_msg_process, p_msg)) != |
| 103 | BT_STATUS_SUCCESS) { |
| 104 | LOG(ERROR) << __func__ << ": do_in_main_thread failed from " |
| 105 | << from_here.ToString(); |
Ajay Panicker | 2b7e905 | 2017-03-01 10:00:30 -0800 | [diff] [blame] | 106 | } |
Ajay Panicker | 2b7e905 | 2017-03-01 10:00:30 -0800 | [diff] [blame] | 107 | } |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 108 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 109 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 110 | * |
| 111 | * Function bte_main_boot_entry |
| 112 | * |
| 113 | * Description BTE MAIN API - Entry point for BTE chip/stack initialization |
| 114 | * |
| 115 | * Returns None |
| 116 | * |
| 117 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 118 | void bte_main_boot_entry(void) { |
| 119 | module_init(get_module(INTEROP_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 120 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 121 | hci = hci_layer_get_interface(); |
Myles Watson | 5d700dd | 2017-03-21 10:24:20 -0700 | [diff] [blame] | 122 | if (!hci) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 123 | LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__); |
Myles Watson | 5d700dd | 2017-03-21 10:24:20 -0700 | [diff] [blame] | 124 | return; |
| 125 | } |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 126 | |
Jack He | 96d16e3 | 2018-08-27 13:55:14 -0700 | [diff] [blame] | 127 | hci->set_data_cb(base::Bind(&post_to_main_message_loop)); |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 128 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 129 | module_init(get_module(STACK_CONFIG_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 133 | * |
| 134 | * Function bte_main_cleanup |
| 135 | * |
| 136 | * Description BTE MAIN API - Cleanup code for BTE chip/stack |
| 137 | * |
| 138 | * Returns None |
| 139 | * |
| 140 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 141 | void bte_main_cleanup() { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 142 | module_clean_up(get_module(STACK_CONFIG_MODULE)); |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 143 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 144 | module_clean_up(get_module(INTEROP_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 148 | * |
| 149 | * Function bte_main_enable |
| 150 | * |
| 151 | * Description BTE MAIN API - Creates all the BTE tasks. Should be called |
| 152 | * part of the Bluetooth stack enable sequence |
| 153 | * |
| 154 | * Returns None |
| 155 | * |
| 156 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 157 | void bte_main_enable() { |
| 158 | APPL_TRACE_DEBUG("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 159 | |
Chris Manton | 1bb0e51 | 2019-09-09 21:11:59 -0700 | [diff] [blame] | 160 | if (bluetooth::shim::is_gd_shim_enabled()) { |
| 161 | LOG_INFO(LOG_TAG, "%s Gd shim module enabled", __func__); |
Chris Manton | 229de74 | 2019-09-10 11:23:13 -0700 | [diff] [blame] | 162 | module_start_up(get_module(GD_SHIM_MODULE)); |
Chris Manton | d434863 | 2019-09-11 09:28:41 -0700 | [diff] [blame] | 163 | module_start_up(get_module(GD_HCI_MODULE)); |
Chris Manton | 1bb0e51 | 2019-09-09 21:11:59 -0700 | [diff] [blame] | 164 | } else { |
| 165 | module_start_up(get_module(BTSNOOP_MODULE)); |
| 166 | module_start_up(get_module(HCI_MODULE)); |
| 167 | } |
Zach Johnson | 093948a | 2014-09-23 18:30:45 -0700 | [diff] [blame] | 168 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 169 | BTU_StartUp(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 173 | * |
| 174 | * Function bte_main_disable |
| 175 | * |
| 176 | * Description BTE MAIN API - Destroys all the BTE tasks. Should be called |
| 177 | * part of the Bluetooth stack disable sequence |
| 178 | * |
| 179 | * Returns None |
| 180 | * |
| 181 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 182 | void bte_main_disable(void) { |
| 183 | APPL_TRACE_DEBUG("%s", __func__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 184 | |
Chris Manton | 1bb0e51 | 2019-09-09 21:11:59 -0700 | [diff] [blame] | 185 | if (bluetooth::shim::is_gd_shim_enabled()) { |
| 186 | LOG_INFO(LOG_TAG, "%s Gd shim module enabled", __func__); |
Chris Manton | d434863 | 2019-09-11 09:28:41 -0700 | [diff] [blame] | 187 | module_shut_down(get_module(GD_HCI_MODULE)); |
Chris Manton | 1bb0e51 | 2019-09-09 21:11:59 -0700 | [diff] [blame] | 188 | module_shut_down(get_module(GD_SHIM_MODULE)); |
| 189 | } else { |
| 190 | module_shut_down(get_module(HCI_MODULE)); |
| 191 | module_shut_down(get_module(BTSNOOP_MODULE)); |
| 192 | } |
Zach Johnson | 733a06e | 2014-09-08 18:31:39 -0700 | [diff] [blame] | 193 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 194 | BTU_ShutDown(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 198 | * |
| 199 | * Function bte_main_postload_cfg |
| 200 | * |
| 201 | * Description BTE MAIN API - Stack postload configuration |
| 202 | * |
| 203 | * Returns None |
| 204 | * |
| 205 | *****************************************************************************/ |
Andre Eisenbach | 796523d | 2016-11-10 16:11:00 -0800 | [diff] [blame] | 206 | void bte_main_postload_cfg(void) { |
| 207 | // TODO(eisenbach): [HIDL] DEPRECATE? |
| 208 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 209 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 210 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 211 | * |
| 212 | * Function bte_main_hci_send |
| 213 | * |
| 214 | * Description BTE MAIN API - This function is called by the upper stack to |
| 215 | * send an HCI message. The function displays a protocol trace |
| 216 | * message (if enabled), and then calls the 'transmit' function |
| 217 | * associated with the currently selected HCI transport |
| 218 | * |
| 219 | * Returns None |
| 220 | * |
| 221 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 222 | void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) { |
| 223 | uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 224 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 225 | p_msg->event = event; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 226 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 227 | if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || |
| 228 | (sub_event == LOCAL_BLE_CONTROLLER_ID)) { |
| 229 | hci->transmit_downward(event, p_msg); |
| 230 | } else { |
| 231 | APPL_TRACE_ERROR("Invalid Controller ID. Discarding message."); |
| 232 | osi_free(p_msg); |
| 233 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 234 | } |