blob: 04ec8f070aeefec7def9936355191e03e03e486d [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 Manton307381e2014-09-04 19:48:49 -070019#define LOG_TAG "btu_task"
The Android Open Source Project5738f832012-12-12 16:00:35 -080020
Chris Manton18023292014-08-29 09:12:06 -070021#include <assert.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080022#include <stdlib.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080023#include <stdio.h>
Chris Manton307381e2014-09-04 19:48:49 -070024#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080025
Chris Manton18023292014-08-29 09:12:06 -070026#include "alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include "bt_target.h"
Chris Manton860a9af2014-08-27 10:30:47 -070028#include "bt_trace.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include "bt_types.h"
Chris Manton860a9af2014-08-27 10:30:47 -070030#include "bt_utils.h"
Chris Manton0eefef02014-09-08 15:01:39 -070031#include "btif_common.h"
Chris Manton860a9af2014-08-27 10:30:47 -070032#include "btm_api.h"
33#include "btm_int.h"
34#include "btu.h"
35#include "fixed_queue.h"
Chris Manton307381e2014-09-04 19:48:49 -070036#include "future.h"
Chris Manton860a9af2014-08-27 10:30:47 -070037#include "gki.h"
Chris Manton18023292014-08-29 09:12:06 -070038#include "hash_map.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080039#include "hcimsgs.h"
40#include "l2c_int.h"
Zach Johnson9891f322014-09-22 22:11:55 -070041#include "module.h"
Chris Manton307381e2014-09-04 19:48:49 -070042#include "osi.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080043#include "osi/include/log.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080044#include "sdpint.h"
Chris Manton307381e2014-09-04 19:48:49 -070045#include "thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080046
47#if ( defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE )
48#include "port_api.h"
49#include "port_ext.h"
50#endif
51
The Android Open Source Project5738f832012-12-12 16:00:35 -080052#if (defined(EVAL) && EVAL == TRUE)
53#include "btu_eval.h"
54#endif
55
56#if GAP_INCLUDED == TRUE
57#include "gap_int.h"
58#endif
59
The Android Open Source Project5738f832012-12-12 16:00:35 -080060/* BTE application task */
61#if APPL_INCLUDED == TRUE
62#include "bte_appl.h"
63#endif
64
The Android Open Source Project5738f832012-12-12 16:00:35 -080065#if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
66#include "bnep_int.h"
67#endif
68
69#if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
70#include "pan_int.h"
71#endif
72
The Android Open Source Project5738f832012-12-12 16:00:35 -080073#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE )
74#include "hidh_int.h"
75#endif
76
77#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
78#include "avdt_int.h"
79#else
80extern void avdt_rcv_sync_info (BT_HDR *p_buf); /* this is for hci_test */
81#endif
82
83#if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
84#include "mca_api.h"
85#include "mca_defs.h"
86#include "mca_int.h"
87#endif
88
The Android Open Source Project5738f832012-12-12 16:00:35 -080089#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080090
91#if (BLE_INCLUDED == TRUE)
92#include "gatt_int.h"
93#if (SMP_INCLUDED == TRUE)
94#include "smp_int.h"
95#endif
96#include "btm_ble_int.h"
97#endif
98
99#ifdef __cplusplus
100extern "C"
101{
102#endif
103
June R. Tate-Gans24933b52014-09-24 15:25:02 -0700104extern void BTE_InitStack(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105
106#ifdef __cplusplus
107}
108#endif
109
110/* Define BTU storage area
111*/
112#if BTU_DYNAMIC_MEMORY == FALSE
113tBTU_CB btu_cb;
114#endif
115
Chris Manton860a9af2014-08-27 10:30:47 -0700116// Communication queue between btu_task and bta.
117extern fixed_queue_t *btu_bta_msg_queue;
118
119// Communication queue between btu_task and hci.
120extern fixed_queue_t *btu_hci_msg_queue;
121
Chris Manton18023292014-08-29 09:12:06 -0700122// Timer queue between btu_task and bta.
123extern fixed_queue_t *btu_bta_alarm_queue;
124
125// General timer queue.
126extern fixed_queue_t *btu_general_alarm_queue;
127extern hash_map_t *btu_general_alarm_hash_map;
128extern pthread_mutex_t btu_general_alarm_lock;
129
130// Oneshot timer queue.
131extern fixed_queue_t *btu_oneshot_alarm_queue;
132extern hash_map_t *btu_oneshot_alarm_hash_map;
133extern pthread_mutex_t btu_oneshot_alarm_lock;
134
135// l2cap timer queue.
136extern fixed_queue_t *btu_l2cap_alarm_queue;
137extern hash_map_t *btu_l2cap_alarm_hash_map;
138extern pthread_mutex_t btu_l2cap_alarm_lock;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139
Chris Manton307381e2014-09-04 19:48:49 -0700140extern fixed_queue_t *event_queue;
141extern fixed_queue_t *btif_msg_queue;
142
143extern thread_t *bt_workqueue_thread;
144
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145/* Define a function prototype to allow a generic timeout handler */
146typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
147
Chris Manton18023292014-08-29 09:12:06 -0700148static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
149static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
Chris Manton307381e2014-09-04 19:48:49 -0700150static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle);
151static void btu_hci_msg_process(BT_HDR *p_msg);
152
153void btu_hci_msg_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
154 BT_HDR *p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
155 btu_hci_msg_process(p_msg);
156}
157
158void btu_general_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
159 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
160 btu_general_alarm_process(p_tle);
161}
162
163void btu_oneshot_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_general_alarm_process(p_tle);
166
167 switch (p_tle->event) {
168#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
169 case BTU_TTYPE_BLE_RANDOM_ADDR:
170 btm_ble_timeout(p_tle);
171 break;
172#endif
173
174 case BTU_TTYPE_USER_FUNC:
175 {
176 tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
177 (*p_uf)(p_tle);
178 }
179 break;
180
181 default:
182 // FAIL
183 BTM_TRACE_WARNING("Received unexpected oneshot timer event:0x%x\n",
184 p_tle->event);
185 break;
186 }
187}
188
189void btu_l2cap_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
190 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
191 btu_l2cap_alarm_process(p_tle);
192}
193
194void btu_bta_msg_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
195 BT_HDR *p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
196 bta_sys_event(p_msg);
197}
198
199void btu_bta_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
200 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
201 btu_bta_alarm_process(p_tle);
202}
Chris Manton18023292014-08-29 09:12:06 -0700203
Chris Manton860a9af2014-08-27 10:30:47 -0700204static void btu_hci_msg_process(BT_HDR *p_msg) {
205 /* Determine the input message type. */
206 switch (p_msg->event & BT_EVT_MASK)
207 {
Zach Johnson218f3752014-09-03 14:36:44 -0700208 case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this
209 ((post_to_task_hack_t *)(&p_msg->data[0]))->callback(p_msg);
210 break;
Chris Manton860a9af2014-08-27 10:30:47 -0700211 case BT_EVT_TO_BTU_HCI_ACL:
212 /* All Acl Data goes to L2CAP */
213 l2c_rcv_acl_data (p_msg);
214 break;
215
216 case BT_EVT_TO_BTU_L2C_SEG_XMIT:
217 /* L2CAP segment transmit complete */
218 l2c_link_segments_xmitted (p_msg);
219 break;
220
221 case BT_EVT_TO_BTU_HCI_SCO:
222#if BTM_SCO_INCLUDED == TRUE
223 btm_route_sco_data (p_msg);
224 break;
225#endif
226
227 case BT_EVT_TO_BTU_HCI_EVT:
228 btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
229 GKI_freebuf(p_msg);
230
231#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
232 /* If host receives events which it doesn't response to, */
233 /* host should start idle timer to enter sleep mode. */
234 btu_check_bt_sleep ();
235#endif
236 break;
237
238 case BT_EVT_TO_BTU_HCI_CMD:
239 btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
240 break;
241
Chris Manton860a9af2014-08-27 10:30:47 -0700242 default:;
243 int i = 0;
244 uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
245 BOOLEAN handled = FALSE;
246
247 for (; !handled && i < BTU_MAX_REG_EVENT; i++)
248 {
249 if (btu_cb.event_reg[i].event_cb == NULL)
250 continue;
251
252 if (mask == btu_cb.event_reg[i].event_range)
253 {
254 if (btu_cb.event_reg[i].event_cb)
255 {
256 btu_cb.event_reg[i].event_cb(p_msg);
257 handled = TRUE;
258 }
259 }
260 }
261
262 if (handled == FALSE)
263 GKI_freebuf (p_msg);
264
265 break;
266 }
267
268}
269
Chris Manton18023292014-08-29 09:12:06 -0700270static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle) {
271 /* call timer callback */
272 if (p_tle->p_cback) {
273 (*p_tle->p_cback)(p_tle);
274 } else if (p_tle->event) {
275 BT_HDR *p_msg;
276 if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) {
277 p_msg->event = p_tle->event;
278 p_msg->layer_specific = 0;
279 bta_sys_sendmsg(p_msg);
Chris Manton860a9af2014-08-27 10:30:47 -0700280 }
281 }
Chris Manton860a9af2014-08-27 10:30:47 -0700282}
283
Chris Manton307381e2014-09-04 19:48:49 -0700284void btu_task_start_up(UNUSED_ATTR void *context) {
285 BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
286 "btu_task pending for preload complete event");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800287
Sharvil Nanavati44802762014-12-23 23:08:58 -0800288 LOG_INFO("Bluetooth chip preload is complete");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700289
Chris Manton307381e2014-09-04 19:48:49 -0700290 BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
291 "btu_task received preload complete event");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700292
Chris Manton307381e2014-09-04 19:48:49 -0700293 /* Initialize the mandatory core stack control blocks
294 (BTU, BTM, L2CAP, and SDP)
295 */
296 btu_init_core();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700297
Chris Manton307381e2014-09-04 19:48:49 -0700298 /* Initialize any optional stack components */
299 BTE_InitStack();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800300
Chris Manton307381e2014-09-04 19:48:49 -0700301 bta_sys_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800302
Chris Manton307381e2014-09-04 19:48:49 -0700303 /* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init()
304 * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
305 */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800306#if ( BT_USE_TRACES==TRUE )
Zach Johnson9891f322014-09-22 22:11:55 -0700307 module_init(get_module(BTE_LOGMSG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308#endif
309
Chris Manton307381e2014-09-04 19:48:49 -0700310 // Inform the bt jni thread initialization is ok.
311 btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800312
Chris Manton307381e2014-09-04 19:48:49 -0700313 fixed_queue_register_dequeue(btu_bta_msg_queue,
314 thread_get_reactor(bt_workqueue_thread),
315 btu_bta_msg_ready,
316 NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800317
Chris Manton307381e2014-09-04 19:48:49 -0700318 fixed_queue_register_dequeue(btu_hci_msg_queue,
319 thread_get_reactor(bt_workqueue_thread),
320 btu_hci_msg_ready,
321 NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322
Chris Manton307381e2014-09-04 19:48:49 -0700323 fixed_queue_register_dequeue(btu_general_alarm_queue,
324 thread_get_reactor(bt_workqueue_thread),
325 btu_general_alarm_ready,
326 NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800327
Chris Manton307381e2014-09-04 19:48:49 -0700328 fixed_queue_register_dequeue(btu_oneshot_alarm_queue,
329 thread_get_reactor(bt_workqueue_thread),
330 btu_oneshot_alarm_ready,
331 NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800332
Chris Manton307381e2014-09-04 19:48:49 -0700333 fixed_queue_register_dequeue(btu_l2cap_alarm_queue,
334 thread_get_reactor(bt_workqueue_thread),
335 btu_l2cap_alarm_ready,
336 NULL);
337}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800338
Chris Manton307381e2014-09-04 19:48:49 -0700339void btu_task_shut_down(UNUSED_ATTR void *context) {
Zach Johnsonc8ac8a22014-09-26 17:04:51 -0700340 fixed_queue_unregister_dequeue(btu_bta_msg_queue);
341 fixed_queue_unregister_dequeue(btu_hci_msg_queue);
342 fixed_queue_unregister_dequeue(btu_general_alarm_queue);
343 fixed_queue_unregister_dequeue(btu_oneshot_alarm_queue);
344 fixed_queue_unregister_dequeue(btu_l2cap_alarm_queue);
345
Chris Mantonb1f99152014-09-26 21:31:38 -0700346#if ( BT_USE_TRACES==TRUE )
347 module_clean_up(get_module(BTE_LOGMSG_MODULE));
348#endif
349
Chris Manton307381e2014-09-04 19:48:49 -0700350 bta_sys_free();
Chris Manton307381e2014-09-04 19:48:49 -0700351 btu_free_core();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352}
353
354/*******************************************************************************
355**
356** Function btu_start_timer
357**
358** Description Start a timer for the specified amount of time.
359** NOTE: The timeout resolution is in SECONDS! (Even
360** though the timer structure field is ticks)
361**
362** Returns void
363**
364*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700365static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle) {
366 assert(p_tle != NULL);
367
368 switch (p_tle->event) {
369 case BTU_TTYPE_BTM_DEV_CTL:
370 btm_dev_timeout(p_tle);
371 break;
372
373 case BTU_TTYPE_BTM_ACL:
374 btm_acl_timeout(p_tle);
375 break;
376
377 case BTU_TTYPE_L2CAP_LINK:
378 case BTU_TTYPE_L2CAP_CHNL:
379 case BTU_TTYPE_L2CAP_HOLD:
380 case BTU_TTYPE_L2CAP_INFO:
381 case BTU_TTYPE_L2CAP_FCR_ACK:
382 l2c_process_timeout (p_tle);
383 break;
384
385 case BTU_TTYPE_SDP:
386 sdp_conn_timeout ((tCONN_CB *)p_tle->param);
387 break;
388
389 case BTU_TTYPE_BTM_RMT_NAME:
390 btm_inq_rmt_name_failed();
391 break;
392
393#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
394 case BTU_TTYPE_RFCOMM_MFC:
395 case BTU_TTYPE_RFCOMM_PORT:
396 rfcomm_process_timeout (p_tle);
397 break;
398
399#endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */
400
401#if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
402 case BTU_TTYPE_BNEP:
403 bnep_process_timeout(p_tle);
404 break;
405#endif
406
407
408#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
409 case BTU_TTYPE_AVDT_CCB_RET:
410 case BTU_TTYPE_AVDT_CCB_RSP:
411 case BTU_TTYPE_AVDT_CCB_IDLE:
412 case BTU_TTYPE_AVDT_SCB_TC:
413 avdt_process_timeout(p_tle);
414 break;
415#endif
416
Chris Manton18023292014-08-29 09:12:06 -0700417#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
418 case BTU_TTYPE_HID_HOST_REPAGE_TO :
419 hidh_proc_repage_timeout(p_tle);
420 break;
421#endif
422
423#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
424 case BTU_TTYPE_BLE_INQUIRY:
425 case BTU_TTYPE_BLE_GAP_LIM_DISC:
426 case BTU_TTYPE_BLE_RANDOM_ADDR:
427 case BTU_TTYPE_BLE_GAP_FAST_ADV:
428 case BTU_TTYPE_BLE_OBSERVE:
429 btm_ble_timeout(p_tle);
430 break;
431
432 case BTU_TTYPE_ATT_WAIT_FOR_RSP:
433 gatt_rsp_timeout(p_tle);
434 break;
435
436 case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
437 gatt_ind_ack_timeout(p_tle);
438 break;
439#if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
440 case BTU_TTYPE_SMP_PAIRING_CMD:
441 smp_rsp_timeout(p_tle);
442 break;
443#endif
444
445#endif
446
447#if (MCA_INCLUDED == TRUE)
448 case BTU_TTYPE_MCA_CCB_RSP:
449 mca_process_timeout(p_tle);
450 break;
451#endif
452 case BTU_TTYPE_USER_FUNC:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800453 {
Chris Manton18023292014-08-29 09:12:06 -0700454 tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
455 (*p_uf)(p_tle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800456 }
Chris Manton18023292014-08-29 09:12:06 -0700457 break;
458
459 default:;
460 int i = 0;
461 BOOLEAN handled = FALSE;
462
463 for (; !handled && i < BTU_MAX_REG_TIMER; i++)
464 {
465 if (btu_cb.timer_reg[i].timer_cb == NULL)
466 continue;
467 if (btu_cb.timer_reg[i].p_tle == p_tle)
468 {
469 btu_cb.timer_reg[i].timer_cb(p_tle);
470 handled = TRUE;
471 }
472 }
473 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800474 }
Chris Manton18023292014-08-29 09:12:06 -0700475}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800476
Chris Manton18023292014-08-29 09:12:06 -0700477void btu_general_alarm_cb(void *data) {
478 assert(data != NULL);
479 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480
Chris Manton18023292014-08-29 09:12:06 -0700481 fixed_queue_enqueue(btu_general_alarm_queue, p_tle);
482 GKI_send_event(BTU_TASK, TIMER_0_EVT_MASK);
483}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800484
Chris Manton18023292014-08-29 09:12:06 -0700485void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
486 assert(p_tle != NULL);
487
488 // Get the alarm for the timer list entry.
489 pthread_mutex_lock(&btu_general_alarm_lock);
490 if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
491 hash_map_set(btu_general_alarm_hash_map, p_tle, alarm_new());
492 }
493 pthread_mutex_unlock(&btu_general_alarm_lock);
494
495 alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
496 if (alarm == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800497 LOG_ERROR("%s Unable to create alarm\n", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700498 return;
499 }
500 alarm_cancel(alarm);
501
502 p_tle->event = type;
503 // NOTE: This value is in seconds but stored in a ticks field.
504 p_tle->ticks = timeout_sec;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700505 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700506 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 -0800507}
508
509/*******************************************************************************
510**
511** Function btu_remaining_time
512**
513** Description Return amount of time to expire
514**
515** Returns time in second
516**
517*******************************************************************************/
518UINT32 btu_remaining_time (TIMER_LIST_ENT *p_tle)
519{
520 return(GKI_get_remaining_ticks (&btu_cb.timer_queue, p_tle));
521}
522
523/*******************************************************************************
524**
525** Function btu_stop_timer
526**
527** Description Stop a timer.
528**
529** Returns void
530**
531*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700532void btu_stop_timer(TIMER_LIST_ENT *p_tle) {
533 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800534
Chris Mantonbc3ac062014-09-17 13:30:13 -0700535 if (p_tle->in_use == FALSE)
536 return;
537 p_tle->in_use = FALSE;
538
Chris Manton18023292014-08-29 09:12:06 -0700539 // Get the alarm for the timer list entry.
540 alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
541 if (alarm == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800542 LOG_WARN("%s Unable to find expected alarm in hashmap\n", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700543 return;
544 }
545 alarm_cancel(alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546}
547
548#if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
549/*******************************************************************************
550**
551** Function btu_start_quick_timer
552**
Chris Manton18023292014-08-29 09:12:06 -0700553** Description Start a timer for the specified amount of time in ticks.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554**
555** Returns void
556**
557*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700558static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle) {
559 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560
Chris Manton18023292014-08-29 09:12:06 -0700561 switch (p_tle->event) {
562 case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */
563 case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */
564 l2c_process_timeout (p_tle);
565 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800566
Chris Manton18023292014-08-29 09:12:06 -0700567 default:
568 break;
569 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570}
571
Chris Manton18023292014-08-29 09:12:06 -0700572static void btu_l2cap_alarm_cb(void *data) {
573 assert(data != NULL);
574 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
575
576 fixed_queue_enqueue(btu_l2cap_alarm_queue, p_tle);
577 GKI_send_event(BTU_TASK, TIMER_2_EVT_MASK);
578}
579
580void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks) {
581 assert(p_tle != NULL);
582
583 // Get the alarm for the timer list entry.
584 pthread_mutex_lock(&btu_l2cap_alarm_lock);
585 if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
586 hash_map_set(btu_l2cap_alarm_hash_map, p_tle, alarm_new());
587 }
588 pthread_mutex_unlock(&btu_l2cap_alarm_lock);
589
590 alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
591 if (alarm == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800592 LOG_ERROR("%s Unable to create alarm\n", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700593 return;
594 }
595 alarm_cancel(alarm);
596
597 p_tle->event = type;
598 p_tle->ticks = timeout_ticks;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700599 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700600 // The quick timer ticks are 100ms long.
601 alarm_set(alarm, (period_ms_t)(timeout_ticks * 100), btu_l2cap_alarm_cb, (void *)p_tle);
602}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800603
604/*******************************************************************************
605**
606** Function btu_stop_quick_timer
607**
608** Description Stop a timer.
609**
610** Returns void
611**
612*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700613void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle) {
614 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615
Chris Mantonbc3ac062014-09-17 13:30:13 -0700616 if (p_tle->in_use == FALSE)
617 return;
618 p_tle->in_use = FALSE;
619
Chris Manton18023292014-08-29 09:12:06 -0700620 // Get the alarm for the timer list entry.
621 alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
622 if (alarm == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800623 LOG_WARN("%s Unable to find expected alarm in hashmap\n", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700624 return;
625 }
626 alarm_cancel(alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800627}
628#endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
629
Chris Manton18023292014-08-29 09:12:06 -0700630void btu_oneshot_alarm_cb(void *data) {
631 assert(data != NULL);
632 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
633
634 btu_stop_timer_oneshot(p_tle);
635
636 fixed_queue_enqueue(btu_oneshot_alarm_queue, p_tle);
637 GKI_send_event(BTU_TASK, TIMER_3_EVT_MASK);
638}
639
Chris Manton0aee3312014-06-12 12:52:33 -0700640/*
641 * Starts a oneshot timer with a timeout in seconds.
642 */
Chris Manton18023292014-08-29 09:12:06 -0700643void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
644 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645
Chris Manton18023292014-08-29 09:12:06 -0700646 // Get the alarm for the timer list entry.
647 pthread_mutex_lock(&btu_oneshot_alarm_lock);
648 if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
649 hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm_new());
650 }
651 pthread_mutex_unlock(&btu_oneshot_alarm_lock);
Chris Manton0aee3312014-06-12 12:52:33 -0700652
Chris Manton18023292014-08-29 09:12:06 -0700653 alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
654 if (alarm == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800655 LOG_ERROR("%s Unable to create alarm\n", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700656 return;
657 }
658 alarm_cancel(alarm);
Chris Manton0aee3312014-06-12 12:52:33 -0700659
Chris Manton18023292014-08-29 09:12:06 -0700660 p_tle->event = type;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700661 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700662 // NOTE: This value is in seconds but stored in a ticks field.
663 p_tle->ticks = timeout_sec;
664 alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_oneshot_alarm_cb, (void *)p_tle);
Chris Manton0aee3312014-06-12 12:52:33 -0700665}
666
667void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) {
Chris Manton18023292014-08-29 09:12:06 -0700668 assert(p_tle != NULL);
Chris Manton0aee3312014-06-12 12:52:33 -0700669
Chris Mantonbc3ac062014-09-17 13:30:13 -0700670 if (p_tle->in_use == FALSE)
671 return;
672 p_tle->in_use = FALSE;
673
Chris Manton18023292014-08-29 09:12:06 -0700674 // Get the alarm for the timer list entry.
675 alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
676 if (alarm == NULL) {
Sharvil Nanavati44802762014-12-23 23:08:58 -0800677 LOG_WARN("%s Unable to find expected alarm in hashmap\n", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700678 return;
679 }
680 alarm_cancel(alarm);
Chris Manton0aee3312014-06-12 12:52:33 -0700681}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800682
The Android Open Source Project5738f832012-12-12 16:00:35 -0800683#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
684/*******************************************************************************
685**
686** Function btu_check_bt_sleep
687**
688** Description This function is called to check if controller can go to sleep.
689**
690** Returns void
691**
692*******************************************************************************/
693void btu_check_bt_sleep (void)
694{
Zach Johnson91ba12d2014-09-26 14:03:13 -0700695 // TODO(zachoverflow) take pending commands into account?
696 if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800697 {
Zach Johnson91ba12d2014-09-26 14:03:13 -0700698 bte_main_lpm_allow_bt_device_sleep();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800699 }
700}
701#endif