xref: /btstack/src/hci_dump.c (revision 7dc86dfd3569d69491d87d64749fd45afb46c67a)
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 static void printf_timestamp(void){
168 #ifdef HAVE_POSIX_FILE_IO
169     struct tm* ptm;
170     struct timeval curr_time;
171     gettimeofday(&curr_time, NULL);
172     time_t curr_time_secs = curr_time.tv_sec;
173     /* Obtain the time of day, and convert it to a tm struct. */
174     ptm = localtime (&curr_time_secs);
175     /* assert localtime was successful */
176     if (!ptm) return;
177     /* Format the date and time, down to a single second. */
178     strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
179     /* Compute milliseconds from microseconds. */
180     uint16_t milliseconds = curr_time.tv_usec / 1000;
181     /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */
182     printf ("%s.%03u] ", time_string, milliseconds);
183 #else
184     uint32_t time_ms = btstack_run_loop_get_time_ms();
185     int      seconds = time_ms / 1000;
186     int      minutes = seconds / 60;
187     unsigned int hours   = minutes / 60;
188 
189     uint16_t p_ms      = time_ms - (seconds * 1000);
190     uint16_t p_seconds = seconds - (minutes * 60);
191     uint16_t p_minutes = minutes - (hours   * 60);
192     printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
193 #endif
194 }
195 
196 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
197 
198     if (dump_file < 0) return; // not activated yet
199 
200 #ifdef HAVE_POSIX_FILE_IO
201 
202     // don't grow bigger than max_nr_packets
203     if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
204         if (nr_packets >= max_nr_packets){
205             lseek(dump_file, 0, SEEK_SET);
206             ftruncate(dump_file, 0);
207             nr_packets = 0;
208         }
209         nr_packets++;
210     }
211 
212     // get time
213     struct timeval curr_time;
214     gettimeofday(&curr_time, NULL);
215 
216     switch (dump_format){
217         case HCI_DUMP_STDOUT: {
218             printf_timestamp();
219             printf_packet(packet_type, in, packet, len);
220             break;
221         }
222 
223         case HCI_DUMP_BLUEZ:
224             little_endian_store_16( header_bluez, 0, 1 + len);
225             header_bluez[2] = in;
226             header_bluez[3] = 0;
227             little_endian_store_32( header_bluez, 4, (uint32_t) curr_time.tv_sec);
228             little_endian_store_32( header_bluez, 8,            curr_time.tv_usec);
229             header_bluez[12] = packet_type;
230             write (dump_file, header_bluez, HCIDUMP_HDR_SIZE);
231             write (dump_file, packet, len );
232             break;
233 
234         case HCI_DUMP_PACKETLOGGER:
235             big_endian_store_32( header_packetlogger, 0, PKTLOG_HDR_SIZE - 4 + len);
236             big_endian_store_32( header_packetlogger, 4,  (uint32_t) curr_time.tv_sec);
237             big_endian_store_32( header_packetlogger, 8, curr_time.tv_usec);
238             switch (packet_type){
239                 case HCI_COMMAND_DATA_PACKET:
240                     header_packetlogger[12] = 0x00;
241                     break;
242                 case HCI_ACL_DATA_PACKET:
243                     if (in) {
244                         header_packetlogger[12] = 0x03;
245                     } else {
246                         header_packetlogger[12] = 0x02;
247                     }
248                     break;
249                 case HCI_SCO_DATA_PACKET:
250                     if (in) {
251                         header_packetlogger[12] = 0x09;
252                     } else {
253                         header_packetlogger[12] = 0x08;
254                     }
255                     break;
256                 case HCI_EVENT_PACKET:
257                     header_packetlogger[12] = 0x01;
258                     break;
259                 case LOG_MESSAGE_PACKET:
260                     header_packetlogger[12] = 0xfc;
261                     break;
262                 default:
263                     return;
264             }
265             write (dump_file, &header_packetlogger, PKTLOG_HDR_SIZE);
266             write (dump_file, packet, len );
267             break;
268 
269         default:
270             break;
271     }
272 #else
273 
274     printf_timestamp();
275     printf_packet(packet_type, in, packet, len);
276 
277 #endif
278 }
279 
280 static int hci_dump_log_level_active(int log_level){
281     if (log_level < 0) return 0;
282     if (log_level > LOG_LEVEL_ERROR) return 0;
283     return log_level_enabled[log_level];
284 }
285 
286 void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){
287     if (!hci_dump_log_level_active(log_level)) return;
288 
289 #ifdef HAVE_POSIX_FILE_IO
290     if (dump_file >= 0){
291         int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
292         hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
293         return;
294     }
295 #endif
296 
297     printf_timestamp();
298     printf("LOG -- ");
299     vprintf(format, argptr);
300     printf("\n");
301 }
302 
303 void hci_dump_log(int log_level, const char * format, ...){
304     va_list argptr;
305     va_start(argptr, format);
306     hci_dump_log_va_arg(log_level, format, argptr);
307     va_end(argptr);
308 }
309 
310 #ifdef __AVR__
311 void hci_dump_log_P(int log_level, PGM_P format, ...){
312     if (!hci_dump_log_level_active(log_level)) return;
313     va_list argptr;
314     va_start(argptr, format);
315     printf_P(PSTR("LOG -- "));
316     vfprintf_P(stdout, format, argptr);
317     printf_P(PSTR("\n"));
318     va_end(argptr);
319 }
320 #endif
321 
322 void hci_dump_close(void){
323 #ifdef HAVE_POSIX_FILE_IO
324     close(dump_file);
325 #endif
326     dump_file = -1;
327 }
328 
329 void hci_dump_enable_log_level(int log_level, int enable){
330     if (log_level < 0) return;
331     if (log_level > LOG_LEVEL_ERROR) return;
332     log_level_enabled[log_level] = enable;
333 }
334 
335