1 | /*
|
---|
2 | * Copyright (C) 2009-2012 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 | * 4. Any redistribution, use, or modification is done solely for
|
---|
17 | * personal benefit and not for any commercial purpose or for
|
---|
18 | * monetary gain.
|
---|
19 | *
|
---|
20 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
---|
21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
---|
24 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
---|
26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
---|
27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
---|
28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
---|
30 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * Please inquire about commercial licensing options at btstack@ringwald.ch
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * run_loop.c
|
---|
39 | *
|
---|
40 | * Created by Matthias Ringwald on 6/6/09.
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include <btstack/run_loop.h>
|
---|
44 |
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <stdlib.h> // exit()
|
---|
47 |
|
---|
48 | #include "run_loop_private.h"
|
---|
49 |
|
---|
50 | #include "debug.h"
|
---|
51 | #include "btstack-config.h"
|
---|
52 |
|
---|
53 | static run_loop_t * the_run_loop = NULL;
|
---|
54 |
|
---|
55 | extern const run_loop_t run_loop_embedded;
|
---|
56 |
|
---|
57 | #ifdef USE_POSIX_RUN_LOOP
|
---|
58 | extern run_loop_t run_loop_posix;
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #ifdef USE_COCOA_RUN_LOOP
|
---|
62 | extern run_loop_t run_loop_cocoa;
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | // assert run loop initialized
|
---|
66 | static void run_loop_assert(void){
|
---|
67 | #ifndef EMBEDDED
|
---|
68 | if (!the_run_loop){
|
---|
69 | log_error("ERROR: run_loop function called before run_loop_init!");
|
---|
70 | exit(10);
|
---|
71 | }
|
---|
72 | #endif
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)){
|
---|
77 | ts->process = process;
|
---|
78 | };
|
---|
79 |
|
---|
80 | void run_loop_set_data_source_handler(data_source_t *ds, int (*process)(data_source_t *_ds)){
|
---|
81 | ds->process = process;
|
---|
82 | };
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Add data_source to run_loop
|
---|
87 | */
|
---|
88 | void run_loop_add_data_source(data_source_t *ds){
|
---|
89 | run_loop_assert();
|
---|
90 | the_run_loop->add_data_source(ds);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Remove data_source from run loop
|
---|
95 | */
|
---|
96 | int run_loop_remove_data_source(data_source_t *ds){
|
---|
97 | run_loop_assert();
|
---|
98 | return the_run_loop->remove_data_source(ds);
|
---|
99 | }
|
---|
100 |
|
---|
101 | void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
|
---|
102 | run_loop_assert();
|
---|
103 | the_run_loop->set_timer(a, timeout_in_ms);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Add timer to run_loop (keep list sorted)
|
---|
108 | */
|
---|
109 | void run_loop_add_timer(timer_source_t *ts){
|
---|
110 | run_loop_assert();
|
---|
111 | the_run_loop->add_timer(ts);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Remove timer from run loop
|
---|
116 | */
|
---|
117 | int run_loop_remove_timer(timer_source_t *ts){
|
---|
118 | run_loop_assert();
|
---|
119 | return the_run_loop->remove_timer(ts);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void run_loop_timer_dump(){
|
---|
123 | run_loop_assert();
|
---|
124 | the_run_loop->dump_timer();
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Execute run_loop
|
---|
129 | */
|
---|
130 | void run_loop_execute() {
|
---|
131 | run_loop_assert();
|
---|
132 | the_run_loop->execute();
|
---|
133 | }
|
---|
134 |
|
---|
135 | // init must be called before any other run_loop call
|
---|
136 | void run_loop_init(RUN_LOOP_TYPE type){
|
---|
137 | #ifndef EMBEDDED
|
---|
138 | if (the_run_loop){
|
---|
139 | log_error("ERROR: run loop initialized twice!");
|
---|
140 | exit(10);
|
---|
141 | }
|
---|
142 | #endif
|
---|
143 | switch (type) {
|
---|
144 | #ifdef EMBEDDED
|
---|
145 | case RUN_LOOP_EMBEDDED:
|
---|
146 | the_run_loop = (run_loop_t*) &run_loop_embedded;
|
---|
147 | break;
|
---|
148 | #endif
|
---|
149 | #ifdef USE_POSIX_RUN_LOOP
|
---|
150 | case RUN_LOOP_POSIX:
|
---|
151 | the_run_loop = &run_loop_posix;
|
---|
152 | break;
|
---|
153 | #endif
|
---|
154 | #ifdef USE_COCOA_RUN_LOOP
|
---|
155 | case RUN_LOOP_COCOA:
|
---|
156 | the_run_loop = &run_loop_cocoa;
|
---|
157 | break;
|
---|
158 | #endif
|
---|
159 | default:
|
---|
160 | #ifndef EMBEDDED
|
---|
161 | log_error("ERROR: invalid run loop type %u selected!", type);
|
---|
162 | exit(10);
|
---|
163 | #endif
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | the_run_loop->init();
|
---|
167 | }
|
---|
168 |
|
---|