1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 #include <utils/Log.h>
22 #ifndef BT_ERROR_TRACE_0
23 #define BT_ERROR_TRACE_0(l,m)
26 /* Make sure that this has been defined in target.h */
27 #ifndef GKI_NUM_TIMERS
28 #error NO TIMERS: Must define at least 1 timer in the system!
32 #define GKI_NO_NEW_TMRS_STARTED (0x7fffffffL) /* Largest signed positive timer count */
33 #define GKI_UNUSED_LIST_ENTRY (0x80000000L) /* Marks an unused timer list entry (initial value) */
34 #define GKI_MAX_INT32 (0x7fffffffL)
36 #define GKI_ERROR(fmt, ...) ALOGE ("ERROR : %s: " fmt, __FUNCTION__, ## __VA_ARGS__)
38 // Used for controlling alarms from AlarmService.
39 extern void alarm_service_reschedule(void);
41 /*******************************************************************************
43 ** Function gki_timers_init
45 ** Description This internal function is called once at startup to initialize
46 ** all the timer structures.
50 *******************************************************************************/
51 void gki_timers_init(void)
55 gki_cb.com.OSTicksTilExp = 0; /* Remaining time (of OSTimeCurTimeout) before next timer expires */
56 gki_cb.com.OSNumOrigTicks = 0;
57 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
58 gki_cb.com.OSTicksTilStop = 0; /* clear inactivity delay timer */
61 for (tt = 0; tt < GKI_MAX_TASKS; tt++)
63 gki_cb.com.OSWaitTmr [tt] = 0;
65 #if (GKI_NUM_TIMERS > 0)
66 gki_cb.com.OSTaskTmr0 [tt] = 0;
67 gki_cb.com.OSTaskTmr0R [tt] = 0;
70 #if (GKI_NUM_TIMERS > 1)
71 gki_cb.com.OSTaskTmr1 [tt] = 0;
72 gki_cb.com.OSTaskTmr1R [tt] = 0;
75 #if (GKI_NUM_TIMERS > 2)
76 gki_cb.com.OSTaskTmr2 [tt] = 0;
77 gki_cb.com.OSTaskTmr2R [tt] = 0;
80 #if (GKI_NUM_TIMERS > 3)
81 gki_cb.com.OSTaskTmr3 [tt] = 0;
82 gki_cb.com.OSTaskTmr3R [tt] = 0;
86 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
88 gki_cb.com.timer_queues[tt] = NULL;
91 gki_cb.com.p_tick_cb = NULL;
92 gki_cb.com.system_tick_running = FALSE;
97 /*******************************************************************************
99 ** Function gki_timers_is_timer_running
101 ** Description This internal function is called to test if any gki timer are running
104 ** Returns TRUE if at least one time is running in the system, FALSE else.
106 *******************************************************************************/
107 BOOLEAN gki_timers_is_timer_running(void)
110 for (tt = 0; tt < GKI_MAX_TASKS; tt++)
113 #if (GKI_NUM_TIMERS > 0)
114 if(gki_cb.com.OSTaskTmr0 [tt])
120 #if (GKI_NUM_TIMERS > 1)
121 if(gki_cb.com.OSTaskTmr1 [tt] )
127 #if (GKI_NUM_TIMERS > 2)
128 if(gki_cb.com.OSTaskTmr2 [tt] )
134 #if (GKI_NUM_TIMERS > 3)
135 if(gki_cb.com.OSTaskTmr3 [tt] )
146 /*******************************************************************************
148 ** Function GKI_get_tick_count
150 ** Description This function returns the current system ticks
152 ** Returns The current number of system ticks
154 *******************************************************************************/
155 UINT32 GKI_get_tick_count(void)
157 return gki_cb.com.OSTicks;
161 /*******************************************************************************
163 ** Function GKI_ready_to_sleep
165 ** Description This function returns the number of system ticks until the
166 ** next timer will expire. It is typically called by a power
167 ** savings manager to find out how long it can have the system
168 ** sleep before it needs to service the next entry.
172 ** Returns Number of ticks til the next timer expires
173 ** Note: the value is a signed value. This value should be
174 ** compared to x > 0, to avoid misinterpreting negative tick
177 *******************************************************************************/
178 INT32 GKI_ready_to_sleep (void)
180 return (gki_cb.com.OSTicksTilExp);
184 /*******************************************************************************
186 ** Function GKI_start_timer
188 ** Description An application can call this function to start one of
189 ** it's four general purpose timers. Any of the four timers
190 ** can be 1-shot or continuous. If a timer is already running,
191 ** it will be reset to the new parameters.
193 ** Parameters tnum - (input) timer number to be started (TIMER_0,
194 ** TIMER_1, TIMER_2, or TIMER_3)
195 ** ticks - (input) the number of system ticks til the
197 ** is_continuous - (input) TRUE if timer restarts automatically,
198 ** else FALSE if it is a 'one-shot'.
202 *******************************************************************************/
203 void GKI_start_timer (UINT8 tnum, INT32 ticks, BOOLEAN is_continuous)
207 UINT8 task_id = GKI_get_taskid();
208 BOOLEAN bad_timer = FALSE;
213 orig_ticks = ticks; /* save the ticks in case adjustment is necessary */
216 /* If continuous timer, set reload, else set it to 0 */
224 if(gki_timers_is_timer_running() == FALSE)
226 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
227 /* if inactivity delay timer is not running, start system tick */
228 if(gki_cb.com.OSTicksTilStop == 0)
231 if(gki_cb.com.p_tick_cb)
233 /* start system tick */
234 gki_cb.com.system_tick_running = TRUE;
235 (gki_cb.com.p_tick_cb) (TRUE);
237 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
241 /* clear inactivity delay timer */
242 gki_cb.com.OSTicksTilStop = 0;
246 /* Add the time since the last task timer update.
247 ** Note that this works when no timers are active since
248 ** both OSNumOrigTicks and OSTicksTilExp are 0.
250 if (GKI_MAX_INT32 - (gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp) > ticks)
252 ticks += gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp;
255 ticks = GKI_MAX_INT32;
259 #if (GKI_NUM_TIMERS > 0)
261 gki_cb.com.OSTaskTmr0R[task_id] = reload;
262 gki_cb.com.OSTaskTmr0 [task_id] = ticks;
266 #if (GKI_NUM_TIMERS > 1)
268 gki_cb.com.OSTaskTmr1R[task_id] = reload;
269 gki_cb.com.OSTaskTmr1 [task_id] = ticks;
273 #if (GKI_NUM_TIMERS > 2)
275 gki_cb.com.OSTaskTmr2R[task_id] = reload;
276 gki_cb.com.OSTaskTmr2 [task_id] = ticks;
280 #if (GKI_NUM_TIMERS > 3)
282 gki_cb.com.OSTaskTmr3R[task_id] = reload;
283 gki_cb.com.OSTaskTmr3 [task_id] = ticks;
287 bad_timer = TRUE; /* Timer number is bad, so do not use */
290 /* Update the expiration timeout if a legitimate timer */
293 /* Only update the timeout value if it is less than any other newly started timers */
294 gki_adjust_timer_count (orig_ticks);
301 /*******************************************************************************
303 ** Function GKI_stop_timer
305 ** Description An application can call this function to stop one of
306 ** it's four general purpose timers. There is no harm in
307 ** stopping a timer that is already stopped.
309 ** Parameters tnum - (input) timer number to be started (TIMER_0,
310 ** TIMER_1, TIMER_2, or TIMER_3)
313 *******************************************************************************/
314 void GKI_stop_timer (UINT8 tnum)
316 UINT8 task_id = GKI_get_taskid();
320 #if (GKI_NUM_TIMERS > 0)
322 gki_cb.com.OSTaskTmr0R[task_id] = 0;
323 gki_cb.com.OSTaskTmr0 [task_id] = 0;
327 #if (GKI_NUM_TIMERS > 1)
329 gki_cb.com.OSTaskTmr1R[task_id] = 0;
330 gki_cb.com.OSTaskTmr1 [task_id] = 0;
334 #if (GKI_NUM_TIMERS > 2)
336 gki_cb.com.OSTaskTmr2R[task_id] = 0;
337 gki_cb.com.OSTaskTmr2 [task_id] = 0;
341 #if (GKI_NUM_TIMERS > 3)
343 gki_cb.com.OSTaskTmr3R[task_id] = 0;
344 gki_cb.com.OSTaskTmr3 [task_id] = 0;
351 if (gki_timers_is_timer_running() == FALSE)
353 if (gki_cb.com.p_tick_cb)
355 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
356 /* if inactivity delay timer is not running */
357 if ((gki_cb.com.system_tick_running)&&(gki_cb.com.OSTicksTilStop == 0))
359 /* set inactivity delay timer */
360 /* when timer expires, system tick will be stopped */
361 gki_cb.com.OSTicksTilStop = GKI_DELAY_STOP_SYS_TICK;
364 gki_cb.com.system_tick_running = FALSE;
365 gki_cb.com.p_tick_cb(FALSE); /* stop system tick */
376 /*******************************************************************************
378 ** Function GKI_timer_update
380 ** Description This function is called by an OS to drive the GKI's timers.
381 ** It is typically called at every system tick to
382 ** update the timers for all tasks, and check for timeouts.
384 ** Note: It has been designed to also allow for variable tick updates
385 ** so that systems with strict power savings requirements can
386 ** have the update occur at variable intervals.
388 ** Parameters: ticks_since_last_update - (input) This is the number of TICKS that have
389 ** occurred since the last time GKI_timer_update was called.
393 *******************************************************************************/
394 void GKI_timer_update (INT32 ticks_since_last_update)
397 long next_expiration; /* Holds the next soonest expiration time after this update */
399 /* Increment the number of ticks used for time stamps */
400 gki_cb.com.OSTicks += ticks_since_last_update;
402 /* If any timers are running in any tasks, decrement the remaining time til
403 * the timer updates need to take place (next expiration occurs)
405 gki_cb.com.OSTicksTilExp -= ticks_since_last_update;
407 /* Don't allow timer interrupt nesting */
408 if (gki_cb.com.timer_nesting)
411 gki_cb.com.timer_nesting = 1;
413 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
414 /* if inactivity delay timer is set and expired */
415 if (gki_cb.com.OSTicksTilStop)
417 if( gki_cb.com.OSTicksTilStop <= (UINT32)ticks_since_last_update )
419 if(gki_cb.com.p_tick_cb)
421 gki_cb.com.system_tick_running = FALSE;
422 (gki_cb.com.p_tick_cb) (FALSE); /* stop system tick */
424 gki_cb.com.OSTicksTilStop = 0; /* clear inactivity delay timer */
425 gki_cb.com.timer_nesting = 0;
429 gki_cb.com.OSTicksTilStop -= ticks_since_last_update;
433 /* No need to update the ticks if no timeout has occurred */
434 if (gki_cb.com.OSTicksTilExp > 0)
436 // When using alarms from AlarmService we should
437 // always have work to be done here.
438 GKI_ERROR("No work to be done when expected work\n");
439 gki_cb.com.timer_nesting = 0;
443 next_expiration = GKI_NO_NEW_TMRS_STARTED;
445 /* If here then gki_cb.com.OSTicksTilExp <= 0. If negative, then increase gki_cb.com.OSNumOrigTicks
446 to account for the difference so timer updates below are decremented by the full number
447 of ticks. gki_cb.com.OSNumOrigTicks is reset at the bottom of this function so changing this
448 value only affects the timer updates below
450 gki_cb.com.OSNumOrigTicks -= gki_cb.com.OSTicksTilExp;
452 #if GKI_TIMER_LIST_NOPREEMPT == TRUE
453 /* Protect this section because if a GKI_timer_stop happens between:
454 * - gki_cb.com.OSTaskTmr0[task_id] -= gki_cb.com.OSNumOrigTicks;
455 * - gki_cb.com.OSTaskTmr0[task_id] = gki_cb.com.OSTaskTmr0R[task_id];
456 * then the timer may appear stopped while it is about to be reloaded.
457 * Note: Not needed if this function cannot be preempted (typical).
462 /* Check for OS Task Timers */
463 for (task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
465 if (gki_cb.com.OSWaitTmr[task_id] > 0) /* If timer is running */
467 gki_cb.com.OSWaitTmr[task_id] -= gki_cb.com.OSNumOrigTicks;
468 if (gki_cb.com.OSWaitTmr[task_id] <= 0)
471 gki_cb.com.OSRdyTbl[task_id] = TASK_READY;
475 #if (GKI_NUM_TIMERS > 0)
476 /* If any timer is running, decrement */
477 if (gki_cb.com.OSTaskTmr0[task_id] > 0)
479 gki_cb.com.OSTaskTmr0[task_id] -= gki_cb.com.OSNumOrigTicks;
481 if (gki_cb.com.OSTaskTmr0[task_id] <= 0)
483 /* Reload timer and set Timer 0 Expired event mask */
484 gki_cb.com.OSTaskTmr0[task_id] = gki_cb.com.OSTaskTmr0R[task_id];
486 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) && GKI_TIMER_UPDATES_FROM_ISR == TRUE)
487 GKI_isend_event (task_id, TIMER_0_EVT_MASK);
489 GKI_send_event (task_id, TIMER_0_EVT_MASK);
494 /* Check to see if this timer is the next one to expire */
495 if (gki_cb.com.OSTaskTmr0[task_id] > 0 && gki_cb.com.OSTaskTmr0[task_id] < next_expiration)
496 next_expiration = gki_cb.com.OSTaskTmr0[task_id];
499 #if (GKI_NUM_TIMERS > 1)
500 /* If any timer is running, decrement */
501 if (gki_cb.com.OSTaskTmr1[task_id] > 0)
503 gki_cb.com.OSTaskTmr1[task_id] -= gki_cb.com.OSNumOrigTicks;
505 if (gki_cb.com.OSTaskTmr1[task_id] <= 0)
507 /* Reload timer and set Timer 1 Expired event mask */
508 gki_cb.com.OSTaskTmr1[task_id] = gki_cb.com.OSTaskTmr1R[task_id];
510 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) && GKI_TIMER_UPDATES_FROM_ISR == TRUE)
511 GKI_isend_event (task_id, TIMER_1_EVT_MASK);
513 GKI_send_event (task_id, TIMER_1_EVT_MASK);
518 /* Check to see if this timer is the next one to expire */
519 if (gki_cb.com.OSTaskTmr1[task_id] > 0 && gki_cb.com.OSTaskTmr1[task_id] < next_expiration)
520 next_expiration = gki_cb.com.OSTaskTmr1[task_id];
523 #if (GKI_NUM_TIMERS > 2)
524 /* If any timer is running, decrement */
525 if (gki_cb.com.OSTaskTmr2[task_id] > 0)
527 gki_cb.com.OSTaskTmr2[task_id] -= gki_cb.com.OSNumOrigTicks;
529 if (gki_cb.com.OSTaskTmr2[task_id] <= 0)
531 /* Reload timer and set Timer 2 Expired event mask */
532 gki_cb.com.OSTaskTmr2[task_id] = gki_cb.com.OSTaskTmr2R[task_id];
534 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) && GKI_TIMER_UPDATES_FROM_ISR == TRUE)
535 GKI_isend_event (task_id, TIMER_2_EVT_MASK);
537 GKI_send_event (task_id, TIMER_2_EVT_MASK);
542 /* Check to see if this timer is the next one to expire */
543 if (gki_cb.com.OSTaskTmr2[task_id] > 0 && gki_cb.com.OSTaskTmr2[task_id] < next_expiration)
544 next_expiration = gki_cb.com.OSTaskTmr2[task_id];
547 #if (GKI_NUM_TIMERS > 3)
548 /* If any timer is running, decrement */
549 if (gki_cb.com.OSTaskTmr3[task_id] > 0)
551 gki_cb.com.OSTaskTmr3[task_id] -= gki_cb.com.OSNumOrigTicks;
553 if (gki_cb.com.OSTaskTmr3[task_id] <= 0)
555 /* Reload timer and set Timer 3 Expired event mask */
556 gki_cb.com.OSTaskTmr3[task_id] = gki_cb.com.OSTaskTmr3R[task_id];
558 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) && GKI_TIMER_UPDATES_FROM_ISR == TRUE)
559 GKI_isend_event (task_id, TIMER_3_EVT_MASK);
561 GKI_send_event (task_id, TIMER_3_EVT_MASK);
566 /* Check to see if this timer is the next one to expire */
567 if (gki_cb.com.OSTaskTmr3[task_id] > 0 && gki_cb.com.OSTaskTmr3[task_id] < next_expiration)
568 next_expiration = gki_cb.com.OSTaskTmr3[task_id];
572 /* Set the next timer experation value if there is one to start */
573 if (next_expiration < GKI_NO_NEW_TMRS_STARTED)
575 gki_cb.com.OSTicksTilExp = gki_cb.com.OSNumOrigTicks = next_expiration;
579 gki_cb.com.OSTicksTilExp = gki_cb.com.OSNumOrigTicks = 0;
582 // Set alarm service for next alarm.
583 alarm_service_reschedule();
585 #if GKI_TIMER_LIST_NOPREEMPT == TRUE
586 /* End the critical section */
590 // GKI_ERROR("Timer expired - next expiration ticks:%ld\n", next_expiration);
592 gki_cb.com.timer_nesting = 0;
598 /*******************************************************************************
600 ** Function GKI_timer_queue_empty
602 ** Description This function is called by applications to see whether the timer
609 *******************************************************************************/
610 BOOLEAN GKI_timer_queue_empty (void)
614 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
616 if (gki_cb.com.timer_queues[tt])
623 /*******************************************************************************
625 ** Function GKI_timer_queue_register_callback
627 ** Description This function is called by applications to register system tick
628 ** start/stop callback for time queues
631 ** Parameters p_callback - (input) pointer to the system tick callback
635 *******************************************************************************/
636 void GKI_timer_queue_register_callback (SYSTEM_TICK_CBACK *p_callback)
638 gki_cb.com.p_tick_cb = p_callback;
643 /*******************************************************************************
645 ** Function GKI_init_timer_list
647 ** Description This function is called by applications when they
648 ** want to initialize a timer list.
650 ** Parameters p_timer_listq - (input) pointer to the timer list queue object
654 *******************************************************************************/
655 void GKI_init_timer_list (TIMER_LIST_Q *p_timer_listq)
657 p_timer_listq->p_first = NULL;
658 p_timer_listq->p_last = NULL;
659 p_timer_listq->last_ticks = 0;
664 /*******************************************************************************
666 ** Function GKI_init_timer_list_entry
668 ** Description This function is called by the applications when they
669 ** want to initialize a timer list entry. This must be
670 ** done prior to first use of the entry.
672 ** Parameters p_tle - (input) pointer to a timer list queue entry
676 *******************************************************************************/
677 void GKI_init_timer_list_entry (TIMER_LIST_ENT *p_tle)
679 p_tle->p_next = NULL;
680 p_tle->p_prev = NULL;
681 p_tle->ticks = GKI_UNUSED_LIST_ENTRY;
682 p_tle->in_use = FALSE;
686 /*******************************************************************************
688 ** Function GKI_update_timer_list
690 ** Description This function is called by the applications when they
691 ** want to update a timer list. This should be at every
692 ** timer list unit tick, e.g. once per sec, once per minute etc.
694 ** Parameters p_timer_listq - (input) pointer to the timer list queue object
695 ** num_units_since_last_update - (input) number of units since the last update
696 ** (allows for variable unit update)
698 ** NOTE: The following timer list update routines should not be used for exact time
699 ** critical purposes. The timer tasks should be used when exact timing is needed.
701 ** Returns the number of timers that have expired
703 *******************************************************************************/
704 UINT16 GKI_update_timer_list (TIMER_LIST_Q *p_timer_listq, INT32 num_units_since_last_update)
706 TIMER_LIST_ENT *p_tle;
707 UINT16 num_time_out = 0;
711 p_tle = p_timer_listq->p_first;
713 /* First, get the guys who have previously timed out */
714 /* Note that the tick value of the timers should always be '0' */
715 while ((p_tle) && (p_tle->ticks <= 0))
718 p_tle = p_tle->p_next;
721 /* Timer entriy tick values are relative to the preceeding entry */
722 rem_ticks = num_units_since_last_update;
724 /* Now, adjust remaining timer entries */
725 while ((p_tle != NULL) && (rem_ticks > 0))
727 temp_ticks = p_tle->ticks;
728 p_tle->ticks -= rem_ticks;
730 /* See if this timer has just timed out */
731 if (p_tle->ticks <= 0)
733 /* We set the number of ticks to '0' so that the legacy code
734 * that assumes a '0' or nonzero value will still work as coded. */
740 rem_ticks -= temp_ticks; /* Decrement the remaining ticks to process */
741 p_tle = p_tle->p_next;
744 if (p_timer_listq->last_ticks > 0)
746 p_timer_listq->last_ticks -= num_units_since_last_update;
748 /* If the last timer has expired set last_ticks to 0 so that other list update
749 * functions will calculate correctly
751 if (p_timer_listq->last_ticks < 0)
752 p_timer_listq->last_ticks = 0;
755 return (num_time_out);
758 /*******************************************************************************
760 ** Function GKI_get_remaining_ticks
762 ** Description This function is called by an application to get remaining
765 ** Parameters p_timer_listq - (input) pointer to the timer list queue object
766 ** p_target_tle - (input) pointer to a timer list queue entry
768 ** Returns 0 if timer is not used or timer is not in the list
769 ** remaining ticks if success
771 *******************************************************************************/
772 UINT32 GKI_get_remaining_ticks (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT *p_target_tle)
774 TIMER_LIST_ENT *p_tle;
775 UINT32 rem_ticks = 0;
777 if (p_target_tle->in_use)
779 p_tle = p_timer_listq->p_first;
781 /* adding up all of ticks in previous entries */
782 while ((p_tle)&&(p_tle != p_target_tle))
784 rem_ticks += p_tle->ticks;
785 p_tle = p_tle->p_next;
788 /* if found target entry */
789 if (p_tle == p_target_tle)
791 rem_ticks += p_tle->ticks;
795 BT_ERROR_TRACE_0(TRACE_LAYER_GKI, "GKI_get_remaining_ticks: No timer entry in the list");
801 BT_ERROR_TRACE_0(TRACE_LAYER_GKI, "GKI_get_remaining_ticks: timer entry is not active");
807 /*******************************************************************************
809 ** Function GKI_add_to_timer_list
811 ** Description This function is called by an application to add a timer
812 ** entry to a timer list.
814 ** Note: A timer value of '0' will effectively insert an already
815 ** expired event. Negative tick values will be ignored.
817 ** Parameters p_timer_listq - (input) pointer to the timer list queue object
818 ** p_tle - (input) pointer to a timer list queue entry
822 *******************************************************************************/
823 void GKI_add_to_timer_list (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT *p_tle)
825 UINT32 nr_ticks_total;
827 TIMER_LIST_ENT *p_temp;
829 /* Only process valid tick values */
830 if (p_tle->ticks >= 0)
832 /* If this entry is the last in the list */
833 if (p_tle->ticks >= p_timer_listq->last_ticks)
835 /* If this entry is the only entry in the list */
836 if (p_timer_listq->p_first == NULL)
837 p_timer_listq->p_first = p_tle;
840 /* Insert the entry onto the end of the list */
841 if (p_timer_listq->p_last != NULL)
842 p_timer_listq->p_last->p_next = p_tle;
844 p_tle->p_prev = p_timer_listq->p_last;
847 p_tle->p_next = NULL;
848 p_timer_listq->p_last = p_tle;
849 nr_ticks_total = p_tle->ticks;
850 p_tle->ticks -= p_timer_listq->last_ticks;
852 p_timer_listq->last_ticks = nr_ticks_total;
854 else /* This entry needs to be inserted before the last entry */
856 p_temp = p_timer_listq->p_first;
860 /* list is corrupted, exit to avoid crash */
861 GKI_TRACE_ERROR_0("GKI_add_to_timer_list : Timerlist Q is empty");
862 GKI_exception(GKI_ERROR_TIMER_LIST_CORRUPTED, "*** "
863 "GKI_add_to_timer_list(): timer list corrupted! ***");
866 /* Find the entry that the new one needs to be inserted in front of
867 * as last_ticks is the expiry value of p_last, it should be inserted
868 * BEFORE p_last. otherwise list is probably corrupted! */
869 while (p_tle->ticks > p_temp->ticks)
871 /* Update the tick value if looking at an unexpired entry */
872 if (p_temp->ticks > 0)
873 p_tle->ticks -= p_temp->ticks;
875 p_temp = p_temp->p_next;
878 /* The new entry is the first in the list */
879 if (p_temp == p_timer_listq->p_first)
881 p_tle->p_next = p_timer_listq->p_first;
882 p_timer_listq->p_first->p_prev = p_tle;
883 p_timer_listq->p_first = p_tle;
887 p_temp->p_prev->p_next = p_tle;
888 p_tle->p_prev = p_temp->p_prev;
889 p_temp->p_prev = p_tle;
890 p_tle->p_next = p_temp;
892 p_temp->ticks -= p_tle->ticks;
895 p_tle->in_use = TRUE;
897 /* if we already add this timer queue to the array */
898 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
900 if (gki_cb.com.timer_queues[tt] == p_timer_listq)
903 /* add this timer queue to the array */
904 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
906 if (gki_cb.com.timer_queues[tt] == NULL)
909 if (tt < GKI_MAX_TIMER_QUEUES)
911 gki_cb.com.timer_queues[tt] = p_timer_listq;
919 /*******************************************************************************
921 ** Function GKI_remove_from_timer_list
923 ** Description This function is called by an application to remove a timer
924 ** entry from a timer list.
926 ** Parameters p_timer_listq - (input) pointer to the timer list queue object
927 ** p_tle - (input) pointer to a timer list queue entry
931 *******************************************************************************/
932 void GKI_remove_from_timer_list (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT *p_tle)
936 /* Verify that the entry is valid */
937 if (p_tle == NULL || p_tle->in_use == FALSE || p_timer_listq->p_first == NULL)
942 /* Add the ticks remaining in this timer (if any) to the next guy in the list.
943 ** Note: Expired timers have a tick value of '0'.
945 if (p_tle->p_next != NULL)
947 p_tle->p_next->ticks += p_tle->ticks;
951 p_timer_listq->last_ticks -= p_tle->ticks;
954 /* Unlink timer from the list.
956 if (p_timer_listq->p_first == p_tle)
958 p_timer_listq->p_first = p_tle->p_next;
960 if (p_timer_listq->p_first != NULL)
961 p_timer_listq->p_first->p_prev = NULL;
963 if (p_timer_listq->p_last == p_tle)
964 p_timer_listq->p_last = NULL;
968 if (p_timer_listq->p_last == p_tle)
970 p_timer_listq->p_last = p_tle->p_prev;
972 if (p_timer_listq->p_last != NULL)
973 p_timer_listq->p_last->p_next = NULL;
977 if (p_tle->p_next != NULL && p_tle->p_next->p_prev == p_tle)
978 p_tle->p_next->p_prev = p_tle->p_prev;
981 /* Error case - chain messed up ?? */
985 if (p_tle->p_prev != NULL && p_tle->p_prev->p_next == p_tle)
986 p_tle->p_prev->p_next = p_tle->p_next;
989 /* Error case - chain messed up ?? */
995 p_tle->p_next = p_tle->p_prev = NULL;
996 p_tle->ticks = GKI_UNUSED_LIST_ENTRY;
997 p_tle->in_use = FALSE;
999 /* if timer queue is empty */
1000 if (p_timer_listq->p_first == NULL && p_timer_listq->p_last == NULL)
1002 for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
1004 if (gki_cb.com.timer_queues[tt] == p_timer_listq)
1006 gki_cb.com.timer_queues[tt] = NULL;
1016 /*******************************************************************************
1018 ** Function gki_adjust_timer_count
1020 ** Description This function is called whenever a new timer or GKI_wait occurs
1021 ** to adjust (if necessary) the current time til the first expiration.
1022 ** This only needs to make an adjustment if the new timer (in ticks) is
1023 ** less than the number of ticks remaining on the current timer.
1025 ** Parameters: ticks - (input) number of system ticks of the new timer entry
1027 ** NOTE: This routine MUST be called while interrupts are disabled to
1028 ** avoid updates while adjusting the timer variables.
1032 *******************************************************************************/
1033 void gki_adjust_timer_count (INT32 ticks)
1037 /* See if the new timer expires before the current first expiration */
1038 if (gki_cb.com.OSNumOrigTicks == 0 || (ticks < gki_cb.com.OSTicksTilExp && gki_cb.com.OSTicksTilExp > 0))
1040 gki_cb.com.OSNumOrigTicks = (gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp) + ticks;
1041 gki_cb.com.OSTicksTilExp = ticks;
1042 alarm_service_reschedule();