1 | /*
|
---|
2 | * Copyright (C) 2009-2012 by Matthias Ringwald
|
---|
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 MATTHIAS RINGWALD 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 btstack@ringwald.ch
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * hci_dump.c
|
---|
39 | *
|
---|
40 | * Dump HCI trace in various formats:
|
---|
41 | *
|
---|
42 | * - BlueZ's hcidump format
|
---|
43 | * - Apple's PacketLogger
|
---|
44 | * - stdout hexdump
|
---|
45 | *
|
---|
46 | * Created by Matthias Ringwald on 5/26/09.
|
---|
47 | */
|
---|
48 |
|
---|
49 | #include "btstack-config.h"
|
---|
50 |
|
---|
51 | #include "hci_dump.h"
|
---|
52 | #include "hci.h"
|
---|
53 | #include "hci_transport.h"
|
---|
54 | #include <btstack/hci_cmds.h>
|
---|
55 | #include <btstack/run_loop.h>
|
---|
56 | #include <stdio.h>
|
---|
57 |
|
---|
58 | #ifndef EMBEDDED
|
---|
59 | #include <fcntl.h> // open
|
---|
60 | #include <unistd.h> // write
|
---|
61 | #include <time.h>
|
---|
62 | #include <sys/time.h> // for timestamps
|
---|
63 | #include <sys/stat.h> // for mode flags
|
---|
64 | #include <stdarg.h> // for va_list
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #include "debug.h"
|
---|
68 |
|
---|
69 | // BLUEZ hcidump
|
---|
70 | typedef struct {
|
---|
71 | uint16_t len;
|
---|
72 | uint8_t in;
|
---|
73 | uint8_t pad;
|
---|
74 | uint32_t ts_sec;
|
---|
75 | uint32_t ts_usec;
|
---|
76 | uint8_t packet_type;
|
---|
77 | }
|
---|
78 | #ifdef __GNUC__
|
---|
79 | __attribute__ ((packed))
|
---|
80 | #endif
|
---|
81 | hcidump_hdr;
|
---|
82 |
|
---|
83 | // APPLE PacketLogger
|
---|
84 | typedef struct {
|
---|
85 | uint32_t len;
|
---|
86 | uint32_t ts_sec;
|
---|
87 | uint32_t ts_usec;
|
---|
88 | uint8_t type; // 0xfc for note
|
---|
89 | }
|
---|
90 | #ifdef __GNUC__
|
---|
91 | __attribute__ ((packed))
|
---|
92 | #endif
|
---|
93 | pktlog_hdr;
|
---|
94 |
|
---|
95 | static int dump_file = -1;
|
---|
96 | #ifndef EMBEDDED
|
---|
97 | static int dump_format;
|
---|
98 | static hcidump_hdr header_bluez;
|
---|
99 | static pktlog_hdr header_packetlogger;
|
---|
100 | static char time_string[40];
|
---|
101 | static int max_nr_packets = -1;
|
---|
102 | static int nr_packets = 0;
|
---|
103 | static char log_message_buffer[256];
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | void hci_dump_open(const char *filename, hci_dump_format_t format){
|
---|
107 | #ifdef EMBEDDED
|
---|
108 | dump_file = 1;
|
---|
109 | #else
|
---|
110 | dump_format = format;
|
---|
111 | if (dump_format == HCI_DUMP_STDOUT) {
|
---|
112 | dump_file = fileno(stdout);
|
---|
113 | } else {
|
---|
114 | #ifdef _WIN32
|
---|
115 | dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
|
---|
116 | #else
|
---|
117 | dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
---|
118 | #endif
|
---|
119 | }
|
---|
120 | #endif
|
---|
121 | }
|
---|
122 |
|
---|
123 | #ifndef EMBEDDED
|
---|
124 | void hci_dump_set_max_packets(int packets){
|
---|
125 | max_nr_packets = packets;
|
---|
126 | }
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | static /*inline*/ void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len){
|
---|
130 | switch (packet_type){
|
---|
131 | case HCI_COMMAND_DATA_PACKET:
|
---|
132 | printf("CMD => ");
|
---|
133 | break;
|
---|
134 | case HCI_EVENT_PACKET:
|
---|
135 | printf("EVT <= ");
|
---|
136 | break;
|
---|
137 | case HCI_ACL_DATA_PACKET:
|
---|
138 | if (in) {
|
---|
139 | printf("ACL <= ");
|
---|
140 | } else {
|
---|
141 | printf("ACL => ");
|
---|
142 | }
|
---|
143 | break;
|
---|
144 | case LOG_MESSAGE_PACKET:
|
---|
145 | printf("LOG -- %s\n", (char*) packet);
|
---|
146 | return;
|
---|
147 | default:
|
---|
148 | return;
|
---|
149 | }
|
---|
150 | printf_hexdump(packet, len);
|
---|
151 | }
|
---|
152 | #ifndef hci_dump_packet
|
---|
153 | void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
|
---|
154 |
|
---|
155 | if (dump_file < 0) return; // not activated yet
|
---|
156 |
|
---|
157 | #ifdef EMBEDDED
|
---|
158 | // #ifdef HAVE_TICK
|
---|
159 | // uint32_t time_ms = embedded_get_time_ms();
|
---|
160 | // printf("[%06u] ", time_ms);
|
---|
161 | // #endif
|
---|
162 | printf_packet(packet_type, in, packet, len);
|
---|
163 | #else
|
---|
164 | // don't grow bigger than max_nr_packets
|
---|
165 | if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
|
---|
166 | if (nr_packets >= max_nr_packets){
|
---|
167 | lseek(dump_file, 0, SEEK_SET);
|
---|
168 | ftruncate(dump_file, 0);
|
---|
169 | nr_packets = 0;
|
---|
170 | }
|
---|
171 | nr_packets++;
|
---|
172 | }
|
---|
173 |
|
---|
174 | // get time
|
---|
175 | struct timeval curr_time;
|
---|
176 | struct tm* ptm;
|
---|
177 | gettimeofday(&curr_time, NULL);
|
---|
178 | time_t curr_time_secs = curr_time.tv_sec;
|
---|
179 |
|
---|
180 | switch (dump_format){
|
---|
181 | case HCI_DUMP_STDOUT: {
|
---|
182 | /* Obtain the time of day, and convert it to a tm struct. */
|
---|
183 | ptm = localtime (&curr_time_secs);
|
---|
184 | /* Format the date and time, down to a single second. */
|
---|
185 | strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
|
---|
186 | /* Compute milliseconds from microseconds. */
|
---|
187 | uint16_t milliseconds = curr_time.tv_usec / 1000;
|
---|
188 | /* Print the formatted time, in seconds, followed by a decimal point
|
---|
189 | and the milliseconds. */
|
---|
190 | printf ("%s.%03u] ", time_string, milliseconds);
|
---|
191 | printf_packet(packet_type, in, packet, len);
|
---|
192 | break;
|
---|
193 | }
|
---|
194 |
|
---|
195 | case HCI_DUMP_BLUEZ:
|
---|
196 | little_endian_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len);
|
---|
197 | header_bluez.in = in;
|
---|
198 | header_bluez.pad = 0;
|
---|
199 | little_endian_store_32( (uint8_t *) &header_bluez.ts_sec, 0, curr_time.tv_sec);
|
---|
200 | little_endian_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec);
|
---|
201 | header_bluez.packet_type = packet_type;
|
---|
202 | write (dump_file, &header_bluez, sizeof(hcidump_hdr) );
|
---|
203 | write (dump_file, packet, len );
|
---|
204 | break;
|
---|
205 |
|
---|
206 | case HCI_DUMP_PACKETLOGGER:
|
---|
207 | net_store_32( (uint8_t *) &header_packetlogger, 0, sizeof(pktlog_hdr) - 4 + len);
|
---|
208 | net_store_32( (uint8_t *) &header_packetlogger, 4, curr_time.tv_sec);
|
---|
209 | net_store_32( (uint8_t *) &header_packetlogger, 8, curr_time.tv_usec);
|
---|
210 | switch (packet_type){
|
---|
211 | case HCI_COMMAND_DATA_PACKET:
|
---|
212 | header_packetlogger.type = 0x00;
|
---|
213 | break;
|
---|
214 | case HCI_ACL_DATA_PACKET:
|
---|
215 | if (in) {
|
---|
216 | header_packetlogger.type = 0x03;
|
---|
217 | } else {
|
---|
218 | header_packetlogger.type = 0x02;
|
---|
219 | }
|
---|
220 | break;
|
---|
221 | case HCI_EVENT_PACKET:
|
---|
222 | header_packetlogger.type = 0x01;
|
---|
223 | break;
|
---|
224 | case LOG_MESSAGE_PACKET:
|
---|
225 | header_packetlogger.type = 0xfc;
|
---|
226 | break;
|
---|
227 | default:
|
---|
228 | return;
|
---|
229 | }
|
---|
230 | write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) );
|
---|
231 | write (dump_file, packet, len );
|
---|
232 | break;
|
---|
233 |
|
---|
234 | default:
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | #endif
|
---|
238 | }
|
---|
239 | #endif
|
---|
240 | #ifndef hci_dump_log
|
---|
241 | void hci_dump_log(const char * format, ...){
|
---|
242 | va_list argptr;
|
---|
243 | if (dump_file < 0) return; // not activated yet
|
---|
244 | va_start(argptr, format);
|
---|
245 | #ifdef EMBEDDED
|
---|
246 | printf("LOG -- ");
|
---|
247 | vprintf(format, argptr);
|
---|
248 | printf("\n");
|
---|
249 | #else
|
---|
250 | int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
|
---|
251 | hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
|
---|
252 | #endif
|
---|
253 | va_end(argptr);
|
---|
254 | }
|
---|
255 | #endif
|
---|
256 | #ifdef __AVR__
|
---|
257 | void hci_dump_log_P(PGM_P format, ...){
|
---|
258 | if (dump_file < 0) return; // not activated yet
|
---|
259 | va_list argptr;
|
---|
260 | va_start(argptr, format);
|
---|
261 | printf_P(PSTR("LOG -- "));
|
---|
262 | vfprintf_P(stdout, format, argptr);
|
---|
263 | printf_P(PSTR("\n"));
|
---|
264 | va_end(argptr);
|
---|
265 | }
|
---|
266 | #endif
|
---|
267 |
|
---|
268 | void hci_dump_close(){
|
---|
269 | #ifndef EMBEDDED
|
---|
270 | close(dump_file);
|
---|
271 | dump_file = -1;
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 |
|
---|