blob: 14612a3900aef1c3500b89c71fd3121693791198 [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
Jack Hef2af1c42016-12-13 01:59:12 -080021#include <base/logging.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"
Myles Watson84baa7f2016-11-14 12:05:37 -080029#include "gatt_api.h"
30#include "gatt_int.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include "l2c_int.h"
Marie Janssen49a86702015-07-08 11:48:57 -070032#include "osi/include/alarm.h"
33#include "osi/include/fixed_queue.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080034#include "osi/include/log.h"
Marie Janssen49a86702015-07-08 11:48:57 -070035#include "osi/include/thread.h"
36#include "sdpint.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include "smp_int.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038
Philip Cuadra0035d8c2017-05-02 21:00:02 -070039// RT priority for audio-related tasks
40#define BTU_TASK_RT_PRIORITY 1
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -070041
Myles Watson911d1ae2016-11-28 16:44:40 -080042extern fixed_queue_t* btif_msg_queue;
Chris Manton307381e2014-09-04 19:48:49 -070043
44// Communication queue from bta thread to bt_workqueue.
Myles Watson911d1ae2016-11-28 16:44:40 -080045fixed_queue_t* btu_bta_msg_queue;
Chris Manton307381e2014-09-04 19:48:49 -070046
47// Communication queue from hci thread to bt_workqueue.
Myles Watson911d1ae2016-11-28 16:44:40 -080048extern fixed_queue_t* btu_hci_msg_queue;
Chris Manton860a9af2014-08-27 10:30:47 -070049
Chris Manton307381e2014-09-04 19:48:49 -070050// General timer queue.
Myles Watson911d1ae2016-11-28 16:44:40 -080051fixed_queue_t* btu_general_alarm_queue;
Chris Manton307381e2014-09-04 19:48:49 -070052
Myles Watson911d1ae2016-11-28 16:44:40 -080053thread_t* bt_workqueue_thread;
54static const char* BT_WORKQUEUE_NAME = "bt_workqueue";
Chris Manton307381e2014-09-04 19:48:49 -070055
Marie Janssend19e0782016-07-15 12:48:27 -070056extern void PLATFORM_DisableHciTransport(uint8_t bDisable);
The Android Open Source Project5738f832012-12-12 16:00:35 -080057/*****************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080058 * V A R I A B L E S *
59 *****************************************************************************/
Chris Manton307381e2014-09-04 19:48:49 -070060// TODO(cmanton) Move this out of this file
Myles Watson911d1ae2016-11-28 16:44:40 -080061const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
The Android Open Source Project5738f832012-12-12 16:00:35 -080062
Myles Watson911d1ae2016-11-28 16:44:40 -080063void btu_task_start_up(void* context);
64void btu_task_shut_down(void* context);
Chris Manton307381e2014-09-04 19:48:49 -070065
The Android Open Source Project5738f832012-12-12 16:00:35 -080066/*****************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080067 *
68 * Function btu_init_core
69 *
70 * Description Initialize control block memory for each core component.
71 *
72 *
73 * Returns void
74 *
75 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -080076void btu_init_core(void) {
77 /* Initialize the mandatory core stack components */
78 btm_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -080079
Myles Watson911d1ae2016-11-28 16:44:40 -080080 l2c_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -080081
Myles Watson911d1ae2016-11-28 16:44:40 -080082 sdp_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
Myles Watson911d1ae2016-11-28 16:44:40 -080084 gatt_init();
Myles Watson2e8e9f42016-11-14 16:45:15 -080085
Myles Watson911d1ae2016-11-28 16:44:40 -080086 SMP_Init();
Myles Watson2e8e9f42016-11-14 16:45:15 -080087
Myles Watson911d1ae2016-11-28 16:44:40 -080088 btm_ble_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -080089}
90
Chris Manton49ada1e2014-02-21 12:36:18 -080091/*****************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080092 *
93 * Function btu_free_core
94 *
95 * Description Releases control block memory for each core component.
96 *
97 *
98 * Returns void
99 *
100 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800101void btu_free_core(void) {
102 /* Free the mandatory core stack components */
103 l2c_free();
Chris Manton305c1592014-09-19 10:49:50 -0700104
Myles Watson911d1ae2016-11-28 16:44:40 -0800105 gatt_free();
Chris Manton49ada1e2014-02-21 12:36:18 -0800106}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800107
108/*****************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800109 *
110 * Function BTU_StartUp
111 *
112 * Description Initializes the BTU control block.
113 *
114 * NOTE: Must be called before creating any tasks
115 * (RPC, BTU, HCIT, APPL, etc.)
116 *
117 * Returns void
118 *
119 *****************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800120void BTU_StartUp(void) {
121 btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800122
Myles Watson911d1ae2016-11-28 16:44:40 -0800123 btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
124 if (btu_bta_msg_queue == NULL) goto error_exit;
Chris Manton307381e2014-09-04 19:48:49 -0700125
Myles Watson911d1ae2016-11-28 16:44:40 -0800126 btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
127 if (btu_general_alarm_queue == NULL) goto error_exit;
Chris Manton307381e2014-09-04 19:48:49 -0700128
Myles Watson911d1ae2016-11-28 16:44:40 -0800129 bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
130 if (bt_workqueue_thread == NULL) goto error_exit;
Chris Manton307381e2014-09-04 19:48:49 -0700131
Philip Cuadra0035d8c2017-05-02 21:00:02 -0700132 thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -0700133
Myles Watson911d1ae2016-11-28 16:44:40 -0800134 // Continue startup on bt workqueue thread.
135 thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
136 return;
Chris Manton307381e2014-09-04 19:48:49 -0700137
Myles Watson911d1ae2016-11-28 16:44:40 -0800138error_exit:;
139 LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue",
140 __func__);
141 BTU_ShutDown();
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700142}
143
Sharvil Nanavatif79d2862014-09-06 16:16:19 -0700144void BTU_ShutDown(void) {
Zach Johnsonc8ac8a22014-09-26 17:04:51 -0700145 btu_task_shut_down(NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700146
147 fixed_queue_free(btu_bta_msg_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700148 btu_bta_msg_queue = NULL;
149
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700150 fixed_queue_free(btu_general_alarm_queue, NULL);
Chris Manton307381e2014-09-04 19:48:49 -0700151 btu_general_alarm_queue = NULL;
152
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700153 thread_free(bt_workqueue_thread);
154
Chris Manton307381e2014-09-04 19:48:49 -0700155 bt_workqueue_thread = NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156}