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 * hci_dump.h 40 * 41 * Dump HCI trace as BlueZ's hcidump format, Apple's PacketLogger, or stdout 42 * 43 * Created by Matthias Ringwald on 5/26/09. 44 */ 45 46 #ifndef HCI_DUMP_H 47 #define HCI_DUMP_H 48 49 #include <stdint.h> 50 #include <stdarg.h> // for va_list 51 #include "btstack_bool.h" 52 53 #ifdef __AVR__ 54 #include <avr/pgmspace.h> 55 #endif 56 57 #if defined __cplusplus 58 extern "C" { 59 #endif 60 61 #define HCI_DUMP_LOG_LEVEL_DEBUG 0 62 #define HCI_DUMP_LOG_LEVEL_INFO 1 63 #define HCI_DUMP_LOG_LEVEL_ERROR 2 64 65 #define HCI_DUMP_HEADER_SIZE_PACKETLOGGER 13 66 #define HCI_DUMP_HEADER_SIZE_BLUEZ 13 67 68 /* API_START */ 69 70 typedef enum { 71 HCI_DUMP_INVALID = 0, 72 HCI_DUMP_BLUEZ, 73 HCI_DUMP_PACKETLOGGER, 74 } hci_dump_format_t; 75 76 typedef struct { 77 // reset output, called if max packets is reached, to limit file size 78 void (*reset)(void); 79 // log packet 80 void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 81 // log message 82 void (*log_message)(const char * format, va_list argptr); 83 #ifdef __AVR__ \ 84 // log message - AVR 85 void (*log_message_P)(int log_level, PGM_P * format, va_list argptr); 86 #endif 87 } hci_dump_t; 88 89 /** 90 * @brief Init HCI Dump 91 * @param hci_dump_impl - platform-specific implementation 92 */ 93 void hci_dump_init(const hci_dump_t * hci_dump_impl); 94 95 /** 96 * @brief Enable packet logging 97 * @param enabled default: true 98 */ 99 void hci_dump_enable_packet_log(bool enabled); 100 101 /** 102 * @brief 103 */ 104 void hci_dump_enable_log_level(int log_level, int enable); 105 106 /* 107 * @brief Set max number of packets - output file might be truncated 108 */ 109 void hci_dump_set_max_packets(int packets); // -1 for unlimited 110 111 /** 112 * @brief Dump Packet 113 * @param packet_type 114 * @param in is 1 for incoming, 0 for outoing 115 * @param packet 116 * @param len 117 */ 118 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 119 120 /** 121 * @brief Dump Message 122 * @param log_level 123 * @param format 124 */ 125 void hci_dump_log(int log_level, const char * format, ...) 126 #ifdef __GNUC__ 127 __attribute__ ((format (__printf__, 2, 3))) 128 #endif 129 ; 130 131 #ifdef __AVR__ 132 /* 133 * @brief Dump Message using format string stored in PGM memory (allows to save RAM) 134 * @param log_level 135 * @param format 136 */ 137 void hci_dump_log_P(int log_level, PGM_P format, ...); 138 #ifdef __GNUC__ 139 __attribute__ ((format (__printf__, 2, 3))) 140 #endif 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