xref: /btstack/src/btstack_run_loop.h (revision 8263662281de58d7c15e8cb0b49b980df510c088)
1*82636622SMatthias Ringwald /*
2*82636622SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*82636622SMatthias Ringwald  *
4*82636622SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*82636622SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*82636622SMatthias Ringwald  * are met:
7*82636622SMatthias Ringwald  *
8*82636622SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*82636622SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*82636622SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*82636622SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*82636622SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*82636622SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*82636622SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*82636622SMatthias Ringwald  *    from this software without specific prior written permission.
16*82636622SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*82636622SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*82636622SMatthias Ringwald  *    monetary gain.
19*82636622SMatthias Ringwald  *
20*82636622SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*82636622SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*82636622SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*82636622SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*82636622SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*82636622SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*82636622SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*82636622SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*82636622SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*82636622SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*82636622SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*82636622SMatthias Ringwald  * SUCH DAMAGE.
32*82636622SMatthias Ringwald  *
33*82636622SMatthias Ringwald  * Please inquire about commercial licensing options at
34*82636622SMatthias Ringwald  * [email protected]
35*82636622SMatthias Ringwald  *
36*82636622SMatthias Ringwald  */
37*82636622SMatthias Ringwald 
38*82636622SMatthias Ringwald /*
39*82636622SMatthias Ringwald  *  run_loop.h
40*82636622SMatthias Ringwald  *
41*82636622SMatthias Ringwald  *  Created by Matthias Ringwald on 6/6/09.
42*82636622SMatthias Ringwald  */
43*82636622SMatthias Ringwald 
44*82636622SMatthias Ringwald #ifndef __RUN_LOOP_H
45*82636622SMatthias Ringwald #define __RUN_LOOP_H
46*82636622SMatthias Ringwald 
47*82636622SMatthias Ringwald #include "btstack-config.h"
48*82636622SMatthias Ringwald 
49*82636622SMatthias Ringwald #include "btstack_linked_list.h"
50*82636622SMatthias Ringwald 
51*82636622SMatthias Ringwald #include <stdint.h>
52*82636622SMatthias Ringwald 
53*82636622SMatthias Ringwald #ifdef HAVE_TIME
54*82636622SMatthias Ringwald #include <sys/time.h>
55*82636622SMatthias Ringwald #endif
56*82636622SMatthias Ringwald 
57*82636622SMatthias Ringwald #if defined __cplusplus
58*82636622SMatthias Ringwald extern "C" {
59*82636622SMatthias Ringwald #endif
60*82636622SMatthias Ringwald 
61*82636622SMatthias Ringwald typedef struct data_source {
62*82636622SMatthias Ringwald     btstack_linked_item_t item;
63*82636622SMatthias Ringwald     int  fd;                                 // <-- file descriptor to watch or 0
64*82636622SMatthias Ringwald     int  (*process)(struct data_source *ds); // <-- do processing
65*82636622SMatthias Ringwald } data_source_t;
66*82636622SMatthias Ringwald 
67*82636622SMatthias Ringwald typedef struct timer {
68*82636622SMatthias Ringwald     btstack_linked_item_t item;
69*82636622SMatthias Ringwald #ifdef HAVE_TIME
70*82636622SMatthias Ringwald     struct timeval timeout;                  // <-- next timeout
71*82636622SMatthias Ringwald #endif
72*82636622SMatthias Ringwald #if defined(HAVE_TICK) || defined(HAVE_TIME_MS)
73*82636622SMatthias Ringwald     uint32_t timeout;                       // timeout in system ticks (HAVE_TICK) or millis (HAVE_TIME_MS)
74*82636622SMatthias Ringwald #endif
75*82636622SMatthias Ringwald     void  (*process)(struct timer *ts);      // <-- do processing
76*82636622SMatthias Ringwald } timer_source_t;
77*82636622SMatthias Ringwald 
78*82636622SMatthias Ringwald //
79*82636622SMatthias Ringwald typedef struct run_loop {
80*82636622SMatthias Ringwald 	void (*init)(void);
81*82636622SMatthias Ringwald 	void (*add_data_source)(data_source_t *dataSource);
82*82636622SMatthias Ringwald 	int  (*remove_data_source)(data_source_t *dataSource);
83*82636622SMatthias Ringwald 	void (*set_timer)(timer_source_t * timer, uint32_t timeout_in_ms);
84*82636622SMatthias Ringwald 	void (*add_timer)(timer_source_t *timer);
85*82636622SMatthias Ringwald 	int  (*remove_timer)(timer_source_t *timer);
86*82636622SMatthias Ringwald 	void (*execute)(void);
87*82636622SMatthias Ringwald 	void (*dump_timer)(void);
88*82636622SMatthias Ringwald 	uint32_t (*get_time_ms)(void);
89*82636622SMatthias Ringwald } run_loop_t;
90*82636622SMatthias Ringwald 
91*82636622SMatthias Ringwald void run_loop_timer_dump(void);
92*82636622SMatthias Ringwald 
93*82636622SMatthias Ringwald 
94*82636622SMatthias Ringwald /* API_START */
95*82636622SMatthias Ringwald 
96*82636622SMatthias Ringwald /**
97*82636622SMatthias Ringwald  * @brief Init main run loop. Must be called before any other run loop call.
98*82636622SMatthias Ringwald  *
99*82636622SMatthias Ringwald  * Use run_loop_$(RUN_LOOP_TYPE)_get_instance() from run_loop_$(RUN_LOOP_TYPE).h to get instance
100*82636622SMatthias Ringwald  */
101*82636622SMatthias Ringwald void run_loop_init(const run_loop_t * run_loop);
102*82636622SMatthias Ringwald 
103*82636622SMatthias Ringwald /**
104*82636622SMatthias Ringwald  * @brief Set timer based on current time in milliseconds.
105*82636622SMatthias Ringwald  */
106*82636622SMatthias Ringwald void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms);
107*82636622SMatthias Ringwald 
108*82636622SMatthias Ringwald /**
109*82636622SMatthias Ringwald  * @brief Set callback that will be executed when timer expires.
110*82636622SMatthias Ringwald  */
111*82636622SMatthias Ringwald void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts));
112*82636622SMatthias Ringwald 
113*82636622SMatthias Ringwald /**
114*82636622SMatthias Ringwald  * @brief Add/Remove timer source.
115*82636622SMatthias Ringwald  */
116*82636622SMatthias Ringwald void run_loop_add_timer(timer_source_t *timer);
117*82636622SMatthias Ringwald int  run_loop_remove_timer(timer_source_t *timer);
118*82636622SMatthias Ringwald 
119*82636622SMatthias Ringwald /**
120*82636622SMatthias Ringwald  * @brief Get current time in ms
121*82636622SMatthias Ringwald  * @note 32-bit ms counter will overflow after approx. 52 days
122*82636622SMatthias Ringwald  */
123*82636622SMatthias Ringwald uint32_t run_loop_get_time_ms(void);
124*82636622SMatthias Ringwald 
125*82636622SMatthias Ringwald /**
126*82636622SMatthias Ringwald  * @brief Set data source callback.
127*82636622SMatthias Ringwald  */
128*82636622SMatthias Ringwald void run_loop_set_data_source_handler(data_source_t *ds, int (*process)(data_source_t *_ds));
129*82636622SMatthias Ringwald 
130*82636622SMatthias Ringwald /**
131*82636622SMatthias Ringwald  * @brief Add/Remove data source.
132*82636622SMatthias Ringwald  */
133*82636622SMatthias Ringwald void run_loop_add_data_source(data_source_t *dataSource);
134*82636622SMatthias Ringwald int  run_loop_remove_data_source(data_source_t *dataSource);
135*82636622SMatthias Ringwald 
136*82636622SMatthias Ringwald /**
137*82636622SMatthias Ringwald  * @brief Execute configured run loop. This function does not return.
138*82636622SMatthias Ringwald  */
139*82636622SMatthias Ringwald void run_loop_execute(void);
140*82636622SMatthias Ringwald 
141*82636622SMatthias Ringwald /* API_END */
142*82636622SMatthias Ringwald 
143*82636622SMatthias Ringwald #if defined __cplusplus
144*82636622SMatthias Ringwald }
145*82636622SMatthias Ringwald #endif
146*82636622SMatthias Ringwald 
147*82636622SMatthias Ringwald #endif // __RUN_LOOP_H
148