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 managment 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 78 bool btstack_run_loop_base_remove_timer(btstack_timer_source_t *ts){ 79 return btstack_linked_list_remove(&btstack_run_loop_base_timers, (btstack_linked_item_t *) ts); 80 } 81 82 void btstack_run_loop_base_add_timer(btstack_timer_source_t *ts){ 83 btstack_linked_item_t *it; 84 for (it = (btstack_linked_item_t *) &btstack_run_loop_base_timers; it->next ; it = it->next){ 85 // don't add timer that's already in there 86 if ((btstack_timer_source_t *) it->next == ts){ 87 log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); 88 return; 89 } 90 // exit if list timeout is after new timeout 91 uint32_t list_timeout = ((btstack_timer_source_t *) it->next)->timeout; 92 int32_t delta = btstack_time_delta(ts->timeout, list_timeout); 93 if (delta < 0) break; 94 } 95 ts->item.next = it->next; 96 it->next = (btstack_linked_item_t *) ts; 97 } 98 99 void btstack_run_loop_base_process_timers(uint32_t now){ 100 // process timers, exit when timeout is in the future 101 while (btstack_run_loop_base_timers) { 102 btstack_timer_source_t * ts = (btstack_timer_source_t *) btstack_run_loop_base_timers; 103 int32_t delta = btstack_time_delta(ts->timeout, now); 104 if (delta > 0) break; 105 btstack_run_loop_base_remove_timer(ts); 106 ts->process(ts); 107 } 108 } 109 110 /** 111 * @brief Get time until first timer fires 112 * @returns -1 if no timers, time until next timeout otherwise 113 */ 114 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now){ 115 if (btstack_run_loop_base_timers == NULL) return -1; 116 btstack_timer_source_t * ts = (btstack_timer_source_t *) btstack_run_loop_base_timers; 117 uint32_t list_timeout = ts->timeout; 118 int32_t delta = btstack_time_delta(list_timeout, now); 119 if (delta < 0){ 120 delta = 0; 121 } 122 return delta; 123 } 124