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 MATTHIAS 24 * RINGWALD 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 static void hci_dump_segger_rtt_hexdump(const void *data, int size){ 53 char buffer[4]; 54 buffer[2] = ' '; 55 buffer[3] = 0; 56 const uint8_t * ptr = (const uint8_t *) data; 57 while (size > 0){ 58 uint8_t byte = *ptr++; 59 buffer[0] = char_for_nibble(byte >> 4); 60 buffer[1] = char_for_nibble(byte & 0x0f); 61 SEGGER_RTT_Write(0, buffer, 3); 62 size--; 63 } 64 SEGGER_RTT_PutChar(0, '\n'); 65 } 66 67 static void hci_dump_segger_rtt_stdout_timestamp(void){ 68 uint32_t time_ms = btstack_run_loop_get_time_ms(); 69 int seconds = time_ms / 1000u; 70 int minutes = seconds / 60; 71 unsigned int hours = minutes / 60; 72 73 uint16_t p_ms = time_ms - (seconds * 1000u); 74 uint16_t p_seconds = seconds - (minutes * 60); 75 uint16_t p_minutes = minutes - (hours * 60u); 76 SEGGER_RTT_printf(0, "[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms); 77 } 78 79 static void hci_dump_segger_rtt_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){ 80 switch (packet_type){ 81 case HCI_COMMAND_DATA_PACKET: 82 SEGGER_RTT_printf(0, "CMD => "); 83 break; 84 case HCI_EVENT_PACKET: 85 SEGGER_RTT_printf(0, "EVT <= "); 86 break; 87 case HCI_ACL_DATA_PACKET: 88 if (in) { 89 SEGGER_RTT_printf(0, "ACL <= "); 90 } else { 91 SEGGER_RTT_printf(0, "ACL => "); 92 } 93 break; 94 case HCI_SCO_DATA_PACKET: 95 if (in) { 96 SEGGER_RTT_printf(0, "SCO <= "); 97 } else { 98 SEGGER_RTT_printf(0, "SCO => "); 99 } 100 break; 101 case LOG_MESSAGE_PACKET: 102 SEGGER_RTT_printf(0, "LOG -- %s\n", (char*) packet); 103 return; 104 default: 105 return; 106 } 107 hci_dump_segger_rtt_hexdump(packet, len); 108 } 109 110 static void hci_dump_segger_rtt_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { 111 hci_dump_segger_rtt_stdout_timestamp(); 112 hci_dump_segger_rtt_stdout_packet(packet_type, in, packet, len); 113 } 114 115 static void hci_dump_segger_rtt_stdout_log_message(const char * format, va_list argptr){ 116 hci_dump_segger_rtt_stdout_timestamp(); 117 SEGGER_RTT_printf(0, "LOG -- "); 118 SEGGER_RTT_vprintf(0, format, &argptr); 119 SEGGER_RTT_printf(0, "\n"); 120 } 121 122 const hci_dump_t * hci_dump_segger_rtt_stdout_get_instance(void){ 123 static const hci_dump_t hci_dump_instance = { 124 // void (*reset)(void); 125 NULL, 126 // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len); 127 &hci_dump_segger_rtt_stdout_log_packet, 128 // void (*log_message)(int log_level, const char * format, va_list argptr); 129 &hci_dump_segger_rtt_stdout_log_message, 130 }; 131 return &hci_dump_instance; 132 } 133