182636622SMatthias Ringwald /* 282636622SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 382636622SMatthias Ringwald * 482636622SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 582636622SMatthias Ringwald * modification, are permitted provided that the following conditions 682636622SMatthias Ringwald * are met: 782636622SMatthias Ringwald * 882636622SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 982636622SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1082636622SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1182636622SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1282636622SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1382636622SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1482636622SMatthias Ringwald * contributors may be used to endorse or promote products derived 1582636622SMatthias Ringwald * from this software without specific prior written permission. 1682636622SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1782636622SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1882636622SMatthias Ringwald * monetary gain. 1982636622SMatthias Ringwald * 2082636622SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2182636622SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2282636622SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2382636622SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2482636622SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2582636622SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2682636622SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2782636622SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2882636622SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2982636622SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3082636622SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3182636622SMatthias Ringwald * SUCH DAMAGE. 3282636622SMatthias Ringwald * 3382636622SMatthias Ringwald * Please inquire about commercial licensing options at 3482636622SMatthias Ringwald * [email protected] 3582636622SMatthias Ringwald * 3682636622SMatthias Ringwald */ 3782636622SMatthias Ringwald 3882636622SMatthias Ringwald /* 3982636622SMatthias Ringwald * run_loop.h 4082636622SMatthias Ringwald * 4182636622SMatthias Ringwald * Created by Matthias Ringwald on 6/6/09. 4282636622SMatthias Ringwald */ 4382636622SMatthias Ringwald 44528a4a3bSMatthias Ringwald #ifndef __btstack_run_loop_H 45528a4a3bSMatthias Ringwald #define __btstack_run_loop_H 4682636622SMatthias Ringwald 477907f069SMatthias Ringwald #include "btstack_config.h" 4882636622SMatthias Ringwald 4982636622SMatthias Ringwald #include "btstack_linked_list.h" 5082636622SMatthias Ringwald 5182636622SMatthias Ringwald #include <stdint.h> 5282636622SMatthias Ringwald 5382636622SMatthias Ringwald #ifdef HAVE_TIME 5482636622SMatthias Ringwald #include <sys/time.h> 5582636622SMatthias Ringwald #endif 5682636622SMatthias Ringwald 5782636622SMatthias Ringwald #if defined __cplusplus 5882636622SMatthias Ringwald extern "C" { 5982636622SMatthias Ringwald #endif 6082636622SMatthias Ringwald 61ec820d77SMatthias Ringwald typedef struct btstack_data_source { 625ed06181SMatthias Ringwald // 6382636622SMatthias Ringwald btstack_linked_item_t item; 645ed06181SMatthias Ringwald // file descriptor to watch for run loops that support file descriptors 655ed06181SMatthias Ringwald int fd; 665ed06181SMatthias Ringwald // callback to call for enabled callback types 675ed06181SMatthias Ringwald int (*process)(struct btstack_data_source *ds); 685ed06181SMatthias Ringwald // flags storing enabled callback types 695ed06181SMatthias Ringwald uint16_t flags; 70ec820d77SMatthias Ringwald } btstack_data_source_t; 7182636622SMatthias Ringwald 72eb886013SMatthias Ringwald typedef struct btstack_timer_source { 7382636622SMatthias Ringwald btstack_linked_item_t item; 7482636622SMatthias Ringwald #ifdef HAVE_TIME 7582636622SMatthias Ringwald struct timeval timeout; // <-- next timeout 7682636622SMatthias Ringwald #endif 7782636622SMatthias Ringwald #if defined(HAVE_TICK) || defined(HAVE_TIME_MS) 7882636622SMatthias Ringwald uint32_t timeout; // timeout in system ticks (HAVE_TICK) or millis (HAVE_TIME_MS) 7982636622SMatthias Ringwald #endif 80fd939756SMatthias Ringwald // will be called when timer fired 81fd939756SMatthias Ringwald void (*process)(struct btstack_timer_source *ts); 82fd939756SMatthias Ringwald void * context; 83ec820d77SMatthias Ringwald } btstack_timer_source_t; 8482636622SMatthias Ringwald 8582636622SMatthias Ringwald // 86ec820d77SMatthias Ringwald typedef struct btstack_run_loop { 8782636622SMatthias Ringwald void (*init)(void); 88ec820d77SMatthias Ringwald void (*add_data_source)(btstack_data_source_t *dataSource); 89ec820d77SMatthias Ringwald int (*remove_data_source)(btstack_data_source_t *dataSource); 90ec820d77SMatthias Ringwald void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms); 91ec820d77SMatthias Ringwald void (*add_timer)(btstack_timer_source_t *timer); 92ec820d77SMatthias Ringwald int (*remove_timer)(btstack_timer_source_t *timer); 9382636622SMatthias Ringwald void (*execute)(void); 9482636622SMatthias Ringwald void (*dump_timer)(void); 9582636622SMatthias Ringwald uint32_t (*get_time_ms)(void); 96528a4a3bSMatthias Ringwald } btstack_run_loop_t; 9782636622SMatthias Ringwald 98528a4a3bSMatthias Ringwald void btstack_run_loop_timer_dump(void); 9982636622SMatthias Ringwald 10082636622SMatthias Ringwald /* API_START */ 10182636622SMatthias Ringwald 10282636622SMatthias Ringwald /** 1035ed06181SMatthias Ringwald * possible callback types for run loop data sources 1045ed06181SMatthias Ringwald */ 1055ed06181SMatthias Ringwald typedef enum { 1065ed06181SMatthias Ringwald DATA_SOURCE_CALLBACK_POLL = 1 << 0, 1075ed06181SMatthias Ringwald DATA_SOURCE_CALLBACK_READ = 1 << 1, 1085ed06181SMatthias Ringwald DATA_SOURCE_CALLBACK_WRITE = 1 << 2, 1095ed06181SMatthias Ringwald } btstack_data_source_callback_type_t; 1105ed06181SMatthias Ringwald 1115ed06181SMatthias Ringwald /** 11282636622SMatthias Ringwald * @brief Init main run loop. Must be called before any other run loop call. 11382636622SMatthias Ringwald * 114528a4a3bSMatthias Ringwald * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance 11582636622SMatthias Ringwald */ 116528a4a3bSMatthias Ringwald void btstack_run_loop_init(const btstack_run_loop_t * run_loop); 11782636622SMatthias Ringwald 11882636622SMatthias Ringwald /** 11982636622SMatthias Ringwald * @brief Set timer based on current time in milliseconds. 12082636622SMatthias Ringwald */ 121ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms); 12282636622SMatthias Ringwald 12382636622SMatthias Ringwald /** 12482636622SMatthias Ringwald * @brief Set callback that will be executed when timer expires. 12582636622SMatthias Ringwald */ 126ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)); 12782636622SMatthias Ringwald 12882636622SMatthias Ringwald /** 129fd939756SMatthias Ringwald * @brief Set context for this timer 130fd939756SMatthias Ringwald */ 131fd939756SMatthias Ringwald void btstack_run_loop_set_timer_context(btstack_timer_source_t *ts, void * context); 132fd939756SMatthias Ringwald 133fd939756SMatthias Ringwald /** 134fd939756SMatthias Ringwald * @brief Get context for this timer 135fd939756SMatthias Ringwald */ 136fd939756SMatthias Ringwald void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts); 137fd939756SMatthias Ringwald 138fd939756SMatthias Ringwald /** 139fd939756SMatthias Ringwald * @brief Add timer source. 14082636622SMatthias Ringwald */ 141ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer); 142fd939756SMatthias Ringwald 143fd939756SMatthias Ringwald /** 144fd939756SMatthias Ringwald * @brief Remove timer source. 145fd939756SMatthias Ringwald */ 146ec820d77SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *timer); 14782636622SMatthias Ringwald 14882636622SMatthias Ringwald /** 14982636622SMatthias Ringwald * @brief Get current time in ms 15082636622SMatthias Ringwald * @note 32-bit ms counter will overflow after approx. 52 days 15182636622SMatthias Ringwald */ 152528a4a3bSMatthias Ringwald uint32_t btstack_run_loop_get_time_ms(void); 15382636622SMatthias Ringwald 15482636622SMatthias Ringwald /** 15582636622SMatthias Ringwald * @brief Set data source callback. 15682636622SMatthias Ringwald */ 157ec820d77SMatthias Ringwald void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)); 15882636622SMatthias Ringwald 15982636622SMatthias Ringwald /** 160*3a5c43eeSMatthias Ringwald * @brief Set data source file descriptor. 161*3a5c43eeSMatthias Ringwald * @note No effect if port doensn't have file descriptors 162*3a5c43eeSMatthias Ringwald */ 163*3a5c43eeSMatthias Ringwald void btstack_run_loop_set_data_source_fd(btstack_data_source_t *ds, int fd); 164*3a5c43eeSMatthias Ringwald 165*3a5c43eeSMatthias Ringwald /** 166*3a5c43eeSMatthias Ringwald * @brief Get data source file descriptor. 167*3a5c43eeSMatthias Ringwald */ 168*3a5c43eeSMatthias Ringwald int btstack_run_loop_get_data_source_fd(btstack_data_source_t *ds); 169*3a5c43eeSMatthias Ringwald 170*3a5c43eeSMatthias Ringwald /** 17182636622SMatthias Ringwald * @brief Add/Remove data source. 17282636622SMatthias Ringwald */ 173ec820d77SMatthias Ringwald void btstack_run_loop_add_data_source(btstack_data_source_t *dataSource); 174ec820d77SMatthias Ringwald int btstack_run_loop_remove_data_source(btstack_data_source_t *dataSource); 17582636622SMatthias Ringwald 17682636622SMatthias Ringwald /** 17782636622SMatthias Ringwald * @brief Execute configured run loop. This function does not return. 17882636622SMatthias Ringwald */ 179528a4a3bSMatthias Ringwald void btstack_run_loop_execute(void); 18082636622SMatthias Ringwald 18182636622SMatthias Ringwald /* API_END */ 18282636622SMatthias Ringwald 18382636622SMatthias Ringwald #if defined __cplusplus 18482636622SMatthias Ringwald } 18582636622SMatthias Ringwald #endif 18682636622SMatthias Ringwald 187528a4a3bSMatthias Ringwald #endif // __btstack_run_loop_H 188