xref: /btstack/platform/embedded/hci_dump_segger_rtt_stdout.c (revision da8e14c5aa3783b6bb7dd63e71572a901bcf168b)
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 "hci_dump.h"
45 #include "btstack_config.h"
46 #include "btstack_util.h"
47 #include "hci.h"
48 #include <stdio.h>
49 
50 #include "SEGGER_RTT.h"
51 
52 // [00:00:37.514] LOG -- xxx\n
53 #define ADDITIONAL_CHARS_PER_LINE (14+1+6+1+1)
54 
55 static void hci_dump_segger_rtt_stdout_timestamp(void);
56 
57 #if SEGGER_RTT_MODE_DEFAULT != SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
58 
59 // avoid trunkated log lines by calculating requires space in rtt buffer
60 // if message needs to be skipped, report number skipped messages
61 
62 static const char * message_packets_skipped = "RTT buffer full, %3u packet(s) skipped";
63 
64 static bool hci_dump_segger_prepare_message(uint16_t message_size){
65 
66     static uint32_t hci_dump_rtt_num_skipped = 0;
67 
68     // calculate num chars required for line
69     uint16_t required_space = ADDITIONAL_CHARS_PER_LINE + message_size;
70 
71     // if we have dropped packets before, we want to print a warning
72     if (hci_dump_rtt_num_skipped > 0){
73         required_space += ADDITIONAL_CHARS_PER_LINE + strlen(message_packets_skipped);
74     }
75 
76     // not enough space yet, drop message
77     if (required_space > SEGGER_RTT_GetAvailWriteSpace(0)){
78         hci_dump_rtt_num_skipped++;
79         return false;
80     }
81 
82     // print message if dropped packets
83     if (hci_dump_rtt_num_skipped > 0){
84         hci_dump_segger_rtt_stdout_timestamp();
85         SEGGER_RTT_printf(0, "LOG -- ");
86         SEGGER_RTT_printf(0, message_packets_skipped, hci_dump_rtt_num_skipped);
87         SEGGER_RTT_printf(0, "\n");
88         hci_dump_rtt_num_skipped = 0;
89     }
90     return true;
91 }
92 #endif
93 
94 static void hci_dump_segger_rtt_hexdump(const void *data, int size){
95     char buffer[4];
96     buffer[2] = ' ';
97     buffer[3] =  0;
98     const uint8_t * ptr = (const uint8_t *) data;
99     while (size > 0){
100         uint8_t byte = *ptr++;
101         buffer[0] = char_for_nibble(byte >> 4);
102         buffer[1] = char_for_nibble(byte & 0x0f);
103         SEGGER_RTT_Write(0, buffer, 3);
104         size--;
105     }
106     SEGGER_RTT_PutChar(0, '\n');
107 }
108 
109 static void hci_dump_segger_rtt_stdout_timestamp(void){
110     uint32_t time_ms = btstack_run_loop_get_time_ms();
111     int      seconds = time_ms / 1000u;
112     int      minutes = seconds / 60;
113     unsigned int hours = minutes / 60;
114 
115     uint16_t p_ms      = time_ms - (seconds * 1000u);
116     uint16_t p_seconds = seconds - (minutes * 60);
117     uint16_t p_minutes = minutes - (hours   * 60u);
118     SEGGER_RTT_printf(0, "[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
119 }
120 
121 static void hci_dump_segger_rtt_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
122     switch (packet_type){
123         case HCI_COMMAND_DATA_PACKET:
124             SEGGER_RTT_printf(0, "CMD => ");
125             break;
126         case HCI_EVENT_PACKET:
127             SEGGER_RTT_printf(0, "EVT <= ");
128             break;
129         case HCI_ACL_DATA_PACKET:
130             if (in) {
131                 SEGGER_RTT_printf(0, "ACL <= ");
132             } else {
133                 SEGGER_RTT_printf(0, "ACL => ");
134             }
135             break;
136         case HCI_SCO_DATA_PACKET:
137             if (in) {
138                 SEGGER_RTT_printf(0, "SCO <= ");
139             } else {
140                 SEGGER_RTT_printf(0, "SCO => ");
141             }
142             break;
143         case HCI_ISO_DATA_PACKET:
144             if (in) {
145                 SEGGER_RTT_printf(0, "ISO <= ");
146             } else {
147                 SEGGER_RTT_printf(0, "ISO => ");
148             }
149             break;
150         default:
151             return;
152     }
153     hci_dump_segger_rtt_hexdump(packet, len);
154 }
155 
156 static void hci_dump_segger_rtt_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
157 #if SEGGER_RTT_MODE_DEFAULT != SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
158     uint16_t message_len = len * 3;
159     bool ready = hci_dump_segger_prepare_message(message_len);
160     if (!ready) return;
161 #endif
162 
163     // print packet
164     hci_dump_segger_rtt_stdout_timestamp();
165     hci_dump_segger_rtt_stdout_packet(packet_type, in, packet, len);
166 }
167 
168 static void hci_dump_segger_rtt_stdout_log_message(int log_level, const char * format, va_list argptr){
169     UNUSED(log_level);
170 #if SEGGER_RTT_MODE_DEFAULT != SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
171     // to avoid using snprintf for this, we cheat and assume that the messages is less then HCI_DUMP_MAX_MESSAGE_LEN
172     bool ready = hci_dump_segger_prepare_message(HCI_DUMP_MAX_MESSAGE_LEN);
173     if (!ready) return;
174 #endif
175 
176     // print message
177     hci_dump_segger_rtt_stdout_timestamp();
178     SEGGER_RTT_printf(0, "LOG -- ");
179     SEGGER_RTT_vprintf(0, format, &argptr);
180     SEGGER_RTT_printf(0, "\n");
181 }
182 
183 const hci_dump_t * hci_dump_segger_rtt_stdout_get_instance(void){
184     static const hci_dump_t hci_dump_instance = {
185         // void (*reset)(void);
186         NULL,
187         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
188         &hci_dump_segger_rtt_stdout_log_packet,
189         // void (*log_message)(int log_level, const char * format, va_list argptr);
190         &hci_dump_segger_rtt_stdout_log_message,
191     };
192     return &hci_dump_instance;
193 }
194