xref: /btstack/src/hci_dump.c (revision 1fb071232ae86e40230a89c18a08900f55014990)
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 
715fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
725fa31a99SMatthias Ringwald #include "SEGGER_RTT.h"
735fa31a99SMatthias Ringwald static char channel1_out[1024];
745fa31a99SMatthias Ringwald #endif
755fa31a99SMatthias 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;
1005fa31a99SMatthias Ringwald static union {
1015fa31a99SMatthias Ringwald     uint8_t header_bluez[HCIDUMP_HDR_SIZE];
1025fa31a99SMatthias Ringwald     uint8_t header_packetlogger[PKTLOG_HDR_SIZE];
1035fa31a99SMatthias Ringwald } header;
1045fa31a99SMatthias 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;
108*1fb07123SMatthias Ringwald #endif
109*1fb07123SMatthias Ringwald 
110*1fb07123SMatthias Ringwald #if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT)
111a1d7dd1fSmatthias.ringwald static char log_message_buffer[256];
11268e27c0fSmatthias.ringwald #endif
1138b658ebcSmatthias.ringwald 
1148a37b10aSMatthias Ringwald // levels: debug, info, error
1158a37b10aSMatthias Ringwald static int log_level_enabled[3] = { 1, 1, 1};
1168a37b10aSMatthias Ringwald 
117a225073eS[email protected] void hci_dump_open(const char *filename, hci_dump_format_t format){
1185fa31a99SMatthias Ringwald 
1198b658ebcSmatthias.ringwald     dump_format = format;
1205fa31a99SMatthias Ringwald 
1215fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
1228adf0ddaSmatthias.ringwald     if (dump_format == HCI_DUMP_STDOUT) {
1238adf0ddaSmatthias.ringwald         dump_file = fileno(stdout);
1248adf0ddaSmatthias.ringwald     } else {
125eb443d3dSMatthias Ringwald 
1261e154302SMatthias Ringwald         int oflags = O_WRONLY | O_CREAT | O_TRUNC;
127b52eaea5S[email protected] #ifdef _WIN32
1281e154302SMatthias Ringwald         oflags |= O_BINARY;
129b52eaea5S[email protected] #endif
1301e154302SMatthias Ringwald         dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
1311e154302SMatthias Ringwald         if (dump_file < 0){
1321e154302SMatthias Ringwald             printf("hci_dump_open: failed to open file %s\n", filename);
1331e154302SMatthias Ringwald         }
13479662672Smatthias.ringwald     }
135eb443d3dSMatthias Ringwald #else
1365fa31a99SMatthias Ringwald 
137d0662982SMatthias Ringwald     UNUSED(filename);
1385fa31a99SMatthias Ringwald 
1395fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
1405fa31a99SMatthias Ringwald     switch (dump_format){
1415fa31a99SMatthias Ringwald         case HCI_DUMP_PACKETLOGGER:
1425fa31a99SMatthias Ringwald         case HCI_DUMP_BLUEZ:
1435fa31a99SMatthias Ringwald             // Configure up channel 1, options:
1445fa31a99SMatthias Ringwald             // - SEGGER_RTT_MODE_NO_BLOCK_SKIP
1455fa31a99SMatthias Ringwald             // - SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
1465fa31a99SMatthias Ringwald             // Note: with SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL, firwmware will hang if RTT not supported/debug probe not connected
1475fa31a99SMatthias Ringwald             // Note: with SEGGER_RTT_MODE_NO_BLOCK_SKIP, there's a chance for log file corruption if second write (packet) is skipped
1485fa31a99SMatthias Ringwald             SEGGER_RTT_ConfigUpBuffer(1, "hci_dump", &channel1_out[0], sizeof(channel1_out), SEGGER_RTT_MODE_NO_BLOCK_SKIP) ;;
1495fa31a99SMatthias Ringwald             break;
1505fa31a99SMatthias Ringwald         default:
1515fa31a99SMatthias Ringwald             break;
1525fa31a99SMatthias Ringwald     }
1535fa31a99SMatthias Ringwald #endif
154d0662982SMatthias Ringwald 
155eb443d3dSMatthias Ringwald     dump_file = 1;
15668e27c0fSmatthias.ringwald #endif
157d9659922Smatthias.ringwald }
15879662672Smatthias.ringwald 
159eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
1602992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets){
1612992c131Smatthias.ringwald     max_nr_packets = packets;
1622992c131Smatthias.ringwald }
1637c5f7483Smatthias.ringwald #endif
1642992c131Smatthias.ringwald 
1655fa31a99SMatthias 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){
1665fa31a99SMatthias Ringwald     big_endian_store_32( buffer, 0, PKTLOG_HDR_SIZE - 4 + len);
1675fa31a99SMatthias Ringwald     big_endian_store_32( buffer, 4, tv_sec);
1685fa31a99SMatthias Ringwald     big_endian_store_32( buffer, 8, tv_us);
1695fa31a99SMatthias Ringwald     uint8_t packet_logger_type = 0;
1705fa31a99SMatthias Ringwald     switch (packet_type){
1715fa31a99SMatthias Ringwald         case HCI_COMMAND_DATA_PACKET:
1725fa31a99SMatthias Ringwald             packet_logger_type = 0x00;
1735fa31a99SMatthias Ringwald             break;
1745fa31a99SMatthias Ringwald         case HCI_ACL_DATA_PACKET:
1755fa31a99SMatthias Ringwald             packet_logger_type = in ? 0x03 : 0x02;
1765fa31a99SMatthias Ringwald             break;
1775fa31a99SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
1785fa31a99SMatthias Ringwald             packet_logger_type = in ? 0x09 : 0x08;
1795fa31a99SMatthias Ringwald             break;
1805fa31a99SMatthias Ringwald         case HCI_EVENT_PACKET:
1815fa31a99SMatthias Ringwald             packet_logger_type = 0x01;
1825fa31a99SMatthias Ringwald             break;
1835fa31a99SMatthias Ringwald         case LOG_MESSAGE_PACKET:
1845fa31a99SMatthias Ringwald             packet_logger_type = 0xfc;
1855fa31a99SMatthias Ringwald             break;
1865fa31a99SMatthias Ringwald         default:
1875fa31a99SMatthias Ringwald             return;
1885fa31a99SMatthias Ringwald     }
1895fa31a99SMatthias Ringwald     buffer[12] = packet_logger_type;
1905fa31a99SMatthias Ringwald }
1915fa31a99SMatthias Ringwald 
1925fa31a99SMatthias 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){
1935fa31a99SMatthias Ringwald     little_endian_store_16( buffer, 0, 1 + len);
1945fa31a99SMatthias Ringwald     buffer[2] = in;
1955fa31a99SMatthias Ringwald     buffer[3] = 0;
1965fa31a99SMatthias Ringwald     little_endian_store_32( buffer, 4, tv_sec);
1975fa31a99SMatthias Ringwald     little_endian_store_32( buffer, 8, tv_us);
1985fa31a99SMatthias Ringwald     buffer[12] = packet_type;
1995fa31a99SMatthias Ringwald }
2005fa31a99SMatthias Ringwald 
2018a37b10aSMatthias Ringwald static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
202198a9e1bS[email protected]     switch (packet_type){
203198a9e1bS[email protected]         case HCI_COMMAND_DATA_PACKET:
204198a9e1bS[email protected]             printf("CMD => ");
205198a9e1bS[email protected]             break;
206198a9e1bS[email protected]         case HCI_EVENT_PACKET:
207198a9e1bS[email protected]             printf("EVT <= ");
208198a9e1bS[email protected]             break;
209198a9e1bS[email protected]         case HCI_ACL_DATA_PACKET:
210198a9e1bS[email protected]             if (in) {
211198a9e1bS[email protected]                 printf("ACL <= ");
212198a9e1bS[email protected]             } else {
213198a9e1bS[email protected]                 printf("ACL => ");
214198a9e1bS[email protected]             }
215198a9e1bS[email protected]             break;
2161e154302SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
2171e154302SMatthias Ringwald             if (in) {
2181e154302SMatthias Ringwald                 printf("SCO <= ");
2191e154302SMatthias Ringwald             } else {
2201e154302SMatthias Ringwald                 printf("SCO => ");
2211e154302SMatthias Ringwald             }
2221e154302SMatthias Ringwald             break;
223198a9e1bS[email protected]         case LOG_MESSAGE_PACKET:
224198a9e1bS[email protected]             printf("LOG -- %s\n", (char*) packet);
225198a9e1bS[email protected]             return;
226198a9e1bS[email protected]         default:
227198a9e1bS[email protected]             return;
228198a9e1bS[email protected]     }
229198a9e1bS[email protected]     printf_hexdump(packet, len);
230198a9e1bS[email protected] }
231198a9e1bS[email protected] 
232ad6274a7SMatthias Ringwald static void printf_timestamp(void){
2336401061aSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
2346401061aSMatthias Ringwald     struct tm* ptm;
2356401061aSMatthias Ringwald     struct timeval curr_time;
2366401061aSMatthias Ringwald     gettimeofday(&curr_time, NULL);
2376401061aSMatthias Ringwald     time_t curr_time_secs = curr_time.tv_sec;
2386401061aSMatthias Ringwald     /* Obtain the time of day, and convert it to a tm struct. */
2396401061aSMatthias Ringwald     ptm = localtime (&curr_time_secs);
2406401061aSMatthias Ringwald     /* assert localtime was successful */
2416401061aSMatthias Ringwald     if (!ptm) return;
2426401061aSMatthias Ringwald     /* Format the date and time, down to a single second. */
2436401061aSMatthias Ringwald     strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
2446401061aSMatthias Ringwald     /* Compute milliseconds from microseconds. */
2456401061aSMatthias Ringwald     uint16_t milliseconds = curr_time.tv_usec / 1000;
2466401061aSMatthias Ringwald     /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */
2476401061aSMatthias Ringwald     printf ("%s.%03u] ", time_string, milliseconds);
2486401061aSMatthias Ringwald #else
249ad6274a7SMatthias Ringwald     uint32_t time_ms = btstack_run_loop_get_time_ms();
250ad6274a7SMatthias Ringwald     int      seconds = time_ms / 1000;
251ad6274a7SMatthias Ringwald     int      minutes = seconds / 60;
252f04a0c31SMatthias Ringwald     unsigned int hours = minutes / 60;
253ad6274a7SMatthias Ringwald 
254f04a0c31SMatthias Ringwald     uint16_t p_ms      = time_ms - (seconds * 1000);
255f04a0c31SMatthias Ringwald     uint16_t p_seconds = seconds - (minutes * 60);
256f04a0c31SMatthias Ringwald     uint16_t p_minutes = minutes - (hours   * 60);
257ad6274a7SMatthias Ringwald     printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
258ad6274a7SMatthias Ringwald #endif
2596401061aSMatthias Ringwald }
260ad6274a7SMatthias Ringwald 
26179662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
26268e27c0fSmatthias.ringwald 
2638b658ebcSmatthias.ringwald     if (dump_file < 0) return; // not activated yet
2648b658ebcSmatthias.ringwald 
265eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
2662992c131Smatthias.ringwald     // don't grow bigger than max_nr_packets
2672992c131Smatthias.ringwald     if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
2682992c131Smatthias.ringwald         if (nr_packets >= max_nr_packets){
2692992c131Smatthias.ringwald             lseek(dump_file, 0, SEEK_SET);
270acb15818SMatthias Ringwald             // avoid -Wunused-result
271acb15818SMatthias Ringwald             int res = ftruncate(dump_file, 0);
272acb15818SMatthias Ringwald             UNUSED(res);
2732992c131Smatthias.ringwald             nr_packets = 0;
2742992c131Smatthias.ringwald         }
2752992c131Smatthias.ringwald         nr_packets++;
2762992c131Smatthias.ringwald     }
2775fa31a99SMatthias Ringwald #endif
2782992c131Smatthias.ringwald 
2795fa31a99SMatthias Ringwald     if (dump_format == HCI_DUMP_STDOUT){
2806401061aSMatthias Ringwald         printf_timestamp();
281198a9e1bS[email protected]         printf_packet(packet_type, in, packet, len);
2825fa31a99SMatthias Ringwald         return;
283a9cf4d77S[email protected]     }
2840d79c710Smatthias.ringwald 
2855fa31a99SMatthias Ringwald     uint32_t tv_sec = 0;
2865fa31a99SMatthias Ringwald     uint32_t tv_us  = 0;
2875fa31a99SMatthias Ringwald 
2885fa31a99SMatthias Ringwald     // get time
2895fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
2905fa31a99SMatthias Ringwald     struct timeval curr_time;
2915fa31a99SMatthias Ringwald     gettimeofday(&curr_time, NULL);
2925fa31a99SMatthias Ringwald     tv_sec = curr_time.tv_sec;
2935fa31a99SMatthias Ringwald     tv_us  = curr_time.tv_usec;
2945fa31a99SMatthias Ringwald #else
2955fa31a99SMatthias Ringwald     uint32_t time_ms = btstack_run_loop_get_time_ms();
2965fa31a99SMatthias Ringwald     tv_us   = time_ms * 1000;
2975fa31a99SMatthias Ringwald     tv_sec  = 946728000UL + (time_ms / 1000);
2985fa31a99SMatthias Ringwald #endif
2995fa31a99SMatthias Ringwald 
3005fa31a99SMatthias Ringwald     uint16_t header_len = 0;
3015fa31a99SMatthias Ringwald     switch (dump_format){
3028b658ebcSmatthias.ringwald         case HCI_DUMP_BLUEZ:
3035fa31a99SMatthias Ringwald             hci_dump_bluez_setup_header(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
3045fa31a99SMatthias Ringwald             header_len = HCIDUMP_HDR_SIZE;
3058b658ebcSmatthias.ringwald             break;
3068b658ebcSmatthias.ringwald         case HCI_DUMP_PACKETLOGGER:
3075fa31a99SMatthias Ringwald             hci_dump_packetlogger_setup_header(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
3085fa31a99SMatthias Ringwald             header_len = PKTLOG_HDR_SIZE;
3090d79c710Smatthias.ringwald             break;
3108b658ebcSmatthias.ringwald         default:
3118b658ebcSmatthias.ringwald             return;
3128b658ebcSmatthias.ringwald     }
3135fa31a99SMatthias Ringwald 
3145fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
3155fa31a99SMatthias Ringwald     // avoid -Wunused-result
3165fa31a99SMatthias Ringwald     int res = 0;
3175fa31a99SMatthias Ringwald     res = write (dump_file, &header, header_len);
318acb15818SMatthias Ringwald     res = write (dump_file, packet, len );
319acb15818SMatthias Ringwald     UNUSED(res);
32068e27c0fSmatthias.ringwald #endif
3215fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
3225fa31a99SMatthias Ringwald     SEGGER_RTT_Write(1, &header, header_len);
3235fa31a99SMatthias Ringwald     SEGGER_RTT_Write(1, packet, len);
3245fa31a99SMatthias Ringwald #endif
3255fa31a99SMatthias Ringwald     UNUSED(header_len);
32679662672Smatthias.ringwald }
32779662672Smatthias.ringwald 
3288a37b10aSMatthias Ringwald static int hci_dump_log_level_active(int log_level){
329fa087deaSMatthias Ringwald     if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return 0;
330fa087deaSMatthias Ringwald     if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return 0;
3318a37b10aSMatthias Ringwald     return log_level_enabled[log_level];
3328a37b10aSMatthias Ringwald }
3338a37b10aSMatthias Ringwald 
33494be1a66SMatthias Ringwald void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){
3356401061aSMatthias Ringwald     if (!hci_dump_log_level_active(log_level)) return;
3366401061aSMatthias Ringwald 
337*1fb07123SMatthias Ringwald #if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT)
3386401061aSMatthias Ringwald     if (dump_file >= 0){
339eb443d3dSMatthias Ringwald         int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
340eb443d3dSMatthias Ringwald         hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
341b8ae70b4SMatthias Ringwald         return;
3426401061aSMatthias Ringwald     }
3436401061aSMatthias Ringwald #endif
3446401061aSMatthias Ringwald 
345ad6274a7SMatthias Ringwald     printf_timestamp();
3461df3b679S[email protected]     printf("LOG -- ");
3471df3b679S[email protected]     vprintf(format, argptr);
3481fd51e45S[email protected]     printf("\n");
34994be1a66SMatthias Ringwald }
35094be1a66SMatthias Ringwald 
35194be1a66SMatthias Ringwald void hci_dump_log(int log_level, const char * format, ...){
35294be1a66SMatthias Ringwald     va_list argptr;
35394be1a66SMatthias Ringwald     va_start(argptr, format);
35494be1a66SMatthias Ringwald     hci_dump_log_va_arg(log_level, format, argptr);
3551df3b679S[email protected]     va_end(argptr);
356a1d7dd1fSmatthias.ringwald }
357a1d7dd1fSmatthias.ringwald 
35820ea11b9S[email protected] #ifdef __AVR__
3598a37b10aSMatthias Ringwald void hci_dump_log_P(int log_level, PGM_P format, ...){
3608a37b10aSMatthias Ringwald     if (!hci_dump_log_level_active(log_level)) return;
36120ea11b9S[email protected]     va_list argptr;
36220ea11b9S[email protected]     va_start(argptr, format);
36320ea11b9S[email protected]     printf_P(PSTR("LOG -- "));
36420ea11b9S[email protected]     vfprintf_P(stdout, format, argptr);
36520ea11b9S[email protected]     printf_P(PSTR("\n"));
36620ea11b9S[email protected]     va_end(argptr);
36720ea11b9S[email protected] }
36820ea11b9S[email protected] #endif
36920ea11b9S[email protected] 
37071de195eSMatthias Ringwald void hci_dump_close(void){
371eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO
37279662672Smatthias.ringwald     close(dump_file);
37368e27c0fSmatthias.ringwald #endif
374eb443d3dSMatthias Ringwald     dump_file = -1;
37579662672Smatthias.ringwald }
37679662672Smatthias.ringwald 
3778a37b10aSMatthias Ringwald void hci_dump_enable_log_level(int log_level, int enable){
378fa087deaSMatthias Ringwald     if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return;
379fa087deaSMatthias Ringwald     if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return;
3808a37b10aSMatthias Ringwald     log_level_enabled[log_level] = enable;
3818a37b10aSMatthias Ringwald }
3828a37b10aSMatthias Ringwald 
383