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 BLUEKITCHEN 24 * GMBH 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_posix_stdout.c" 39 40 /* 41 * Dump HCI trace on stdout 42 */ 43 44 #include "hci_dump.h" 45 #include "btstack_config.h" 46 #include "hci.h" 47 #include <time.h> 48 #include <sys/time.h> // for timestamps 49 #include "hci_cmd.h" 50 #include <stdio.h> 51 52 #ifndef ENABLE_PRINTF_HEXDUMP 53 #error "HCI Dump on stdout requires ENABLE_PRINTF_HEXDUMP to be defined. Use different hci dump implementation or add ENABLE_PRINTF_HEXDUMP to btstack_config.h" 54 #endif 55 56 static char time_string[40]; 57 static char log_message_buffer[HCI_DUMP_MAX_MESSAGE_LEN]; 58 59 static void hci_dump_posix_stdout_timestamp(void){ 60 struct tm* ptm; 61 struct timeval curr_time; 62 gettimeofday(&curr_time, NULL); 63 time_t curr_time_secs = curr_time.tv_sec; 64 /* Obtain the time of day, and convert it to a tm struct. */ 65 ptm = localtime (&curr_time_secs); 66 /* assert localtime was successful */ 67 if (!ptm) return; 68 /* Format the date and time, down to a single second. */ 69 strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); 70 /* Compute milliseconds from microseconds. */ 71 uint16_t milliseconds = curr_time.tv_usec / 1000; 72 /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ 73 printf ("%s.%03u] ", time_string, milliseconds); 74 } 75 76 static void hci_dump_posix_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 77 switch (packet_type){ 78 case HCI_COMMAND_DATA_PACKET: 79 printf("CMD => "); 80 break; 81 case HCI_EVENT_PACKET: 82 printf("EVT <= "); 83 break; 84 case HCI_ACL_DATA_PACKET: 85 if (in) { 86 printf("ACL <= "); 87 } else { 88 printf("ACL => "); 89 } 90 break; 91 case HCI_SCO_DATA_PACKET: 92 if (in) { 93 printf("SCO <= "); 94 } else { 95 printf("SCO => "); 96 } 97 break; 98 case HCI_ISO_DATA_PACKET: 99 if (in) { 100 printf("ISO <= "); 101 } else { 102 printf("ISO => "); 103 } 104 break; 105 case LOG_MESSAGE_PACKET: 106 printf("LOG -- %s\n", (char*) packet); 107 return; 108 default: 109 return; 110 } 111 printf_hexdump(packet, len); 112 } 113 114 static void hci_dump_posix_posix_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 115 hci_dump_posix_stdout_timestamp(); 116 hci_dump_posix_stdout_packet(packet_type, in, packet, len); 117 } 118 119 static void hci_dump_posix_stdout_log_message(int log_level, const char * format, va_list argptr){ 120 UNUSED(log_level); 121 int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); 122 hci_dump_posix_posix_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); 123 } 124 125 const hci_dump_t * hci_dump_posix_stdout_get_instance(void){ 126 static const hci_dump_t hci_dump_instance = { 127 // void (*reset)(void); 128 NULL, 129 // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 130 &hci_dump_posix_posix_stdout_log_packet, 131 // void (*log_message)(int log_level, const char * format, va_list argptr); 132 &hci_dump_posix_stdout_log_message, 133 }; 134 return &hci_dump_instance; 135 } 136