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