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" |
| 33 | #include "osi/include/hash_functions.h" |
| 34 | #include "osi/include/hash_map.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 35 | #include "osi/include/log.h" |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 36 | #include "osi/include/thread.h" |
| 37 | #include "sdpint.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 38 | |
| 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 Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 47 | // Increase BTU task thread priority to avoid pre-emption |
| 48 | // of audio realated tasks. |
| 49 | #define BTU_TASK_THREAD_PRIORITY -19 |
| 50 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 51 | extern fixed_queue_t *btif_msg_queue; |
| 52 | |
| 53 | // Communication queue from bta thread to bt_workqueue. |
| 54 | fixed_queue_t *btu_bta_msg_queue; |
| 55 | |
| 56 | // Communication queue from hci thread to bt_workqueue. |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 57 | extern fixed_queue_t *btu_hci_msg_queue; |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 58 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 59 | // General timer queue. |
| 60 | fixed_queue_t *btu_general_alarm_queue; |
| 61 | hash_map_t *btu_general_alarm_hash_map; |
| 62 | pthread_mutex_t btu_general_alarm_lock; |
| 63 | static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 17; |
| 64 | |
| 65 | // Oneshot timer queue. |
| 66 | fixed_queue_t *btu_oneshot_alarm_queue; |
| 67 | hash_map_t *btu_oneshot_alarm_hash_map; |
| 68 | pthread_mutex_t btu_oneshot_alarm_lock; |
| 69 | static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 17; |
| 70 | |
| 71 | // l2cap timer queue. |
| 72 | fixed_queue_t *btu_l2cap_alarm_queue; |
| 73 | hash_map_t *btu_l2cap_alarm_hash_map; |
| 74 | pthread_mutex_t btu_l2cap_alarm_lock; |
| 75 | static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 17; |
| 76 | |
| 77 | thread_t *bt_workqueue_thread; |
| 78 | static const char *BT_WORKQUEUE_NAME = "bt_workqueue"; |
| 79 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 80 | extern void PLATFORM_DisableHciTransport(UINT8 bDisable); |
| 81 | /***************************************************************************** |
| 82 | ** V A R I A B L E S * |
| 83 | ******************************************************************************/ |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 84 | // TODO(cmanton) Move this out of this file |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 85 | const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 86 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 87 | void btu_task_start_up(void *context); |
| 88 | void btu_task_shut_down(void *context); |
| 89 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 90 | /***************************************************************************** |
| 91 | ** |
| 92 | ** Function btu_init_core |
| 93 | ** |
| 94 | ** Description Initialize control block memory for each core component. |
| 95 | ** |
| 96 | ** |
| 97 | ** Returns void |
| 98 | ** |
| 99 | ******************************************************************************/ |
| 100 | void 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 Manton | 49ada1e | 2014-02-21 12:36:18 -0800 | [diff] [blame] | 118 | /***************************************************************************** |
| 119 | ** |
| 120 | ** Function btu_free_core |
| 121 | ** |
| 122 | ** Description Releases control block memory for each core component. |
| 123 | ** |
| 124 | ** |
| 125 | ** Returns void |
| 126 | ** |
| 127 | ******************************************************************************/ |
| 128 | void btu_free_core(void) |
| 129 | { |
| 130 | /* Free the mandatory core stack components */ |
Chris Manton | 305c159 | 2014-09-19 10:49:50 -0700 | [diff] [blame] | 131 | l2c_free(); |
| 132 | |
Chris Manton | 49ada1e | 2014-02-21 12:36:18 -0800 | [diff] [blame] | 133 | #if BLE_INCLUDED == TRUE |
| 134 | gatt_free(); |
| 135 | #endif |
| 136 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 137 | |
| 138 | /***************************************************************************** |
| 139 | ** |
Sharvil Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 140 | ** Function BTU_StartUp |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 141 | ** |
| 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 Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 150 | void BTU_StartUp(void) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 151 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 152 | memset (&btu_cb, 0, sizeof (tBTU_CB)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 153 | btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL; |
| 154 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 155 | 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 Johnson | aa3a011 | 2014-11-05 14:25:49 -0800 | [diff] [blame] | 160 | hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 161 | 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 Johnson | aa3a011 | 2014-11-05 14:25:49 -0800 | [diff] [blame] | 172 | hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 173 | 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 Johnson | aa3a011 | 2014-11-05 14:25:49 -0800 | [diff] [blame] | 184 | hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 185 | 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 Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 199 | thread_set_priority(bt_workqueue_thread, BTU_TASK_THREAD_PRIORITY); |
| 200 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 201 | // Continue startup on bt workqueue thread. |
| 202 | thread_post(bt_workqueue_thread, btu_task_start_up, NULL); |
| 203 | return; |
| 204 | |
| 205 | error_exit:; |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 206 | 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] | 207 | BTU_ShutDown(); |
Sharvil Nanavati | da0446b | 2014-08-29 18:28:19 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Sharvil Nanavati | f79d286 | 2014-09-06 16:16:19 -0700 | [diff] [blame] | 210 | void BTU_ShutDown(void) { |
Zach Johnson | c8ac8a2 | 2014-09-26 17:04:51 -0700 | [diff] [blame] | 211 | btu_task_shut_down(NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 212 | |
| 213 | fixed_queue_free(btu_bta_msg_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 214 | btu_bta_msg_queue = NULL; |
| 215 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 216 | hash_map_free(btu_general_alarm_hash_map); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 217 | btu_general_alarm_hash_map = NULL; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 218 | pthread_mutex_destroy(&btu_general_alarm_lock); |
| 219 | fixed_queue_free(btu_general_alarm_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 220 | btu_general_alarm_queue = NULL; |
| 221 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 222 | hash_map_free(btu_oneshot_alarm_hash_map); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 223 | btu_oneshot_alarm_hash_map = NULL; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 224 | pthread_mutex_destroy(&btu_oneshot_alarm_lock); |
| 225 | fixed_queue_free(btu_oneshot_alarm_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 226 | btu_oneshot_alarm_queue = NULL; |
| 227 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 228 | hash_map_free(btu_l2cap_alarm_hash_map); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 229 | btu_l2cap_alarm_hash_map = NULL; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 230 | pthread_mutex_destroy(&btu_l2cap_alarm_lock); |
| 231 | fixed_queue_free(btu_l2cap_alarm_queue, NULL); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 232 | btu_l2cap_alarm_queue = NULL; |
| 233 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 234 | thread_free(bt_workqueue_thread); |
| 235 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 236 | bt_workqueue_thread = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 237 | } |
| 238 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 239 | /***************************************************************************** |
| 240 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 241 | ** Function BTU_BleAclPktSize |
| 242 | ** |
| 243 | ** Description export the BLE ACL packet size. |
| 244 | ** |
| 245 | ** Returns UINT16 |
| 246 | ** |
| 247 | ******************************************************************************/ |
| 248 | UINT16 BTU_BleAclPktSize(void) |
| 249 | { |
| 250 | #if BLE_INCLUDED == TRUE |
Zach Johnson | 30e5806 | 2014-09-26 21:14:34 -0700 | [diff] [blame] | 251 | return controller_get_interface()->get_acl_packet_size_ble(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 252 | #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 Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 267 | void 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 272 | } |