achitectural change: hci_transport implementations must add itself to the run loop

This commit is contained in:
matthias.ringwald 2009-07-09 18:49:43 +00:00
parent 9320a67b01
commit 9bf70ccba9
7 changed files with 59 additions and 52 deletions

View File

@ -59,6 +59,7 @@
9CC813A10FFC0774002816F9 /* btstack.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = btstack.c; path = src/btstack.c; sourceTree = "<group>"; };
9CC813A30FFC0A51002816F9 /* daemon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = daemon.h; path = src/daemon.h; sourceTree = "<group>"; };
9CC813A40FFC0A51002816F9 /* daemon.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = daemon.c; path = src/daemon.c; sourceTree = "<group>"; };
9CEB22DC1005345400FA2B98 /* hci_transport_usb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = hci_transport_usb.h; path = src/hci_transport_usb.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -100,6 +101,7 @@
9C46FC380FA906F700ABEF05 /* hci_transport.h */,
9C46FC370FA906F700ABEF05 /* hci_transport_h4.h */,
9C46FC360FA906F700ABEF05 /* hci_transport_h4.c */,
9CEB22DC1005345400FA2B98 /* hci_transport_usb.h */,
9C2071F210014D3200A07EA4 /* hci_transport_usb.c */,
9C88500C0FBF6702004980E4 /* l2cap.c */,
9C88500D0FBF6702004980E4 /* l2cap.h */,

View File

