xref: /btstack/src/hci_dump.c (revision c7b9c5599dc1af8ebdb23db5e9b2996f16c70ab2)
1 /*
2  *  hci_dump.c
3  *
4  *  Dump HCI trace in BlueZ's hcidump format
5  *
6  *  Created by Matthias Ringwald on 5/26/09.
7  */
8 
9 #include "hci_dump.h"
10 #include "hci.h"
11 #include "hci_transport_h4.h"
12 
13 #include <fcntl.h>        // open
14 #include <arpa/inet.h>    // hton..
15 #include <strings.h>      // bzero
16 #include <unistd.h>       // write
17 #include <stdio.h>
18 #include <sys/time.h>     // for timestamps
19 #include <sys/stat.h>     // for mode flags
20 
21 // BLUEZ hcidump
22 typedef struct {
23 	uint16_t	len;
24 	uint8_t		in;
25 	uint8_t		pad;
26 	uint32_t	ts_sec;
27 	uint32_t	ts_usec;
28     uint8_t     packet_type;
29 } __attribute__ ((packed)) hcidump_hdr;
30 
31 // APPLE PacketLogger
32 typedef struct {
33 	uint32_t	len;
34 	uint32_t	ts_sec;
35 	uint32_t	ts_usec;
36 	uint8_t		type;
37 } __attribute__ ((packed)) pktlog_hdr;
38 
39 static int dump_file = -1;
40 static int dump_format;
41 static hcidump_hdr header_bluez;
42 static pktlog_hdr  header_packetlogger;
43 
44 void hci_dump_open(char *filename, hci_dump_format_t format){
45     if (dump_file < 0) {
46         dump_file =  open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
47         dump_format = format;
48     }
49 }
50 
51 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
52     if (dump_file < 0) return; // not activated yet
53 
54     // get time
55     struct timeval curr_time;
56     gettimeofday(&curr_time, NULL);
57 
58     switch (dump_format){
59         case HCI_DUMP_BLUEZ:
60             bt_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len);
61             header_bluez.in  = in;
62             header_bluez.pad = 0;
63             bt_store_32( (uint8_t *) &header_bluez.ts_sec,  0, curr_time.tv_sec);
64             bt_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec);
65             header_bluez.packet_type = packet_type;
66             write (dump_file, &header_bluez, sizeof(hcidump_hdr) );
67             write (dump_file, packet, len );
68             break;
69         case HCI_DUMP_PACKETLOGGER:
70             header_packetlogger.len = htonl( sizeof(pktlog_hdr) - 4 + len);
71             header_packetlogger.ts_sec =  htonl(curr_time.tv_sec);
72             header_packetlogger.ts_usec = htonl(curr_time.tv_usec);
73             switch (packet_type){
74                 case HCI_COMMAND_DATA_PACKET:
75                     header_packetlogger.type = 0x00;
76                     break;
77                 case HCI_ACL_DATA_PACKET:
78                     if (in) {
79                         header_packetlogger.type = 0x03;
80                     } else {
81                         header_packetlogger.type = 0x02;
82                     }
83                     break;
84                 case HCI_EVENT_PACKET:
85                     header_packetlogger.type = 0x01;
86                     break;
87                 default:
88                     return;
89             }
90             write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) );
91             write (dump_file, packet, len );
92     }
93 }
94 
95 void hci_dump_close(){
96     close(dump_file);
97     dump_file = -1;
98 }
99 
100