xref: /btstack/src/hci_dump.c (revision 5fa31a9940804989b94f828b5982860dcb81c77d)
179662672Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci_dump.c"
39ab2c6ae4SMatthias Ringwald 
401713bceaSmatthias.ringwald /*
4179662672Smatthias.ringwald  *  hci_dump.c
4279662672Smatthias.ringwald  *
43fe1ed1b8Smatthias.ringwald  *  Dump HCI trace in various formats:
44fe1ed1b8Smatthias.ringwald  *
45fe1ed1b8Smatthias.ringwald  *  - BlueZ's hcidump format
46fe1ed1b8Smatthias.ringwald  *  - Apple's PacketLogger
47fe1ed1b8Smatthias.ringwald  *  - stdout hexdump
4879662672Smatthias.ringwald  *
4979662672Smatthias.ringwald  */
5079662672Smatthias.ringwald 
517907f069SMatthias Ringwald #include "btstack_config.h"
52a1d7dd1fSmatthias.ringwald 
53359cfe47SMatthias Ringwald // enable POSIX functions (needed for -std=c99)
54359cfe47SMatthias Ringwald #define _POSIX_C_SOURCE 200809
55359cfe47SMatthias Ringwald 
5679662672Smatthias.ringwald #include "hci_dump.h"
5779662672Smatthias.ringwald #include "hci.h"
58c6448b67Smatthias.ringwald #include "hci_transport.h"
5956042629SMatthias Ringwald #include "hci_cmd.h"
6082636622SMatthias Ringwald #include "btstack_run_loop.h"
61198a9e1bS[email protected] #include <stdio.h>
6279662672Smatthias.ringwald 
63eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
64d5ea8924S[email protected] #include <fcntl.h>        // open
6579662672Smatthias.ringwald #include <unistd.h>       // write
668adf0ddaSmatthias.ringwald #include <time.h>
678b658ebcSmatthias.ringwald #include <sys/time.h>     // for timestamps
68c7b9c559Smatthias.ringwald #include <sys/stat.h>     // for mode flags
6968e27c0fSmatthias.ringwald #endif
7079662672Smatthias.ringwald 
71*5fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
72*5fa31a99SMatthias Ringwald #include "SEGGER_RTT.h"
73*5fa31a99SMatthias Ringwald static char channel1_out[1024];
74*5fa31a99SMatthias Ringwald #endif
75*5fa31a99SMatthias Ringwald 
761a1c8389SMatthias Ringwald // BLUEZ hcidump - struct not used directly, but left here as documentation
778b658ebcSmatthias.ringwald typedef struct {
788b658ebcSmatthias.ringwald     uint16_t    len;
798b658ebcSmatthias.ringwald     uint8_t     in;
808b658ebcSmatthias.ringwald     uint8_t     pad;
818b658ebcSmatthias.ringwald     uint32_t    ts_sec;
828b658ebcSmatthias.ringwald     uint32_t    ts_usec;
838b658ebcSmatthias.ringwald     uint8_t     packet_type;
846c5c6faaSmatthias.ringwald }
856c5c6faaSmatthias.ringwald hcidump_hdr;
86f3e036dcSMatthias Ringwald #define HCIDUMP_HDR_SIZE 13
8779662672Smatthias.ringwald 
881a1c8389SMatthias Ringwald // APPLE PacketLogger - struct not used directly, but left here as documentation
898b658ebcSmatthias.ringwald typedef struct {
908b658ebcSmatthias.ringwald     uint32_t    len;
918b658ebcSmatthias.ringwald     uint32_t    ts_sec;
928b658ebcSmatthias.ringwald     uint32_t    ts_usec;
932df12229Smatthias.ringwald     uint8_t     type;   // 0xfc for note
946c5c6faaSmatthias.ringwald }
956c5c6faaSmatthias.ringwald pktlog_hdr;
96f3e036dcSMatthias Ringwald #define PKTLOG_HDR_SIZE 13
978b658ebcSmatthias.ringwald 
988b658ebcSmatthias.ringwald static int dump_file = -1;
998b658ebcSmatthias.ringwald static int dump_format;
100*5fa31a99SMatthias Ringwald static union {
101*5fa31a99SMatthias Ringwald     uint8_t header_bluez[HCIDUMP_HDR_SIZE];
102*5fa31a99SMatthias Ringwald     uint8_t header_packetlogger[PKTLOG_HDR_SIZE];
103*5fa31a99SMatthias Ringwald } header;
104*5fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
1058adf0ddaSmatthias.ringwald static char time_string[40];
1062992c131Smatthias.ringwald static int  max_nr_packets = -1;
1079ae0c346Smatthias.ringwald static int  nr_packets = 0;
108a1d7dd1fSmatthias.ringwald static char log_message_buffer[256];
10968e27c0fSmatthias.ringwald #endif
1108b658ebcSmatthias.ringwald 
1118a37b10aSMatthias Ringwald // levels: debug, info, error
1128a37b10aSMatthias Ringwald static int log_level_enabled[3] = { 1, 1, 1};
1138a37b10aSMatthias Ringwald 
114a225073eS[email protected] void hci_dump_open(const char *filename, hci_dump_format_t format){
115*5fa31a99SMatthias Ringwald 
1168b658ebcSmatthias.ringwald     dump_format = format;
117*5fa31a99SMatthias Ringwald 
118*5fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
1198adf0ddaSmatthias.ringwald     if (dump_format == HCI_DUMP_STDOUT) {
1208adf0ddaSmatthias.ringwald         dump_file = fileno(stdout);
1218adf0ddaSmatthias.ringwald     } else {
122eb443d3dSMatthias Ringwald 
1231e154302SMatthias Ringwald         int oflags = O_WRONLY | O_CREAT | O_TRUNC;
124b52eaea5S[email protected] #ifdef _WIN32
1251e154302SMatthias Ringwald         oflags |= O_BINARY;
126b52eaea5S[email protected] #endif
1271e154302SMatthias Ringwald         dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
1281e154302SMatthias Ringwald         if (dump_file < 0){
1291e154302SMatthias Ringwald             printf("hci_dump_open: failed to open file %s\n", filename);
1301e154302SMatthias Ringwald         }
13179662672Smatthias.ringwald     }
132eb443d3dSMatthias Ringwald #else
133*5fa31a99SMatthias Ringwald 
134d0662982SMatthias Ringwald     UNUSED(filename);
135*5fa31a99SMatthias Ringwald 
136*5fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
137*5fa31a99SMatthias Ringwald     switch (dump_format){
138*5fa31a99SMatthias Ringwald         case HCI_DUMP_PACKETLOGGER:
139*5fa31a99SMatthias Ringwald         case HCI_DUMP_BLUEZ:
140*5fa31a99SMatthias Ringwald             // Configure up channel 1, options:
141*5fa31a99SMatthias Ringwald             // - SEGGER_RTT_MODE_NO_BLOCK_SKIP
142*5fa31a99SMatthias Ringwald             // - SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
143*5fa31a99SMatthias Ringwald             // Note: with SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL, firwmware will hang if RTT not supported/debug probe not connected
144*5fa31a99SMatthias Ringwald             // Note: with SEGGER_RTT_MODE_NO_BLOCK_SKIP, there's a chance for log file corruption if second write (packet) is skipped
145*5fa31a99SMatthias Ringwald             SEGGER_RTT_ConfigUpBuffer(1, "hci_dump", &channel1_out[0], sizeof(channel1_out), SEGGER_RTT_MODE_NO_BLOCK_SKIP) ;;
146*5fa31a99SMatthias Ringwald             break;
147*5fa31a99SMatthias Ringwald         default:
148*5fa31a99SMatthias Ringwald             break;
149*5fa31a99SMatthias Ringwald     }
150*5fa31a99SMatthias Ringwald #endif
151d0662982SMatthias Ringwald 
152eb443d3dSMatthias Ringwald     dump_file = 1;
15368e27c0fSmatthias.ringwald #endif
154d9659922Smatthias.ringwald }
15579662672Smatthias.ringwald 
156eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
1572992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets){
1582992c131Smatthias.ringwald     max_nr_packets = packets;
1592992c131Smatthias.ringwald }
1607c5f7483Smatthias.ringwald #endif
1612992c131Smatthias.ringwald 
162*5fa31a99SMatthias Ringwald static void hci_dump_packetlogger_setup_header(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
163*5fa31a99SMatthias Ringwald     big_endian_store_32( buffer, 0, PKTLOG_HDR_SIZE - 4 + len);
164*5fa31a99SMatthias Ringwald     big_endian_store_32( buffer, 4, tv_sec);
165*5fa31a99SMatthias Ringwald     big_endian_store_32( buffer, 8, tv_us);
166*5fa31a99SMatthias Ringwald     uint8_t packet_logger_type = 0;
167*5fa31a99SMatthias Ringwald     switch (packet_type){
168*5fa31a99SMatthias Ringwald         case HCI_COMMAND_DATA_PACKET:
169*5fa31a99SMatthias Ringwald             packet_logger_type = 0x00;
170*5fa31a99SMatthias Ringwald             break;
171*5fa31a99SMatthias Ringwald         case HCI_ACL_DATA_PACKET:
172*5fa31a99SMatthias Ringwald             packet_logger_type = in ? 0x03 : 0x02;
173*5fa31a99SMatthias Ringwald             break;
174*5fa31a99SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
175*5fa31a99SMatthias Ringwald             packet_logger_type = in ? 0x09 : 0x08;
176*5fa31a99SMatthias Ringwald             break;
177*5fa31a99SMatthias Ringwald         case HCI_EVENT_PACKET:
178*5fa31a99SMatthias Ringwald             packet_logger_type = 0x01;
179*5fa31a99SMatthias Ringwald             break;
180*5fa31a99SMatthias Ringwald         case LOG_MESSAGE_PACKET:
181*5fa31a99SMatthias Ringwald             packet_logger_type = 0xfc;
182*5fa31a99SMatthias Ringwald             break;
183*5fa31a99SMatthias Ringwald         default:
184*5fa31a99SMatthias Ringwald             return;
185*5fa31a99SMatthias Ringwald     }
186*5fa31a99SMatthias Ringwald     buffer[12] = packet_logger_type;
187*5fa31a99SMatthias Ringwald }
188*5fa31a99SMatthias Ringwald 
189*5fa31a99SMatthias Ringwald static void hci_dump_bluez_setup_header(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
190*5fa31a99SMatthias Ringwald     little_endian_store_16( buffer, 0, 1 + len);
191*5fa31a99SMatthias Ringwald     buffer[2] = in;
192*5fa31a99SMatthias Ringwald     buffer[3] = 0;
193*5fa31a99SMatthias Ringwald     little_endian_store_32( buffer, 4, tv_sec);
194*5fa31a99SMatthias Ringwald     little_endian_store_32( buffer, 8, tv_us);
195*5fa31a99SMatthias Ringwald     buffer[12] = packet_type;
196*5fa31a99SMatthias Ringwald }
197*5fa31a99SMatthias Ringwald 
1988a37b10aSMatthias Ringwald static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
199198a9e1bS[email protected]     switch (packet_type){
200198a9e1bS[email protected]         case HCI_COMMAND_DATA_PACKET:
201198a9e1bS[email protected]             printf("CMD => ");
202198a9e1bS[email protected]             break;
203198a9e1bS[email protected]         case HCI_EVENT_PACKET:
204198a9e1bS[email protected]             printf("EVT <= ");
205198a9e1bS[email protected]             break;
206198a9e1bS[email protected]         case HCI_ACL_DATA_PACKET:
207198a9e1bS[email protected]             if (in) {
208198a9e1bS[email protected]                 printf("ACL <= ");
209198a9e1bS[email protected]             } else {
210198a9e1bS[email protected]                 printf("ACL => ");
211198a9e1bS[email protected]             }
212198a9e1bS[email protected]             break;
2131e154302SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
2141e154302SMatthias Ringwald             if (in) {
2151e154302SMatthias Ringwald                 printf("SCO <= ");
2161e154302SMatthias Ringwald             } else {
2171e154302SMatthias Ringwald                 printf("SCO => ");
2181e154302SMatthias Ringwald             }
2191e154302SMatthias Ringwald             break;
220198a9e1bS[email protected]         case LOG_MESSAGE_PACKET:
221198a9e1bS[email protected]             printf("LOG -- %s\n", (char*) packet);
222198a9e1bS[email protected]             return;
223198a9e1bS[email protected]         default:
224198a9e1bS[email protected]             return;
225198a9e1bS[email protected]     }
226198a9e1bS[email protected]     printf_hexdump(packet, len);
227198a9e1bS[email protected] }
228198a9e1bS[email protected] 
229ad6274a7SMatthias Ringwald static void printf_timestamp(void){
2306401061aSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
2316401061aSMatthias Ringwald     struct tm* ptm;
2326401061aSMatthias Ringwald     struct timeval curr_time;
2336401061aSMatthias Ringwald     gettimeofday(&curr_time, NULL);
2346401061aSMatthias Ringwald     time_t curr_time_secs = curr_time.tv_sec;
2356401061aSMatthias Ringwald     /* Obtain the time of day, and convert it to a tm struct. */
2366401061aSMatthias Ringwald     ptm = localtime (&curr_time_secs);
2376401061aSMatthias Ringwald     /* assert localtime was successful */
2386401061aSMatthias Ringwald     if (!ptm) return;
2396401061aSMatthias Ringwald     /* Format the date and time, down to a single second. */
2406401061aSMatthias Ringwald     strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
2416401061aSMatthias Ringwald     /* Compute milliseconds from microseconds. */
2426401061aSMatthias Ringwald     uint16_t milliseconds = curr_time.tv_usec / 1000;
2436401061aSMatthias Ringwald     /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */
2446401061aSMatthias Ringwald     printf ("%s.%03u] ", time_string, milliseconds);
2456401061aSMatthias Ringwald #else
246ad6274a7SMatthias Ringwald     uint32_t time_ms = btstack_run_loop_get_time_ms();
247ad6274a7SMatthias Ringwald     int      seconds = time_ms / 1000;
248ad6274a7SMatthias Ringwald     int      minutes = seconds / 60;
249f04a0c31SMatthias Ringwald     unsigned int hours = minutes / 60;
250ad6274a7SMatthias Ringwald 
251f04a0c31SMatthias Ringwald     uint16_t p_ms      = time_ms - (seconds * 1000);
252f04a0c31SMatthias Ringwald     uint16_t p_seconds = seconds - (minutes * 60);
253f04a0c31SMatthias Ringwald     uint16_t p_minutes = minutes - (hours   * 60);
254ad6274a7SMatthias Ringwald     printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
255ad6274a7SMatthias Ringwald #endif
2566401061aSMatthias Ringwald }
257ad6274a7SMatthias Ringwald 
25879662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
25968e27c0fSmatthias.ringwald 
2608b658ebcSmatthias.ringwald     if (dump_file < 0) return; // not activated yet
2618b658ebcSmatthias.ringwald 
262eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
2632992c131Smatthias.ringwald     // don't grow bigger than max_nr_packets
2642992c131Smatthias.ringwald     if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
2652992c131Smatthias.ringwald         if (nr_packets >= max_nr_packets){
2662992c131Smatthias.ringwald             lseek(dump_file, 0, SEEK_SET);
267acb15818SMatthias Ringwald             // avoid -Wunused-result
268acb15818SMatthias Ringwald             int res = ftruncate(dump_file, 0);
269acb15818SMatthias Ringwald             UNUSED(res);
2702992c131Smatthias.ringwald             nr_packets = 0;
2712992c131Smatthias.ringwald         }
2722992c131Smatthias.ringwald         nr_packets++;
2732992c131Smatthias.ringwald     }
274*5fa31a99SMatthias Ringwald #endif
2752992c131Smatthias.ringwald 
276*5fa31a99SMatthias Ringwald     if (dump_format == HCI_DUMP_STDOUT){
2776401061aSMatthias Ringwald         printf_timestamp();
278198a9e1bS[email protected]         printf_packet(packet_type, in, packet, len);
279*5fa31a99SMatthias Ringwald         return;
280a9cf4d77S[email protected]     }
2810d79c710Smatthias.ringwald 
282*5fa31a99SMatthias Ringwald     uint32_t tv_sec = 0;
283*5fa31a99SMatthias Ringwald     uint32_t tv_us  = 0;
284*5fa31a99SMatthias Ringwald 
285*5fa31a99SMatthias Ringwald     // get time
286*5fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
287*5fa31a99SMatthias Ringwald     struct timeval curr_time;
288*5fa31a99SMatthias Ringwald     gettimeofday(&curr_time, NULL);
289*5fa31a99SMatthias Ringwald     tv_sec = curr_time.tv_sec;
290*5fa31a99SMatthias Ringwald     tv_us  = curr_time.tv_usec;
291*5fa31a99SMatthias Ringwald #else
292*5fa31a99SMatthias Ringwald     uint32_t time_ms = btstack_run_loop_get_time_ms();
293*5fa31a99SMatthias Ringwald     tv_us   = time_ms * 1000;
294*5fa31a99SMatthias Ringwald     tv_sec  = 946728000UL + (time_ms / 1000);
295*5fa31a99SMatthias Ringwald #endif
296*5fa31a99SMatthias Ringwald 
297*5fa31a99SMatthias Ringwald     uint16_t header_len = 0;
298*5fa31a99SMatthias Ringwald     switch (dump_format){
2998b658ebcSmatthias.ringwald         case HCI_DUMP_BLUEZ:
300*5fa31a99SMatthias Ringwald             hci_dump_bluez_setup_header(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
301*5fa31a99SMatthias Ringwald             header_len = HCIDUMP_HDR_SIZE;
3028b658ebcSmatthias.ringwald             break;
3038b658ebcSmatthias.ringwald         case HCI_DUMP_PACKETLOGGER:
304*5fa31a99SMatthias Ringwald             hci_dump_packetlogger_setup_header(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
305*5fa31a99SMatthias Ringwald             header_len = PKTLOG_HDR_SIZE;
3060d79c710Smatthias.ringwald             break;
3078b658ebcSmatthias.ringwald         default:
3088b658ebcSmatthias.ringwald             return;
3098b658ebcSmatthias.ringwald     }
310*5fa31a99SMatthias Ringwald 
311*5fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
312*5fa31a99SMatthias Ringwald     // avoid -Wunused-result
313*5fa31a99SMatthias Ringwald     int res = 0;
314*5fa31a99SMatthias Ringwald     res = write (dump_file, &header, header_len);
315acb15818SMatthias Ringwald     res = write (dump_file, packet, len );
316acb15818SMatthias Ringwald     UNUSED(res);
31768e27c0fSmatthias.ringwald #endif
318*5fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
319*5fa31a99SMatthias Ringwald     SEGGER_RTT_Write(1, &header, header_len);
320*5fa31a99SMatthias Ringwald     SEGGER_RTT_Write(1, packet, len);
321*5fa31a99SMatthias Ringwald #endif
322*5fa31a99SMatthias Ringwald     UNUSED(header_len);
32379662672Smatthias.ringwald }
32479662672Smatthias.ringwald 
3258a37b10aSMatthias Ringwald static int hci_dump_log_level_active(int log_level){
326fa087deaSMatthias Ringwald     if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return 0;
327fa087deaSMatthias Ringwald     if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return 0;
3288a37b10aSMatthias Ringwald     return log_level_enabled[log_level];
3298a37b10aSMatthias Ringwald }
3308a37b10aSMatthias Ringwald 
33194be1a66SMatthias Ringwald void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){
3326401061aSMatthias Ringwald     if (!hci_dump_log_level_active(log_level)) return;
3336401061aSMatthias Ringwald 
334eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
3356401061aSMatthias Ringwald     if (dump_file >= 0){
336eb443d3dSMatthias Ringwald         int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
337eb443d3dSMatthias Ringwald         hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
338b8ae70b4SMatthias Ringwald         return;
3396401061aSMatthias Ringwald     }
3406401061aSMatthias Ringwald #endif
3416401061aSMatthias Ringwald 
342ad6274a7SMatthias Ringwald     printf_timestamp();
3431df3b679S[email protected]     printf("LOG -- ");
3441df3b679S[email protected]     vprintf(format, argptr);
3451fd51e45S[email protected]     printf("\n");
34694be1a66SMatthias Ringwald }
34794be1a66SMatthias Ringwald 
34894be1a66SMatthias Ringwald void hci_dump_log(int log_level, const char * format, ...){
34994be1a66SMatthias Ringwald     va_list argptr;
35094be1a66SMatthias Ringwald     va_start(argptr, format);
35194be1a66SMatthias Ringwald     hci_dump_log_va_arg(log_level, format, argptr);
3521df3b679S[email protected]     va_end(argptr);
353a1d7dd1fSmatthias.ringwald }
354a1d7dd1fSmatthias.ringwald 
35520ea11b9S[email protected] #ifdef __AVR__
3568a37b10aSMatthias Ringwald void hci_dump_log_P(int log_level, PGM_P format, ...){
3578a37b10aSMatthias Ringwald     if (!hci_dump_log_level_active(log_level)) return;
35820ea11b9S[email protected]     va_list argptr;
35920ea11b9S[email protected]     va_start(argptr, format);
36020ea11b9S[email protected]     printf_P(PSTR("LOG -- "));
36120ea11b9S[email protected]     vfprintf_P(stdout, format, argptr);
36220ea11b9S[email protected]     printf_P(PSTR("\n"));
36320ea11b9S[email protected]     va_end(argptr);
36420ea11b9S[email protected] }
36520ea11b9S[email protected] #endif
36620ea11b9S[email protected] 
36771de195eSMatthias Ringwald void hci_dump_close(void){
368eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
36979662672Smatthias.ringwald     close(dump_file);
37068e27c0fSmatthias.ringwald #endif
371eb443d3dSMatthias Ringwald     dump_file = -1;
37279662672Smatthias.ringwald }
37379662672Smatthias.ringwald 
3748a37b10aSMatthias Ringwald void hci_dump_enable_log_level(int log_level, int enable){
375fa087deaSMatthias Ringwald     if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return;
376fa087deaSMatthias Ringwald     if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return;
3778a37b10aSMatthias Ringwald     log_level_enabled[log_level] = enable;
3788a37b10aSMatthias Ringwald }
3798a37b10aSMatthias Ringwald 
380