@ -23,6 +23,7 @@
#include "hci.h"
#include "hci_transport_h4.h"
#include "hci_transport_usb.h"
#include "hci_dump.h"
#include "bt_control_iphone.h"
@ -32,6 +33,7 @@
#include "socket_server.h"
#include "daemon.h"
#include <libusb-1.0/libusb.h>
hci_con_handle_t con_handle_out = 0;
hci_con_handle_t con_handle_in = 0;
@ -255,9 +257,13 @@ int daemon_main (int argc, const char * argv[]){
// use logger: format HCI_DUMP_PACKETLOGGER or HCI_DUMP_BLUEZ
hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
#if 1
// H4 UART
transport = hci_transport_h4_instance();
#else
transport = hci_transport_usb_instance();
#endif
// init HCI
hci_init(transport, &config, control);
@ -273,15 +279,16 @@ int daemon_main (int argc, const char * argv[]){
// turn on
hci_power_control(HCI_POWER_ON);
// configure run loop
run_loop_add( (data_source_t *) transport);
// create server
data_source_t *socket_server = socket_server_create_tcp(1919);
run_loop_add( socket_server );
socket_server_create_tcp(1919);
// go!
run_loop_execute();
/*
while (1) {
libusb_handle_events(NULL);
hci_run();
}
*/
return 0;
}

View File

@ -11,6 +11,8 @@
#include <stdint.h>
#include "run_loop.h"
/* HCI packet types */
/**
* Indicates (first byte) that this pkt to the bluetooth module is a command pkt. The
* module will send a #HCI_EVENT_PACKET for response.
@ -33,7 +35,6 @@
#define HCI_EVENT_PACKET 0x04
typedef struct {
data_source_t ds;
int (*open)(void *transport_config);
int (*close)();
int (*send_cmd_packet)(uint8_t *packet, int size);

View File

@ -22,9 +22,15 @@ typedef enum {
H4_W4_ACL_PAYLOAD
} H4_STATE;
// single instance
static hci_transport_t * hci_transport_h4 = NULL;
typedef struct hci_transport_h4 {
hci_transport_t transport;
data_source_t *ds;
} hci_transport_h4_t;
// single instance
static hci_transport_h4_t * hci_transport_h4 = NULL;
static int h4_process(struct data_source *ds, int ready);
static void dummy_handler(uint8_t *packet, int size);
static hci_uart_config_t *hci_uart_config;
@ -100,7 +106,11 @@ static int h4_open(void *transport_config){
}
// set up data_source
hci_transport_h4->ds.fd = fd;
hci_transport_h4->ds = malloc(sizeof(data_source_t));
if (!hci_transport_h4) return -1;
hci_transport_h4->ds->fd = fd;
hci_transport_h4->ds->process = h4_process;
run_loop_add(hci_transport_h4->ds);
// init state machine
bytes_to_read = 1;
@ -111,21 +121,23 @@ static int h4_open(void *transport_config){
}
static int h4_close(){
close(hci_transport_h4->ds.fd);
hci_transport_h4->ds.fd = 0;
close(hci_transport_h4->ds->fd);
free(hci_transport_h4->ds);
hci_transport_h4->ds = NULL;
return 0;
}
static int h4_send_cmd_packet(uint8_t *packet, int size){
if (hci_transport_h4->ds.fd == 0) return -1;
if (hci_transport_h4->ds == NULL) return -1;
if (hci_transport_h4->ds->fd == 0) return -1;
char *data = (char*) packet;
char packet_type = HCI_COMMAND_DATA_PACKET;
hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
write(hci_transport_h4->ds.fd, &packet_type, 1);
write(hci_transport_h4->ds->fd, &packet_type, 1);
while (size > 0) {
int bytes_written = write(hci_transport_h4->ds.fd, data, size);
int bytes_written = write(hci_transport_h4->ds->fd, data, size);
if (bytes_written < 0) {
return bytes_written;
}
@ -136,16 +148,16 @@ static int h4_send_cmd_packet(uint8_t *packet, int size){
}
static int h4_send_acl_packet(uint8_t *packet, int size){
if (hci_transport_h4->ds.fd == 0) return -1;
if (hci_transport_h4->ds->fd == 0) return -1;
char *data = (char*) packet;
char packet_type = HCI_ACL_DATA_PACKET;
hci_dump_packet( (uint8_t) packet_type, 0, packet, size);
write(hci_transport_h4->ds.fd, &packet_type, 1);
write(hci_transport_h4->ds->fd, &packet_type, 1);
while (size > 0) {
int bytes_written = write(hci_transport_h4->ds.fd, data, size);
int bytes_written = write(hci_transport_h4->ds->fd, data, size);
if (bytes_written < 0) {
return bytes_written;
}
@ -164,10 +176,10 @@ static void h4_register_acl_packet_handler (void (*handler)(uint8_t *packet,
}
static int h4_process(struct data_source *ds, int ready) {
if (hci_transport_h4->ds.fd == 0) return -1;
if (hci_transport_h4->ds->fd == 0) return -1;
// read up to bytes_to_read data in
int bytes_read = read(hci_transport_h4->ds.fd, &hci_packet[read_pos], bytes_to_read);
int bytes_read = read(hci_transport_h4->ds->fd, &hci_packet[read_pos], bytes_to_read);
if (bytes_read < 0) {
return bytes_read;
}
@ -229,16 +241,15 @@ static void dummy_handler(uint8_t *packet, int size){
// get h4 singleton
hci_transport_t * hci_transport_h4_instance() {
if (hci_transport_h4 == NULL) {
hci_transport_h4 = malloc( sizeof(hci_transport_t));
hci_transport_h4->ds.fd = 0;
hci_transport_h4->ds.process = h4_process;
hci_transport_h4->open = h4_open;
hci_transport_h4->close = h4_close;
hci_transport_h4->send_cmd_packet = h4_send_cmd_packet;
hci_transport_h4->send_acl_packet = h4_send_acl_packet;
hci_transport_h4->register_event_packet_handler = h4_register_event_packet_handler;
hci_transport_h4->register_acl_packet_handler = h4_register_acl_packet_handler;
hci_transport_h4->get_transport_name = h4_get_transport_name;
hci_transport_h4 = malloc( sizeof(hci_transport_h4_t));
hci_transport_h4->ds = NULL;
hci_transport_h4->transport.open = h4_open;
hci_transport_h4->transport.close = h4_close;
hci_transport_h4->transport.send_cmd_packet = h4_send_cmd_packet;
hci_transport_h4->transport.send_acl_packet = h4_send_acl_packet;
hci_transport_h4->transport.register_event_packet_handler = h4_register_event_packet_handler;
hci_transport_h4->transport.register_acl_packet_handler = h4_register_acl_packet_handler;
hci_transport_h4->transport.get_transport_name = h4_get_transport_name;
}
return hci_transport_h4;
}

View File

@ -8,7 +8,4 @@
#include "hci_transport.h"
/* HCI packet types */
extern hci_transport_t * hci_transport_h4_instance();

View File

@ -207,27 +207,23 @@ static int usb_open(void *transport_config){
// interrupt (= HCI event) handler
libusb_fill_interrupt_transfer(interrupt_transfer, handle, 0x81, hci_event_buffer, 260, event_callback, NULL, 3000) ;
interrupt_transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;
// interrupt_transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;
r = libusb_submit_transfer(interrupt_transfer);
if (r) {
printf("Error submitting interrupt transfer %d\n", r);
}
printf("interrupt started\n");
// set up data_source
// set up data_sources
const struct libusb_pollfd ** pollfd = libusb_get_pollfds(NULL);
for (r = 0 ; pollfd[r] ; r++) {
data_source_t *ds = malloc(sizeof(data_source_t));
ds->fd = pollfd[r]->fd;
ds->process = usb_process;
run_loop_add(ds);
printf("%u: %x fd: %u, events %x\n", r, pollfd[r], pollfd[r]->fd, pollfd[r]->events);
}
// HACK
hci_transport_usb->ds.fd = pollfd[0]->fd;
hci_transport_usb->ds.process = usb_process;
data_source_t *second_ds = malloc(sizeof(data_source_t));
second_ds->fd = pollfd[1]->fd;
second_ds->process = usb_process;
run_loop_add(second_ds);
// init state machine
// bytes_to_read = 1;
// usb_state = USB_W4_PACKET_TYPE;
@ -262,7 +258,7 @@ static int usb_open(void *transport_config){
#endif
static int usb_close(){
hci_transport_usb->ds.fd = 0;
// @TODO remove all run loops!
switch (libusb_state){
case LIB_USB_TRANSFERS_ALLOCATED:
@ -333,8 +329,6 @@ static void dummy_handler(uint8_t *packet, int size){
hci_transport_t * hci_transport_usb_instance() {
if (!hci_transport_usb) {
hci_transport_usb = malloc( sizeof(hci_transport_t));
hci_transport_usb->ds.fd = 0;
hci_transport_usb->ds.process = usb_process;
hci_transport_usb->open = usb_open;
hci_transport_usb->close = usb_close;
hci_transport_usb->send_cmd_packet = usb_send_cmd_packet;

View File

@ -6,11 +6,6 @@
* Created by Matthias Ringwald on 4/29/09.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include "daemon.h"
int main (int argc, const char * argv[]) {