diff --git a/src/daemon.c b/src/daemon.c index d36b47964..6d2b2e7b9 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -41,11 +41,15 @@ static int daemon_packet_handler(connection_t *connection, uint8_t packet_type, switch (packet_type){ case HCI_COMMAND_DATA_PACKET: hci_send_cmd_packet(data, length); + // printf("CMD from client: "); break; case HCI_ACL_DATA_PACKET: hci_send_acl_packet(data, length); + // printf("ACL from client: "); break; } + hexdump(data, length); + // printf("\n"); return 0; } diff --git a/src/hci.c b/src/hci.c index 5d564681e..3f1f29bf7 100644 --- a/src/hci.c +++ b/src/hci.c @@ -20,7 +20,7 @@ static hci_stack_t hci_stack; * * @return connection OR NULL, if not found */ -static hci_connection_t *link_for_addr(bd_addr_t address){ +static hci_connection_t * connection_for_address(bd_addr_t address){ linked_item_t *it; for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ @@ -35,7 +35,7 @@ static hci_connection_t *link_for_addr(bd_addr_t address){ * * @return connection OR NULL, if not found */ -static hci_connection_t *link_for_handle(hci_con_handle_t con_handle){ +static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ linked_item_t *it; for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ if ( ((hci_connection_t *) it)->con_handle == con_handle){ @@ -76,6 +76,7 @@ static void acl_handler(uint8_t *packet, int size){ static void event_handler(uint8_t *packet, int size){ bd_addr_t addr; + hci_con_handle_t handle; // Get Num_HCI_Command_Packets if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || @@ -83,6 +84,42 @@ static void event_handler(uint8_t *packet, int size){ hci_stack.num_cmd_packets = packet[2]; } + // Connection management + if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { + if (!packet[2]){ + bt_flip_addr(addr, &packet[5]); + hci_connection_t * conn = connection_for_address(addr); + if (!conn) { + conn = malloc( sizeof(hci_connection_t) ); + if (conn) { + linked_list_add(&hci_stack.connections, (linked_item_t *) conn); + } + } + if (conn) { + BD_ADDR_COPY(conn->address, addr); + conn->con_handle = READ_BT_16(packet, 3); + conn->flags = 0; + printf("New connection: handle %u, ", conn->con_handle); + print_bd_addr( conn->address ); + printf("\n"); + } + } + } + + if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { + if (!packet[2]){ + handle = READ_BT_16(packet, 3); + hci_connection_t * conn = connection_for_handle(handle); + if (conn) { + printf("Connection closed: handle %u, ", conn->con_handle); + print_bd_addr( conn->address ); + printf("\n"); + linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); + free( conn ); + } + } + } + // handle BT initialization if (hci_stack.state == HCI_STATE_INITIALIZING){ // handle H4 synchronization loss on restart @@ -140,6 +177,9 @@ void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ // reference to used config hci_stack.config = config; + // no connections yet + hci_stack.connections = NULL; + // empty cmd buffer hci_stack.hci_cmd_buffer = malloc(3+255); diff --git a/src/hci.h b/src/hci.h index ed818cfd0..cbccdc581 100644 --- a/src/hci.h +++ b/src/hci.h @@ -44,7 +44,7 @@ typedef enum { typedef struct hci_connection { // linked list - assert: first field - linked_item_t item; + linked_item_t item; // remote side bd_addr_t address; @@ -68,7 +68,7 @@ typedef struct { bt_control_t * control; // list of existing baseband connections - hci_connection_t * connections; + linked_list_t connections; // single buffer for HCI Command assembly uint8_t * hci_cmd_buffer; diff --git a/src/hci_dump.c b/src/hci_dump.c index b5a4101f6..eab3e3ba5 100644 --- a/src/hci_dump.c +++ b/src/hci_dump.c @@ -1,8 +1,12 @@ /* * hci_dump.c * - * Dump HCI trace in BlueZ's hcidump format - * + * Dump HCI trace in various formats: + * + * - BlueZ's hcidump format + * - Apple's PacketLogger + * - stdout hexdump + * * Created by Matthias Ringwald on 5/26/09. */ @@ -52,7 +56,6 @@ void hci_dump_open(char *filename, hci_dump_format_t format){ } } - void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { if (dump_file < 0) return; // not activated yet diff --git a/src/utils.c b/src/utils.c index 1eb30d36a..9ef02d6aa 100644 --- a/src/utils.c +++ b/src/utils.c @@ -37,3 +37,11 @@ void hexdump(void *data, int size){ } printf("\n"); } + +void print_bd_addr( bd_addr_t addr){ + int i; + for (i=0; i