xref: /btstack/platform/posix/hci_dump_posix_fs.c (revision 9620d15fc7b5ce2b46a496e1d55ced1f2cb946f1)
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
23128d6c99SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24128d6c99SMatthias Ringwald  * RINGWALD 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 
56128d6c99SMatthias Ringwald #include "hci_dump_posix_fs.h"
57128d6c99SMatthias Ringwald 
58128d6c99SMatthias Ringwald #include "btstack_debug.h"
59128d6c99SMatthias Ringwald #include "hci_cmd.h"
60128d6c99SMatthias Ringwald 
61128d6c99SMatthias Ringwald #include <time.h>
62128d6c99SMatthias Ringwald #include <stdio.h>        // printf
63128d6c99SMatthias Ringwald #include <fcntl.h>        // open
64128d6c99SMatthias Ringwald #include <unistd.h>       // write
65128d6c99SMatthias Ringwald #include <errno.h>        // errno
66128d6c99SMatthias Ringwald #include <sys/time.h>     // for timestamps
67128d6c99SMatthias Ringwald #include <sys/stat.h>     // file modes
68128d6c99SMatthias Ringwald 
69128d6c99SMatthias Ringwald static int  dump_file = -1;
70128d6c99SMatthias Ringwald static int  dump_format;
71128d6c99SMatthias Ringwald static char log_message_buffer[256];
72128d6c99SMatthias Ringwald 
73128d6c99SMatthias Ringwald static void hci_dump_posix_fs_reset(void){
74128d6c99SMatthias Ringwald     btstack_assert(dump_file >= 0);
75128d6c99SMatthias Ringwald     (void) lseek(dump_file, 0, SEEK_SET);
76128d6c99SMatthias Ringwald     (void) ftruncate(dump_file, 0);
77128d6c99SMatthias Ringwald }
78128d6c99SMatthias Ringwald 
79128d6c99SMatthias Ringwald static void hci_dump_posix_fs_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
80128d6c99SMatthias Ringwald     if (dump_file < 0) return;
81128d6c99SMatthias Ringwald 
82128d6c99SMatthias Ringwald     static union {
83128d6c99SMatthias Ringwald         uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
84128d6c99SMatthias Ringwald         uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
85*9620d15fSMatthias Ringwald         uint8_t header_btsnoop[HCI_DUMP_HEADER_SIZE_BTSNOOP];
86128d6c99SMatthias Ringwald     } header;
87128d6c99SMatthias Ringwald 
88128d6c99SMatthias Ringwald     uint32_t tv_sec = 0;
89128d6c99SMatthias Ringwald     uint32_t tv_us  = 0;
90*9620d15fSMatthias Ringwald     uint64_t ts_usec;
91128d6c99SMatthias Ringwald 
92128d6c99SMatthias Ringwald     // get time
93128d6c99SMatthias Ringwald     struct timeval curr_time;
94128d6c99SMatthias Ringwald     gettimeofday(&curr_time, NULL);
95128d6c99SMatthias Ringwald     tv_sec = curr_time.tv_sec;
96128d6c99SMatthias Ringwald     tv_us  = curr_time.tv_usec;
97128d6c99SMatthias Ringwald 
98128d6c99SMatthias Ringwald     uint16_t header_len = 0;
99128d6c99SMatthias Ringwald     switch (dump_format){
100128d6c99SMatthias Ringwald         case HCI_DUMP_BLUEZ:
101128d6c99SMatthias Ringwald             hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
102128d6c99SMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
103128d6c99SMatthias Ringwald             break;
104128d6c99SMatthias Ringwald         case HCI_DUMP_PACKETLOGGER:
105128d6c99SMatthias Ringwald             hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
106128d6c99SMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
107128d6c99SMatthias Ringwald             break;
108*9620d15fSMatthias Ringwald         case HCI_DUMP_BTSNOOP:
109*9620d15fSMatthias Ringwald             // log messages not supported
110*9620d15fSMatthias Ringwald             if (packet_type == LOG_MESSAGE_PACKET) return;
111*9620d15fSMatthias Ringwald             ts_usec = ((uint64_t)  curr_time.tv_sec) * 1000000 + curr_time.tv_usec;
112*9620d15fSMatthias Ringwald             hci_dump_setup_header_btsnoop(header.header_btsnoop, ts_usec >> 32, ts_usec & 0xFFFFFFFF, 0, packet_type, in, len);
113*9620d15fSMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_BTSNOOP;
114*9620d15fSMatthias Ringwald             break;
115128d6c99SMatthias Ringwald         default:
116*9620d15fSMatthias Ringwald             btstack_unreachable();
117128d6c99SMatthias Ringwald             return;
118128d6c99SMatthias Ringwald     }
119128d6c99SMatthias Ringwald 
120128d6c99SMatthias Ringwald     (void) write(dump_file, &header, header_len);
121128d6c99SMatthias Ringwald     (void) write(dump_file, packet, len );
122128d6c99SMatthias Ringwald }
123128d6c99SMatthias Ringwald 
124128d6c99SMatthias Ringwald static void hci_dump_posix_fs_log_message(const char * format, va_list argptr){
125128d6c99SMatthias Ringwald     if (dump_file < 0) return;
126128d6c99SMatthias Ringwald     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
127128d6c99SMatthias Ringwald     hci_dump_posix_fs_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
128128d6c99SMatthias Ringwald }
129128d6c99SMatthias Ringwald 
130128d6c99SMatthias Ringwald // returns system errno
131128d6c99SMatthias Ringwald int hci_dump_posix_fs_open(const char *filename, hci_dump_format_t format){
132*9620d15fSMatthias Ringwald     btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER || format == HCI_DUMP_BTSNOOP);
133128d6c99SMatthias Ringwald 
134128d6c99SMatthias Ringwald     dump_format = format;
135128d6c99SMatthias Ringwald     int oflags = O_WRONLY | O_CREAT | O_TRUNC;
136128d6c99SMatthias Ringwald #ifdef _WIN32
137128d6c99SMatthias Ringwald     oflags |= O_BINARY;
138128d6c99SMatthias Ringwald #endif
139128d6c99SMatthias Ringwald     dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
140128d6c99SMatthias Ringwald     if (dump_file < 0){
141128d6c99SMatthias Ringwald         printf("failed to open file %s, errno = %d\n", filename, errno);
142128d6c99SMatthias Ringwald         return errno;
143128d6c99SMatthias Ringwald     }
144*9620d15fSMatthias Ringwald 
145*9620d15fSMatthias Ringwald     if (format == HCI_DUMP_BTSNOOP){
146*9620d15fSMatthias Ringwald         // write BTSnoop file header
147*9620d15fSMatthias Ringwald         const uint8_t file_header[] = {
148*9620d15fSMatthias Ringwald             // Identification Pattern: "btsnoop\0"
149*9620d15fSMatthias Ringwald             0x62, 0x74, 0x73, 0x6E, 0x6F, 0x6F, 0x70, 0x00,
150*9620d15fSMatthias Ringwald             // Version: 1
151*9620d15fSMatthias Ringwald             0x00, 0x00, 0x00, 0x01,
152*9620d15fSMatthias Ringwald             // Datalink Type: 1001 - Un-encapsulated HCI
153*9620d15fSMatthias Ringwald             0x00, 0x00, 0x03, 0xE9,
154*9620d15fSMatthias Ringwald         };
155*9620d15fSMatthias Ringwald         (void) write(dump_file, &file_header, sizeof(file_header));
156*9620d15fSMatthias Ringwald     }
157128d6c99SMatthias Ringwald     return 0;
158128d6c99SMatthias Ringwald }
159128d6c99SMatthias Ringwald 
160128d6c99SMatthias Ringwald void hci_dump_posix_fs_close(void){
161128d6c99SMatthias Ringwald     close(dump_file);
162128d6c99SMatthias Ringwald     dump_file = -1;
163128d6c99SMatthias Ringwald }
164128d6c99SMatthias Ringwald 
165128d6c99SMatthias Ringwald const hci_dump_t * hci_dump_posix_fs_get_instance(void){
166128d6c99SMatthias Ringwald     static const hci_dump_t hci_dump_instance = {
167128d6c99SMatthias Ringwald         // void (*reset)(void);
168128d6c99SMatthias Ringwald         &hci_dump_posix_fs_reset,
169128d6c99SMatthias Ringwald         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
170128d6c99SMatthias Ringwald         &hci_dump_posix_fs_log_packet,
171128d6c99SMatthias Ringwald         // void (*log_message)(int log_level, const char * format, va_list argptr);
172128d6c99SMatthias Ringwald         &hci_dump_posix_fs_log_message,
173128d6c99SMatthias Ringwald     };
174128d6c99SMatthias Ringwald     return &hci_dump_instance;
175128d6c99SMatthias Ringwald }
176