blob: f6d1bdfd860b321738414cb647316ca51150850f [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 <cutils/properties.h>
31#include <fcntl.h>
32#include <hardware/bluetooth.h>
Chris Manton18023292014-08-29 09:12:06 -070033#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070034#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070035#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070036#include <time.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080037
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070038#include "alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080039#include "bta_api.h"
Matthew Xie66432dc2014-04-27 05:45:32 -070040#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070041#include "bte.h"
Chris Manton0eefef02014-09-08 15:01:39 -070042#include "btif_common.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070043#include "btu.h"
Zach Johnson733a06e2014-09-08 18:31:39 -070044#include "btsnoop.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070045#include "bt_utils.h"
Chris Manton78a51cb2014-08-13 16:38:57 -070046#include "counter.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070047#include "fixed_queue.h"
Chris Manton307381e2014-09-04 19:48:49 -070048#include "future.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070049#include "gki.h"
Chris Manton18023292014-08-29 09:12:06 -070050#include "hash_functions.h"
51#include "hash_map.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070052#include "hci_layer.h"
Zach Johnson9891f322014-09-22 22:11:55 -070053#include "module.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070054#include "osi.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080055#include "osi/include/log.h"
Zach Johnson9891f322014-09-22 22:11:55 -070056#include "stack_config.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070057#include "thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080058
59/*******************************************************************************
60** Constants & Macros
61*******************************************************************************/
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)
Sharvil Nanavati44802762014-12-23 23:08:58 -0800103 LOG_ERROR("%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) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800107 LOG_ERROR("%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
Chris Manton307381e2014-09-04 19:48:49 -0700111 data_dispatcher_register_default(hci->upward_dispatcher, 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 Johnson733a06e2014-09-08 18:31:39 -0700130 data_dispatcher_register_default(hci_layer_get_interface()->upward_dispatcher, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700131 fixed_queue_free(btu_hci_msg_queue, NULL);
Zach Johnson733a06e2014-09-08 18:31:39 -0700132
Chris Manton307381e2014-09-04 19:48:49 -0700133 btu_hci_msg_queue = NULL;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700134
Zach Johnson9891f322014-09-22 22:11:55 -0700135 module_clean_up(get_module(STACK_CONFIG_MODULE));
136
Chris Manton78a51cb2014-08-13 16:38:57 -0700137 module_clean_up(get_module(COUNTER_MODULE));
Zach Johnson3e130f32014-09-23 20:25:47 -0700138 module_clean_up(get_module(GKI_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139}
140
141/******************************************************************************
142**
143** Function bte_main_enable
144**
145** Description BTE MAIN API - Creates all the BTE tasks. Should be called
146** part of the Bluetooth stack enable sequence
147**
148** Returns None
149**
150******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700151void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700153 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154
Zach Johnson9891f322014-09-22 22:11:55 -0700155 module_start_up(get_module(BTSNOOP_MODULE));
Zach Johnson093948a2014-09-23 18:30:45 -0700156 module_start_up(get_module(HCI_MODULE));
157
158 BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700159}
160
161/******************************************************************************
162**
163** Function bte_main_disable
164**
165** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
166** part of the Bluetooth stack disable sequence
167**
168** Returns None
169**
170******************************************************************************/
171void bte_main_disable(void)
172{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700173 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700174
Zach Johnson093948a2014-09-23 18:30:45 -0700175 module_shut_down(get_module(HCI_MODULE));
Zach Johnson9891f322014-09-22 22:11:55 -0700176 module_shut_down(get_module(BTSNOOP_MODULE));
Zach Johnson733a06e2014-09-08 18:31:39 -0700177
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700178 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700179}
180
181/******************************************************************************
182**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800183** Function bte_main_postload_cfg
184**
185** Description BTE MAIN API - Stack postload configuration
186**
187** Returns None
188**
189******************************************************************************/
190void bte_main_postload_cfg(void)
191{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700192 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193}
194
195#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
196/******************************************************************************
197**
198** Function bte_main_enable_lpm
199**
200** Description BTE MAIN API - Enable/Disable low power mode operation
201**
202** Returns None
203**
204******************************************************************************/
205void bte_main_enable_lpm(BOOLEAN enable)
206{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700207 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800208}
209
210/******************************************************************************
211**
212** Function bte_main_lpm_allow_bt_device_sleep
213**
214** Description BTE MAIN API - Allow BT controller goest to sleep
215**
216** Returns None
217**
218******************************************************************************/
219void bte_main_lpm_allow_bt_device_sleep()
220{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700221 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222}
223
224/******************************************************************************
225**
226** Function bte_main_lpm_wake_bt_device
227**
228** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
229**
230** Returns None
231**
232******************************************************************************/
233void bte_main_lpm_wake_bt_device()
234{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700235 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800236}
237#endif // HCILP_INCLUDED
238
Matthew Xie66432dc2014-04-27 05:45:32 -0700239
240/* NOTICE:
241 * Definitions for audio state structure, this type needs to match to
242 * the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h
243 */
244typedef struct {
245 UINT16 handle;
246 UINT16 peer_codec;
247 UINT16 state;
248} bt_hc_audio_state_t;
249
250struct bt_audio_state_tag {
251 BT_HDR hdr;
252 bt_hc_audio_state_t audio;
253};
254
255/******************************************************************************
256**
257** Function set_audio_state
258**
259** Description Sets audio state on controller state for SCO (PCM, WBS, FM)
260**
261** Parameters handle: codec related handle for SCO: sco cb idx, unused for
262** codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec
263** state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP
264** param: future extensions, e.g. call-in structure/event.
265**
266** Returns None
267**
268******************************************************************************/
269int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param)
270{
271 struct bt_audio_state_tag *p_msg;
272 int result = -1;
273
274 APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle,
275 codec, state);
276 if (NULL != param)
277 APPL_TRACE_WARNING("set_audio_state() non-null param not supported");
278 p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg));
279 if (!p_msg)
280 return result;
281 p_msg->audio.handle = handle;
282 p_msg->audio.peer_codec = codec;
283 p_msg->audio.state = state;
284
285 p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE);
286 p_msg->hdr.len = sizeof(p_msg->audio);
287 p_msg->hdr.offset = 0;
288 /* layer_specific shall contain return path event! for BTA events!
289 * 0 means no return message is expected. */
290 p_msg->hdr.layer_specific = 0;
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700291 hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg);
Matthew Xie66432dc2014-04-27 05:45:32 -0700292 return result;
293}
294
295
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296/******************************************************************************
297**
298** Function bte_main_hci_send
299**
300** Description BTE MAIN API - This function is called by the upper stack to
301** send an HCI message. The function displays a protocol trace
302** message (if enabled), and then calls the 'transmit' function
303** associated with the currently selected HCI transport
304**
305** Returns None
306**
307******************************************************************************/
308void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
309{
310 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
311
312 p_msg->event = event;
313
Chris Manton78a51cb2014-08-13 16:38:57 -0700314 counter_add("main.tx.packets", 1);
315 counter_add("main.tx.bytes", p_msg->len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800316
317 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
318 (sub_event == LOCAL_BLE_CONTROLLER_ID))
319 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700320 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800321 }
322 else
323 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700324 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800325 GKI_freebuf(p_msg);
326 }
327}