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 /* 39 * run_loop.h 40 * 41 * Created by Matthias Ringwald on 6/6/09. 42 */ 43 44 #ifndef __btstack_run_loop_H 45 #define __btstack_run_loop_H 46 47 #include "btstack-config.h" 48 49 #include "btstack_linked_list.h" 50 51 #include <stdint.h> 52 53 #ifdef HAVE_TIME 54 #include <sys/time.h> 55 #endif 56 57 #if defined __cplusplus 58 extern "C" { 59 #endif 60 61 typedef struct btstack_data_source { 62 btstack_linked_item_t item; 63 int fd; // <-- file descriptor to watch or 0 64 int (*process)(struct data_source *ds); // <-- do processing 65 } btstack_data_source_t; 66 67 typedef struct btstack_timer { 68 btstack_linked_item_t item; 69 #ifdef HAVE_TIME 70 struct timeval timeout; // <-- next timeout 71 #endif 72 #if defined(HAVE_TICK) || defined(HAVE_TIME_MS) 73 uint32_t timeout; // timeout in system ticks (HAVE_TICK) or millis (HAVE_TIME_MS) 74 #endif 75 void (*process)(struct timer *ts); // <-- do processing 76 } btstack_timer_source_t; 77 78 // 79 typedef struct btstack_run_loop { 80 void (*init)(void); 81 void (*add_data_source)(btstack_data_source_t *dataSource); 82 int (*remove_data_source)(btstack_data_source_t *dataSource); 83 void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms); 84 void (*add_timer)(btstack_timer_source_t *timer); 85 int (*remove_timer)(btstack_timer_source_t *timer); 86 void (*execute)(void); 87 void (*dump_timer)(void); 88 uint32_t (*get_time_ms)(void); 89 } btstack_run_loop_t; 90 91 void btstack_run_loop_timer_dump(void); 92 93 94 /* API_START */ 95 96 /** 97 * @brief Init main run loop. Must be called before any other run loop call. 98 * 99 * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance 100 */ 101 void btstack_run_loop_init(const btstack_run_loop_t * run_loop); 102 103 /** 104 * @brief Set timer based on current time in milliseconds. 105 */ 106 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms); 107 108 /** 109 * @brief Set callback that will be executed when timer expires. 110 */ 111 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)); 112 113 /** 114 * @brief Add/Remove timer source. 115 */ 116 void btstack_run_loop_add_timer(btstack_timer_source_t *timer); 117 int btstack_run_loop_remove_timer(btstack_timer_source_t *timer); 118 119 /** 120 * @brief Get current time in ms 121 * @note 32-bit ms counter will overflow after approx. 52 days 122 */ 123 uint32_t btstack_run_loop_get_time_ms(void); 124 125 /** 126 * @brief Set data source callback. 127 */ 128 void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)); 129 130 /** 131 * @brief Add/Remove data source. 132 */ 133 void btstack_run_loop_add_data_source(btstack_data_source_t *dataSource); 134 int btstack_run_loop_remove_data_source(btstack_data_source_t *dataSource); 135 136 /** 137 * @brief Execute configured run loop. This function does not return. 138 */ 139 void btstack_run_loop_execute(void); 140 141 /* API_END */ 142 143 #if defined __cplusplus 144 } 145 #endif 146 147 #endif // __btstack_run_loop_H 148