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 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD 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 381713bceaSmatthias.ringwald /* 3979662672Smatthias.ringwald * hci_dump.c 4079662672Smatthias.ringwald * 41fe1ed1b8Smatthias.ringwald * Dump HCI trace in various formats: 42fe1ed1b8Smatthias.ringwald * 43fe1ed1b8Smatthias.ringwald * - BlueZ's hcidump format 44fe1ed1b8Smatthias.ringwald * - Apple's PacketLogger 45fe1ed1b8Smatthias.ringwald * - stdout hexdump 4679662672Smatthias.ringwald * 4779662672Smatthias.ringwald * Created by Matthias Ringwald on 5/26/09. 4879662672Smatthias.ringwald */ 4979662672Smatthias.ringwald 507907f069SMatthias Ringwald #include "btstack_config.h" 51a1d7dd1fSmatthias.ringwald 5279662672Smatthias.ringwald #include "hci_dump.h" 5379662672Smatthias.ringwald #include "hci.h" 54c6448b67Smatthias.ringwald #include "hci_transport.h" 5556042629SMatthias Ringwald #include "hci_cmd.h" 5682636622SMatthias Ringwald #include "btstack_run_loop.h" 57198a9e1bS[email protected] #include <stdio.h> 5879662672Smatthias.ringwald 59eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 60d5ea8924S[email protected] #include <fcntl.h> // open 6179662672Smatthias.ringwald #include <unistd.h> // write 628adf0ddaSmatthias.ringwald #include <time.h> 638b658ebcSmatthias.ringwald #include <sys/time.h> // for timestamps 64c7b9c559Smatthias.ringwald #include <sys/stat.h> // for mode flags 6568e27c0fSmatthias.ringwald #endif 6679662672Smatthias.ringwald 671a1c8389SMatthias Ringwald // BLUEZ hcidump - struct not used directly, but left here as documentation 688b658ebcSmatthias.ringwald typedef struct { 698b658ebcSmatthias.ringwald uint16_t len; 708b658ebcSmatthias.ringwald uint8_t in; 718b658ebcSmatthias.ringwald uint8_t pad; 728b658ebcSmatthias.ringwald uint32_t ts_sec; 738b658ebcSmatthias.ringwald uint32_t ts_usec; 748b658ebcSmatthias.ringwald uint8_t packet_type; 756c5c6faaSmatthias.ringwald } 766c5c6faaSmatthias.ringwald hcidump_hdr; 77f3e036dcSMatthias Ringwald #define HCIDUMP_HDR_SIZE 13 7879662672Smatthias.ringwald 791a1c8389SMatthias Ringwald // APPLE PacketLogger - struct not used directly, but left here as documentation 808b658ebcSmatthias.ringwald typedef struct { 818b658ebcSmatthias.ringwald uint32_t len; 828b658ebcSmatthias.ringwald uint32_t ts_sec; 838b658ebcSmatthias.ringwald uint32_t ts_usec; 842df12229Smatthias.ringwald uint8_t type; // 0xfc for note 856c5c6faaSmatthias.ringwald } 866c5c6faaSmatthias.ringwald pktlog_hdr; 87f3e036dcSMatthias Ringwald #define PKTLOG_HDR_SIZE 13 888b658ebcSmatthias.ringwald 898b658ebcSmatthias.ringwald static int dump_file = -1; 90eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 918b658ebcSmatthias.ringwald static int dump_format; 92f3e036dcSMatthias Ringwald static uint8_t header_bluez[HCIDUMP_HDR_SIZE]; 93f3e036dcSMatthias Ringwald static uint8_t header_packetlogger[PKTLOG_HDR_SIZE]; 948adf0ddaSmatthias.ringwald static char time_string[40]; 952992c131Smatthias.ringwald static int max_nr_packets = -1; 969ae0c346Smatthias.ringwald static int nr_packets = 0; 97a1d7dd1fSmatthias.ringwald static char log_message_buffer[256]; 9868e27c0fSmatthias.ringwald #endif 998b658ebcSmatthias.ringwald 1008a37b10aSMatthias Ringwald // levels: debug, info, error 1018a37b10aSMatthias Ringwald static int log_level_enabled[3] = { 1, 1, 1}; 1028a37b10aSMatthias Ringwald 103a225073eS[email protected] void hci_dump_open(const char *filename, hci_dump_format_t format){ 104eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 1058b658ebcSmatthias.ringwald dump_format = format; 1068adf0ddaSmatthias.ringwald if (dump_format == HCI_DUMP_STDOUT) { 1078adf0ddaSmatthias.ringwald dump_file = fileno(stdout); 1088adf0ddaSmatthias.ringwald } else { 109eb443d3dSMatthias Ringwald 110*1e154302SMatthias Ringwald int oflags = O_WRONLY | O_CREAT | O_TRUNC; 111b52eaea5S[email protected] #ifdef _WIN32 112*1e154302SMatthias Ringwald oflags |= O_BINARY; 113b52eaea5S[email protected] #endif 114eb443d3dSMatthias Ringwald 115*1e154302SMatthias Ringwald dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); 116*1e154302SMatthias Ringwald if (dump_file < 0){ 117*1e154302SMatthias Ringwald printf("hci_dump_open: failed to open file %s\n", filename); 118*1e154302SMatthias Ringwald } 11979662672Smatthias.ringwald } 120eb443d3dSMatthias Ringwald #else 121eb443d3dSMatthias Ringwald dump_file = 1; 12268e27c0fSmatthias.ringwald #endif 123d9659922Smatthias.ringwald } 12479662672Smatthias.ringwald 125eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 1262992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets){ 1272992c131Smatthias.ringwald max_nr_packets = packets; 1282992c131Smatthias.ringwald } 1297c5f7483Smatthias.ringwald #endif 1302992c131Smatthias.ringwald 1318a37b10aSMatthias Ringwald static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 132198a9e1bS[email protected] switch (packet_type){ 133198a9e1bS[email protected] case HCI_COMMAND_DATA_PACKET: 134198a9e1bS[email protected] printf("CMD => "); 135198a9e1bS[email protected] break; 136198a9e1bS[email protected] case HCI_EVENT_PACKET: 137198a9e1bS[email protected] printf("EVT <= "); 138198a9e1bS[email protected] break; 139198a9e1bS[email protected] case HCI_ACL_DATA_PACKET: 140198a9e1bS[email protected] if (in) { 141198a9e1bS[email protected] printf("ACL <= "); 142198a9e1bS[email protected] } else { 143198a9e1bS[email protected] printf("ACL => "); 144198a9e1bS[email protected] } 145198a9e1bS[email protected] break; 146*1e154302SMatthias Ringwald case HCI_SCO_DATA_PACKET: 147*1e154302SMatthias Ringwald if (in) { 148*1e154302SMatthias Ringwald printf("SCO <= "); 149*1e154302SMatthias Ringwald } else { 150*1e154302SMatthias Ringwald printf("SCO => "); 151*1e154302SMatthias Ringwald } 152*1e154302SMatthias Ringwald break; 153198a9e1bS[email protected] case LOG_MESSAGE_PACKET: 154198a9e1bS[email protected] printf("LOG -- %s\n", (char*) packet); 155198a9e1bS[email protected] return; 156198a9e1bS[email protected] default: 157198a9e1bS[email protected] return; 158198a9e1bS[email protected] } 159198a9e1bS[email protected] printf_hexdump(packet, len); 160198a9e1bS[email protected] } 161198a9e1bS[email protected] 162ad6274a7SMatthias Ringwald #ifndef HAVE_POSIX_FILE_IO 163ad6274a7SMatthias Ringwald static void printf_timestamp(void){ 164ad6274a7SMatthias Ringwald uint32_t time_ms = btstack_run_loop_get_time_ms(); 165ad6274a7SMatthias Ringwald int seconds = time_ms / 1000; 166ad6274a7SMatthias Ringwald int minutes = seconds / 60; 167f04a0c31SMatthias Ringwald unsigned int hours = minutes / 60; 168ad6274a7SMatthias Ringwald 169f04a0c31SMatthias Ringwald uint16_t p_ms = time_ms - (seconds * 1000); 170f04a0c31SMatthias Ringwald uint16_t p_seconds = seconds - (minutes * 60); 171f04a0c31SMatthias Ringwald uint16_t p_minutes = minutes - (hours * 60); 172ad6274a7SMatthias Ringwald printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms); 173ad6274a7SMatthias Ringwald } 174ad6274a7SMatthias Ringwald #endif 175ad6274a7SMatthias Ringwald 17679662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 17768e27c0fSmatthias.ringwald 1788b658ebcSmatthias.ringwald if (dump_file < 0) return; // not activated yet 1798b658ebcSmatthias.ringwald 180eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 181eb443d3dSMatthias Ringwald 1822992c131Smatthias.ringwald // don't grow bigger than max_nr_packets 1832992c131Smatthias.ringwald if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){ 1842992c131Smatthias.ringwald if (nr_packets >= max_nr_packets){ 1852992c131Smatthias.ringwald lseek(dump_file, 0, SEEK_SET); 1862992c131Smatthias.ringwald ftruncate(dump_file, 0); 1872992c131Smatthias.ringwald nr_packets = 0; 1882992c131Smatthias.ringwald } 1892992c131Smatthias.ringwald nr_packets++; 1902992c131Smatthias.ringwald } 1912992c131Smatthias.ringwald 1928b658ebcSmatthias.ringwald // get time 1938b658ebcSmatthias.ringwald struct timeval curr_time; 1948adf0ddaSmatthias.ringwald struct tm* ptm; 1958b658ebcSmatthias.ringwald gettimeofday(&curr_time, NULL); 196d5ea8924S[email protected] time_t curr_time_secs = curr_time.tv_sec; 1978b658ebcSmatthias.ringwald 1988b658ebcSmatthias.ringwald switch (dump_format){ 199a9cf4d77S[email protected] case HCI_DUMP_STDOUT: { 2008adf0ddaSmatthias.ringwald /* Obtain the time of day, and convert it to a tm struct. */ 201d5ea8924S[email protected] ptm = localtime (&curr_time_secs); 2027224be7eSMatthias Ringwald /* assert localtime was successful */ 2037224be7eSMatthias Ringwald if (!ptm) break; 2048adf0ddaSmatthias.ringwald /* Format the date and time, down to a single second. */ 2058adf0ddaSmatthias.ringwald strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); 2068adf0ddaSmatthias.ringwald /* Compute milliseconds from microseconds. */ 2078adf0ddaSmatthias.ringwald uint16_t milliseconds = curr_time.tv_usec / 1000; 2087224be7eSMatthias Ringwald /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ 2098adf0ddaSmatthias.ringwald printf ("%s.%03u] ", time_string, milliseconds); 210198a9e1bS[email protected] printf_packet(packet_type, in, packet, len); 2118adf0ddaSmatthias.ringwald break; 212a9cf4d77S[email protected] } 2130d79c710Smatthias.ringwald 2148b658ebcSmatthias.ringwald case HCI_DUMP_BLUEZ: 215f8fbdce0SMatthias Ringwald little_endian_store_16( header_bluez, 0, 1 + len); 2161a1c8389SMatthias Ringwald header_bluez[2] = in; 2171a1c8389SMatthias Ringwald header_bluez[3] = 0; 2187c959318SMatthias Ringwald little_endian_store_32( header_bluez, 4, (uint32_t) curr_time.tv_sec); 219f8fbdce0SMatthias Ringwald little_endian_store_32( header_bluez, 8, curr_time.tv_usec); 2201a1c8389SMatthias Ringwald header_bluez[12] = packet_type; 221f3e036dcSMatthias Ringwald write (dump_file, header_bluez, HCIDUMP_HDR_SIZE); 22279662672Smatthias.ringwald write (dump_file, packet, len ); 2238b658ebcSmatthias.ringwald break; 2240d79c710Smatthias.ringwald 2258b658ebcSmatthias.ringwald case HCI_DUMP_PACKETLOGGER: 226f8fbdce0SMatthias Ringwald big_endian_store_32( header_packetlogger, 0, PKTLOG_HDR_SIZE - 4 + len); 2277c959318SMatthias Ringwald big_endian_store_32( header_packetlogger, 4, (uint32_t) curr_time.tv_sec); 228f8fbdce0SMatthias Ringwald big_endian_store_32( header_packetlogger, 8, curr_time.tv_usec); 2298b658ebcSmatthias.ringwald switch (packet_type){ 2308b658ebcSmatthias.ringwald case HCI_COMMAND_DATA_PACKET: 2311a1c8389SMatthias Ringwald header_packetlogger[12] = 0x00; 2328b658ebcSmatthias.ringwald break; 2338b658ebcSmatthias.ringwald case HCI_ACL_DATA_PACKET: 2348b658ebcSmatthias.ringwald if (in) { 2351a1c8389SMatthias Ringwald header_packetlogger[12] = 0x03; 2368b658ebcSmatthias.ringwald } else { 2371a1c8389SMatthias Ringwald header_packetlogger[12] = 0x02; 2388b658ebcSmatthias.ringwald } 2398b658ebcSmatthias.ringwald break; 2408d675e3dS[email protected] case HCI_SCO_DATA_PACKET: 2418d675e3dS[email protected] if (in) { 2421a1c8389SMatthias Ringwald header_packetlogger[12] = 0x09; 2438d675e3dS[email protected] } else { 2441a1c8389SMatthias Ringwald header_packetlogger[12] = 0x08; 2458d675e3dS[email protected] } 2468d675e3dS[email protected] break; 2478b658ebcSmatthias.ringwald case HCI_EVENT_PACKET: 2481a1c8389SMatthias Ringwald header_packetlogger[12] = 0x01; 2498b658ebcSmatthias.ringwald break; 2500d79c710Smatthias.ringwald case LOG_MESSAGE_PACKET: 2511a1c8389SMatthias Ringwald header_packetlogger[12] = 0xfc; 2520d79c710Smatthias.ringwald break; 2538b658ebcSmatthias.ringwald default: 2548b658ebcSmatthias.ringwald return; 2558b658ebcSmatthias.ringwald } 256f3e036dcSMatthias Ringwald write (dump_file, &header_packetlogger, PKTLOG_HDR_SIZE); 2578b658ebcSmatthias.ringwald write (dump_file, packet, len ); 2580d79c710Smatthias.ringwald break; 2590d79c710Smatthias.ringwald 2600d79c710Smatthias.ringwald default: 2610d79c710Smatthias.ringwald break; 2628b658ebcSmatthias.ringwald } 263eb443d3dSMatthias Ringwald #else 264eb443d3dSMatthias Ringwald 265ad6274a7SMatthias Ringwald printf_timestamp(); 266eb443d3dSMatthias Ringwald printf_packet(packet_type, in, packet, len); 267eb443d3dSMatthias Ringwald 26868e27c0fSmatthias.ringwald #endif 26979662672Smatthias.ringwald } 27079662672Smatthias.ringwald 2718a37b10aSMatthias Ringwald static int hci_dump_log_level_active(int log_level){ 2728a37b10aSMatthias Ringwald if (log_level < 0) return 0; 2738a37b10aSMatthias Ringwald if (log_level > LOG_LEVEL_ERROR) return 0; 2748a37b10aSMatthias Ringwald return log_level_enabled[log_level]; 2758a37b10aSMatthias Ringwald } 2768a37b10aSMatthias Ringwald 27794be1a66SMatthias Ringwald void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){ 27894be1a66SMatthias Ringwald if (hci_dump_log_level_active(log_level)) { 279eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 280eb443d3dSMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 281eb443d3dSMatthias Ringwald hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 282eb443d3dSMatthias Ringwald #else 283ad6274a7SMatthias Ringwald printf_timestamp(); 2841df3b679S[email protected] printf("LOG -- "); 2851df3b679S[email protected] vprintf(format, argptr); 2861fd51e45S[email protected] printf("\n"); 287a1d7dd1fSmatthias.ringwald #endif 28894be1a66SMatthias Ringwald } 28994be1a66SMatthias Ringwald } 29094be1a66SMatthias Ringwald 29194be1a66SMatthias Ringwald void hci_dump_log(int log_level, const char * format, ...){ 29294be1a66SMatthias Ringwald va_list argptr; 29394be1a66SMatthias Ringwald va_start(argptr, format); 29494be1a66SMatthias Ringwald hci_dump_log_va_arg(log_level, format, argptr); 2951df3b679S[email protected] va_end(argptr); 296a1d7dd1fSmatthias.ringwald } 297a1d7dd1fSmatthias.ringwald 29820ea11b9S[email protected] #ifdef __AVR__ 2998a37b10aSMatthias Ringwald void hci_dump_log_P(int log_level, PGM_P format, ...){ 3008a37b10aSMatthias Ringwald if (!hci_dump_log_level_active(log_level)) return; 30120ea11b9S[email protected] va_list argptr; 30220ea11b9S[email protected] va_start(argptr, format); 30320ea11b9S[email protected] printf_P(PSTR("LOG -- ")); 30420ea11b9S[email protected] vfprintf_P(stdout, format, argptr); 30520ea11b9S[email protected] printf_P(PSTR("\n")); 30620ea11b9S[email protected] va_end(argptr); 30720ea11b9S[email protected] } 30820ea11b9S[email protected] #endif 30920ea11b9S[email protected] 31071de195eSMatthias Ringwald void hci_dump_close(void){ 311eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 31279662672Smatthias.ringwald close(dump_file); 31368e27c0fSmatthias.ringwald #endif 314eb443d3dSMatthias Ringwald dump_file = -1; 31579662672Smatthias.ringwald } 31679662672Smatthias.ringwald 3178a37b10aSMatthias Ringwald void hci_dump_enable_log_level(int log_level, int enable){ 3188a37b10aSMatthias Ringwald if (log_level < 0) return; 3198a37b10aSMatthias Ringwald if (log_level > LOG_LEVEL_ERROR) return; 3208a37b10aSMatthias Ringwald log_level_enabled[log_level] = enable; 3218a37b10aSMatthias Ringwald } 3228a37b10aSMatthias Ringwald 323