xref: /btstack/src/btstack_run_loop.h (revision f316a84558120969d598bdc790a72904dc72e9ca)
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 #if defined __cplusplus
5482636622SMatthias Ringwald extern "C" {
5582636622SMatthias Ringwald #endif
5682636622SMatthias Ringwald 
577cd5ef95SMatthias Ringwald 
587cd5ef95SMatthias Ringwald /**
597cd5ef95SMatthias Ringwald  * Callback types for run loop data sources
607cd5ef95SMatthias Ringwald  */
617cd5ef95SMatthias Ringwald typedef enum {
627cd5ef95SMatthias Ringwald 	DATA_SOURCE_CALLBACK_POLL  = 1 << 0,
637cd5ef95SMatthias Ringwald 	DATA_SOURCE_CALLBACK_READ  = 1 << 1,
647cd5ef95SMatthias Ringwald 	DATA_SOURCE_CALLBACK_WRITE = 1 << 2,
657cd5ef95SMatthias Ringwald } btstack_data_source_callback_type_t;
667cd5ef95SMatthias Ringwald 
67ec820d77SMatthias Ringwald typedef struct btstack_data_source {
685ed06181SMatthias Ringwald 	//
6982636622SMatthias Ringwald     btstack_linked_item_t item;
705ed06181SMatthias Ringwald     // file descriptor to watch for run loops that support file descriptors
715ed06181SMatthias Ringwald     int  fd;
725ed06181SMatthias Ringwald     // callback to call for enabled callback types
737cd5ef95SMatthias Ringwald     void  (*process)(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type);
745ed06181SMatthias Ringwald     // flags storing enabled callback types
755ed06181SMatthias Ringwald     uint16_t flags;
76ec820d77SMatthias Ringwald } btstack_data_source_t;
7782636622SMatthias Ringwald 
78eb886013SMatthias Ringwald typedef struct btstack_timer_source {
7982636622SMatthias Ringwald     btstack_linked_item_t item;
80*f316a845SMatthias Ringwald     // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS)
81*f316a845SMatthias Ringwald     uint32_t timeout;
82fd939756SMatthias Ringwald     // will be called when timer fired
83fd939756SMatthias Ringwald     void  (*process)(struct btstack_timer_source *ts);
84fd939756SMatthias Ringwald     void * context;
85ec820d77SMatthias Ringwald } btstack_timer_source_t;
8682636622SMatthias Ringwald 
87ec820d77SMatthias Ringwald typedef struct btstack_run_loop {
8882636622SMatthias Ringwald 	void (*init)(void);
890d70dd62SMatthias Ringwald 	void (*add_data_source)(btstack_data_source_t * data_source);
900d70dd62SMatthias Ringwald 	int  (*remove_data_source)(btstack_data_source_t * data_source);
910d70dd62SMatthias Ringwald 	void (*enable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
920d70dd62SMatthias Ringwald 	void (*disable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks);
93ec820d77SMatthias Ringwald 	void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms);
94ec820d77SMatthias Ringwald 	void (*add_timer)(btstack_timer_source_t *timer);
95ec820d77SMatthias Ringwald 	int  (*remove_timer)(btstack_timer_source_t *timer);
9682636622SMatthias Ringwald 	void (*execute)(void);
9782636622SMatthias Ringwald 	void (*dump_timer)(void);
9882636622SMatthias Ringwald 	uint32_t (*get_time_ms)(void);
99528a4a3bSMatthias Ringwald } btstack_run_loop_t;
10082636622SMatthias Ringwald 
101528a4a3bSMatthias Ringwald void btstack_run_loop_timer_dump(void);
10282636622SMatthias Ringwald 
10382636622SMatthias Ringwald /* API_START */
10482636622SMatthias Ringwald 
10582636622SMatthias Ringwald /**
10682636622SMatthias Ringwald  * @brief Init main run loop. Must be called before any other run loop call.
10782636622SMatthias Ringwald  *
108528a4a3bSMatthias Ringwald  * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance
10982636622SMatthias Ringwald  */
110528a4a3bSMatthias Ringwald void btstack_run_loop_init(const btstack_run_loop_t * run_loop);
11182636622SMatthias Ringwald 
11282636622SMatthias Ringwald /**
11382636622SMatthias Ringwald  * @brief Set timer based on current time in milliseconds.
11482636622SMatthias Ringwald  */
115896424b7SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t * ts, uint32_t timeout_in_ms);
11682636622SMatthias Ringwald 
11782636622SMatthias Ringwald /**
11882636622SMatthias Ringwald  * @brief Set callback that will be executed when timer expires.
11982636622SMatthias Ringwald  */
120ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts, void (*process)(btstack_timer_source_t *_ts));
12182636622SMatthias Ringwald 
12282636622SMatthias Ringwald /**
123fd939756SMatthias Ringwald  * @brief Set context for this timer
124fd939756SMatthias Ringwald  */
125fd939756SMatthias Ringwald void btstack_run_loop_set_timer_context(btstack_timer_source_t * ts, void * context);
126fd939756SMatthias Ringwald 
127fd939756SMatthias Ringwald /**
128fd939756SMatthias Ringwald  * @brief Get context for this timer
129fd939756SMatthias Ringwald  */
130fd939756SMatthias Ringwald void * btstack_run_loop_get_timer_context(btstack_timer_source_t * ts);
131fd939756SMatthias Ringwald 
132fd939756SMatthias Ringwald /**
133fd939756SMatthias Ringwald  * @brief Add timer source.
13482636622SMatthias Ringwald  */
135ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t * timer);
136fd939756SMatthias Ringwald 
137fd939756SMatthias Ringwald /**
138fd939756SMatthias Ringwald  * @brief Remove timer source.
139fd939756SMatthias Ringwald  */
140ec820d77SMatthias Ringwald int  btstack_run_loop_remove_timer(btstack_timer_source_t * timer);
14182636622SMatthias Ringwald 
14282636622SMatthias Ringwald /**
14382636622SMatthias Ringwald  * @brief Get current time in ms
14482636622SMatthias Ringwald  * @note 32-bit ms counter will overflow after approx. 52 days
14582636622SMatthias Ringwald  */
146528a4a3bSMatthias Ringwald uint32_t btstack_run_loop_get_time_ms(void);
14782636622SMatthias Ringwald 
14882636622SMatthias Ringwald /**
14982636622SMatthias Ringwald  * @brief Set data source callback.
15082636622SMatthias Ringwald  */
151896424b7SMatthias Ringwald void btstack_run_loop_set_data_source_handler(btstack_data_source_t * data_source, void (*process)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type));
15282636622SMatthias Ringwald 
15382636622SMatthias Ringwald /**
1543a5c43eeSMatthias Ringwald  * @brief Set data source file descriptor.
155896424b7SMatthias Ringwald  * @param data_source
156896424b7SMatthias Ringwald  * @param fd file descriptor
1573a5c43eeSMatthias Ringwald  * @note No effect if port doensn't have file descriptors
1583a5c43eeSMatthias Ringwald  */
159896424b7SMatthias Ringwald void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd);
1603a5c43eeSMatthias Ringwald 
1613a5c43eeSMatthias Ringwald /**
1623a5c43eeSMatthias Ringwald  * @brief Get data source file descriptor.
163896424b7SMatthias Ringwald  * @param data_source
1643a5c43eeSMatthias Ringwald  */
165896424b7SMatthias Ringwald int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source);
1663a5c43eeSMatthias Ringwald 
1673a5c43eeSMatthias Ringwald /**
168896424b7SMatthias Ringwald  * @brief Enable callbacks for a data source
169896424b7SMatthias Ringwald  * @param data_source to remove
170896424b7SMatthias Ringwald  * @param callback types to enable
17182636622SMatthias Ringwald  */
17224ced5a6SMatthias Ringwald void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
173896424b7SMatthias Ringwald 
174896424b7SMatthias Ringwald /**
175896424b7SMatthias Ringwald  * @brief Enable callbacks for a data source
176896424b7SMatthias Ringwald  * @param data_source to remove
177896424b7SMatthias Ringwald  * @param callback types to disable
178896424b7SMatthias Ringwald  */
17924ced5a6SMatthias Ringwald void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks);
180896424b7SMatthias Ringwald 
181896424b7SMatthias Ringwald /**
182896424b7SMatthias Ringwald  * @brief Add data source to run loop
183896424b7SMatthias Ringwald  * @param data_source to add
184896424b7SMatthias Ringwald  */
185896424b7SMatthias Ringwald void btstack_run_loop_add_data_source(btstack_data_source_t * data_source);
186896424b7SMatthias Ringwald 
187896424b7SMatthias Ringwald /**
188896424b7SMatthias Ringwald  * @brief Remove data source from run loop
189896424b7SMatthias Ringwald  * @param data_source to remove
190896424b7SMatthias Ringwald  */
191896424b7SMatthias Ringwald int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source);
19282636622SMatthias Ringwald 
19382636622SMatthias Ringwald /**
19482636622SMatthias Ringwald  * @brief Execute configured run loop. This function does not return.
19582636622SMatthias Ringwald  */
196528a4a3bSMatthias Ringwald void btstack_run_loop_execute(void);
19782636622SMatthias Ringwald 
19882636622SMatthias Ringwald /* API_END */
19982636622SMatthias Ringwald 
20082636622SMatthias Ringwald #if defined __cplusplus
20182636622SMatthias Ringwald }
20282636622SMatthias Ringwald #endif
20382636622SMatthias Ringwald 
204528a4a3bSMatthias Ringwald #endif // __btstack_run_loop_H
205