mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-31 01:20:44 +00:00
clean up run_loop sublcassing as suggested in #356
This commit is contained in:
parent
0d0bcdceb3
commit
372248418e
@ -98,6 +98,11 @@ int run_loop_remove_data_source(data_source_t *ds){
|
|||||||
return the_run_loop->remove_data_source(ds);
|
return the_run_loop->remove_data_source(ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
|
||||||
|
run_loop_assert();
|
||||||
|
the_run_loop->set_timer(a, timeout_in_ms);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add timer to run_loop (keep list sorted)
|
* Add timer to run_loop (keep list sorted)
|
||||||
*/
|
*/
|
||||||
|
@ -138,6 +138,17 @@ int cocoa_remove_timer(timer_source_t * ts){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set timer
|
||||||
|
static void cocoa_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
|
||||||
|
gettimeofday(&a->timeout, NULL);
|
||||||
|
a->timeout.tv_sec += timeout_in_ms / 1000;
|
||||||
|
a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000;
|
||||||
|
if (a->timeout.tv_usec > 1000000) {
|
||||||
|
a->timeout.tv_usec -= 1000000;
|
||||||
|
a->timeout.tv_sec++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void cocoa_init(void){
|
void cocoa_init(void){
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,9 +166,10 @@ run_loop_t run_loop_cocoa = {
|
|||||||
&cocoa_init,
|
&cocoa_init,
|
||||||
&cocoa_add_data_source,
|
&cocoa_add_data_source,
|
||||||
&cocoa_remove_data_source,
|
&cocoa_remove_data_source,
|
||||||
|
&cocoa_set_timer,
|
||||||
&cocoa_add_timer,
|
&cocoa_add_timer,
|
||||||
&cocoa_remove_timer,
|
&cocoa_remove_timer,
|
||||||
&cocoa_execute,
|
&cocoa_execute,
|
||||||
&cocoa_dump_timer
|
&cocoa_dump_timer,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -73,31 +73,31 @@ static uint32_t system_ticks;
|
|||||||
|
|
||||||
static int trigger_event_received = 0;
|
static int trigger_event_received = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* trigger run loop iteration
|
|
||||||
*/
|
|
||||||
void embedded_trigger(void){
|
|
||||||
trigger_event_received = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add data_source to run_loop
|
* Add data_source to run_loop
|
||||||
*/
|
*/
|
||||||
void embedded_add_data_source(data_source_t *ds){
|
static void embedded_add_data_source(data_source_t *ds){
|
||||||
linked_list_add(&data_sources, (linked_item_t *) ds);
|
linked_list_add(&data_sources, (linked_item_t *) ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove data_source from run loop
|
* Remove data_source from run loop
|
||||||
*/
|
*/
|
||||||
int embedded_remove_data_source(data_source_t *ds){
|
static int embedded_remove_data_source(data_source_t *ds){
|
||||||
return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set timer
|
||||||
|
static void embedded_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){
|
||||||
|
uint32_t ticks = embedded_ticks_for_ms(timeout_in_ms);
|
||||||
|
if (ticks == 0) ticks++;
|
||||||
|
ts->timeout = system_ticks + ticks;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add timer to run_loop (keep list sorted)
|
* Add timer to run_loop (keep list sorted)
|
||||||
*/
|
*/
|
||||||
void embedded_add_timer(timer_source_t *ts){
|
static void embedded_add_timer(timer_source_t *ts){
|
||||||
#ifdef HAVE_TICK
|
#ifdef HAVE_TICK
|
||||||
linked_item_t *it;
|
linked_item_t *it;
|
||||||
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
||||||
@ -115,7 +115,7 @@ void embedded_add_timer(timer_source_t *ts){
|
|||||||
/**
|
/**
|
||||||
* Remove timer from run loop
|
* Remove timer from run loop
|
||||||
*/
|
*/
|
||||||
int embedded_remove_timer(timer_source_t *ts){
|
static int embedded_remove_timer(timer_source_t *ts){
|
||||||
#ifdef HAVE_TICK
|
#ifdef HAVE_TICK
|
||||||
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
||||||
return linked_list_remove(&timers, (linked_item_t *) ts);
|
return linked_list_remove(&timers, (linked_item_t *) ts);
|
||||||
@ -124,7 +124,7 @@ int embedded_remove_timer(timer_source_t *ts){
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void embedded_dump_timer(void){
|
static void embedded_dump_timer(void){
|
||||||
#ifdef HAVE_TICK
|
#ifdef HAVE_TICK
|
||||||
#ifdef ENABLE_LOG_INFO
|
#ifdef ENABLE_LOG_INFO
|
||||||
linked_item_t *it;
|
linked_item_t *it;
|
||||||
@ -140,7 +140,7 @@ void embedded_dump_timer(void){
|
|||||||
/**
|
/**
|
||||||
* Execute run_loop once
|
* Execute run_loop once
|
||||||
*/
|
*/
|
||||||
void embedded_execute_once(void) {
|
static void embedded_execute_once(void) {
|
||||||
data_source_t *ds;
|
data_source_t *ds;
|
||||||
|
|
||||||
// process data sources
|
// process data sources
|
||||||
@ -173,7 +173,7 @@ void embedded_execute_once(void) {
|
|||||||
/**
|
/**
|
||||||
* Execute run_loop
|
* Execute run_loop
|
||||||
*/
|
*/
|
||||||
void embedded_execute(void) {
|
static void embedded_execute(void) {
|
||||||
while (1) {
|
while (1) {
|
||||||
embedded_execute_once();
|
embedded_execute_once();
|
||||||
}
|
}
|
||||||
@ -192,16 +192,16 @@ uint32_t embedded_get_ticks(void){
|
|||||||
uint32_t embedded_ticks_for_ms(uint32_t time_in_ms){
|
uint32_t embedded_ticks_for_ms(uint32_t time_in_ms){
|
||||||
return time_in_ms / hal_tick_get_tick_period_in_ms();
|
return time_in_ms / hal_tick_get_tick_period_in_ms();
|
||||||
}
|
}
|
||||||
|
|
||||||
// set timer
|
|
||||||
void run_loop_set_timer(timer_source_t *ts, uint32_t timeout_in_ms){
|
|
||||||
uint32_t ticks = embedded_ticks_for_ms(timeout_in_ms);
|
|
||||||
if (ticks == 0) ticks++;
|
|
||||||
ts->timeout = system_ticks + ticks;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void embedded_init(void){
|
/**
|
||||||
|
* trigger run loop iteration
|
||||||
|
*/
|
||||||
|
void embedded_trigger(void){
|
||||||
|
trigger_event_received = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void embedded_init(void){
|
||||||
|
|
||||||
data_sources = NULL;
|
data_sources = NULL;
|
||||||
|
|
||||||
@ -217,8 +217,9 @@ const run_loop_t run_loop_embedded = {
|
|||||||
&embedded_init,
|
&embedded_init,
|
||||||
&embedded_add_data_source,
|
&embedded_add_data_source,
|
||||||
&embedded_remove_data_source,
|
&embedded_remove_data_source,
|
||||||
|
&embedded_set_timer,
|
||||||
&embedded_add_timer,
|
&embedded_add_timer,
|
||||||
&embedded_remove_timer,
|
&embedded_remove_timer,
|
||||||
&embedded_execute,
|
&embedded_execute,
|
||||||
&embedded_dump_timer
|
&embedded_dump_timer,
|
||||||
};
|
};
|
||||||
|
@ -50,7 +50,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void posix_dump_timer(void);
|
static void posix_dump_timer(void);
|
||||||
|
static int posix_timeval_compare(struct timeval *a, struct timeval *b);
|
||||||
|
static int posix_timer_compare(timer_source_t *a, timer_source_t *b);
|
||||||
|
|
||||||
// the run loop
|
// the run loop
|
||||||
static linked_list_t data_sources;
|
static linked_list_t data_sources;
|
||||||
@ -60,7 +62,7 @@ static linked_list_t timers;
|
|||||||
/**
|
/**
|
||||||
* Add data_source to run_loop
|
* Add data_source to run_loop
|
||||||
*/
|
*/
|
||||||
void posix_add_data_source(data_source_t *ds){
|
static void posix_add_data_source(data_source_t *ds){
|
||||||
data_sources_modified = 1;
|
data_sources_modified = 1;
|
||||||
// log_info("posix_add_data_source %x with fd %u\n", (int) ds, ds->fd);
|
// log_info("posix_add_data_source %x with fd %u\n", (int) ds, ds->fd);
|
||||||
linked_list_add(&data_sources, (linked_item_t *) ds);
|
linked_list_add(&data_sources, (linked_item_t *) ds);
|
||||||
@ -69,7 +71,7 @@ void posix_add_data_source(data_source_t *ds){
|
|||||||
/**
|
/**
|
||||||
* Remove data_source from run loop
|
* Remove data_source from run loop
|
||||||
*/
|
*/
|
||||||
int posix_remove_data_source(data_source_t *ds){
|
static int posix_remove_data_source(data_source_t *ds){
|
||||||
data_sources_modified = 1;
|
data_sources_modified = 1;
|
||||||
// log_info("posix_remove_data_source %x\n", (int) ds);
|
// log_info("posix_remove_data_source %x\n", (int) ds);
|
||||||
return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
||||||
@ -78,14 +80,14 @@ int posix_remove_data_source(data_source_t *ds){
|
|||||||
/**
|
/**
|
||||||
* Add timer to run_loop (keep list sorted)
|
* Add timer to run_loop (keep list sorted)
|
||||||
*/
|
*/
|
||||||
void posix_add_timer(timer_source_t *ts){
|
static void posix_add_timer(timer_source_t *ts){
|
||||||
linked_item_t *it;
|
linked_item_t *it;
|
||||||
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
||||||
if ((timer_source_t *) it->next == ts){
|
if ((timer_source_t *) it->next == ts){
|
||||||
log_error( "run_loop_timer_add error: timer to add already in list!\n");
|
log_error( "run_loop_timer_add error: timer to add already in list!\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (run_loop_timer_compare( (timer_source_t *) it->next, ts) > 0) {
|
if (posix_timer_compare( (timer_source_t *) it->next, ts) > 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,13 +100,13 @@ void posix_add_timer(timer_source_t *ts){
|
|||||||
/**
|
/**
|
||||||
* Remove timer from run loop
|
* Remove timer from run loop
|
||||||
*/
|
*/
|
||||||
int posix_remove_timer(timer_source_t *ts){
|
static int posix_remove_timer(timer_source_t *ts){
|
||||||
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
// log_info("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
||||||
// posix_dump_timer();
|
// posix_dump_timer();
|
||||||
return linked_list_remove(&timers, (linked_item_t *) ts);
|
return linked_list_remove(&timers, (linked_item_t *) ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
void posix_dump_timer(void){
|
static void posix_dump_timer(void){
|
||||||
linked_item_t *it;
|
linked_item_t *it;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (it = (linked_item_t *) timers; it ; it = it->next){
|
for (it = (linked_item_t *) timers; it ; it = it->next){
|
||||||
@ -116,7 +118,7 @@ void posix_dump_timer(void){
|
|||||||
/**
|
/**
|
||||||
* Execute run_loop
|
* Execute run_loop
|
||||||
*/
|
*/
|
||||||
void posix_execute(void) {
|
static void posix_execute(void) {
|
||||||
fd_set descriptors;
|
fd_set descriptors;
|
||||||
data_source_t *ds;
|
data_source_t *ds;
|
||||||
timer_source_t *ts;
|
timer_source_t *ts;
|
||||||
@ -190,7 +192,7 @@ void posix_execute(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set timer
|
// set timer
|
||||||
void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
|
static void posix_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
|
||||||
gettimeofday(&a->timeout, NULL);
|
gettimeofday(&a->timeout, NULL);
|
||||||
a->timeout.tv_sec += timeout_in_ms / 1000;
|
a->timeout.tv_sec += timeout_in_ms / 1000;
|
||||||
a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000;
|
a->timeout.tv_usec += (timeout_in_ms % 1000) * 1000;
|
||||||
@ -202,7 +204,7 @@ void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
|
|||||||
|
|
||||||
// compare timers - NULL is assumed to be before the Big Bang
|
// compare timers - NULL is assumed to be before the Big Bang
|
||||||
// pre: 0 <= tv_usec < 1000000
|
// pre: 0 <= tv_usec < 1000000
|
||||||
int run_loop_timeval_compare(struct timeval *a, struct timeval *b){
|
static int posix_timeval_compare(struct timeval *a, struct timeval *b){
|
||||||
if (!a && !b) return 0;
|
if (!a && !b) return 0;
|
||||||
if (!a) return -1;
|
if (!a) return -1;
|
||||||
if (!b) return 1;
|
if (!b) return 1;
|
||||||
@ -227,14 +229,14 @@ int run_loop_timeval_compare(struct timeval *a, struct timeval *b){
|
|||||||
|
|
||||||
// compare timers - NULL is assumed to be before the Big Bang
|
// compare timers - NULL is assumed to be before the Big Bang
|
||||||
// pre: 0 <= tv_usec < 1000000
|
// pre: 0 <= tv_usec < 1000000
|
||||||
int run_loop_timer_compare(timer_source_t *a, timer_source_t *b){
|
static int posix_timer_compare(timer_source_t *a, timer_source_t *b){
|
||||||
if (!a && !b) return 0;
|
if (!a && !b) return 0;
|
||||||
if (!a) return -1;
|
if (!a) return -1;
|
||||||
if (!b) return 1;
|
if (!b) return 1;
|
||||||
return run_loop_timeval_compare(&a->timeout, &b->timeout);
|
return posix_timeval_compare(&a->timeout, &b->timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void posix_init(void){
|
static void posix_init(void){
|
||||||
data_sources = NULL;
|
data_sources = NULL;
|
||||||
timers = NULL;
|
timers = NULL;
|
||||||
}
|
}
|
||||||
@ -243,8 +245,9 @@ run_loop_t run_loop_posix = {
|
|||||||
&posix_init,
|
&posix_init,
|
||||||
&posix_add_data_source,
|
&posix_add_data_source,
|
||||||
&posix_remove_data_source,
|
&posix_remove_data_source,
|
||||||
|
&posix_set_timer,
|
||||||
&posix_add_timer,
|
&posix_add_timer,
|
||||||
&posix_remove_timer,
|
&posix_remove_timer,
|
||||||
&posix_execute,
|
&posix_execute,
|
||||||
&posix_dump_timer
|
&posix_dump_timer,
|
||||||
};
|
};
|
||||||
|
@ -52,12 +52,6 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_TIME
|
|
||||||
// compare timeval or timers - NULL is assumed to be before the Big Bang
|
|
||||||
int run_loop_timeval_compare(struct timeval *a, struct timeval *b);
|
|
||||||
int run_loop_timer_compare(timer_source_t *a, timer_source_t *b);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
//
|
||||||
void run_loop_timer_dump(void);
|
void run_loop_timer_dump(void);
|
||||||
|
|
||||||
@ -66,6 +60,7 @@ typedef struct {
|
|||||||
void (*init)(void);
|
void (*init)(void);
|
||||||
void (*add_data_source)(data_source_t *dataSource);
|
void (*add_data_source)(data_source_t *dataSource);
|
||||||
int (*remove_data_source)(data_source_t *dataSource);
|
int (*remove_data_source)(data_source_t *dataSource);
|
||||||
|
void (*set_timer)(timer_source_t * timer, uint32_t timeout_in_ms);
|
||||||
void (*add_timer)(timer_source_t *timer);
|
void (*add_timer)(timer_source_t *timer);
|
||||||
int (*remove_timer)(timer_source_t *timer);
|
int (*remove_timer)(timer_source_t *timer);
|
||||||
void (*execute)(void);
|
void (*execute)(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user