btstack_run_loop_posix.c 12 KB

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