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 #define BTSTACK_FILE__ "hci_dump.c" 39 40 /* 41 * hci_dump.c 42 * 43 * Dump HCI trace in various formats: 44 * 45 * - BlueZ's hcidump format 46 * - Apple's PacketLogger 47 * - stdout hexdump 48 * 49 */ 50 51 #include "btstack_config.h" 52 53 // enable POSIX functions (needed for -std=c99) 54 #define _POSIX_C_SOURCE 200809 55 56 #include "hci_dump.h" 57 #include "hci.h" 58 #include "hci_transport.h" 59 #include "hci_cmd.h" 60 #include "btstack_run_loop.h" 61 #include <stdio.h> 62 63 #ifdef HAVE_POSIX_FILE_IO 64 #include <fcntl.h> // open 65 #include <unistd.h> // write 66 #include <time.h> 67 #include <sys/time.h> // for timestamps 68 #include <sys/stat.h> // for mode flags 69 #endif 70 71 #ifdef ENABLE_SEGGER_RTT 72 #include "SEGGER_RTT.h" 73 74 // allow to configure mode, channel, up buffer size in btstack_config.h for binary HCI formats (PacketLogger/BlueZ) 75 76 #ifndef SEGGER_RTT_PACKETLOG_MODE 77 #define SEGGER_RTT_PACKETLOG_MODE SEGGER_RTT_MODE_DEFAULT 78 #endif 79 #ifndef SEGGER_RTT_PACKETLOG_BUFFER_SIZE 80 #define SEGGER_RTT_PACKETLOG_BUFFER_SIZE 1024 81 #endif 82 #ifndef SEGGER_RTT_PACKETLOG_CHANNEL 83 #define SEGGER_RTT_PACKETLOG_CHANNEL 1 84 #endif 85 86 static char segger_rtt_packetlog_buffer[SEGGER_RTT_PACKETLOG_BUFFER_SIZE]; 87 #endif 88 89 // BLUEZ hcidump - struct not used directly, but left here as documentation 90 typedef struct { 91 uint16_t len; 92 uint8_t in; 93 uint8_t pad; 94 uint32_t ts_sec; 95 uint32_t ts_usec; 96 uint8_t packet_type; 97 } 98 hcidump_hdr; 99 #define HCIDUMP_HDR_SIZE 13 100 101 // APPLE PacketLogger - struct not used directly, but left here as documentation 102 typedef struct { 103 uint32_t len; 104 uint32_t ts_sec; 105 uint32_t ts_usec; 106 uint8_t type; // 0xfc for note 107 } 108 pktlog_hdr; 109 #define PKTLOG_HDR_SIZE 13 110 111 static int dump_file = -1; 112 static int dump_format; 113 static union { 114 uint8_t header_bluez[HCIDUMP_HDR_SIZE]; 115 uint8_t header_packetlogger[PKTLOG_HDR_SIZE]; 116 } header; 117 #ifdef HAVE_POSIX_FILE_IO 118 static char time_string[40]; 119 static int max_nr_packets = -1; 120 static int nr_packets = 0; 121 #endif 122 123 #if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT) 124 static char log_message_buffer[256]; 125 #endif 126 127 // levels: debug, info, error 128 static int log_level_enabled[3] = { 1, 1, 1}; 129 130 void hci_dump_open(const char *filename, hci_dump_format_t format){ 131 132 dump_format = format; 133 134 #ifdef HAVE_POSIX_FILE_IO 135 if (dump_format == HCI_DUMP_STDOUT) { 136 dump_file = fileno(stdout); 137 } else { 138 139 int oflags = O_WRONLY | O_CREAT | O_TRUNC; 140 #ifdef _WIN32 141 oflags |= O_BINARY; 142 #endif 143 dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); 144 if (dump_file < 0){ 145 printf("hci_dump_open: failed to open file %s\n", filename); 146 } 147 } 148 #else 149 150 UNUSED(filename); 151 152 #ifdef ENABLE_SEGGER_RTT 153 switch (dump_format){ 154 case HCI_DUMP_PACKETLOGGER: 155 case HCI_DUMP_BLUEZ: 156 SEGGER_RTT_ConfigUpBuffer(SEGGER_RTT_PACKETLOG_CHANNEL, "hci_dump", &segger_rtt_packetlog_buffer[0], SEGGER_RTT_PACKETLOG_BUFFER_SIZE, SEGGER_RTT_PACKETLOG_MODE); 157 break; 158 default: 159 break; 160 } 161 #endif 162 163 dump_file = 1; 164 #endif 165 } 166 167 #ifdef HAVE_POSIX_FILE_IO 168 void hci_dump_set_max_packets(int packets){ 169 max_nr_packets = packets; 170 } 171 #endif 172 173 static void hci_dump_packetlogger_setup_header(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){ 174 big_endian_store_32( buffer, 0, PKTLOG_HDR_SIZE - 4 + len); 175 big_endian_store_32( buffer, 4, tv_sec); 176 big_endian_store_32( buffer, 8, tv_us); 177 uint8_t packet_logger_type = 0; 178 switch (packet_type){ 179 case HCI_COMMAND_DATA_PACKET: 180 packet_logger_type = 0x00; 181 break; 182 case HCI_ACL_DATA_PACKET: 183 packet_logger_type = in ? 0x03 : 0x02; 184 break; 185 case HCI_SCO_DATA_PACKET: 186 packet_logger_type = in ? 0x09 : 0x08; 187 break; 188 case HCI_EVENT_PACKET: 189 packet_logger_type = 0x01; 190 break; 191 case LOG_MESSAGE_PACKET: 192 packet_logger_type = 0xfc; 193 break; 194 default: 195 return; 196 } 197 buffer[12] = packet_logger_type; 198 } 199 200 static void hci_dump_bluez_setup_header(uint8_t * buffer, uint32_t tv_sec, uint32_t tv_us, uint8_t packet_type, uint8_t in, uint16_t len){ 201 little_endian_store_16( buffer, 0, 1 + len); 202 buffer[2] = in; 203 buffer[3] = 0; 204 little_endian_store_32( buffer, 4, tv_sec); 205 little_endian_store_32( buffer, 8, tv_us); 206 buffer[12] = packet_type; 207 } 208 209 static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 210 switch (packet_type){ 211 case HCI_COMMAND_DATA_PACKET: 212 printf("CMD => "); 213 break; 214 case HCI_EVENT_PACKET: 215 printf("EVT <= "); 216 break; 217 case HCI_ACL_DATA_PACKET: 218 if (in) { 219 printf("ACL <= "); 220 } else { 221 printf("ACL => "); 222 } 223 break; 224 case HCI_SCO_DATA_PACKET: 225 if (in) { 226 printf("SCO <= "); 227 } else { 228 printf("SCO => "); 229 } 230 break; 231 case LOG_MESSAGE_PACKET: 232 printf("LOG -- %s\n", (char*) packet); 233 return; 234 default: 235 return; 236 } 237 printf_hexdump(packet, len); 238 } 239 240 static void printf_timestamp(void){ 241 #ifdef HAVE_POSIX_FILE_IO 242 struct tm* ptm; 243 struct timeval curr_time; 244 gettimeofday(&curr_time, NULL); 245 time_t curr_time_secs = curr_time.tv_sec; 246 /* Obtain the time of day, and convert it to a tm struct. */ 247 ptm = localtime (&curr_time_secs); 248 /* assert localtime was successful */ 249 if (!ptm) return; 250 /* Format the date and time, down to a single second. */ 251 strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); 252 /* Compute milliseconds from microseconds. */ 253 uint16_t milliseconds = curr_time.tv_usec / 1000; 254 /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ 255 printf ("%s.%03u] ", time_string, milliseconds); 256 #else 257 uint32_t time_ms = btstack_run_loop_get_time_ms(); 258 int seconds = time_ms / 1000; 259 int minutes = seconds / 60; 260 unsigned int hours = minutes / 60; 261 262 uint16_t p_ms = time_ms - (seconds * 1000); 263 uint16_t p_seconds = seconds - (minutes * 60); 264 uint16_t p_minutes = minutes - (hours * 60); 265 printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms); 266 #endif 267 } 268 269 void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 270 271 if (dump_file < 0) return; // not activated yet 272 273 #ifdef HAVE_POSIX_FILE_IO 274 // don't grow bigger than max_nr_packets 275 if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){ 276 if (nr_packets >= max_nr_packets){ 277 lseek(dump_file, 0, SEEK_SET); 278 // avoid -Wunused-result 279 int res = ftruncate(dump_file, 0); 280 UNUSED(res); 281 nr_packets = 0; 282 } 283 nr_packets++; 284 } 285 #endif 286 287 if (dump_format == HCI_DUMP_STDOUT){ 288 printf_timestamp(); 289 printf_packet(packet_type, in, packet, len); 290 return; 291 } 292 293 uint32_t tv_sec = 0; 294 uint32_t tv_us = 0; 295 296 // get time 297 #ifdef HAVE_POSIX_FILE_IO 298 struct timeval curr_time; 299 gettimeofday(&curr_time, NULL); 300 tv_sec = curr_time.tv_sec; 301 tv_us = curr_time.tv_usec; 302 #else 303 uint32_t time_ms = btstack_run_loop_get_time_ms(); 304 tv_us = time_ms * 1000; 305 tv_sec = 946728000UL + (time_ms / 1000); 306 #endif 307 308 #ifdef ENABLE_SEGGER_RTT 309 #if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP) 310 static const char rtt_warning[] = "RTT buffer full - packet(s) skipped"; 311 static bool rtt_packet_skipped = false; 312 if (rtt_packet_skipped){ 313 // try to write warning log message 314 rtt_packet_skipped = false; 315 packet_type = LOG_MESSAGE_PACKET; 316 packet = (uint8_t *) &rtt_warning[0]; 317 len = sizeof(rtt_warning)-1; 318 } 319 #endif 320 #endif 321 322 uint16_t header_len = 0; 323 switch (dump_format){ 324 case HCI_DUMP_BLUEZ: 325 hci_dump_bluez_setup_header(header.header_bluez, tv_sec, tv_us, packet_type, in, len); 326 header_len = HCIDUMP_HDR_SIZE; 327 break; 328 case HCI_DUMP_PACKETLOGGER: 329 hci_dump_packetlogger_setup_header(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len); 330 header_len = PKTLOG_HDR_SIZE; 331 break; 332 default: 333 return; 334 } 335 336 #ifdef HAVE_POSIX_FILE_IO 337 // avoid -Wunused-result 338 int res = 0; 339 res = write (dump_file, &header, header_len); 340 res = write (dump_file, packet, len ); 341 UNUSED(res); 342 #endif 343 344 #ifdef ENABLE_SEGGER_RTT 345 346 #if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP) 347 // check available space in buffer to avoid writing header but not packet 348 unsigned space_free = SEGGER_RTT_GetAvailWriteSpace(SEGGER_RTT_PACKETLOG_CHANNEL); 349 if ((header_len + len) > space_free) { 350 rtt_packet_skipped = true; 351 return; 352 } 353 #endif 354 355 SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, &header, header_len); 356 SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, packet, len); 357 #endif 358 UNUSED(header_len); 359 } 360 361 static int hci_dump_log_level_active(int log_level){ 362 if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return 0; 363 if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return 0; 364 return log_level_enabled[log_level]; 365 } 366 367 void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){ 368 if (!hci_dump_log_level_active(log_level)) return; 369 370 #if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT) 371 if (dump_file >= 0){ 372 int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 373 hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 374 return; 375 } 376 #endif 377 378 printf_timestamp(); 379 printf("LOG -- "); 380 vprintf(format, argptr); 381 printf("\n"); 382 } 383 384 void hci_dump_log(int log_level, const char * format, ...){ 385 va_list argptr; 386 va_start(argptr, format); 387 hci_dump_log_va_arg(log_level, format, argptr); 388 va_end(argptr); 389 } 390 391 #ifdef __AVR__ 392 void hci_dump_log_P(int log_level, PGM_P format, ...){ 393 if (!hci_dump_log_level_active(log_level)) return; 394 va_list argptr; 395 va_start(argptr, format); 396 printf_P(PSTR("LOG -- ")); 397 vfprintf_P(stdout, format, argptr); 398 printf_P(PSTR("\n")); 399 va_end(argptr); 400 } 401 #endif 402 403 void hci_dump_close(void){ 404 #ifdef HAVE_POSIX_FILE_IO 405 close(dump_file); 406 #endif 407 dump_file = -1; 408 } 409 410 void hci_dump_enable_log_level(int log_level, int enable){ 411 if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return; 412 if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return; 413 log_level_enabled[log_level] = enable; 414 } 415 416