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 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 4179662672Smatthias.ringwald * hci_dump.c 4279662672Smatthias.ringwald * 43fe1ed1b8Smatthias.ringwald * Dump HCI trace in various formats: 44fe1ed1b8Smatthias.ringwald * 45fe1ed1b8Smatthias.ringwald * - BlueZ's hcidump format 46fe1ed1b8Smatthias.ringwald * - Apple's PacketLogger 47fe1ed1b8Smatthias.ringwald * - stdout hexdump 4879662672Smatthias.ringwald * 4979662672Smatthias.ringwald */ 5079662672Smatthias.ringwald 517907f069SMatthias Ringwald #include "btstack_config.h" 52a1d7dd1fSmatthias.ringwald 53359cfe47SMatthias Ringwald // enable POSIX functions (needed for -std=c99) 54359cfe47SMatthias Ringwald #define _POSIX_C_SOURCE 200809 55359cfe47SMatthias Ringwald 5679662672Smatthias.ringwald #include "hci_dump.h" 5779662672Smatthias.ringwald #include "hci.h" 58c6448b67Smatthias.ringwald #include "hci_transport.h" 5956042629SMatthias Ringwald #include "hci_cmd.h" 6082636622SMatthias Ringwald #include "btstack_run_loop.h" 61198a9e1bS[email protected] #include <stdio.h> 6279662672Smatthias.ringwald 63eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 64d5ea8924S[email protected] #include <fcntl.h> // open 6579662672Smatthias.ringwald #include <unistd.h> // write 668adf0ddaSmatthias.ringwald #include <time.h> 678b658ebcSmatthias.ringwald #include <sys/time.h> // for timestamps 68c7b9c559Smatthias.ringwald #include <sys/stat.h> // for mode flags 6968e27c0fSmatthias.ringwald #endif 7079662672Smatthias.ringwald 715fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT 725fa31a99SMatthias Ringwald #include "SEGGER_RTT.h" 738cbbcccdSMatthias Ringwald 748cbbcccdSMatthias Ringwald // allow to configure mode, channel, up buffer size in btstack_config.h for binary HCI formats (PacketLogger/BlueZ) 758cbbcccdSMatthias Ringwald 768cbbcccdSMatthias Ringwald #ifndef SEGGER_RTT_PACKETLOG_MODE 778cbbcccdSMatthias Ringwald #define SEGGER_RTT_PACKETLOG_MODE SEGGER_RTT_MODE_DEFAULT 788cbbcccdSMatthias Ringwald #endif 798cbbcccdSMatthias Ringwald #ifndef SEGGER_RTT_PACKETLOG_BUFFER_SIZE 808cbbcccdSMatthias Ringwald #define SEGGER_RTT_PACKETLOG_BUFFER_SIZE 1024 818cbbcccdSMatthias Ringwald #endif 828cbbcccdSMatthias Ringwald #ifndef SEGGER_RTT_PACKETLOG_CHANNEL 838cbbcccdSMatthias Ringwald #define SEGGER_RTT_PACKETLOG_CHANNEL 1 848cbbcccdSMatthias Ringwald #endif 858cbbcccdSMatthias Ringwald 868cbbcccdSMatthias Ringwald static char segger_rtt_packetlog_buffer[SEGGER_RTT_PACKETLOG_BUFFER_SIZE]; 875fa31a99SMatthias Ringwald #endif 885fa31a99SMatthias Ringwald 891a1c8389SMatthias Ringwald // BLUEZ hcidump - struct not used directly, but left here as documentation 908b658ebcSmatthias.ringwald typedef struct { 918b658ebcSmatthias.ringwald uint16_t len; 928b658ebcSmatthias.ringwald uint8_t in; 938b658ebcSmatthias.ringwald uint8_t pad; 948b658ebcSmatthias.ringwald uint32_t ts_sec; 958b658ebcSmatthias.ringwald uint32_t ts_usec; 968b658ebcSmatthias.ringwald uint8_t packet_type; 976c5c6faaSmatthias.ringwald } 986c5c6faaSmatthias.ringwald hcidump_hdr; 99f3e036dcSMatthias Ringwald #define HCIDUMP_HDR_SIZE 13 10079662672Smatthias.ringwald 1011a1c8389SMatthias Ringwald // APPLE PacketLogger - struct not used directly, but left here as documentation 1028b658ebcSmatthias.ringwald typedef struct { 1038b658ebcSmatthias.ringwald uint32_t len; 1048b658ebcSmatthias.ringwald uint32_t ts_sec; 1058b658ebcSmatthias.ringwald uint32_t ts_usec; 1062df12229Smatthias.ringwald uint8_t type; // 0xfc for note 1076c5c6faaSmatthias.ringwald } 1086c5c6faaSmatthias.ringwald pktlog_hdr; 109f3e036dcSMatthias Ringwald #define PKTLOG_HDR_SIZE 13 1108b658ebcSmatthias.ringwald 1118b658ebcSmatthias.ringwald static int dump_file = -1; 1128b658ebcSmatthias.ringwald static int dump_format; 1135fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 1148adf0ddaSmatthias.ringwald static char time_string[40]; 1152992c131Smatthias.ringwald static int max_nr_packets = -1; 1169ae0c346Smatthias.ringwald static int nr_packets = 0; 1171fb07123SMatthias Ringwald #endif 1181fb07123SMatthias Ringwald 1191fb07123SMatthias Ringwald #if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT) 120a1d7dd1fSmatthias.ringwald static char log_message_buffer[256]; 12168e27c0fSmatthias.ringwald #endif 1228b658ebcSmatthias.ringwald 1238a37b10aSMatthias Ringwald // levels: debug, info, error 1248a37b10aSMatthias Ringwald static int log_level_enabled[3] = { 1, 1, 1}; 1258a37b10aSMatthias Ringwald 126a225073eS[email protected] void hci_dump_open(const char *filename, hci_dump_format_t format){ 1275fa31a99SMatthias Ringwald 1288b658ebcSmatthias.ringwald dump_format = format; 1295fa31a99SMatthias Ringwald 1305fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 1318adf0ddaSmatthias.ringwald if (dump_format == HCI_DUMP_STDOUT) { 1328adf0ddaSmatthias.ringwald dump_file = fileno(stdout); 1338adf0ddaSmatthias.ringwald } else { 134eb443d3dSMatthias Ringwald 1351e154302SMatthias Ringwald int oflags = O_WRONLY | O_CREAT | O_TRUNC; 136b52eaea5S[email protected] #ifdef _WIN32 1371e154302SMatthias Ringwald oflags |= O_BINARY; 138b52eaea5S[email protected] #endif 1391e154302SMatthias Ringwald dump_file = open(filename, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); 1401e154302SMatthias Ringwald if (dump_file < 0){ 1411e154302SMatthias Ringwald printf("hci_dump_open: failed to open file %s\n", filename); 1421e154302SMatthias Ringwald } 14379662672Smatthias.ringwald } 144eb443d3dSMatthias Ringwald #else 1455fa31a99SMatthias Ringwald 146d0662982SMatthias Ringwald UNUSED(filename); 1475fa31a99SMatthias Ringwald 1485fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT 1495fa31a99SMatthias Ringwald switch (dump_format){ 1505fa31a99SMatthias Ringwald case HCI_DUMP_PACKETLOGGER: 1515fa31a99SMatthias Ringwald case HCI_DUMP_BLUEZ: 1528cbbcccdSMatthias Ringwald SEGGER_RTT_ConfigUpBuffer(SEGGER_RTT_PACKETLOG_CHANNEL, "hci_dump", &segger_rtt_packetlog_buffer[0], SEGGER_RTT_PACKETLOG_BUFFER_SIZE, SEGGER_RTT_PACKETLOG_MODE); 1535fa31a99SMatthias Ringwald break; 1545fa31a99SMatthias Ringwald default: 1555fa31a99SMatthias Ringwald break; 1565fa31a99SMatthias Ringwald } 1575fa31a99SMatthias Ringwald #endif 158d0662982SMatthias Ringwald 159eb443d3dSMatthias Ringwald dump_file = 1; 16068e27c0fSmatthias.ringwald #endif 161d9659922Smatthias.ringwald } 16279662672Smatthias.ringwald 163eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 1642992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets){ 1652992c131Smatthias.ringwald max_nr_packets = packets; 1662992c131Smatthias.ringwald } 1677c5f7483Smatthias.ringwald #endif 1682992c131Smatthias.ringwald 1695fa31a99SMatthias Ringwald 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){ 1705fa31a99SMatthias Ringwald big_endian_store_32( buffer, 0, PKTLOG_HDR_SIZE - 4 + len); 1715fa31a99SMatthias Ringwald big_endian_store_32( buffer, 4, tv_sec); 1725fa31a99SMatthias Ringwald big_endian_store_32( buffer, 8, tv_us); 1735fa31a99SMatthias Ringwald uint8_t packet_logger_type = 0; 1745fa31a99SMatthias Ringwald switch (packet_type){ 1755fa31a99SMatthias Ringwald case HCI_COMMAND_DATA_PACKET: 1765fa31a99SMatthias Ringwald packet_logger_type = 0x00; 1775fa31a99SMatthias Ringwald break; 1785fa31a99SMatthias Ringwald case HCI_ACL_DATA_PACKET: 1795fa31a99SMatthias Ringwald packet_logger_type = in ? 0x03 : 0x02; 1805fa31a99SMatthias Ringwald break; 1815fa31a99SMatthias Ringwald case HCI_SCO_DATA_PACKET: 1825fa31a99SMatthias Ringwald packet_logger_type = in ? 0x09 : 0x08; 1835fa31a99SMatthias Ringwald break; 1845fa31a99SMatthias Ringwald case HCI_EVENT_PACKET: 1855fa31a99SMatthias Ringwald packet_logger_type = 0x01; 1865fa31a99SMatthias Ringwald break; 1875fa31a99SMatthias Ringwald case LOG_MESSAGE_PACKET: 1885fa31a99SMatthias Ringwald packet_logger_type = 0xfc; 1895fa31a99SMatthias Ringwald break; 1905fa31a99SMatthias Ringwald default: 1915fa31a99SMatthias Ringwald return; 1925fa31a99SMatthias Ringwald } 1935fa31a99SMatthias Ringwald buffer[12] = packet_logger_type; 1945fa31a99SMatthias Ringwald } 1955fa31a99SMatthias Ringwald 1965fa31a99SMatthias Ringwald 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){ 197*4ea43905SMatthias Ringwald little_endian_store_16( buffer, 0u, 1u + len); 1985fa31a99SMatthias Ringwald buffer[2] = in; 1995fa31a99SMatthias Ringwald buffer[3] = 0; 2005fa31a99SMatthias Ringwald little_endian_store_32( buffer, 4, tv_sec); 2015fa31a99SMatthias Ringwald little_endian_store_32( buffer, 8, tv_us); 2025fa31a99SMatthias Ringwald buffer[12] = packet_type; 2035fa31a99SMatthias Ringwald } 2045fa31a99SMatthias Ringwald 2058a37b10aSMatthias Ringwald static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 206198a9e1bS[email protected] switch (packet_type){ 207198a9e1bS[email protected] case HCI_COMMAND_DATA_PACKET: 208198a9e1bS[email protected] printf("CMD => "); 209198a9e1bS[email protected] break; 210198a9e1bS[email protected] case HCI_EVENT_PACKET: 211198a9e1bS[email protected] printf("EVT <= "); 212198a9e1bS[email protected] break; 213198a9e1bS[email protected] case HCI_ACL_DATA_PACKET: 214198a9e1bS[email protected] if (in) { 215198a9e1bS[email protected] printf("ACL <= "); 216198a9e1bS[email protected] } else { 217198a9e1bS[email protected] printf("ACL => "); 218198a9e1bS[email protected] } 219198a9e1bS[email protected] break; 2201e154302SMatthias Ringwald case HCI_SCO_DATA_PACKET: 2211e154302SMatthias Ringwald if (in) { 2221e154302SMatthias Ringwald printf("SCO <= "); 2231e154302SMatthias Ringwald } else { 2241e154302SMatthias Ringwald printf("SCO => "); 2251e154302SMatthias Ringwald } 2261e154302SMatthias Ringwald break; 227198a9e1bS[email protected] case LOG_MESSAGE_PACKET: 228198a9e1bS[email protected] printf("LOG -- %s\n", (char*) packet); 229198a9e1bS[email protected] return; 230198a9e1bS[email protected] default: 231198a9e1bS[email protected] return; 232198a9e1bS[email protected] } 233198a9e1bS[email protected] printf_hexdump(packet, len); 234198a9e1bS[email protected] } 235198a9e1bS[email protected] 236ad6274a7SMatthias Ringwald static void printf_timestamp(void){ 2376401061aSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 2386401061aSMatthias Ringwald struct tm* ptm; 2396401061aSMatthias Ringwald struct timeval curr_time; 2406401061aSMatthias Ringwald gettimeofday(&curr_time, NULL); 2416401061aSMatthias Ringwald time_t curr_time_secs = curr_time.tv_sec; 2426401061aSMatthias Ringwald /* Obtain the time of day, and convert it to a tm struct. */ 2436401061aSMatthias Ringwald ptm = localtime (&curr_time_secs); 2446401061aSMatthias Ringwald /* assert localtime was successful */ 2456401061aSMatthias Ringwald if (!ptm) return; 2466401061aSMatthias Ringwald /* Format the date and time, down to a single second. */ 2476401061aSMatthias Ringwald strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); 2486401061aSMatthias Ringwald /* Compute milliseconds from microseconds. */ 2496401061aSMatthias Ringwald uint16_t milliseconds = curr_time.tv_usec / 1000; 2506401061aSMatthias Ringwald /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ 2516401061aSMatthias Ringwald printf ("%s.%03u] ", time_string, milliseconds); 2526401061aSMatthias Ringwald #else 253ad6274a7SMatthias Ringwald uint32_t time_ms = btstack_run_loop_get_time_ms(); 254*4ea43905SMatthias Ringwald int seconds = time_ms / 1000u; 255ad6274a7SMatthias Ringwald int minutes = seconds / 60; 256f04a0c31SMatthias Ringwald unsigned int hours = minutes / 60; 257ad6274a7SMatthias Ringwald 258*4ea43905SMatthias Ringwald uint16_t p_ms = time_ms - (seconds * 1000u); 259f04a0c31SMatthias Ringwald uint16_t p_seconds = seconds - (minutes * 60); 260*4ea43905SMatthias Ringwald uint16_t p_minutes = minutes - (hours * 60u); 261ad6274a7SMatthias Ringwald printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms); 262ad6274a7SMatthias Ringwald #endif 2636401061aSMatthias Ringwald } 264ad6274a7SMatthias Ringwald 26579662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 26668e27c0fSmatthias.ringwald 2678334d3d8SMatthias Ringwald static union { 2688334d3d8SMatthias Ringwald uint8_t header_bluez[HCIDUMP_HDR_SIZE]; 2698334d3d8SMatthias Ringwald uint8_t header_packetlogger[PKTLOG_HDR_SIZE]; 2708334d3d8SMatthias Ringwald } header; 2718334d3d8SMatthias Ringwald 2728b658ebcSmatthias.ringwald if (dump_file < 0) return; // not activated yet 2738b658ebcSmatthias.ringwald 274eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 2752992c131Smatthias.ringwald // don't grow bigger than max_nr_packets 2762992c131Smatthias.ringwald if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){ 2772992c131Smatthias.ringwald if (nr_packets >= max_nr_packets){ 2782992c131Smatthias.ringwald lseek(dump_file, 0, SEEK_SET); 279acb15818SMatthias Ringwald // avoid -Wunused-result 280acb15818SMatthias Ringwald int res = ftruncate(dump_file, 0); 281acb15818SMatthias Ringwald UNUSED(res); 2822992c131Smatthias.ringwald nr_packets = 0; 2832992c131Smatthias.ringwald } 2842992c131Smatthias.ringwald nr_packets++; 2852992c131Smatthias.ringwald } 2865fa31a99SMatthias Ringwald #endif 2872992c131Smatthias.ringwald 2885fa31a99SMatthias Ringwald if (dump_format == HCI_DUMP_STDOUT){ 2896401061aSMatthias Ringwald printf_timestamp(); 290198a9e1bS[email protected] printf_packet(packet_type, in, packet, len); 2915fa31a99SMatthias Ringwald return; 292a9cf4d77S[email protected] } 2930d79c710Smatthias.ringwald 2945fa31a99SMatthias Ringwald uint32_t tv_sec = 0; 2955fa31a99SMatthias Ringwald uint32_t tv_us = 0; 2965fa31a99SMatthias Ringwald 2975fa31a99SMatthias Ringwald // get time 2985fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 2995fa31a99SMatthias Ringwald struct timeval curr_time; 3005fa31a99SMatthias Ringwald gettimeofday(&curr_time, NULL); 3015fa31a99SMatthias Ringwald tv_sec = curr_time.tv_sec; 3025fa31a99SMatthias Ringwald tv_us = curr_time.tv_usec; 3035fa31a99SMatthias Ringwald #else 3045fa31a99SMatthias Ringwald uint32_t time_ms = btstack_run_loop_get_time_ms(); 305*4ea43905SMatthias Ringwald tv_us = time_ms * 1000u; 306*4ea43905SMatthias Ringwald tv_sec = 946728000UL + (time_ms / 1000u); 3075fa31a99SMatthias Ringwald #endif 3085fa31a99SMatthias Ringwald 3093b809c72SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT 3108cbbcccdSMatthias Ringwald #if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP) 3113b809c72SMatthias Ringwald static const char rtt_warning[] = "RTT buffer full - packet(s) skipped"; 3123b809c72SMatthias Ringwald static bool rtt_packet_skipped = false; 3133b809c72SMatthias Ringwald if (rtt_packet_skipped){ 3143b809c72SMatthias Ringwald // try to write warning log message 3153b809c72SMatthias Ringwald rtt_packet_skipped = false; 3163b809c72SMatthias Ringwald packet_type = LOG_MESSAGE_PACKET; 3173b809c72SMatthias Ringwald packet = (uint8_t *) &rtt_warning[0]; 3183b809c72SMatthias Ringwald len = sizeof(rtt_warning)-1; 3193b809c72SMatthias Ringwald } 3203b809c72SMatthias Ringwald #endif 3213b809c72SMatthias Ringwald #endif 3223b809c72SMatthias Ringwald 3235fa31a99SMatthias Ringwald uint16_t header_len = 0; 3245fa31a99SMatthias Ringwald switch (dump_format){ 3258b658ebcSmatthias.ringwald case HCI_DUMP_BLUEZ: 3265fa31a99SMatthias Ringwald hci_dump_bluez_setup_header(header.header_bluez, tv_sec, tv_us, packet_type, in, len); 3275fa31a99SMatthias Ringwald header_len = HCIDUMP_HDR_SIZE; 3288b658ebcSmatthias.ringwald break; 3298b658ebcSmatthias.ringwald case HCI_DUMP_PACKETLOGGER: 3305fa31a99SMatthias Ringwald hci_dump_packetlogger_setup_header(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len); 3315fa31a99SMatthias Ringwald header_len = PKTLOG_HDR_SIZE; 3320d79c710Smatthias.ringwald break; 3338b658ebcSmatthias.ringwald default: 3348b658ebcSmatthias.ringwald return; 3358b658ebcSmatthias.ringwald } 3365fa31a99SMatthias Ringwald 3375fa31a99SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 3385fa31a99SMatthias Ringwald // avoid -Wunused-result 3395fa31a99SMatthias Ringwald int res = 0; 3405fa31a99SMatthias Ringwald res = write (dump_file, &header, header_len); 341acb15818SMatthias Ringwald res = write (dump_file, packet, len ); 342acb15818SMatthias Ringwald UNUSED(res); 34368e27c0fSmatthias.ringwald #endif 3443b809c72SMatthias Ringwald 3455fa31a99SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT 3463b809c72SMatthias Ringwald 3478cbbcccdSMatthias Ringwald #if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP) 3483b809c72SMatthias Ringwald // check available space in buffer to avoid writing header but not packet 3498cbbcccdSMatthias Ringwald unsigned space_free = SEGGER_RTT_GetAvailWriteSpace(SEGGER_RTT_PACKETLOG_CHANNEL); 3503b809c72SMatthias Ringwald if ((header_len + len) > space_free) { 3513b809c72SMatthias Ringwald rtt_packet_skipped = true; 3523b809c72SMatthias Ringwald return; 3533b809c72SMatthias Ringwald } 3543b809c72SMatthias Ringwald #endif 3553b809c72SMatthias Ringwald 3568cbbcccdSMatthias Ringwald SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, &header, header_len); 3578cbbcccdSMatthias Ringwald SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, packet, len); 3585fa31a99SMatthias Ringwald #endif 3595fa31a99SMatthias Ringwald UNUSED(header_len); 36079662672Smatthias.ringwald } 36179662672Smatthias.ringwald 3628a37b10aSMatthias Ringwald static int hci_dump_log_level_active(int log_level){ 363fa087deaSMatthias Ringwald if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return 0; 364fa087deaSMatthias Ringwald if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return 0; 3658a37b10aSMatthias Ringwald return log_level_enabled[log_level]; 3668a37b10aSMatthias Ringwald } 3678a37b10aSMatthias Ringwald 36894be1a66SMatthias Ringwald void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){ 3696401061aSMatthias Ringwald if (!hci_dump_log_level_active(log_level)) return; 3706401061aSMatthias Ringwald 3711fb07123SMatthias Ringwald #if defined(HAVE_POSIX_FILE_IO) || defined (ENABLE_SEGGER_RTT) 3726401061aSMatthias Ringwald if (dump_file >= 0){ 373eb443d3dSMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 374eb443d3dSMatthias Ringwald hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 375b8ae70b4SMatthias Ringwald return; 3766401061aSMatthias Ringwald } 3776401061aSMatthias Ringwald #endif 3786401061aSMatthias Ringwald 379ad6274a7SMatthias Ringwald printf_timestamp(); 3801df3b679S[email protected] printf("LOG -- "); 3811df3b679S[email protected] vprintf(format, argptr); 3821fd51e45S[email protected] printf("\n"); 38394be1a66SMatthias Ringwald } 38494be1a66SMatthias Ringwald 38594be1a66SMatthias Ringwald void hci_dump_log(int log_level, const char * format, ...){ 38694be1a66SMatthias Ringwald va_list argptr; 38794be1a66SMatthias Ringwald va_start(argptr, format); 38894be1a66SMatthias Ringwald hci_dump_log_va_arg(log_level, format, argptr); 3891df3b679S[email protected] va_end(argptr); 390a1d7dd1fSmatthias.ringwald } 391a1d7dd1fSmatthias.ringwald 39220ea11b9S[email protected] #ifdef __AVR__ 3938a37b10aSMatthias Ringwald void hci_dump_log_P(int log_level, PGM_P format, ...){ 3948a37b10aSMatthias Ringwald if (!hci_dump_log_level_active(log_level)) return; 39520ea11b9S[email protected] va_list argptr; 39620ea11b9S[email protected] va_start(argptr, format); 39720ea11b9S[email protected] printf_P(PSTR("LOG -- ")); 39820ea11b9S[email protected] vfprintf_P(stdout, format, argptr); 39920ea11b9S[email protected] printf_P(PSTR("\n")); 40020ea11b9S[email protected] va_end(argptr); 40120ea11b9S[email protected] } 40220ea11b9S[email protected] #endif 40320ea11b9S[email protected] 40471de195eSMatthias Ringwald void hci_dump_close(void){ 405eb443d3dSMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 40679662672Smatthias.ringwald close(dump_file); 40768e27c0fSmatthias.ringwald #endif 408eb443d3dSMatthias Ringwald dump_file = -1; 40979662672Smatthias.ringwald } 41079662672Smatthias.ringwald 4118a37b10aSMatthias Ringwald void hci_dump_enable_log_level(int log_level, int enable){ 412fa087deaSMatthias Ringwald if (log_level < HCI_DUMP_LOG_LEVEL_DEBUG) return; 413fa087deaSMatthias Ringwald if (log_level > HCI_DUMP_LOG_LEVEL_ERROR) return; 4148a37b10aSMatthias Ringwald log_level_enabled[log_level] = enable; 4158a37b10aSMatthias Ringwald } 4168a37b10aSMatthias Ringwald 417