blob: ee603258978e9aa492052209245b63011522390e [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"
33#include "osi/include/hash_functions.h"
34#include "osi/include/hash_map.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080035#include "osi/include/log.h"
Marie Janssen49a86702015-07-08 11:48:57 -070036#include "osi/include/thread.h"
37#include "sdpint.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038
39#if (BLE_INCLUDED == TRUE)
40#include "gatt_api.h"
41#include "gatt_int.h"
42#if SMP_INCLUDED == TRUE
43#include "smp_int.h"
44#endif
45#endif
46
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -070047// Increase BTU task thread priority to avoid pre-emption
48// of audio realated tasks.
49#define BTU_TASK_THREAD_PRIORITY -19
50
Chris Manton307381e2014-09-04 19:48:49 -070051extern fixed_queue_t *btif_msg_queue;
52
53// Communication queue from bta thread to bt_workqueue.
54fixed_queue_t *btu_bta_msg_queue;
55
56// Communication queue from hci thread to bt_workqueue.
Chris Manton860a9af2014-08-27 10:30:47 -070057extern fixed_queue_t *btu_hci_msg_queue;
Chris Manton860a9af2014-08-27 10:30:47 -070058
Chris Manton307381e2014-09-04 19:48:49 -070059// General timer queue.
60fixed_queue_t *btu_general_alarm_queue;
61hash_map_t *btu_general_alarm_hash_map;
62pthread_mutex_t btu_general_alarm_lock;
63static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 17;
64
65// Oneshot timer queue.
66fixed_queue_t *btu_oneshot_alarm_queue;
67hash_map_t *btu_oneshot_alarm_hash_map;
68pthread_mutex_t btu_oneshot_alarm_lock;
69static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 17;
70
71// l2cap timer queue.
72fixed_queue_t *btu_l2cap_alarm_queue;
73hash_map_t *btu_l2cap_alarm_hash_map;
74pthread_mutex_t btu_l2cap_alarm_lock;
75static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 17;
76
77thread_t *bt_workqueue_thread;
78static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
79
The Android Open Source Project5738f832012-12-12 16:00:35 -080080extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
81/*****************************************************************************
82** V A R I A B L E S *
83******************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -070084// TODO(cmanton) Move this out of this file
The Android Open Source Project5738f832012-12-12 16:00:35 -080085const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
86
Chris Manton307381e2014-09-04 19:48:49 -070087void btu_task_start_up(void *context);
88void btu_task_shut_down(void *context);
89
The Android Open Source Project5738f832012-12-12 16:00:35 -080090/*****************************************************************************
91**
92** Function btu_init_core
93**
94** Description Initialize control block memory for each core component.
95**
96**
97** Returns void
98**
99******************************************************************************/
100void btu_init_core(void)
101{
102 /* Initialize the mandatory core stack components */
103 btm_init();
104
105 l2c_init();
106
107 sdp_init();
108
109#if BLE_INCLUDED == TRUE
110 gatt_init();
111#if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
112 SMP_Init();
113#endif
114 btm_ble_init();
115#endif
116}
117
Chris Manton49ada1e2014-02-21 12:36:18 -0800118/*****************************************************************************
119**
120** Function btu_free_core
121**
122** Description Releases control block memory for each core component.
123**
124**
125** Returns void
126**
127******************************************************************************/
128void btu_free_core(void)
129{
130 /* Free the mandatory core stack components */
Chris Manton305c1592014-09-19 10:49:50 -0700131 l2c_free();
132
Chris Manton49ada1e2014-02-21 12:36:18 -0800133#if BLE_INCLUDED == TRUE
134 gatt_free();
135#endif
136}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137
138/*****************************************************************************
139**
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700140** Function BTU_StartUp
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141**
142** Description Initializes the BTU control block.
143**
144** NOTE: Must be called before creating any tasks
145** (RPC, BTU, HCIT, APPL, etc.)
146**
147** Returns void
148**
149******************************************************************************/
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700150void BTU_StartUp(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800151{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152 memset (&btu_cb, 0, sizeof (tBTU_CB));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800153 btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
154
Chris Manton307381e2014-09-04 19:48:49 -0700155 btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
156 if (btu_bta_msg_queue == NULL)
157 goto error_exit;
158
159 btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
Zach Johnsonaa3a0112014-11-05 14:25:49 -0800160 hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700161 if (btu_general_alarm_hash_map == NULL)
162 goto error_exit;
163
164 if (pthread_mutex_init(&btu_general_alarm_lock, NULL))
165 goto error_exit;
166
167 btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
168 if (btu_general_alarm_queue == NULL)
169 goto error_exit;
170
171 btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
Zach Johnsonaa3a0112014-11-05 14:25:49 -0800172 hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700173 if (btu_oneshot_alarm_hash_map == NULL)
174 goto error_exit;
175
176 if (pthread_mutex_init(&btu_oneshot_alarm_lock, NULL))
177 goto error_exit;
178
179 btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
180 if (btu_oneshot_alarm_queue == NULL)
181 goto error_exit;
182
183 btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
Zach Johnsonaa3a0112014-11-05 14:25:49 -0800184 hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700185 if (btu_l2cap_alarm_hash_map == NULL)
186 goto error_exit;
187
188 if (pthread_mutex_init(&btu_l2cap_alarm_lock, NULL))
189 goto error_exit;
190
191 btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
192 if (btu_l2cap_alarm_queue == NULL)
193 goto error_exit;
194
195 bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
196 if (bt_workqueue_thread == NULL)
197 goto error_exit;
198
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -0700199 thread_set_priority(bt_workqueue_thread, BTU_TASK_THREAD_PRIORITY);
200
Chris Manton307381e2014-09-04 19:48:49 -0700201 // Continue startup on bt workqueue thread.
202 thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
203 return;
204
205 error_exit:;
Marie Janssendb554582015-06-26 14:53:46 -0700206 LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", __func__);
Chris Manton307381e2014-09-04 19:48:49 -0700207 BTU_ShutDown();
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700208}
209
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700210void BTU_ShutDown(void) {
Zach Johnsonc8ac8a22014-09-26 17:04:51 -0700211 btu_task_shut_down(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700212
213 fixed_queue_free(btu_bta_msg_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700214 btu_bta_msg_queue = NULL;
215
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700216 hash_map_free(btu_general_alarm_hash_map);
Chris Manton307381e2014-09-04 19:48:49 -0700217 btu_general_alarm_hash_map = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700218 pthread_mutex_destroy(&btu_general_alarm_lock);
219 fixed_queue_free(btu_general_alarm_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700220 btu_general_alarm_queue = NULL;
221
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700222 hash_map_free(btu_oneshot_alarm_hash_map);
Chris Manton307381e2014-09-04 19:48:49 -0700223 btu_oneshot_alarm_hash_map = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700224 pthread_mutex_destroy(&btu_oneshot_alarm_lock);
225 fixed_queue_free(btu_oneshot_alarm_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700226 btu_oneshot_alarm_queue = NULL;
227
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700228 hash_map_free(btu_l2cap_alarm_hash_map);
Chris Manton307381e2014-09-04 19:48:49 -0700229 btu_l2cap_alarm_hash_map = NULL;
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700230 pthread_mutex_destroy(&btu_l2cap_alarm_lock);
231 fixed_queue_free(btu_l2cap_alarm_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700232 btu_l2cap_alarm_queue = NULL;
233
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700234 thread_free(bt_workqueue_thread);
235
Chris Manton307381e2014-09-04 19:48:49 -0700236 bt_workqueue_thread = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237}
238
The Android Open Source Project5738f832012-12-12 16:00:35 -0800239/*****************************************************************************
240**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800241** Function BTU_BleAclPktSize
242**
243** Description export the BLE ACL packet size.
244**
245** Returns UINT16
246**
247******************************************************************************/
248UINT16 BTU_BleAclPktSize(void)
249{
250#if BLE_INCLUDED == TRUE
Zach Johnson30e58062014-09-26 21:14:34 -0700251 return controller_get_interface()->get_acl_packet_size_ble();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800252#else
253 return 0;
254#endif
255}
256
257/*******************************************************************************
258**
259** Function btu_uipc_rx_cback
260**
261** Description
262**
263**
264** Returns void
265**
266*******************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -0700267void btu_uipc_rx_cback(BT_HDR *p_msg) {
268 assert(p_msg != NULL);
269 BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_DEBUG, "btu_uipc_rx_cback event 0x%x,"
270 " len %d, offset %d", p_msg->event, p_msg->len, p_msg->offset);
271 fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272}