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 | * |
| 21 | * Filename: bte_main.c |
| 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 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 29 | #include <assert.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 | |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 38 | #include "bt_hci_bdroid.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 39 | #include "bt_utils.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 40 | #include "bta_api.h" |
Sharvil Nanavati | 95b74f2 | 2015-03-12 15:55:21 -0700 | [diff] [blame] | 41 | #include "btcore/include/counter.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" |
| 47 | #include "gki.h" |
| 48 | #include "hci_layer.h" |
| 49 | #include "osi/include/alarm.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 50 | #include "osi/include/fixed_queue.h" |
| 51 | #include "osi/include/future.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 52 | #include "osi/include/hash_functions.h" |
| 53 | #include "osi/include/hash_map.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" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 56 | #include "osi/include/thread.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 57 | #include "stack_config.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 58 | |
| 59 | /******************************************************************************* |
| 60 | ** Constants & Macros |
| 61 | *******************************************************************************/ |
| 62 | |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 63 | /* Run-time configuration file for BLE*/ |
| 64 | #ifndef BTE_BLE_STACK_CONF_FILE |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame^] | 65 | // 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 Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 69 | #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf" |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame^] | 70 | #endif // defined(OS_GENERIC) |
| 71 | #endif // BT_BLE_STACK_CONF_FILE |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 72 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 73 | /****************************************************************************** |
| 74 | ** Variables |
| 75 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 76 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 77 | /******************************************************************************* |
| 78 | ** Static variables |
| 79 | *******************************************************************************/ |
Zach Johnson | bf8193b | 2014-09-08 09:56:35 -0700 | [diff] [blame] | 80 | static const hci_t *hci; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 81 | |
| 82 | /******************************************************************************* |
| 83 | ** Static functions |
| 84 | *******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 85 | |
| 86 | /******************************************************************************* |
| 87 | ** Externs |
| 88 | *******************************************************************************/ |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 89 | extern void bte_load_ble_conf(const char *p_path); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 90 | fixed_queue_t *btu_hci_msg_queue; |
| 91 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 92 | /****************************************************************************** |
| 93 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 94 | ** Function bte_main_boot_entry |
| 95 | ** |
| 96 | ** Description BTE MAIN API - Entry point for BTE chip/stack initialization |
| 97 | ** |
| 98 | ** Returns None |
| 99 | ** |
| 100 | ******************************************************************************/ |
| 101 | void bte_main_boot_entry(void) |
| 102 | { |
Zach Johnson | 3e130f3 | 2014-09-23 20:25:47 -0700 | [diff] [blame] | 103 | module_init(get_module(GKI_MODULE)); |
Chris Manton | 78a51cb | 2014-08-13 16:38:57 -0700 | [diff] [blame] | 104 | module_init(get_module(COUNTER_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 105 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 106 | hci = hci_layer_get_interface(); |
| 107 | if (!hci) |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 108 | LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 109 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 110 | btu_hci_msg_queue = fixed_queue_new(SIZE_MAX); |
| 111 | if (btu_hci_msg_queue == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 112 | LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 113 | return; |
| 114 | } |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 115 | |
Zach Johnson | b55a8a6 | 2015-03-04 14:47:00 -0800 | [diff] [blame] | 116 | data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue); |
| 117 | hci->set_data_queue(btu_hci_msg_queue); |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 118 | |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 119 | #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) |
| 120 | bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE); |
| 121 | #endif |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 122 | module_init(get_module(STACK_CONFIG_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /****************************************************************************** |
| 126 | ** |
| 127 | ** Function bte_main_shutdown |
| 128 | ** |
| 129 | ** Description BTE MAIN API - Shutdown code for BTE chip/stack |
| 130 | ** |
| 131 | ** Returns None |
| 132 | ** |
| 133 | ******************************************************************************/ |
| 134 | void bte_main_shutdown() |
| 135 | { |
Zach Johnson | b55a8a6 | 2015-03-04 14:47:00 -0800 | [diff] [blame] | 136 | data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL); |
| 137 | hci->set_data_queue(NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 138 | fixed_queue_free(btu_hci_msg_queue, NULL); |
Zach Johnson | 733a06e | 2014-09-08 18:31:39 -0700 | [diff] [blame] | 139 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 140 | btu_hci_msg_queue = NULL; |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 141 | |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 142 | module_clean_up(get_module(STACK_CONFIG_MODULE)); |
| 143 | |
Chris Manton | 78a51cb | 2014-08-13 16:38:57 -0700 | [diff] [blame] | 144 | module_clean_up(get_module(COUNTER_MODULE)); |
Zach Johnson | 3e130f3 | 2014-09-23 20:25:47 -0700 | [diff] [blame] | 145 | module_clean_up(get_module(GKI_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /****************************************************************************** |
| 149 | ** |
| 150 | ** Function bte_main_enable |
| 151 | ** |
| 152 | ** Description BTE MAIN API - Creates all the BTE tasks. Should be called |
| 153 | ** part of the Bluetooth stack enable sequence |
| 154 | ** |
| 155 | ** Returns None |
| 156 | ** |
| 157 | ******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 158 | void bte_main_enable() |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 159 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 160 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 161 | |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 162 | module_start_up(get_module(BTSNOOP_MODULE)); |
Zach Johnson | 093948a | 2014-09-23 18:30:45 -0700 | [diff] [blame] | 163 | module_start_up(get_module(HCI_MODULE)); |
| 164 | |
| 165 | BTU_StartUp(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /****************************************************************************** |
| 169 | ** |
| 170 | ** Function bte_main_disable |
| 171 | ** |
| 172 | ** Description BTE MAIN API - Destroys all the BTE tasks. Should be called |
| 173 | ** part of the Bluetooth stack disable sequence |
| 174 | ** |
| 175 | ** Returns None |
| 176 | ** |
| 177 | ******************************************************************************/ |
| 178 | void bte_main_disable(void) |
| 179 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 180 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 181 | |
Zach Johnson | 093948a | 2014-09-23 18:30:45 -0700 | [diff] [blame] | 182 | module_shut_down(get_module(HCI_MODULE)); |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 183 | module_shut_down(get_module(BTSNOOP_MODULE)); |
Zach Johnson | 733a06e | 2014-09-08 18:31:39 -0700 | [diff] [blame] | 184 | |
Sharvil Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 185 | BTU_ShutDown(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /****************************************************************************** |
| 189 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 190 | ** Function bte_main_postload_cfg |
| 191 | ** |
| 192 | ** Description BTE MAIN API - Stack postload configuration |
| 193 | ** |
| 194 | ** Returns None |
| 195 | ** |
| 196 | ******************************************************************************/ |
| 197 | void bte_main_postload_cfg(void) |
| 198 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 199 | hci->do_postload(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 203 | /****************************************************************************** |
| 204 | ** |
| 205 | ** Function bte_main_enable_lpm |
| 206 | ** |
| 207 | ** Description BTE MAIN API - Enable/Disable low power mode operation |
| 208 | ** |
| 209 | ** Returns None |
| 210 | ** |
| 211 | ******************************************************************************/ |
| 212 | void bte_main_enable_lpm(BOOLEAN enable) |
| 213 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 214 | hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /****************************************************************************** |
| 218 | ** |
| 219 | ** Function bte_main_lpm_allow_bt_device_sleep |
| 220 | ** |
| 221 | ** Description BTE MAIN API - Allow BT controller goest to sleep |
| 222 | ** |
| 223 | ** Returns None |
| 224 | ** |
| 225 | ******************************************************************************/ |
| 226 | void bte_main_lpm_allow_bt_device_sleep() |
| 227 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 228 | hci->send_low_power_command(LPM_WAKE_DEASSERT); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /****************************************************************************** |
| 232 | ** |
| 233 | ** Function bte_main_lpm_wake_bt_device |
| 234 | ** |
| 235 | ** Description BTE MAIN API - Wake BT controller up if it is in sleep mode |
| 236 | ** |
| 237 | ** Returns None |
| 238 | ** |
| 239 | ******************************************************************************/ |
| 240 | void bte_main_lpm_wake_bt_device() |
| 241 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 242 | hci->send_low_power_command(LPM_WAKE_ASSERT); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 243 | } |
| 244 | #endif // HCILP_INCLUDED |
| 245 | |
| 246 | /****************************************************************************** |
| 247 | ** |
| 248 | ** Function bte_main_hci_send |
| 249 | ** |
| 250 | ** Description BTE MAIN API - This function is called by the upper stack to |
| 251 | ** send an HCI message. The function displays a protocol trace |
| 252 | ** message (if enabled), and then calls the 'transmit' function |
| 253 | ** associated with the currently selected HCI transport |
| 254 | ** |
| 255 | ** Returns None |
| 256 | ** |
| 257 | ******************************************************************************/ |
| 258 | void bte_main_hci_send (BT_HDR *p_msg, UINT16 event) |
| 259 | { |
| 260 | UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */ |
| 261 | |
| 262 | p_msg->event = event; |
| 263 | |
Chris Manton | 78a51cb | 2014-08-13 16:38:57 -0700 | [diff] [blame] | 264 | counter_add("main.tx.packets", 1); |
| 265 | counter_add("main.tx.bytes", p_msg->len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 266 | |
| 267 | if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \ |
| 268 | (sub_event == LOCAL_BLE_CONTROLLER_ID)) |
| 269 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame] | 270 | hci->transmit_downward(event, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 271 | } |
| 272 | else |
| 273 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 274 | APPL_TRACE_ERROR("Invalid Controller ID. Discarding message."); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 275 | GKI_freebuf(p_msg); |
| 276 | } |
| 277 | } |