The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 19 | #define LOG_TAG "bt_task" |
| 20 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 21 | #include <base/logging.h> |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 23 | #include <string.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 24 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 25 | #include "bt_target.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 26 | #include "btm_int.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 27 | #include "btu.h" |
| 28 | #include "device/include/controller.h" |
Myles Watson | 84baa7f | 2016-11-14 12:05:37 -0800 | [diff] [blame] | 29 | #include "gatt_api.h" |
| 30 | #include "gatt_int.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 31 | #include "l2c_int.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 32 | #include "osi/include/alarm.h" |
| 33 | #include "osi/include/fixed_queue.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 34 | #include "osi/include/log.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 35 | #include "osi/include/thread.h" |
| 36 | #include "sdpint.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 37 | #include "smp_int.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 38 | |
Philip Cuadra | 0035d8c | 2017-05-02 21:00:02 -0700 | [diff] [blame^] | 39 | // RT priority for audio-related tasks |
| 40 | #define BTU_TASK_RT_PRIORITY 1 |
Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 41 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 42 | extern fixed_queue_t* btif_msg_queue; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 43 | |
| 44 | // Communication queue from bta thread to bt_workqueue. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 45 | fixed_queue_t* btu_bta_msg_queue; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 46 | |
| 47 | // Communication queue from hci thread to bt_workqueue. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 48 | extern fixed_queue_t* btu_hci_msg_queue; |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 49 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 50 | // General timer queue. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 51 | fixed_queue_t* btu_general_alarm_queue; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 52 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 53 | thread_t* bt_workqueue_thread; |
| 54 | static const char* BT_WORKQUEUE_NAME = "bt_workqueue"; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 55 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 56 | extern void PLATFORM_DisableHciTransport(uint8_t bDisable); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 57 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 58 | * V A R I A B L E S * |
| 59 | *****************************************************************************/ |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 60 | // TODO(cmanton) Move this out of this file |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 61 | const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 62 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 63 | void btu_task_start_up(void* context); |
| 64 | void btu_task_shut_down(void* context); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 65 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 66 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 67 | * |
| 68 | * Function btu_init_core |
| 69 | * |
| 70 | * Description Initialize control block memory for each core component. |
| 71 | * |
| 72 | * |
| 73 | * Returns void |
| 74 | * |
| 75 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 76 | void btu_init_core(void) { |
| 77 | /* Initialize the mandatory core stack components */ |
| 78 | btm_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 79 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 80 | l2c_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 81 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 82 | sdp_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 83 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 84 | gatt_init(); |
Myles Watson | 2e8e9f4 | 2016-11-14 16:45:15 -0800 | [diff] [blame] | 85 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 86 | SMP_Init(); |
Myles Watson | 2e8e9f4 | 2016-11-14 16:45:15 -0800 | [diff] [blame] | 87 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 88 | btm_ble_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Chris Manton | 49ada1e | 2014-02-21 12:36:18 -0800 | [diff] [blame] | 91 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 92 | * |
| 93 | * Function btu_free_core |
| 94 | * |
| 95 | * Description Releases control block memory for each core component. |
| 96 | * |
| 97 | * |
| 98 | * Returns void |
| 99 | * |
| 100 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 101 | void btu_free_core(void) { |
| 102 | /* Free the mandatory core stack components */ |
| 103 | l2c_free(); |
Chris Manton | 305c159 | 2014-09-19 10:49:50 -0700 | [diff] [blame] | 104 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 105 | gatt_free(); |
Chris Manton | 49ada1e | 2014-02-21 12:36:18 -0800 | [diff] [blame] | 106 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 107 | |
| 108 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 109 | * |
| 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 Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 120 | void BTU_StartUp(void) { |
| 121 | btu_trace_level = HCI_INITIAL_TRACE_LEVEL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 122 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 123 | btu_bta_msg_queue = fixed_queue_new(SIZE_MAX); |
| 124 | if (btu_bta_msg_queue == NULL) goto error_exit; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 125 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 126 | btu_general_alarm_queue = fixed_queue_new(SIZE_MAX); |
| 127 | if (btu_general_alarm_queue == NULL) goto error_exit; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 128 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 129 | bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME); |
| 130 | if (bt_workqueue_thread == NULL) goto error_exit; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 131 | |
Philip Cuadra | 0035d8c | 2017-05-02 21:00:02 -0700 | [diff] [blame^] | 132 | thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY); |
Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 133 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 134 | // Continue startup on bt workqueue thread. |
| 135 | thread_post(bt_workqueue_thread, btu_task_start_up, NULL); |
| 136 | return; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 137 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 138 | error_exit:; |
| 139 | LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", |
| 140 | __func__); |
| 141 | BTU_ShutDown(); |
Sharvil Nanavati | da0446b | 2014-08-29 18:28:19 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Sharvil Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 144 | void BTU_ShutDown(void) { |
Zach Johnson | c8ac8a2 | 2014-09-26 17:04:51 -0700 | [diff] [blame] | 145 | btu_task_shut_down(NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 146 | |
| 147 | fixed_queue_free(btu_bta_msg_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 148 | btu_bta_msg_queue = NULL; |
| 149 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 150 | fixed_queue_free(btu_general_alarm_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 151 | btu_general_alarm_queue = NULL; |
| 152 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 153 | thread_free(bt_workqueue_thread); |
| 154 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 155 | bt_workqueue_thread = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 156 | } |