The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 1999-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 | |
Chris Manton | f802700 | 2015-03-12 09:22:48 -0700 | [diff] [blame] | 19 | #define LOG_TAG "bt_btu_task" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 20 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 21 | #include <assert.h> |
Arman Uguray | 6850e33 | 2015-05-29 15:07:51 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 25 | #include <string.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 26 | |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 27 | #include "osi/include/alarm.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 28 | #include "bt_target.h" |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 29 | #include "bt_trace.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 30 | #include "bt_types.h" |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 31 | #include "bt_utils.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 32 | #include "btif_common.h" |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 33 | #include "btm_api.h" |
| 34 | #include "btm_int.h" |
| 35 | #include "btu.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 36 | #include "osi/include/fixed_queue.h" |
| 37 | #include "osi/include/future.h" |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 38 | #include "gki.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 39 | #include "osi/include/hash_map.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 40 | #include "hcimsgs.h" |
| 41 | #include "l2c_int.h" |
Sharvil Nanavati | 95b74f2 | 2015-03-12 15:55:21 -0700 | [diff] [blame] | 42 | #include "btcore/include/module.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 43 | #include "osi/include/osi.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 44 | #include "osi/include/log.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 45 | #include "sdpint.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 46 | #include "osi/include/thread.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 47 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 48 | #include "port_api.h" |
| 49 | #include "port_ext.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 50 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 51 | #include "gap_int.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 52 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 53 | #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE) |
| 54 | #include "bnep_int.h" |
| 55 | #endif |
| 56 | |
| 57 | #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE) |
| 58 | #include "pan_int.h" |
| 59 | #endif |
| 60 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 61 | #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE ) |
| 62 | #include "hidh_int.h" |
| 63 | #endif |
| 64 | |
| 65 | #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE) |
| 66 | #include "avdt_int.h" |
| 67 | #else |
| 68 | extern void avdt_rcv_sync_info (BT_HDR *p_buf); /* this is for hci_test */ |
| 69 | #endif |
| 70 | |
| 71 | #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE) |
| 72 | #include "mca_api.h" |
| 73 | #include "mca_defs.h" |
| 74 | #include "mca_int.h" |
| 75 | #endif |
| 76 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 77 | #include "bta_sys.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 78 | |
| 79 | #if (BLE_INCLUDED == TRUE) |
| 80 | #include "gatt_int.h" |
| 81 | #if (SMP_INCLUDED == TRUE) |
| 82 | #include "smp_int.h" |
| 83 | #endif |
| 84 | #include "btm_ble_int.h" |
| 85 | #endif |
| 86 | |
June R. Tate-Gans | 24933b5 | 2014-09-24 15:25:02 -0700 | [diff] [blame] | 87 | extern void BTE_InitStack(void); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 88 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | /* Define BTU storage area |
| 90 | */ |
| 91 | #if BTU_DYNAMIC_MEMORY == FALSE |
| 92 | tBTU_CB btu_cb; |
| 93 | #endif |
| 94 | |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 95 | // Communication queue between btu_task and bta. |
| 96 | extern fixed_queue_t *btu_bta_msg_queue; |
| 97 | |
| 98 | // Communication queue between btu_task and hci. |
| 99 | extern fixed_queue_t *btu_hci_msg_queue; |
| 100 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 101 | // General timer queue. |
| 102 | extern fixed_queue_t *btu_general_alarm_queue; |
| 103 | extern hash_map_t *btu_general_alarm_hash_map; |
| 104 | extern pthread_mutex_t btu_general_alarm_lock; |
| 105 | |
| 106 | // Oneshot timer queue. |
| 107 | extern fixed_queue_t *btu_oneshot_alarm_queue; |
| 108 | extern hash_map_t *btu_oneshot_alarm_hash_map; |
| 109 | extern pthread_mutex_t btu_oneshot_alarm_lock; |
| 110 | |
| 111 | // l2cap timer queue. |
| 112 | extern fixed_queue_t *btu_l2cap_alarm_queue; |
| 113 | extern hash_map_t *btu_l2cap_alarm_hash_map; |
| 114 | extern pthread_mutex_t btu_l2cap_alarm_lock; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 115 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 116 | extern fixed_queue_t *event_queue; |
| 117 | extern fixed_queue_t *btif_msg_queue; |
| 118 | |
| 119 | extern thread_t *bt_workqueue_thread; |
| 120 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 121 | /* Define a function prototype to allow a generic timeout handler */ |
| 122 | typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle); |
| 123 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 124 | static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle); |
| 125 | static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 126 | static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle); |
| 127 | static void btu_hci_msg_process(BT_HDR *p_msg); |
| 128 | |
| 129 | void btu_hci_msg_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 130 | BT_HDR *p_msg = (BT_HDR *)fixed_queue_dequeue(queue); |
| 131 | btu_hci_msg_process(p_msg); |
| 132 | } |
| 133 | |
| 134 | void btu_general_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 135 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue); |
| 136 | btu_general_alarm_process(p_tle); |
| 137 | } |
| 138 | |
| 139 | void btu_oneshot_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 140 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue); |
| 141 | btu_general_alarm_process(p_tle); |
| 142 | |
| 143 | switch (p_tle->event) { |
| 144 | #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE) |
| 145 | case BTU_TTYPE_BLE_RANDOM_ADDR: |
| 146 | btm_ble_timeout(p_tle); |
| 147 | break; |
| 148 | #endif |
| 149 | |
| 150 | case BTU_TTYPE_USER_FUNC: |
| 151 | { |
| 152 | tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param; |
| 153 | (*p_uf)(p_tle); |
| 154 | } |
| 155 | break; |
| 156 | |
| 157 | default: |
| 158 | // FAIL |
| 159 | BTM_TRACE_WARNING("Received unexpected oneshot timer event:0x%x\n", |
| 160 | p_tle->event); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void btu_l2cap_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 166 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue); |
| 167 | btu_l2cap_alarm_process(p_tle); |
| 168 | } |
| 169 | |
| 170 | void btu_bta_msg_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 171 | BT_HDR *p_msg = (BT_HDR *)fixed_queue_dequeue(queue); |
| 172 | bta_sys_event(p_msg); |
| 173 | } |
| 174 | |
| 175 | void btu_bta_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 176 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue); |
| 177 | btu_bta_alarm_process(p_tle); |
| 178 | } |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 179 | |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 180 | static void btu_hci_msg_process(BT_HDR *p_msg) { |
| 181 | /* Determine the input message type. */ |
| 182 | switch (p_msg->event & BT_EVT_MASK) |
| 183 | { |
Zach Johnson | 218f375 | 2014-09-03 14:36:44 -0700 | [diff] [blame] | 184 | case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this |
| 185 | ((post_to_task_hack_t *)(&p_msg->data[0]))->callback(p_msg); |
| 186 | break; |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 187 | case BT_EVT_TO_BTU_HCI_ACL: |
| 188 | /* All Acl Data goes to L2CAP */ |
| 189 | l2c_rcv_acl_data (p_msg); |
| 190 | break; |
| 191 | |
| 192 | case BT_EVT_TO_BTU_L2C_SEG_XMIT: |
| 193 | /* L2CAP segment transmit complete */ |
| 194 | l2c_link_segments_xmitted (p_msg); |
| 195 | break; |
| 196 | |
| 197 | case BT_EVT_TO_BTU_HCI_SCO: |
| 198 | #if BTM_SCO_INCLUDED == TRUE |
| 199 | btm_route_sco_data (p_msg); |
| 200 | break; |
| 201 | #endif |
| 202 | |
| 203 | case BT_EVT_TO_BTU_HCI_EVT: |
| 204 | btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg); |
| 205 | GKI_freebuf(p_msg); |
| 206 | |
| 207 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 208 | /* If host receives events which it doesn't response to, */ |
| 209 | /* host should start idle timer to enter sleep mode. */ |
| 210 | btu_check_bt_sleep (); |
| 211 | #endif |
| 212 | break; |
| 213 | |
| 214 | case BT_EVT_TO_BTU_HCI_CMD: |
| 215 | btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg); |
| 216 | break; |
| 217 | |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 218 | default:; |
| 219 | int i = 0; |
| 220 | uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK); |
| 221 | BOOLEAN handled = FALSE; |
| 222 | |
| 223 | for (; !handled && i < BTU_MAX_REG_EVENT; i++) |
| 224 | { |
| 225 | if (btu_cb.event_reg[i].event_cb == NULL) |
| 226 | continue; |
| 227 | |
| 228 | if (mask == btu_cb.event_reg[i].event_range) |
| 229 | { |
| 230 | if (btu_cb.event_reg[i].event_cb) |
| 231 | { |
| 232 | btu_cb.event_reg[i].event_cb(p_msg); |
| 233 | handled = TRUE; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (handled == FALSE) |
| 239 | GKI_freebuf (p_msg); |
| 240 | |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 246 | static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle) { |
| 247 | /* call timer callback */ |
| 248 | if (p_tle->p_cback) { |
| 249 | (*p_tle->p_cback)(p_tle); |
| 250 | } else if (p_tle->event) { |
| 251 | BT_HDR *p_msg; |
| 252 | if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) { |
| 253 | p_msg->event = p_tle->event; |
| 254 | p_msg->layer_specific = 0; |
| 255 | bta_sys_sendmsg(p_msg); |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 260 | void btu_task_start_up(UNUSED_ATTR void *context) { |
| 261 | BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API, |
| 262 | "btu_task pending for preload complete event"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 263 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 264 | LOG_INFO(LOG_TAG, "Bluetooth chip preload is complete"); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 265 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 266 | BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API, |
| 267 | "btu_task received preload complete event"); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 268 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 269 | /* Initialize the mandatory core stack control blocks |
| 270 | (BTU, BTM, L2CAP, and SDP) |
| 271 | */ |
| 272 | btu_init_core(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 273 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 274 | /* Initialize any optional stack components */ |
| 275 | BTE_InitStack(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 276 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 277 | bta_sys_init(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 278 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 279 | /* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init() |
| 280 | * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL |
| 281 | */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 282 | #if ( BT_USE_TRACES==TRUE ) |
Zach Johnson | 9891f32 | 2014-09-22 22:11:55 -0700 | [diff] [blame] | 283 | module_init(get_module(BTE_LOGMSG_MODULE)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 284 | #endif |
| 285 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 286 | // Inform the bt jni thread initialization is ok. |
| 287 | btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 288 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 289 | fixed_queue_register_dequeue(btu_bta_msg_queue, |
| 290 | thread_get_reactor(bt_workqueue_thread), |
| 291 | btu_bta_msg_ready, |
| 292 | NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 293 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 294 | fixed_queue_register_dequeue(btu_hci_msg_queue, |
| 295 | thread_get_reactor(bt_workqueue_thread), |
| 296 | btu_hci_msg_ready, |
| 297 | NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 298 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 299 | fixed_queue_register_dequeue(btu_general_alarm_queue, |
| 300 | thread_get_reactor(bt_workqueue_thread), |
| 301 | btu_general_alarm_ready, |
| 302 | NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 303 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 304 | fixed_queue_register_dequeue(btu_oneshot_alarm_queue, |
| 305 | thread_get_reactor(bt_workqueue_thread), |
| 306 | btu_oneshot_alarm_ready, |
| 307 | NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 308 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 309 | fixed_queue_register_dequeue(btu_l2cap_alarm_queue, |
| 310 | thread_get_reactor(bt_workqueue_thread), |
| 311 | btu_l2cap_alarm_ready, |
| 312 | NULL); |
| 313 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 314 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 315 | void btu_task_shut_down(UNUSED_ATTR void *context) { |
Zach Johnson | c8ac8a2 | 2014-09-26 17:04:51 -0700 | [diff] [blame] | 316 | fixed_queue_unregister_dequeue(btu_bta_msg_queue); |
| 317 | fixed_queue_unregister_dequeue(btu_hci_msg_queue); |
| 318 | fixed_queue_unregister_dequeue(btu_general_alarm_queue); |
| 319 | fixed_queue_unregister_dequeue(btu_oneshot_alarm_queue); |
| 320 | fixed_queue_unregister_dequeue(btu_l2cap_alarm_queue); |
| 321 | |
Chris Manton | b1f9915 | 2014-09-26 21:31:38 -0700 | [diff] [blame] | 322 | #if ( BT_USE_TRACES==TRUE ) |
| 323 | module_clean_up(get_module(BTE_LOGMSG_MODULE)); |
| 324 | #endif |
| 325 | |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 326 | bta_sys_free(); |
Chris Manton | 307381e | 2014-09-04 19:48:49 -0700 | [diff] [blame] | 327 | btu_free_core(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /******************************************************************************* |
| 331 | ** |
| 332 | ** Function btu_start_timer |
| 333 | ** |
| 334 | ** Description Start a timer for the specified amount of time. |
| 335 | ** NOTE: The timeout resolution is in SECONDS! (Even |
| 336 | ** though the timer structure field is ticks) |
| 337 | ** |
| 338 | ** Returns void |
| 339 | ** |
| 340 | *******************************************************************************/ |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 341 | static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle) { |
| 342 | assert(p_tle != NULL); |
| 343 | |
| 344 | switch (p_tle->event) { |
| 345 | case BTU_TTYPE_BTM_DEV_CTL: |
| 346 | btm_dev_timeout(p_tle); |
| 347 | break; |
| 348 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 349 | case BTU_TTYPE_L2CAP_LINK: |
| 350 | case BTU_TTYPE_L2CAP_CHNL: |
| 351 | case BTU_TTYPE_L2CAP_HOLD: |
| 352 | case BTU_TTYPE_L2CAP_INFO: |
| 353 | case BTU_TTYPE_L2CAP_FCR_ACK: |
| 354 | l2c_process_timeout (p_tle); |
| 355 | break; |
| 356 | |
| 357 | case BTU_TTYPE_SDP: |
| 358 | sdp_conn_timeout ((tCONN_CB *)p_tle->param); |
| 359 | break; |
| 360 | |
| 361 | case BTU_TTYPE_BTM_RMT_NAME: |
| 362 | btm_inq_rmt_name_failed(); |
| 363 | break; |
| 364 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 365 | case BTU_TTYPE_RFCOMM_MFC: |
| 366 | case BTU_TTYPE_RFCOMM_PORT: |
| 367 | rfcomm_process_timeout (p_tle); |
| 368 | break; |
| 369 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 370 | #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)) |
| 371 | case BTU_TTYPE_BNEP: |
| 372 | bnep_process_timeout(p_tle); |
| 373 | break; |
| 374 | #endif |
| 375 | |
| 376 | |
| 377 | #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE) |
| 378 | case BTU_TTYPE_AVDT_CCB_RET: |
| 379 | case BTU_TTYPE_AVDT_CCB_RSP: |
| 380 | case BTU_TTYPE_AVDT_CCB_IDLE: |
| 381 | case BTU_TTYPE_AVDT_SCB_TC: |
| 382 | avdt_process_timeout(p_tle); |
| 383 | break; |
| 384 | #endif |
| 385 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 386 | #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE) |
| 387 | case BTU_TTYPE_HID_HOST_REPAGE_TO : |
| 388 | hidh_proc_repage_timeout(p_tle); |
| 389 | break; |
| 390 | #endif |
| 391 | |
| 392 | #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE) |
| 393 | case BTU_TTYPE_BLE_INQUIRY: |
| 394 | case BTU_TTYPE_BLE_GAP_LIM_DISC: |
| 395 | case BTU_TTYPE_BLE_RANDOM_ADDR: |
| 396 | case BTU_TTYPE_BLE_GAP_FAST_ADV: |
| 397 | case BTU_TTYPE_BLE_OBSERVE: |
| 398 | btm_ble_timeout(p_tle); |
| 399 | break; |
| 400 | |
| 401 | case BTU_TTYPE_ATT_WAIT_FOR_RSP: |
| 402 | gatt_rsp_timeout(p_tle); |
| 403 | break; |
| 404 | |
| 405 | case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK: |
| 406 | gatt_ind_ack_timeout(p_tle); |
| 407 | break; |
| 408 | #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE) |
| 409 | case BTU_TTYPE_SMP_PAIRING_CMD: |
| 410 | smp_rsp_timeout(p_tle); |
| 411 | break; |
| 412 | #endif |
| 413 | |
| 414 | #endif |
| 415 | |
| 416 | #if (MCA_INCLUDED == TRUE) |
| 417 | case BTU_TTYPE_MCA_CCB_RSP: |
| 418 | mca_process_timeout(p_tle); |
| 419 | break; |
| 420 | #endif |
| 421 | case BTU_TTYPE_USER_FUNC: |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 422 | { |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 423 | tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param; |
| 424 | (*p_uf)(p_tle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 425 | } |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | default:; |
| 429 | int i = 0; |
| 430 | BOOLEAN handled = FALSE; |
| 431 | |
| 432 | for (; !handled && i < BTU_MAX_REG_TIMER; i++) |
| 433 | { |
| 434 | if (btu_cb.timer_reg[i].timer_cb == NULL) |
| 435 | continue; |
| 436 | if (btu_cb.timer_reg[i].p_tle == p_tle) |
| 437 | { |
| 438 | btu_cb.timer_reg[i].timer_cb(p_tle); |
| 439 | handled = TRUE; |
| 440 | } |
| 441 | } |
| 442 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 443 | } |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 444 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 445 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 446 | void btu_general_alarm_cb(void *data) { |
| 447 | assert(data != NULL); |
| 448 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 449 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 450 | fixed_queue_enqueue(btu_general_alarm_queue, p_tle); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 451 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 452 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 453 | void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) { |
| 454 | assert(p_tle != NULL); |
| 455 | |
| 456 | // Get the alarm for the timer list entry. |
| 457 | pthread_mutex_lock(&btu_general_alarm_lock); |
| 458 | if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) { |
| 459 | hash_map_set(btu_general_alarm_hash_map, p_tle, alarm_new()); |
| 460 | } |
| 461 | pthread_mutex_unlock(&btu_general_alarm_lock); |
| 462 | |
| 463 | alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle); |
| 464 | if (alarm == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 465 | LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | alarm_cancel(alarm); |
| 469 | |
| 470 | p_tle->event = type; |
| 471 | // NOTE: This value is in seconds but stored in a ticks field. |
| 472 | p_tle->ticks = timeout_sec; |
Chris Manton | bc3ac06 | 2014-09-17 13:30:13 -0700 | [diff] [blame] | 473 | p_tle->in_use = TRUE; |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 474 | alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_general_alarm_cb, (void *)p_tle); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /******************************************************************************* |
| 478 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 479 | ** Function btu_stop_timer |
| 480 | ** |
| 481 | ** Description Stop a timer. |
| 482 | ** |
| 483 | ** Returns void |
| 484 | ** |
| 485 | *******************************************************************************/ |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 486 | void btu_stop_timer(TIMER_LIST_ENT *p_tle) { |
| 487 | assert(p_tle != NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 488 | |
Chris Manton | bc3ac06 | 2014-09-17 13:30:13 -0700 | [diff] [blame] | 489 | if (p_tle->in_use == FALSE) |
| 490 | return; |
| 491 | p_tle->in_use = FALSE; |
| 492 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 493 | // Get the alarm for the timer list entry. |
| 494 | alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle); |
| 495 | if (alarm == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 496 | LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | alarm_cancel(alarm); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) |
| 503 | /******************************************************************************* |
| 504 | ** |
| 505 | ** Function btu_start_quick_timer |
| 506 | ** |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 507 | ** Description Start a timer for the specified amount of time in ticks. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 508 | ** |
| 509 | ** Returns void |
| 510 | ** |
| 511 | *******************************************************************************/ |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 512 | static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle) { |
| 513 | assert(p_tle != NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 514 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 515 | switch (p_tle->event) { |
| 516 | case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */ |
| 517 | case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */ |
| 518 | l2c_process_timeout (p_tle); |
| 519 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 520 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 521 | default: |
| 522 | break; |
| 523 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 526 | static void btu_l2cap_alarm_cb(void *data) { |
| 527 | assert(data != NULL); |
| 528 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data; |
| 529 | |
| 530 | fixed_queue_enqueue(btu_l2cap_alarm_queue, p_tle); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks) { |
| 534 | assert(p_tle != NULL); |
| 535 | |
| 536 | // Get the alarm for the timer list entry. |
| 537 | pthread_mutex_lock(&btu_l2cap_alarm_lock); |
| 538 | if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) { |
| 539 | hash_map_set(btu_l2cap_alarm_hash_map, p_tle, alarm_new()); |
| 540 | } |
| 541 | pthread_mutex_unlock(&btu_l2cap_alarm_lock); |
| 542 | |
| 543 | alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle); |
| 544 | if (alarm == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 545 | LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | alarm_cancel(alarm); |
| 549 | |
| 550 | p_tle->event = type; |
| 551 | p_tle->ticks = timeout_ticks; |
Chris Manton | bc3ac06 | 2014-09-17 13:30:13 -0700 | [diff] [blame] | 552 | p_tle->in_use = TRUE; |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 553 | // The quick timer ticks are 100ms long. |
| 554 | alarm_set(alarm, (period_ms_t)(timeout_ticks * 100), btu_l2cap_alarm_cb, (void *)p_tle); |
| 555 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 556 | |
| 557 | /******************************************************************************* |
| 558 | ** |
| 559 | ** Function btu_stop_quick_timer |
| 560 | ** |
| 561 | ** Description Stop a timer. |
| 562 | ** |
| 563 | ** Returns void |
| 564 | ** |
| 565 | *******************************************************************************/ |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 566 | void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle) { |
| 567 | assert(p_tle != NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 568 | |
Chris Manton | bc3ac06 | 2014-09-17 13:30:13 -0700 | [diff] [blame] | 569 | if (p_tle->in_use == FALSE) |
| 570 | return; |
| 571 | p_tle->in_use = FALSE; |
| 572 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 573 | // Get the alarm for the timer list entry. |
| 574 | alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle); |
| 575 | if (alarm == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 576 | LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | alarm_cancel(alarm); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 580 | } |
| 581 | #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */ |
| 582 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 583 | void btu_oneshot_alarm_cb(void *data) { |
| 584 | assert(data != NULL); |
| 585 | TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data; |
| 586 | |
| 587 | btu_stop_timer_oneshot(p_tle); |
| 588 | |
| 589 | fixed_queue_enqueue(btu_oneshot_alarm_queue, p_tle); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Chris Manton | 0aee331 | 2014-06-12 12:52:33 -0700 | [diff] [blame] | 592 | /* |
| 593 | * Starts a oneshot timer with a timeout in seconds. |
| 594 | */ |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 595 | void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) { |
| 596 | assert(p_tle != NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 597 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 598 | // Get the alarm for the timer list entry. |
| 599 | pthread_mutex_lock(&btu_oneshot_alarm_lock); |
| 600 | if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) { |
| 601 | hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm_new()); |
| 602 | } |
| 603 | pthread_mutex_unlock(&btu_oneshot_alarm_lock); |
Chris Manton | 0aee331 | 2014-06-12 12:52:33 -0700 | [diff] [blame] | 604 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 605 | alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle); |
| 606 | if (alarm == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 607 | LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | alarm_cancel(alarm); |
Chris Manton | 0aee331 | 2014-06-12 12:52:33 -0700 | [diff] [blame] | 611 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 612 | p_tle->event = type; |
Chris Manton | bc3ac06 | 2014-09-17 13:30:13 -0700 | [diff] [blame] | 613 | p_tle->in_use = TRUE; |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 614 | // NOTE: This value is in seconds but stored in a ticks field. |
| 615 | p_tle->ticks = timeout_sec; |
| 616 | alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_oneshot_alarm_cb, (void *)p_tle); |
Chris Manton | 0aee331 | 2014-06-12 12:52:33 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) { |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 620 | assert(p_tle != NULL); |
Chris Manton | 0aee331 | 2014-06-12 12:52:33 -0700 | [diff] [blame] | 621 | |
Chris Manton | bc3ac06 | 2014-09-17 13:30:13 -0700 | [diff] [blame] | 622 | if (p_tle->in_use == FALSE) |
| 623 | return; |
| 624 | p_tle->in_use = FALSE; |
| 625 | |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 626 | // Get the alarm for the timer list entry. |
| 627 | alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle); |
| 628 | if (alarm == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 629 | LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__); |
Chris Manton | 1802329 | 2014-08-29 09:12:06 -0700 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | alarm_cancel(alarm); |
Chris Manton | 0aee331 | 2014-06-12 12:52:33 -0700 | [diff] [blame] | 633 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 634 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 635 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 636 | /******************************************************************************* |
| 637 | ** |
| 638 | ** Function btu_check_bt_sleep |
| 639 | ** |
| 640 | ** Description This function is called to check if controller can go to sleep. |
| 641 | ** |
| 642 | ** Returns void |
| 643 | ** |
| 644 | *******************************************************************************/ |
| 645 | void btu_check_bt_sleep (void) |
| 646 | { |
Zach Johnson | 91ba12d | 2014-09-26 14:03:13 -0700 | [diff] [blame] | 647 | // TODO(zachoverflow) take pending commands into account? |
| 648 | if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 649 | { |
Zach Johnson | 91ba12d | 2014-09-26 14:03:13 -0700 | [diff] [blame] | 650 | bte_main_lpm_allow_bt_device_sleep(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | #endif |