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.cc 22 * 23 * Description: Contains BTE core stack initialization and shutdown code 24 * 25 ******************************************************************************/ 26 27 #define LOG_TAG "bt_main" 28 29 #include <base/logging.h> 30 #include <base/threading/thread.h> 31 #include <fcntl.h> 32 #include <pthread.h> 33 #include <signal.h> 34 #include <stdlib.h> 35 #include <time.h> 36 37 #include <hardware/bluetooth.h> 38 39 #include "bt_common.h" 40 #include "bt_hci_bdroid.h" 41 #include "bt_utils.h" 42 #include "bta_api.h" 43 #include "btcore/include/module.h" 44 #include "bte.h" 45 #include "btif_common.h" 46 #include "btsnoop.h" 47 #include "btu.h" 48 #include "device/include/interop.h" 49 #include "hci_layer.h" 50 #include "hcimsgs.h" 51 #include "osi/include/alarm.h" 52 #include "osi/include/fixed_queue.h" 53 #include "osi/include/future.h" 54 #include "osi/include/log.h" 55 #include "osi/include/osi.h" 56 #include "osi/include/thread.h" 57 #include "stack_config.h" 58 59 /******************************************************************************* 60 * Constants & Macros 61 ******************************************************************************/ 62 63 /* Run-time configuration file for BLE*/ 64 #ifndef BTE_BLE_STACK_CONF_FILE 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) 69 #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf" 70 #endif // defined(OS_GENERIC) 71 #endif // BT_BLE_STACK_CONF_FILE 72 73 /****************************************************************************** 74 * Variables 75 *****************************************************************************/ 76 77 /******************************************************************************* 78 * Static variables 79 ******************************************************************************/ 80 static const hci_t* hci; 81 82 /******************************************************************************* 83 * Externs 84 ******************************************************************************/ 85 extern void btu_hci_msg_process(BT_HDR* p_msg); 86 87 /******************************************************************************* 88 * Static functions 89 ******************************************************************************/ 90 91 /****************************************************************************** 92 * 93 * Function post_to_hci_message_loop 94 * 95 * Description Post an HCI event to the hci message queue 96 * 97 * Returns None 98 * 99 *****************************************************************************/ 100 void post_to_hci_message_loop(const tracked_objects::Location& from_here, 101 BT_HDR* p_msg) { 102 base::MessageLoop* hci_message_loop = get_message_loop(); 103 if (!hci_message_loop || !hci_message_loop->task_runner().get()) { 104 LOG_ERROR(LOG_TAG, "%s: HCI message loop not running, accessed from %s", 105 __func__, from_here.ToString().c_str()); 106 return; 107 } 108 109 hci_message_loop->task_runner()->PostTask( 110 from_here, base::Bind(&btu_hci_msg_process, p_msg)); 111 } 112 113 /****************************************************************************** 114 * 115 * Function bte_main_boot_entry 116 * 117 * Description BTE MAIN API - Entry point for BTE chip/stack initialization 118 * 119 * Returns None 120 * 121 *****************************************************************************/ 122 void bte_main_boot_entry(void) { 123 module_init(get_module(INTEROP_MODULE)); 124 125 hci = hci_layer_get_interface(); 126 if (!hci) { 127 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__); 128 return; 129 } 130 131 hci->set_data_cb(base::Bind(&post_to_hci_message_loop)); 132 133 module_init(get_module(STACK_CONFIG_MODULE)); 134 } 135 136 /****************************************************************************** 137 * 138 * Function bte_main_cleanup 139 * 140 * Description BTE MAIN API - Cleanup code for BTE chip/stack 141 * 142 * Returns None 143 * 144 *****************************************************************************/ 145 void bte_main_cleanup() { 146 module_clean_up(get_module(STACK_CONFIG_MODULE)); 147 148 module_clean_up(get_module(INTEROP_MODULE)); 149 } 150 151 /****************************************************************************** 152 * 153 * Function bte_main_enable 154 * 155 * Description BTE MAIN API - Creates all the BTE tasks. Should be called 156 * part of the Bluetooth stack enable sequence 157 * 158 * Returns None 159 * 160 *****************************************************************************/ 161 void bte_main_enable() { 162 APPL_TRACE_DEBUG("%s", __func__); 163 164 module_start_up(get_module(BTSNOOP_MODULE)); 165 module_start_up(get_module(HCI_MODULE)); 166 167 BTU_StartUp(); 168 } 169 170 /****************************************************************************** 171 * 172 * Function bte_main_disable 173 * 174 * Description BTE MAIN API - Destroys all the BTE tasks. Should be called 175 * part of the Bluetooth stack disable sequence 176 * 177 * Returns None 178 * 179 *****************************************************************************/ 180 void bte_main_disable(void) { 181 APPL_TRACE_DEBUG("%s", __func__); 182 183 module_shut_down(get_module(HCI_MODULE)); 184 module_shut_down(get_module(BTSNOOP_MODULE)); 185 186 BTU_ShutDown(); 187 } 188 189 /****************************************************************************** 190 * 191 * Function bte_main_postload_cfg 192 * 193 * Description BTE MAIN API - Stack postload configuration 194 * 195 * Returns None 196 * 197 *****************************************************************************/ 198 void bte_main_postload_cfg(void) { 199 // TODO(eisenbach): [HIDL] DEPRECATE? 200 } 201 202 /****************************************************************************** 203 * 204 * Function bte_main_hci_send 205 * 206 * Description BTE MAIN API - This function is called by the upper stack to 207 * send an HCI message. The function displays a protocol trace 208 * message (if enabled), and then calls the 'transmit' function 209 * associated with the currently selected HCI transport 210 * 211 * Returns None 212 * 213 *****************************************************************************/ 214 void bte_main_hci_send(BT_HDR* p_msg, uint16_t event) { 215 uint16_t sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */ 216 217 p_msg->event = event; 218 219 if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || 220 (sub_event == LOCAL_BLE_CONTROLLER_ID)) { 221 hci->transmit_downward(event, p_msg); 222 } else { 223 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message."); 224 osi_free(p_msg); 225 } 226 } 227