keep track of HCI connections

This commit is contained in:
matthias.ringwald 2009-07-24 21:13:53 +00:00
parent b77926b5ca
commit fe1ed1b8b6
6 changed files with 64 additions and 7 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -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<BD_ADDR_LEN-1;i++){
printf("%02X-", ((uint8_t *)addr)[i]);
}
printf("%02X", ((uint8_t *)addr)[i]);
}

View File

@ -37,5 +37,7 @@ void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
void bt_flip_addr(bd_addr_t dest, bd_addr_t src);
void hexdump(void *data, int size);
void print_bd_addr( bd_addr_t addr);
#define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN)
#define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN)