xref: /btstack/platform/windows/hci_dump_windows_stdout.c (revision 25d5427a345779b159b63c0d8b197d2dee40cf37)
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_windows_stdout.c"
39 
40 
41 #include <Windows.h>
42 /*
43  *  Dump HCI trace on stdout
44  */
45 
46 #include "hci_dump.h"
47 #include "btstack_config.h"
48 #include "hci.h"
49 #include "hci_cmd.h"
50 #include <stdio.h>
51 
52 
53 #include "hci_dump_windows_stdout.h"
54 
55 #ifndef ENABLE_PRINTF_HEXDUMP
56 #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"
57 #endif
58 
59 static char log_message_buffer[HCI_DUMP_MAX_MESSAGE_LEN];
60 
61 static void hci_dump_windows_stdout_timestamp(void){
62 	SYSTEMTIME lt;
63 	GetLocalTime(&lt);
64 	printf("[%04u-%02u-%02u %02u:%02u:%02u.%3u] ", lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute, lt.wSecond, lt.wMilliseconds);
65 }
66 
67 static void hci_dump_windows_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
68     switch (packet_type){
69         case HCI_COMMAND_DATA_PACKET:
70             printf("CMD => ");
71             break;
72         case HCI_EVENT_PACKET:
73             printf("EVT <= ");
74             break;
75         case HCI_ACL_DATA_PACKET:
76 #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ACL
77             if (len > HCI_DUMP_STDOUT_MAX_SIZE_ACL){
78                 printf("LOG -- ACL %s, size %u\n", in ? "in" : "out", len);
79                 return;
80             }
81 #endif
82             if (in) {
83                 printf("ACL <= ");
84             } else {
85                 printf("ACL => ");
86             }
87             break;
88         case HCI_SCO_DATA_PACKET:
89 #ifdef HCI_DUMP_STDOUT_MAX_SIZE_SCO
90             if (len > HCI_DUMP_STDOUT_MAX_SIZE_SCO){
91                 printf("LOG -- SCO %s, size %u\n", in ? "in" : "out", len);
92                 return;
93             }
94 #endif
95             if (in) {
96                 printf("SCO <= ");
97             } else {
98                 printf("SCO => ");
99             }
100             break;
101         case HCI_ISO_DATA_PACKET:
102 #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ISO
103             if (len > HCI_DUMP_STDOUT_MAX_SIZE_ISO){
104                 printf("LOG -- ISO %s, size %u\n", in ? "in" : "out", len);
105                 return;
106             }
107 #endif
108             if (in) {
109                 printf("ISO <= ");
110             } else {
111                 printf("ISO => ");
112             }
113             break;
114         case LOG_MESSAGE_PACKET:
115             printf("LOG -- %s\n", (char*) packet);
116             return;
117         default:
118             return;
119     }
120     printf_hexdump(packet, len);
121 }
122 
123 static void hci_dump_windows_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
124     hci_dump_windows_stdout_timestamp();
125     hci_dump_windows_stdout_packet(packet_type, in, packet, len);
126 }
127 
128 static void hci_dump_windows_stdout_log_message(int log_level, const char * format, va_list argptr){
129     UNUSED(log_level);
130     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
131     hci_dump_windows_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
132 }
133 
134 const hci_dump_t * hci_dump_windows_stdout_get_instance(void){
135     static const hci_dump_t hci_dump_instance = {
136         // void (*reset)(void);
137         NULL,
138         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
139         &hci_dump_windows_stdout_log_packet,
140         // void (*log_message)(int log_level, const char * format, va_list argptr);
141         &hci_dump_windows_stdout_log_message,
142     };
143     return &hci_dump_instance;
144 }
145