blob: b870b5ef58acdbd326ad4036c2952396e6867678 [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
Marie Janssen49120dc2015-07-07 16:47:20 -070027#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>
Chris Manton18023292014-08-29 09:12:06 -070031#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070032#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070033#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070034#include <time.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Marie Janssendb554582015-06-26 14:53:46 -070036#include <hardware/bluetooth.h>
37
Matthew Xie66432dc2014-04-27 05:45:32 -070038#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070039#include "bt_utils.h"
Marie Janssendb554582015-06-26 14:53:46 -070040#include "bta_api.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070041#include "btcore/include/counter.h"
Andre Eisenbachcae219f2015-05-18 09:41:06 -070042#include "btcore/include/module.h"
Marie Janssendb554582015-06-26 14:53:46 -070043#include "bte.h"
44#include "btif_common.h"
45#include "btsnoop.h"
46#include "btu.h"
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070047#include "bt_common.h"
Andre Eisenbache42d1d72016-01-14 02:00:24 -080048#include "device/include/interop.h"
Marie Janssendb554582015-06-26 14:53:46 -070049#include "hci_layer.h"
50#include "osi/include/alarm.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070051#include "osi/include/fixed_queue.h"
52#include "osi/include/future.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070053#include "osi/include/hash_functions.h"
54#include "osi/include/hash_map.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080055#include "osi/include/log.h"
Marie Janssendb554582015-06-26 14:53:46 -070056#include "osi/include/osi.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070057#include "osi/include/thread.h"
Marie Janssendb554582015-06-26 14:53:46 -070058#include "stack_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080059
60/*******************************************************************************
61** Constants & Macros
62*******************************************************************************/
63
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070064/* Run-time configuration file for BLE*/
65#ifndef BTE_BLE_STACK_CONF_FILE
Arman Ugurayf2d64342015-07-08 15:47:39 -070066// 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 Viswanadham4c94c5f2014-07-18 15:20:54 -070070#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
Arman Ugurayf2d64342015-07-08 15:47:39 -070071#endif // defined(OS_GENERIC)
72#endif // BT_BLE_STACK_CONF_FILE
The Android Open Source Project5738f832012-12-12 16:00:35 -080073
The Android Open Source Project5738f832012-12-12 16:00:35 -080074/******************************************************************************
75** Variables
76******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080077
The Android Open Source Project5738f832012-12-12 16:00:35 -080078/*******************************************************************************
79** Static variables
80*******************************************************************************/
Zach Johnsonbf8193b2014-09-08 09:56:35 -070081static const hci_t *hci;
The Android Open Source Project5738f832012-12-12 16:00:35 -080082
83/*******************************************************************************
84** Static functions
85*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080086
87/*******************************************************************************
88** Externs
89*******************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -070090fixed_queue_t *btu_hci_msg_queue;
91
The Android Open Source Project5738f832012-12-12 16:00:35 -080092/******************************************************************************
93**
The Android Open Source Project5738f832012-12-12 16:00:35 -080094** Function bte_main_boot_entry
95**
96** Description BTE MAIN API - Entry point for BTE chip/stack initialization
97**
98** Returns None
99**
100******************************************************************************/
101void bte_main_boot_entry(void)
102{
Chris Manton78a51cb2014-08-13 16:38:57 -0700103 module_init(get_module(COUNTER_MODULE));
Andre Eisenbache42d1d72016-01-14 02:00:24 -0800104 module_init(get_module(INTEROP_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700106 hci = hci_layer_get_interface();
107 if (!hci)
Marie Janssendb554582015-06-26 14:53:46 -0700108 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700109
Chris Manton307381e2014-09-04 19:48:49 -0700110 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
111 if (btu_hci_msg_queue == NULL) {
Marie Janssendb554582015-06-26 14:53:46 -0700112 LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__);
Chris Manton307381e2014-09-04 19:48:49 -0700113 return;
114 }
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700115
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800116 data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
117 hci->set_data_queue(btu_hci_msg_queue);
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700118
Zach Johnson9891f322014-09-22 22:11:55 -0700119 module_init(get_module(STACK_CONFIG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800120}
121
122/******************************************************************************
123**
124** Function bte_main_shutdown
125**
126** Description BTE MAIN API - Shutdown code for BTE chip/stack
127**
128** Returns None
129**
130******************************************************************************/
131void bte_main_shutdown()
132{
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800133 data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
134 hci->set_data_queue(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700135 fixed_queue_free(btu_hci_msg_queue, NULL);
Zach Johnson733a06e2014-09-08 18:31:39 -0700136
Chris Manton307381e2014-09-04 19:48:49 -0700137 btu_hci_msg_queue = NULL;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700138
Zach Johnson9891f322014-09-22 22:11:55 -0700139 module_clean_up(get_module(STACK_CONFIG_MODULE));
140
Andre Eisenbache42d1d72016-01-14 02:00:24 -0800141 module_clean_up(get_module(INTEROP_MODULE));
Chris Manton78a51cb2014-08-13 16:38:57 -0700142 module_clean_up(get_module(COUNTER_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800143}
144
145/******************************************************************************
146**
147** Function bte_main_enable
148**
149** Description BTE MAIN API - Creates all the BTE tasks. Should be called
150** part of the Bluetooth stack enable sequence
151**
152** Returns None
153**
154******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700155void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700157 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158
Zach Johnson9891f322014-09-22 22:11:55 -0700159 module_start_up(get_module(BTSNOOP_MODULE));
Zach Johnson093948a2014-09-23 18:30:45 -0700160 module_start_up(get_module(HCI_MODULE));
161
162 BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700163}
164
165/******************************************************************************
166**
167** Function bte_main_disable
168**
169** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
170** part of the Bluetooth stack disable sequence
171**
172** Returns None
173**
174******************************************************************************/
175void bte_main_disable(void)
176{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700177 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700178
Zach Johnson093948a2014-09-23 18:30:45 -0700179 module_shut_down(get_module(HCI_MODULE));
Zach Johnson9891f322014-09-22 22:11:55 -0700180 module_shut_down(get_module(BTSNOOP_MODULE));
Zach Johnson733a06e2014-09-08 18:31:39 -0700181
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700182 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700183}
184
185/******************************************************************************
186**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800187** Function bte_main_postload_cfg
188**
189** Description BTE MAIN API - Stack postload configuration
190**
191** Returns None
192**
193******************************************************************************/
194void bte_main_postload_cfg(void)
195{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700196 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800197}
198
199#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
200/******************************************************************************
201**
202** Function bte_main_enable_lpm
203**
204** Description BTE MAIN API - Enable/Disable low power mode operation
205**
206** Returns None
207**
208******************************************************************************/
209void bte_main_enable_lpm(BOOLEAN enable)
210{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700211 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212}
213
214/******************************************************************************
215**
216** Function bte_main_lpm_allow_bt_device_sleep
217**
Miao Choufb685ef2015-08-11 11:41:58 -0700218** Description BTE MAIN API - Allow the BT controller to go to sleep
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219**
220** Returns None
221**
222******************************************************************************/
223void bte_main_lpm_allow_bt_device_sleep()
224{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700225 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800226}
227
228/******************************************************************************
229**
230** Function bte_main_lpm_wake_bt_device
231**
232** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
233**
234** Returns None
235**
236******************************************************************************/
237void bte_main_lpm_wake_bt_device()
238{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700239 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800240}
241#endif // HCILP_INCLUDED
242
243/******************************************************************************
244**
245** Function bte_main_hci_send
246**
247** Description BTE MAIN API - This function is called by the upper stack to
248** send an HCI message. The function displays a protocol trace
249** message (if enabled), and then calls the 'transmit' function
250** associated with the currently selected HCI transport
251**
252** Returns None
253**
254******************************************************************************/
255void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
256{
257 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
258
259 p_msg->event = event;
260
Chris Manton78a51cb2014-08-13 16:38:57 -0700261 counter_add("main.tx.packets", 1);
262 counter_add("main.tx.bytes", p_msg->len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263
264 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
265 (sub_event == LOCAL_BLE_CONTROLLER_ID))
266 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700267 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268 }
269 else
270 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700271 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800272 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273 }
274}