xref: /btstack/src/hci_dump.h (revision be78df3b6c7394390fa6d2bc04f991dd7ac2de2c)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title HCI Logging
4079662672Smatthias.ringwald  *
41fe5a6c4eSMilanka Ringwald  * Dump HCI trace as BlueZ's hcidump format, Apple's PacketLogger, or stdout.
4279662672Smatthias.ringwald  *
4379662672Smatthias.ringwald  */
4479662672Smatthias.ringwald 
4580e33422SMatthias Ringwald #ifndef HCI_DUMP_H
4680e33422SMatthias Ringwald #define HCI_DUMP_H
47a1d7dd1fSmatthias.ringwald 
4879662672Smatthias.ringwald #include <stdint.h>
4994be1a66SMatthias Ringwald #include <stdarg.h>       // for va_list
50cdc66b5eSMatthias Ringwald #include "btstack_bool.h"
5179662672Smatthias.ringwald 
5220ea11b9S[email protected] #ifdef __AVR__
53f61ec823S[email protected] #include <avr/pgmspace.h>
5420ea11b9S[email protected] #endif
5520ea11b9S[email protected] 
56eed533f6S[email protected] #if defined __cplusplus
57eed533f6S[email protected] extern "C" {
58eed533f6S[email protected] #endif
59eed533f6S[email protected] 
60fa087deaSMatthias Ringwald #define HCI_DUMP_LOG_LEVEL_DEBUG 0
61fa087deaSMatthias Ringwald #define HCI_DUMP_LOG_LEVEL_INFO  1
62fa087deaSMatthias Ringwald #define HCI_DUMP_LOG_LEVEL_ERROR 2
638a37b10aSMatthias Ringwald 
64128d6c99SMatthias Ringwald #define HCI_DUMP_HEADER_SIZE_PACKETLOGGER 13
65128d6c99SMatthias Ringwald #define HCI_DUMP_HEADER_SIZE_BLUEZ        13
66a9b0ec79SMatthias Ringwald #define HCI_DUMP_HEADER_SIZE_BTSNOOP      24
67128d6c99SMatthias Ringwald 
68eb0d5579SMatthias Ringwald // we expect that there's no log_x call that creates a longer message string without the time header
69eb0d5579SMatthias Ringwald #define HCI_DUMP_MAX_MESSAGE_LEN        256
70eb0d5579SMatthias Ringwald 
7199e736a0SMilanka Ringwald /* API_START */
7299e736a0SMilanka Ringwald 
738b658ebcSmatthias.ringwald typedef enum {
74128d6c99SMatthias Ringwald     HCI_DUMP_INVALID = 0,
75128d6c99SMatthias Ringwald     HCI_DUMP_BLUEZ,
768adf0ddaSmatthias.ringwald     HCI_DUMP_PACKETLOGGER,
77a9b0ec79SMatthias Ringwald     HCI_DUMP_BTSNOOP,
788b658ebcSmatthias.ringwald } hci_dump_format_t;
7979662672Smatthias.ringwald 
80128d6c99SMatthias Ringwald typedef struct {
81128d6c99SMatthias Ringwald     // reset output, called if max packets is reached, to limit file size
82128d6c99SMatthias Ringwald     void (*reset)(void);
83128d6c99SMatthias Ringwald     // log packet
84128d6c99SMatthias Ringwald     void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
85128d6c99SMatthias Ringwald     // log message
862d17d4c0SMatthias Ringwald     void (*log_message)(int log_level, const char * format, va_list argptr);
87128d6c99SMatthias Ringwald #ifdef __AVR__ \
88128d6c99SMatthias Ringwald     // log message - AVR
892d17d4c0SMatthias Ringwald     void (*log_message_P)(int log_level, PGM_P * format, va_list argptr);
90128d6c99SMatthias Ringwald #endif
91128d6c99SMatthias Ringwald } hci_dump_t;
9299e736a0SMilanka Ringwald 
93cdc66b5eSMatthias Ringwald /**
94128d6c99SMatthias Ringwald  * @brief Init HCI Dump
95128d6c99SMatthias Ringwald  * @param hci_dump_impl - platform-specific implementation
96128d6c99SMatthias Ringwald  */
97128d6c99SMatthias Ringwald void hci_dump_init(const hci_dump_t * hci_dump_impl);
98128d6c99SMatthias Ringwald 
99cdc66b5eSMatthias Ringwald /**
100cdc66b5eSMatthias Ringwald  * @brief Enable packet logging
101cdc66b5eSMatthias Ringwald  * @param enabled default: true
102cdc66b5eSMatthias Ringwald  */
103cdc66b5eSMatthias Ringwald void hci_dump_enable_packet_log(bool enabled);
104cdc66b5eSMatthias Ringwald 
105cdc66b5eSMatthias Ringwald /**
106cdc66b5eSMatthias Ringwald  * @brief
107cdc66b5eSMatthias Ringwald  */
108cdc66b5eSMatthias Ringwald void hci_dump_enable_log_level(int log_level, int enable);
109cdc66b5eSMatthias Ringwald 
110128d6c99SMatthias Ringwald /*
111128d6c99SMatthias Ringwald  * @brief Set max number of packets - output file might be truncated
11299e736a0SMilanka Ringwald  */
1132992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets); // -1 for unlimited
11499e736a0SMilanka Ringwald 
115cdc66b5eSMatthias Ringwald /**
116128d6c99SMatthias Ringwald  * @brief Dump Packet
117128d6c99SMatthias Ringwald  * @param packet_type
118128d6c99SMatthias Ringwald  * @param in is 1 for incoming, 0 for outoing
119128d6c99SMatthias Ringwald  * @param packet
120128d6c99SMatthias Ringwald  * @param len
12199e736a0SMilanka Ringwald  */
12279662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
12399e736a0SMilanka Ringwald 
124cdc66b5eSMatthias Ringwald /**
125128d6c99SMatthias Ringwald  * @brief Dump Message
126128d6c99SMatthias Ringwald  * @param log_level
127128d6c99SMatthias Ringwald  * @param format
12899e736a0SMilanka Ringwald  */
12965280ee1SMatthias Ringwald void hci_dump_log(int log_level, const char * format, ...)
13065280ee1SMatthias Ringwald #ifdef __GNUC__
131c006cd19SMilanka Ringwald __attribute__ ((format (__printf__, 2, 3)))
13265280ee1SMatthias Ringwald #endif
13365280ee1SMatthias Ringwald ;
13499e736a0SMilanka Ringwald 
135128d6c99SMatthias Ringwald #ifdef __AVR__
136128d6c99SMatthias Ringwald /*
137128d6c99SMatthias Ringwald  * @brief Dump Message using format string stored in PGM memory (allows to save RAM)
138128d6c99SMatthias Ringwald  * @param log_level
139128d6c99SMatthias Ringwald  * @param format
140128d6c99SMatthias Ringwald  */
141560d2a7aSMatthias Ringwald void hci_dump_log_P(int log_level, PGM_P format, ...)
142128d6c99SMatthias Ringwald #ifdef __GNUC__
143128d6c99SMatthias Ringwald __attribute__ ((format (__printf__, 2, 3)))
144128d6c99SMatthias Ringwald #endif
145560d2a7aSMatthias Ringwald ;
146128d6c99SMatthias Ringwald #endif
147128d6c99SMatthias Ringwald 
148128d6c99SMatthias Ringwald /**
149*be78df3bSMatthias Ringwald  * @brief Dump internal BTstack event
150*be78df3bSMatthias Ringwald  * @note only logged if ENABLE_LOG_BTSTACK_EVENTS is defined
151*be78df3bSMatthias Ringwald  * @param packet
152*be78df3bSMatthias Ringwald  * @param len
153*be78df3bSMatthias Ringwald  */
154*be78df3bSMatthias Ringwald void hci_dump_btstack_event(const uint8_t *packet, uint16_t len);
155*be78df3bSMatthias Ringwald 
156*be78df3bSMatthias Ringwald /**
157128d6c99SMatthias Ringwald  * @brief Setup header for PacketLogger format
158128d6c99SMatthias Ringwald  * @param buffer
159128d6c99SMatthias Ringwald  * @param tv_sec
160128d6c99SMatthias Ringwald  * @param tv_us
161128d6c99SMatthias Ringwald  * @param packet_type
162128d6c99SMatthias Ringwald  * @param in
163128d6c99SMatthias Ringwald  * @param len
164128d6c99SMatthias Ringwald  */
165128d6c99SMatthias Ringwald 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);
166a9b0ec79SMatthias Ringwald 
167128d6c99SMatthias Ringwald /**
168128d6c99SMatthias Ringwald  * @brief Setup header for BLUEZ (hcidump) format
169128d6c99SMatthias Ringwald  * @param buffer
170128d6c99SMatthias Ringwald  * @param tv_sec
171128d6c99SMatthias Ringwald  * @param tv_us
172128d6c99SMatthias Ringwald  * @param packet_type
173128d6c99SMatthias Ringwald  * @param in
174128d6c99SMatthias Ringwald  * @param len
175128d6c99SMatthias Ringwald  */
176128d6c99SMatthias Ringwald 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);
177128d6c99SMatthias Ringwald 
178a9b0ec79SMatthias Ringwald /**
179a9b0ec79SMatthias Ringwald  * @brief Setup header for BT Snoop format
180a9b0ec79SMatthias Ringwald  * @param buffer
181a9b0ec79SMatthias Ringwald  * @param ts_usec_high upper 32-bit of 64-bit microsecond timestamp
182a9b0ec79SMatthias Ringwald  * @param ts_usec_low  lower 2-bit of 64-bit microsecond timestamp
183a9b0ec79SMatthias Ringwald  * @param cumulative_drops since last packet was recorded
184a9b0ec79SMatthias Ringwald  * @param packet_type
185a9b0ec79SMatthias Ringwald  * @param in
186a9b0ec79SMatthias Ringwald  * @param len
187a9b0ec79SMatthias Ringwald  */
188a9b0ec79SMatthias Ringwald 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);
189a9b0ec79SMatthias Ringwald 
19099e736a0SMilanka Ringwald /* API_END */
19199e736a0SMilanka Ringwald 
19220ea11b9S[email protected] 
193eed533f6S[email protected] #if defined __cplusplus
194eed533f6S[email protected] }
195eed533f6S[email protected] #endif
19680e33422SMatthias Ringwald #endif // HCI_DUMP_H
197