xref: /btstack/src/btstack_run_loop_base.c (revision 50b47281dcd786f25b8e20527b26e5aaa0460809)
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 #define BTSTACK_FILE__ "btstack_run_loop_base.c"
39 
40 /*
41  *  btstack_run_loop_base.h
42  *
43  *  Portable implementation of timer and data source management as base for platform specific implementations
44  */
45 
46 #include "btstack_debug.h"
47 #include "btstack_config.h"
48 #include "btstack_util.h"
49 
50 #include "btstack_run_loop_base.h"
51 
52 // private data (access only by run loop implementations)
53 btstack_linked_list_t btstack_run_loop_base_timers;
54 btstack_linked_list_t btstack_run_loop_base_data_sources;
55 
56 void btstack_run_loop_base_init(void){
57     btstack_run_loop_base_timers = NULL;
58     btstack_run_loop_base_data_sources = NULL;
59 }
60 
61 void btstack_run_loop_base_add_data_source(btstack_data_source_t *ds){
62     btstack_linked_list_add(&btstack_run_loop_base_data_sources, (btstack_linked_item_t *) ds);
63 }
64 
65 bool btstack_run_loop_base_remove_data_source(btstack_data_source_t *ds){
66     return btstack_linked_list_remove(&btstack_run_loop_base_data_sources, (btstack_linked_item_t *) ds);
67 }
68 
69 void btstack_run_loop_base_enable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
70     ds->flags |= callback_types;
71 }
72 
73 void btstack_run_loop_base_disable_data_source_callbacks(btstack_data_source_t * ds, uint16_t callback_types){
74     ds->flags &= ~callback_types;
75 }
76 
77 bool btstack_run_loop_base_remove_timer(btstack_timer_source_t *ts){
78     return btstack_linked_list_remove(&btstack_run_loop_base_timers, (btstack_linked_item_t *) ts);
79 }
80 
81 void btstack_run_loop_base_add_timer(btstack_timer_source_t *ts){
82     btstack_linked_item_t *it;
83     for (it = (btstack_linked_item_t *) &btstack_run_loop_base_timers; it->next ; it = it->next){
84         btstack_timer_source_t * next = (btstack_timer_source_t *) it->next;
85         btstack_assert(next != ts);
86         int32_t delta = btstack_time_delta(ts->timeout, next->timeout);
87         if (delta < 0) break;
88     }
89     ts->item.next = it->next;
90     it->next = (btstack_linked_item_t *) ts;
91 }
92 
93 void btstack_run_loop_base_process_timers(uint32_t now){
94     // process timers, exit when timeout is in the future
95     while (btstack_run_loop_base_timers) {
96         btstack_timer_source_t * ts = (btstack_timer_source_t *) btstack_run_loop_base_timers;
97         int32_t delta = btstack_time_delta(ts->timeout, now);
98         if (delta > 0) break;
99         btstack_run_loop_base_remove_timer(ts);
100         ts->process(ts);
101     }
102 }
103 
104 void btstack_run_loop_base_dump_timer(void){
105 #ifdef ENABLE_LOG_INFO
106     btstack_linked_item_t *it;
107     uint16_t i = 0;
108     for (it = (btstack_linked_item_t *) btstack_run_loop_base_timers; it ; it = it->next){
109         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
110         log_info("timer %u (%p): timeout %u\n", i, ts, ts->timeout);
111     }
112 #endif
113 
114 }
115 /**
116  * @brief Get time until first timer fires
117  * @returns -1 if no timers, time until next timeout otherwise
118  */
119 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now){
120     if (btstack_run_loop_base_timers == NULL) return -1;
121     btstack_timer_source_t * ts = (btstack_timer_source_t *) btstack_run_loop_base_timers;
122     uint32_t list_timeout  = ts->timeout;
123     int32_t delta = btstack_time_delta(list_timeout, now);
124     if (delta < 0){
125         delta = 0;
126     }
127     return delta;
128 }
129 
130 void btstack_run_loop_base_poll_data_sources(void){
131     // poll data sources
132     btstack_data_source_t *ds;
133     btstack_data_source_t *next;
134     for (ds = (btstack_data_source_t *) btstack_run_loop_base_data_sources; ds != NULL ; ds = next){
135         next = (btstack_data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
136         if (ds->flags & DATA_SOURCE_CALLBACK_POLL){
137             ds->process(ds, DATA_SOURCE_CALLBACK_POLL);
138         }
139     }
140 }
141