xref: /btstack/src/hci_dump.c (revision 1e598166446edd12daa519488aedfe7ced2004e2)
1 /*
2  * Copyright (C) 2014 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 MATTHIAS
24  * RINGWALD 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 /*
39  *  hci_dump.c
40  *
41  *  Dump HCI trace in various formats:
42  *
43  *  - BlueZ's hcidump format
44  *  - Apple's PacketLogger
45  *  - stdout hexdump
46  *
47  *  Created by Matthias Ringwald on 5/26/09.
48  */
49 
50 #include "btstack-config.h"
51 
52 #include "hci_dump.h"
53 #include "hci.h"
54 #include "hci_transport.h"
55 #include "hci_cmd.h"
56 #include "btstack_run_loop.h"
57 #include <stdio.h>
58 
59 #ifndef EMBEDDED
60 #include <fcntl.h>        // open
61 #include <unistd.h>       // write
62 #include <time.h>
63 #include <sys/time.h>     // for timestamps
64 #include <sys/stat.h>     // for mode flags
65 #include <stdarg.h>       // for va_list
66 #endif
67 
68 // BLUEZ hcidump - struct not used directly, but left here as documentation
69 typedef struct {
70     uint16_t    len;
71     uint8_t     in;
72     uint8_t     pad;
73     uint32_t    ts_sec;
74     uint32_t    ts_usec;
75     uint8_t     packet_type;
76 }
77 hcidump_hdr;
78 #define HCIDUMP_HDR_SIZE 13
79 
80 // APPLE PacketLogger - struct not used directly, but left here as documentation
81 typedef struct {
82     uint32_t    len;
83     uint32_t    ts_sec;
84     uint32_t    ts_usec;
85     uint8_t     type;   // 0xfc for note
86 }
87 pktlog_hdr;
88 #define PKTLOG_HDR_SIZE 13
89 
90 static int dump_file = -1;
91 #ifndef EMBEDDED
92 static int dump_format;
93 static uint8_t header_bluez[HCIDUMP_HDR_SIZE];
94 static uint8_t header_packetlogger[PKTLOG_HDR_SIZE];
95 static char time_string[40];
96 static int  max_nr_packets = -1;
97 static int  nr_packets = 0;
98 static char log_message_buffer[256];
99 #endif
100 
101 // levels: debug, info, error
102 static int log_level_enabled[3] = { 1, 1, 1};
103 
104 void hci_dump_open(const char *filename, hci_dump_format_t format){
105 #ifdef EMBEDDED
106     dump_file = 1;
107 #else
108     dump_format = format;
109     if (dump_format == HCI_DUMP_STDOUT) {
110         dump_file = fileno(stdout);
111     } else {
112 #ifdef _WIN32
113         dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
114 #else
115         dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
116 #endif
117     }
118 #endif
119 }
120 
121 #ifndef EMBEDDED
122 void hci_dump_set_max_packets(int packets){
123     max_nr_packets = packets;
124 }
125 #endif
126 
127 static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
128     switch (packet_type){
129         case HCI_COMMAND_DATA_PACKET:
130             printf("CMD => ");
131             break;
132         case HCI_EVENT_PACKET:
133             printf("EVT <= ");
134             break;
135         case HCI_ACL_DATA_PACKET:
136             if (in) {
137                 printf("ACL <= ");
138             } else {
139                 printf("ACL => ");
140             }
141             break;
142         case LOG_MESSAGE_PACKET:
143             printf("LOG -- %s\n", (char*) packet);
144             return;
145         default:
146             return;
147     }
148     printf_hexdump(packet, len);
149 }
150 
151 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
152 
153     if (dump_file < 0) return; // not activated yet
154 
155 #ifdef EMBEDDED
156 // #ifdef HAVE_TICK
157 //     uint32_t time_ms = btstack_run_loop_embedded_get_time_ms();
158 //     printf("[%06u] ", time_ms);
159 // #endif
160     printf_packet(packet_type, in, packet, len);
161 #else
162     // don't grow bigger than max_nr_packets
163     if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
164         if (nr_packets >= max_nr_packets){
165             lseek(dump_file, 0, SEEK_SET);
166             ftruncate(dump_file, 0);
167             nr_packets = 0;
168         }
169         nr_packets++;
170     }
171 
172     // get time
173     struct timeval curr_time;
174     struct tm* ptm;
175     gettimeofday(&curr_time, NULL);
176     time_t curr_time_secs = curr_time.tv_sec;
177 
178     switch (dump_format){
179         case HCI_DUMP_STDOUT: {
180             /* Obtain the time of day, and convert it to a tm struct. */
181             ptm = localtime (&curr_time_secs);
182             /* Format the date and time, down to a single second. */
183             strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
184             /* Compute milliseconds from microseconds. */
185             uint16_t milliseconds = curr_time.tv_usec / 1000;
186             /* Print the formatted time, in seconds, followed by a decimal point
187              and the milliseconds. */
188             printf ("%s.%03u] ", time_string, milliseconds);
189             printf_packet(packet_type, in, packet, len);
190             break;
191         }
192 
193         case HCI_DUMP_BLUEZ:
194             bt_store_16( header_bluez, 0, 1 + len);
195             header_bluez[2] = in;
196             header_bluez[3] = 0;
197             bt_store_32( header_bluez, 4, curr_time.tv_sec);
198             bt_store_32( header_bluez, 8, curr_time.tv_usec);
199             header_bluez[12] = packet_type;
200             write (dump_file, header_bluez, HCIDUMP_HDR_SIZE);
201             write (dump_file, packet, len );
202             break;
203 
204         case HCI_DUMP_PACKETLOGGER:
205             net_store_32( header_packetlogger, 0, PKTLOG_HDR_SIZE - 4 + len);
206             net_store_32( header_packetlogger, 4, curr_time.tv_sec);
207             net_store_32( header_packetlogger, 8, curr_time.tv_usec);
208             switch (packet_type){
209                 case HCI_COMMAND_DATA_PACKET:
210                     header_packetlogger[12] = 0x00;
211                     break;
212                 case HCI_ACL_DATA_PACKET:
213                     if (in) {
214                         header_packetlogger[12] = 0x03;
215                     } else {
216                         header_packetlogger[12] = 0x02;
217                     }
218                     break;
219                 case HCI_SCO_DATA_PACKET:
220                     if (in) {
221                         header_packetlogger[12] = 0x09;
222                     } else {
223                         header_packetlogger[12] = 0x08;
224                     }
225                     break;
226                 case HCI_EVENT_PACKET:
227                     header_packetlogger[12] = 0x01;
228                     break;
229                 case LOG_MESSAGE_PACKET:
230                     header_packetlogger[12] = 0xfc;
231                     break;
232                 default:
233                     return;
234             }
235             write (dump_file, &header_packetlogger, PKTLOG_HDR_SIZE);
236             write (dump_file, packet, len );
237             break;
238 
239         default:
240             break;
241     }
242 #endif
243 }
244 
245 static int hci_dump_log_level_active(int log_level){
246     if (log_level < 0) return 0;
247     if (log_level > LOG_LEVEL_ERROR) return 0;
248     return log_level_enabled[log_level];
249 }
250 
251 void hci_dump_log(int log_level, const char * format, ...){
252     if (!hci_dump_log_level_active(log_level)) return;
253     va_list argptr;
254     va_start(argptr, format);
255 #ifdef EMBEDDED
256     printf("LOG -- ");
257     vprintf(format, argptr);
258     printf("\n");
259 #else
260     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
261     hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
262 #endif
263     va_end(argptr);
264 }
265 
266 #ifdef __AVR__
267 void hci_dump_log_P(int log_level, PGM_P format, ...){
268     if (!hci_dump_log_level_active(log_level)) return;
269     va_list argptr;
270     va_start(argptr, format);
271     printf_P(PSTR("LOG -- "));
272     vfprintf_P(stdout, format, argptr);
273     printf_P(PSTR("\n"));
274     va_end(argptr);
275 }
276 #endif
277 
278 void hci_dump_close(void){
279 #ifndef EMBEDDED
280     close(dump_file);
281     dump_file = -1;
282 #endif
283 }
284 
285 void hci_dump_enable_log_level(int log_level, int enable){
286     if (log_level < 0) return;
287     if (log_level > LOG_LEVEL_ERROR) return;
288     log_level_enabled[log_level] = enable;
289 }
290 
291