xref: /btstack/platform/posix/hci_dump_posix_fs.c (revision 10bc401a5a0623f92ef27bca1661e85273ca3339)
1128d6c99SMatthias Ringwald /*
2128d6c99SMatthias Ringwald  * Copyright (C) 2014-2020 BlueKitchen GmbH
3128d6c99SMatthias Ringwald  *
4128d6c99SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5128d6c99SMatthias Ringwald  * modification, are permitted provided that the following conditions
6128d6c99SMatthias Ringwald  * are met:
7128d6c99SMatthias Ringwald  *
8128d6c99SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9128d6c99SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10128d6c99SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11128d6c99SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12128d6c99SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13128d6c99SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14128d6c99SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15128d6c99SMatthias Ringwald  *    from this software without specific prior written permission.
16128d6c99SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17128d6c99SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18128d6c99SMatthias Ringwald  *    monetary gain.
19128d6c99SMatthias Ringwald  *
20128d6c99SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21128d6c99SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22128d6c99SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25128d6c99SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26128d6c99SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27128d6c99SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28128d6c99SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29128d6c99SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30128d6c99SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31128d6c99SMatthias Ringwald  * SUCH DAMAGE.
32128d6c99SMatthias Ringwald  *
33128d6c99SMatthias Ringwald  * Please inquire about commercial licensing options at
34128d6c99SMatthias Ringwald  * [email protected]
35128d6c99SMatthias Ringwald  *
36128d6c99SMatthias Ringwald  */
37128d6c99SMatthias Ringwald 
38128d6c99SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump_posix_fs.c"
39128d6c99SMatthias Ringwald 
40128d6c99SMatthias Ringwald /*
41128d6c99SMatthias Ringwald  *  hci_dump_posix_fs.c
42128d6c99SMatthias Ringwald  *
43128d6c99SMatthias Ringwald  *  Dump HCI trace in various formats into a file:
44128d6c99SMatthias Ringwald  *
45128d6c99SMatthias Ringwald  *  - BlueZ's hcidump format
46128d6c99SMatthias Ringwald  *  - Apple's PacketLogger
47128d6c99SMatthias Ringwald  *  - stdout hexdump
48128d6c99SMatthias Ringwald  *
49128d6c99SMatthias Ringwald  */
50128d6c99SMatthias Ringwald 
51128d6c99SMatthias Ringwald #include "btstack_config.h"
52128d6c99SMatthias Ringwald 
53128d6c99SMatthias Ringwald // enable POSIX functions (needed for -std=c99)
54128d6c99SMatthias Ringwald #define _POSIX_C_SOURCE 200809
55128d6c99SMatthias Ringwald 
56*10bc401aSMatthias Ringwald #ifdef __FreeBSD__
57*10bc401aSMatthias Ringwald // FreeBSD does not set __BSD_VISIBLE or __XSI_VISIBLE if _POSIX_C_SOURCE is defined
58*10bc401aSMatthias Ringwald #define __BSD_VISIBLE 1
59*10bc401aSMatthias Ringwald #define __XSI_VISIBLE 1
60*10bc401aSMatthias Ringwald #endif
61*10bc401aSMatthias Ringwald 
62128d6c99SMatthias Ringwald #include "hci_dump_posix_fs.h"
63128d6c99SMatthias Ringwald 
64128d6c99SMatthias Ringwald #include "btstack_debug.h"
65997ab207SMatthias Ringwald #include "btstack_util.h"
66*10bc401aSMatthias Ringwald 
67*10bc401aSMatthias Ringwald #include <sys/time.h>     // for timestamps
68*10bc401aSMatthias Ringwald #include <sys/stat.h>     // file modes
69128d6c99SMatthias Ringwald 
70128d6c99SMatthias Ringwald #include <time.h>
71128d6c99SMatthias Ringwald #include <stdio.h>        // printf
72128d6c99SMatthias Ringwald #include <fcntl.h>        // open
73128d6c99SMatthias Ringwald #include <unistd.h>       // write
74128d6c99SMatthias Ringwald #include <errno.h>        // errno
75128d6c99SMatthias Ringwald 
76128d6c99SMatthias Ringwald static int  dump_file = -1;
77128d6c99SMatthias Ringwald static int  dump_format;
78128d6c99SMatthias Ringwald static char log_message_buffer[256];
79128d6c99SMatthias Ringwald 
80128d6c99SMatthias Ringwald static void hci_dump_posix_fs_reset(void){
81128d6c99SMatthias Ringwald     btstack_assert(dump_file >= 0);
82128d6c99SMatthias Ringwald     (void) lseek(dump_file, 0, SEEK_SET);
839e6ecc42SMatthias Ringwald     int err = ftruncate(dump_file, 0);
849e6ecc42SMatthias Ringwald     UNUSED(err);
85128d6c99SMatthias Ringwald }
86128d6c99SMatthias Ringwald 
87997ab207SMatthias Ringwald // provide summary for ISO Data Packets if not supported by fileformat/viewer yet
88997ab207SMatthias Ringwald static uint16_t hci_dump_iso_summary(uint8_t in,  uint8_t *packet, uint16_t len){
89ce27b10eSMatthias Ringwald     UNUSED(len);
90997ab207SMatthias Ringwald     uint16_t conn_handle = little_endian_read_16(packet, 0) & 0xfff;
91997ab207SMatthias Ringwald     uint8_t pb = (packet[1] >> 4) & 3;
92997ab207SMatthias Ringwald     uint8_t ts = (packet[1] >> 6) & 1;
93997ab207SMatthias Ringwald     uint16_t pos = 4;
94997ab207SMatthias Ringwald     uint32_t time_stamp = 0;
95997ab207SMatthias Ringwald     if (ts){
96997ab207SMatthias Ringwald         time_stamp = little_endian_read_32(packet, pos);
97997ab207SMatthias Ringwald         pos += 4;
98997ab207SMatthias Ringwald     }
99997ab207SMatthias Ringwald     uint16_t packet_sequence = little_endian_read_16(packet, pos);
100997ab207SMatthias Ringwald     pos += 2;
101997ab207SMatthias Ringwald     uint16_t iso_sdu_len = little_endian_read_16(packet, pos);
102997ab207SMatthias Ringwald     uint8_t packet_status_flag = packet[pos+1] >> 6;
103997ab207SMatthias 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",
104997ab207SMatthias Ringwald                     in ? "IN" : "OUT", conn_handle, pb, time_stamp, packet_sequence, packet_status_flag, iso_sdu_len);
105997ab207SMatthias Ringwald }
106997ab207SMatthias Ringwald 
107128d6c99SMatthias Ringwald static void hci_dump_posix_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
108128d6c99SMatthias Ringwald     if (dump_file < 0) return;
109128d6c99SMatthias Ringwald 
110128d6c99SMatthias Ringwald     static union {
111128d6c99SMatthias Ringwald         uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
112128d6c99SMatthias Ringwald         uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
11326d7634cSMatthias Ringwald         uint8_t header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP+1];
114128d6c99SMatthias Ringwald     } header;
115128d6c99SMatthias Ringwald 
116128d6c99SMatthias Ringwald     uint32_t tv_sec = 0;
117128d6c99SMatthias Ringwald     uint32_t tv_us  = 0;
1189620d15fSMatthias Ringwald     uint64_t ts_usec;
119128d6c99SMatthias Ringwald 
120128d6c99SMatthias Ringwald     // get time
121128d6c99SMatthias Ringwald     struct timeval curr_time;
122128d6c99SMatthias Ringwald     gettimeofday(&curr_time, NULL);
123128d6c99SMatthias Ringwald     tv_sec = curr_time.tv_sec;
124128d6c99SMatthias Ringwald     tv_us  = curr_time.tv_usec;
125128d6c99SMatthias Ringwald 
126128d6c99SMatthias Ringwald     uint16_t header_len = 0;
127128d6c99SMatthias Ringwald     switch (dump_format){
128128d6c99SMatthias Ringwald         case HCI_DUMP_BLUEZ:
129997ab207SMatthias Ringwald             // ISO packets not supported
130997ab207SMatthias Ringwald             if (packet_type == HCI_ISO_DATA_PACKET){
131997ab207SMatthias Ringwald                 len = hci_dump_iso_summary(in, packet, len);
132997ab207SMatthias Ringwald                 packet_type = LOG_MESSAGE_PACKET;
133997ab207SMatthias Ringwald                 packet = (uint8_t*) log_message_buffer;
134997ab207SMatthias Ringwald             }
135128d6c99SMatthias Ringwald             hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
136128d6c99SMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
137128d6c99SMatthias Ringwald             break;
138128d6c99SMatthias Ringwald         case HCI_DUMP_PACKETLOGGER:
139997ab207SMatthias Ringwald             // ISO packets not supported
140997ab207SMatthias Ringwald             if (packet_type == HCI_ISO_DATA_PACKET){
141997ab207SMatthias Ringwald                 len = hci_dump_iso_summary(in, packet, len);
142997ab207SMatthias Ringwald                 packet_type = LOG_MESSAGE_PACKET;
143997ab207SMatthias Ringwald                 packet = (uint8_t*) log_message_buffer;
144997ab207SMatthias Ringwald             }
145128d6c99SMatthias Ringwald             hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
146128d6c99SMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
147128d6c99SMatthias Ringwald             break;
1489620d15fSMatthias Ringwald         case HCI_DUMP_BTSNOOP:
1499620d15fSMatthias Ringwald             // log messages not supported
1509620d15fSMatthias Ringwald             if (packet_type == LOG_MESSAGE_PACKET) return;
151b215babeSMatthias Ringwald             ts_usec = 0xdcddb30f2f8000LLU + 1000000LLU * curr_time.tv_sec + curr_time.tv_usec;
15226d7634cSMatthias Ringwald             // append packet type to pcap header
15326d7634cSMatthias Ringwald             hci_dump_setup_header_btsnoop(header.header_btsnoop, ts_usec >> 32, ts_usec & 0xFFFFFFFF, 0, packet_type, in, len+1);
15426d7634cSMatthias Ringwald             header.header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP] = packet_type;
15526d7634cSMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_BTSNOOP + 1;
1569620d15fSMatthias Ringwald             break;
157128d6c99SMatthias Ringwald         default:
1589620d15fSMatthias Ringwald             btstack_unreachable();
159128d6c99SMatthias Ringwald             return;
160128d6c99SMatthias Ringwald     }
161128d6c99SMatthias Ringwald 
1629e6ecc42SMatthias Ringwald     ssize_t bytes_written;
1639e6ecc42SMatthias Ringwald     bytes_written = write(dump_file, &header, header_len);
1649e6ecc42SMatthias Ringwald     UNUSED(bytes_written);
1659e6ecc42SMatthias Ringwald     bytes_written = write(dump_file, packet, len );
1669e6ecc42SMatthias Ringwald     UNUSED(bytes_written);
167128d6c99SMatthias Ringwald }
168128d6c99SMatthias Ringwald 
1692d17d4c0SMatthias Ringwald static void hci_dump_posix_fs_log_message(int log_level, const char * format, va_list argptr){
1702d17d4c0SMatthias Ringwald     UNUSED(log_level);
171128d6c99SMatthias Ringwald     if (dump_file < 0) return;
172128d6c99SMatthias Ringwald     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
173128d6c99SMatthias Ringwald     hci_dump_posix_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
174128d6c99SMatthias Ringwald }
175128d6c99SMatthias Ringwald 
176128d6c99SMatthias Ringwald // returns system errno
177128d6c99SMatthias Ringwald int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format){
1789620d15fSMatthias Ringwald     btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER || format == HCI_DUMP_BTSNOOP);
179128d6c99SMatthias Ringwald 
180128d6c99SMatthias Ringwald     dump_format = format;
181128d6c99SMatthias Ringwald     int oflags = O_WRONLY | O_CREAT | O_TRUNC;
182128d6c99SMatthias Ringwald #ifdef _WIN32
183128d6c99SMatthias Ringwald     oflags |= O_BINARY;
184128d6c99SMatthias Ringwald #endif
185128d6c99SMatthias Ringwald     dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
186128d6c99SMatthias Ringwald     if (dump_file < 0){
187128d6c99SMatthias Ringwald         printf("failed to open file %s, errno = %d\n", filename, errno);
188128d6c99SMatthias Ringwald         return errno;
189128d6c99SMatthias Ringwald     }
1909620d15fSMatthias Ringwald 
1919620d15fSMatthias Ringwald     if (format == HCI_DUMP_BTSNOOP){
1929620d15fSMatthias Ringwald         // write BTSnoop file header
1939620d15fSMatthias Ringwald         const uint8_t file_header[] = {
1949620d15fSMatthias Ringwald             // Identification Pattern: "btsnoop\0"
1959620d15fSMatthias Ringwald             0x62, 0x74, 0x73, 0x6E, 0x6F, 0x6F, 0x70, 0x00,
1969620d15fSMatthias Ringwald             // Version: 1
1979620d15fSMatthias Ringwald             0x00, 0x00, 0x00, 0x01,
19826d7634cSMatthias Ringwald             // Datalink Type: 1002 - H4
19926d7634cSMatthias Ringwald             0x00, 0x00, 0x03, 0xEA,
2009620d15fSMatthias Ringwald         };
2019e6ecc42SMatthias Ringwald         ssize_t bytes_written = write(dump_file, &file_header, sizeof(file_header));
2029e6ecc42SMatthias Ringwald         UNUSED(bytes_written);
2039620d15fSMatthias Ringwald     }
204128d6c99SMatthias Ringwald     return 0;
205128d6c99SMatthias Ringwald }
206128d6c99SMatthias Ringwald 
207128d6c99SMatthias Ringwald void hci_dump_posix_fs_close(void){
208128d6c99SMatthias Ringwald     close(dump_file);
209128d6c99SMatthias Ringwald     dump_file = -1;
210128d6c99SMatthias Ringwald }
211128d6c99SMatthias Ringwald 
212128d6c99SMatthias Ringwald const hci_dump_t * hci_dump_posix_fs_get_instance(void){
213128d6c99SMatthias Ringwald     static const hci_dump_t hci_dump_instance = {
214128d6c99SMatthias Ringwald         // void (*reset)(void);
215128d6c99SMatthias Ringwald         &hci_dump_posix_fs_reset,
216128d6c99SMatthias Ringwald         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
217128d6c99SMatthias Ringwald         &hci_dump_posix_fs_log_packet,
218128d6c99SMatthias Ringwald         // void (*log_message)(int log_level, const char * format, va_list argptr);
219128d6c99SMatthias Ringwald         &hci_dump_posix_fs_log_message,
220128d6c99SMatthias Ringwald     };
221128d6c99SMatthias Ringwald     return &hci_dump_instance;
222128d6c99SMatthias Ringwald }
223