1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 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 BLUEKITCHEN GMBH 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 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "btstack_run_loop_posix.c" 39 40 /* 41 * btstack_run_loop.c 42 * 43 * Created by Matthias Ringwald on 6/6/09. 44 */ 45 46 // enable POSIX functions (needed for -std=c99) 47 #define _POSIX_C_SOURCE 200809 48 49 #include "btstack_run_loop_posix.h" 50 51 #include "btstack_run_loop.h" 52 #include "btstack_util.h" 53 #include "btstack_linked_list.h" 54 #include "btstack_debug.h" 55 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <sys/select.h> 59 #include <sys/time.h> 60 #include <time.h> 61 #include <unistd.h> 62 63 // the run loop 64 static int data_sources_modified; 65 66 static bool run_loop_exit_requested; 67 68 // start time. tv_usec/tv_nsec = 0 69 #ifdef _POSIX_MONOTONIC_CLOCK 70 // use monotonic clock if available 71 static struct timespec init_ts; 72 #else 73 // fallback to gettimeofday 74 static struct timeval init_tv; 75 #endif 76 77 /** 78 * Add data_source to run_loop 79 */ 80 static void btstack_run_loop_posix_add_data_source(btstack_data_source_t *ds){ 81 data_sources_modified = 1; 82 btstack_run_loop_base_add_data_source(ds); 83 } 84 85 /** 86 * Remove data_source from run loop 87 */ 88 static bool btstack_run_loop_posix_remove_data_source(btstack_data_source_t *ds){ 89 data_sources_modified = 1; 90 return btstack_run_loop_base_remove_data_source(ds); 91 } 92 93 #ifdef _POSIX_MONOTONIC_CLOCK 94 /** 95 * @brief Returns the timespec which represents the time(stop - start). It might be negative 96 */ 97 static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result){ 98 result->tv_sec = stop->tv_sec - start->tv_sec; 99 if ((stop->tv_nsec - start->tv_nsec) < 0) { 100 result->tv_sec = stop->tv_sec - start->tv_sec - 1; 101 result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000; 102 } else { 103 result->tv_sec = stop->tv_sec - start->tv_sec; 104 result->tv_nsec = stop->tv_nsec - start->tv_nsec; 105 } 106 } 107 108 /** 109 * @brief Convert timespec to miliseconds, might overflow 110 */ 111 static uint64_t timespec_to_milliseconds(struct timespec *a){ 112 uint64_t ret = 0; 113 uint64_t sec_val = (uint64_t)(a->tv_sec); 114 uint64_t nsec_val = (uint64_t)(a->tv_nsec); 115 ret = (sec_val*1000) + (nsec_val/1000000); 116 return ret; 117 } 118 119 /** 120 * @brief Returns the milisecond value of (stop - start). Might overflow 121 */ 122 static uint64_t timespec_diff_milis(struct timespec* start, struct timespec* stop){ 123 struct timespec diff_ts; 124 timespec_diff(start, stop, &diff_ts); 125 return timespec_to_milliseconds(&diff_ts); 126 } 127 #endif 128 129 /** 130 * @brief Queries the current time in ms since start 131 */ 132 static uint32_t btstack_run_loop_posix_get_time_ms(void){ 133 uint32_t time_ms; 134 #ifdef _POSIX_MONOTONIC_CLOCK 135 struct timespec now_ts; 136 clock_gettime(CLOCK_MONOTONIC, &now_ts); 137 time_ms = (uint32_t) timespec_diff_milis(&init_ts, &now_ts); 138 #else 139 struct timeval tv; 140 gettimeofday(&tv, NULL); 141 time_ms = (uint32_t) ((tv.tv_sec - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000); 142 #endif 143 return time_ms; 144 } 145 146 /** 147 * Execute run_loop 148 */ 149 static void btstack_run_loop_posix_execute(void) { 150 fd_set descriptors_read; 151 fd_set descriptors_write; 152 153 btstack_linked_list_iterator_t it; 154 struct timeval * timeout; 155 struct timeval tv; 156 uint32_t now_ms; 157 158 #ifdef _POSIX_MONOTONIC_CLOCK 159 log_info("POSIX run loop with monotonic clock"); 160 #else 161 log_info("POSIX run loop using ettimeofday fallback."); 162 #endif 163 164 while (run_loop_exit_requested == false) { 165 // collect FDs 166 FD_ZERO(&descriptors_read); 167 FD_ZERO(&descriptors_write); 168 int highest_fd = -1; 169 btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 170 while (btstack_linked_list_iterator_has_next(&it)){ 171 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 172 if (ds->source.fd < 0) continue; 173 if (ds->flags & DATA_SOURCE_CALLBACK_READ){ 174 FD_SET(ds->source.fd, &descriptors_read); 175 if (ds->source.fd > highest_fd) { 176 highest_fd = ds->source.fd; 177 } 178 log_debug("btstack_run_loop_execute adding fd %u for read", ds->source.fd); 179 } 180 if (ds->flags & DATA_SOURCE_CALLBACK_WRITE){ 181 FD_SET(ds->source.fd, &descriptors_write); 182 if (ds->source.fd > highest_fd) { 183 highest_fd = ds->source.fd; 184 } 185 log_debug("btstack_run_loop_execute adding fd %u for write", ds->source.fd); 186 } 187 } 188 189 // get next timeout 190 timeout = NULL; 191 now_ms = btstack_run_loop_posix_get_time_ms(); 192 int32_t delta_ms = btstack_run_loop_base_get_time_until_timeout(now_ms); 193 if (delta_ms >= 0) { 194 timeout = &tv; 195 tv.tv_sec = delta_ms / 1000; 196 tv.tv_usec = (int) (delta_ms - (tv.tv_sec * 1000)) * 1000; 197 log_debug("btstack_run_loop_execute next timeout in %u ms", delta_ms); 198 } 199 200 // wait for ready FDs 201 select( highest_fd+1 , &descriptors_read, &descriptors_write, NULL, timeout); 202 203 204 data_sources_modified = 0; 205 btstack_linked_list_iterator_init(&it, &btstack_run_loop_base_data_sources); 206 while (btstack_linked_list_iterator_has_next(&it) && !data_sources_modified){ 207 btstack_data_source_t *ds = (btstack_data_source_t*) btstack_linked_list_iterator_next(&it); 208 log_debug("btstack_run_loop_posix_execute: check ds %p with fd %u\n", ds, ds->source.fd); 209 if (FD_ISSET(ds->source.fd, &descriptors_read)) { 210 log_debug("btstack_run_loop_posix_execute: process read ds %p with fd %u\n", ds, ds->source.fd); 211 ds->process(ds, DATA_SOURCE_CALLBACK_READ); 212 } 213 if (data_sources_modified) break; 214 if (FD_ISSET(ds->source.fd, &descriptors_write)) { 215 log_debug("btstack_run_loop_posix_execute: process write ds %p with fd %u\n", ds, ds->source.fd); 216 ds->process(ds, DATA_SOURCE_CALLBACK_WRITE); 217 } 218 } 219 log_debug("btstack_run_loop_posix_execute: after ds check\n"); 220 221 // process timers 222 now_ms = btstack_run_loop_posix_get_time_ms(); 223 btstack_run_loop_base_process_timers(now_ms); 224 } 225 } 226 227 static void btstack_run_loop_posix_trigger_exit(void){ 228 run_loop_exit_requested = true; 229 } 230 231 // set timer 232 static void btstack_run_loop_posix_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 233 uint32_t time_ms = btstack_run_loop_posix_get_time_ms(); 234 a->timeout = time_ms + timeout_in_ms; 235 log_debug("btstack_run_loop_posix_set_timer to %u ms (now %u, timeout %u)", a->timeout, time_ms, timeout_in_ms); 236 } 237 238 static void btstack_run_loop_posix_init(void){ 239 btstack_run_loop_base_init(); 240 241 #ifdef _POSIX_MONOTONIC_CLOCK 242 clock_gettime(CLOCK_MONOTONIC, &init_ts); 243 init_ts.tv_nsec = 0; 244 #else 245 // just assume that we started at tv_usec == 0 246 gettimeofday(&init_tv, NULL); 247 init_tv.tv_usec = 0; 248 #endif 249 } 250 251 252 static const btstack_run_loop_t btstack_run_loop_posix = { 253 &btstack_run_loop_posix_init, 254 &btstack_run_loop_posix_add_data_source, 255 &btstack_run_loop_posix_remove_data_source, 256 &btstack_run_loop_base_enable_data_source_callbacks, 257 &btstack_run_loop_base_disable_data_source_callbacks, 258 &btstack_run_loop_posix_set_timer, 259 &btstack_run_loop_base_add_timer, 260 &btstack_run_loop_base_remove_timer, 261 &btstack_run_loop_posix_execute, 262 &btstack_run_loop_base_dump_timer, 263 &btstack_run_loop_posix_get_time_ms, 264 NULL, /* poll data sources from irq */ 265 NULL, 266 &btstack_run_loop_posix_trigger_exit, 267 }; 268 269 /** 270 * Provide btstack_run_loop_posix instance 271 */ 272 const btstack_run_loop_t * btstack_run_loop_posix_get_instance(void){ 273 return &btstack_run_loop_posix; 274 } 275 276