implemented basic linked list and used it for the run loop

This commit is contained in:
matthias.ringwald 2009-07-13 21:51:42 +00:00
parent 855ca3c659
commit 4918f63ce1
6 changed files with 21 additions and 27 deletions

View File

@ -13,6 +13,7 @@
9C20720F10025E0500A07EA4 /* libusb-1.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C20720E10025E0500A07EA4 /* libusb-1.0.dylib */; };
9C46FC390FA906F700ABEF05 /* hci.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C46FC340FA906F700ABEF05 /* hci.c */; };
9C46FC3A0FA906F700ABEF05 /* hci_transport_h4.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C46FC360FA906F700ABEF05 /* hci_transport_h4.c */; };
9C7B5AC0100BD3340065D87E /* linked_list.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7B5ABF100BD3340065D87E /* linked_list.c */; };
9C7ECB840FCC85650085DAC5 /* bt_control_iphone.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7ECB820FCC85650085DAC5 /* bt_control_iphone.c */; };
9C7ECBB50FCC95DD0085DAC5 /* hci_dump.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C7ECBB40FCC95DD0085DAC5 /* hci_dump.c */; };
9C88500E0FBF6702004980E4 /* l2cap.c in Sources */ = {isa = PBXBuildFile; fileRef = 9C88500C0FBF6702004980E4 /* l2cap.c */; };
@ -45,6 +46,8 @@
9C46FC360FA906F700ABEF05 /* hci_transport_h4.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; name = hci_transport_h4.c; path = src/hci_transport_h4.c; sourceTree = "<group>"; };
9C46FC370FA906F700ABEF05 /* hci_transport_h4.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = hci_transport_h4.h; path = src/hci_transport_h4.h; sourceTree = "<group>"; };
9C46FC380FA906F700ABEF05 /* hci_transport.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = hci_transport.h; path = src/hci_transport.h; sourceTree = "<group>"; };
9C7B5ABE100BD3340065D87E /* linked_list.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = linked_list.h; path = src/linked_list.h; sourceTree = "<group>"; };
9C7B5ABF100BD3340065D87E /* linked_list.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = linked_list.c; path = src/linked_list.c; sourceTree = "<group>"; };
9C7ECB810FCC85650085DAC5 /* bt_control.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = bt_control.h; path = src/bt_control.h; sourceTree = "<group>"; };
9C7ECB820FCC85650085DAC5 /* bt_control_iphone.c */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.c; name = bt_control_iphone.c; path = src/bt_control_iphone.c; sourceTree = "<group>"; };
9C7ECB830FCC85650085DAC5 /* bt_control_iphone.h */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = sourcecode.c.h; name = bt_control_iphone.h; path = src/bt_control_iphone.h; sourceTree = "<group>"; };
@ -105,6 +108,8 @@
9C88500D0FBF6702004980E4 /* l2cap.h */,
9C7ECBB30FCC95DD0085DAC5 /* hci_dump.h */,
9C7ECBB40FCC95DD0085DAC5 /* hci_dump.c */,
9C7B5ABE100BD3340065D87E /* linked_list.h */,
9C7B5ABF100BD3340065D87E /* linked_list.c */,
9C1F0E990FDAE023008F472F /* run_loop.h */,
9C1F0E980FDAE023008F472F /* run_loop.c */,
9C1F0F320FDB024B008F472F /* socket_server.c */,
@ -182,6 +187,7 @@
9CC813A20FFC0774002816F9 /* btstack.c in Sources */,
9CC813A50FFC0A51002816F9 /* daemon.c in Sources */,
9C2071F310014D3200A07EA4 /* hci_transport_usb.c in Sources */,
9C7B5AC0100BD3340065D87E /* linked_list.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -12,6 +12,7 @@ BTdaemon_SOURCES = \
hci_transport_h4.c \
$(usb_support) \
l2cap.c \
linked_list.c \
run_loop.c \
socket_server.c
@ -23,6 +24,7 @@ include_HEADERS = \
hci_transport.h \
hci_transport_h4.h \
hci_transport_usb.h \
linked_list.h \
l2cap.h \
run_loop.h \
socket_server.h

View File

@ -25,7 +25,7 @@
#include "hci_dump.h"
#include "l2cap.h"
#include "linked_list.h"
#include "run_loop.h"
#include "socket_server.h"

View File

@ -5,42 +5,26 @@
*/
#include "run_loop.h"
#include "linked_list.h"
#include <sys/select.h>
#include <stdlib.h>
// the run loop
static data_source_t * the_run_loop = NULL;
static linked_list_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;
linked_list_add(&the_run_loop, (linked_item_t *) 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;
return linked_list_remove(&the_run_loop, (linked_item_t *) ds);
}
/**
@ -53,7 +37,7 @@ void run_loop_execute() {
// collect FDs
FD_ZERO(&descriptors);
int highest_fd = 0;
for (ds = the_run_loop; ds ; ds = ds->next){
for (ds = (data_source_t *) the_run_loop; ds ; ds = (data_source_t *) ds->item.next){
if (ds->fd) {
FD_SET(ds->fd, &descriptors);
if (ds->fd > highest_fd) {
@ -66,8 +50,8 @@ void run_loop_execute() {
// process input
data_source_t *next;
for (ds = the_run_loop; ds != NULL ; ds = next){
next = ds->next; // cache pointer to next data_source to allow data source to remove itself
for (ds = (data_source_t *) the_run_loop; ds != NULL ; ds = (data_source_t *) ds->item.next){
next = (data_source_t *) ds->item.next; // cache pointer to next data_source to allow data source to remove itself
if (FD_ISSET(ds->fd, &descriptors)) {
ds->process(ds, FD_ISSET(ds->fd, &descriptors));
}

View File

@ -6,8 +6,10 @@
#pragma once
#include "linked_list.h"
typedef struct data_source {
struct data_source *next; // <-- used internally by RunLoop
linked_item_t item;
int fd; // <-- file descriptors to watch or 0
int (*process)(struct data_source *ds, int ready); // <-- do processing, @return: more to do
} data_source_t;

View File

@ -92,7 +92,7 @@ static int socket_server_connection_process(struct data_source *ds, int ready) {
connection_t *conn = (connection_t *) ds;
int bytes_read = read(ds->fd, &conn->buffer[conn->bytes_read],
sizeof(packet_header_t) - conn->bytes_read);
if (bytes_read < 0){
if (bytes_read <= 0){
//
}
conn->bytes_read += bytes_read;