xref: /btstack/src/hci_dump.c (revision 8a37b10a01d3c093626a0c12428d8301ced5552e)
179662672Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
3979662672Smatthias.ringwald  *  hci_dump.c
4079662672Smatthias.ringwald  *
41fe1ed1b8Smatthias.ringwald  *  Dump HCI trace in various formats:
42fe1ed1b8Smatthias.ringwald  *
43fe1ed1b8Smatthias.ringwald  *  - BlueZ's hcidump format
44fe1ed1b8Smatthias.ringwald  *  - Apple's PacketLogger
45fe1ed1b8Smatthias.ringwald  *  - stdout hexdump
4679662672Smatthias.ringwald  *
4779662672Smatthias.ringwald  *  Created by Matthias Ringwald on 5/26/09.
4879662672Smatthias.ringwald  */
4979662672Smatthias.ringwald 
50bde315ceS[email protected] #include "btstack-config.h"
51a1d7dd1fSmatthias.ringwald 
5279662672Smatthias.ringwald #include "hci_dump.h"
5379662672Smatthias.ringwald #include "hci.h"
54c6448b67Smatthias.ringwald #include "hci_transport.h"
55a1d7dd1fSmatthias.ringwald #include <btstack/hci_cmds.h>
561df3b679S[email protected] #include <btstack/run_loop.h>
57198a9e1bS[email protected] #include <stdio.h>
5879662672Smatthias.ringwald 
5968e27c0fSmatthias.ringwald #ifndef EMBEDDED
60d5ea8924S[email protected] #include <fcntl.h>        // open
6179662672Smatthias.ringwald #include <unistd.h>       // write
628adf0ddaSmatthias.ringwald #include <time.h>
638b658ebcSmatthias.ringwald #include <sys/time.h>     // for timestamps
64c7b9c559Smatthias.ringwald #include <sys/stat.h>     // for mode flags
65a1d7dd1fSmatthias.ringwald #include <stdarg.h>       // for va_list
6668e27c0fSmatthias.ringwald #endif
6779662672Smatthias.ringwald 
688b658ebcSmatthias.ringwald // BLUEZ hcidump
698b658ebcSmatthias.ringwald typedef struct {
708b658ebcSmatthias.ringwald 	uint16_t	len;
718b658ebcSmatthias.ringwald 	uint8_t		in;
728b658ebcSmatthias.ringwald 	uint8_t		pad;
738b658ebcSmatthias.ringwald 	uint32_t	ts_sec;
748b658ebcSmatthias.ringwald 	uint32_t	ts_usec;
758b658ebcSmatthias.ringwald     uint8_t     packet_type;
766c5c6faaSmatthias.ringwald }
776c5c6faaSmatthias.ringwald #ifdef __GNUC__
786c5c6faaSmatthias.ringwald __attribute__ ((packed))
796c5c6faaSmatthias.ringwald #endif
806c5c6faaSmatthias.ringwald hcidump_hdr;
8179662672Smatthias.ringwald 
828b658ebcSmatthias.ringwald // APPLE PacketLogger
838b658ebcSmatthias.ringwald typedef struct {
848b658ebcSmatthias.ringwald 	uint32_t	len;
858b658ebcSmatthias.ringwald 	uint32_t	ts_sec;
868b658ebcSmatthias.ringwald 	uint32_t	ts_usec;
872df12229Smatthias.ringwald 	uint8_t		type;   // 0xfc for note
886c5c6faaSmatthias.ringwald }
896c5c6faaSmatthias.ringwald #ifdef __GNUC__
906c5c6faaSmatthias.ringwald __attribute__ ((packed))
916c5c6faaSmatthias.ringwald #endif
926c5c6faaSmatthias.ringwald pktlog_hdr;
938b658ebcSmatthias.ringwald 
948b658ebcSmatthias.ringwald static int dump_file = -1;
95198a9e1bS[email protected] #ifndef EMBEDDED
968b658ebcSmatthias.ringwald static int dump_format;
978b658ebcSmatthias.ringwald static hcidump_hdr header_bluez;
988b658ebcSmatthias.ringwald static pktlog_hdr  header_packetlogger;
998adf0ddaSmatthias.ringwald static char time_string[40];
1002992c131Smatthias.ringwald static int  max_nr_packets = -1;
1019ae0c346Smatthias.ringwald static int  nr_packets = 0;
102a1d7dd1fSmatthias.ringwald static char log_message_buffer[256];
10368e27c0fSmatthias.ringwald #endif
1048b658ebcSmatthias.ringwald 
105*8a37b10aSMatthias Ringwald // levels: debug, info, error
106*8a37b10aSMatthias Ringwald static int log_level_enabled[3] = { 1, 1, 1};
107*8a37b10aSMatthias Ringwald 
108a225073eS[email protected] void hci_dump_open(const char *filename, hci_dump_format_t format){
109198a9e1bS[email protected] #ifdef EMBEDDED
110198a9e1bS[email protected]     dump_file = 1;
111198a9e1bS[email protected] #else
1128b658ebcSmatthias.ringwald     dump_format = format;
1138adf0ddaSmatthias.ringwald     if (dump_format == HCI_DUMP_STDOUT) {
1148adf0ddaSmatthias.ringwald         dump_file = fileno(stdout);
1158adf0ddaSmatthias.ringwald     } else {
116b52eaea5S[email protected] #ifdef _WIN32
117b52eaea5S[email protected]         dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
118b52eaea5S[email protected] #else
1198adf0ddaSmatthias.ringwald         dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
120b52eaea5S[email protected] #endif
12179662672Smatthias.ringwald     }
12268e27c0fSmatthias.ringwald #endif
123d9659922Smatthias.ringwald }
12479662672Smatthias.ringwald 
1257c5f7483Smatthias.ringwald #ifndef EMBEDDED
1262992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets){
1272992c131Smatthias.ringwald     max_nr_packets = packets;
1282992c131Smatthias.ringwald }
1297c5f7483Smatthias.ringwald #endif
1302992c131Smatthias.ringwald 
131*8a37b10aSMatthias Ringwald static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
132198a9e1bS[email protected]     switch (packet_type){
133198a9e1bS[email protected]         case HCI_COMMAND_DATA_PACKET:
134198a9e1bS[email protected]             printf("CMD => ");
135198a9e1bS[email protected]             break;
136198a9e1bS[email protected]         case HCI_EVENT_PACKET:
137198a9e1bS[email protected]             printf("EVT <= ");
138198a9e1bS[email protected]             break;
139198a9e1bS[email protected]         case HCI_ACL_DATA_PACKET:
140198a9e1bS[email protected]             if (in) {
141198a9e1bS[email protected]                 printf("ACL <= ");
142198a9e1bS[email protected]             } else {
143198a9e1bS[email protected]                 printf("ACL => ");
144198a9e1bS[email protected]             }
145198a9e1bS[email protected]             break;
146198a9e1bS[email protected]         case LOG_MESSAGE_PACKET:
147198a9e1bS[email protected]             printf("LOG -- %s\n", (char*) packet);
148198a9e1bS[email protected]             return;
149198a9e1bS[email protected]         default:
150198a9e1bS[email protected]             return;
151198a9e1bS[email protected]     }
152198a9e1bS[email protected]     printf_hexdump(packet, len);
153198a9e1bS[email protected] }
154198a9e1bS[email protected] 
15579662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
15668e27c0fSmatthias.ringwald 
1578b658ebcSmatthias.ringwald     if (dump_file < 0) return; // not activated yet
1588b658ebcSmatthias.ringwald 
159198a9e1bS[email protected] #ifdef EMBEDDED
1601df3b679S[email protected] // #ifdef HAVE_TICK
1611df3b679S[email protected] //     uint32_t time_ms = embedded_get_time_ms();
1621df3b679S[email protected] //     printf("[%06u] ", time_ms);
1631df3b679S[email protected] // #endif
164198a9e1bS[email protected]     printf_packet(packet_type, in, packet, len);
165198a9e1bS[email protected] #else
1662992c131Smatthias.ringwald     // don't grow bigger than max_nr_packets
1672992c131Smatthias.ringwald     if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
1682992c131Smatthias.ringwald         if (nr_packets >= max_nr_packets){
1692992c131Smatthias.ringwald             lseek(dump_file, 0, SEEK_SET);
1702992c131Smatthias.ringwald             ftruncate(dump_file, 0);
1712992c131Smatthias.ringwald             nr_packets = 0;
1722992c131Smatthias.ringwald         }
1732992c131Smatthias.ringwald         nr_packets++;
1742992c131Smatthias.ringwald     }
1752992c131Smatthias.ringwald 
1768b658ebcSmatthias.ringwald     // get time
1778b658ebcSmatthias.ringwald     struct timeval curr_time;
1788adf0ddaSmatthias.ringwald     struct tm* ptm;
1798b658ebcSmatthias.ringwald     gettimeofday(&curr_time, NULL);
180d5ea8924S[email protected]     time_t curr_time_secs = curr_time.tv_sec;
1818b658ebcSmatthias.ringwald 
1828b658ebcSmatthias.ringwald     switch (dump_format){
183a9cf4d77S[email protected]         case HCI_DUMP_STDOUT: {
1848adf0ddaSmatthias.ringwald             /* Obtain the time of day, and convert it to a tm struct. */
185d5ea8924S[email protected]             ptm = localtime (&curr_time_secs);
1868adf0ddaSmatthias.ringwald             /* Format the date and time, down to a single second. */
1878adf0ddaSmatthias.ringwald             strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
1888adf0ddaSmatthias.ringwald             /* Compute milliseconds from microseconds. */
1898adf0ddaSmatthias.ringwald             uint16_t milliseconds = curr_time.tv_usec / 1000;
1908adf0ddaSmatthias.ringwald             /* Print the formatted time, in seconds, followed by a decimal point
1918adf0ddaSmatthias.ringwald              and the milliseconds. */
1928adf0ddaSmatthias.ringwald             printf ("%s.%03u] ", time_string, milliseconds);
193198a9e1bS[email protected]             printf_packet(packet_type, in, packet, len);
1948adf0ddaSmatthias.ringwald             break;
195a9cf4d77S[email protected]         }
1960d79c710Smatthias.ringwald 
1978b658ebcSmatthias.ringwald         case HCI_DUMP_BLUEZ:
1988b658ebcSmatthias.ringwald             bt_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len);
1998b658ebcSmatthias.ringwald             header_bluez.in  = in;
2008b658ebcSmatthias.ringwald             header_bluez.pad = 0;
2018b658ebcSmatthias.ringwald             bt_store_32( (uint8_t *) &header_bluez.ts_sec,  0, curr_time.tv_sec);
2028b658ebcSmatthias.ringwald             bt_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec);
2038b658ebcSmatthias.ringwald             header_bluez.packet_type = packet_type;
2048b658ebcSmatthias.ringwald             write (dump_file, &header_bluez, sizeof(hcidump_hdr) );
20579662672Smatthias.ringwald             write (dump_file, packet, len );
2068b658ebcSmatthias.ringwald             break;
2070d79c710Smatthias.ringwald 
2088b658ebcSmatthias.ringwald         case HCI_DUMP_PACKETLOGGER:
209b52eaea5S[email protected]             net_store_32( (uint8_t *) &header_packetlogger, 0, sizeof(pktlog_hdr) - 4 + len);
210b52eaea5S[email protected]             net_store_32( (uint8_t *) &header_packetlogger, 4, curr_time.tv_sec);
211b52eaea5S[email protected]             net_store_32( (uint8_t *) &header_packetlogger, 8, curr_time.tv_usec);
2128b658ebcSmatthias.ringwald             switch (packet_type){
2138b658ebcSmatthias.ringwald                 case HCI_COMMAND_DATA_PACKET:
2148b658ebcSmatthias.ringwald                     header_packetlogger.type = 0x00;
2158b658ebcSmatthias.ringwald                     break;
2168b658ebcSmatthias.ringwald                 case HCI_ACL_DATA_PACKET:
2178b658ebcSmatthias.ringwald                     if (in) {
2188b658ebcSmatthias.ringwald                         header_packetlogger.type = 0x03;
2198b658ebcSmatthias.ringwald                     } else {
2208b658ebcSmatthias.ringwald                         header_packetlogger.type = 0x02;
2218b658ebcSmatthias.ringwald                     }
2228b658ebcSmatthias.ringwald                     break;
2238d675e3dS[email protected]                 case HCI_SCO_DATA_PACKET:
2248d675e3dS[email protected]                     if (in) {
2258d675e3dS[email protected]                         header_packetlogger.type = 0x09;
2268d675e3dS[email protected]                     } else {
2278d675e3dS[email protected]                         header_packetlogger.type = 0x08;
2288d675e3dS[email protected]                     }
2298d675e3dS[email protected]                     break;
2308b658ebcSmatthias.ringwald                 case HCI_EVENT_PACKET:
2318b658ebcSmatthias.ringwald                     header_packetlogger.type = 0x01;
2328b658ebcSmatthias.ringwald                     break;
2330d79c710Smatthias.ringwald                 case LOG_MESSAGE_PACKET:
2340d79c710Smatthias.ringwald                     header_packetlogger.type = 0xfc;
2350d79c710Smatthias.ringwald                     break;
2368b658ebcSmatthias.ringwald                 default:
2378b658ebcSmatthias.ringwald                     return;
2388b658ebcSmatthias.ringwald             }
2398b658ebcSmatthias.ringwald             write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) );
2408b658ebcSmatthias.ringwald             write (dump_file, packet, len );
2410d79c710Smatthias.ringwald             break;
2420d79c710Smatthias.ringwald 
2430d79c710Smatthias.ringwald         default:
2440d79c710Smatthias.ringwald             break;
2458b658ebcSmatthias.ringwald     }
24668e27c0fSmatthias.ringwald #endif
24779662672Smatthias.ringwald }
24879662672Smatthias.ringwald 
249*8a37b10aSMatthias Ringwald static int hci_dump_log_level_active(int log_level){
250*8a37b10aSMatthias Ringwald     if (log_level < 0) return 0;
251*8a37b10aSMatthias Ringwald     if (log_level > LOG_LEVEL_ERROR) return 0;
252*8a37b10aSMatthias Ringwald     return log_level_enabled[log_level];
253*8a37b10aSMatthias Ringwald }
254*8a37b10aSMatthias Ringwald 
255*8a37b10aSMatthias Ringwald void hci_dump_log(int log_level, const char * format, ...){
256*8a37b10aSMatthias Ringwald     if (!hci_dump_log_level_active(log_level)) return;
257a1d7dd1fSmatthias.ringwald     va_list argptr;
258a1d7dd1fSmatthias.ringwald     va_start(argptr, format);
2591df3b679S[email protected] #ifdef EMBEDDED
2601df3b679S[email protected]     printf("LOG -- ");
2611df3b679S[email protected]     vprintf(format, argptr);
2621fd51e45S[email protected]     printf("\n");
2631df3b679S[email protected] #else
264a1d7dd1fSmatthias.ringwald     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
265a1d7dd1fSmatthias.ringwald     hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
266a1d7dd1fSmatthias.ringwald #endif
2671df3b679S[email protected]     va_end(argptr);
268a1d7dd1fSmatthias.ringwald }
269a1d7dd1fSmatthias.ringwald 
27020ea11b9S[email protected] #ifdef __AVR__
271*8a37b10aSMatthias Ringwald void hci_dump_log_P(int log_level, PGM_P format, ...){
272*8a37b10aSMatthias Ringwald     if (!hci_dump_log_level_active(log_level)) return;
27320ea11b9S[email protected]     va_list argptr;
27420ea11b9S[email protected]     va_start(argptr, format);
27520ea11b9S[email protected]     printf_P(PSTR("LOG -- "));
27620ea11b9S[email protected]     vfprintf_P(stdout, format, argptr);
27720ea11b9S[email protected]     printf_P(PSTR("\n"));
27820ea11b9S[email protected]     va_end(argptr);
27920ea11b9S[email protected] }
28020ea11b9S[email protected] #endif
28120ea11b9S[email protected] 
28271de195eSMatthias Ringwald void hci_dump_close(void){
28368e27c0fSmatthias.ringwald #ifndef EMBEDDED
28479662672Smatthias.ringwald     close(dump_file);
285d9659922Smatthias.ringwald     dump_file = -1;
28668e27c0fSmatthias.ringwald #endif
28779662672Smatthias.ringwald }
28879662672Smatthias.ringwald 
289*8a37b10aSMatthias Ringwald void hci_dump_enable_log_level(int log_level, int enable){
290*8a37b10aSMatthias Ringwald     if (log_level < 0) return;
291*8a37b10aSMatthias Ringwald     if (log_level > LOG_LEVEL_ERROR) return;
292*8a37b10aSMatthias Ringwald     log_level_enabled[log_level] = enable;
293*8a37b10aSMatthias Ringwald }
294*8a37b10aSMatthias Ringwald 
295