xref: /btstack/src/hci_dump.c (revision b52eaea5b41cc190d24dfd72c8c9f8fdcc00bad9)
179662672Smatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
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  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
3879662672Smatthias.ringwald  *  hci_dump.c
3979662672Smatthias.ringwald  *
40fe1ed1b8Smatthias.ringwald  *  Dump HCI trace in various formats:
41fe1ed1b8Smatthias.ringwald  *
42fe1ed1b8Smatthias.ringwald  *  - BlueZ's hcidump format
43fe1ed1b8Smatthias.ringwald  *  - Apple's PacketLogger
44fe1ed1b8Smatthias.ringwald  *  - stdout hexdump
4579662672Smatthias.ringwald  *
4679662672Smatthias.ringwald  *  Created by Matthias Ringwald on 5/26/09.
4779662672Smatthias.ringwald  */
4879662672Smatthias.ringwald 
49bde315ceS[email protected] #include "btstack-config.h"
50a1d7dd1fSmatthias.ringwald 
5179662672Smatthias.ringwald #include "hci_dump.h"
5279662672Smatthias.ringwald #include "hci.h"
53c6448b67Smatthias.ringwald #include "hci_transport.h"
54a1d7dd1fSmatthias.ringwald #include <btstack/hci_cmds.h>
55198a9e1bS[email protected] #include <stdio.h>
5679662672Smatthias.ringwald 
5768e27c0fSmatthias.ringwald #ifndef EMBEDDED
58d5ea8924S[email protected] #include <fcntl.h>        // open
5979662672Smatthias.ringwald #include <unistd.h>       // write
608adf0ddaSmatthias.ringwald #include <time.h>
618b658ebcSmatthias.ringwald #include <sys/time.h>     // for timestamps
62c7b9c559Smatthias.ringwald #include <sys/stat.h>     // for mode flags
63a1d7dd1fSmatthias.ringwald #include <stdarg.h>       // for va_list
6468e27c0fSmatthias.ringwald #endif
6579662672Smatthias.ringwald 
668b658ebcSmatthias.ringwald // BLUEZ hcidump
678b658ebcSmatthias.ringwald typedef struct {
688b658ebcSmatthias.ringwald 	uint16_t	len;
698b658ebcSmatthias.ringwald 	uint8_t		in;
708b658ebcSmatthias.ringwald 	uint8_t		pad;
718b658ebcSmatthias.ringwald 	uint32_t	ts_sec;
728b658ebcSmatthias.ringwald 	uint32_t	ts_usec;
738b658ebcSmatthias.ringwald     uint8_t     packet_type;
746c5c6faaSmatthias.ringwald }
756c5c6faaSmatthias.ringwald #ifdef __GNUC__
766c5c6faaSmatthias.ringwald __attribute__ ((packed))
776c5c6faaSmatthias.ringwald #endif
786c5c6faaSmatthias.ringwald hcidump_hdr;
7979662672Smatthias.ringwald 
808b658ebcSmatthias.ringwald // APPLE PacketLogger
818b658ebcSmatthias.ringwald typedef struct {
828b658ebcSmatthias.ringwald 	uint32_t	len;
838b658ebcSmatthias.ringwald 	uint32_t	ts_sec;
848b658ebcSmatthias.ringwald 	uint32_t	ts_usec;
852df12229Smatthias.ringwald 	uint8_t		type;   // 0xfc for note
866c5c6faaSmatthias.ringwald }
876c5c6faaSmatthias.ringwald #ifdef __GNUC__
886c5c6faaSmatthias.ringwald __attribute__ ((packed))
896c5c6faaSmatthias.ringwald #endif
906c5c6faaSmatthias.ringwald pktlog_hdr;
918b658ebcSmatthias.ringwald 
928b658ebcSmatthias.ringwald static int dump_file = -1;
93198a9e1bS[email protected] #ifndef EMBEDDED
948b658ebcSmatthias.ringwald static int dump_format;
958b658ebcSmatthias.ringwald static hcidump_hdr header_bluez;
968b658ebcSmatthias.ringwald static pktlog_hdr  header_packetlogger;
978adf0ddaSmatthias.ringwald static char time_string[40];
982992c131Smatthias.ringwald static int  max_nr_packets = -1;
999ae0c346Smatthias.ringwald static int  nr_packets = 0;
100a1d7dd1fSmatthias.ringwald static char log_message_buffer[256];
10168e27c0fSmatthias.ringwald #endif
1028b658ebcSmatthias.ringwald 
103a225073eS[email protected] void hci_dump_open(const char *filename, hci_dump_format_t format){
104198a9e1bS[email protected] #ifdef EMBEDDED
105198a9e1bS[email protected]     dump_file = 1;
106198a9e1bS[email protected] #else
1078b658ebcSmatthias.ringwald     dump_format = format;
1088adf0ddaSmatthias.ringwald     if (dump_format == HCI_DUMP_STDOUT) {
1098adf0ddaSmatthias.ringwald         dump_file = fileno(stdout);
1108adf0ddaSmatthias.ringwald     } else {
111*b52eaea5S[email protected] #ifdef _WIN32
112*b52eaea5S[email protected]         dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
113*b52eaea5S[email protected] #else
1148adf0ddaSmatthias.ringwald         dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
115*b52eaea5S[email protected] #endif
11679662672Smatthias.ringwald     }
11768e27c0fSmatthias.ringwald #endif
118d9659922Smatthias.ringwald }
11979662672Smatthias.ringwald 
1207c5f7483Smatthias.ringwald #ifndef EMBEDDED
1212992c131Smatthias.ringwald void hci_dump_set_max_packets(int packets){
1222992c131Smatthias.ringwald     max_nr_packets = packets;
1232992c131Smatthias.ringwald }
1247c5f7483Smatthias.ringwald #endif
1252992c131Smatthias.ringwald 
126198a9e1bS[email protected] static inline void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
127198a9e1bS[email protected]     switch (packet_type){
128198a9e1bS[email protected]         case HCI_COMMAND_DATA_PACKET:
129198a9e1bS[email protected]             printf("CMD => ");
130198a9e1bS[email protected]             break;
131198a9e1bS[email protected]         case HCI_EVENT_PACKET:
132198a9e1bS[email protected]             printf("EVT <= ");
133198a9e1bS[email protected]             break;
134198a9e1bS[email protected]         case HCI_ACL_DATA_PACKET:
135198a9e1bS[email protected]             if (in) {
136198a9e1bS[email protected]                 printf("ACL <= ");
137198a9e1bS[email protected]             } else {
138198a9e1bS[email protected]                 printf("ACL => ");
139198a9e1bS[email protected]             }
140198a9e1bS[email protected]             break;
141198a9e1bS[email protected]         case LOG_MESSAGE_PACKET:
142198a9e1bS[email protected]             printf("LOG -- %s\n", (char*) packet);
143198a9e1bS[email protected]             return;
144198a9e1bS[email protected]         default:
145198a9e1bS[email protected]             return;
146198a9e1bS[email protected]     }
147198a9e1bS[email protected]     printf_hexdump(packet, len);
148198a9e1bS[email protected] }
149198a9e1bS[email protected] 
15079662672Smatthias.ringwald void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
15168e27c0fSmatthias.ringwald 
1528b658ebcSmatthias.ringwald     if (dump_file < 0) return; // not activated yet
1538b658ebcSmatthias.ringwald 
154198a9e1bS[email protected] #ifdef EMBEDDED
155198a9e1bS[email protected]     printf_packet(packet_type, in, packet, len);
156198a9e1bS[email protected] #else
1572992c131Smatthias.ringwald     // don't grow bigger than max_nr_packets
1582992c131Smatthias.ringwald     if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
1592992c131Smatthias.ringwald         if (nr_packets >= max_nr_packets){
1602992c131Smatthias.ringwald             lseek(dump_file, 0, SEEK_SET);
1612992c131Smatthias.ringwald             ftruncate(dump_file, 0);
1622992c131Smatthias.ringwald             nr_packets = 0;
1632992c131Smatthias.ringwald         }
1642992c131Smatthias.ringwald         nr_packets++;
1652992c131Smatthias.ringwald     }
1662992c131Smatthias.ringwald 
1678b658ebcSmatthias.ringwald     // get time
1688b658ebcSmatthias.ringwald     struct timeval curr_time;
1698adf0ddaSmatthias.ringwald     struct tm* ptm;
1708b658ebcSmatthias.ringwald     gettimeofday(&curr_time, NULL);
171d5ea8924S[email protected]     time_t curr_time_secs = curr_time.tv_sec;
1728b658ebcSmatthias.ringwald 
1738b658ebcSmatthias.ringwald     switch (dump_format){
174a9cf4d77S[email protected]         case HCI_DUMP_STDOUT: {
1758adf0ddaSmatthias.ringwald             /* Obtain the time of day, and convert it to a tm struct. */
176d5ea8924S[email protected]             ptm = localtime (&curr_time_secs);
1778adf0ddaSmatthias.ringwald             /* Format the date and time, down to a single second. */
1788adf0ddaSmatthias.ringwald             strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
1798adf0ddaSmatthias.ringwald             /* Compute milliseconds from microseconds. */
1808adf0ddaSmatthias.ringwald             uint16_t milliseconds = curr_time.tv_usec / 1000;
1818adf0ddaSmatthias.ringwald             /* Print the formatted time, in seconds, followed by a decimal point
1828adf0ddaSmatthias.ringwald              and the milliseconds. */
1838adf0ddaSmatthias.ringwald             printf ("%s.%03u] ", time_string, milliseconds);
184198a9e1bS[email protected]             printf_packet(packet_type, in, packet, len);
1858adf0ddaSmatthias.ringwald             break;
186a9cf4d77S[email protected]         }
1870d79c710Smatthias.ringwald 
1888b658ebcSmatthias.ringwald         case HCI_DUMP_BLUEZ:
1898b658ebcSmatthias.ringwald             bt_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len);
1908b658ebcSmatthias.ringwald             header_bluez.in  = in;
1918b658ebcSmatthias.ringwald             header_bluez.pad = 0;
1928b658ebcSmatthias.ringwald             bt_store_32( (uint8_t *) &header_bluez.ts_sec,  0, curr_time.tv_sec);
1938b658ebcSmatthias.ringwald             bt_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec);
1948b658ebcSmatthias.ringwald             header_bluez.packet_type = packet_type;
1958b658ebcSmatthias.ringwald             write (dump_file, &header_bluez, sizeof(hcidump_hdr) );
19679662672Smatthias.ringwald             write (dump_file, packet, len );
1978b658ebcSmatthias.ringwald             break;
1980d79c710Smatthias.ringwald 
1998b658ebcSmatthias.ringwald         case HCI_DUMP_PACKETLOGGER:
200*b52eaea5S[email protected]             net_store_32( (uint8_t *) &header_packetlogger, 0, sizeof(pktlog_hdr) - 4 + len);
201*b52eaea5S[email protected]             net_store_32( (uint8_t *) &header_packetlogger, 4, curr_time.tv_sec);
202*b52eaea5S[email protected]             net_store_32( (uint8_t *) &header_packetlogger, 8, curr_time.tv_usec);
2038b658ebcSmatthias.ringwald             switch (packet_type){
2048b658ebcSmatthias.ringwald                 case HCI_COMMAND_DATA_PACKET:
2058b658ebcSmatthias.ringwald                     header_packetlogger.type = 0x00;
2068b658ebcSmatthias.ringwald                     break;
2078b658ebcSmatthias.ringwald                 case HCI_ACL_DATA_PACKET:
2088b658ebcSmatthias.ringwald                     if (in) {
2098b658ebcSmatthias.ringwald                         header_packetlogger.type = 0x03;
2108b658ebcSmatthias.ringwald                     } else {
2118b658ebcSmatthias.ringwald                         header_packetlogger.type = 0x02;
2128b658ebcSmatthias.ringwald                     }
2138b658ebcSmatthias.ringwald                     break;
2148b658ebcSmatthias.ringwald                 case HCI_EVENT_PACKET:
2158b658ebcSmatthias.ringwald                     header_packetlogger.type = 0x01;
2168b658ebcSmatthias.ringwald                     break;
2170d79c710Smatthias.ringwald                 case LOG_MESSAGE_PACKET:
2180d79c710Smatthias.ringwald                     header_packetlogger.type = 0xfc;
2190d79c710Smatthias.ringwald                     break;
2208b658ebcSmatthias.ringwald                 default:
2218b658ebcSmatthias.ringwald                     return;
2228b658ebcSmatthias.ringwald             }
2238b658ebcSmatthias.ringwald             write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) );
2248b658ebcSmatthias.ringwald             write (dump_file, packet, len );
2250d79c710Smatthias.ringwald             break;
2260d79c710Smatthias.ringwald 
2270d79c710Smatthias.ringwald         default:
2280d79c710Smatthias.ringwald             break;
2298b658ebcSmatthias.ringwald     }
23068e27c0fSmatthias.ringwald #endif
23179662672Smatthias.ringwald }
23279662672Smatthias.ringwald 
233a1d7dd1fSmatthias.ringwald void hci_dump_log(const char * format, ...){
234a1d7dd1fSmatthias.ringwald #ifndef EMBEDDED
235a1d7dd1fSmatthias.ringwald     va_list argptr;
236a1d7dd1fSmatthias.ringwald     va_start(argptr, format);
237a1d7dd1fSmatthias.ringwald     int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
238a1d7dd1fSmatthias.ringwald     hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
239a1d7dd1fSmatthias.ringwald     va_end(argptr);
240a1d7dd1fSmatthias.ringwald #endif
241a1d7dd1fSmatthias.ringwald }
242a1d7dd1fSmatthias.ringwald 
24379662672Smatthias.ringwald void hci_dump_close(){
24468e27c0fSmatthias.ringwald #ifndef EMBEDDED
24579662672Smatthias.ringwald     close(dump_file);
246d9659922Smatthias.ringwald     dump_file = -1;
24768e27c0fSmatthias.ringwald #endif
24879662672Smatthias.ringwald }
24979662672Smatthias.ringwald 
250