xref: /btstack/platform/posix/hci_dump_posix_fs.c (revision 1d3bd1e51ca491d6783233c8d7431c44f06daa5a)
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 "hci_cmd.h"
60 
61 #include <time.h>
62 #include <stdio.h>        // printf
63 #include <fcntl.h>        // open
64 #include <unistd.h>       // write
65 #include <errno.h>        // errno
66 #include <sys/time.h>     // for timestamps
67 #include <sys/stat.h>     // file modes
68 
69 static int  dump_file = -1;
70 static int  dump_format;
71 static char log_message_buffer[256];
72 
73 static void hci_dump_posix_fs_reset(void){
74     btstack_assert(dump_file >= 0);
75     (void) lseek(dump_file, 0, SEEK_SET);
76     int err = ftruncate(dump_file, 0);
77     UNUSED(err);
78 }
79 
80 static void hci_dump_posix_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
81     if (dump_file < 0) return;
82 
83     static union {
84         uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
85         uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
86         uint8_t header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP];
87     } header;
88 
89     uint32_t tv_sec = 0;
90     uint32_t tv_us  = 0;
91     uint64_t ts_usec;
92 
93     // get time
94     struct timeval curr_time;
95     gettimeofday(&curr_time, NULL);
96     tv_sec = curr_time.tv_sec;
97     tv_us  = curr_time.tv_usec;
98 
99     uint16_t header_len = 0;
100     switch (dump_format){
101         case HCI_DUMP_BLUEZ:
102             hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
103             header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
104             break;
105         case HCI_DUMP_PACKETLOGGER:
106             hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
107             header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
108             break;
109         case HCI_DUMP_BTSNOOP:
110             // log messages not supported
111             if (packet_type == LOG_MESSAGE_PACKET) return;
112             ts_usec = ((uint64_t)  curr_time.tv_sec) * 1000000 + curr_time.tv_usec;
113             hci_dump_setup_header_btsnoop(header.header_btsnoop, ts_usec >> 32, ts_usec & 0xFFFFFFFF, 0, packet_type, in, len);
114             header_len = HCI_DUMP_HEADER_SIZE_BTSNOOP;
115             break;
116         default:
117             btstack_unreachable();
118             return;
119     }
120 
121     ssize_t bytes_written;
122     bytes_written = write(dump_file, &header, header_len);
123     UNUSED(bytes_written);
124     bytes_written = write(dump_file, packet, len );
125     UNUSED(bytes_written);
126 }
127 
128 static void hci_dump_posix_fs_log_message(const char * format, va_list argptr){
129     if (dump_file < 0) return;
130     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
131     hci_dump_posix_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
132 }
133 
134 // returns system errno
135 int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format){
136     btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER || format == HCI_DUMP_BTSNOOP);
137 
138     dump_format = format;
139     int oflags = O_WRONLY | O_CREAT | O_TRUNC;
140 #ifdef _WIN32
141     oflags |= O_BINARY;
142 #endif
143     dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
144     if (dump_file < 0){
145         printf("failed to open file %s, errno = %d\n", filename, errno);
146         return errno;
147     }
148 
149     if (format == HCI_DUMP_BTSNOOP){
150         // write BTSnoop file header
151         const uint8_t file_header[] = {
152             // Identification Pattern: "btsnoop\0"
153             0x62, 0x74, 0x73, 0x6E, 0x6F, 0x6F, 0x70, 0x00,
154             // Version: 1
155             0x00, 0x00, 0x00, 0x01,
156             // Datalink Type: 1001 - Un-encapsulated HCI
157             0x00, 0x00, 0x03, 0xE9,
158         };
159         ssize_t bytes_written = write(dump_file, &file_header, sizeof(file_header));
160         UNUSED(bytes_written);
161     }
162     return 0;
163 }
164 
165 void hci_dump_posix_fs_close(void){
166     close(dump_file);
167     dump_file = -1;
168 }
169 
170 const hci_dump_t * hci_dump_posix_fs_get_instance(void){
171     static const hci_dump_t hci_dump_instance = {
172         // void (*reset)(void);
173         &hci_dump_posix_fs_reset,
174         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
175         &hci_dump_posix_fs_log_packet,
176         // void (*log_message)(int log_level, const char * format, va_list argptr);
177         &hci_dump_posix_fs_log_message,
178     };
179     return &hci_dump_instance;
180 }
181