blob: 4f6c1b0305ff520ec11c28e78318efd4b8d15987 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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 Nanavati44802762014-12-23 23:08:58 -080026
27#define LOG_TAG "bt_main"
28
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include <assert.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070030#include <fcntl.h>
31#include <hardware/bluetooth.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
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070037#include "osi/include/alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038#include "bta_api.h"
Matthew Xie66432dc2014-04-27 05:45:32 -070039#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070040#include "bte.h"
Chris Manton0eefef02014-09-08 15:01:39 -070041#include "btif_common.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070042#include "btu.h"
Zach Johnson733a06e2014-09-08 18:31:39 -070043#include "btsnoop.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070044#include "bt_utils.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070045#include "btcore/include/counter.h"
Andre Eisenbachcae219f2015-05-18 09:41:06 -070046#include "btcore/include/module.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070047#include "osi/include/fixed_queue.h"
48#include "osi/include/future.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070049#include "gki.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070050#include "osi/include/hash_functions.h"
51#include "osi/include/hash_map.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070052#include "hci_layer.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070053#include "osi/include/osi.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080054#include "osi/include/log.h"
Zach Johnson9891f322014-09-22 22:11:55 -070055#include "stack_config.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070056#include "osi/include/thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080057
58/*******************************************************************************
59** Constants & Macros
60*******************************************************************************/
61
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070062/* Run-time configuration file for BLE*/
63#ifndef BTE_BLE_STACK_CONF_FILE
64#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
65#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080066
The Android Open Source Project5738f832012-12-12 16:00:35 -080067/******************************************************************************
68** Variables
69******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080070
The Android Open Source Project5738f832012-12-12 16:00:35 -080071/*******************************************************************************
72** Static variables
73*******************************************************************************/
Zach Johnsonbf8193b2014-09-08 09:56:35 -070074static const hci_t *hci;
The Android Open Source Project5738f832012-12-12 16:00:35 -080075
76/*******************************************************************************
77** Static functions
78*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080079
80/*******************************************************************************
81** Externs
82*******************************************************************************/
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070083extern void bte_load_ble_conf(const char *p_path);
Chris Manton307381e2014-09-04 19:48:49 -070084fixed_queue_t *btu_hci_msg_queue;
85
The Android Open Source Project5738f832012-12-12 16:00:35 -080086/******************************************************************************
87**
The Android Open Source Project5738f832012-12-12 16:00:35 -080088** Function bte_main_boot_entry
89**
90** Description BTE MAIN API - Entry point for BTE chip/stack initialization
91**
92** Returns None
93**
94******************************************************************************/
95void bte_main_boot_entry(void)
96{
Zach Johnson3e130f32014-09-23 20:25:47 -070097 module_init(get_module(GKI_MODULE));
Chris Manton78a51cb2014-08-13 16:38:57 -070098 module_init(get_module(COUNTER_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -080099
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700100 hci = hci_layer_get_interface();
101 if (!hci)
Sharvil Nanavati44802762014-12-23 23:08:58 -0800102 LOG_ERROR("%s could not get hci layer interface.", __func__);
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700103
Chris Manton307381e2014-09-04 19:48:49 -0700104 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
105 if (btu_hci_msg_queue == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800106 LOG_ERROR("%s unable to allocate hci message queue.", __func__);
Chris Manton307381e2014-09-04 19:48:49 -0700107 return;
108 }
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700109
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800110 data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
111 hci->set_data_queue(btu_hci_msg_queue);
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700112
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700113#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
114 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
115#endif
Zach Johnson9891f322014-09-22 22:11:55 -0700116 module_init(get_module(STACK_CONFIG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800117}
118
119/******************************************************************************
120**
121** Function bte_main_shutdown
122**
123** Description BTE MAIN API - Shutdown code for BTE chip/stack
124**
125** Returns None
126**
127******************************************************************************/
128void bte_main_shutdown()
129{
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800130 data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
131 hci->set_data_queue(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700132 fixed_queue_free(btu_hci_msg_queue, NULL);
Zach Johnson733a06e2014-09-08 18:31:39 -0700133
Chris Manton307381e2014-09-04 19:48:49 -0700134 btu_hci_msg_queue = NULL;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700135
Zach Johnson9891f322014-09-22 22:11:55 -0700136 module_clean_up(get_module(STACK_CONFIG_MODULE));
137
Chris Manton78a51cb2014-08-13 16:38:57 -0700138 module_clean_up(get_module(COUNTER_MODULE));
Zach Johnson3e130f32014-09-23 20:25:47 -0700139 module_clean_up(get_module(GKI_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800140}
141
142/******************************************************************************
143**
144** Function bte_main_enable
145**
146** Description BTE MAIN API - Creates all the BTE tasks. Should be called
147** part of the Bluetooth stack enable sequence
148**
149** Returns None
150**
151******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700152void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800153{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700154 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800155
Zach Johnson9891f322014-09-22 22:11:55 -0700156 module_start_up(get_module(BTSNOOP_MODULE));
Zach Johnson093948a2014-09-23 18:30:45 -0700157 module_start_up(get_module(HCI_MODULE));
158
159 BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700160}
161
162/******************************************************************************
163**
164** Function bte_main_disable
165**
166** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
167** part of the Bluetooth stack disable sequence
168**
169** Returns None
170**
171******************************************************************************/
172void bte_main_disable(void)
173{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700174 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700175
Zach Johnson093948a2014-09-23 18:30:45 -0700176 module_shut_down(get_module(HCI_MODULE));
Zach Johnson9891f322014-09-22 22:11:55 -0700177 module_shut_down(get_module(BTSNOOP_MODULE));
Zach Johnson733a06e2014-09-08 18:31:39 -0700178
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700179 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700180}
181
182/******************************************************************************
183**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800184** Function bte_main_postload_cfg
185**
186** Description BTE MAIN API - Stack postload configuration
187**
188** Returns None
189**
190******************************************************************************/
191void bte_main_postload_cfg(void)
192{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700193 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194}
195
196#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
197/******************************************************************************
198**
199** Function bte_main_enable_lpm
200**
201** Description BTE MAIN API - Enable/Disable low power mode operation
202**
203** Returns None
204**
205******************************************************************************/
206void bte_main_enable_lpm(BOOLEAN enable)
207{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700208 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800209}
210
211/******************************************************************************
212**
213** Function bte_main_lpm_allow_bt_device_sleep
214**
215** Description BTE MAIN API - Allow BT controller goest to sleep
216**
217** Returns None
218**
219******************************************************************************/
220void bte_main_lpm_allow_bt_device_sleep()
221{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700222 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800223}
224
225/******************************************************************************
226**
227** Function bte_main_lpm_wake_bt_device
228**
229** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
230**
231** Returns None
232**
233******************************************************************************/
234void bte_main_lpm_wake_bt_device()
235{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700236 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237}
238#endif // HCILP_INCLUDED
239
240/******************************************************************************
241**
242** Function bte_main_hci_send
243**
244** Description BTE MAIN API - This function is called by the upper stack to
245** send an HCI message. The function displays a protocol trace
246** message (if enabled), and then calls the 'transmit' function
247** associated with the currently selected HCI transport
248**
249** Returns None
250**
251******************************************************************************/
252void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
253{
254 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
255
256 p_msg->event = event;
257
Chris Manton78a51cb2014-08-13 16:38:57 -0700258 counter_add("main.tx.packets", 1);
259 counter_add("main.tx.bytes", p_msg->len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800260
261 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
262 (sub_event == LOCAL_BLE_CONTROLLER_ID))
263 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700264 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265 }
266 else
267 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700268 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800269 GKI_freebuf(p_msg);
270 }
271}