2009-10-29 20:25:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 by Matthias Ringwald
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-05-26 22:50:30 +00:00
|
|
|
/*
|
|
|
|
* hci_dump.c
|
|
|
|
*
|
2009-07-24 21:13:53 +00:00
|
|
|
* Dump HCI trace in various formats:
|
|
|
|
*
|
|
|
|
* - BlueZ's hcidump format
|
|
|
|
* - Apple's PacketLogger
|
|
|
|
* - stdout hexdump
|
|
|
|
*
|
2009-05-26 22:50:30 +00:00
|
|
|
* Created by Matthias Ringwald on 5/26/09.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hci_dump.h"
|
|
|
|
#include "hci.h"
|
2010-06-04 18:09:01 +00:00
|
|
|
#include "hci_transport.h"
|
2009-05-26 22:50:30 +00:00
|
|
|
|
2010-07-04 16:06:32 +00:00
|
|
|
#ifndef EMBEDDED
|
2009-05-26 22:50:30 +00:00
|
|
|
#include <fcntl.h> // open
|
|
|
|
#include <arpa/inet.h> // hton..
|
|
|
|
#include <strings.h> // bzero
|
|
|
|
#include <unistd.h> // write
|
|
|
|
#include <stdio.h>
|
2009-07-16 20:12:42 +00:00
|
|
|
#include <time.h>
|
2009-05-27 20:22:19 +00:00
|
|
|
#include <sys/time.h> // for timestamps
|
2009-07-11 10:46:17 +00:00
|
|
|
#include <sys/stat.h> // for mode flags
|
2010-07-04 16:06:32 +00:00
|
|
|
#endif
|
2009-05-26 22:50:30 +00:00
|
|
|
|
2009-05-27 20:22:19 +00:00
|
|
|
// 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;
|
2009-05-26 22:50:30 +00:00
|
|
|
|
2009-05-27 20:22:19 +00:00
|
|
|
// APPLE PacketLogger
|
|
|
|
typedef struct {
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t ts_sec;
|
|
|
|
uint32_t ts_usec;
|
|
|
|
uint8_t type;
|
|
|
|
} __attribute__ ((packed)) pktlog_hdr;
|
|
|
|
|
2010-07-04 16:06:32 +00:00
|
|
|
#ifndef EMBEDDED
|
2009-05-27 20:22:19 +00:00
|
|
|
static int dump_file = -1;
|
|
|
|
static int dump_format;
|
|
|
|
static hcidump_hdr header_bluez;
|
|
|
|
static pktlog_hdr header_packetlogger;
|
2009-07-16 20:12:42 +00:00
|
|
|
static char time_string[40];
|
2010-07-04 16:06:32 +00:00
|
|
|
#endif
|
2009-05-27 20:22:19 +00:00
|
|
|
|
|
|
|
void hci_dump_open(char *filename, hci_dump_format_t format){
|
2010-07-04 16:06:32 +00:00
|
|
|
#ifndef EMBEDDED
|
2009-07-16 20:12:42 +00:00
|
|
|
dump_format = format;
|
|
|
|
if (dump_format == HCI_DUMP_STDOUT) {
|
|
|
|
dump_file = fileno(stdout);
|
|
|
|
} else {
|
2009-07-11 10:45:06 +00:00
|
|
|
dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2009-06-04 20:30:03 +00:00
|
|
|
}
|
2010-07-04 16:06:32 +00:00
|
|
|
#endif
|
2009-05-26 22:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
|
2010-07-04 16:06:32 +00:00
|
|
|
#ifndef EMBEDDED
|
|
|
|
|
2009-05-27 20:22:19 +00:00
|
|
|
if (dump_file < 0) return; // not activated yet
|
|
|
|
|
|
|
|
// get time
|
|
|
|
struct timeval curr_time;
|
2009-07-16 20:12:42 +00:00
|
|
|
struct tm* ptm;
|
2009-05-27 20:22:19 +00:00
|
|
|
gettimeofday(&curr_time, NULL);
|
|
|
|
|
|
|
|
switch (dump_format){
|
2009-07-16 20:12:42 +00:00
|
|
|
case HCI_DUMP_STDOUT:
|
|
|
|
/* Obtain the time of day, and convert it to a tm struct. */
|
|
|
|
ptm = localtime (&curr_time.tv_sec);
|
|
|
|
/* Format the date and time, down to a single second. */
|
|
|
|
strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
|
|
|
|
/* Compute milliseconds from microseconds. */
|
|
|
|
uint16_t milliseconds = curr_time.tv_usec / 1000;
|
|
|
|
/* Print the formatted time, in seconds, followed by a decimal point
|
|
|
|
and the milliseconds. */
|
|
|
|
printf ("%s.%03u] ", time_string, milliseconds);
|
|
|
|
switch (packet_type){
|
|
|
|
case HCI_COMMAND_DATA_PACKET:
|
|
|
|
printf("CMD => ");
|
|
|
|
break;
|
|
|
|
case HCI_EVENT_PACKET:
|
|
|
|
printf("EVT <= ");
|
|
|
|
break;
|
|
|
|
case HCI_ACL_DATA_PACKET:
|
|
|
|
if (in) {
|
|
|
|
printf("ACL <= ");
|
|
|
|
} else {
|
|
|
|
printf("ACL => ");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hexdump(packet, len);
|
|
|
|
break;
|
2009-05-27 20:22:19 +00:00
|
|
|
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 );
|
|
|
|
}
|
2010-07-04 16:06:32 +00:00
|
|
|
#endif
|
2009-05-26 22:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hci_dump_close(){
|
2010-07-04 16:06:32 +00:00
|
|
|
#ifndef EMBEDDED
|
2009-05-26 22:50:30 +00:00
|
|
|
close(dump_file);
|
2009-06-04 20:30:03 +00:00
|
|
|
dump_file = -1;
|
2010-07-04 16:06:32 +00:00
|
|
|
#endif
|
2009-05-26 22:50:30 +00:00
|
|
|
}
|
|
|
|
|