From 8b658ebcf868c8176b3f7e17042b1385b71e098d Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Wed, 27 May 2009 20:22:19 +0000 Subject: [PATCH] cleaned up hci_dump integration, added support for Apple's PacketLogger, also store local timestamp --- src/hci.c | 11 +++++-- src/hci.h | 1 + src/hci_dump.c | 75 +++++++++++++++++++++++++++++++++++++----- src/hci_dump.h | 14 +++----- src/hci_transport_h4.c | 20 +++-------- src/main.c | 8 +++-- 6 files changed, 90 insertions(+), 39 deletions(-) diff --git a/src/hci.c b/src/hci.c index df77ce368..e569a1cc1 100644 --- a/src/hci.c +++ b/src/hci.c @@ -60,8 +60,15 @@ static hci_stack_t hci_stack; void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ - buffer[pos] = value & 0xff; - buffer[pos+1] = value >> 8; + buffer[pos++] = value; + buffer[pos++] = value >> 8; +} + +void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ + buffer[pos++] = value; + buffer[pos++] = value >> 8; + buffer[pos++] = value >> 16; + buffer[pos++] = value >> 24; } void bt_flip_addr(bd_addr_t dest, bd_addr_t src){ diff --git a/src/hci.h b/src/hci.h index c3533dab9..f218621f5 100644 --- a/src/hci.h +++ b/src/hci.h @@ -173,6 +173,7 @@ int hci_send_acl_packet(uint8_t *packet, int size); // helper extern void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); +extern void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); extern hci_cmd_t hci_inquiry; extern hci_cmd_t hci_link_key_request_negative_reply; diff --git a/src/hci_dump.c b/src/hci_dump.c index 1d41b83d8..a18947fe6 100644 --- a/src/hci_dump.c +++ b/src/hci_dump.c @@ -15,21 +15,78 @@ #include // bzero #include // write #include +#include // for timestamps -static int dump_file; +// BLUEZ hcidump +typedef struct { + uint16_t len; + uint8_t in; + uint8_t pad; + uint32_t ts_sec; + uint32_t ts_usec; + uint8_t packet_type; +} __attribute__ ((packed)) hcidump_hdr; -void hci_dump_open(char *filename){ +// APPLE PacketLogger +typedef struct { + uint32_t len; + uint32_t ts_sec; + uint32_t ts_usec; + uint8_t type; +} __attribute__ ((packed)) pktlog_hdr; + +static int dump_file = -1; +static int dump_format; +static hcidump_hdr header_bluez; +static pktlog_hdr header_packetlogger; + +void hci_dump_open(char *filename, hci_dump_format_t format){ dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + dump_format = format; } void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { - static hcidump_hdr header; - bzero( &header, sizeof(hcidump_hdr)); - bt_store_16( (uint8_t *) &header, 0, 1 + len); - header.in = in; - header.packet_type = packet_type; - write (dump_file, &header, sizeof(hcidump_hdr) ); - write (dump_file, packet, len ); + if (dump_file < 0) return; // not activated yet + + // get time + struct timeval curr_time; + gettimeofday(&curr_time, NULL); + + switch (dump_format){ + case HCI_DUMP_BLUEZ: + bt_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len); + header_bluez.in = in; + header_bluez.pad = 0; + bt_store_32( (uint8_t *) &header_bluez.ts_sec, 0, curr_time.tv_sec); + bt_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec); + header_bluez.packet_type = packet_type; + write (dump_file, &header_bluez, sizeof(hcidump_hdr) ); + write (dump_file, packet, len ); + break; + case HCI_DUMP_PACKETLOGGER: + header_packetlogger.len = htonl( sizeof(pktlog_hdr) - 4 + len); + header_packetlogger.ts_sec = htonl(curr_time.tv_sec); + header_packetlogger.ts_usec = htonl(curr_time.tv_usec); + switch (packet_type){ + case HCI_COMMAND_DATA_PACKET: + header_packetlogger.type = 0x00; + break; + case HCI_ACL_DATA_PACKET: + if (in) { + header_packetlogger.type = 0x03; + } else { + header_packetlogger.type = 0x02; + } + break; + case HCI_EVENT_PACKET: + header_packetlogger.type = 0x01; + break; + default: + return; + } + write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) ); + write (dump_file, packet, len ); + } } void hci_dump_close(){ diff --git a/src/hci_dump.h b/src/hci_dump.h index 1e1011555..496a756b8 100644 --- a/src/hci_dump.h +++ b/src/hci_dump.h @@ -8,15 +8,11 @@ #include -typedef struct { - uint16_t len; - uint8_t in; - uint8_t pad; - uint32_t ts_sec; - uint32_t ts_usec; - uint8_t packet_type; -} __attribute__ ((packed)) hcidump_hdr; +typedef enum { + HCI_DUMP_BLUEZ = 0, + HCI_DUMP_PACKETLOGGER +} hci_dump_format_t; -void hci_dump_open(char *filename); +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); void hci_dump_close(); diff --git a/src/hci_transport_h4.c b/src/hci_transport_h4.c index 7bbef7bd6..b51b4b6ef 100644 --- a/src/hci_transport_h4.c +++ b/src/hci_transport_h4.c @@ -14,8 +14,6 @@ #include "hci_transport_h4.h" #include "hci_dump.h" -#define HCI_DUMP - typedef enum { H4_W4_PACKET_TYPE, H4_W4_EVENT_HEADER, @@ -105,17 +103,11 @@ static int h4_open(void *transport_config){ h4_state = H4_W4_PACKET_TYPE; read_pos = 0; -#ifdef HCI_DUMP - hci_dump_open("/tmp/hci_dump.cap"); -#endif - return 0; } static int h4_close(){ -#ifdef HCI_DUMP hci_dump_close(); -#endif return 0; } @@ -126,9 +118,7 @@ static int h4_send_cmd_packet(uint8_t *packet, int size){ char *data = (char*) packet; char packet_type = HCI_COMMAND_DATA_PACKET; -#ifdef HCI_DUMP hci_dump_packet( (uint8_t) packet_type, 0, packet, size); -#endif write(fd, &packet_type, 1); while (size > 0) { @@ -149,9 +139,7 @@ static int h4_send_acl_packet(uint8_t *packet, int size){ char *data = (char*) packet; char packet_type = HCI_ACL_DATA_PACKET; -#ifdef HCI_DUMP hci_dump_packet( (uint8_t) packet_type, 0, packet, size); -#endif write(fd, &packet_type, 1); while (size > 0) { @@ -211,9 +199,9 @@ static int h4_handle_data() { case H4_W4_EVENT_PAYLOAD: printf("EVT: "); hexdump(hci_packet, read_pos); -#ifdef HCI_DUMP + hci_dump_packet( HCI_EVENT_PACKET, 1, hci_packet, read_pos); -#endif + event_packet_handler(hci_packet, read_pos); h4_state = H4_W4_PACKET_TYPE; read_pos = 0; @@ -226,9 +214,9 @@ static int h4_handle_data() { case H4_W4_ACL_PAYLOAD: printf(" #include -#include "bt_control_iphone.h" - #include "hci.h" #include "hci_transport_h4.h" +#include "hci_dump.h" #include "l2cap.h" @@ -96,7 +95,7 @@ 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"); @@ -119,6 +118,9 @@ int main (int argc, const char * argv[]) { control = &bt_control_iphone; #endif + // use logger: format HCI_DUMP_PACKETLOGGER or HCI_DUMP_BLUEZ + hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); + // H4 UART transport = &hci_transport_h4;