% !TEX root = btstack_gettingstarted.tex
\section{Run Loop API}
\label{appendix:api_run_loop}
$ $
\begin{lstlisting}
// Set timer based on current time in milliseconds.
void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms);

// Set callback that will be executed when timer expires.
void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts));

// Add/Remove timer source.
void run_loop_add_timer(timer_source_t *timer); 
int  run_loop_remove_timer(timer_source_t *timer);

// Init must be called before any other run_loop call. 
// Use RUN_LOOP_EMBEDDED for embedded devices.
void run_loop_init(RUN_LOOP_TYPE type);

// Set data source callback.
void run_loop_set_data_source_handler(data_source_t *ds, int (*process)(data_source_t *_ds));

// Add/Remove data source.
void run_loop_add_data_source(data_source_t *dataSource);
int  run_loop_remove_data_source(data_source_t *dataSource);

// Execute configured run loop. This function does not return.
void run_loop_execute(void);

// Sets how many milliseconds has one tick.
uint32_t embedded_ticks_for_ms(uint32_t time_in_ms);

// Queries the current time in ticks.
uint32_t embedded_get_ticks(void);

// Sets an internal flag that is checked in the critical section
// just before entering sleep mode. Has to be called by the 
// interrupt handler of a data source to signal the run loop that  
// a new data is available.
void embedded_trigger(void);
\end{lstlisting}
\pagebreak