blob: 43dcf8b8c837bfcb64511aef911a2489eb9c37ce [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
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include <assert.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070028#include <fcntl.h>
Chris Manton18023292014-08-29 09:12:06 -070029#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070030#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070031#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070032#include <time.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080033
Marie Janssendb554582015-06-26 14:53:46 -070034#include <hardware/bluetooth.h>
35
Matthew Xie66432dc2014-04-27 05:45:32 -070036#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070037#include "bt_utils.h"
Marie Janssendb554582015-06-26 14:53:46 -070038#include "bta_api.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070039#include "btcore/include/counter.h"
Andre Eisenbachcae219f2015-05-18 09:41:06 -070040#include "btcore/include/module.h"
Marie Janssendb554582015-06-26 14:53:46 -070041#include "bte.h"
42#include "btif_common.h"
43#include "btsnoop.h"
44#include "btu.h"
45#include "gki.h"
46#include "hci_layer.h"
47#include "osi/include/alarm.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070048#include "osi/include/fixed_queue.h"
49#include "osi/include/future.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070050#include "osi/include/hash_functions.h"
51#include "osi/include/hash_map.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080052#include "osi/include/log.h"
Marie Janssendb554582015-06-26 14:53:46 -070053#include "osi/include/osi.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070054#include "osi/include/thread.h"
Marie Janssendb554582015-06-26 14:53:46 -070055#include "stack_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080056
57/*******************************************************************************
58** Constants & Macros
59*******************************************************************************/
60
Marie Janssendb554582015-06-26 14:53:46 -070061#define LOG_TAG "bt_main"
62
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070063/* Run-time configuration file for BLE*/
64#ifndef BTE_BLE_STACK_CONF_FILE
65#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
66#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080067
The Android Open Source Project5738f832012-12-12 16:00:35 -080068/******************************************************************************
69** Variables
70******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080071
The Android Open Source Project5738f832012-12-12 16:00:35 -080072/*******************************************************************************
73** Static variables
74*******************************************************************************/
Zach Johnsonbf8193b2014-09-08 09:56:35 -070075static const hci_t *hci;
The Android Open Source Project5738f832012-12-12 16:00:35 -080076
77/*******************************************************************************
78** Static functions
79*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080080
81/*******************************************************************************
82** Externs
83*******************************************************************************/
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070084extern void bte_load_ble_conf(const char *p_path);
Chris Manton307381e2014-09-04 19:48:49 -070085fixed_queue_t *btu_hci_msg_queue;
86
The Android Open Source Project5738f832012-12-12 16:00:35 -080087/******************************************************************************
88**
The Android Open Source Project5738f832012-12-12 16:00:35 -080089** Function bte_main_boot_entry
90**
91** Description BTE MAIN API - Entry point for BTE chip/stack initialization
92**
93** Returns None
94**
95******************************************************************************/
96void bte_main_boot_entry(void)
97{
Zach Johnson3e130f32014-09-23 20:25:47 -070098 module_init(get_module(GKI_MODULE));
Chris Manton78a51cb2014-08-13 16:38:57 -070099 module_init(get_module(COUNTER_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700101 hci = hci_layer_get_interface();
102 if (!hci)
Marie Janssendb554582015-06-26 14:53:46 -0700103 LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700104
Chris Manton307381e2014-09-04 19:48:49 -0700105 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
106 if (btu_hci_msg_queue == NULL) {
Marie Janssendb554582015-06-26 14:53:46 -0700107 LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__);
Chris Manton307381e2014-09-04 19:48:49 -0700108 return;
109 }
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700110
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800111 data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
112 hci->set_data_queue(btu_hci_msg_queue);
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700113
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700114#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
115 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
116#endif
Zach Johnson9891f322014-09-22 22:11:55 -0700117 module_init(get_module(STACK_CONFIG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800118}
119
120/******************************************************************************
121**
122** Function bte_main_shutdown
123**
124** Description BTE MAIN API - Shutdown code for BTE chip/stack
125**
126** Returns None
127**
128******************************************************************************/
129void bte_main_shutdown()
130{
Zach Johnsonb55a8a62015-03-04 14:47:00 -0800131 data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
132 hci->set_data_queue(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700133 fixed_queue_free(btu_hci_msg_queue, NULL);
Zach Johnson733a06e2014-09-08 18:31:39 -0700134
Chris Manton307381e2014-09-04 19:48:49 -0700135 btu_hci_msg_queue = NULL;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700136
Zach Johnson9891f322014-09-22 22:11:55 -0700137 module_clean_up(get_module(STACK_CONFIG_MODULE));
138
Chris Manton78a51cb2014-08-13 16:38:57 -0700139 module_clean_up(get_module(COUNTER_MODULE));
Zach Johnson3e130f32014-09-23 20:25:47 -0700140 module_clean_up(get_module(GKI_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141}
142
143/******************************************************************************
144**
145** Function bte_main_enable
146**
147** Description BTE MAIN API - Creates all the BTE tasks. Should be called
148** part of the Bluetooth stack enable sequence
149**
150** Returns None
151**
152******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700153void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700155 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156
Zach Johnson9891f322014-09-22 22:11:55 -0700157 module_start_up(get_module(BTSNOOP_MODULE));
Zach Johnson093948a2014-09-23 18:30:45 -0700158 module_start_up(get_module(HCI_MODULE));
159
160 BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700161}
162
163/******************************************************************************
164**
165** Function bte_main_disable
166**
167** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
168** part of the Bluetooth stack disable sequence
169**
170** Returns None
171**
172******************************************************************************/
173void bte_main_disable(void)
174{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700175 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700176
Zach Johnson093948a2014-09-23 18:30:45 -0700177 module_shut_down(get_module(HCI_MODULE));
Zach Johnson9891f322014-09-22 22:11:55 -0700178 module_shut_down(get_module(BTSNOOP_MODULE));
Zach Johnson733a06e2014-09-08 18:31:39 -0700179
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700180 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700181}
182
183/******************************************************************************
184**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800185** Function bte_main_postload_cfg
186**
187** Description BTE MAIN API - Stack postload configuration
188**
189** Returns None
190**
191******************************************************************************/
192void bte_main_postload_cfg(void)
193{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700194 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800195}
196
197#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
198/******************************************************************************
199**
200** Function bte_main_enable_lpm
201**
202** Description BTE MAIN API - Enable/Disable low power mode operation
203**
204** Returns None
205**
206******************************************************************************/
207void bte_main_enable_lpm(BOOLEAN enable)
208{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700209 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800210}
211
212/******************************************************************************
213**
214** Function bte_main_lpm_allow_bt_device_sleep
215**
216** Description BTE MAIN API - Allow BT controller goest to sleep
217**
218** Returns None
219**
220******************************************************************************/
221void bte_main_lpm_allow_bt_device_sleep()
222{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700223 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224}
225
226/******************************************************************************
227**
228** Function bte_main_lpm_wake_bt_device
229**
230** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
231**
232** Returns None
233**
234******************************************************************************/
235void bte_main_lpm_wake_bt_device()
236{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700237 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238}
239#endif // HCILP_INCLUDED
240
241/******************************************************************************
242**
243** Function bte_main_hci_send
244**
245** Description BTE MAIN API - This function is called by the upper stack to
246** send an HCI message. The function displays a protocol trace
247** message (if enabled), and then calls the 'transmit' function
248** associated with the currently selected HCI transport
249**
250** Returns None
251**
252******************************************************************************/
253void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
254{
255 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
256
257 p_msg->event = event;
258
Chris Manton78a51cb2014-08-13 16:38:57 -0700259 counter_add("main.tx.packets", 1);
260 counter_add("main.tx.bytes", p_msg->len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800261
262 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
263 (sub_event == LOCAL_BLE_CONTROLLER_ID))
264 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700265 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266 }
267 else
268 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700269 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800270 GKI_freebuf(p_msg);
271 }
272}