xref: /btstack/src/hci_dump.h (revision 29502c9b6278f5ef85634162beef9b6a43c78688)
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 BLUEKITCHEN
24  * GMBH 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 /**
39  * @title HCI Logging
40  *
41  * Dump HCI trace as BlueZ's hcidump format, Apple's PacketLogger, or stdout.
42  *
43  */
44 
45 #ifndef HCI_DUMP_H
46 #define HCI_DUMP_H
47 
48 #include <stdint.h>
49 #include <stdarg.h>       // for va_list
50 #include "btstack_bool.h"
51 
52 #ifdef __AVR__
53 #include <avr/pgmspace.h>
54 #endif
55 
56 #if defined __cplusplus
57 extern "C" {
58 #endif
59 
60 #define HCI_DUMP_LOG_LEVEL_DEBUG 0
61 #define HCI_DUMP_LOG_LEVEL_INFO  1
62 #define HCI_DUMP_LOG_LEVEL_ERROR 2
63 
64 #define HCI_DUMP_HEADER_SIZE_PACKETLOGGER 13
65 #define HCI_DUMP_HEADER_SIZE_BLUEZ        13
66 #define HCI_DUMP_HEADER_SIZE_BTSNOOP      24
67 
68 // we expect that there's no log_x call that creates a longer message string without the time header
69 #define HCI_DUMP_MAX_MESSAGE_LEN        256
70 
71 /* API_START */
72 
73 typedef enum {
74     HCI_DUMP_INVALID = 0,
75     HCI_DUMP_BLUEZ,
76     HCI_DUMP_PACKETLOGGER,
77     HCI_DUMP_BTSNOOP,
78 } hci_dump_format_t;
79 
80 typedef struct {
81     // reset output, called if max packets is reached, to limit file size
82     void (*reset)(void);
83     // log packet
84     void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
85     // log message
86     void (*log_message)(int log_level, const char * format, va_list argptr);
87 #ifdef __AVR__ \
88     // log message - AVR
89     void (*log_message_P)(int log_level, PGM_P * format, va_list argptr);
90 #endif
91 } hci_dump_t;
92 
93 /**
94  * @brief Init HCI Dump
95  * @param hci_dump_impl - platform-specific implementation
96  */
97 void hci_dump_init(const hci_dump_t * hci_dump_impl);
98 
99 /**
100  * @brief Enable packet logging
101  * @param enabled default: true
102  */
103 void hci_dump_enable_packet_log(bool enabled);
104 
105 /**
106  * @brief
107  */
108 void hci_dump_enable_log_level(int log_level, int enable);
109 
110 /*
111  * @brief Set max number of packets - output file might be truncated
112  */
113 void hci_dump_set_max_packets(int packets); // -1 for unlimited
114 
115 /**
116  * @brief Dump Packet
117  * @param packet_type
118  * @param in is 1 for incoming, 0 for outoing
119  * @param packet
120  * @param len
121  */
122 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
123 
124 /**
125  * @brief Dump Message
126  * @param log_level
127  * @param format
128  */
129 void hci_dump_log(int log_level, const char * format, ...)
130 #ifdef __GNUC__
131 __attribute__ ((format (__printf__, 2, 3)))
132 #endif
133 ;
134 
135 #ifdef __AVR__
136 /*
137  * @brief Dump Message using format string stored in PGM memory (allows to save RAM)
138  * @param log_level
139  * @param format
140  */
141 void hci_dump_log_P(int log_level, PGM_P format, ...)
142 #ifdef __GNUC__
143 __attribute__ ((format (__printf__, 2, 3)))
144 #endif
145 ;
146 #endif
147 
148 /**
149  * @brief Dump internal BTstack event
150  * @note only logged if ENABLE_LOG_BTSTACK_EVENTS is defined
151  * @param packet
152  * @param len
153  */
154 void hci_dump_btstack_event(const uint8_t *packet, uint16_t len);
155 
156 /**
157  * @brief Setup header for PacketLogger format
158  * @param buffer
159  * @param tv_sec
160  * @param tv_us
161  * @param packet_type
162  * @param in
163  * @param len
164  */
165 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);
166 
167 /**
168  * @brief Setup header for BLUEZ (hcidump) format
169  * @param buffer
170  * @param tv_sec
171  * @param tv_us
172  * @param packet_type
173  * @param in
174  * @param len
175  */
176 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);
177 
178 /**
179  * @brief Setup header for BT Snoop format
180  * @param buffer
181  * @param ts_usec_high upper 32-bit of 64-bit microsecond timestamp
182  * @param ts_usec_low  lower 2-bit of 64-bit microsecond timestamp
183  * @param cumulative_drops since last packet was recorded
184  * @param packet_type
185  * @param in
186  * @param len
187  */
188 void hci_dump_setup_header_btsnoop(uint8_t * buffer, uint32_t ts_usec_high, uint32_t ts_usec_low, uint32_t cumulative_drops, uint8_t packet_type, uint8_t in, uint16_t len);
189 
190 /* API_END */
191 
192 
193 #if defined __cplusplus
194 }
195 #endif
196 #endif // HCI_DUMP_H
197