xref: /btstack/src/hci_dump.h (revision a9b0ec79f4a5165e5a364fba87b9b4b8535ce69b)
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 /**
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 /* API_START */
69 
70 typedef enum {
71     HCI_DUMP_INVALID = 0,
72     HCI_DUMP_BLUEZ,
73     HCI_DUMP_PACKETLOGGER,
74     HCI_DUMP_BTSNOOP,
75 } hci_dump_format_t;
76 
77 typedef struct {
78     // reset output, called if max packets is reached, to limit file size
79     void (*reset)(void);
80     // log packet
81     void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
82     // log message
83     void (*log_message)(const char * format, va_list argptr);
84 #ifdef __AVR__ \
85     // log message - AVR
86     void (*log_message_P)(PGM_P * format, va_list argptr);
87 #endif
88 } hci_dump_t;
89 
90 /**
91  * @brief Init HCI Dump
92  * @param hci_dump_impl - platform-specific implementation
93  */
94 void hci_dump_init(const hci_dump_t * hci_dump_impl);
95 
96 /**
97  * @brief Enable packet logging
98  * @param enabled default: true
99  */
100 void hci_dump_enable_packet_log(bool enabled);
101 
102 /**
103  * @brief
104  */
105 void hci_dump_enable_log_level(int log_level, int enable);
106 
107 /*
108  * @brief Set max number of packets - output file might be truncated
109  */
110 void hci_dump_set_max_packets(int packets); // -1 for unlimited
111 
112 /**
113  * @brief Dump Packet
114  * @param packet_type
115  * @param in is 1 for incoming, 0 for outoing
116  * @param packet
117  * @param len
118  */
119 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
120 
121 /**
122  * @brief Dump Message
123  * @param log_level
124  * @param format
125  */
126 void hci_dump_log(int log_level, const char * format, ...)
127 #ifdef __GNUC__
128 __attribute__ ((format (__printf__, 2, 3)))
129 #endif
130 ;
131 
132 #ifdef __AVR__
133 /*
134  * @brief Dump Message using format string stored in PGM memory (allows to save RAM)
135  * @param log_level
136  * @param format
137  */
138 void hci_dump_log_P(int log_level, PGM_P format, ...)
139 #ifdef __GNUC__
140 __attribute__ ((format (__printf__, 2, 3)))
141 #endif
142 ;
143 #endif
144 
145 /**
146  * @brief Setup header for PacketLogger format
147  * @param buffer
148  * @param tv_sec
149  * @param tv_us
150  * @param packet_type
151  * @param in
152  * @param len
153  */
154 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);
155 
156 /**
157  * @brief Setup header for BLUEZ (hcidump) 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_bluez(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 BT Snoop format
169  * @param buffer
170  * @param ts_usec_high upper 32-bit of 64-bit microsecond timestamp
171  * @param ts_usec_low  lower 2-bit of 64-bit microsecond timestamp
172  * @param cumulative_drops since last packet was recorded
173  * @param packet_type
174  * @param in
175  * @param len
176  */
177 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);
178 
179 /* API_END */
180 
181 
182 #if defined __cplusplus
183 }
184 #endif
185 #endif // HCI_DUMP_H
186