115d50271SMatthias Ringwald /* 215d50271SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 315d50271SMatthias Ringwald * 415d50271SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 515d50271SMatthias Ringwald * modification, are permitted provided that the following conditions 615d50271SMatthias Ringwald * are met: 715d50271SMatthias Ringwald * 815d50271SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 915d50271SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1015d50271SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1115d50271SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1215d50271SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1315d50271SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1415d50271SMatthias Ringwald * contributors may be used to endorse or promote products derived 1515d50271SMatthias Ringwald * from this software without specific prior written permission. 1615d50271SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1715d50271SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1815d50271SMatthias Ringwald * monetary gain. 1915d50271SMatthias Ringwald * 2015d50271SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2115d50271SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2215d50271SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2315d50271SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2415d50271SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2515d50271SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2615d50271SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2715d50271SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2815d50271SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2915d50271SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3015d50271SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3115d50271SMatthias Ringwald * SUCH DAMAGE. 3215d50271SMatthias Ringwald * 3315d50271SMatthias Ringwald * Please inquire about commercial licensing options at 3415d50271SMatthias Ringwald * [email protected] 3515d50271SMatthias Ringwald * 3615d50271SMatthias Ringwald */ 3715d50271SMatthias Ringwald 3815d50271SMatthias Ringwald /* 3915d50271SMatthias Ringwald * run_loop.c 4015d50271SMatthias Ringwald * 4115d50271SMatthias Ringwald * Created by Matthias Ringwald on 6/6/09. 4215d50271SMatthias Ringwald */ 4315d50271SMatthias Ringwald 4415d50271SMatthias Ringwald #include "btstack_run_loop.h" 4515d50271SMatthias Ringwald 4615d50271SMatthias Ringwald #include <stdio.h> 4715d50271SMatthias Ringwald #include <stdlib.h> // exit() 4815d50271SMatthias Ringwald 4915d50271SMatthias Ringwald 5015d50271SMatthias Ringwald #include "btstack_debug.h" 51*7907f069SMatthias Ringwald #include "btstack_config.h" 5215d50271SMatthias Ringwald 53528a4a3bSMatthias Ringwald static const btstack_run_loop_t * the_run_loop = NULL; 5415d50271SMatthias Ringwald 55528a4a3bSMatthias Ringwald extern const btstack_run_loop_t btstack_run_loop_embedded; 5615d50271SMatthias Ringwald 5715d50271SMatthias Ringwald // assert run loop initialized 58528a4a3bSMatthias Ringwald static void btstack_run_loop_assert(void){ 5915d50271SMatthias Ringwald if (!the_run_loop){ 60528a4a3bSMatthias Ringwald log_error("ERROR: run_loop function called before btstack_run_loop_init!"); 6115d50271SMatthias Ringwald #ifdef EMBEDDED 6215d50271SMatthias Ringwald exit(10); 6315d50271SMatthias Ringwald #else 6415d50271SMatthias Ringwald while(1); 6515d50271SMatthias Ringwald #endif 6615d50271SMatthias Ringwald } 6715d50271SMatthias Ringwald } 6815d50271SMatthias Ringwald 6915d50271SMatthias Ringwald 70ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 7115d50271SMatthias Ringwald ts->process = process; 7215d50271SMatthias Ringwald }; 7315d50271SMatthias Ringwald 74ec820d77SMatthias Ringwald void btstack_run_loop_set_data_source_handler(btstack_data_source_t *ds, int (*process)(btstack_data_source_t *_ds)){ 7515d50271SMatthias Ringwald ds->process = process; 7615d50271SMatthias Ringwald }; 7715d50271SMatthias Ringwald 7815d50271SMatthias Ringwald 7915d50271SMatthias Ringwald /** 8015d50271SMatthias Ringwald * Add data_source to run_loop 8115d50271SMatthias Ringwald */ 82ec820d77SMatthias Ringwald void btstack_run_loop_add_data_source(btstack_data_source_t *ds){ 83528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 8415d50271SMatthias Ringwald if (the_run_loop->add_data_source){ 8515d50271SMatthias Ringwald the_run_loop->add_data_source(ds); 8615d50271SMatthias Ringwald } else { 87528a4a3bSMatthias Ringwald log_error("btstack_run_loop_add_data_source not implemented"); 8815d50271SMatthias Ringwald } 8915d50271SMatthias Ringwald } 9015d50271SMatthias Ringwald 9115d50271SMatthias Ringwald /** 9215d50271SMatthias Ringwald * Remove data_source from run loop 9315d50271SMatthias Ringwald */ 94ec820d77SMatthias Ringwald int btstack_run_loop_remove_data_source(btstack_data_source_t *ds){ 95528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 9615d50271SMatthias Ringwald if (the_run_loop->remove_data_source){ 9715d50271SMatthias Ringwald return the_run_loop->remove_data_source(ds); 9815d50271SMatthias Ringwald } else { 99528a4a3bSMatthias Ringwald log_error("btstack_run_loop_remove_data_source not implemented"); 10015d50271SMatthias Ringwald return 0; 10115d50271SMatthias Ringwald } 10215d50271SMatthias Ringwald } 10315d50271SMatthias Ringwald 104ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 105528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 10615d50271SMatthias Ringwald the_run_loop->set_timer(a, timeout_in_ms); 10715d50271SMatthias Ringwald } 10815d50271SMatthias Ringwald 10915d50271SMatthias Ringwald /** 11015d50271SMatthias Ringwald * Add timer to run_loop (keep list sorted) 11115d50271SMatthias Ringwald */ 112ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *ts){ 113528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 11415d50271SMatthias Ringwald the_run_loop->add_timer(ts); 11515d50271SMatthias Ringwald } 11615d50271SMatthias Ringwald 11715d50271SMatthias Ringwald /** 11815d50271SMatthias Ringwald * Remove timer from run loop 11915d50271SMatthias Ringwald */ 120ec820d77SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *ts){ 121528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 12215d50271SMatthias Ringwald return the_run_loop->remove_timer(ts); 12315d50271SMatthias Ringwald } 12415d50271SMatthias Ringwald 12515d50271SMatthias Ringwald /** 12615d50271SMatthias Ringwald * @brief Get current time in ms 12715d50271SMatthias Ringwald */ 128528a4a3bSMatthias Ringwald uint32_t btstack_run_loop_get_time_ms(void){ 129528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 13015d50271SMatthias Ringwald return the_run_loop->get_time_ms(); 13115d50271SMatthias Ringwald } 13215d50271SMatthias Ringwald 13315d50271SMatthias Ringwald 134528a4a3bSMatthias Ringwald void btstack_run_loop_timer_dump(void){ 135528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 13615d50271SMatthias Ringwald the_run_loop->dump_timer(); 13715d50271SMatthias Ringwald } 13815d50271SMatthias Ringwald 13915d50271SMatthias Ringwald /** 14015d50271SMatthias Ringwald * Execute run_loop 14115d50271SMatthias Ringwald */ 142528a4a3bSMatthias Ringwald void btstack_run_loop_execute(void){ 143528a4a3bSMatthias Ringwald btstack_run_loop_assert(); 14415d50271SMatthias Ringwald the_run_loop->execute(); 14515d50271SMatthias Ringwald } 14615d50271SMatthias Ringwald 14715d50271SMatthias Ringwald // init must be called before any other run_loop call 148528a4a3bSMatthias Ringwald void btstack_run_loop_init(const btstack_run_loop_t * run_loop){ 14915d50271SMatthias Ringwald if (the_run_loop){ 15015d50271SMatthias Ringwald log_error("ERROR: run loop initialized twice!"); 15115d50271SMatthias Ringwald #ifdef EMBEDDED 15215d50271SMatthias Ringwald while(1); 15315d50271SMatthias Ringwald #else 15415d50271SMatthias Ringwald exit(10); 15515d50271SMatthias Ringwald #endif 15615d50271SMatthias Ringwald } 15715d50271SMatthias Ringwald the_run_loop = run_loop; 15815d50271SMatthias Ringwald the_run_loop->init(); 15915d50271SMatthias Ringwald } 16015d50271SMatthias Ringwald 161