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