blob: a7861c71bcb113d76f85831e5e923e589f4c78c4 [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 <stdio.h>
Marie Janssen49a86702015-07-08 11:48:57 -070024#include <stdlib.h>
Chris Manton307381e2014-09-04 19:48:49 -070025#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080026
27#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"
Marie Janssen49a86702015-07-08 11:48:57 -070031#include "btcore/include/module.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"
Marie Janssen49a86702015-07-08 11:48:57 -070036#include "gap_int.h"
Chris Manton860a9af2014-08-27 10:30:47 -070037#include "gki.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038#include "hcimsgs.h"
39#include "l2c_int.h"
Marie Janssen49a86702015-07-08 11:48:57 -070040#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 Nanavati44802762014-12-23 23:08:58 -080044#include "osi/include/log.h"
Marie Janssen49a86702015-07-08 11:48:57 -070045#include "osi/include/osi.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#include "port_api.h"
48#include "port_ext.h"
Marie Janssen49a86702015-07-08 11:48:57 -070049#include "sdpint.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080050
The Android Open Source Project5738f832012-12-12 16:00:35 -080051#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 Project5738f832012-12-12 16:00:35 -080059#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
66extern 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 Project5738f832012-12-12 16:00:35 -080075#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080076
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-Gans24933b52014-09-24 15:25:02 -070085extern void BTE_InitStack(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -080086
The Android Open Source Project5738f832012-12-12 16:00:35 -080087/* Define BTU storage area
88*/
89#if BTU_DYNAMIC_MEMORY == FALSE
90tBTU_CB btu_cb;
91#endif
92
Chris Manton860a9af2014-08-27 10:30:47 -070093// Communication queue between btu_task and bta.
94extern fixed_queue_t *btu_bta_msg_queue;
95
96// Communication queue between btu_task and hci.
97extern fixed_queue_t *btu_hci_msg_queue;
98
Chris Manton18023292014-08-29 09:12:06 -070099// General timer queue.
100extern fixed_queue_t *btu_general_alarm_queue;
101extern hash_map_t *btu_general_alarm_hash_map;
102extern pthread_mutex_t btu_general_alarm_lock;
103
104// Oneshot timer queue.
105extern fixed_queue_t *btu_oneshot_alarm_queue;
106extern hash_map_t *btu_oneshot_alarm_hash_map;
107extern pthread_mutex_t btu_oneshot_alarm_lock;
108
109// l2cap timer queue.
110extern fixed_queue_t *btu_l2cap_alarm_queue;
111extern hash_map_t *btu_l2cap_alarm_hash_map;
112extern pthread_mutex_t btu_l2cap_alarm_lock;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800113
Chris Manton307381e2014-09-04 19:48:49 -0700114extern fixed_queue_t *event_queue;
115extern fixed_queue_t *btif_msg_queue;
116
117extern thread_t *bt_workqueue_thread;
118
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119/* Define a function prototype to allow a generic timeout handler */
120typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
121
Chris Manton18023292014-08-29 09:12:06 -0700122static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
123static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
Chris Manton307381e2014-09-04 19:48:49 -0700124static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle);
125static void btu_hci_msg_process(BT_HDR *p_msg);
126
127void 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
132void 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
137void 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
163void 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
168void 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
173void 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 Manton18023292014-08-29 09:12:06 -0700177
Chris Manton860a9af2014-08-27 10:30:47 -0700178static 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 Johnson218f3752014-09-03 14:36:44 -0700182 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 Manton860a9af2014-08-27 10:30:47 -0700185 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 Manton860a9af2014-08-27 10:30:47 -0700216 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 Manton18023292014-08-29 09:12:06 -0700244static 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 Manton860a9af2014-08-27 10:30:47 -0700254 }
255 }
Chris Manton860a9af2014-08-27 10:30:47 -0700256}
257
Chris Manton307381e2014-09-04 19:48:49 -0700258void 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 Project5738f832012-12-12 16:00:35 -0800261
Marie Janssendb554582015-06-26 14:53:46 -0700262 LOG_INFO(LOG_TAG, "Bluetooth chip preload is complete");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700263
Chris Manton307381e2014-09-04 19:48:49 -0700264 BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
265 "btu_task received preload complete event");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700266
Chris Manton307381e2014-09-04 19:48:49 -0700267 /* Initialize the mandatory core stack control blocks
268 (BTU, BTM, L2CAP, and SDP)
269 */
270 btu_init_core();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700271
Chris Manton307381e2014-09-04 19:48:49 -0700272 /* Initialize any optional stack components */
273 BTE_InitStack();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800274
Chris Manton307381e2014-09-04 19:48:49 -0700275 bta_sys_init();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800276
Chris Manton307381e2014-09-04 19:48:49 -0700277 /* 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 Project5738f832012-12-12 16:00:35 -0800280#if ( BT_USE_TRACES==TRUE )
Zach Johnson9891f322014-09-22 22:11:55 -0700281 module_init(get_module(BTE_LOGMSG_MODULE));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800282#endif
283
Chris Manton307381e2014-09-04 19:48:49 -0700284 // Inform the bt jni thread initialization is ok.
285 btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286
Chris Manton307381e2014-09-04 19:48:49 -0700287 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 Project5738f832012-12-12 16:00:35 -0800291
Chris Manton307381e2014-09-04 19:48:49 -0700292 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 Project5738f832012-12-12 16:00:35 -0800296
Chris Manton307381e2014-09-04 19:48:49 -0700297 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 Project5738f832012-12-12 16:00:35 -0800301
Chris Manton307381e2014-09-04 19:48:49 -0700302 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 Project5738f832012-12-12 16:00:35 -0800306
Chris Manton307381e2014-09-04 19:48:49 -0700307 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 Project5738f832012-12-12 16:00:35 -0800312
Chris Manton307381e2014-09-04 19:48:49 -0700313void btu_task_shut_down(UNUSED_ATTR void *context) {
Zach Johnsonc8ac8a22014-09-26 17:04:51 -0700314 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 Mantonb1f99152014-09-26 21:31:38 -0700320#if ( BT_USE_TRACES==TRUE )
321 module_clean_up(get_module(BTE_LOGMSG_MODULE));
322#endif
323
Chris Manton307381e2014-09-04 19:48:49 -0700324 bta_sys_free();
Chris Manton307381e2014-09-04 19:48:49 -0700325 btu_free_core();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800326}
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 Manton18023292014-08-29 09:12:06 -0700339static 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 Manton18023292014-08-29 09:12:06 -0700347 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 Manton18023292014-08-29 09:12:06 -0700363 case BTU_TTYPE_RFCOMM_MFC:
364 case BTU_TTYPE_RFCOMM_PORT:
365 rfcomm_process_timeout (p_tle);
366 break;
367
Chris Manton18023292014-08-29 09:12:06 -0700368#if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
369 case BTU_TTYPE_BNEP:
370 bnep_process_timeout(p_tle);
371 break;
372#endif
373
Chris Manton18023292014-08-29 09:12:06 -0700374#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 Manton18023292014-08-29 09:12:06 -0700383#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 Project5738f832012-12-12 16:00:35 -0800419 {
Chris Manton18023292014-08-29 09:12:06 -0700420 tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
421 (*p_uf)(p_tle);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422 }
Chris Manton18023292014-08-29 09:12:06 -0700423 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 Project5738f832012-12-12 16:00:35 -0800440 }
Chris Manton18023292014-08-29 09:12:06 -0700441}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800442
Chris Manton18023292014-08-29 09:12:06 -0700443void btu_general_alarm_cb(void *data) {
444 assert(data != NULL);
445 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446
Chris Manton18023292014-08-29 09:12:06 -0700447 fixed_queue_enqueue(btu_general_alarm_queue, p_tle);
Chris Manton18023292014-08-29 09:12:06 -0700448}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449
Chris Manton18023292014-08-29 09:12:06 -0700450void 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 Janssendb554582015-06-26 14:53:46 -0700462 LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700463 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 Mantonbc3ac062014-09-17 13:30:13 -0700470 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700471 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 -0800472}
473
474/*******************************************************************************
475**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800476** Function btu_stop_timer
477**
478** Description Stop a timer.
479**
480** Returns void
481**
482*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700483void btu_stop_timer(TIMER_LIST_ENT *p_tle) {
484 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800485
Chris Mantonbc3ac062014-09-17 13:30:13 -0700486 if (p_tle->in_use == FALSE)
487 return;
488 p_tle->in_use = FALSE;
489
Chris Manton18023292014-08-29 09:12:06 -0700490 // 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 Janssendb554582015-06-26 14:53:46 -0700493 LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700494 return;
495 }
496 alarm_cancel(alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497}
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 Manton18023292014-08-29 09:12:06 -0700504** Description Start a timer for the specified amount of time in ticks.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800505**
506** Returns void
507**
508*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700509static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle) {
510 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800511
Chris Manton18023292014-08-29 09:12:06 -0700512 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 Project5738f832012-12-12 16:00:35 -0800517
Chris Manton18023292014-08-29 09:12:06 -0700518 default:
519 break;
520 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800521}
522
Chris Manton18023292014-08-29 09:12:06 -0700523static 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 Manton18023292014-08-29 09:12:06 -0700528}
529
530void 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 Janssendb554582015-06-26 14:53:46 -0700542 LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700543 return;
544 }
545 alarm_cancel(alarm);
546
547 p_tle->event = type;
548 p_tle->ticks = timeout_ticks;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700549 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700550 // 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 Project5738f832012-12-12 16:00:35 -0800553
554/*******************************************************************************
555**
556** Function btu_stop_quick_timer
557**
558** Description Stop a timer.
559**
560** Returns void
561**
562*******************************************************************************/
Chris Manton18023292014-08-29 09:12:06 -0700563void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle) {
564 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
Chris Mantonbc3ac062014-09-17 13:30:13 -0700566 if (p_tle->in_use == FALSE)
567 return;
568 p_tle->in_use = FALSE;
569
Chris Manton18023292014-08-29 09:12:06 -0700570 // 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 Janssendb554582015-06-26 14:53:46 -0700573 LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700574 return;
575 }
576 alarm_cancel(alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800577}
578#endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
579
Chris Manton18023292014-08-29 09:12:06 -0700580void 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 Manton18023292014-08-29 09:12:06 -0700587}
588
Chris Manton0aee3312014-06-12 12:52:33 -0700589/*
590 * Starts a oneshot timer with a timeout in seconds.
591 */
Chris Manton18023292014-08-29 09:12:06 -0700592void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
593 assert(p_tle != NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800594
Chris Manton18023292014-08-29 09:12:06 -0700595 // 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 Manton0aee3312014-06-12 12:52:33 -0700601
Chris Manton18023292014-08-29 09:12:06 -0700602 alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
603 if (alarm == NULL) {
Marie Janssendb554582015-06-26 14:53:46 -0700604 LOG_ERROR(LOG_TAG, "%s Unable to create alarm", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700605 return;
606 }
607 alarm_cancel(alarm);
Chris Manton0aee3312014-06-12 12:52:33 -0700608
Chris Manton18023292014-08-29 09:12:06 -0700609 p_tle->event = type;
Chris Mantonbc3ac062014-09-17 13:30:13 -0700610 p_tle->in_use = TRUE;
Chris Manton18023292014-08-29 09:12:06 -0700611 // 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 Manton0aee3312014-06-12 12:52:33 -0700614}
615
616void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) {
Chris Manton18023292014-08-29 09:12:06 -0700617 assert(p_tle != NULL);
Chris Manton0aee3312014-06-12 12:52:33 -0700618
Chris Mantonbc3ac062014-09-17 13:30:13 -0700619 if (p_tle->in_use == FALSE)
620 return;
621 p_tle->in_use = FALSE;
622
Chris Manton18023292014-08-29 09:12:06 -0700623 // 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 Janssendb554582015-06-26 14:53:46 -0700626 LOG_WARN(LOG_TAG, "%s Unable to find expected alarm in hashmap", __func__);
Chris Manton18023292014-08-29 09:12:06 -0700627 return;
628 }
629 alarm_cancel(alarm);
Chris Manton0aee3312014-06-12 12:52:33 -0700630}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631
The Android Open Source Project5738f832012-12-12 16:00:35 -0800632#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*******************************************************************************/
642void btu_check_bt_sleep (void)
643{
Zach Johnson91ba12d2014-09-26 14:03:13 -0700644 // TODO(zachoverflow) take pending commands into account?
645 if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800646 {
Zach Johnson91ba12d2014-09-26 14:03:13 -0700647 bte_main_lpm_allow_bt_device_sleep();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648 }
649}
650#endif