xref: /btstack/src/btstack_run_loop.h (revision 24ced5a6f682f147f6a647d60c96f7528a8a2378)
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 #ifdef HAVE_TIME
54 #include <sys/time.h>
55 #endif
56 
57 #if defined __cplusplus
58 extern "C" {
59 #endif
60 
61 
62 /**
63  * Callback types for run loop data sources
64  */
65 typedef enum {
66 	DATA_SOURCE_CALLBACK_POLL  = 1 << 0,
67 	DATA_SOURCE_CALLBACK_READ  = 1 << 1,
68 	DATA_SOURCE_CALLBACK_WRITE = 1 << 2,
69 } btstack_data_source_callback_type_t;
70 
71 typedef struct btstack_data_source {
72 	//
73     btstack_linked_item_t item;
74     // file descriptor to watch for run loops that support file descriptors
75     int  fd;
76     // callback to call for enabled callback types
77     void  (*process)(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type);
78     // flags storing enabled callback types
79     uint16_t flags;
80 } btstack_data_source_t;
81 
82 typedef struct btstack_timer_source {
83     btstack_linked_item_t item;
84 #ifdef HAVE_TIME
85     struct timeval timeout;                  // <-- next timeout
86 #endif
87 #if defined(HAVE_TICK) || defined(HAVE_TIME_MS)
88     uint32_t timeout;                       // timeout in system ticks (HAVE_TICK) or millis (HAVE_TIME_MS)
89 #endif
90     // will be called when timer fired
91     void  (*process)(struct btstack_timer_source *ts);
92     void * context;
93 } btstack_timer_source_t;
94 
95 typedef struct btstack_run_loop {
96 	void (*init)(void);
97 	void (*add_data_source)(btstack_data_source_t *dataSource);
98 	int  (*remove_data_source)(btstack_data_source_t *dataSource);
99 	void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms);
100 	void (*add_timer)(btstack_timer_source_t *timer);
101 	int  (*remove_timer)(btstack_timer_source_t *timer);
102 	void (*execute)(void);
103 	void (*dump_timer)(void);
104 	uint32_t (*get_time_ms)(void);
105 } btstack_run_loop_t;
106 
107 void btstack_run_loop_timer_dump(void);
108 
109 /* API_START */
110 
111 /**
112  * @brief Init main run loop. Must be called before any other run loop call.
113  *
114  * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance
115  */
116 void btstack_run_loop_init(const btstack_run_loop_t * run_loop);
117 
118 /**
119  * @brief Set timer based on current time in milliseconds.
120  */
121 void btstack_run_loop_set_timer(btstack_timer_source_t * ts, uint32_t timeout_in_ms);
122 
123 /**
124  * @brief Set callback that will be executed when timer expires.
125  */
126 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts, void (*process)(btstack_timer_source_t *_ts));
127 
128 /**
129  * @brief Set context for this timer
130  */
131 void btstack_run_loop_set_timer_context(btstack_timer_source_t * ts, void * context);
132 
133 /**
134  * @brief Get context for this timer
135  */
136 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * ts);
137 
138 /**
139  * @brief Add timer source.
140  */
141 void btstack_run_loop_add_timer(btstack_timer_source_t * timer);
142 
143 /**
144  * @brief Remove timer source.
145  */
146 int  btstack_run_loop_remove_timer(btstack_timer_source_t * timer);
147 
148 /**
149  * @brief Get current time in ms
150  * @note 32-bit ms counter will overflow after approx. 52 days
151  */
152 uint32_t btstack_run_loop_get_time_ms(void);
153 
154 /**
155  * @brief Set data source callback.
156  */
157 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));
158 
159 /**
160  * @brief Set data source file descriptor.
161  * @param data_source
162  * @param fd file descriptor
163  * @note No effect if port doensn't have file descriptors
164  */
165 void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd);
166 
167 /**
168  * @brief Get data source file descriptor.
169  * @param data_source
170  */
171 int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source);
172 
173 /**
174  * @brief Enable callbacks for a data source
175  * @param data_source to remove
176  * @param callback types to enable
177  */
178 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
179 
180 /**
181  * @brief Enable callbacks for a data source
182  * @param data_source to remove
183  * @param callback types to disable
184  */
185 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
186 
187 /**
188  * @brief Add data source to run loop
189  * @param data_source to add
190  */
191 void btstack_run_loop_add_data_source(btstack_data_source_t * data_source);
192 
193 /**
194  * @brief Remove data source from run loop
195  * @param data_source to remove
196  */
197 int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source);
198 
199 /**
200  * @brief Execute configured run loop. This function does not return.
201  */
202 void btstack_run_loop_execute(void);
203 
204 /* API_END */
205 
206 #if defined __cplusplus
207 }
208 #endif
209 
210 #endif // __btstack_run_loop_H
211