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 4782636622SMatthias 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 64*eb886013SMatthias Ringwald int (*process)(struct btstack_data_source *ds); // <-- do processing 65ec820d77SMatthias Ringwald } btstack_data_source_t; 6682636622SMatthias Ringwald 67*eb886013SMatthias 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*eb886013SMatthias Ringwald void (*process)(struct btstack_timer_source *ts); // <-- do processing 76ec820d77SMatthias Ringwald } btstack_timer_source_t; 7782636622SMatthias Ringwald 7882636622SMatthias Ringwald // 79ec820d77SMatthias Ringwald typedef struct btstack_run_loop { 8082636622SMatthias Ringwald void (*init)(void); 81ec820d77SMatthias Ringwald void (*add_data_source)(btstack_data_source_t *dataSource); 82ec820d77SMatthias Ringwald int (*remove_data_source)(btstack_data_source_t *dataSource); 83ec820d77SMatthias Ringwald void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms); 84ec820d77SMatthias Ringwald void (*add_timer)(btstack_timer_source_t *timer); 85ec820d77SMatthias Ringwald int (*remove_timer)(btstack_timer_source_t *timer); 8682636622SMatthias Ringwald void (*execute)(void); 8782636622SMatthias Ringwald void (*dump_timer)(void); 8882636622SMatthias Ringwald uint32_t (*get_time_ms)(void); 89528a4a3bSMatthias Ringwald } btstack_run_loop_t; 9082636622SMatthias Ringwald 91528a4a3bSMatthias Ringwald void btstack_run_loop_timer_dump(void); 9282636622SMatthias Ringwald 9382636622SMatthias Ringwald 9482636622SMatthias Ringwald /* API_START */ 9582636622SMatthias Ringwald 9682636622SMatthias Ringwald /** 9782636622SMatthias Ringwald * @brief Init main run loop. Must be called before any other run loop call. 9882636622SMatthias Ringwald * 99528a4a3bSMatthias Ringwald * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance 10082636622SMatthias Ringwald */ 101528a4a3bSMatthias Ringwald void btstack_run_loop_init(const btstack_run_loop_t * run_loop); 10282636622SMatthias Ringwald 10382636622SMatthias Ringwald /** 10482636622SMatthias Ringwald * @brief Set timer based on current time in milliseconds. 10582636622SMatthias Ringwald */ 106ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms); 10782636622SMatthias Ringwald 10882636622SMatthias Ringwald /** 10982636622SMatthias Ringwald * @brief Set callback that will be executed when timer expires. 11082636622SMatthias Ringwald */ 111ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)); 11282636622SMatthias Ringwald 11382636622SMatthias Ringwald /** 11482636622SMatthias Ringwald * @brief Add/Remove timer source. 11582636622SMatthias Ringwald */ 116ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer); 117ec820d77SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *timer); 11882636622SMatthias Ringwald 11982636622SMatthias Ringwald /** 12082636622SMatthias Ringwald * @brief Get current time in ms 12182636622SMatthias Ringwald * @note 32-bit ms counter will overflow after approx. 52 days 12282636622SMatthias Ringwald */ 123528a4a3bSMatthias Ringwald uint32_t btstack_run_loop_get_time_ms(void); 12482636622SMatthias Ringwald 12582636622SMatthias Ringwald /** 12682636622SMatthias Ringwald * @brief Set data source callback. 12782636622SMatthias Ringwald */ 128ec820d77SMatthias Ringwald void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)); 12982636622SMatthias Ringwald 13082636622SMatthias Ringwald /** 13182636622SMatthias Ringwald * @brief Add/Remove data source. 13282636622SMatthias Ringwald */ 133ec820d77SMatthias Ringwald void btstack_run_loop_add_data_source(btstack_data_source_t *dataSource); 134ec820d77SMatthias Ringwald int btstack_run_loop_remove_data_source(btstack_data_source_t *dataSource); 13582636622SMatthias Ringwald 13682636622SMatthias Ringwald /** 13782636622SMatthias Ringwald * @brief Execute configured run loop. This function does not return. 13882636622SMatthias Ringwald */ 139528a4a3bSMatthias Ringwald void btstack_run_loop_execute(void); 14082636622SMatthias Ringwald 14182636622SMatthias Ringwald /* API_END */ 14282636622SMatthias Ringwald 14382636622SMatthias Ringwald #if defined __cplusplus 14482636622SMatthias Ringwald } 14582636622SMatthias Ringwald #endif 14682636622SMatthias Ringwald 147528a4a3bSMatthias Ringwald #endif // __btstack_run_loop_H 148