xref: /btstack/src/btstack_run_loop.h (revision f12924e0a61c0582b548a1e70cea1bd59ba21f1d)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  run_loop.h
40  *
41  *  Created by Matthias Ringwald on 6/6/09.
42  */
43 
44 #ifndef __btstack_run_loop_H
45 #define __btstack_run_loop_H
46 
47 #include "btstack_config.h"
48 
49 #include "btstack_linked_list.h"
50 
51 #include <stdint.h>
52 
53 #if defined __cplusplus
54 extern "C" {
55 #endif
56 
57 
58 /**
59  * Callback types for run loop data sources
60  */
61 typedef enum {
62 	DATA_SOURCE_CALLBACK_POLL  = 1 << 0,
63 	DATA_SOURCE_CALLBACK_READ  = 1 << 1,
64 	DATA_SOURCE_CALLBACK_WRITE = 1 << 2,
65 } btstack_data_source_callback_type_t;
66 
67 typedef struct btstack_data_source {
68 	// linked item
69     btstack_linked_item_t item;
70 
71     // item to watch in run loop
72     union {
73 	    // file descriptor for posix systems
74 	    int  fd;
75     	// handle on windows
76     	void * handle;
77     };
78 
79     // callback to call for enabled callback types
80     void  (*process)(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type);
81 
82     // flags storing enabled callback types
83     uint16_t flags;
84 
85 } btstack_data_source_t;
86 
87 typedef struct btstack_timer_source {
88     btstack_linked_item_t item;
89     // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS)
90     uint32_t timeout;
91     // will be called when timer fired
92     void  (*process)(struct btstack_timer_source *ts);
93     void * context;
94 } btstack_timer_source_t;
95 
96 typedef struct btstack_run_loop {
97 	void (*init)(void);
98 	void (*add_data_source)(btstack_data_source_t * data_source);
99 	int  (*remove_data_source)(btstack_data_source_t * data_source);
100 	void (*enable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
101 	void (*disable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
102 	void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms);
103 	void (*add_timer)(btstack_timer_source_t *timer);
104 	int  (*remove_timer)(btstack_timer_source_t *timer);
105 	void (*execute)(void);
106 	void (*dump_timer)(void);
107 	uint32_t (*get_time_ms)(void);
108 } btstack_run_loop_t;
109 
110 void btstack_run_loop_timer_dump(void);
111 
112 /* API_START */
113 
114 /**
115  * @brief Init main run loop. Must be called before any other run loop call.
116  *
117  * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance
118  */
119 void btstack_run_loop_init(const btstack_run_loop_t * run_loop);
120 
121 /**
122  * @brief Set timer based on current time in milliseconds.
123  */
124 void btstack_run_loop_set_timer(btstack_timer_source_t * ts, uint32_t timeout_in_ms);
125 
126 /**
127  * @brief Set callback that will be executed when timer expires.
128  */
129 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts, void (*process)(btstack_timer_source_t *_ts));
130 
131 /**
132  * @brief Set context for this timer
133  */
134 void btstack_run_loop_set_timer_context(btstack_timer_source_t * ts, void * context);
135 
136 /**
137  * @brief Get context for this timer
138  */
139 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * ts);
140 
141 /**
142  * @brief Add timer source.
143  */
144 void btstack_run_loop_add_timer(btstack_timer_source_t * timer);
145 
146 /**
147  * @brief Remove timer source.
148  */
149 int  btstack_run_loop_remove_timer(btstack_timer_source_t * timer);
150 
151 /**
152  * @brief Get current time in ms
153  * @note 32-bit ms counter will overflow after approx. 52 days
154  */
155 uint32_t btstack_run_loop_get_time_ms(void);
156 
157 /**
158  * @brief Set data source callback.
159  */
160 void btstack_run_loop_set_data_source_handler(btstack_data_source_t * data_source, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type));
161 
162 /**
163  * @brief Set data source file descriptor.
164  * @param data_source
165  * @param fd file descriptor
166  * @note No effect if port doensn't have file descriptors
167  */
168 void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd);
169 
170 /**
171  * @brief Get data source file descriptor.
172  * @param data_source
173  */
174 int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source);
175 
176 /**
177  * @brief Enable callbacks for a data source
178  * @param data_source to remove
179  * @param callback types to enable
180  */
181 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
182 
183 /**
184  * @brief Enable callbacks for a data source
185  * @param data_source to remove
186  * @param callback types to disable
187  */
188 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
189 
190 /**
191  * @brief Add data source to run loop
192  * @param data_source to add
193  */
194 void btstack_run_loop_add_data_source(btstack_data_source_t * data_source);
195 
196 /**
197  * @brief Remove data source from run loop
198  * @param data_source to remove
199  */
200 int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source);
201 
202 /**
203  * @brief Execute configured run loop. This function does not return.
204  */
205 void btstack_run_loop_execute(void);
206 
207 /* API_END */
208 
209 #if defined __cplusplus
210 }
211 #endif
212 
213 #endif // __btstack_run_loop_H
214