blob: 982f5dfe7dc3ef58dec7e987c624385aaa8d4fe7 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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 Mantonf8027002015-03-12 09:22:48 -070019#define LOG_TAG "bt_btu_task"
The Android Open Source Project5738f832012-12-12 16:00:35 -080020
Chris Manton18023292014-08-29 09:12:06 -070021#include <assert.h>
Arman Uguray6850e332015-05-29 15:07:51 -070022#include <pthread.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080023#include <stdlib.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080024#include <stdio.h>
Chris Manton307381e2014-09-04 19:48:49 -070025#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080026
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070027#include "osi/include/alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include "bt_target.h"
Chris Manton860a9af2014-08-27 10:30:47 -070029#include "bt_trace.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include "bt_types.h"
Chris Manton860a9af2014-08-27 10:30:47 -070031#include "bt_utils.h"
Chris Manton0eefef02014-09-08 15:01:39 -070032#include "btif_common.h"
Chris Manton860a9af2014-08-27 10:30:47 -070033#include "btm_api.h"
34#include "btm_int.h"
35#include "btu.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070036#include "osi/include/fixed_queue.h"
37#include "osi/include/future.h"
Chris Manton860a9af2014-08-27 10:30:47 -070038#include "gki.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070039#include "osi/include/hash_map.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080040#include "hcimsgs.h"
41#include "l2c_int.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070042#include "btcore/include/module.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070043#include "osi/include/osi.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080044#include "osi/include/log.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080045#include "sdpint.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070046#include "osi/include/thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080047
The Android Open Source Project5738f832012-12-12 16:00:35 -080048#include "port_api.h"
49#include "port_ext.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080050
The Android Open Source Project5738f832012-12-12 16:00:35 -080051#include "gap_int.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080052
The Android Open Source Project5738f832012-12-12 16:00:35 -080053#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 Project5738f832012-12-12 16:00:35 -080061#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
68extern 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 Project5738f832012-12-12 16:00:35 -080077#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080078
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-Gans24933b52014-09-24 15:25:02 -070087extern void BTE_InitStack(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -080088
The Android Open Source Project5738f832012-12-12 16:00:35 -080089/* Define BTU storage area
90*/
91#if BTU_DYNAMIC_MEMORY == FALSE
92tBTU_CB btu_cb;
93#endif
94
Chris Manton860a9af2014-08-27 10:30:47 -070095// Communication queue between btu_task and bta.
96extern fixed_queue_t *btu_bta_msg_queue;
97
98// Communication queue between btu_task and hci.
99extern fixed_queue_t *btu_hci_msg_queue;
100
Chris Manton18023292014-08-29 09:12:06 -0700101// General timer queue.
102extern fixed_queue_t *btu_general_alarm_queue;
103extern hash_map_t *btu_general_alarm_hash_map;
104extern pthread_mutex_t btu_general_alarm_lock;
105
106// Oneshot timer queue.
107extern fixed_queue_t *btu_oneshot_alarm_queue;
108extern hash_map_t *btu_oneshot_alarm_hash_map;
109extern pthread_mutex_t btu_oneshot_alarm_lock;
110
111// l2cap timer queue.
112extern fixed_queue_t *btu_l2cap_alarm_queue;
113extern hash_map_t *btu_l2cap_alarm_hash_map;
114extern pthread_mutex_t btu_l2cap_alarm_lock;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115
Chris Manton307381e2014-09-04 19:48:49 -0700116extern fixed_queue_t *event_queue;
117extern fixed_queue_t *btif_msg_queue;
118
119extern thread_t *bt_workqueue_thread;
120
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121/* Define a function prototype to allow a generic timeout handler */
122typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
123
Chris Manton18023292014-08-29 09:12:06 -0700124static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
125static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
Chris Manton307381e2014-09-04 19:48:49 -0700126static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle);
127static void btu_hci_msg_process(BT_HDR *p_msg);
128
129void 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
134void 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
139void 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
165void 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
170void 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
175void 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 Manton18023292014-08-29 09:12:06 -0700179
Chris Manton860a9af2014-08-27 10:30:47 -0700180static 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 Johnson218f3752014-09-03 14:36:44 -0700184 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 Manton860a9af2014-08-27 10:30:47 -0700187 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 Manton860a9af2014-08-27 10:30:47 -0700218 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 Manton18023292014-08-29 09:12:06 -0700246static 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 Manton860a9af2014-08-27 10:30:47 -0700256 }
257 }
Chris Manton860a9af2014-08-27 10:30:47 -0700258}
259
Chris Manton307381e2014-09-04 19:48:49 -0700260void 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 Project5738f832012-12-12 16:00:35 -0800263
Marie Janssendb554582015-06-26 14:53:46 -0700264 LOG_INFO(LOG_TAG, "Bluetooth chip preload is complete");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700265
Chris Manton307381e2014-09-04 19:48:49 -0700266 BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
267 "btu_task received preload complete event");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700268
Chris Manton307381e2014-09-04 19:48:49 -0700269 /* Initialize the mandatory core stack control blocks
270 (BTU, BTM, L2CAP, and SDP)
271 */
272 btu_init_core();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700273
Chris Manton307381e2014-09-04 19:48:49 -0700274 /* Initialize any optional stack components */
275 BTE_InitStack();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276
Chris Manton307381e2014-09-04 19:48:49 -0700277 bta_sys_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800278
Chris Manton307381e2014-09-04 19:48:49 -0700279 /* 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 Project5738f832012-12-12 16:00:35 -0800282#if ( BT_USE_TRACES==TRUE )
Zach Johnson9891f322014-09-22 22:11:55 -0700283 module_init(get_module(BTE_LOGMSG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284#endif
285
Chris Manton307381e2014-09-04 19:48:49 -0700286 // Inform the bt jni thread initialization is ok.
287 btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800288
Chris Manton307381e2014-09-04 19:48:49 -0700289 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 Project5738f832012-12-12 16:00:35 -0800293
Chris Manton307381e2014-09-04 19:48:49 -0700294 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 Project5738f832012-12-12 16:00:35 -0800298
Chris Manton307381e2014-09-04 19:48:49 -0700299 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 Project5738f832012-12-12 16:00:35 -0800303
Chris Manton307381e2014-09-04 19:48:49 -0700304 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 Project5738f832012-12-12 16:00:35 -0800308
Chris Manton307381e2014-09-04 19:48:49 -0700309 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 Project5738f832012-12-12 16:00:35 -0800314
Chris Manton307381e2014-09-04 19:48:49 -0700315void btu_task_shut_down(UNUSED_ATTR void *context) {
Zach Johnsonc8ac8a22014-09-26 17:04:51 -0700316 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 Mantonb1f99152014-09-26 21:31:38 -0700322#if ( BT_USE_TRACES==TRUE )
323 module_clean_up(get_module(BTE_LOGMSG_MODULE));
324#endif
325
Chris Manton307381e2014-09-04 19:48:49 -0700326 bta_sys_free();
Chris Manton307381e2014-09-04 19:48:49 -0700327 btu_free_core();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800328}
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 Manton18023292014-08-29 09:12:06 -0700341static 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 Manton18023292014-08-29 09:12:06 -0700349 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 Manton18023292014-08-29 09:12:06 -0700365 case BTU_TTYPE_RFCOMM_MFC:
366 case BTU_TTYPE_RFCOMM_PORT:
367 rfcomm_process_timeout (p_tle);
368 break;
369
Chris Manton18023292014-08-29 09:12:06 -0700370#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 Manton18023292014-08-29 09:12:06 -0700386#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 Project5738f832012-12-12 16:00:35 -0800422 {
Chris Manton18023292014-08-29 09:12:06 -0700423 tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
424 (*p_uf)(p_tle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800425 }
Chris Manton18023292014-08-29 09:12:06 -0700426 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 Project5738f832012-12-12 16:00:35 -0800443 }
Chris Manton18023292014-08-29 09:12:06 -0700444}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800445
Chris Manton18023292014-08-29 09:12:06 -0700446void btu_general_alarm_cb(void *data) {
447 assert(data != NULL);
448 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449
Chris Manton18023292014-08-29 09:12:06 -0700450 fixed_queue_enqueue(btu_general_alarm_queue, p_tle);
Chris Manton18023292014-08-29 09:12:06 -0700451}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800452
Chris Manton18023292014-08-29 09:12:06 -0700453void 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 Janssendb554582015-06-26 14:53:46 -0700465 LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700466 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 Mantonbc3ac062014-09-17 13:30:13 -0700473 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700474 alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_general_alarm_cb, (void *)p_tle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800475}
476
477/*******************************************************************************
478**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800479** Function btu_stop_timer
480**
481** Description Stop a timer.
482**
483** Returns void
484**
485*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700486void btu_stop_timer(TIMER_LIST_ENT *p_tle) {
487 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488
Chris Mantonbc3ac062014-09-17 13:30:13 -0700489 if (p_tle->in_use == FALSE)
490 return;
491 p_tle->in_use = FALSE;
492
Chris Manton18023292014-08-29 09:12:06 -0700493 // 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 Janssendb554582015-06-26 14:53:46 -0700496 LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700497 return;
498 }
499 alarm_cancel(alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500}
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 Manton18023292014-08-29 09:12:06 -0700507** Description Start a timer for the specified amount of time in ticks.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800508**
509** Returns void
510**
511*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700512static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle) {
513 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800514
Chris Manton18023292014-08-29 09:12:06 -0700515 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 Project5738f832012-12-12 16:00:35 -0800520
Chris Manton18023292014-08-29 09:12:06 -0700521 default:
522 break;
523 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800524}
525
Chris Manton18023292014-08-29 09:12:06 -0700526static 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 Manton18023292014-08-29 09:12:06 -0700531}
532
533void 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 Janssendb554582015-06-26 14:53:46 -0700545 LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700546 return;
547 }
548 alarm_cancel(alarm);
549
550 p_tle->event = type;
551 p_tle->ticks = timeout_ticks;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700552 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700553 // 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 Project5738f832012-12-12 16:00:35 -0800556
557/*******************************************************************************
558**
559** Function btu_stop_quick_timer
560**
561** Description Stop a timer.
562**
563** Returns void
564**
565*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700566void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle) {
567 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568
Chris Mantonbc3ac062014-09-17 13:30:13 -0700569 if (p_tle->in_use == FALSE)
570 return;
571 p_tle->in_use = FALSE;
572
Chris Manton18023292014-08-29 09:12:06 -0700573 // 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 Janssendb554582015-06-26 14:53:46 -0700576 LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700577 return;
578 }
579 alarm_cancel(alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800580}
581#endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
582
Chris Manton18023292014-08-29 09:12:06 -0700583void 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 Manton18023292014-08-29 09:12:06 -0700590}
591
Chris Manton0aee3312014-06-12 12:52:33 -0700592/*
593 * Starts a oneshot timer with a timeout in seconds.
594 */
Chris Manton18023292014-08-29 09:12:06 -0700595void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
596 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800597
Chris Manton18023292014-08-29 09:12:06 -0700598 // 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 Manton0aee3312014-06-12 12:52:33 -0700604
Chris Manton18023292014-08-29 09:12:06 -0700605 alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
606 if (alarm == NULL) {
Marie Janssendb554582015-06-26 14:53:46 -0700607 LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700608 return;
609 }
610 alarm_cancel(alarm);
Chris Manton0aee3312014-06-12 12:52:33 -0700611
Chris Manton18023292014-08-29 09:12:06 -0700612 p_tle->event = type;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700613 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700614 // 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 Manton0aee3312014-06-12 12:52:33 -0700617}
618
619void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) {
Chris Manton18023292014-08-29 09:12:06 -0700620 assert(p_tle != NULL);
Chris Manton0aee3312014-06-12 12:52:33 -0700621
Chris Mantonbc3ac062014-09-17 13:30:13 -0700622 if (p_tle->in_use == FALSE)
623 return;
624 p_tle->in_use = FALSE;
625
Chris Manton18023292014-08-29 09:12:06 -0700626 // 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 Janssendb554582015-06-26 14:53:46 -0700629 LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700630 return;
631 }
632 alarm_cancel(alarm);
Chris Manton0aee3312014-06-12 12:52:33 -0700633}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800634
The Android Open Source Project5738f832012-12-12 16:00:35 -0800635#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*******************************************************************************/
645void btu_check_bt_sleep (void)
646{
Zach Johnson91ba12d2014-09-26 14:03:13 -0700647 // TODO(zachoverflow) take pending commands into account?
648 if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800649 {
Zach Johnson91ba12d2014-09-26 14:03:13 -0700650 bte_main_lpm_allow_bt_device_sleep();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651 }
652}
653#endif