xref: /btstack/src/btstack_run_loop.c (revision 2044a9b9ae072d40605300dae349cd05c5194e66)
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.c
40  *
41  *  Created by Matthias Ringwald on 6/6/09.
42  */
43 
44 #include "btstack_run_loop.h"
45 
46 #include <stdio.h>
47 #include <stdlib.h>  // exit()
48 
49 
50 #include "btstack_debug.h"
51 #include "btstack_config.h"
52 
53 static const btstack_run_loop_t * the_run_loop = NULL;
54 
55 extern const btstack_run_loop_t btstack_run_loop_embedded;
56 
57 // assert run loop initialized
58 static void btstack_run_loop_assert(void){
59     if (!the_run_loop){
60         log_error("ERROR: run_loop function called before btstack_run_loop_init!");
61         while(1);
62     }
63 }
64 
65 
66 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
67     ts->process = process;
68 };
69 
70 void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)){
71     ds->process = process;
72 };
73 
74 
75 /**
76  * Add data_source to run_loop
77  */
78 void btstack_run_loop_add_data_source(btstack_data_source_t *ds){
79     btstack_run_loop_assert();
80     if (the_run_loop->add_data_source){
81         the_run_loop->add_data_source(ds);
82     } else {
83         log_error("btstack_run_loop_add_data_source not implemented");
84     }
85 }
86 
87 /**
88  * Remove data_source from run loop
89  */
90 int btstack_run_loop_remove_data_source(btstack_data_source_t *ds){
91     btstack_run_loop_assert();
92     if (the_run_loop->remove_data_source){
93         return the_run_loop->remove_data_source(ds);
94     } else {
95         log_error("btstack_run_loop_remove_data_source not implemented");
96         return 0;
97     }
98 }
99 
100 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
101     btstack_run_loop_assert();
102     the_run_loop->set_timer(a, timeout_in_ms);
103 }
104 
105 /**
106  * Add timer to run_loop (keep list sorted)
107  */
108 void btstack_run_loop_add_timer(btstack_timer_source_t *ts){
109     btstack_run_loop_assert();
110     the_run_loop->add_timer(ts);
111 }
112 
113 /**
114  * Remove timer from run loop
115  */
116 int btstack_run_loop_remove_timer(btstack_timer_source_t *ts){
117     btstack_run_loop_assert();
118     return the_run_loop->remove_timer(ts);
119 }
120 
121 /**
122  * @brief Get current time in ms
123  */
124 uint32_t btstack_run_loop_get_time_ms(void){
125     btstack_run_loop_assert();
126     return the_run_loop->get_time_ms();
127 }
128 
129 
130 void btstack_run_loop_timer_dump(void){
131     btstack_run_loop_assert();
132     the_run_loop->dump_timer();
133 }
134 
135 /**
136  * Execute run_loop
137  */
138 void btstack_run_loop_execute(void){
139     btstack_run_loop_assert();
140     the_run_loop->execute();
141 }
142 
143 // init must be called before any other run_loop call
144 void btstack_run_loop_init(const btstack_run_loop_t * run_loop){
145     if (the_run_loop){
146         log_error("ERROR: run loop initialized twice!");
147         while(1);
148     }
149     the_run_loop = run_loop;
150     the_run_loop->init();
151 }
152 
153