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 #define BTSTACK_FILE__ "btstack_run_loop.c" 39 40 /* 41 * run_loop.c 42 * 43 * Created by Matthias Ringwald on 6/6/09. 44 */ 45 46 #include "btstack_run_loop.h" 47 48 #include "btstack_debug.h" 49 #include "btstack_config.h" 50 51 static const btstack_run_loop_t * the_run_loop = NULL; 52 53 extern const btstack_run_loop_t btstack_run_loop_embedded; 54 55 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 56 ts->process = process; 57 }; 58 59 void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type)){ 60 ds->process = process; 61 }; 62 63 void btstack_run_loop_set_data_source_fd(btstack_data_source_t *ds, int fd){ 64 ds->source.fd = fd; 65 } 66 67 int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds){ 68 return ds->source.fd; 69 } 70 71 void btstack_run_loop_set_data_source_handle(btstack_data_source_t *ds, void * handle){ 72 ds->source.handle = handle; 73 } 74 75 void * btstack_run_loop_get_data_source_handle(btstack_data_source_t *ds){ 76 return ds->source.handle; 77 } 78 79 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t *ds, uint16_t callbacks){ 80 btstack_assert(the_run_loop != NULL); 81 if (the_run_loop->enable_data_source_callbacks){ 82 the_run_loop->enable_data_source_callbacks(ds, callbacks); 83 } else { 84 log_error("btstack_run_loop_remove_data_source not implemented"); 85 } 86 } 87 88 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t *ds, uint16_t callbacks){ 89 btstack_assert(the_run_loop != NULL); 90 if (the_run_loop->disable_data_source_callbacks){ 91 the_run_loop->disable_data_source_callbacks(ds, callbacks); 92 } else { 93 log_error("btstack_run_loop_disable_data_source_callbacks not implemented"); 94 } 95 } 96 97 /** 98 * Add data_source to run_loop 99 */ 100 void btstack_run_loop_add_data_source(btstack_data_source_t *ds){ 101 btstack_assert(the_run_loop != NULL); 102 if (the_run_loop->add_data_source){ 103 the_run_loop->add_data_source(ds); 104 } else { 105 log_error("btstack_run_loop_add_data_source not implemented"); 106 } 107 } 108 109 /** 110 * Remove data_source from run loop 111 */ 112 int btstack_run_loop_remove_data_source(btstack_data_source_t *ds){ 113 btstack_assert(the_run_loop != NULL); 114 if (the_run_loop->remove_data_source){ 115 return the_run_loop->remove_data_source(ds); 116 } else { 117 log_error("btstack_run_loop_remove_data_source not implemented"); 118 return 0; 119 } 120 } 121 122 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 123 btstack_assert(the_run_loop != NULL); 124 the_run_loop->set_timer(a, timeout_in_ms); 125 } 126 127 /** 128 * @brief Set context for this timer 129 */ 130 void btstack_run_loop_set_timer_context(btstack_timer_source_t *ts, void * context){ 131 ts->context = context; 132 } 133 134 /** 135 * @brief Get context for this timer 136 */ 137 void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts){ 138 return ts->context; 139 } 140 141 /** 142 * Add timer to run_loop (keep list sorted) 143 */ 144 void btstack_run_loop_add_timer(btstack_timer_source_t *ts){ 145 btstack_assert(the_run_loop != NULL); 146 the_run_loop->add_timer(ts); 147 } 148 149 /** 150 * Remove timer from run loop 151 */ 152 int btstack_run_loop_remove_timer(btstack_timer_source_t *ts){ 153 btstack_assert(the_run_loop != NULL); 154 return the_run_loop->remove_timer(ts); 155 } 156 157 /** 158 * @brief Get current time in ms 159 */ 160 uint32_t btstack_run_loop_get_time_ms(void){ 161 btstack_assert(the_run_loop != NULL); 162 return the_run_loop->get_time_ms(); 163 } 164 165 166 void btstack_run_loop_timer_dump(void){ 167 btstack_assert(the_run_loop != NULL); 168 the_run_loop->dump_timer(); 169 } 170 171 /** 172 * Execute run_loop 173 */ 174 void btstack_run_loop_execute(void){ 175 btstack_assert(the_run_loop != NULL); 176 the_run_loop->execute(); 177 } 178 179 // init must be called before any other run_loop call 180 void btstack_run_loop_init(const btstack_run_loop_t * run_loop){ 181 btstack_assert(the_run_loop == NULL); 182 the_run_loop = run_loop; 183 the_run_loop->init(); 184 } 185 186