1 | /*
|
---|
2 | * Copyright (C) 2009 by Matthias Ringwald
|
---|
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 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
---|
18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
---|
21 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
---|
23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
---|
24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
---|
25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
---|
27 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
28 | * SUCH DAMAGE.
|
---|
29 | *
|
---|
30 | */
|
---|
31 |
|
---|
32 | /*
|
---|
33 | * run_loop.h
|
---|
34 | *
|
---|
35 | * Created by Matthias Ringwald on 6/6/09.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #ifndef __RUN_LOOP_H
|
---|
39 | #define __RUN_LOOP_H
|
---|
40 |
|
---|
41 | #include "btstack-config.h"
|
---|
42 |
|
---|
43 | #include <btstack/linked_list.h>
|
---|
44 |
|
---|
45 | #include <stdint.h>
|
---|
46 |
|
---|
47 | #ifdef HAVE_TIME
|
---|
48 | #include <sys/time.h>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #if defined __cplusplus
|
---|
52 | extern "C" {
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | typedef enum {
|
---|
56 | RUN_LOOP_POSIX = 1,
|
---|
57 | RUN_LOOP_COCOA,
|
---|
58 | RUN_LOOP_EMBEDDED
|
---|
59 | } RUN_LOOP_TYPE;
|
---|
60 |
|
---|
61 | typedef struct data_source {
|
---|
62 | linked_item_t item;
|
---|
63 | int fd; // <-- file descriptor to watch or 0
|
---|
64 | int (*process)(struct data_source *ds); // <-- do processing
|
---|
65 | } data_source_t;
|
---|
66 |
|
---|
67 | typedef struct timer {
|
---|
68 | linked_item_t item;
|
---|
69 | #ifdef HAVE_TIME
|
---|
70 | struct timeval timeout; // <-- next timeout
|
---|
71 | #endif
|
---|
72 | #ifdef HAVE_TICK
|
---|
73 | uint32_t timeout; // timeout in system ticks
|
---|
74 | #endif
|
---|
75 | void (*process)(struct timer *ts); // <-- do processing
|
---|
76 | } timer_source_t;
|
---|
77 |
|
---|
78 |
|
---|
79 | // Set timer based on current time in milliseconds.
|
---|
80 | void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms);
|
---|
81 |
|
---|
82 | // Set callback that will be executed when timer expires.
|
---|
83 | void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts));
|
---|
84 |
|
---|
85 | // Add/Remove timer source.
|
---|
86 | void run_loop_add_timer(timer_source_t *timer);
|
---|
87 | int run_loop_remove_timer(timer_source_t *timer);
|
---|
88 |
|
---|
89 | // Init must be called before any other run_loop call.
|
---|
90 | // Use RUN_LOOP_EMBEDDED for embedded devices.
|
---|
91 | void run_loop_init(RUN_LOOP_TYPE type);
|
---|
92 |
|
---|
93 | // Set data source callback.
|
---|
94 | void run_loop_set_data_source_handler(data_source_t *ds, int (*process)(data_source_t *_ds));
|
---|
95 |
|
---|
96 |
|
---|
97 | // Add/Remove data source.
|
---|
98 | void run_loop_add_data_source(data_source_t *dataSource);
|
---|
99 | int run_loop_remove_data_source(data_source_t *dataSource);
|
---|
100 |
|
---|
101 |
|
---|
102 | // Execute configured run loop. This function does not return.
|
---|
103 | void run_loop_execute(void);
|
---|
104 |
|
---|
105 | // hack to fix HCI timer handling
|
---|
106 | #ifdef HAVE_TICK
|
---|
107 | // Sets how many miliseconds has one tick.
|
---|
108 | uint32_t embedded_ticks_for_ms(uint32_t time_in_ms);
|
---|
109 | // Queries the current time in ticks.
|
---|
110 | uint32_t embedded_get_ticks(void);
|
---|
111 | // Queries the current time in ms
|
---|
112 | uint32_t embedded_get_time_ms(void);
|
---|
113 | // Allows to update BTstack system ticks based on another already existing clock
|
---|
114 | void embedded_set_ticks(uint32_t ticks);
|
---|
115 | #endif
|
---|
116 | #ifdef EMBEDDED
|
---|
117 | // Sets an internal flag that is checked in the critical section
|
---|
118 | // just before entering sleep mode. Has to be called by the interupt
|
---|
119 | // handler of a data source to signal the run loop that a new data
|
---|
120 | // is available.
|
---|
121 | void embedded_trigger(void);
|
---|
122 | // Execute run_loop once
|
---|
123 | // can be used to integrate BTstack's timer and data source processing into a foreign run runloop (not recommended)
|
---|
124 | void embedded_execute_once(void);
|
---|
125 | #endif
|
---|
126 | #if defined __cplusplus
|
---|
127 | }
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | #endif // __RUN_LOOP_H
|
---|