High Performance OPC UA Server SDK  1.1.0.158
timer

If timers are updated the elapsed time between their due time and the time they were actually updated is not considered. More...

Data Structures

struct  timer_base
 Timer management base structure. More...
 

Macros

#define TIMER_ID_INVALID   UINT32_MAX
 Timer id, that is always considered invalid by the timer functions.
 

Typedefs

typedef int timer_cb (uint64_t elapsed, void *data)
 Format of a timer callback. More...
 

Enumerations

enum  timer_mode { TIMER_DEFAULT = 0, TIMER_SINGLESHOT = 1 }
 Mode for newly added timers. More...
 

Functions

static bool timer_isvalid (struct timer_base *base, uint32_t id)
 
int timer_base_init (struct timer_base *base, uint32_t num_timers)
 Initialize a timer base and allocate resources. More...
 
void timer_base_clear (struct timer_base *base)
 Clear a timer base and free all its associated resources. More...
 
int timer_add (struct timer_base *base, uint32_t *id, uint32_t ms, timer_cb cb, void *data, uint8_t mode)
 Initialize and activate an emtpy timer element in the base. More...
 
int timer_remove (struct timer_base *base, uint32_t id, void **data)
 Deactivate and remove a timer entry. More...
 
int timer_activate (struct timer_base *base, uint32_t id)
 Activate the timer entry with id. More...
 
int timer_deactivate (struct timer_base *base, uint32_t id)
 Deactivate the timer entry with id. More...
 
uint32_t timer_update (struct timer_base *base)
 Find and shoot all due timers in the base. More...
 
uint64_t timer_tickcount (void)
 Returns a monotonically increasing tickcount in milisecond resolution. More...
 
uint64_t timer_tickdiff (uint64_t current, uint64_t last)
 Calculate the difference between two tickcounts. More...
 

Detailed Description

If timers are updated the elapsed time between their due time and the time they were actually updated is not considered.

So timers won't shoot at a constant frequency if update isn't called exactly at their due times, but they will always run for at least their configured interval.

A timer can be removed from inside the timer callback using one of the following options:

Note: Each of these is a valid option, but do not combine them!

Typedef Documentation

typedef int timer_cb(uint64_t elapsed, void *data)

Format of a timer callback.

Parameters
elapsedActually elapesd time since the last callback in miliseconds.
dataThe data that was added to the timer with timer_add.
Returns
Zero by default, -1 will remove the timer after the callback.

Enumeration Type Documentation

enum timer_mode

Mode for newly added timers.

See also
timer_add
Enumerator
TIMER_DEFAULT 

Default: periodic timer.

TIMER_SINGLESHOT 

Singleshot timer.

Function Documentation

int timer_activate ( struct timer_base base,
uint32_t  id 
)

Activate the timer entry with id.

The timer will resume shooting with the properties it was added. The timer will not shoot before ms time has past.

Parameters
baseThe timer base holding the timer to activate.
idThe id of the timer to be activated.
Returns
Zero on success or errocode on failure.
int timer_add ( struct timer_base base,
uint32_t *  id,
uint32_t  ms,
timer_cb  cb,
void *  data,
uint8_t  mode 
)

Initialize and activate an emtpy timer element in the base.

Parameters
baseThe timer base to add a new timer to.
idOutput parameter that contains the id of the new timer (may be NULL).
msThe interval of the timer to shoot at in milliseconds. Zero is not allowed.
cbThe callback function to be called when the timer shoots.
dataThe data passed to the callback function (may be NULL).
modeTimer mode like TIMER_DEFAULT, see timer_mode.
Returns
Zero on success or errocode on failure.
void timer_base_clear ( struct timer_base base)

Clear a timer base and free all its associated resources.

Parameters
baseThe base to be cleared.
int timer_base_init ( struct timer_base base,
uint32_t  num_timers 
)

Initialize a timer base and allocate resources.

Parameters
baseThe base to be initialized.
num_timersThe maximum number of timers the base can hold.
Returns
Zero on success or errocode on failure.
int timer_deactivate ( struct timer_base base,
uint32_t  id 
)

Deactivate the timer entry with id.

The timer will stop shooting, but its resources will still be allocated. Note: Calling deactivate and then activate is much faster than calling remove and add for a timer.

Parameters
baseThe timer base holding the timer to deactivate.
idThe id of the timer to be deactivated.
Returns
Zero on success or errocode on failure.
int timer_remove ( struct timer_base base,
uint32_t  id,
void **  data 
)

Deactivate and remove a timer entry.

Parameters
baseThe timer base holding the timer to remove.
idThe id of the timer to be removed.
dataOutput parameter that returns the data added with timer_add (may be NULL).
Returns
Zero on success or errocode on failure.
uint64_t timer_tickcount ( void  )

Returns a monotonically increasing tickcount in milisecond resolution.

Depending on the platform this value may or may not overflow.

uint64_t timer_tickdiff ( uint64_t  current,
uint64_t  last 
)

Calculate the difference between two tickcounts.

This function handles a possible overflow between the tickcounts, but doesn't work correctly if more than one epoch elapsed between the tickcounts.

Parameters
currentThe newer tickcount.
lastThe older tickcount.
Returns
Difference between current and last in miliseconds.
uint32_t timer_update ( struct timer_base base)

Find and shoot all due timers in the base.

Looks at every timer and calls the callback if its due time has passed.

Parameters
baseTimer base whose timers need to be updated
Returns
ms till the next timer is due, or UINT32_MAX if there is none.