xref: /btstack/src/hci_dump.c (revision 128d6c999a4a7cc321497ddbaddd9cd6c8d1edef)
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 based on platform-specific implementation
44  */
45 
46 #include "btstack_config.h"
47 #include "btstack_debug.h"
48 #include "btstack_bool.h"
49 #include "btstack_util.h"
50 
51 static const hci_dump_t * hci_dump_impl;
52 static int  max_nr_packets;
53 static int  nr_packets;
54 
55 // levels: debug, info, error
56 static bool log_level_enabled[3] = { 1, 1, 1};
57 
58 static bool hci_dump_log_level_active(int log_level){
59     if (hci_dump_impl == NULL) return false;
60     if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return false;
61     if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return false;
62     return log_level_enabled[log_level];
63 }
64 
65 void hci_dump_init(const hci_dump_t * impl){
66     max_nr_packets = -1;
67     nr_packets = 0;
68     hci_dump_impl = impl;
69 }
70 
71 void hci_dump_set_max_packets(int packets){
72     max_nr_packets = packets;
73 }
74 
75 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
76     if (hci_dump_impl == NULL) return;
77     if (max_nr_packets > 0){
78         if ((nr_packets >= max_nr_packets) && (hci_dump_impl->reset != NULL)) {
79             nr_packets = 0;
80             (*hci_dump_impl->reset)();
81         }
82         nr_packets++;
83     }
84     (*hci_dump_impl->log_packet)(packet_type, in, packet, len);
85 }
86 
87 void hci_dump_log(int log_level, const char * format, ...){
88     if (!hci_dump_log_level_active(log_level)) return;
89 
90     va_list argptr;
91     va_start(argptr, format);
92     (*hci_dump_impl->log_message)(format, argptr);
93     va_end(argptr);
94 }
95 
96 #ifdef __AVR__
97 void hci_dump_log_P(int log_level, PGM_P format, ...){
98     if (!hci_dump_log_level_active(log_level)) return;
99 
100     va_list argptr;
101     va_start(argptr, format);
102     (*hci_dump_impl->log_packet_P)(format, argptr);
103     va_end(argptr);
104 }
105 #endif
106 
107 void hci_dump_enable_log_level(int log_level, int enable){
108     if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return;
109     if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return;
110     log_level_enabled[log_level] = enable != 0;
111 }
112 
113 void hci_dump_setup_header_packetlogger(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
114     big_endian_store_32( buffer, 0, HCI_DUMP_HEADER_SIZE_PACKETLOGGER - 4 + len);
115     big_endian_store_32( buffer, 4, tv_sec);
116     big_endian_store_32( buffer, 8, tv_us);
117     uint8_t packet_logger_type = 0;
118     switch (packet_type){
119         case HCI_COMMAND_DATA_PACKET:
120             packet_logger_type = 0x00;
121             break;
122         case HCI_ACL_DATA_PACKET:
123             packet_logger_type = in ? 0x03 : 0x02;
124             break;
125         case HCI_SCO_DATA_PACKET:
126             packet_logger_type = in ? 0x09 : 0x08;
127             break;
128         case HCI_EVENT_PACKET:
129             packet_logger_type = 0x01;
130             break;
131         case LOG_MESSAGE_PACKET:
132             packet_logger_type = 0xfc;
133             break;
134         default:
135             return;
136     }
137     buffer[12] = packet_logger_type;
138 }
139 
140 void hci_dump_setup_header_bluez(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){
141     little_endian_store_16( buffer, 0u, 1u + len);
142     buffer[2] = in;
143     buffer[3] = 0;
144     little_endian_store_32( buffer, 4, tv_sec);
145     little_endian_store_32( buffer, 8, tv_us);
146     buffer[12] = packet_type;
147 }
148