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_posix_stdout.c"
39128d6c99SMatthias Ringwald
40128d6c99SMatthias Ringwald /*
41128d6c99SMatthias Ringwald * Dump HCI trace on stdout
42128d6c99SMatthias Ringwald */
43128d6c99SMatthias Ringwald
44128d6c99SMatthias Ringwald #include "hci_dump.h"
45128d6c99SMatthias Ringwald #include "btstack_config.h"
46128d6c99SMatthias Ringwald #include "hci.h"
47128d6c99SMatthias Ringwald #include <time.h>
48128d6c99SMatthias Ringwald #include <sys/time.h> // for timestamps
49128d6c99SMatthias Ringwald #include "hci_cmd.h"
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 time_string[40];
57eb0d5579SMatthias Ringwald static char log_message_buffer[HCI_DUMP_MAX_MESSAGE_LEN];
58128d6c99SMatthias Ringwald
hci_dump_posix_stdout_timestamp(void)59128d6c99SMatthias Ringwald static void hci_dump_posix_stdout_timestamp(void){
60128d6c99SMatthias Ringwald struct tm* ptm;
61128d6c99SMatthias Ringwald struct timeval curr_time;
62128d6c99SMatthias Ringwald gettimeofday(&curr_time, NULL);
63128d6c99SMatthias Ringwald time_t curr_time_secs = curr_time.tv_sec;
64128d6c99SMatthias Ringwald /* Obtain the time of day, and convert it to a tm struct. */
65128d6c99SMatthias Ringwald ptm = localtime (&curr_time_secs);
66128d6c99SMatthias Ringwald /* assert localtime was successful */
67128d6c99SMatthias Ringwald if (!ptm) return;
68128d6c99SMatthias Ringwald /* Format the date and time, down to a single second. */
69128d6c99SMatthias Ringwald strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
70128d6c99SMatthias Ringwald /* Compute milliseconds from microseconds. */
71128d6c99SMatthias Ringwald uint16_t milliseconds = curr_time.tv_usec / 1000;
72128d6c99SMatthias Ringwald /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */
73128d6c99SMatthias Ringwald printf ("%s.%03u] ", time_string, milliseconds);
74128d6c99SMatthias Ringwald }
75128d6c99SMatthias Ringwald
hci_dump_posix_stdout_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)76128d6c99SMatthias Ringwald static void hci_dump_posix_stdout_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
77128d6c99SMatthias Ringwald switch (packet_type){
78128d6c99SMatthias Ringwald case HCI_COMMAND_DATA_PACKET:
79128d6c99SMatthias Ringwald printf("CMD => ");
80128d6c99SMatthias Ringwald break;
81128d6c99SMatthias Ringwald case HCI_EVENT_PACKET:
82128d6c99SMatthias Ringwald printf("EVT <= ");
83128d6c99SMatthias Ringwald break;
84128d6c99SMatthias Ringwald case HCI_ACL_DATA_PACKET:
851b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ACL
861b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ACL){
871b5f4ae3SMatthias Ringwald printf("LOG -- ACL %s, size %u\n", in ? "in" : "out", len);
881b5f4ae3SMatthias Ringwald return;
891b5f4ae3SMatthias Ringwald }
901b5f4ae3SMatthias Ringwald #endif
91128d6c99SMatthias Ringwald if (in) {
92128d6c99SMatthias Ringwald printf("ACL <= ");
93128d6c99SMatthias Ringwald } else {
94128d6c99SMatthias Ringwald printf("ACL => ");
95128d6c99SMatthias Ringwald }
96128d6c99SMatthias Ringwald break;
97128d6c99SMatthias Ringwald case HCI_SCO_DATA_PACKET:
981b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_SCO
991b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_SCO){
1001b5f4ae3SMatthias Ringwald printf("LOG -- SCO %s, size %u\n", in ? "in" : "out", len);
1011b5f4ae3SMatthias Ringwald return;
1021b5f4ae3SMatthias Ringwald }
1031b5f4ae3SMatthias Ringwald #endif
104128d6c99SMatthias Ringwald if (in) {
105128d6c99SMatthias Ringwald printf("SCO <= ");
106128d6c99SMatthias Ringwald } else {
107128d6c99SMatthias Ringwald printf("SCO => ");
108128d6c99SMatthias Ringwald }
109128d6c99SMatthias Ringwald break;
1105a831baeSMatthias Ringwald case HCI_ISO_DATA_PACKET:
1111b5f4ae3SMatthias Ringwald #ifdef HCI_DUMP_STDOUT_MAX_SIZE_ISO
1121b5f4ae3SMatthias Ringwald if (len > HCI_DUMP_STDOUT_MAX_SIZE_ISO){
1131b5f4ae3SMatthias Ringwald printf("LOG -- ISO %s, size %u\n", in ? "in" : "out", len);
1141b5f4ae3SMatthias Ringwald return;
1151b5f4ae3SMatthias Ringwald }
1161b5f4ae3SMatthias Ringwald #endif
1175a831baeSMatthias Ringwald if (in) {
1185a831baeSMatthias Ringwald printf("ISO <= ");
1195a831baeSMatthias Ringwald } else {
1205a831baeSMatthias Ringwald printf("ISO => ");
1215a831baeSMatthias Ringwald }
1225a831baeSMatthias Ringwald break;
123128d6c99SMatthias Ringwald case LOG_MESSAGE_PACKET:
124128d6c99SMatthias Ringwald printf("LOG -- %s\n", (char*) packet);
125128d6c99SMatthias Ringwald return;
126128d6c99SMatthias Ringwald default:
127128d6c99SMatthias Ringwald return;
128128d6c99SMatthias Ringwald }
129128d6c99SMatthias Ringwald printf_hexdump(packet, len);
130128d6c99SMatthias Ringwald }
131128d6c99SMatthias Ringwald
hci_dump_posix_posix_stdout_log_packet(uint8_t packet_type,uint8_t in,uint8_t * packet,uint16_t len)132128d6c99SMatthias Ringwald static void hci_dump_posix_posix_stdout_log_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
133128d6c99SMatthias Ringwald hci_dump_posix_stdout_timestamp();
134128d6c99SMatthias Ringwald hci_dump_posix_stdout_packet(packet_type, in, packet, len);
135128d6c99SMatthias Ringwald }
136128d6c99SMatthias Ringwald
hci_dump_posix_stdout_log_message(int log_level,const char * format,va_list argptr)1372d17d4c0SMatthias Ringwald static void hci_dump_posix_stdout_log_message(int log_level, const char * format, va_list argptr){
1382d17d4c0SMatthias Ringwald UNUSED(log_level);
139*64b4329fSMatthias Ringwald int full_string_len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
140*64b4329fSMatthias Ringwald int len = btstack_min(sizeof(log_message_buffer), full_string_len);
141128d6c99SMatthias Ringwald hci_dump_posix_posix_stdout_log_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
142128d6c99SMatthias Ringwald }
143128d6c99SMatthias Ringwald
hci_dump_posix_stdout_get_instance(void)144128d6c99SMatthias Ringwald const hci_dump_t * hci_dump_posix_stdout_get_instance(void){
145128d6c99SMatthias Ringwald static const hci_dump_t hci_dump_instance = {
146128d6c99SMatthias Ringwald // void (*reset)(void);
147128d6c99SMatthias Ringwald NULL,
148128d6c99SMatthias Ringwald // void (*log_packet)(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len);
149128d6c99SMatthias Ringwald &hci_dump_posix_posix_stdout_log_packet,
150128d6c99SMatthias Ringwald // void (*log_message)(int log_level, const char * format, va_list argptr);
151128d6c99SMatthias Ringwald &hci_dump_posix_stdout_log_message,
152128d6c99SMatthias Ringwald };
153128d6c99SMatthias Ringwald return &hci_dump_instance;
154128d6c99SMatthias Ringwald }
155