use new run_loop to handle BT stack

This commit is contained in:
matthias.ringwald 2009-06-06 19:00:32 +00:00
parent 8d4343fb95
commit 94ab26f8d1
3 changed files with 91 additions and 22 deletions

View File

@ -119,6 +119,9 @@ static void dummy_handler(uint8_t *packet, int size){
static void acl_handler(uint8_t *packet, int size){
hci_stack.acl_packet_handler(packet, size);
// execute main loop
hci_run();
}
static void event_handler(uint8_t *packet, int size){
@ -159,6 +162,9 @@ static void event_handler(uint8_t *packet, int size){
}
hci_stack.event_packet_handler(packet, size);
// execute main loop
hci_run();
}
/** Register L2CAP handlers */

View File

@ -17,6 +17,8 @@
#include "l2cap.h"
#include "run_loop.h"
static hci_transport_t * transport;
static hci_uart_config_t config;
@ -26,8 +28,8 @@ uint16_t dest_cid;
void event_handler(uint8_t *packet, int size){
// printf("Event type: %x, opcode: %x, other %x\n", packet[0], packet[3] | packet[4] << 8);
#if 0
// bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard
#if 1
bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard
// bd_addr_t addr = { 0x00, 0x16, 0xcb, 0x09, 0x94, 0xa9}; // MacBook Pro
// bt stack activated, set authentication enabled
@ -107,11 +109,16 @@ void acl_handler(uint8_t *packet, int size){
}
}
int transport_run_loop_cb(data_source_t * ds, int ready){
transport->handle_data();
return 0;
}
int main (int argc, const char * argv[]) {
bt_control_t * control = NULL;
#if 0
#if 1
//
if (argc <= 1){
printf("HCI Daemon tester. Specify device name for Ericsson ROK 101 007\n");
@ -151,24 +158,17 @@ int main (int argc, const char * argv[]) {
// init L2CAP
l2cap_init();
//
fd_set descriptors;
FD_ZERO(&descriptors);
while (1){
// handle HCI
hci_run();
// get fd for select call
int transport_fd = transport->get_fd();
FD_SET(transport_fd, &descriptors);
// int ready =
select( transport_fd+1, &descriptors, NULL, NULL, NULL);
// printf("Ready: %d, isset() = %u\n", ready, FD_ISSET(transport_fd, &descriptors));
// handle incoming data from BT module
transport->handle_data();
}
// trigger first hci action
hci_run();
// configure run loop
data_source_t hci_transport;
hci_transport.fd = transport->get_fd();
hci_transport.process = &transport_run_loop_cb;
run_loop_add(&hci_transport);
// go!
run_loop_execute();
return 0;
}

View File

@ -5,9 +5,72 @@
*/
#include "run_loop.h"
#include <sys/select.h>
#include <stdlib.h>
// the run loop
static data_source_t the_run_loop;
static data_source_t * the_run_loop = NULL;
/**
* Add data_source to run_loop
*/
void run_loop_add(data_source_t *ds){
// check if already in list
data_source_t *it;
for (it = the_run_loop; it ; it = it->next){
if (it == ds) {
return;
}
}
// add first
ds->next = the_run_loop;
the_run_loop = ds;
}
/**
* Remove data_source from run loop
*
* @note: assumes that data_source_t.next is first element in data_source
*/
int run_loop_remove(data_source_t *ds){
data_source_t *it;
for (it = (data_source_t *) &the_run_loop; it ; it = it->next){
if (it->next == ds){
it->next = ds->next;
return 0;
}
}
return -1;
}
/**
* Execute run_loop
*/
void run_loop_execute() {
fd_set descriptors;
data_source_t *ds;
int highest;
while (1) {
// collect FDs
FD_ZERO(&descriptors);
int highest_fd = 0;
for (ds = the_run_loop; ds ; ds = ds->next){
if (ds->fd) {
FD_SET(ds->fd, &descriptors);
if (ds->fd > highest_fd) {
highest_fd = ds->fd;
}
}
}
// wait for ready FDs
select( highest+1, &descriptors, NULL, NULL, NULL);
// process input
for (ds = the_run_loop; ds != NULL ; ds = ds->next){
ds->process(ds, FD_ISSET(ds->fd, &descriptors));
}
}
}