-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhci_dump.c
More file actions
239 lines (219 loc) · 7.48 KB
/
Copy pathhci_dump.c
File metadata and controls
239 lines (219 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (C) 2009-2012 by Matthias Ringwald
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at btstack@ringwald.ch
*
*/
/*
* hci_dump.c
*
* Dump HCI trace in various formats:
*
* - BlueZ's hcidump format
* - Apple's PacketLogger
* - stdout hexdump
*
* Created by Matthias Ringwald on 5/26/09.
*/
#include "config.h"
#include "hci_dump.h"
#include "hci.h"
#include "hci_transport.h"
#include <btstack/hci_cmds.h>
#ifndef EMBEDDED
#include <fcntl.h> // open
#include <arpa/inet.h> // hton..
#include <unistd.h> // write
#include <stdio.h>
#include <time.h>
#include <sys/time.h> // for timestamps
#include <sys/stat.h> // for mode flags
#include <stdarg.h> // for va_list
#endif
// BLUEZ hcidump
typedef struct {
uint16_t len;
uint8_t in;
uint8_t pad;
uint32_t ts_sec;
uint32_t ts_usec;
uint8_t packet_type;
}
#ifdef __GNUC__
__attribute__ ((packed))
#endif
hcidump_hdr;
// APPLE PacketLogger
typedef struct {
uint32_t len;
uint32_t ts_sec;
uint32_t ts_usec;
uint8_t type; // 0xfc for note
}
#ifdef __GNUC__
__attribute__ ((packed))
#endif
pktlog_hdr;
#ifndef EMBEDDED
static int dump_file = -1;
static int dump_format;
static hcidump_hdr header_bluez;
static pktlog_hdr header_packetlogger;
static char time_string[40];
static int max_nr_packets = -1;
static int nr_packets = 0;
static char log_message_buffer[256];
#endif
void hci_dump_open(char *filename, hci_dump_format_t format){
#ifndef EMBEDDED
dump_format = format;
if (dump_format == HCI_DUMP_STDOUT) {
dump_file = fileno(stdout);
} else {
dump_file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}
#endif
}
#ifndef EMBEDDED
void hci_dump_set_max_packets(int packets){
max_nr_packets = packets;
}
#endif
void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) {
#ifndef EMBEDDED
if (dump_file < 0) return; // not activated yet
// don't grow bigger than max_nr_packets
if (dump_format != HCI_DUMP_STDOUT && max_nr_packets > 0){
if (nr_packets >= max_nr_packets){
lseek(dump_file, 0, SEEK_SET);
ftruncate(dump_file, 0);
nr_packets = 0;
}
nr_packets++;
}
// get time
struct timeval curr_time;
struct tm* ptm;
gettimeofday(&curr_time, NULL);
switch (dump_format){
case HCI_DUMP_STDOUT: {
/* Obtain the time of day, and convert it to a tm struct. */
ptm = localtime (&curr_time.tv_sec);
/* Format the date and time, down to a single second. */
strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm);
/* Compute milliseconds from microseconds. */
uint16_t milliseconds = curr_time.tv_usec / 1000;
/* Print the formatted time, in seconds, followed by a decimal point
and the milliseconds. */
printf ("%s.%03u] ", time_string, milliseconds);
switch (packet_type){
case HCI_COMMAND_DATA_PACKET:
printf("CMD => ");
break;
case HCI_EVENT_PACKET:
printf("EVT <= ");
break;
case HCI_ACL_DATA_PACKET:
if (in) {
printf("ACL <= ");
} else {
printf("ACL => ");
}
break;
case LOG_MESSAGE_PACKET:
// assume buffer is big enough
packet[len] = 0;
printf("LOG -- %s\n", (char*) packet);
return;
default:
return;
}
hexdump(packet, len);
break;
}
case HCI_DUMP_BLUEZ:
bt_store_16( (uint8_t *) &header_bluez.len, 0, 1 + len);
header_bluez.in = in;
header_bluez.pad = 0;
bt_store_32( (uint8_t *) &header_bluez.ts_sec, 0, curr_time.tv_sec);
bt_store_32( (uint8_t *) &header_bluez.ts_usec, 0, curr_time.tv_usec);
header_bluez.packet_type = packet_type;
write (dump_file, &header_bluez, sizeof(hcidump_hdr) );
write (dump_file, packet, len );
break;
case HCI_DUMP_PACKETLOGGER:
header_packetlogger.len = htonl( sizeof(pktlog_hdr) - 4 + len);
header_packetlogger.ts_sec = htonl(curr_time.tv_sec);
header_packetlogger.ts_usec = htonl(curr_time.tv_usec);
switch (packet_type){
case HCI_COMMAND_DATA_PACKET:
header_packetlogger.type = 0x00;
break;
case HCI_ACL_DATA_PACKET:
if (in) {
header_packetlogger.type = 0x03;
} else {
header_packetlogger.type = 0x02;
}
break;
case HCI_EVENT_PACKET:
header_packetlogger.type = 0x01;
break;
case LOG_MESSAGE_PACKET:
header_packetlogger.type = 0xfc;
break;
default:
return;
}
write (dump_file, &header_packetlogger, sizeof(pktlog_hdr) );
write (dump_file, packet, len );
break;
default:
break;
}
#endif
}
void hci_dump_log(const char * format, ...){
#ifndef EMBEDDED
va_list argptr;
va_start(argptr, format);
int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
va_end(argptr);
#endif
}
void hci_dump_close(){
#ifndef EMBEDDED
close(dump_file);
dump_file = -1;
#endif
}