xref: /btstack/src/btstack_run_loop_base.h (revision a8d51f092f1b660d0f6921369ad2bc3f9368296c)
1 /*
2  * Copyright (C) 2019 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  *  btstack_run_loop_base.h
40  *
41  *  Portable implementation of timer and data source managment as base for platform specific implementations
42  */
43 
44 #ifndef BTSTACK_RUN_LOOP_BASE_H
45 #define BTSTACK_RUN_LOOP_BASE_H
46 
47 #include "btstack_run_loop.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 // private data (access only by run loop implementations)
58 extern btstack_linked_list_t btstack_run_loop_base_timers;
59 extern btstack_linked_list_t btstack_run_loop_base_data_sources;
60 
61 /**
62  * @brief Init
63  */
64 void btstack_run_loop_base_init(void);
65 
66 /**
67  * @brief Add timer source.
68  * @param timer to add
69  */
70 void btstack_run_loop_base_add_timer(btstack_timer_source_t * timer);
71 
72 /**
73  * @brief Remove timer source.
74  * @param timer to remove
75  * @returns true if timer was removed
76  */
77 bool  btstack_run_loop_base_remove_timer(btstack_timer_source_t * timer);
78 
79 /**
80  * @brief Process timers: remove expired timers from list and call their process function
81  * @param now
82  */
83 void  btstack_run_loop_base_process_timers(uint32_t now);
84 
85 /**
86  * @brief Dump list of timers via log_info
87  */
88 void btstack_run_loop_base_dump_timer(void);
89 
90 /**
91  * @brief Get time until first timer fires
92  * @returns -1 if no timers, time until next timeout otherwise
93  */
94 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now);
95 
96 /**
97  * @brief Add data source to run loop
98  * @param data_source to add
99  */
100 void btstack_run_loop_base_add_data_source(btstack_data_source_t * data_source);
101 
102 /**
103  * @brief Remove data source from run loop
104  * @param data_source to remove
105  * @returns true if data srouce was removed
106  */
107 bool btstack_run_loop_base_remove_data_source(btstack_data_source_t * data_source);
108 
109 /**
110  * @brief Enable callbacks for a data source
111  * @param data_source to remove
112  * @param callback types to enable
113  */
114 void btstack_run_loop_base_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
115 
116 /**
117  * @brief Enable callbacks for a data source
118  * @param data_source to remove
119  * @param callback types to disable
120  */
121 void btstack_run_loop_base_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
122 
123 #if defined __cplusplus
124 }
125 #endif
126 
127 #endif // BTSTACK_RUN_LOOP_BASE_H
128