xref: /btstack/platform/embedded/hci_dump_embedded_stdout.c (revision 4a9eead824c50b40e12b6f72611a74a3f57a47f6)
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_embedded_stdout.c"
39 
40 /*
41  *  Dump HCI trace on stdout
42  */
43 
44 #include "btstack_config.h"
45 
46 #include "hci_dump_embedded_stdout.h"
47 #include "btstack_run_loop.h"
48 #include "hci.h"
49 
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 log_message_buffer[256];
57 
58 static void hci_dump_embedded_stdout_timestamp(void){
59     uint32_t time_ms = btstack_run_loop_get_time_ms();
60     int      seconds = time_ms / 1000u;
61     int      minutes = seconds / 60;
62     unsigned int hours = minutes / 60;
63 
64     uint16_t p_ms      = time_ms - (seconds * 1000u);
65     uint16_t p_seconds = seconds - (minutes * 60);
66     uint16_t p_minutes = minutes - (hours   * 60u);
67     printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
68 }
69 
70 static void hci_dump_embedded_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
71     switch (packet_type){
72         case HCI_COMMAND_DATA_PACKET:
73             printf("CMD => ");
74             break;
75         case HCI_EVENT_PACKET:
76             printf("EVT <= ");
77             break;
78         case HCI_ACL_DATA_PACKET:
79             if (in) {
80                 printf("ACL <= ");
81             } else {
82                 printf("ACL => ");
83             }
84             break;
85         case HCI_SCO_DATA_PACKET:
86             if (in) {
87                 printf("SCO <= ");
88             } else {
89                 printf("SCO => ");
90             }
91             break;
92         case HCI_ISO_DATA_PACKET:
93             if (in) {
94                 printf("ISO <= ");
95             } else {
96                 printf("ISO => ");
97             }
98             break;
99         case LOG_MESSAGE_PACKET:
100             printf("LOG -- %s\n", (char*) packet);
101             return;
102         default:
103             return;
104     }
105     printf_hexdump(packet, len);
106 }
107 
108 static void hci_dump_embedded_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
109     hci_dump_embedded_stdout_timestamp();
110     hci_dump_embedded_stdout_packet(packet_type, in, packet, len);
111 }
112 
113 static void hci_dump_embedded_stdout_log_message(const char * format, va_list argptr){
114     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
115     hci_dump_embedded_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
116 }
117 
118 #ifdef __AVR__
119 void hci_dump_embedded_stdout_log_message_P(int log_level, PGM_P * format, va_list argptr){
120     hci_dump_embedded_stdout_timestamp();
121     printf_P(PSTR("LOG -- "));
122     vfprintf_P(stdout, format, argptr);
123     printf_P(PSTR("\n"));
124 }
125 #endif
126 
127 const hci_dump_t * hci_dump_embedded_stdout_get_instance(void){
128     static const hci_dump_t hci_dump_instance = {
129         // void (*reset)(void);
130         NULL,
131         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
132         &hci_dump_embedded_stdout_log_packet,
133         // void (*log_message)(int log_level, const char * format, va_list argptr);
134         &hci_dump_embedded_stdout_log_message,
135 #ifdef __AVR__ \
136         // void (*log_message_P)(int log_level, PGM_P * format, va_list argptr);
137         &hci_dump_embedded_stdout_log_message_P,
138 #endif
139     };
140     return &hci_dump_instance;
141 }
142