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