blob: 0c5209a0c26b2cb782f4fc2ba84b62828e3f67f7 [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 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080026#include <assert.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070027#include <cutils/properties.h>
28#include <fcntl.h>
29#include <hardware/bluetooth.h>
Chris Manton18023292014-08-29 09:12:06 -070030#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070031#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070032#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070033#include <time.h>
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070034#include <utils/Log.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070036#include "alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include "bd.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"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070045#include "fixed_queue.h"
Chris Manton307381e2014-09-04 19:48:49 -070046#include "future.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070047#include "gki.h"
Chris Manton18023292014-08-29 09:12:06 -070048#include "hash_functions.h"
49#include "hash_map.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070050#include "hci_layer.h"
Zach Johnson9891f322014-09-22 22:11:55 -070051#include "module.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070052#include "osi.h"
Zach Johnson9891f322014-09-22 22:11:55 -070053#include "stack_config.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070054#include "thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080055
56/*******************************************************************************
57** Constants & Macros
58*******************************************************************************/
59
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070060/* Run-time configuration file for BLE*/
61#ifndef BTE_BLE_STACK_CONF_FILE
62#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
63#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080064
The Android Open Source Project5738f832012-12-12 16:00:35 -080065/******************************************************************************
66** Variables
67******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080068
The Android Open Source Project5738f832012-12-12 16:00:35 -080069/*******************************************************************************
70** Static variables
71*******************************************************************************/
Zach Johnsonbf8193b2014-09-08 09:56:35 -070072static const hci_t *hci;
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070073static const hci_callbacks_t hci_callbacks;
Zach Johnson7fa65f72014-08-29 18:20:48 -070074// Lock to serialize shutdown requests from upper layer.
75static pthread_mutex_t shutdown_lock;
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*******************************************************************************/
Sharvil Nanavatida0446b2014-08-29 18:28:19 -070084void BTE_LoadStack(void);
85void BTE_UnloadStack(void);
86void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
87void bte_load_conf(const char *p_path);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070088extern void bte_load_ble_conf(const char *p_path);
Sharvil Nanavatida0446b2014-08-29 18:28:19 -070089bt_bdaddr_t btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
Chris Manton307381e2014-09-04 19:48:49 -070091fixed_queue_t *btu_hci_msg_queue;
92
The Android Open Source Project5738f832012-12-12 16:00:35 -080093/******************************************************************************
94**
The Android Open Source Project5738f832012-12-12 16:00:35 -080095** Function bte_main_boot_entry
96**
97** Description BTE MAIN API - Entry point for BTE chip/stack initialization
98**
99** Returns None
100**
101******************************************************************************/
102void bte_main_boot_entry(void)
103{
104 /* initialize OS */
105 GKI_init();
106
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700107 hci = hci_layer_get_interface();
108 if (!hci)
109 ALOGE("%s could not get hci layer interface.", __func__);
110
Chris Manton307381e2014-09-04 19:48:49 -0700111 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
112 if (btu_hci_msg_queue == NULL) {
113 ALOGE("%s unable to allocate hci message queue.", __func__);
114 return;
115 }
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700116
Chris Manton307381e2014-09-04 19:48:49 -0700117 data_dispatcher_register_default(hci->upward_dispatcher, btu_hci_msg_queue);
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700118
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700119#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
120 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
121#endif
Zach Johnson9891f322014-09-22 22:11:55 -0700122 module_init(get_module(STACK_CONFIG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123
124#if (BTTRC_INCLUDED == TRUE)
125 /* Initialize trace feature */
126 BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
127#endif
Chris Manton6c25e792014-08-07 16:23:41 -0700128
Zach Johnson7fa65f72014-08-29 18:20:48 -0700129 pthread_mutex_init(&shutdown_lock, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130}
131
132/******************************************************************************
133**
134** Function bte_main_shutdown
135**
136** Description BTE MAIN API - Shutdown code for BTE chip/stack
137**
138** Returns None
139**
140******************************************************************************/
141void bte_main_shutdown()
142{
Zach Johnson733a06e2014-09-08 18:31:39 -0700143 data_dispatcher_register_default(hci_layer_get_interface()->upward_dispatcher, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700144 fixed_queue_free(btu_hci_msg_queue, NULL);
Zach Johnson733a06e2014-09-08 18:31:39 -0700145
Chris Manton307381e2014-09-04 19:48:49 -0700146 btu_hci_msg_queue = NULL;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700147
Zach Johnson9891f322014-09-22 22:11:55 -0700148 module_clean_up(get_module(STACK_CONFIG_MODULE));
149
Zach Johnson7fa65f72014-08-29 18:20:48 -0700150 pthread_mutex_destroy(&shutdown_lock);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800151 GKI_shutdown();
152}
153
154/******************************************************************************
155**
156** Function bte_main_enable
157**
158** Description BTE MAIN API - Creates all the BTE tasks. Should be called
159** part of the Bluetooth stack enable sequence
160**
161** Returns None
162**
163******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700164void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700166 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800167
Chris Manton307381e2014-09-04 19:48:49 -0700168// BTU_StartUp();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700169
Zach Johnson733a06e2014-09-08 18:31:39 -0700170 assert(hci->start_up_async(btif_local_bd_addr.address, &hci_callbacks));
Zach Johnson9891f322014-09-22 22:11:55 -0700171 module_start_up(get_module(BTSNOOP_MODULE));
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700172}
173
174/******************************************************************************
175**
176** Function bte_main_disable
177**
178** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
179** part of the Bluetooth stack disable sequence
180**
181** Returns None
182**
183******************************************************************************/
184void bte_main_disable(void)
185{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700186 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700187
Zach Johnson9891f322014-09-22 22:11:55 -0700188 module_shut_down(get_module(BTSNOOP_MODULE));
Zach Johnson733a06e2014-09-08 18:31:39 -0700189 if (hci) {
190 // Shutdown is not thread safe and must be protected.
191 pthread_mutex_lock(&shutdown_lock);
192
Zach Johnson733a06e2014-09-08 18:31:39 -0700193 hci->shut_down();
194
195 pthread_mutex_unlock(&shutdown_lock);
196 }
197
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700198 BTU_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700199}
200
201/******************************************************************************
202**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203** Function bte_main_postload_cfg
204**
205** Description BTE MAIN API - Stack postload configuration
206**
207** Returns None
208**
209******************************************************************************/
210void bte_main_postload_cfg(void)
211{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700212 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213}
214
215#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
216/******************************************************************************
217**
218** Function bte_main_enable_lpm
219**
220** Description BTE MAIN API - Enable/Disable low power mode operation
221**
222** Returns None
223**
224******************************************************************************/
225void bte_main_enable_lpm(BOOLEAN enable)
226{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700227 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800228}
229
230/******************************************************************************
231**
232** Function bte_main_lpm_allow_bt_device_sleep
233**
234** Description BTE MAIN API - Allow BT controller goest to sleep
235**
236** Returns None
237**
238******************************************************************************/
239void bte_main_lpm_allow_bt_device_sleep()
240{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700241 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800242}
243
244/******************************************************************************
245**
246** Function bte_main_lpm_wake_bt_device
247**
248** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
249**
250** Returns None
251**
252******************************************************************************/
253void bte_main_lpm_wake_bt_device()
254{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700255 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800256}
257#endif // HCILP_INCLUDED
258
Matthew Xie66432dc2014-04-27 05:45:32 -0700259
260/* NOTICE:
261 * Definitions for audio state structure, this type needs to match to
262 * the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h
263 */
264typedef struct {
265 UINT16 handle;
266 UINT16 peer_codec;
267 UINT16 state;
268} bt_hc_audio_state_t;
269
270struct bt_audio_state_tag {
271 BT_HDR hdr;
272 bt_hc_audio_state_t audio;
273};
274
275/******************************************************************************
276**
277** Function set_audio_state
278**
279** Description Sets audio state on controller state for SCO (PCM, WBS, FM)
280**
281** Parameters handle: codec related handle for SCO: sco cb idx, unused for
282** codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec
283** state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP
284** param: future extensions, e.g. call-in structure/event.
285**
286** Returns None
287**
288******************************************************************************/
289int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param)
290{
291 struct bt_audio_state_tag *p_msg;
292 int result = -1;
293
294 APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle,
295 codec, state);
296 if (NULL != param)
297 APPL_TRACE_WARNING("set_audio_state() non-null param not supported");
298 p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg));
299 if (!p_msg)
300 return result;
301 p_msg->audio.handle = handle;
302 p_msg->audio.peer_codec = codec;
303 p_msg->audio.state = state;
304
305 p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE);
306 p_msg->hdr.len = sizeof(p_msg->audio);
307 p_msg->hdr.offset = 0;
308 /* layer_specific shall contain return path event! for BTA events!
309 * 0 means no return message is expected. */
310 p_msg->hdr.layer_specific = 0;
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700311 hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg);
Matthew Xie66432dc2014-04-27 05:45:32 -0700312 return result;
313}
314
315
The Android Open Source Project5738f832012-12-12 16:00:35 -0800316/******************************************************************************
317**
318** Function bte_main_hci_send
319**
320** Description BTE MAIN API - This function is called by the upper stack to
321** send an HCI message. The function displays a protocol trace
322** message (if enabled), and then calls the 'transmit' function
323** associated with the currently selected HCI transport
324**
325** Returns None
326**
327******************************************************************************/
328void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
329{
330 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
331
332 p_msg->event = event;
333
334
335 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
336 (sub_event == LOCAL_BLE_CONTROLLER_ID))
337 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700338 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800339 }
340 else
341 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700342 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800343 GKI_freebuf(p_msg);
344 }
345}
346
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347/*****************************************************************************
348**
349** libbt-hci Callback Functions
350**
351*****************************************************************************/
352
353/******************************************************************************
354**
Zach Johnson733a06e2014-09-08 18:31:39 -0700355** Function hci_startup_cb
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356**
357** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
358** when the libbt-hci completed stack preload process
359**
360** Returns None
361**
362******************************************************************************/
Zach Johnson733a06e2014-09-08 18:31:39 -0700363static void hci_startup_cb(bool success)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700365 APPL_TRACE_EVENT("HC preload_cb %d [1:SUCCESS 0:FAIL]", success);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366
Zach Johnson733a06e2014-09-08 18:31:39 -0700367 if (success) {
Chris Manton307381e2014-09-04 19:48:49 -0700368 BTU_StartUp();
Zach Johnson733a06e2014-09-08 18:31:39 -0700369 } else {
Chris Manton307381e2014-09-04 19:48:49 -0700370 ALOGE("%s hci startup failed", __func__);
371 // TODO(cmanton) Initiate shutdown sequence.
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700372 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373}
374
The Android Open Source Project5738f832012-12-12 16:00:35 -0800375/******************************************************************************
376**
377** Function tx_result
378**
379** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
380** from the libbt-hci once it has processed/sent the prior data
381** buffer which core stack passed to it through transmit_buf
382** call earlier.
383**
384** The core stack is responsible for releasing the data buffer
385** if it has been completedly processed.
386**
387** Bluedroid libbt-hci library uses 'transac' parameter to
388** pass data-path buffer/packet across bt_hci_lib interface
389** boundary. The 'p_buf' is not intended to be used here
390** but might point to data portion in data-path buffer.
391**
Chris Manton307381e2014-09-04 19:48:49 -0700392** Returns void
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393**
394******************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -0700395static void tx_result(void *p_buf, bool all_fragments_sent) {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700396 if (!all_fragments_sent)
Chris Manton860a9af2014-08-27 10:30:47 -0700397 fixed_queue_enqueue(btu_hci_msg_queue, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398 else
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700399 GKI_freebuf(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400}
401
402/*****************************************************************************
403** The libbt-hci Callback Functions Table
404*****************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700405static const hci_callbacks_t hci_callbacks = {
Zach Johnson733a06e2014-09-08 18:31:39 -0700406 hci_startup_cb,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800407 tx_result
408};