1 /*
2 * Copyright (C) 2014-2020 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holders nor the names of
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38 #define BTSTACK_FILE__ "hci_dump_posix_fs.c"
39
40 /*
41 * hci_dump_posix_fs.c
42 *
43 * Dump HCI trace in various formats into a file:
44 *
45 * - BlueZ's hcidump format
46 * - Apple's PacketLogger
47 * - stdout hexdump
48 *
49 */
50
51 #include "btstack_config.h"
52
53 // enable POSIX functions (needed for -std=c99)
54 #define _POSIX_C_SOURCE 200809
55
56 #ifdef __FreeBSD__
57 // FreeBSD does not set __BSD_VISIBLE or __XSI_VISIBLE if _POSIX_C_SOURCE is defined
58 #define __BSD_VISIBLE 1
59 #define __XSI_VISIBLE 1
60 #endif
61
62 #include "hci_dump_posix_fs.h"
63
64 #include "btstack_debug.h"
65 #include "btstack_util.h"
66
67 #include <sys/time.h> // for timestamps
68 #include <sys/stat.h> // file modes
69
70 #include <time.h>
71 #include <stdio.h> // printf
72 #include <fcntl.h> // open
73 #include <unistd.h> // write
74 #include <errno.h> // errno
75
76 static int dump_file = -1;
77 static int dump_format;
78 static char log_message_buffer[256];
79
hci_dump_posix_fs_reset(void)80 static void hci_dump_posix_fs_reset(void){
81 btstack_assert(dump_file >= 0);
82 (void) lseek(dump_file, 0, SEEK_SET);
83 int err = ftruncate(dump_file, 0);
84 UNUSED(err);
85 }
86
87 // provide summary for ISO Data Packets if not supported by fileformat/viewer yet
hci_dump_iso_summary(uint8_t in,uint8_t * packet,uint16_t len)88 static uint16_t hci_dump_iso_summary(uint8_t in, uint8_t *packet, uint16_t len){
89 UNUSED(len);
90 uint16_t conn_handle = little_endian_read_16(packet, 0) & 0xfff;
91 uint8_t pb = (packet[1] >> 4) & 3;
92 uint8_t ts = (packet[1] >> 6) & 1;
93 uint16_t pos = 4;
94 uint32_t time_stamp = 0;
95 if (ts){
96 time_stamp = little_endian_read_32(packet, pos);
97 pos += 4;
98 }
99 if ((pb & 1) == 0) {
100 uint16_t packet_sequence = little_endian_read_16(packet, pos);
101 pos += 2;
102 uint16_t iso_sdu_len = little_endian_read_16(packet, pos);
103 uint8_t packet_status_flag = packet[pos+1] >> 6;
104 return btstack_snprintf_assert_complete(log_message_buffer,sizeof(log_message_buffer), "ISO %s, handle %04x, pb %u, ts 0x%08x, size %u, sequence 0x%04x, packet status %u, iso pdu len %u",
105 in ? "IN" : "OUT", conn_handle, pb, time_stamp, len, packet_sequence, packet_status_flag, iso_sdu_len);
106 } else {
107 return btstack_snprintf_assert_complete(log_message_buffer,sizeof(log_message_buffer), "ISO %s, handle %04x, pb %u, ts 0x%08x, size %u",
108 in ? "IN" : "OUT", conn_handle, pb, time_stamp, len);
109 }
110 }
111
hci_dump_posix_fs_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)112 static void hci_dump_posix_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
113 if (dump_file < 0) return;
114
115 static union {
116 uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
117 uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
118 uint8_t header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP+1];
119 } header;
120
121 uint32_t tv_sec = 0;
122 uint32_t tv_us = 0;
123 uint64_t ts_usec;
124
125 // get time
126 struct timeval curr_time;
127 gettimeofday(&curr_time, NULL);
128 tv_sec = curr_time.tv_sec;
129 tv_us = curr_time.tv_usec;
130
131 uint16_t header_len = 0;
132 switch (dump_format){
133 case HCI_DUMP_BLUEZ:
134 // ISO packets not supported
135 if (packet_type == HCI_ISO_DATA_PACKET){
136 len = hci_dump_iso_summary(in, packet, len);
137 packet_type = LOG_MESSAGE_PACKET;
138 packet = (uint8_t*) log_message_buffer;
139 }
140 hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
141 header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
142 break;
143 case HCI_DUMP_PACKETLOGGER:
144 hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
145 header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
146 break;
147 case HCI_DUMP_BTSNOOP:
148 // log messages not supported
149 if (packet_type == LOG_MESSAGE_PACKET) return;
150 ts_usec = 0xdcddb30f2f8000LLU + 1000000LLU * curr_time.tv_sec + curr_time.tv_usec;
151 // append packet type to pcap header
152 hci_dump_setup_header_btsnoop(header.header_btsnoop, ts_usec >> 32, ts_usec & 0xFFFFFFFF, 0, packet_type, in, len+1);
153 header.header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP] = packet_type;
154 header_len = HCI_DUMP_HEADER_SIZE_BTSNOOP + 1;
155 break;
156 default:
157 btstack_unreachable();
158 return;
159 }
160
161 ssize_t bytes_written;
162 bytes_written = write(dump_file, &header, header_len);
163 UNUSED(bytes_written);
164 bytes_written = write(dump_file, packet, len );
165 UNUSED(bytes_written);
166 }
167
hci_dump_posix_fs_log_message(int log_level,const char * format,va_list argptr)168 static void hci_dump_posix_fs_log_message(int log_level, const char * format, va_list argptr){
169 UNUSED(log_level);
170 if (dump_file < 0) return;
171 int full_string_len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
172 int len = btstack_min(sizeof(log_message_buffer), full_string_len);
173 hci_dump_posix_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
174 }
175
176 // returns system errno
hci_dump_posix_fs_open(const char * filename,hci_dump_format_t format)177 int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format){
178 btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER || format == HCI_DUMP_BTSNOOP);
179
180 dump_format = format;
181 int oflags = O_WRONLY | O_CREAT | O_TRUNC;
182 #ifdef _WIN32
183 oflags |= O_BINARY;
184 #endif
185 dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
186 if (dump_file < 0){
187 printf("failed to open file %s, errno = %d\n", filename, errno);
188 return errno;
189 }
190
191 if (format == HCI_DUMP_BTSNOOP){
192 // write BTSnoop file header
193 const uint8_t file_header[] = {
194 // Identification Pattern: "btsnoop\0"
195 0x62, 0x74, 0x73, 0x6E, 0x6F, 0x6F, 0x70, 0x00,
196 // Version: 1
197 0x00, 0x00, 0x00, 0x01,
198 // Datalink Type: 1002 - H4
199 0x00, 0x00, 0x03, 0xEA,
200 };
201 ssize_t bytes_written = write(dump_file, &file_header, sizeof(file_header));
202 UNUSED(bytes_written);
203 }
204 return 0;
205 }
206
hci_dump_posix_fs_close(void)207 void hci_dump_posix_fs_close(void){
208 close(dump_file);
209 dump_file = -1;
210 }
211
hci_dump_posix_fs_get_instance(void)212 const hci_dump_t * hci_dump_posix_fs_get_instance(void){
213 static const hci_dump_t hci_dump_instance = {
214 // void (*reset)(void);
215 &hci_dump_posix_fs_reset,
216 // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
217 &hci_dump_posix_fs_log_packet,
218 // void (*log_message)(int log_level, const char * format, va_list argptr);
219 &hci_dump_posix_fs_log_message,
220 };
221 return &hci_dump_instance;
222 }
223