xref: /btstack/src/btstack_run_loop.c (revision a06bcae0f7f63d9d69b2f846d04300ea7058ea66)
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 #define __BTSTACK_FILE__ "btstack_run_loop.c"
39 
40 /*
41  *  run_loop.c
42  *
43  *  Created by Matthias Ringwald on 6/6/09.
44  */
45 
46 #include "btstack_run_loop.h"
47 
48 #include <stdio.h>
49 #include <stdlib.h>  // exit()
50 
51 
52 #include "btstack_debug.h"
53 #include "btstack_config.h"
54 
55 static const btstack_run_loop_t * the_run_loop = NULL;
56 
57 extern const btstack_run_loop_t btstack_run_loop_embedded;
58 
59 // assert run loop initialized
60 static void btstack_run_loop_assert(void){
61     if (!the_run_loop){
62         log_error("ERROR: run_loop function called before btstack_run_loop_init!");
63         while(1);
64     }
65 }
66 
67 
68 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
69     ts->process = process;
70 };
71 
72 void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, void (*process)(btstack_data_source_t *_ds,  btstack_data_source_callback_type_t callback_type)){
73     ds->process = process;
74 };
75 
76 void btstack_run_loop_set_data_source_fd(btstack_data_source_t *ds, int fd){
77     ds->fd = fd;
78 }
79 
80 int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds){
81     return ds->fd;
82 }
83 
84 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t *ds, uint16_t callbacks){
85     btstack_run_loop_assert();
86     if (the_run_loop->enable_data_source_callbacks){
87         the_run_loop->enable_data_source_callbacks(ds, callbacks);
88     } else {
89         log_error("btstack_run_loop_remove_data_source not implemented");
90     }
91 }
92 
93 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t *ds, uint16_t callbacks){
94     btstack_run_loop_assert();
95     if (the_run_loop->disable_data_source_callbacks){
96         the_run_loop->disable_data_source_callbacks(ds, callbacks);
97     } else {
98         log_error("btstack_run_loop_disable_data_source_callbacks not implemented");
99     }
100 }
101 
102 /**
103  * Add data_source to run_loop
104  */
105 void btstack_run_loop_add_data_source(btstack_data_source_t *ds){
106     btstack_run_loop_assert();
107     if (the_run_loop->add_data_source){
108         the_run_loop->add_data_source(ds);
109     } else {
110         log_error("btstack_run_loop_add_data_source not implemented");
111     }
112 }
113 
114 /**
115  * Remove data_source from run loop
116  */
117 int btstack_run_loop_remove_data_source(btstack_data_source_t *ds){
118     btstack_run_loop_assert();
119     if (the_run_loop->remove_data_source){
120         return the_run_loop->remove_data_source(ds);
121     } else {
122         log_error("btstack_run_loop_remove_data_source not implemented");
123         return 0;
124     }
125 }
126 
127 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
128     btstack_run_loop_assert();
129     the_run_loop->set_timer(a, timeout_in_ms);
130 }
131 
132 /**
133  * @brief Set context for this timer
134  */
135 void btstack_run_loop_set_timer_context(btstack_timer_source_t *ts, void * context){
136     ts->context = context;
137 }
138 
139 /**
140  * @brief Get context for this timer
141  */
142 void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts){
143     return ts->context;
144 }
145 
146 /**
147  * Add timer to run_loop (keep list sorted)
148  */
149 void btstack_run_loop_add_timer(btstack_timer_source_t *ts){
150     btstack_run_loop_assert();
151     the_run_loop->add_timer(ts);
152 }
153 
154 /**
155  * Remove timer from run loop
156  */
157 int btstack_run_loop_remove_timer(btstack_timer_source_t *ts){
158     btstack_run_loop_assert();
159     return the_run_loop->remove_timer(ts);
160 }
161 
162 /**
163  * @brief Get current time in ms
164  */
165 uint32_t btstack_run_loop_get_time_ms(void){
166     btstack_run_loop_assert();
167     return the_run_loop->get_time_ms();
168 }
169 
170 
171 void btstack_run_loop_timer_dump(void){
172     btstack_run_loop_assert();
173     the_run_loop->dump_timer();
174 }
175 
176 /**
177  * Execute run_loop
178  */
179 void btstack_run_loop_execute(void){
180     btstack_run_loop_assert();
181     the_run_loop->execute();
182 }
183 
184 // init must be called before any other run_loop call
185 void btstack_run_loop_init(const btstack_run_loop_t * run_loop){
186     if (the_run_loop){
187         log_error("ERROR: run loop initialized twice!");
188         while(1);
189     }
190     the_run_loop = run_loop;
191     the_run_loop->init();
192 }
193 
194