blob: 752db6d989af561ae96b3836baa0dba9c61a08a4 [file] [log] [blame]
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -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#include "gki_int.h"
19
20#ifndef BT_ERROR_TRACE_0
21#define BT_ERROR_TRACE_0(l,m)
22#endif
23
24/* Make sure that this has been defined in target.h */
25#ifndef GKI_NUM_TIMERS
26#error NO TIMERS: Must define at least 1 timer in the system!
27#endif
28
29
30#define GKI_NO_NEW_TMRS_STARTED (0x7fffffffL) /* Largest signed positive timer count */
31#define GKI_UNUSED_LIST_ENTRY (0x80000000L) /* Marks an unused timer list entry (initial value) */
32#define GKI_MAX_INT32 (0x7fffffffL)
33
34/*******************************************************************************
35**
36** Function gki_timers_init
37**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -080038** Description This internal function is called once at startup to
39** initialize all the timer structures.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080040**
41** Returns void
42**
43*******************************************************************************/
44void gki_timers_init(void)
45{
Ruchi Kandoi512ee632017-01-03 13:59:10 -080046 uint8_t tt;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080047
48 gki_cb.com.OSTicksTilExp = 0; /* Remaining time (of OSTimeCurTimeout) before next timer expires */
49 gki_cb.com.OSNumOrigTicks = 0;
Ruchi Kandoi303fec12016-12-14 13:22:38 -080050#if (GKI_DELAY_STOP_SYS_TICK > 0)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080051 gki_cb.com.OSTicksTilStop = 0; /* clear inactivity delay timer */
52#endif
53
54 for (tt = 0; tt < GKI_MAX_TASKS; tt++)
55 {
56 gki_cb.com.OSWaitTmr [tt] = 0;
57
58#if (GKI_NUM_TIMERS > 0)
59 gki_cb.com.OSTaskTmr0 [tt] = 0;
60 gki_cb.com.OSTaskTmr0R [tt] = 0;
61#endif
62
63#if (GKI_NUM_TIMERS > 1)
64 gki_cb.com.OSTaskTmr1 [tt] = 0;
65 gki_cb.com.OSTaskTmr1R [tt] = 0;
66#endif
67
68#if (GKI_NUM_TIMERS > 2)
69 gki_cb.com.OSTaskTmr2 [tt] = 0;
70 gki_cb.com.OSTaskTmr2R [tt] = 0;
71#endif
72
73#if (GKI_NUM_TIMERS > 3)
74 gki_cb.com.OSTaskTmr3 [tt] = 0;
75 gki_cb.com.OSTaskTmr3R [tt] = 0;
76#endif
77 }
78
79 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
80 {
81 gki_cb.com.timer_queues[tt] = NULL;
82 }
83
84 gki_cb.com.p_tick_cb = NULL;
Ruchi Kandoi4a179642017-01-04 10:04:48 -080085 gki_cb.com.system_tick_running = false;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080086
87 return;
88}
89
90/*******************************************************************************
91**
92** Function gki_timers_is_timer_running
93**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -080094** Description This internal function is called to test if any gki timer
95** are running
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080096**
97**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -080098** Returns TRUE if at least one time is running in the system, FALSE
99** else.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800100**
101*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800102bool gki_timers_is_timer_running(void)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800103{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800104 uint8_t tt;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800105 for (tt = 0; tt < GKI_MAX_TASKS; tt++)
106 {
107
108#if (GKI_NUM_TIMERS > 0)
109 if(gki_cb.com.OSTaskTmr0 [tt])
110 {
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800111 return true;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800112 }
113#endif
114
115#if (GKI_NUM_TIMERS > 1)
116 if(gki_cb.com.OSTaskTmr1 [tt] )
117 {
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800118 return true;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800119 }
120#endif
121
122#if (GKI_NUM_TIMERS > 2)
123 if(gki_cb.com.OSTaskTmr2 [tt] )
124 {
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800125 return true;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800126 }
127#endif
128
129#if (GKI_NUM_TIMERS > 3)
130 if(gki_cb.com.OSTaskTmr3 [tt] )
131 {
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800132 return true;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800133 }
134#endif
135 }
136
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800137 return false;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800138
139}
140
141/*******************************************************************************
142**
143** Function GKI_get_tick_count
144**
145** Description This function returns the current system ticks
146**
147** Returns The current number of system ticks
148**
149*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800150uint32_t GKI_get_tick_count(void)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800151{
152 return gki_cb.com.OSTicks;
153}
154
155
156/*******************************************************************************
157**
158** Function GKI_ready_to_sleep
159**
160** Description This function returns the number of system ticks until the
161** next timer will expire. It is typically called by a power
162** savings manager to find out how long it can have the system
163** sleep before it needs to service the next entry.
164**
165** Parameters: None
166**
167** Returns Number of ticks til the next timer expires
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800168** Note: The value is a signed value. This value should be
169** compared to x > 0, to avoid misinterpreting negative
170** tick values.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800171**
172*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800173int32_t GKI_ready_to_sleep (void)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800174{
175 return (gki_cb.com.OSTicksTilExp);
176}
177
178
179/*******************************************************************************
180**
181** Function GKI_start_timer
182**
183** Description An application can call this function to start one of
184** it's four general purpose timers. Any of the four timers
185** can be 1-shot or continuous. If a timer is already running,
186** it will be reset to the new parameters.
187**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800188** Parameters tnum - (input) timer number to be started
189** (TIMER_0, TIMER_1, TIMER_2, or
190** TIMER_3)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800191** ticks - (input) the number of system ticks til the
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800192** timer expires.
193** is_continuous - (input) TRUE if timer restarts
194** automatically, else FALSE if it is
195** a 'one-shot'.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800196**
197** Returns void
198**
199*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800200void GKI_start_timer (uint8_t tnum, int32_t ticks, bool is_continuous)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800201{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800202 int32_t reload;
203 int32_t orig_ticks;
204 uint8_t task_id = GKI_get_taskid();
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800205 bool bad_timer = false;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800206
207 if (ticks <= 0)
208 ticks = 1;
209
210 orig_ticks = ticks; /* save the ticks in case adjustment is necessary */
211
212
213 /* If continuous timer, set reload, else set it to 0 */
214 if (is_continuous)
215 reload = ticks;
216 else
217 reload = 0;
218
219 GKI_disable();
220
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800221 if(gki_timers_is_timer_running() == false)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800222 {
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800223#if (GKI_DELAY_STOP_SYS_TICK > 0)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800224 /* if inactivity delay timer is not running, start system tick */
225 if(gki_cb.com.OSTicksTilStop == 0)
226 {
227#endif
228 if(gki_cb.com.p_tick_cb)
229 {
230 /* start system tick */
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800231 gki_cb.com.system_tick_running = true;
232 (gki_cb.com.p_tick_cb)(true);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800233 }
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800234#if (GKI_DELAY_STOP_SYS_TICK > 0)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800235 }
236 else
237 {
238 /* clear inactivity delay timer */
239 gki_cb.com.OSTicksTilStop = 0;
240 }
241#endif
242 }
243 /* Add the time since the last task timer update.
244 ** Note that this works when no timers are active since
245 ** both OSNumOrigTicks and OSTicksTilExp are 0.
246 */
247 if (GKI_MAX_INT32 - (gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp) > ticks)
248 {
249 ticks += gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp;
250 }
251 else
252 ticks = GKI_MAX_INT32;
253
254 switch (tnum)
255 {
256#if (GKI_NUM_TIMERS > 0)
257 case TIMER_0:
258 gki_cb.com.OSTaskTmr0R[task_id] = reload;
259 gki_cb.com.OSTaskTmr0 [task_id] = ticks;
260 break;
261#endif
262
263#if (GKI_NUM_TIMERS > 1)
264 case TIMER_1:
265 gki_cb.com.OSTaskTmr1R[task_id] = reload;
266 gki_cb.com.OSTaskTmr1 [task_id] = ticks;
267 break;
268#endif
269
270#if (GKI_NUM_TIMERS > 2)
271 case TIMER_2:
272 gki_cb.com.OSTaskTmr2R[task_id] = reload;
273 gki_cb.com.OSTaskTmr2 [task_id] = ticks;
274 break;
275#endif
276
277#if (GKI_NUM_TIMERS > 3)
278 case TIMER_3:
279 gki_cb.com.OSTaskTmr3R[task_id] = reload;
280 gki_cb.com.OSTaskTmr3 [task_id] = ticks;
281 break;
282#endif
283 default:
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800284 bad_timer = true; /* Timer number is bad, so do not use */
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800285 }
286
287 /* Update the expiration timeout if a legitimate timer */
288 if (!bad_timer)
289 {
290 /* Only update the timeout value if it is less than any other newly started timers */
291 gki_adjust_timer_count (orig_ticks);
292 }
293
294 GKI_enable();
295
296}
297
298/*******************************************************************************
299**
300** Function GKI_stop_timer
301**
302** Description An application can call this function to stop one of
303** it's four general purpose timers. There is no harm in
304** stopping a timer that is already stopped.
305**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800306** Parameters tnum - (input) timer number to be started (TIMER_0,
307** TIMER_1, TIMER_2, or TIMER_3)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800308** Returns void
309**
310*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800311void GKI_stop_timer (uint8_t tnum)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800312{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800313 uint8_t task_id = GKI_get_taskid();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800314
315 GKI_disable();
316
317 switch (tnum)
318 {
319#if (GKI_NUM_TIMERS > 0)
320 case TIMER_0:
321 gki_cb.com.OSTaskTmr0R[task_id] = 0;
322 gki_cb.com.OSTaskTmr0 [task_id] = 0;
323 break;
324#endif
325
326#if (GKI_NUM_TIMERS > 1)
327 case TIMER_1:
328 gki_cb.com.OSTaskTmr1R[task_id] = 0;
329 gki_cb.com.OSTaskTmr1 [task_id] = 0;
330 break;
331#endif
332
333#if (GKI_NUM_TIMERS > 2)
334 case TIMER_2:
335 gki_cb.com.OSTaskTmr2R[task_id] = 0;
336 gki_cb.com.OSTaskTmr2 [task_id] = 0;
337 break;
338#endif
339
340#if (GKI_NUM_TIMERS > 3)
341 case TIMER_3:
342 gki_cb.com.OSTaskTmr3R[task_id] = 0;
343 gki_cb.com.OSTaskTmr3 [task_id] = 0;
344 break;
345#endif
346 }
347
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800348 if (gki_timers_is_timer_running() == false)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800349 {
350 if (gki_cb.com.p_tick_cb)
351 {
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800352#if (GKI_DELAY_STOP_SYS_TICK > 0)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800353 /* if inactivity delay timer is not running */
354 if ((gki_cb.com.system_tick_running)&&(gki_cb.com.OSTicksTilStop == 0))
355 {
356 /* set inactivity delay timer */
357 /* when timer expires, system tick will be stopped */
358 gki_cb.com.OSTicksTilStop = GKI_DELAY_STOP_SYS_TICK;
359 }
360#else
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800361 gki_cb.com.system_tick_running = false;
362 (gki_cb.com.p_tick_cb)(false); /* stop system tick */
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800363#endif
364 }
365 }
366
367 GKI_enable();
368
369
370}
371
372
373/*******************************************************************************
374**
375** Function GKI_timer_update
376**
377** Description This function is called by an OS to drive the GKI's timers.
378** It is typically called at every system tick to
379** update the timers for all tasks, and check for timeouts.
380**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800381** Note: It has been designed to also allow for variable tick
382** updates so that systems with strict power savings
383** requirements can have the update occur at variable
384** intervals.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800385**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800386** Parameters: ticks_since_last_update - (input) This is the number of
387** TICKS that have occurred since the last time
388** GKI_timer_update was called.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800389**
390** Returns void
391**
392*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800393void GKI_timer_update (int32_t ticks_since_last_update)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800394{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800395 uint8_t task_id;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800396 long next_expiration; /* Holds the next soonest expiration time after this update */
397
398 /* Increment the number of ticks used for time stamps */
399 gki_cb.com.OSTicks += ticks_since_last_update;
400
401 /* If any timers are running in any tasks, decrement the remaining time til
402 * the timer updates need to take place (next expiration occurs)
403 */
404 gki_cb.com.OSTicksTilExp -= ticks_since_last_update;
405
406 /* Don't allow timer interrupt nesting */
407 if (gki_cb.com.timer_nesting)
408 return;
409
410 gki_cb.com.timer_nesting = 1;
411
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800412#if (GKI_DELAY_STOP_SYS_TICK > 0)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800413 /* if inactivity delay timer is set and expired */
414 if (gki_cb.com.OSTicksTilStop)
415 {
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800416 if( gki_cb.com.OSTicksTilStop <= (uint32_t)ticks_since_last_update )
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800417 {
418 if(gki_cb.com.p_tick_cb)
419 {
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800420 gki_cb.com.system_tick_running = false;
421 (gki_cb.com.p_tick_cb)(false); /* stop system tick */
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800422 }
423 gki_cb.com.OSTicksTilStop = 0; /* clear inactivity delay timer */
424 gki_cb.com.timer_nesting = 0;
425 return;
426 }
427 else
428 gki_cb.com.OSTicksTilStop -= ticks_since_last_update;
429 }
430#endif
431
432 /* No need to update the ticks if no timeout has occurred */
433 if (gki_cb.com.OSTicksTilExp > 0)
434 {
435 gki_cb.com.timer_nesting = 0;
436 return;
437 }
438
439 GKI_disable();
440
441 next_expiration = GKI_NO_NEW_TMRS_STARTED;
442
443 /* If here then gki_cb.com.OSTicksTilExp <= 0. If negative, then increase gki_cb.com.OSNumOrigTicks
444 to account for the difference so timer updates below are decremented by the full number
445 of ticks. gki_cb.com.OSNumOrigTicks is reset at the bottom of this function so changing this
446 value only affects the timer updates below
447 */
448 gki_cb.com.OSNumOrigTicks -= gki_cb.com.OSTicksTilExp;
449
450 /* Check for OS Task Timers */
451 for (task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
452 {
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700453 if (gki_cb.com.OSRdyTbl[task_id] == TASK_DEAD)
454 {
455 // task is shutdown do not try to service timers
456 continue;
457 }
458
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800459 if (gki_cb.com.OSWaitTmr[task_id] > 0) /* If timer is running */
460 {
461 gki_cb.com.OSWaitTmr[task_id] -= gki_cb.com.OSNumOrigTicks;
462 if (gki_cb.com.OSWaitTmr[task_id] <= 0)
463 {
464 /* Timer Expired */
465 gki_cb.com.OSRdyTbl[task_id] = TASK_READY;
466 }
467 }
468
469#if (GKI_NUM_TIMERS > 0)
470 /* If any timer is running, decrement */
471 if (gki_cb.com.OSTaskTmr0[task_id] > 0)
472 {
473 gki_cb.com.OSTaskTmr0[task_id] -= gki_cb.com.OSNumOrigTicks;
474
475 if (gki_cb.com.OSTaskTmr0[task_id] <= 0)
476 {
477 /* Set Timer 0 Expired event mask and reload timer */
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800478#if (GKI_TIMER_UPDATES_FROM_ISR == TRUE)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800479 GKI_isend_event (task_id, TIMER_0_EVT_MASK);
480#else
481 GKI_send_event (task_id, TIMER_0_EVT_MASK);
482#endif
483 gki_cb.com.OSTaskTmr0[task_id] = gki_cb.com.OSTaskTmr0R[task_id];
484 }
485 }
486
487 /* Check to see if this timer is the next one to expire */
488 if (gki_cb.com.OSTaskTmr0[task_id] > 0 && gki_cb.com.OSTaskTmr0[task_id] < next_expiration)
489 next_expiration = gki_cb.com.OSTaskTmr0[task_id];
490#endif
491
492#if (GKI_NUM_TIMERS > 1)
493 /* If any timer is running, decrement */
494 if (gki_cb.com.OSTaskTmr1[task_id] > 0)
495 {
496 gki_cb.com.OSTaskTmr1[task_id] -= gki_cb.com.OSNumOrigTicks;
497
498 if (gki_cb.com.OSTaskTmr1[task_id] <= 0)
499 {
500 /* Set Timer 1 Expired event mask and reload timer */
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800501#if (GKI_TIMER_UPDATES_FROM_ISR == TRUE)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800502 GKI_isend_event (task_id, TIMER_1_EVT_MASK);
503#else
504 GKI_send_event (task_id, TIMER_1_EVT_MASK);
505#endif
506 gki_cb.com.OSTaskTmr1[task_id] = gki_cb.com.OSTaskTmr1R[task_id];
507 }
508 }
509
510 /* Check to see if this timer is the next one to expire */
511 if (gki_cb.com.OSTaskTmr1[task_id] > 0 && gki_cb.com.OSTaskTmr1[task_id] < next_expiration)
512 next_expiration = gki_cb.com.OSTaskTmr1[task_id];
513#endif
514
515#if (GKI_NUM_TIMERS > 2)
516 /* If any timer is running, decrement */
517 if (gki_cb.com.OSTaskTmr2[task_id] > 0)
518 {
519 gki_cb.com.OSTaskTmr2[task_id] -= gki_cb.com.OSNumOrigTicks;
520
521 if (gki_cb.com.OSTaskTmr2[task_id] <= 0)
522 {
523 /* Set Timer 2 Expired event mask and reload timer */
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800524#if (GKI_TIMER_UPDATES_FROM_ISR == TRUE)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800525 GKI_isend_event (task_id, TIMER_2_EVT_MASK);
526#else
527 GKI_send_event (task_id, TIMER_2_EVT_MASK);
528#endif
529 gki_cb.com.OSTaskTmr2[task_id] = gki_cb.com.OSTaskTmr2R[task_id];
530 }
531 }
532
533 /* Check to see if this timer is the next one to expire */
534 if (gki_cb.com.OSTaskTmr2[task_id] > 0 && gki_cb.com.OSTaskTmr2[task_id] < next_expiration)
535 next_expiration = gki_cb.com.OSTaskTmr2[task_id];
536#endif
537
538#if (GKI_NUM_TIMERS > 3)
539 /* If any timer is running, decrement */
540 if (gki_cb.com.OSTaskTmr3[task_id] > 0)
541 {
542 gki_cb.com.OSTaskTmr3[task_id] -= gki_cb.com.OSNumOrigTicks;
543
544 if (gki_cb.com.OSTaskTmr3[task_id] <= 0)
545 {
546 /* Set Timer 3 Expired event mask and reload timer */
Ruchi Kandoi303fec12016-12-14 13:22:38 -0800547#if (GKI_TIMER_UPDATES_FROM_ISR == TRUE)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800548 GKI_isend_event (task_id, TIMER_3_EVT_MASK);
549#else
550 GKI_send_event (task_id, TIMER_3_EVT_MASK);
551#endif
552 gki_cb.com.OSTaskTmr3[task_id] = gki_cb.com.OSTaskTmr3R[task_id];
553 }
554 }
555
556 /* Check to see if this timer is the next one to expire */
557 if (gki_cb.com.OSTaskTmr3[task_id] > 0 && gki_cb.com.OSTaskTmr3[task_id] < next_expiration)
558 next_expiration = gki_cb.com.OSTaskTmr3[task_id];
559#endif
560
561 }
562
563 /* Set the next timer experation value if there is one to start */
564 if (next_expiration < GKI_NO_NEW_TMRS_STARTED)
565 {
566 gki_cb.com.OSTicksTilExp = gki_cb.com.OSNumOrigTicks = next_expiration;
567 }
568 else
569 {
570 gki_cb.com.OSTicksTilExp = gki_cb.com.OSNumOrigTicks = 0;
571 }
572
573 gki_cb.com.timer_nesting = 0;
574
575 GKI_enable();
576
577 return;
578}
579
580
581/*******************************************************************************
582**
583** Function GKI_timer_queue_empty
584**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800585** Description This function is called by applications to see whether the
586** timer queue is empty
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800587**
588** Parameters
589**
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800590** Returns bool
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800591**
592*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800593bool GKI_timer_queue_empty (void)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800594{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800595 uint8_t tt;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800596
597 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
598 {
599 if (gki_cb.com.timer_queues[tt])
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800600 return false;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800601 }
602
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800603 return true;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800604}
605
606/*******************************************************************************
607**
608** Function GKI_timer_queue_register_callback
609**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800610** Description This function is called by applications to register system
611** tick start/stop callback for time queues
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800612**
613**
614** Parameters p_callback - (input) pointer to the system tick callback
615**
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800616** Returns bool
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800617**
618*******************************************************************************/
619void GKI_timer_queue_register_callback (SYSTEM_TICK_CBACK *p_callback)
620{
621 gki_cb.com.p_tick_cb = p_callback;
622
623 return;
624}
625
626/*******************************************************************************
627**
628** Function GKI_init_timer_list
629**
630** Description This function is called by applications when they
631** want to initialize a timer list.
632**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800633** Parameters p_timer_listq - (input) pointer to the timer list queue
634** object
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800635**
636** Returns void
637**
638*******************************************************************************/
639void GKI_init_timer_list (TIMER_LIST_Q *p_timer_listq)
640{
641 p_timer_listq->p_first = NULL;
642 p_timer_listq->p_last = NULL;
643 p_timer_listq->last_ticks = 0;
644
645 return;
646}
647
648/*******************************************************************************
649**
650** Function GKI_init_timer_list_entry
651**
652** Description This function is called by the applications when they
653** want to initialize a timer list entry. This must be
654** done prior to first use of the entry.
655**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800656** Parameters p_tle - (input) pointer to a timer list queue entry
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800657**
658** Returns void
659**
660*******************************************************************************/
661void GKI_init_timer_list_entry (TIMER_LIST_ENT *p_tle)
662{
663 p_tle->p_next = NULL;
664 p_tle->p_prev = NULL;
665 p_tle->ticks = GKI_UNUSED_LIST_ENTRY;
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800666 p_tle->in_use = false;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800667}
668
669
670/*******************************************************************************
671**
672** Function GKI_update_timer_list
673**
674** Description This function is called by the applications when they
675** want to update a timer list. This should be at every
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800676** timer list unit tick, e.g. once per sec, once per minute
677** etc.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800678**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800679** Parameters p_timer_listq - (input) pointer to the timer list queue
680** object
681** num_units_since_last_update - (input) number of units since
682** the last update (allows for variable unit update)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800683**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800684** NOTE: The following timer list update routines should not be used for exact
685** time critical purposes. The timer tasks should be used when exact
686** timing is needed.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800687**
688** Returns the number of timers that have expired
689**
690*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800691uint16_t GKI_update_timer_list (TIMER_LIST_Q *p_timer_listq, int32_t num_units_since_last_update)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800692{
693 TIMER_LIST_ENT *p_tle;
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800694 uint16_t num_time_out = 0;
695 int32_t rem_ticks;
696 int32_t temp_ticks;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800697
698 p_tle = p_timer_listq->p_first;
699
700 /* First, get the guys who have previously timed out */
701 /* Note that the tick value of the timers should always be '0' */
702 while ((p_tle) && (p_tle->ticks <= 0))
703 {
704 num_time_out++;
705 p_tle = p_tle->p_next;
706 }
707
708 /* Timer entriy tick values are relative to the preceeding entry */
709 rem_ticks = num_units_since_last_update;
710
711 /* Now, adjust remaining timer entries */
712 while ((p_tle != NULL) && (rem_ticks > 0))
713 {
714 temp_ticks = p_tle->ticks;
715 p_tle->ticks -= rem_ticks;
716
717 /* See if this timer has just timed out */
718 if (p_tle->ticks <= 0)
719 {
720 /* We set the number of ticks to '0' so that the legacy code
721 * that assumes a '0' or nonzero value will still work as coded. */
722 p_tle->ticks = 0;
723
724 num_time_out++;
725 }
726
727 rem_ticks -= temp_ticks; /* Decrement the remaining ticks to process */
728 p_tle = p_tle->p_next;
729 }
730
731 if (p_timer_listq->last_ticks > 0)
732 {
733 p_timer_listq->last_ticks -= num_units_since_last_update;
734
735 /* If the last timer has expired set last_ticks to 0 so that other list update
736 * functions will calculate correctly
737 */
738 if (p_timer_listq->last_ticks < 0)
739 p_timer_listq->last_ticks = 0;
740 }
741
742 return (num_time_out);
743}
744
745/*******************************************************************************
746**
747** Function GKI_get_remaining_ticks
748**
749** Description This function is called by an application to get remaining
750** ticks to expire
751**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800752** Parameters p_timer_listq - (input) pointer to the timer list queue
753** object
754** p_target_tle - (input) pointer to a timer list queue entry
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800755**
756** Returns 0 if timer is not used or timer is not in the list
757** remaining ticks if success
758**
759*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800760uint32_t GKI_get_remaining_ticks (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT *p_target_tle)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800761{
762 TIMER_LIST_ENT *p_tle;
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800763 uint32_t rem_ticks = 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800764
765 if (p_target_tle->in_use)
766 {
767 p_tle = p_timer_listq->p_first;
768
769 /* adding up all of ticks in previous entries */
770 while ((p_tle)&&(p_tle != p_target_tle))
771 {
772 rem_ticks += p_tle->ticks;
773 p_tle = p_tle->p_next;
774 }
775
776 /* if found target entry */
777 if (p_tle == p_target_tle)
778 {
779 rem_ticks += p_tle->ticks;
780 }
781 else
782 {
783 BT_ERROR_TRACE_0(TRACE_LAYER_GKI, "GKI_get_remaining_ticks: No timer entry in the list");
784 return(0);
785 }
786 }
787 else
788 {
789 BT_ERROR_TRACE_0(TRACE_LAYER_GKI, "GKI_get_remaining_ticks: timer entry is not active");
790 }
791
792 return (rem_ticks);
793}
794
795/*******************************************************************************
796**
797** Function GKI_add_to_timer_list
798**
799** Description This function is called by an application to add a timer
800** entry to a timer list.
801**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800802** Note: A timer value of '0' will effectively insert an
803** already expired event. Negative tick values will be
804** ignored.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800805**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800806** Parameters p_timer_listq - (input) pointer to the timer list queue
807** object
808** p_tle - (input) pointer to a timer list queue entry
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800809**
810** Returns void
811**
812*******************************************************************************/
813void GKI_add_to_timer_list (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT *p_tle)
814{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800815 uint32_t nr_ticks_total;
816 uint8_t tt;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800817 TIMER_LIST_ENT *p_temp;
818 if (p_tle == NULL || p_timer_listq == NULL) {
819 GKI_TRACE_3("%s: invalid argument %x, %x****************************<<", __func__, p_timer_listq, p_tle);
820 return;
821 }
822
823
824 /* Only process valid tick values */
825 if (p_tle->ticks >= 0)
826 {
827 /* If this entry is the last in the list */
828 if (p_tle->ticks >= p_timer_listq->last_ticks)
829 {
830 /* If this entry is the only entry in the list */
831 if (p_timer_listq->p_first == NULL)
832 p_timer_listq->p_first = p_tle;
833 else
834 {
835 /* Insert the entry onto the end of the list */
836 if (p_timer_listq->p_last != NULL)
837 p_timer_listq->p_last->p_next = p_tle;
838
839 p_tle->p_prev = p_timer_listq->p_last;
840 }
841
842 p_tle->p_next = NULL;
843 p_timer_listq->p_last = p_tle;
844 nr_ticks_total = p_tle->ticks;
845 p_tle->ticks -= p_timer_listq->last_ticks;
846
847 p_timer_listq->last_ticks = nr_ticks_total;
848 }
849 else /* This entry needs to be inserted before the last entry */
850 {
851 /* Find the entry that the new one needs to be inserted in front of */
852 p_temp = p_timer_listq->p_first;
853 while (p_tle->ticks > p_temp->ticks)
854 {
855 /* Update the tick value if looking at an unexpired entry */
856 if (p_temp->ticks > 0)
857 p_tle->ticks -= p_temp->ticks;
858
859 p_temp = p_temp->p_next;
860 }
861
862 /* The new entry is the first in the list */
863 if (p_temp == p_timer_listq->p_first)
864 {
865 p_tle->p_next = p_timer_listq->p_first;
866 p_timer_listq->p_first->p_prev = p_tle;
867 p_timer_listq->p_first = p_tle;
868 }
869 else
870 {
871 p_temp->p_prev->p_next = p_tle;
872 p_tle->p_prev = p_temp->p_prev;
873 p_temp->p_prev = p_tle;
874 p_tle->p_next = p_temp;
875 }
876 p_temp->ticks -= p_tle->ticks;
877 }
878
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800879 p_tle->in_use = true;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800880
881 /* if we already add this timer queue to the array */
882 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
883 {
884 if (gki_cb.com.timer_queues[tt] == p_timer_listq)
885 return;
886 }
887 /* add this timer queue to the array */
888 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
889 {
890 if (gki_cb.com.timer_queues[tt] == NULL)
891 break;
892 }
893 if (tt < GKI_MAX_TIMER_QUEUES)
894 {
895 gki_cb.com.timer_queues[tt] = p_timer_listq;
896 }
897 }
898
899 return;
900}
901
902
903/*******************************************************************************
904**
905** Function GKI_remove_from_timer_list
906**
907** Description This function is called by an application to remove a timer
908** entry from a timer list.
909**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800910** Parameters p_timer_listq - (input) pointer to the timer list queue
911** object
912** p_tle - (input) pointer to a timer list queue entry
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800913**
914** Returns void
915**
916*******************************************************************************/
917void GKI_remove_from_timer_list (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT *p_tle)
918{
Ruchi Kandoi512ee632017-01-03 13:59:10 -0800919 uint8_t tt;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800920
921 /* Verify that the entry is valid */
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800922 if (p_tle == NULL || p_tle->in_use == false || p_timer_listq->p_first == NULL)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800923 {
924 return;
925 }
926
927 /* Add the ticks remaining in this timer (if any) to the next guy in the list.
928 ** Note: Expired timers have a tick value of '0'.
929 */
930 if (p_tle->p_next != NULL)
931 {
932 p_tle->p_next->ticks += p_tle->ticks;
933 }
934 else
935 {
936 p_timer_listq->last_ticks -= p_tle->ticks;
937 }
938
939 /* Unlink timer from the list.
940 */
941 if (p_timer_listq->p_first == p_tle)
942 {
943 p_timer_listq->p_first = p_tle->p_next;
944
945 if (p_timer_listq->p_first != NULL)
946 p_timer_listq->p_first->p_prev = NULL;
947
948 if (p_timer_listq->p_last == p_tle)
949 p_timer_listq->p_last = NULL;
950 }
951 else
952 {
953 if (p_timer_listq->p_last == p_tle)
954 {
955 p_timer_listq->p_last = p_tle->p_prev;
956
957 if (p_timer_listq->p_last != NULL)
958 p_timer_listq->p_last->p_next = NULL;
959 }
960 else
961 {
962 if (p_tle->p_next != NULL && p_tle->p_next->p_prev == p_tle)
963 p_tle->p_next->p_prev = p_tle->p_prev;
964 else
965 {
966 /* Error case - chain messed up ?? */
967 return;
968 }
969
970 if (p_tle->p_prev != NULL && p_tle->p_prev->p_next == p_tle)
971 p_tle->p_prev->p_next = p_tle->p_next;
972 else
973 {
974 /* Error case - chain messed up ?? */
975 return;
976 }
977 }
978 }
979
980 p_tle->p_next = p_tle->p_prev = NULL;
981 p_tle->ticks = GKI_UNUSED_LIST_ENTRY;
Ruchi Kandoi4a179642017-01-04 10:04:48 -0800982 p_tle->in_use = false;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800983
984 /* if timer queue is empty */
985 if (p_timer_listq->p_first == NULL && p_timer_listq->p_last == NULL)
986 {
987 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
988 {
989 if (gki_cb.com.timer_queues[tt] == p_timer_listq)
990 {
991 gki_cb.com.timer_queues[tt] = NULL;
992 break;
993 }
994 }
995 }
996
997 return;
998}
999
1000
1001/*******************************************************************************
1002**
1003** Function gki_adjust_timer_count
1004**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -08001005** Description This function is called whenever a new timer or GKI_wait
1006** occurs to adjust (if necessary) the current time til the
1007** first expiration. This only needs to make an adjustment if
1008** the new timer (in ticks) is less than the number of ticks
1009** remaining on the current timer.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001010**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -08001011** Parameters: ticks - (input) number of system ticks of the new timer
1012** entry
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001013**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -08001014** NOTE: This routine MUST be called while interrupts are
1015** disabled to avoid updates while adjusting the timer
1016** variables.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001017**
1018** Returns void
1019**
1020*******************************************************************************/
Ruchi Kandoi512ee632017-01-03 13:59:10 -08001021void gki_adjust_timer_count (int32_t ticks)
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001022{
1023 if (ticks > 0)
1024 {
1025 /* See if the new timer expires before the current first expiration */
1026 if (gki_cb.com.OSNumOrigTicks == 0 || (ticks < gki_cb.com.OSTicksTilExp && gki_cb.com.OSTicksTilExp > 0))
1027 {
1028 gki_cb.com.OSNumOrigTicks = (gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp) + ticks;
1029 gki_cb.com.OSTicksTilExp = ticks;
1030 }
1031 }
1032
1033 return;
1034}