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 #include "hci_dump_posix_fs.h" 57 58 #include "btstack_debug.h" 59 #include "btstack_util.h" 60 #include "hci_cmd.h" 61 62 #include <time.h> 63 #include <stdio.h> // printf 64 #include <fcntl.h> // open 65 #include <unistd.h> // write 66 #include <errno.h> // errno 67 #include <sys/time.h> // for timestamps 68 #include <sys/stat.h> // file modes 69 70 static int dump_file = -1; 71 static int dump_format; 72 static char log_message_buffer[256]; 73 74 static void hci_dump_posix_fs_reset(void){ 75 btstack_assert(dump_file >= 0); 76 (void) lseek(dump_file, 0, SEEK_SET); 77 int err = ftruncate(dump_file, 0); 78 UNUSED(err); 79 } 80 81 // provide summary for ISO Data Packets if not supported by fileformat/viewer yet 82 static uint16_t hci_dump_iso_summary(uint8_t in, uint8_t *packet, uint16_t len){ 83 UNUSED(len); 84 uint16_t conn_handle = little_endian_read_16(packet, 0) & 0xfff; 85 uint8_t pb = (packet[1] >> 4) & 3; 86 uint8_t ts = (packet[1] >> 6) & 1; 87 uint16_t pos = 4; 88 uint32_t time_stamp = 0; 89 if (ts){ 90 time_stamp = little_endian_read_32(packet, pos); 91 pos += 4; 92 } 93 uint16_t packet_sequence = little_endian_read_16(packet, pos); 94 pos += 2; 95 uint16_t iso_sdu_len = little_endian_read_16(packet, pos); 96 uint8_t packet_status_flag = packet[pos+1] >> 6; 97 return snprintf(log_message_buffer,sizeof(log_message_buffer), "ISO %s, handle %04x, pb %u, ts 0x%08x, sequence 0x%04x, packet status %u, iso len %u", 98 in ? "IN" : "OUT", conn_handle, pb, time_stamp, packet_sequence, packet_status_flag, iso_sdu_len); 99 } 100 101 static void hci_dump_posix_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 102 if (dump_file < 0) return; 103 104 static union { 105 uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ]; 106 uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER]; 107 uint8_t header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP+1]; 108 } header; 109 110 uint32_t tv_sec = 0; 111 uint32_t tv_us = 0; 112 uint64_t ts_usec; 113 114 // get time 115 struct timeval curr_time; 116 gettimeofday(&curr_time, NULL); 117 tv_sec = curr_time.tv_sec; 118 tv_us = curr_time.tv_usec; 119 120 uint16_t header_len = 0; 121 switch (dump_format){ 122 case HCI_DUMP_BLUEZ: 123 // ISO packets not supported 124 if (packet_type == HCI_ISO_DATA_PACKET){ 125 len = hci_dump_iso_summary(in, packet, len); 126 packet_type = LOG_MESSAGE_PACKET; 127 packet = (uint8_t*) log_message_buffer; 128 } 129 hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len); 130 header_len = HCI_DUMP_HEADER_SIZE_BLUEZ; 131 break; 132 case HCI_DUMP_PACKETLOGGER: 133 // ISO packets not supported 134 if (packet_type == HCI_ISO_DATA_PACKET){ 135 len = hci_dump_iso_summary(in, packet, len); 136 packet_type = LOG_MESSAGE_PACKET; 137 packet = (uint8_t*) log_message_buffer; 138 } 139 hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len); 140 header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER; 141 break; 142 case HCI_DUMP_BTSNOOP: 143 // log messages not supported 144 if (packet_type == LOG_MESSAGE_PACKET) return; 145 ts_usec = 0xdcddb30f2f8000LLU + 1000000LLU * curr_time.tv_sec + curr_time.tv_usec; 146 // append packet type to pcap header 147 hci_dump_setup_header_btsnoop(header.header_btsnoop, ts_usec >> 32, ts_usec & 0xFFFFFFFF, 0, packet_type, in, len+1); 148 header.header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP] = packet_type; 149 header_len = HCI_DUMP_HEADER_SIZE_BTSNOOP + 1; 150 break; 151 default: 152 btstack_unreachable(); 153 return; 154 } 155 156 ssize_t bytes_written; 157 bytes_written = write(dump_file, &header, header_len); 158 UNUSED(bytes_written); 159 bytes_written = write(dump_file, packet, len ); 160 UNUSED(bytes_written); 161 } 162 163 static void hci_dump_posix_fs_log_message(const char * format, va_list argptr){ 164 if (dump_file < 0) return; 165 int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 166 hci_dump_posix_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 167 } 168 169 // returns system errno 170 int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format){ 171 btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER || format == HCI_DUMP_BTSNOOP); 172 173 dump_format = format; 174 int oflags = O_WRONLY | O_CREAT | O_TRUNC; 175 #ifdef _WIN32 176 oflags |= O_BINARY; 177 #endif 178 dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); 179 if (dump_file < 0){ 180 printf("failed to open file %s, errno = %d\n", filename, errno); 181 return errno; 182 } 183 184 if (format == HCI_DUMP_BTSNOOP){ 185 // write BTSnoop file header 186 const uint8_t file_header[] = { 187 // Identification Pattern: "btsnoop\0" 188 0x62, 0x74, 0x73, 0x6E, 0x6F, 0x6F, 0x70, 0x00, 189 // Version: 1 190 0x00, 0x00, 0x00, 0x01, 191 // Datalink Type: 1002 - H4 192 0x00, 0x00, 0x03, 0xEA, 193 }; 194 ssize_t bytes_written = write(dump_file, &file_header, sizeof(file_header)); 195 UNUSED(bytes_written); 196 } 197 return 0; 198 } 199 200 void hci_dump_posix_fs_close(void){ 201 close(dump_file); 202 dump_file = -1; 203 } 204 205 const hci_dump_t * hci_dump_posix_fs_get_instance(void){ 206 static const hci_dump_t hci_dump_instance = { 207 // void (*reset)(void); 208 &hci_dump_posix_fs_reset, 209 // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 210 &hci_dump_posix_fs_log_packet, 211 // void (*log_message)(int log_level, const char * format, va_list argptr); 212 &hci_dump_posix_fs_log_message, 213 }; 214 return &hci_dump_instance; 215 } 216