xref: /btstack/platform/embedded/hci_dump_segger_rtt_binary.c (revision da39cc4eccd07793d055d20e9c7bab74c315455f)
1128d6c99SMatthias Ringwald /*
2128d6c99SMatthias Ringwald  * Copyright (C) 2014-2020 BlueKitchen GmbH
3128d6c99SMatthias Ringwald  *
4128d6c99SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5128d6c99SMatthias Ringwald  * modification, are permitted provided that the following conditions
6128d6c99SMatthias Ringwald  * are met:
7128d6c99SMatthias Ringwald  *
8128d6c99SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9128d6c99SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10128d6c99SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11128d6c99SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12128d6c99SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13128d6c99SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14128d6c99SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15128d6c99SMatthias Ringwald  *    from this software without specific prior written permission.
16128d6c99SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17128d6c99SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18128d6c99SMatthias Ringwald  *    monetary gain.
19128d6c99SMatthias Ringwald  *
20128d6c99SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21128d6c99SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22128d6c99SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25128d6c99SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26128d6c99SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27128d6c99SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28128d6c99SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29128d6c99SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30128d6c99SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31128d6c99SMatthias Ringwald  * SUCH DAMAGE.
32128d6c99SMatthias Ringwald  *
33128d6c99SMatthias Ringwald  * Please inquire about commercial licensing options at
34128d6c99SMatthias Ringwald  * contact@bluekitchen-gmbh.com
35128d6c99SMatthias Ringwald  *
36128d6c99SMatthias Ringwald  */
37128d6c99SMatthias Ringwald 
38128d6c99SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump_segger_rtt_binary.c"
39128d6c99SMatthias Ringwald 
40128d6c99SMatthias Ringwald /**
41128d6c99SMatthias Ringwald  * Dump HCI trace on SEGGER RTT Logging Channel #1
42128d6c99SMatthias Ringwald  */
43128d6c99SMatthias Ringwald #include "btstack_config.h"
44128d6c99SMatthias Ringwald 
45*da39cc4eSMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
46*da39cc4eSMatthias Ringwald 
47128d6c99SMatthias Ringwald #include "hci_dump_segger_rtt_binary.h"
48128d6c99SMatthias Ringwald 
49128d6c99SMatthias Ringwald #include "btstack_debug.h"
50128d6c99SMatthias Ringwald #include "hci.h"
51128d6c99SMatthias Ringwald 
52128d6c99SMatthias Ringwald #include "SEGGER_RTT.h"
53128d6c99SMatthias Ringwald 
54d813b298SMatthias Ringwald #include <stdio.h>  // vsnprintf
55d813b298SMatthias Ringwald 
56128d6c99SMatthias Ringwald // allow to configure mode, channel, up buffer size in btstack_config.h
57128d6c99SMatthias Ringwald #ifndef SEGGER_RTT_PACKETLOG_MODE
58128d6c99SMatthias Ringwald #define SEGGER_RTT_PACKETLOG_MODE SEGGER_RTT_MODE_DEFAULT
59128d6c99SMatthias Ringwald #endif
60128d6c99SMatthias Ringwald #ifndef SEGGER_RTT_PACKETLOG_BUFFER_SIZE
61128d6c99SMatthias Ringwald #define SEGGER_RTT_PACKETLOG_BUFFER_SIZE 1024
62128d6c99SMatthias Ringwald #endif
63128d6c99SMatthias Ringwald #ifndef SEGGER_RTT_PACKETLOG_CHANNEL
64128d6c99SMatthias Ringwald #define SEGGER_RTT_PACKETLOG_CHANNEL 1
65128d6c99SMatthias Ringwald #endif
66128d6c99SMatthias Ringwald 
67128d6c99SMatthias Ringwald static char segger_rtt_packetlog_buffer[SEGGER_RTT_PACKETLOG_BUFFER_SIZE];
68128d6c99SMatthias Ringwald 
69128d6c99SMatthias Ringwald static int  dump_format;
70128d6c99SMatthias Ringwald static char log_message_buffer[256];
71128d6c99SMatthias Ringwald 
hci_dump_segger_rtt_binary_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)72128d6c99SMatthias Ringwald static void hci_dump_segger_rtt_binary_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
73128d6c99SMatthias Ringwald     if (dump_format == HCI_DUMP_INVALID) return;
74128d6c99SMatthias Ringwald 
75128d6c99SMatthias Ringwald     static union {
76128d6c99SMatthias Ringwald         uint8_t header_bluez[HCI_DUMP_HEADER_SIZE_BLUEZ];
77128d6c99SMatthias Ringwald         uint8_t header_packetlogger[HCI_DUMP_HEADER_SIZE_PACKETLOGGER];
78128d6c99SMatthias Ringwald     } header;
79128d6c99SMatthias Ringwald 
80128d6c99SMatthias Ringwald     uint32_t time_ms = btstack_run_loop_get_time_ms();
81128d6c99SMatthias Ringwald     uint32_t tv_sec  = time_ms / 1000u;
82128d6c99SMatthias Ringwald     uint32_t tv_us   = (time_ms - (tv_sec * 1000)) * 1000;
83128d6c99SMatthias Ringwald     // Saturday, January 1, 2000 12:00:00
84128d6c99SMatthias Ringwald     tv_sec += 946728000UL;
85128d6c99SMatthias Ringwald 
86128d6c99SMatthias Ringwald #if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP)
87128d6c99SMatthias Ringwald     static const char rtt_warning[] = "RTT buffer full - packet(s) skipped";
88128d6c99SMatthias Ringwald     static bool rtt_packet_skipped = false;
89128d6c99SMatthias Ringwald     if (rtt_packet_skipped){
90128d6c99SMatthias Ringwald         // try to write warning log message
91128d6c99SMatthias Ringwald         rtt_packet_skipped = false;
92128d6c99SMatthias Ringwald         packet_type = LOG_MESSAGE_PACKET;
93128d6c99SMatthias Ringwald         packet      = (uint8_t *) &rtt_warning[0];
94128d6c99SMatthias Ringwald         len         = sizeof(rtt_warning)-1;
95128d6c99SMatthias Ringwald     }
96128d6c99SMatthias Ringwald #endif
97128d6c99SMatthias Ringwald 
98128d6c99SMatthias Ringwald     uint16_t header_len = 0;
99128d6c99SMatthias Ringwald     switch (dump_format){
100128d6c99SMatthias Ringwald         case HCI_DUMP_BLUEZ:
101128d6c99SMatthias Ringwald             hci_dump_setup_header_bluez(header.header_bluez, tv_sec, tv_us, packet_type, in, len);
102128d6c99SMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_BLUEZ;
103128d6c99SMatthias Ringwald             break;
104128d6c99SMatthias Ringwald         case HCI_DUMP_PACKETLOGGER:
105128d6c99SMatthias Ringwald             hci_dump_setup_header_packetlogger(header.header_packetlogger, tv_sec, tv_us, packet_type, in, len);
106128d6c99SMatthias Ringwald             header_len = HCI_DUMP_HEADER_SIZE_PACKETLOGGER;
107128d6c99SMatthias Ringwald             break;
108128d6c99SMatthias Ringwald         default:
109128d6c99SMatthias Ringwald             return;
110128d6c99SMatthias Ringwald     }
111128d6c99SMatthias Ringwald 
112128d6c99SMatthias Ringwald #if (SEGGER_RTT_PACKETLOG_MODE == SEGGER_RTT_MODE_NO_BLOCK_SKIP)
113128d6c99SMatthias Ringwald     // check available space in buffer to avoid writing header but not packet
114128d6c99SMatthias Ringwald     unsigned space_free = SEGGER_RTT_GetAvailWriteSpace(SEGGER_RTT_PACKETLOG_CHANNEL);
115128d6c99SMatthias Ringwald     if ((header_len + len) > space_free) {
116128d6c99SMatthias Ringwald         rtt_packet_skipped = true;
117128d6c99SMatthias Ringwald         return;
118128d6c99SMatthias Ringwald     }
119128d6c99SMatthias Ringwald #endif
120128d6c99SMatthias Ringwald 
121128d6c99SMatthias Ringwald     // write complete header and payload
122128d6c99SMatthias Ringwald     SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, &header, header_len);
123128d6c99SMatthias Ringwald     SEGGER_RTT_Write(SEGGER_RTT_PACKETLOG_CHANNEL, packet, len);
124128d6c99SMatthias Ringwald }
125128d6c99SMatthias Ringwald 
hci_dump_segger_rtt_binary_log_message(int log_level,const char * format,va_list argptr)1262d17d4c0SMatthias Ringwald static void hci_dump_segger_rtt_binary_log_message(int log_level, const char * format, va_list argptr){
1272d17d4c0SMatthias Ringwald     UNUSED(log_level);
128128d6c99SMatthias Ringwald     if (dump_format == HCI_DUMP_INVALID) return;
129128d6c99SMatthias Ringwald     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
130128d6c99SMatthias Ringwald     hci_dump_segger_rtt_binary_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
131128d6c99SMatthias Ringwald }
132128d6c99SMatthias Ringwald 
133128d6c99SMatthias Ringwald //
134128d6c99SMatthias Ringwald 
hci_dump_segger_rtt_binary_open(hci_dump_format_t format)135128d6c99SMatthias Ringwald void hci_dump_segger_rtt_binary_open(hci_dump_format_t format){
136128d6c99SMatthias Ringwald     btstack_assert(format == HCI_DUMP_BLUEZ || format == HCI_DUMP_PACKETLOGGER);
137128d6c99SMatthias Ringwald     dump_format = format;
138128d6c99SMatthias Ringwald     SEGGER_RTT_ConfigUpBuffer(SEGGER_RTT_PACKETLOG_CHANNEL, "hci_dump", &segger_rtt_packetlog_buffer[0], SEGGER_RTT_PACKETLOG_BUFFER_SIZE, SEGGER_RTT_PACKETLOG_MODE);
139128d6c99SMatthias Ringwald }
140128d6c99SMatthias Ringwald 
hci_dump_segger_rtt_binary_get_instance(void)141128d6c99SMatthias Ringwald const hci_dump_t * hci_dump_segger_rtt_binary_get_instance(void){
142128d6c99SMatthias Ringwald     static const hci_dump_t hci_dump_instance = {
143128d6c99SMatthias Ringwald         // void (*reset)(void);
144128d6c99SMatthias Ringwald         NULL,
145128d6c99SMatthias Ringwald         // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
146128d6c99SMatthias Ringwald         &hci_dump_segger_rtt_binary_log_packet,
147128d6c99SMatthias Ringwald         // void (*log_message)(int log_level, const char * format, va_list argptr);
148128d6c99SMatthias Ringwald         &hci_dump_segger_rtt_binary_log_message,
149128d6c99SMatthias Ringwald     };
150128d6c99SMatthias Ringwald     return &hci_dump_instance;
151128d6c99SMatthias Ringwald }
152*da39cc4eSMatthias Ringwald 
153*da39cc4eSMatthias Ringwald #endif
154*da39cc4eSMatthias Ringwald 
155