19c8d7c63SMatthias Ringwald /*
29c8d7c63SMatthias Ringwald * Copyright (C) 2022 BlueKitchen GmbH
39c8d7c63SMatthias Ringwald *
49c8d7c63SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
59c8d7c63SMatthias Ringwald * modification, are permitted provided that the following conditions
69c8d7c63SMatthias Ringwald * are met:
79c8d7c63SMatthias Ringwald *
89c8d7c63SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
99c8d7c63SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
109c8d7c63SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
119c8d7c63SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
129c8d7c63SMatthias Ringwald * documentation and/or other materials provided with the distribution.
139c8d7c63SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
149c8d7c63SMatthias Ringwald * contributors may be used to endorse or promote products derived
159c8d7c63SMatthias Ringwald * from this software without specific prior written permission.
169c8d7c63SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
179c8d7c63SMatthias Ringwald * personal benefit and not for any commercial purpose or for
189c8d7c63SMatthias Ringwald * monetary gain.
199c8d7c63SMatthias Ringwald *
209c8d7c63SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
219c8d7c63SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
229c8d7c63SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
239c8d7c63SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
249c8d7c63SMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
259c8d7c63SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
269c8d7c63SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
279c8d7c63SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
289c8d7c63SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
299c8d7c63SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
309c8d7c63SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319c8d7c63SMatthias Ringwald * SUCH DAMAGE.
329c8d7c63SMatthias Ringwald *
339c8d7c63SMatthias Ringwald * Please inquire about commercial licensing options at
349c8d7c63SMatthias Ringwald * [email protected]
359c8d7c63SMatthias Ringwald *
369c8d7c63SMatthias Ringwald */
379c8d7c63SMatthias Ringwald
389c8d7c63SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump_windows_fs.c"
399c8d7c63SMatthias Ringwald
409c8d7c63SMatthias Ringwald /*
419c8d7c63SMatthias Ringwald * hci_dump_windows_fs.c
429c8d7c63SMatthias Ringwald *
439c8d7c63SMatthias Ringwald * Dump HCI trace in various formats into a file:
449c8d7c63SMatthias Ringwald *
459c8d7c63SMatthias Ringwald * - BlueZ's hcidump format
469c8d7c63SMatthias Ringwald * - Apple's PacketLogger
479c8d7c63SMatthias Ringwald * - stdout hexdump
489c8d7c63SMatthias Ringwald *
499c8d7c63SMatthias Ringwald */
50b795dcd0SMarcel Wappler #include <windows.h>
519c8d7c63SMatthias Ringwald #include "btstack_config.h"
529c8d7c63SMatthias Ringwald
539c8d7c63SMatthias Ringwald #include "hci_dump_windows_fs.h"
549c8d7c63SMatthias Ringwald
559c8d7c63SMatthias Ringwald #include "btstack_debug.h"
569c8d7c63SMatthias Ringwald #include "btstack_util.h"
579c8d7c63SMatthias Ringwald #include "hci_cmd.h"
589c8d7c63SMatthias Ringwald
599c8d7c63SMatthias Ringwald #include <stdio.h>
60d282ad88SMarcel Wappler #include <share.h>
619c8d7c63SMatthias Ringwald
629c8d7c63SMatthias Ringwald /**
639c8d7c63SMatthias Ringwald * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
649c8d7c63SMatthias Ringwald */
659c8d7c63SMatthias Ringwald #define EPOCH_DIFF 11644473600LL
669c8d7c63SMatthias Ringwald
679c8d7c63SMatthias Ringwald static HANDLE dump_file = INVALID_HANDLE_VALUE;
689c8d7c63SMatthias Ringwald static int dump_format;
69*d1a44612SMatthias Ringwald #ifndef HCI_LOG_MESSAGE_BUFFER_WINUSB
70*d1a44612SMatthias Ringwald #define HCI_LOG_MESSAGE_BUFFER_WINUSB 256
71*d1a44612SMatthias Ringwald #endif
72*d1a44612SMatthias Ringwald static char log_message_buffer[HCI_LOG_MESSAGE_BUFFER_WINUSB];
739c8d7c63SMatthias Ringwald
hci_dump_windows_fs_reset(void)749c8d7c63SMatthias Ringwald static void hci_dump_windows_fs_reset(void){
759c8d7c63SMatthias Ringwald btstack_assert(dump_file != INVALID_HANDLE_VALUE);
769c8d7c63SMatthias Ringwald SetEndOfFile(dump_file);
779c8d7c63SMatthias Ringwald }
789c8d7c63SMatthias Ringwald
799c8d7c63SMatthias Ringwald // 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)809c8d7c63SMatthias Ringwald static uint16_t hci_dump_iso_summary(uint8_t in, uint8_t *packet, uint16_t len){
819c8d7c63SMatthias Ringwald uint16_t conn_handle = little_endian_read_16(packet, 0) & 0xfff;
829c8d7c63SMatthias Ringwald uint8_t pb = (packet[1] >> 4) & 3;
839c8d7c63SMatthias Ringwald uint8_t ts = (packet[1] >> 6) & 1;
849c8d7c63SMatthias Ringwald uint16_t pos = 4;
859c8d7c63SMatthias Ringwald uint32_t time_stamp = 0;
869c8d7c63SMatthias Ringwald if (ts){
879c8d7c63SMatthias Ringwald time_stamp = little_endian_read_32(packet, pos);
889c8d7c63SMatthias Ringwald pos += 4;
899c8d7c63SMatthias Ringwald }
909c8d7c63SMatthias Ringwald uint16_t packet_sequence = little_endian_read_16(packet, pos);
919c8d7c63SMatthias Ringwald pos += 2;
929c8d7c63SMatthias Ringwald uint16_t iso_sdu_len = little_endian_read_16(packet, pos);
939c8d7c63SMatthias Ringwald uint8_t packet_status_flag = packet[pos+1] >> 6;
949c8d7c63SMatthias Ringwald 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",
959c8d7c63SMatthias Ringwald in ? "IN" : "OUT", conn_handle, pb, time_stamp, packet_sequence, packet_status_flag, iso_sdu_len);
969c8d7c63SMatthias Ringwald }
979c8d7c63SMatthias Ringwald
hci_dump_windows_fs_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)989c8d7c63SMatthias Ringwald static void hci_dump_windows_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
999c8d7c63SMatthias Ringwald if (dump_file < 0) return;
1009c8d7c63SMatthias Ringwald
1019c8d7c63SMatthias Ringwald static union {
1029c8d7c63SMatthias Ringwald uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
1039c8d7c63SMatthias Ringwald uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
1049c8d7c63SMatthias Ringwald uint8_t header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP+1];
1059c8d7c63SMatthias Ringwald } header;
1069c8d7c63SMatthias Ringwald
1079c8d7c63SMatthias Ringwald uint32_t tv_sec = 0;
1089c8d7c63SMatthias Ringwald uint32_t tv_us = 0;
1099c8d7c63SMatthias Ringwald uint64_t ts_usec;
1109c8d7c63SMatthias Ringwald
1119c8d7c63SMatthias Ringwald // get time
1129c8d7c63SMatthias Ringwald FILETIME file_time;
1139c8d7c63SMatthias Ringwald SYSTEMTIME local_time;
1149c8d7c63SMatthias Ringwald ULARGE_INTEGER now_time;
1159c8d7c63SMatthias Ringwald GetLocalTime(&local_time);
1169c8d7c63SMatthias Ringwald SystemTimeToFileTime(&local_time, &file_time);
1179c8d7c63SMatthias Ringwald now_time.LowPart = file_time.dwLowDateTime;
1189c8d7c63SMatthias Ringwald now_time.HighPart = file_time.dwHighDateTime;
1199c8d7c63SMatthias Ringwald
12035d4a926SMatthias Ringwald tv_sec = (uint32_t) ((now_time.QuadPart / 10000000ULL) - EPOCH_DIFF);
12135d4a926SMatthias Ringwald tv_us = (now_time.QuadPart % 10000000ULL) / 10ULL;
1229c8d7c63SMatthias Ringwald
1239c8d7c63SMatthias Ringwald uint16_t header_len = 0;
1249c8d7c63SMatthias Ringwald switch (dump_format){
1259c8d7c63SMatthias Ringwald case HCI_DUMP_BLUEZ:
1269c8d7c63SMatthias Ringwald // ISO packets not supported
1279c8d7c63SMatthias Ringwald if (packet_type == HCI_ISO_DATA_PACKET){
1289c8d7c63SMatthias Ringwald len = hci_dump_iso_summary(in, packet, len);
1299c8d7c63SMatthias Ringwald packet_type = LOG_MESSAGE_PACKET;
1309c8d7c63SMatthias Ringwald packet = (uint8_t*) log_message_buffer;
1319c8d7c63SMatthias Ringwald }
1329c8d7c63SMatthias Ringwald hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
1339c8d7c63SMatthias Ringwald header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
1349c8d7c63SMatthias Ringwald break;
1359c8d7c63SMatthias Ringwald case HCI_DUMP_PACKETLOGGER:
1369c8d7c63SMatthias Ringwald hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
1379c8d7c63SMatthias Ringwald header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
1389c8d7c63SMatthias Ringwald break;
1399c8d7c63SMatthias Ringwald case HCI_DUMP_BTSNOOP:
1409c8d7c63SMatthias Ringwald // log messages not supported
1419c8d7c63SMatthias Ringwald if (packet_type == LOG_MESSAGE_PACKET) return;
1429c8d7c63SMatthias Ringwald ts_usec = 0xdcddb30f2f8000LLU + 1000000LLU * tv_sec + tv_us;
1439c8d7c63SMatthias Ringwald // append packet type to pcap header
1449c8d7c63SMatthias Ringwald hci_dump_setup_header_btsnoop(header.header_btsnoop, ts_usec >> 32, ts_usec & 0xFFFFFFFF, 0, packet_type, in, len+1);
1459c8d7c63SMatthias Ringwald header.header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP] = packet_type;
1469c8d7c63SMatthias Ringwald header_len = HCI_DUMP_HEADER_SIZE_BTSNOOP + 1;
1479c8d7c63SMatthias Ringwald break;
1489c8d7c63SMatthias Ringwald default:
1499c8d7c63SMatthias Ringwald btstack_unreachable();
1509c8d7c63SMatthias Ringwald return;
1519c8d7c63SMatthias Ringwald }
1529c8d7c63SMatthias Ringwald
1539c8d7c63SMatthias Ringwald DWORD dwBytesWritten = 0;
1549c8d7c63SMatthias Ringwald (void) WriteFile(
1559c8d7c63SMatthias Ringwald dump_file, // file handle
1569c8d7c63SMatthias Ringwald &header, // start of data to write
1579c8d7c63SMatthias Ringwald header_len, // number of bytes to write
1589c8d7c63SMatthias Ringwald &dwBytesWritten, // number of bytes that were written
1599c8d7c63SMatthias Ringwald NULL); // no overlapped structure
1609c8d7c63SMatthias Ringwald UNUSED(dwBytesWritten);
1619c8d7c63SMatthias Ringwald
1629c8d7c63SMatthias Ringwald (void)WriteFile(
1639c8d7c63SMatthias Ringwald dump_file, // file handle
1649c8d7c63SMatthias Ringwald packet, // start of data to write
1659c8d7c63SMatthias Ringwald len, // number of bytes to write
1669c8d7c63SMatthias Ringwald &dwBytesWritten, // number of bytes that were written
1679c8d7c63SMatthias Ringwald NULL); // no overlapped structure
1689c8d7c63SMatthias Ringwald UNUSED(dwBytesWritten);
1699c8d7c63SMatthias Ringwald }
1709c8d7c63SMatthias Ringwald
hci_dump_windows_fs_log_message(int log_level,const char * format,va_list argptr)1712d17d4c0SMatthias Ringwald static void hci_dump_windows_fs_log_message(int log_level, const char * format, va_list argptr){
1722d17d4c0SMatthias Ringwald UNUSED(log_level);
1739c8d7c63SMatthias Ringwald if (dump_file < 0) return;
1749c8d7c63SMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
1759c8d7c63SMatthias Ringwald hci_dump_windows_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
1769c8d7c63SMatthias Ringwald }
1779c8d7c63SMatthias Ringwald
1789c8d7c63SMatthias Ringwald // returns system errno
hci_dump_windows_fs_open(const char * filename,hci_dump_format_t format)1799c8d7c63SMatthias Ringwald int hci_dump_windows_fs_open(const char *filename, hci_dump_format_t format){
1809c8d7c63SMatthias Ringwald btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER || format == HCI_DUMP_BTSNOOP);
1819c8d7c63SMatthias Ringwald
1829c8d7c63SMatthias Ringwald dump_format = format;
1839c8d7c63SMatthias Ringwald
1849c8d7c63SMatthias Ringwald dump_file = CreateFile(filename, // name of the write
1859c8d7c63SMatthias Ringwald GENERIC_WRITE, // open for writing
186d282ad88SMarcel Wappler FILE_SHARE_READ, // readable while still writing
1879c8d7c63SMatthias Ringwald NULL, // default security
1889c8d7c63SMatthias Ringwald CREATE_ALWAYS, // create new file always
1899c8d7c63SMatthias Ringwald FILE_ATTRIBUTE_NORMAL, // normal file
1909c8d7c63SMatthias Ringwald NULL); // no attr. template
1919c8d7c63SMatthias Ringwald
1929c8d7c63SMatthias Ringwald if (dump_file == INVALID_HANDLE_VALUE){
1939c8d7c63SMatthias Ringwald printf("failed to open file %s, error code = 0x%lx\n", filename, GetLastError());
1949c8d7c63SMatthias Ringwald return -1;
1959c8d7c63SMatthias Ringwald }
1969c8d7c63SMatthias Ringwald
1979c8d7c63SMatthias Ringwald if (format == HCI_DUMP_BTSNOOP){
1989c8d7c63SMatthias Ringwald // write BTSnoop file header
1999c8d7c63SMatthias Ringwald const uint8_t file_header[] = {
2009c8d7c63SMatthias Ringwald // Identification Pattern: "btsnoop\0"
2019c8d7c63SMatthias Ringwald 0x62, 0x74, 0x73, 0x6E, 0x6F, 0x6F, 0x70, 0x00,
2029c8d7c63SMatthias Ringwald // Version: 1
2039c8d7c63SMatthias Ringwald 0x00, 0x00, 0x00, 0x01,
2049c8d7c63SMatthias Ringwald // Datalink Type: 1002 - H4
2059c8d7c63SMatthias Ringwald 0x00, 0x00, 0x03, 0xEA,
2069c8d7c63SMatthias Ringwald };
2079c8d7c63SMatthias Ringwald
2089c8d7c63SMatthias Ringwald DWORD dwBytesWritten = 0;
2099c8d7c63SMatthias Ringwald (void) WriteFile(
2109c8d7c63SMatthias Ringwald dump_file, // file handle
2119c8d7c63SMatthias Ringwald file_header, // start of data to write
2129c8d7c63SMatthias Ringwald sizeof(file_header), // number of bytes to write
2139c8d7c63SMatthias Ringwald &dwBytesWritten, // number of bytes that were written
2149c8d7c63SMatthias Ringwald NULL); // no overlapped structure
2159c8d7c63SMatthias Ringwald UNUSED(dwBytesWritten);
2169c8d7c63SMatthias Ringwald }
2179c8d7c63SMatthias Ringwald return 0;
2189c8d7c63SMatthias Ringwald }
2199c8d7c63SMatthias Ringwald
hci_dump_windows_fs_close(void)2209c8d7c63SMatthias Ringwald void hci_dump_windows_fs_close(void){
2219c8d7c63SMatthias Ringwald CloseHandle(dump_file);
2229c8d7c63SMatthias Ringwald dump_file = INVALID_HANDLE_VALUE;
2239c8d7c63SMatthias Ringwald }
2249c8d7c63SMatthias Ringwald
hci_dump_windows_fs_get_instance(void)2259c8d7c63SMatthias Ringwald const hci_dump_t * hci_dump_windows_fs_get_instance(void){
2269c8d7c63SMatthias Ringwald static const hci_dump_t hci_dump_instance = {
2279c8d7c63SMatthias Ringwald // void (*reset)(void);
2289c8d7c63SMatthias Ringwald &hci_dump_windows_fs_reset,
2299c8d7c63SMatthias Ringwald // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
2309c8d7c63SMatthias Ringwald &hci_dump_windows_fs_log_packet,
2319c8d7c63SMatthias Ringwald // void (*log_message)(int log_level, const char * format, va_list argptr);
2329c8d7c63SMatthias Ringwald &hci_dump_windows_fs_log_message,
2339c8d7c63SMatthias Ringwald };
2349c8d7c63SMatthias Ringwald return &hci_dump_instance;
2359c8d7c63SMatthias Ringwald }
236