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 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 21 | #include <assert.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" |
| 29 | #include "dyn_mem.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 30 | #include "l2c_int.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 31 | #include "osi/include/alarm.h" |
| 32 | #include "osi/include/fixed_queue.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 33 | #include "osi/include/log.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 34 | #include "osi/include/thread.h" |
| 35 | #include "sdpint.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 36 | |
| 37 | #if (BLE_INCLUDED == TRUE) |
| 38 | #include "gatt_api.h" |
| 39 | #include "gatt_int.h" |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 40 | #if (SMP_INCLUDED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 41 | #include "smp_int.h" |
| 42 | #endif |
| 43 | #endif |
| 44 | |
Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 45 | // Increase BTU task thread priority to avoid pre-emption |
| 46 | // of audio realated tasks. |
Chih-Hung Hsieh | 63b0519 | 2016-05-20 10:29:31 -0700 | [diff] [blame] | 47 | #define BTU_TASK_THREAD_PRIORITY (-19) |
Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 48 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 49 | extern fixed_queue_t *btif_msg_queue; |
| 50 | |
| 51 | // Communication queue from bta thread to bt_workqueue. |
| 52 | fixed_queue_t *btu_bta_msg_queue; |
| 53 | |
| 54 | // Communication queue from hci thread to bt_workqueue. |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 55 | extern fixed_queue_t *btu_hci_msg_queue; |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 56 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 57 | // General timer queue. |
| 58 | fixed_queue_t *btu_general_alarm_queue; |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 59 | |
| 60 | thread_t *bt_workqueue_thread; |
| 61 | static const char *BT_WORKQUEUE_NAME = "bt_workqueue"; |
| 62 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 63 | extern void PLATFORM_DisableHciTransport(uint8_t bDisable); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 64 | /***************************************************************************** |
| 65 | ** V A R I A B L E S * |
| 66 | ******************************************************************************/ |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 67 | // TODO(cmanton) Move this out of this file |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 68 | const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 69 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 70 | void btu_task_start_up(void *context); |
| 71 | void btu_task_shut_down(void *context); |
| 72 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 73 | /***************************************************************************** |
| 74 | ** |
| 75 | ** Function btu_init_core |
| 76 | ** |
| 77 | ** Description Initialize control block memory for each core component. |
| 78 | ** |
| 79 | ** |
| 80 | ** Returns void |
| 81 | ** |
| 82 | ******************************************************************************/ |
| 83 | void 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 Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 92 | #if (BLE_INCLUDED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 93 | gatt_init(); |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 94 | #if (SMP_INCLUDED == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 95 | SMP_Init(); |
| 96 | #endif |
| 97 | btm_ble_init(); |
| 98 | #endif |
| 99 | } |
| 100 | |
Chris Manton | 49ada1e | 2014-02-21 12:36:18 -0800 | [diff] [blame] | 101 | /***************************************************************************** |
| 102 | ** |
| 103 | ** Function btu_free_core |
| 104 | ** |
| 105 | ** Description Releases control block memory for each core component. |
| 106 | ** |
| 107 | ** |
| 108 | ** Returns void |
| 109 | ** |
| 110 | ******************************************************************************/ |
| 111 | void btu_free_core(void) |
| 112 | { |
| 113 | /* Free the mandatory core stack components */ |
Chris Manton | 305c159 | 2014-09-19 10:49:50 -0700 | [diff] [blame] | 114 | l2c_free(); |
| 115 | |
Marie Janssen | d19e078 | 2016-07-15 12:48:27 -0700 | [diff] [blame] | 116 | #if (BLE_INCLUDED == TRUE) |
Chris Manton | 49ada1e | 2014-02-21 12:36:18 -0800 | [diff] [blame] | 117 | gatt_free(); |
| 118 | #endif |
| 119 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 120 | |
| 121 | /***************************************************************************** |
| 122 | ** |
Sharvil Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 123 | ** Function BTU_StartUp |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 124 | ** |
| 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 Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 133 | void BTU_StartUp(void) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 134 | { |
Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 135 | btu_trace_level = HCI_INITIAL_TRACE_LEVEL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 136 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 137 | btu_bta_msg_queue = fixed_queue_new(SIZE_MAX); |
| 138 | if (btu_bta_msg_queue == NULL) |
| 139 | goto error_exit; |
| 140 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 141 | btu_general_alarm_queue = fixed_queue_new(SIZE_MAX); |
| 142 | if (btu_general_alarm_queue == NULL) |
| 143 | goto error_exit; |
| 144 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 145 | bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME); |
| 146 | if (bt_workqueue_thread == NULL) |
| 147 | goto error_exit; |
| 148 | |
Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 149 | thread_set_priority(bt_workqueue_thread, BTU_TASK_THREAD_PRIORITY); |
| 150 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 151 | // Continue startup on bt workqueue thread. |
| 152 | thread_post(bt_workqueue_thread, btu_task_start_up, NULL); |
| 153 | return; |
| 154 | |
| 155 | error_exit:; |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 156 | LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", __func__); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 157 | BTU_ShutDown(); |
Sharvil Nanavati | da0446b | 2014-08-29 18:28:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Sharvil Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 160 | void BTU_ShutDown(void) { |
Zach Johnson | c8ac8a2 | 2014-09-26 17:04:51 -0700 | [diff] [blame] | 161 | btu_task_shut_down(NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 162 | |
| 163 | fixed_queue_free(btu_bta_msg_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 164 | btu_bta_msg_queue = NULL; |
| 165 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 166 | fixed_queue_free(btu_general_alarm_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 167 | btu_general_alarm_queue = NULL; |
| 168 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 169 | thread_free(bt_workqueue_thread); |
| 170 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 171 | bt_workqueue_thread = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 172 | } |