xref: /btstack/src/btstack_run_loop_base.c (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 #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         // don't add timer that's already in there
85         if ((btstack_timer_source_t *) it->next == ts){
86             log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
87             return;
88         }
89         // exit if list timeout is after new timeout
90         uint32_t list_timeout = ((btstack_timer_source_t *) it->next)->timeout;
91         int32_t delta = btstack_time_delta(ts->timeout, list_timeout);
92         if (delta < 0) break;
93     }
94     ts->item.next = it->next;
95     it->next = (btstack_linked_item_t *) ts;
96 }
97 
98 void btstack_run_loop_base_process_timers(uint32_t now){
99     // process timers, exit when timeout is in the future
100     while (btstack_run_loop_base_timers) {
101         btstack_timer_source_t * ts = (btstack_timer_source_t *) btstack_run_loop_base_timers;
102         int32_t delta = btstack_time_delta(ts->timeout, now);
103         if (delta > 0) break;
104         btstack_run_loop_base_remove_timer(ts);
105         ts->process(ts);
106     }
107 }
108 
109 void btstack_run_loop_base_dump_timer(void){
110 #ifdef ENABLE_LOG_INFO
111     btstack_linked_item_t *it;
112     uint16_t i = 0;
113     for (it = (btstack_linked_item_t *) btstack_run_loop_base_timers; it ; it = it->next){
114         btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
115         log_info("timer %u (%p): timeout %u\n", i, ts, ts->timeout);
116     }
117 #endif
118 
119 }
120 /**
121  * @brief Get time until first timer fires
122  * @returns -1 if no timers, time until next timeout otherwise
123  */
124 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now){
125     if (btstack_run_loop_base_timers == NULL) return -1;
126     btstack_timer_source_t * ts = (btstack_timer_source_t *) btstack_run_loop_base_timers;
127     uint32_t list_timeout  = ts->timeout;
128     int32_t delta = btstack_time_delta(list_timeout, now);
129     if (delta < 0){
130         delta = 0;
131     }
132     return delta;
133 }
134