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 { 6282636622SMatthias Ringwald btstack_linked_item_t item; 6382636622SMatthias Ringwald int fd; // <-- file descriptor to watch or 0 64eb886013SMatthias Ringwald int (*process)(struct btstack_data_source *ds); // <-- do processing 65ec820d77SMatthias Ringwald } btstack_data_source_t; 6682636622SMatthias Ringwald 67eb886013SMatthias Ringwald typedef struct btstack_timer_source { 6882636622SMatthias Ringwald btstack_linked_item_t item; 6982636622SMatthias Ringwald #ifdef HAVE_TIME 7082636622SMatthias Ringwald struct timeval timeout; // <-- next timeout 7182636622SMatthias Ringwald #endif 7282636622SMatthias Ringwald #if defined(HAVE_TICK) || defined(HAVE_TIME_MS) 7382636622SMatthias Ringwald uint32_t timeout; // timeout in system ticks (HAVE_TICK) or millis (HAVE_TIME_MS) 7482636622SMatthias Ringwald #endif 75*fd939756SMatthias Ringwald // will be called when timer fired 76*fd939756SMatthias Ringwald void (*process)(struct btstack_timer_source *ts); 77*fd939756SMatthias Ringwald void * context; 78ec820d77SMatthias Ringwald } btstack_timer_source_t; 7982636622SMatthias Ringwald 8082636622SMatthias Ringwald // 81ec820d77SMatthias Ringwald typedef struct btstack_run_loop { 8282636622SMatthias Ringwald void (*init)(void); 83ec820d77SMatthias Ringwald void (*add_data_source)(btstack_data_source_t *dataSource); 84ec820d77SMatthias Ringwald int (*remove_data_source)(btstack_data_source_t *dataSource); 85ec820d77SMatthias Ringwald void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms); 86ec820d77SMatthias Ringwald void (*add_timer)(btstack_timer_source_t *timer); 87ec820d77SMatthias Ringwald int (*remove_timer)(btstack_timer_source_t *timer); 8882636622SMatthias Ringwald void (*execute)(void); 8982636622SMatthias Ringwald void (*dump_timer)(void); 9082636622SMatthias Ringwald uint32_t (*get_time_ms)(void); 91528a4a3bSMatthias Ringwald } btstack_run_loop_t; 9282636622SMatthias Ringwald 93528a4a3bSMatthias Ringwald void btstack_run_loop_timer_dump(void); 9482636622SMatthias Ringwald 9582636622SMatthias Ringwald 9682636622SMatthias Ringwald /* API_START */ 9782636622SMatthias Ringwald 9882636622SMatthias Ringwald /** 9982636622SMatthias Ringwald * @brief Init main run loop. Must be called before any other run loop call. 10082636622SMatthias Ringwald * 101528a4a3bSMatthias Ringwald * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance 10282636622SMatthias Ringwald */ 103528a4a3bSMatthias Ringwald void btstack_run_loop_init(const btstack_run_loop_t * run_loop); 10482636622SMatthias Ringwald 10582636622SMatthias Ringwald /** 10682636622SMatthias Ringwald * @brief Set timer based on current time in milliseconds. 10782636622SMatthias Ringwald */ 108ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms); 10982636622SMatthias Ringwald 11082636622SMatthias Ringwald /** 11182636622SMatthias Ringwald * @brief Set callback that will be executed when timer expires. 11282636622SMatthias Ringwald */ 113ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)); 11482636622SMatthias Ringwald 11582636622SMatthias Ringwald /** 116*fd939756SMatthias Ringwald * @brief Set context for this timer 117*fd939756SMatthias Ringwald */ 118*fd939756SMatthias Ringwald void btstack_run_loop_set_timer_context(btstack_timer_source_t *ts, void * context); 119*fd939756SMatthias Ringwald 120*fd939756SMatthias Ringwald /** 121*fd939756SMatthias Ringwald * @brief Get context for this timer 122*fd939756SMatthias Ringwald */ 123*fd939756SMatthias Ringwald void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts); 124*fd939756SMatthias Ringwald 125*fd939756SMatthias Ringwald /** 126*fd939756SMatthias Ringwald * @brief Add timer source. 12782636622SMatthias Ringwald */ 128ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer); 129*fd939756SMatthias Ringwald 130*fd939756SMatthias Ringwald /** 131*fd939756SMatthias Ringwald * @brief Remove timer source. 132*fd939756SMatthias Ringwald */ 133ec820d77SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *timer); 13482636622SMatthias Ringwald 13582636622SMatthias Ringwald /** 13682636622SMatthias Ringwald * @brief Get current time in ms 13782636622SMatthias Ringwald * @note 32-bit ms counter will overflow after approx. 52 days 13882636622SMatthias Ringwald */ 139528a4a3bSMatthias Ringwald uint32_t btstack_run_loop_get_time_ms(void); 14082636622SMatthias Ringwald 14182636622SMatthias Ringwald /** 14282636622SMatthias Ringwald * @brief Set data source callback. 14382636622SMatthias Ringwald */ 144ec820d77SMatthias Ringwald void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)); 14582636622SMatthias Ringwald 14682636622SMatthias Ringwald /** 14782636622SMatthias Ringwald * @brief Add/Remove data source. 14882636622SMatthias Ringwald */ 149ec820d77SMatthias Ringwald void btstack_run_loop_add_data_source(btstack_data_source_t *dataSource); 150ec820d77SMatthias Ringwald int btstack_run_loop_remove_data_source(btstack_data_source_t *dataSource); 15182636622SMatthias Ringwald 15282636622SMatthias Ringwald /** 15382636622SMatthias Ringwald * @brief Execute configured run loop. This function does not return. 15482636622SMatthias Ringwald */ 155528a4a3bSMatthias Ringwald void btstack_run_loop_execute(void); 15682636622SMatthias Ringwald 15782636622SMatthias Ringwald /* API_END */ 15882636622SMatthias Ringwald 15982636622SMatthias Ringwald #if defined __cplusplus 16082636622SMatthias Ringwald } 16182636622SMatthias Ringwald #endif 16282636622SMatthias Ringwald 163528a4a3bSMatthias Ringwald #endif // __btstack_run_loop_H 164