2009-10-29 20:25:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 by Matthias Ringwald
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-06-06 17:43:50 +00:00
|
|
|
/*
|
|
|
|
* run_loop.c
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 6/6/09.
|
|
|
|
*/
|
|
|
|
|
2009-09-28 21:19:05 +00:00
|
|
|
#include <btstack/run_loop.h>
|
|
|
|
#include <btstack/linked_list.h>
|
2009-07-13 21:51:42 +00:00
|
|
|
|
2011-05-05 18:45:28 +00:00
|
|
|
#include "debug.h"
|
2010-06-29 20:58:45 +00:00
|
|
|
#include "run_loop_private.h"
|
|
|
|
|
2009-06-06 19:00:32 +00:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <stdlib.h>
|
2009-08-09 13:00:09 +00:00
|
|
|
#include <stdio.h>
|
2009-06-06 17:43:50 +00:00
|
|
|
|
2011-04-30 20:11:20 +00:00
|
|
|
void posix_dump_timer();
|
|
|
|
|
2009-06-06 17:43:50 +00:00
|
|
|
// the run loop
|
2009-10-07 20:17:19 +00:00
|
|
|
static linked_list_t data_sources;
|
2011-04-30 20:11:20 +00:00
|
|
|
static int data_sources_modified;
|
2009-10-07 20:17:19 +00:00
|
|
|
static linked_list_t timers;
|
2009-06-06 19:00:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data_source to run_loop
|
|
|
|
*/
|
2009-10-07 20:17:19 +00:00
|
|
|
void posix_add_data_source(data_source_t *ds){
|
2011-04-30 20:11:20 +00:00
|
|
|
data_sources_modified = 1;
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_add_data_source %x with fd %u\n", (int) ds, ds->fd);
|
2009-08-09 13:00:09 +00:00
|
|
|
linked_list_add(&data_sources, (linked_item_t *) ds);
|
2009-06-06 19:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove data_source from run loop
|
|
|
|
*/
|
2009-10-07 20:17:19 +00:00
|
|
|
int posix_remove_data_source(data_source_t *ds){
|
2011-04-30 20:11:20 +00:00
|
|
|
data_sources_modified = 1;
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_remove_data_source %x\n", (int) ds);
|
2009-08-09 13:00:09 +00:00
|
|
|
return linked_list_remove(&data_sources, (linked_item_t *) ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add timer to run_loop (keep list sorted)
|
|
|
|
*/
|
2009-11-23 19:45:32 +00:00
|
|
|
void posix_add_timer(timer_source_t *ts){
|
2009-08-09 13:00:09 +00:00
|
|
|
linked_item_t *it;
|
2009-11-05 22:26:43 +00:00
|
|
|
for (it = (linked_item_t *) &timers; it->next ; it = it->next){
|
2011-04-30 20:11:20 +00:00
|
|
|
if ((timer_source_t *) it->next == ts){
|
2011-05-05 18:45:28 +00:00
|
|
|
log_err( "run_loop_timer_add error: timer to add already in list!\n");
|
2011-04-30 20:11:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (run_loop_timer_compare( (timer_source_t *) it->next, ts) > 0) {
|
2009-11-05 22:26:43 +00:00
|
|
|
break;
|
2009-08-09 13:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-05 22:26:43 +00:00
|
|
|
ts->item.next = it->next;
|
|
|
|
it->next = (linked_item_t *) ts;
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("Added timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
2009-11-05 22:26:43 +00:00
|
|
|
// posix_dump_timer();
|
2009-08-09 13:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove timer from run loop
|
|
|
|
*/
|
2009-11-23 19:45:32 +00:00
|
|
|
int posix_remove_timer(timer_source_t *ts){
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("Removed timer %x at %u\n", (int) ts, (unsigned int) ts->timeout.tv_sec);
|
2011-04-30 20:11:20 +00:00
|
|
|
// posix_dump_timer();
|
2009-08-09 13:00:09 +00:00
|
|
|
return linked_list_remove(&timers, (linked_item_t *) ts);
|
|
|
|
}
|
|
|
|
|
2009-10-07 20:17:19 +00:00
|
|
|
void posix_dump_timer(){
|
2009-08-09 13:00:09 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
int i = 0;
|
|
|
|
for (it = (linked_item_t *) timers; it ; it = it->next){
|
2009-11-23 19:45:32 +00:00
|
|
|
timer_source_t *ts = (timer_source_t*) it;
|
2011-05-05 18:45:28 +00:00
|
|
|
log_dbg("timer %u, timeout %u\n", i, (unsigned int) ts->timeout.tv_sec);
|
2009-08-09 13:00:09 +00:00
|
|
|
}
|
2009-06-06 19:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute run_loop
|
|
|
|
*/
|
2009-10-07 20:17:19 +00:00
|
|
|
void posix_execute() {
|
2009-06-06 19:00:32 +00:00
|
|
|
fd_set descriptors;
|
|
|
|
data_source_t *ds;
|
2009-11-23 19:45:32 +00:00
|
|
|
timer_source_t *ts;
|
2009-11-05 22:26:43 +00:00
|
|
|
struct timeval current_tv;
|
|
|
|
struct timeval next_tv;
|
2009-08-09 13:49:02 +00:00
|
|
|
struct timeval *timeout;
|
2009-08-09 13:00:09 +00:00
|
|
|
|
2009-06-06 19:00:32 +00:00
|
|
|
while (1) {
|
|
|
|
// collect FDs
|
|
|
|
FD_ZERO(&descriptors);
|
|
|
|
int highest_fd = 0;
|
2009-08-09 13:00:09 +00:00
|
|
|
for (ds = (data_source_t *) data_sources; ds ; ds = (data_source_t *) ds->item.next){
|
2009-06-06 19:00:32 +00:00
|
|
|
if (ds->fd) {
|
|
|
|
FD_SET(ds->fd, &descriptors);
|
|
|
|
if (ds->fd > highest_fd) {
|
|
|
|
highest_fd = ds->fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-09 13:00:09 +00:00
|
|
|
|
|
|
|
// get next timeout
|
2009-08-09 13:49:02 +00:00
|
|
|
// pre: 0 <= tv_usec < 1000000
|
|
|
|
timeout = NULL;
|
|
|
|
if (timers) {
|
2009-11-05 22:26:43 +00:00
|
|
|
gettimeofday(¤t_tv, NULL);
|
2009-11-23 19:45:32 +00:00
|
|
|
ts = (timer_source_t *) timers;
|
2009-11-05 22:26:43 +00:00
|
|
|
next_tv.tv_usec = ts->timeout.tv_usec - current_tv.tv_usec;
|
|
|
|
next_tv.tv_sec = ts->timeout.tv_sec - current_tv.tv_sec;
|
|
|
|
while (next_tv.tv_usec < 0){
|
|
|
|
next_tv.tv_usec += 1000000;
|
|
|
|
next_tv.tv_sec--;
|
2009-08-09 13:49:02 +00:00
|
|
|
}
|
2009-11-05 22:26:43 +00:00
|
|
|
if (next_tv.tv_sec < 0 || (next_tv.tv_sec == 0 && next_tv.tv_usec < 0)){
|
|
|
|
next_tv.tv_sec = 0;
|
|
|
|
next_tv.tv_usec = 0;
|
2009-08-09 13:49:02 +00:00
|
|
|
}
|
2009-11-05 22:26:43 +00:00
|
|
|
timeout = &next_tv;
|
2009-08-09 13:49:02 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 19:00:32 +00:00
|
|
|
// wait for ready FDs
|
2009-08-09 13:49:02 +00:00
|
|
|
select( highest_fd+1 , &descriptors, NULL, NULL, timeout);
|
|
|
|
|
2011-04-30 20:11:20 +00:00
|
|
|
// process data sources very carefully
|
|
|
|
// bt_control.close() triggered from a client can remove a different data source
|
|
|
|
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_execute: before ds check\n");
|
2011-04-30 20:11:20 +00:00
|
|
|
data_sources_modified = 0;
|
|
|
|
for (ds = (data_source_t *) data_sources; !data_sources_modified && ds != NULL; ds = (data_source_t *) ds->item.next){
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_execute: check %x with fd %u\n", (int) ds, ds->fd);
|
2009-06-06 21:10:20 +00:00
|
|
|
if (FD_ISSET(ds->fd, &descriptors)) {
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_execute: process %x with fd %u\n", (int) ds, ds->fd);
|
2009-08-09 13:00:09 +00:00
|
|
|
ds->process(ds);
|
2009-06-06 21:10:20 +00:00
|
|
|
}
|
2009-06-06 19:00:32 +00:00
|
|
|
}
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_execute: after ds check\n");
|
2009-08-09 13:00:09 +00:00
|
|
|
|
|
|
|
// process timers
|
2009-08-09 13:49:02 +00:00
|
|
|
// pre: 0 <= tv_usec < 1000000
|
|
|
|
while (timers) {
|
2009-11-05 22:26:43 +00:00
|
|
|
gettimeofday(¤t_tv, NULL);
|
2009-11-23 19:45:32 +00:00
|
|
|
ts = (timer_source_t *) timers;
|
2009-11-05 22:26:43 +00:00
|
|
|
if (ts->timeout.tv_sec > current_tv.tv_sec) break;
|
|
|
|
if (ts->timeout.tv_sec == current_tv.tv_sec && ts->timeout.tv_usec > current_tv.tv_usec) break;
|
2011-05-05 18:45:28 +00:00
|
|
|
// log_dbg("posix_execute: process times %x\n", (int) ts);
|
2011-04-30 20:11:20 +00:00
|
|
|
|
|
|
|
// remove timer before processing it to allow handler to re-register with run loop
|
2009-08-09 13:49:02 +00:00
|
|
|
run_loop_remove_timer(ts);
|
|
|
|
ts->process(ts);
|
|
|
|
}
|
2009-06-06 19:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-06 17:43:50 +00:00
|
|
|
|
2009-10-07 20:17:19 +00:00
|
|
|
void posix_init(){
|
|
|
|
data_sources = NULL;
|
|
|
|
timers = NULL;
|
|
|
|
}
|
2009-06-06 17:43:50 +00:00
|
|
|
|
2009-10-07 20:17:19 +00:00
|
|
|
const run_loop_t run_loop_posix = {
|
|
|
|
&posix_init,
|
|
|
|
&posix_add_data_source,
|
|
|
|
&posix_remove_data_source,
|
|
|
|
&posix_add_timer,
|
|
|
|
&posix_remove_timer,
|
|
|
|
&posix_execute,
|
|
|
|
&posix_dump_timer
|
|
|
|
};
|