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