blob: 7692a396f0b68bfb29cdf32de690b4ec5a3999af [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2000-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
Sharvil Nanavati44802762014-12-23 23:08:58 -080019#define LOG_TAG "bt_task"
20
Chris Manton307381e2014-09-04 19:48:49 -070021#include <assert.h>
Chris Manton307381e2014-09-04 19:48:49 -070022#include <pthread.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080023#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080024
Marie Janssen49a86702015-07-08 11:48:57 -070025#include "bt_target.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080026#include "btm_int.h"
Marie Janssen49a86702015-07-08 11:48:57 -070027#include "btu.h"
28#include "device/include/controller.h"
29#include "dyn_mem.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include "l2c_int.h"
Marie Janssen49a86702015-07-08 11:48:57 -070031#include "osi/include/alarm.h"
32#include "osi/include/fixed_queue.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080033#include "osi/include/log.h"
Marie Janssen49a86702015-07-08 11:48:57 -070034#include "osi/include/thread.h"
35#include "sdpint.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080036
37#if (BLE_INCLUDED == TRUE)
38#include "gatt_api.h"
39#include "gatt_int.h"
Marie Janssend19e0782016-07-15 12:48:27 -070040#if (SMP_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -080041#include "smp_int.h"
42#endif
43#endif
44
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -070045// Increase BTU task thread priority to avoid pre-emption
46// of audio realated tasks.
Chih-Hung Hsieh63b05192016-05-20 10:29:31 -070047#define BTU_TASK_THREAD_PRIORITY (-19)
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -070048
Chris Manton307381e2014-09-04 19:48:49 -070049extern fixed_queue_t *btif_msg_queue;
50
51// Communication queue from bta thread to bt_workqueue.
52fixed_queue_t *btu_bta_msg_queue;
53
54// Communication queue from hci thread to bt_workqueue.
Chris Manton860a9af2014-08-27 10:30:47 -070055extern fixed_queue_t *btu_hci_msg_queue;
Chris Manton860a9af2014-08-27 10:30:47 -070056
Chris Manton307381e2014-09-04 19:48:49 -070057// General timer queue.
58fixed_queue_t *btu_general_alarm_queue;
Chris Manton307381e2014-09-04 19:48:49 -070059
60thread_t *bt_workqueue_thread;
61static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
62
Marie Janssend19e0782016-07-15 12:48:27 -070063extern void PLATFORM_DisableHciTransport(uint8_t bDisable);
The Android Open Source Project5738f832012-12-12 16:00:35 -080064/*****************************************************************************
65** V A R I A B L E S *
66******************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -070067// TODO(cmanton) Move this out of this file
The Android Open Source Project5738f832012-12-12 16:00:35 -080068const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
69
Chris Manton307381e2014-09-04 19:48:49 -070070void btu_task_start_up(void *context);
71void btu_task_shut_down(void *context);
72
The Android Open Source Project5738f832012-12-12 16:00:35 -080073/*****************************************************************************
74**
75** Function btu_init_core
76**
77** Description Initialize control block memory for each core component.
78**
79**
80** Returns void
81**
82******************************************************************************/
83void btu_init_core(void)
84{
85 /* Initialize the mandatory core stack components */
86 btm_init();
87
88 l2c_init();
89
90 sdp_init();
91
Marie Janssend19e0782016-07-15 12:48:27 -070092#if (BLE_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -080093 gatt_init();
Marie Janssend19e0782016-07-15 12:48:27 -070094#if (SMP_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -080095 SMP_Init();
96#endif
97 btm_ble_init();
98#endif
99}
100
Chris Manton49ada1e2014-02-21 12:36:18 -0800101/*****************************************************************************
102**
103** Function btu_free_core
104**
105** Description Releases control block memory for each core component.
106**
107**
108** Returns void
109**
110******************************************************************************/
111void btu_free_core(void)
112{
113 /* Free the mandatory core stack components */
Chris Manton305c1592014-09-19 10:49:50 -0700114 l2c_free();
115
Marie Janssend19e0782016-07-15 12:48:27 -0700116#if (BLE_INCLUDED == TRUE)
Chris Manton49ada1e2014-02-21 12:36:18 -0800117 gatt_free();
118#endif
119}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800120
121/*****************************************************************************
122**
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700123** Function BTU_StartUp
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124**
125** Description Initializes the BTU control block.
126**
127** NOTE: Must be called before creating any tasks
128** (RPC, BTU, HCIT, APPL, etc.)
129**
130** Returns void
131**
132******************************************************************************/
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700133void BTU_StartUp(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800134{
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800135 btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136
Chris Manton307381e2014-09-04 19:48:49 -0700137 btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
138 if (btu_bta_msg_queue == NULL)
139 goto error_exit;
140
Chris Manton307381e2014-09-04 19:48:49 -0700141 btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
142 if (btu_general_alarm_queue == NULL)
143 goto error_exit;
144
Chris Manton307381e2014-09-04 19:48:49 -0700145 bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
146 if (bt_workqueue_thread == NULL)
147 goto error_exit;
148
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -0700149 thread_set_priority(bt_workqueue_thread, BTU_TASK_THREAD_PRIORITY);
150
Chris Manton307381e2014-09-04 19:48:49 -0700151 // Continue startup on bt workqueue thread.
152 thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
153 return;
154
155 error_exit:;
Marie Janssendb554582015-06-26 14:53:46 -0700156 LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", __func__);
Chris Manton307381e2014-09-04 19:48:49 -0700157 BTU_ShutDown();
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700158}
159
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700160void BTU_ShutDown(void) {
Zach Johnsonc8ac8a22014-09-26 17:04:51 -0700161 btu_task_shut_down(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700162
163 fixed_queue_free(btu_bta_msg_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700164 btu_bta_msg_queue = NULL;
165
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700166 fixed_queue_free(btu_general_alarm_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700167 btu_general_alarm_queue = NULL;
168
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700169 thread_free(bt_workqueue_thread);
170
Chris Manton307381e2014-09-04 19:48:49 -0700171 bt_workqueue_thread = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800172}