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.c 40 * 41 * Dump HCI trace in various formats: 42 * 43 * - BlueZ's hcidump format 44 * - Apple's PacketLogger 45 * - stdout hexdump 46 * 47 * Created by Matthias Ringwald on 5/26/09. 48 */ 49 50 #include "btstack_config.h" 51 52 #include "hci_dump.h" 53 #include "hci.h" 54 #include "hci_transport.h" 55 #include "hci_cmd.h" 56 #include "btstack_run_loop.h" 57 #include <stdio.h> 58 59 #ifdef HAVE_POSIX_FILE_IO 60 #include <fcntl.h> // open 61 #include <unistd.h> // write 62 #include <time.h> 63 #include <sys/time.h> // for timestamps 64 #include <sys/stat.h> // for mode flags 65 #include <stdarg.h> // for va_list 66 #endif 67 68 // BLUEZ hcidump - struct not used directly, but left here as documentation 69 typedef struct { 70 uint16_t len; 71 uint8_t in; 72 uint8_t pad; 73 uint32_t ts_sec; 74 uint32_t ts_usec; 75 uint8_t packet_type; 76 } 77 hcidump_hdr; 78 #define HCIDUMP_HDR_SIZE 13 79 80 // APPLE PacketLogger - struct not used directly, but left here as documentation 81 typedef struct { 82 uint32_t len; 83 uint32_t ts_sec; 84 uint32_t ts_usec; 85 uint8_t type; // 0xfc for note 86 } 87 pktlog_hdr; 88 #define PKTLOG_HDR_SIZE 13 89 90 static int dump_file = -1; 91 #ifdef HAVE_POSIX_FILE_IO 92 static int dump_format; 93 static uint8_t header_bluez[HCIDUMP_HDR_SIZE]; 94 static uint8_t header_packetlogger[PKTLOG_HDR_SIZE]; 95 static char time_string[40]; 96 static int max_nr_packets = -1; 97 static int nr_packets = 0; 98 static char log_message_buffer[256]; 99 #endif 100 101 // levels: debug, info, error 102 static int log_level_enabled[3] = { 1, 1, 1}; 103 104 void hci_dump_open(const char *filename, hci_dump_format_t format){ 105 #ifdef HAVE_POSIX_FILE_IO 106 dump_format = format; 107 if (dump_format == HCI_DUMP_STDOUT) { 108 dump_file = fileno(stdout); 109 } else { 110 111 # ifdef _WIN32 112 dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC); 113 # else 114 dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 115 # endif 116 117 } 118 #else 119 dump_file = 1; 120 #endif 121 } 122 123 #ifdef HAVE_POSIX_FILE_IO 124 void hci_dump_set_max_packets(int packets){ 125 max_nr_packets = packets; 126 } 127 #endif 128 129 static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 130 switch (packet_type){ 131 case HCI_COMMAND_DATA_PACKET: 132 printf("CMD => "); 133 break; 134 case HCI_EVENT_PACKET: 135 printf("EVT <= "); 136 break; 137 case HCI_ACL_DATA_PACKET: 138 if (in) { 139 printf("ACL <= "); 140 } else { 141 printf("ACL => "); 142 } 143 break; 144 case LOG_MESSAGE_PACKET: 145 printf("LOG -- %s\n", (char*) packet); 146 return; 147 default: 148 return; 149 } 150 printf_hexdump(packet, len); 151 } 152 153 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 154 155 if (dump_file < 0) return; // not activated yet 156 157 #ifdef HAVE_POSIX_FILE_IO 158 159 // don't grow bigger than max_nr_packets 160 if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){ 161 if (nr_packets >= max_nr_packets){ 162 lseek(dump_file, 0, SEEK_SET); 163 ftruncate(dump_file, 0); 164 nr_packets = 0; 165 } 166 nr_packets++; 167 } 168 169 // get time 170 struct timeval curr_time; 171 struct tm* ptm; 172 gettimeofday(&curr_time, NULL); 173 time_t curr_time_secs = curr_time.tv_sec; 174 175 switch (dump_format){ 176 case HCI_DUMP_STDOUT: { 177 /* Obtain the time of day, and convert it to a tm struct. */ 178 ptm = localtime (&curr_time_secs); 179 /* Format the date and time, down to a single second. */ 180 strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); 181 /* Compute milliseconds from microseconds. */ 182 uint16_t milliseconds = curr_time.tv_usec / 1000; 183 /* Print the formatted time, in seconds, followed by a decimal point 184 and the milliseconds. */ 185 printf ("%s.%03u] ", time_string, milliseconds); 186 printf_packet(packet_type, in, packet, len); 187 break; 188 } 189 190 case HCI_DUMP_BLUEZ: 191 little_endian_store_16( header_bluez, 0, 1 + len); 192 header_bluez[2] = in; 193 header_bluez[3] = 0; 194 little_endian_store_32( header_bluez, 4, curr_time.tv_sec); 195 little_endian_store_32( header_bluez, 8, curr_time.tv_usec); 196 header_bluez[12] = packet_type; 197 write (dump_file, header_bluez, HCIDUMP_HDR_SIZE); 198 write (dump_file, packet, len ); 199 break; 200 201 case HCI_DUMP_PACKETLOGGER: 202 big_endian_store_32( header_packetlogger, 0, PKTLOG_HDR_SIZE - 4 + len); 203 big_endian_store_32( header_packetlogger, 4, curr_time.tv_sec); 204 big_endian_store_32( header_packetlogger, 8, curr_time.tv_usec); 205 switch (packet_type){ 206 case HCI_COMMAND_DATA_PACKET: 207 header_packetlogger[12] = 0x00; 208 break; 209 case HCI_ACL_DATA_PACKET: 210 if (in) { 211 header_packetlogger[12] = 0x03; 212 } else { 213 header_packetlogger[12] = 0x02; 214 } 215 break; 216 case HCI_SCO_DATA_PACKET: 217 if (in) { 218 header_packetlogger[12] = 0x09; 219 } else { 220 header_packetlogger[12] = 0x08; 221 } 222 break; 223 case HCI_EVENT_PACKET: 224 header_packetlogger[12] = 0x01; 225 break; 226 case LOG_MESSAGE_PACKET: 227 header_packetlogger[12] = 0xfc; 228 break; 229 default: 230 return; 231 } 232 write (dump_file, &header_packetlogger, PKTLOG_HDR_SIZE); 233 write (dump_file, packet, len ); 234 break; 235 236 default: 237 break; 238 } 239 #else 240 241 // #ifdef HAVE_EMBEDDED_TICK 242 // uint32_t time_ms = btstack_run_loop_embedded_get_time_ms(); 243 // printf("[%06u] ", time_ms); 244 // #endif 245 printf_packet(packet_type, in, packet, len); 246 247 #endif 248 } 249 250 static int hci_dump_log_level_active(int log_level){ 251 if (log_level < 0) return 0; 252 if (log_level > LOG_LEVEL_ERROR) return 0; 253 return log_level_enabled[log_level]; 254 } 255 256 void hci_dump_log(int log_level, const char * format, ...){ 257 if (!hci_dump_log_level_active(log_level)) return; 258 va_list argptr; 259 va_start(argptr, format); 260 #ifdef HAVE_POSIX_FILE_IO 261 int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 262 hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 263 #else 264 printf("LOG -- "); 265 vprintf(format, argptr); 266 printf("\n"); 267 #endif 268 va_end(argptr); 269 } 270 271 #ifdef __AVR__ 272 void hci_dump_log_P(int log_level, PGM_P format, ...){ 273 if (!hci_dump_log_level_active(log_level)) return; 274 va_list argptr; 275 va_start(argptr, format); 276 printf_P(PSTR("LOG -- ")); 277 vfprintf_P(stdout, format, argptr); 278 printf_P(PSTR("\n")); 279 va_end(argptr); 280 } 281 #endif 282 283 void hci_dump_close(void){ 284 #ifdef HAVE_POSIX_FILE_IO 285 close(dump_file); 286 #endif 287 dump_file = -1; 288 } 289 290 void hci_dump_enable_log_level(int log_level, int enable){ 291 if (log_level < 0) return; 292 if (log_level > LOG_LEVEL_ERROR) return; 293 log_level_enabled[log_level] = enable; 294 } 295 296