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 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)(int log_level, 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 Set max number of packets - output file might be truncated 96 */ 97 void hci_dump_set_max_packets(int packets); // -1 for unlimited 98 99 /* 100 * @brief Dump Packet 101 * @param packet_type 102 * @param in is 1 for incoming, 0 for outoing 103 * @param packet 104 * @param len 105 */ 106 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 107 108 /* 109 * @brief Dump Message 110 * @param log_level 111 * @param format 112 */ 113 void hci_dump_log(int log_level, const char * format, ...) 114 #ifdef __GNUC__ 115 __attribute__ ((format (__printf__, 2, 3))) 116 #endif 117 ; 118 119 #ifdef __AVR__ 120 /* 121 * @brief Dump Message using format string stored in PGM memory (allows to save RAM) 122 * @param log_level 123 * @param format 124 */ 125 void hci_dump_log_P(int log_level, PGM_P format, ...); 126 #ifdef __GNUC__ 127 __attribute__ ((format (__printf__, 2, 3))) 128 #endif 129 #endif 130 131 /** 132 * @brief Setup header for PacketLogger format 133 * @param buffer 134 * @param tv_sec 135 * @param tv_us 136 * @param packet_type 137 * @param in 138 * @param len 139 */ 140 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); 141 /** 142 * @brief Setup header for BLUEZ (hcidump) format 143 * @param buffer 144 * @param tv_sec 145 * @param tv_us 146 * @param packet_type 147 * @param in 148 * @param len 149 */ 150 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); 151 152 /* 153 * @brief 154 */ 155 void hci_dump_enable_log_level(int log_level, int enable); 156 157 /* API_END */ 158 159 160 #if defined __cplusplus 161 } 162 #endif 163 #endif // HCI_DUMP_H 164