The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | * |
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> |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 30 | #include <fcntl.h> |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 32 | #include <signal.h> |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 34 | #include <time.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 35 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 36 | #include <hardware/bluetooth.h> |
| 37 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 38 | #include "bt_common.h" |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 39 | #include "bt_hci_bdroid.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 40 | #include "bt_utils.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 41 | #include "bta_api.h" |
Andre Eisenbach | cae219f | 2015-05-18 09:41:06 -0700 | [diff] [blame] | 42 | #include "btcore/include/module.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 43 | #include "bte.h" |
| 44 | #include "btif_common.h" |
| 45 | #include "btsnoop.h" |
| 46 | #include "btu.h" |
Andre Eisenbach | e42d1d7 | 2016-01-14 02:00:24 -0800 | [diff] [blame] | 47 | #include "device/include/interop.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 48 | #include "hci_layer.h" |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 49 | #include "hcimsgs.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 50 | #include "osi/include/alarm.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 51 | #include "osi/include/fixed_queue.h" |
| 52 | #include "osi/include/future.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 53 | #include "osi/include/log.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 54 | #include "osi/include/osi.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 55 | #include "osi/include/thread.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 56 | #include "stack_config.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 57 | |
| 58 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 59 | * Constants & Macros |
| 60 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 61 | |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 62 | /* Run-time configuration file for BLE*/ |
| 63 | #ifndef BTE_BLE_STACK_CONF_FILE |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 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) |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 68 | #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf" |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 69 | #endif // defined(OS_GENERIC) |
| 70 | #endif // BT_BLE_STACK_CONF_FILE |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 71 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 72 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 73 | * Variables |
| 74 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 75 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 76 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 77 | * Static variables |
| 78 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 79 | static const hci_t* hci; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 80 | |
| 81 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 82 | * Static functions |
| 83 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 84 | |
| 85 | /******************************************************************************* |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 86 | * Externs |
| 87 | ******************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 88 | fixed_queue_t* btu_hci_msg_queue; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 89 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 90 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 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 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 99 | void bte_main_boot_entry(void) { |
| 100 | module_init(get_module(INTEROP_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 101 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 102 | hci = hci_layer_get_interface(); |
| 103 | if (!hci) |
| 104 | LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 105 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 106 | btu_hci_msg_queue = fixed_queue_new(SIZE_MAX); |
| 107 | if (btu_hci_msg_queue == NULL) { |
| 108 | LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__); |
| 109 | return; |
| 110 | } |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 111 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 112 | data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue); |
| 113 | hci->set_data_queue(btu_hci_msg_queue); |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 114 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 115 | module_init(get_module(STACK_CONFIG_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 119 | * |
| 120 | * Function bte_main_cleanup |
| 121 | * |
| 122 | * Description BTE MAIN API - Cleanup code for BTE chip/stack |
| 123 | * |
| 124 | * Returns None |
| 125 | * |
| 126 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 127 | void bte_main_cleanup() { |
| 128 | data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, |
| 129 | NULL); |
| 130 | hci->set_data_queue(NULL); |
| 131 | fixed_queue_free(btu_hci_msg_queue, NULL); |
Zach Johnson | 733a06e | 2014-09-08 18:31:39 -0700 | [diff] [blame] | 132 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 133 | btu_hci_msg_queue = NULL; |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 134 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 135 | module_clean_up(get_module(STACK_CONFIG_MODULE)); |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 136 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 137 | module_clean_up(get_module(INTEROP_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 141 | * |
| 142 | * Function bte_main_enable |
| 143 | * |
| 144 | * Description BTE MAIN API - Creates all the BTE tasks. Should be called |
| 145 | * part of the Bluetooth stack enable sequence |
| 146 | * |
| 147 | * Returns None |
| 148 | * |
| 149 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 150 | void bte_main_enable() { |
| 151 | APPL_TRACE_DEBUG("%s", __func__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 152 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 153 | module_start_up(get_module(BTSNOOP_MODULE)); |
| 154 | module_start_up(get_module(HCI_MODULE)); |
Zach Johnson | 093948a | 2014-09-23 18:30:45 -0700 | [diff] [blame] | 155 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 156 | BTU_StartUp(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 160 | * |
| 161 | * Function bte_main_disable |
| 162 | * |
| 163 | * Description BTE MAIN API - Destroys all the BTE tasks. Should be called |
| 164 | * part of the Bluetooth stack disable sequence |
| 165 | * |
| 166 | * Returns None |
| 167 | * |
| 168 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 169 | void bte_main_disable(void) { |
| 170 | APPL_TRACE_DEBUG("%s", __func__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 171 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 172 | module_shut_down(get_module(HCI_MODULE)); |
| 173 | module_shut_down(get_module(BTSNOOP_MODULE)); |
Zach Johnson | 733a06e | 2014-09-08 18:31:39 -0700 | [diff] [blame] | 174 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 175 | BTU_ShutDown(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 179 | * |
| 180 | * Function bte_main_postload_cfg |
| 181 | * |
| 182 | * Description BTE MAIN API - Stack postload configuration |
| 183 | * |
| 184 | * Returns None |
| 185 | * |
| 186 | *****************************************************************************/ |
Andre Eisenbach | 796523d | 2016-11-10 16:11:00 -0800 | [diff] [blame^] | 187 | void bte_main_postload_cfg(void) { |
| 188 | // TODO(eisenbach): [HIDL] DEPRECATE? |
| 189 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 190 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 191 | #if (HCILP_INCLUDED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 192 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 193 | * |
| 194 | * Function bte_main_enable_lpm |
| 195 | * |
| 196 | * Description BTE MAIN API - Enable/Disable low power mode operation |
| 197 | * |
| 198 | * Returns None |
| 199 | * |
| 200 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 201 | void bte_main_enable_lpm(bool enable) { |
Andre Eisenbach | 796523d | 2016-11-10 16:11:00 -0800 | [diff] [blame^] | 202 | // TODO(eisenbach): [HIDL] DEPRECATE? |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 206 | * |
| 207 | * Function bte_main_lpm_allow_bt_device_sleep |
| 208 | * |
| 209 | * Description BTE MAIN API - Allow the BT controller to go to sleep |
| 210 | * |
| 211 | * Returns None |
| 212 | * |
| 213 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 214 | void bte_main_lpm_allow_bt_device_sleep() { |
Andre Eisenbach | 796523d | 2016-11-10 16:11:00 -0800 | [diff] [blame^] | 215 | // TODO(eisenbach): [HIDL] DEPRECATE? |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 219 | * |
| 220 | * Function bte_main_lpm_wake_bt_device |
| 221 | * |
| 222 | * Description BTE MAIN API - Wake BT controller up if it is in sleep mode |
| 223 | * |
| 224 | * Returns None |
| 225 | * |
| 226 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 227 | void bte_main_lpm_wake_bt_device() { |
Andre Eisenbach | 796523d | 2016-11-10 16:11:00 -0800 | [diff] [blame^] | 228 | // TODO(eisenbach): [HIDL] DEPRECATE? |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 229 | } |
| 230 | #endif // HCILP_INCLUDED |
| 231 | |
| 232 | /****************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 233 | * |
| 234 | * Function bte_main_hci_send |
| 235 | * |
| 236 | * Description BTE MAIN API - This function is called by the upper stack to |
| 237 | * send an HCI message. The function displays a protocol trace |
| 238 | * message (if enabled), and then calls the 'transmit' function |
| 239 | * associated with the currently selected HCI transport |
| 240 | * |
| 241 | * Returns None |
| 242 | * |
| 243 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 244 | void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) { |
| 245 | 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] | 246 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 247 | p_msg->event = event; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 248 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 249 | if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || |
| 250 | (sub_event == LOCAL_BLE_CONTROLLER_ID)) { |
| 251 | hci->transmit_downward(event, p_msg); |
| 252 | } else { |
| 253 | APPL_TRACE_ERROR("Invalid Controller ID. Discarding message."); |
| 254 | osi_free(p_msg); |
| 255 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 256 | } |