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
hci_dump_posix_stdout_timestamp(void)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
hci_dump_posix_stdout_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)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 #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ACL
86 if (len > HCI_DUMP_STDOUT_MAX_SIZE_ACL){
87 printf("LOG -- ACL %s, size %u\n", in ? "in" : "out", len);
88 return;
89 }
90 #endif
91 if (in) {
92 printf("ACL <= ");
93 } else {
94 printf("ACL => ");
95 }
96 break;
97 case HCI_SCO_DATA_PACKET:
98 #ifdef HCI_DUMP_STDOUT_MAX_SIZE_SCO
99 if (len > HCI_DUMP_STDOUT_MAX_SIZE_SCO){
100 printf("LOG -- SCO %s, size %u\n", in ? "in" : "out", len);
101 return;
102 }
103 #endif
104 if (in) {
105 printf("SCO <= ");
106 } else {
107 printf("SCO => ");
108 }
109 break;
110 case HCI_ISO_DATA_PACKET:
111 #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ISO
112 if (len > HCI_DUMP_STDOUT_MAX_SIZE_ISO){
113 printf("LOG -- ISO %s, size %u\n", in ? "in" : "out", len);
114 return;
115 }
116 #endif
117 if (in) {
118 printf("ISO <= ");
119 } else {
120 printf("ISO => ");
121 }
122 break;
123 case LOG_MESSAGE_PACKET:
124 printf("LOG -- %s\n", (char*) packet);
125 return;
126 default:
127 return;
128 }
129 printf_hexdump(packet, len);
130 }
131
hci_dump_posix_posix_stdout_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)132 static void hci_dump_posix_posix_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
133 hci_dump_posix_stdout_timestamp();
134 hci_dump_posix_stdout_packet(packet_type, in, packet, len);
135 }
136
hci_dump_posix_stdout_log_message(int log_level,const char * format,va_list argptr)137 static void hci_dump_posix_stdout_log_message(int log_level, const char * format, va_list argptr){
138 UNUSED(log_level);
139 int full_string_len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
140 int len = btstack_min(sizeof(log_message_buffer), full_string_len);
141 hci_dump_posix_posix_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
142 }
143
hci_dump_posix_stdout_get_instance(void)144 const hci_dump_t * hci_dump_posix_stdout_get_instance(void){
145 static const hci_dump_t hci_dump_instance = {
146 // void (*reset)(void);
147 NULL,
148 // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
149 &hci_dump_posix_posix_stdout_log_packet,
150 // void (*log_message)(int log_level, const char * format, va_list argptr);
151 &hci_dump_posix_stdout_log_message,
152 };
153 return &hci_dump_instance;
154 }
155