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 BLUEKITCHEN 24 * GMBH 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 /** 39 * BTstack Run Loop Abstraction 40 * 41 * Provides generic functionality required by the Bluetooth stack and example applications: 42 * - asynchronous IO for various devices Serial, USB, network sockets, console 43 * - system time in milliseconds 44 * - single shot timers 45 * - integration for threaded environments 46 */ 47 48 #ifndef BTSTACK_RUN_LOOP_H 49 #define BTSTACK_RUN_LOOP_H 50 51 #include "btstack_config.h" 52 53 #include "btstack_bool.h" 54 #include "btstack_linked_list.h" 55 #include "btstack_defines.h" 56 57 #include <stdint.h> 58 59 #if defined __cplusplus 60 extern "C" { 61 #endif 62 63 /** 64 * Callback types for run loop data sources 65 */ 66 typedef enum { 67 DATA_SOURCE_CALLBACK_POLL = 1 << 0, 68 DATA_SOURCE_CALLBACK_READ = 1 << 1, 69 DATA_SOURCE_CALLBACK_WRITE = 1 << 2, 70 } btstack_data_source_callback_type_t; 71 72 typedef struct btstack_data_source { 73 // linked item 74 btstack_linked_item_t item; 75 76 // item to watch in run loop 77 union { 78 // file descriptor for posix systems 79 int fd; 80 // handle on windows 81 void * handle; 82 } source; 83 84 // callback to call for enabled callback types 85 void (*process)(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type); 86 87 // flags storing enabled callback types 88 uint16_t flags; 89 90 } btstack_data_source_t; 91 92 typedef struct btstack_timer_source { 93 btstack_linked_item_t item; 94 // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS) 95 uint32_t timeout; 96 // will be called when timer fired 97 void (*process)(struct btstack_timer_source *ts); 98 void * context; 99 } btstack_timer_source_t; 100 101 typedef struct btstack_run_loop { 102 void (*init)(void); 103 void (*add_data_source)(btstack_data_source_t * data_source); 104 bool (*remove_data_source)(btstack_data_source_t * data_source); 105 void (*enable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks); 106 void (*disable_data_source_callbacks)(btstack_data_source_t * data_source, uint16_t callbacks); 107 void (*set_timer)(btstack_timer_source_t * timer, uint32_t timeout_in_ms); 108 void (*add_timer)(btstack_timer_source_t *timer); 109 bool (*remove_timer)(btstack_timer_source_t *timer); 110 void (*execute)(void); 111 void (*dump_timer)(void); 112 uint32_t (*get_time_ms)(void); 113 void (*poll_data_sources_from_irq)(void); 114 void (*execute_on_main_thread)(btstack_context_callback_registration_t * callback_registration); 115 void (*trigger_exit)(void); 116 } btstack_run_loop_t; 117 118 119 /* 120 * BTstack Run Loop Base Implementation 121 * Portable implementation of timer and data source management as base for platform specific implementations 122 */ 123 124 // private data (access only by run loop implementations) 125 extern btstack_linked_list_t btstack_run_loop_base_timers; 126 extern btstack_linked_list_t btstack_run_loop_base_data_sources; 127 extern btstack_linked_list_t btstack_run_loop_base_callbacks; 128 129 /** 130 * @brief Init 131 */ 132 void btstack_run_loop_base_init(void); 133 134 /** 135 * @brief Add timer source. 136 * @param timer to add 137 */ 138 void btstack_run_loop_base_add_timer(btstack_timer_source_t * timer); 139 140 /** 141 * @brief Remove timer source. 142 * @param timer to remove 143 * @return true if timer was removed 144 */ 145 bool btstack_run_loop_base_remove_timer(btstack_timer_source_t * timer); 146 147 /** 148 * @brief Process timers: remove expired timers from list and call their process function 149 * @param now 150 */ 151 void btstack_run_loop_base_process_timers(uint32_t now); 152 153 /** 154 * @brief Dump list of timers via log_info 155 */ 156 void btstack_run_loop_base_dump_timer(void); 157 158 /** 159 * @brief Get time until first timer fires 160 * @return -1 if no timers, time until next timeout otherwise 161 */ 162 int32_t btstack_run_loop_base_get_time_until_timeout(uint32_t now); 163 164 /** 165 * @brief Add data source to run loop 166 * @param data_source to add 167 */ 168 void btstack_run_loop_base_add_data_source(btstack_data_source_t * data_source); 169 170 /** 171 * @brief Remove data source from run loop 172 * @param data_source to remove 173 * @return true if data srouce was removed 174 */ 175 bool btstack_run_loop_base_remove_data_source(btstack_data_source_t * data_source); 176 177 /** 178 * @brief Enable callbacks for a data source 179 * @param data_source to remove 180 * @param callback_types to enable 181 */ 182 void btstack_run_loop_base_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callback_types); 183 184 /** 185 * @brief Enable callbacks for a data source 186 * @param data_source to remove 187 * @param callback_types to disable 188 */ 189 void btstack_run_loop_base_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callback_types); 190 191 /** 192 * @brief Poll data sources. It calls the procss function for all data sources where DATA_SOURCE_CALLBACK_POLL is set 193 */ 194 void btstack_run_loop_base_poll_data_sources(void); 195 196 /** 197 * @bried Add Callbacks to list of callbacks to execute with btstack_run_loop_base_execute_callbacks 198 */ 199 void btstack_run_loop_base_add_callback(btstack_context_callback_registration_t * callback_registration); 200 201 /** 202 * @bried Process Callbacks: remove all callback-registrations and call the registered function with its context 203 */ 204 void btstack_run_loop_base_execute_callbacks(void); 205 206 207 /* API_START */ 208 209 /** 210 * @brief Init main run loop. Must be called before any other run loop call. 211 * 212 * Use btstack_run_loop_$(btstack_run_loop_TYPE)_get_instance() from btstack_run_loop_$(btstack_run_loop_TYPE).h to get instance 213 */ 214 void btstack_run_loop_init(const btstack_run_loop_t * run_loop); 215 216 /** 217 * @brief Set timer based on current time in milliseconds. 218 */ 219 void btstack_run_loop_set_timer(btstack_timer_source_t * timer, uint32_t timeout_in_ms); 220 221 /** 222 * @brief Set callback that will be executed when timer expires. 223 */ 224 void btstack_run_loop_set_timer_handler(btstack_timer_source_t * timer, void (*process)(btstack_timer_source_t * _timer)); 225 226 /** 227 * @brief Set context for this timer 228 */ 229 void btstack_run_loop_set_timer_context(btstack_timer_source_t * timer, void * context); 230 231 /** 232 * @brief Get context for this timer 233 */ 234 void * btstack_run_loop_get_timer_context(btstack_timer_source_t * timer); 235 236 /** 237 * @brief Add timer source. 238 */ 239 void btstack_run_loop_add_timer(btstack_timer_source_t * timer); 240 241 /** 242 * @brief Remove timer source. 243 */ 244 int btstack_run_loop_remove_timer(btstack_timer_source_t * timer); 245 246 /** 247 * @brief Get current time in ms 248 * @note 32-bit ms counter will overflow after approx. 52 days 249 */ 250 uint32_t btstack_run_loop_get_time_ms(void); 251 252 /** 253 * @brief Dump timers using log_info 254 */ 255 void btstack_run_loop_timer_dump(void); 256 257 258 /** 259 * @brief Set data source callback. 260 */ 261 void btstack_run_loop_set_data_source_handler(btstack_data_source_t * data_source, void (*process)(btstack_data_source_t * _data_source, btstack_data_source_callback_type_t callback_type)); 262 263 /** 264 * @brief Set data source file descriptor. 265 * @param data_source 266 * @param fd file descriptor 267 * @note No effect if port doensn't have file descriptors 268 */ 269 void btstack_run_loop_set_data_source_fd(btstack_data_source_t * data_source, int fd); 270 271 /** 272 * @brief Get data source file descriptor. 273 * @param data_source 274 */ 275 int btstack_run_loop_get_data_source_fd(btstack_data_source_t * data_source); 276 277 /** 278 * @brief Set data source file descriptor. 279 * @param data_source 280 * @param handle 281 * @note No effect if port doensn't have file descriptors 282 */ 283 void btstack_run_loop_set_data_source_handle(btstack_data_source_t * data_source, void * handle); 284 285 /** 286 * @brief Get data source file descriptor. 287 * @param data_source 288 */ 289 void * btstack_run_loop_get_data_source_handle(btstack_data_source_t * data_source); 290 291 /** 292 * @brief Enable callbacks for a data source 293 * @param data_source to remove 294 * @param callback types to enable 295 */ 296 void btstack_run_loop_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks); 297 298 /** 299 * @brief Enable callbacks for a data source 300 * @param data_source to remove 301 * @param callback types to disable 302 */ 303 void btstack_run_loop_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks); 304 305 /** 306 * @brief Add data source to run loop 307 * @param data_source to add 308 */ 309 void btstack_run_loop_add_data_source(btstack_data_source_t * data_source); 310 311 /** 312 * @brief Remove data source from run loop 313 * @param data_source to remove 314 */ 315 int btstack_run_loop_remove_data_source(btstack_data_source_t * data_source); 316 317 /** 318 * @brief Poll data sources - called only from IRQ context 319 * @note Can be used to trigger processing of received peripheral data on the main thread 320 * by registering a data source with DATA_SOURCE_CALLBACK_POLL and calling this 321 * function from the IRQ handler. 322 */ 323 void btstack_run_loop_poll_data_sources_from_irq(void); 324 325 326 /** 327 * @brief Execute configured run loop. This function does not return. 328 */ 329 void btstack_run_loop_execute(void); 330 331 /** 332 * @brief Registers callback with run loop and mark main thread as ready 333 * @note If callback is already registered, the call will be ignored. 334 * This function allows to implement, e.g., a quque-based message passing mechanism: 335 * The external thread puts an item into a queue and call this function to trigger 336 * processing by the BTstack main thread. If this happens multiple times, it is 337 * guranteed that the callback will run at least once after the last item was added. 338 * @param callback_registration 339 */ 340 void btstack_run_loop_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration); 341 342 /** 343 * @brief Trigger exit of active run loop (started via btstack_run_loop_execute) if possible 344 * @note This is only supported if there's a loop in the btstack_run_loop_execute function. 345 * It is not supported if timers and data sources are only mapped to RTOS equivalents, e.g. 346 * in the Qt or Core Foundation implementations. 347 */ 348 void btstack_run_loop_trigger_exit(void); 349 350 351 /** 352 * @brief De-Init Run Loop 353 */ 354 void btstack_run_loop_deinit(void); 355 356 /* API_END */ 357 358 359 #if defined __cplusplus 360 } 361 #endif 362 363 #endif // BTSTACK_RUN_LOOP_H 364