1128d6c99SMatthias Ringwald /*
2128d6c99SMatthias Ringwald * Copyright (C) 2014 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 * [email protected]
35128d6c99SMatthias Ringwald *
36128d6c99SMatthias Ringwald */
37128d6c99SMatthias Ringwald
38128d6c99SMatthias Ringwald #define BTSTACK_FILE__ "hci_dump_embedded_stdout.c"
39128d6c99SMatthias Ringwald
40128d6c99SMatthias Ringwald /*
41128d6c99SMatthias Ringwald * Dump HCI trace on stdout
42128d6c99SMatthias Ringwald */
43128d6c99SMatthias Ringwald
44128d6c99SMatthias Ringwald #include "btstack_config.h"
45128d6c99SMatthias Ringwald
46128d6c99SMatthias Ringwald #include "hci_dump_embedded_stdout.h"
47128d6c99SMatthias Ringwald #include "btstack_run_loop.h"
48128d6c99SMatthias Ringwald #include "hci.h"
49128d6c99SMatthias Ringwald
50128d6c99SMatthias Ringwald #include <stdio.h>
51128d6c99SMatthias Ringwald
52128d6c99SMatthias Ringwald #ifndef ENABLE_PRINTF_HEXDUMP
53128d6c99SMatthias Ringwald #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"
54128d6c99SMatthias Ringwald #endif
55128d6c99SMatthias Ringwald
56128d6c99SMatthias Ringwald static char log_message_buffer[256];
57128d6c99SMatthias Ringwald
hci_dump_embedded_stdout_timestamp(void)58128d6c99SMatthias Ringwald static void hci_dump_embedded_stdout_timestamp(void){
59128d6c99SMatthias Ringwald uint32_t time_ms = btstack_run_loop_get_time_ms();
60128d6c99SMatthias Ringwald int seconds = time_ms / 1000u;
61128d6c99SMatthias Ringwald int minutes = seconds / 60;
62128d6c99SMatthias Ringwald unsigned int hours = minutes / 60;
63128d6c99SMatthias Ringwald
64128d6c99SMatthias Ringwald uint16_t p_ms = time_ms - (seconds * 1000u);
65128d6c99SMatthias Ringwald uint16_t p_seconds = seconds - (minutes * 60);
66128d6c99SMatthias Ringwald uint16_t p_minutes = minutes - (hours * 60u);
67128d6c99SMatthias Ringwald printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms);
68128d6c99SMatthias Ringwald }
69128d6c99SMatthias Ringwald
hci_dump_embedded_stdout_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)70128d6c99SMatthias Ringwald static void hci_dump_embedded_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
71128d6c99SMatthias Ringwald switch (packet_type){
72128d6c99SMatthias Ringwald case HCI_COMMAND_DATA_PACKET:
73128d6c99SMatthias Ringwald printf("CMD => ");
74128d6c99SMatthias Ringwald break;
75128d6c99SMatthias Ringwald case HCI_EVENT_PACKET:
76128d6c99SMatthias Ringwald printf("EVT <= ");
77128d6c99SMatthias Ringwald break;
78128d6c99SMatthias Ringwald case HCI_ACL_DATA_PACKET:
79*1b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ACL
80*1b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ACL){
81*1b5f4ae3SMatthias Ringwald printf("LOG -- ACL %s, size %u\n", in ? "in" : "out", len);
82*1b5f4ae3SMatthias Ringwald return;
83*1b5f4ae3SMatthias Ringwald }
84*1b5f4ae3SMatthias Ringwald #endif
85128d6c99SMatthias Ringwald if (in) {
86128d6c99SMatthias Ringwald printf("ACL <= ");
87128d6c99SMatthias Ringwald } else {
88128d6c99SMatthias Ringwald printf("ACL => ");
89128d6c99SMatthias Ringwald }
90128d6c99SMatthias Ringwald break;
91128d6c99SMatthias Ringwald case HCI_SCO_DATA_PACKET:
92*1b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_SCO
93*1b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_SCO){
94*1b5f4ae3SMatthias Ringwald printf("LOG -- SCO %s, size %u\n", in ? "in" : "out", len);
95*1b5f4ae3SMatthias Ringwald return;
96*1b5f4ae3SMatthias Ringwald }
97*1b5f4ae3SMatthias Ringwald #endif
98128d6c99SMatthias Ringwald if (in) {
99128d6c99SMatthias Ringwald printf("SCO <= ");
100128d6c99SMatthias Ringwald } else {
101128d6c99SMatthias Ringwald printf("SCO => ");
102128d6c99SMatthias Ringwald }
103128d6c99SMatthias Ringwald break;
1045a831baeSMatthias Ringwald case HCI_ISO_DATA_PACKET:
105*1b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ISO
106*1b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ISO){
107*1b5f4ae3SMatthias Ringwald printf("LOG -- ISO %s, size %u\n", in ? "in" : "out", len);
108*1b5f4ae3SMatthias Ringwald return;
109*1b5f4ae3SMatthias Ringwald }
110*1b5f4ae3SMatthias Ringwald #endif
1115a831baeSMatthias Ringwald if (in) {
1125a831baeSMatthias Ringwald printf("ISO <= ");
1135a831baeSMatthias Ringwald } else {
1145a831baeSMatthias Ringwald printf("ISO => ");
1155a831baeSMatthias Ringwald }
1165a831baeSMatthias Ringwald break;
117128d6c99SMatthias Ringwald case LOG_MESSAGE_PACKET:
118128d6c99SMatthias Ringwald printf("LOG -- %s\n", (char*) packet);
119128d6c99SMatthias Ringwald return;
120128d6c99SMatthias Ringwald default:
121128d6c99SMatthias Ringwald return;
122128d6c99SMatthias Ringwald }
123128d6c99SMatthias Ringwald printf_hexdump(packet, len);
124128d6c99SMatthias Ringwald }
125128d6c99SMatthias Ringwald
hci_dump_embedded_stdout_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)126128d6c99SMatthias Ringwald static void hci_dump_embedded_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
127128d6c99SMatthias Ringwald hci_dump_embedded_stdout_timestamp();
128128d6c99SMatthias Ringwald hci_dump_embedded_stdout_packet(packet_type, in, packet, len);
129128d6c99SMatthias Ringwald }
130128d6c99SMatthias Ringwald
hci_dump_embedded_stdout_log_message(int log_level,const char * format,va_list argptr)1312d17d4c0SMatthias Ringwald static void hci_dump_embedded_stdout_log_message(int log_level, const char * format, va_list argptr){
1322d17d4c0SMatthias Ringwald UNUSED(log_level);
133128d6c99SMatthias Ringwald int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
134128d6c99SMatthias Ringwald hci_dump_embedded_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
135128d6c99SMatthias Ringwald }
136128d6c99SMatthias Ringwald
137128d6c99SMatthias Ringwald #ifdef __AVR__
hci_dump_embedded_stdout_log_message_P(int log_level,PGM_P * format,va_list argptr)138128d6c99SMatthias Ringwald void hci_dump_embedded_stdout_log_message_P(int log_level, PGM_P * format, va_list argptr){
139128d6c99SMatthias Ringwald hci_dump_embedded_stdout_timestamp();
140128d6c99SMatthias Ringwald printf_P(PSTR("LOG -- "));
141128d6c99SMatthias Ringwald vfprintf_P(stdout, format, argptr);
142128d6c99SMatthias Ringwald printf_P(PSTR("\n"));
143128d6c99SMatthias Ringwald }
144128d6c99SMatthias Ringwald #endif
145128d6c99SMatthias Ringwald
hci_dump_embedded_stdout_get_instance(void)146128d6c99SMatthias Ringwald const hci_dump_t * hci_dump_embedded_stdout_get_instance(void){
147128d6c99SMatthias Ringwald static const hci_dump_t hci_dump_instance = {
148128d6c99SMatthias Ringwald // void (*reset)(void);
149128d6c99SMatthias Ringwald NULL,
150128d6c99SMatthias Ringwald // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
151128d6c99SMatthias Ringwald &hci_dump_embedded_stdout_log_packet,
152128d6c99SMatthias Ringwald // void (*log_message)(int log_level, const char * format, va_list argptr);
153128d6c99SMatthias Ringwald &hci_dump_embedded_stdout_log_message,
154128d6c99SMatthias Ringwald #ifdef __AVR__ \
155128d6c99SMatthias Ringwald // void (*log_message_P)(int log_level, PGM_P * format, va_list argptr);
156128d6c99SMatthias Ringwald &hci_dump_embedded_stdout_log_message_P,
157128d6c99SMatthias Ringwald #endif
158128d6c99SMatthias Ringwald };
159128d6c99SMatthias Ringwald return &hci_dump_instance;
160128d6c99SMatthias Ringwald }
161