-
Notifications
You must be signed in to change notification settings - Fork 18
/
log2asc.c
206 lines (175 loc) · 5.66 KB
/
log2asc.c
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
/*
* log2asc.c - convert compact CAN frame logfile to ASC logfile
*
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
* All rights reserved.
*
* 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 Volkswagen nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* Alternatively, provided that this notice is retained in full, this
* software may be distributed under the terms of the GNU General
* Public License ("GPL") version 2, in which case the provisions of the
* GPL apply INSTEAD OF those given above.
*
* The provided data structures and external interfaces from this code
* are not restricted to be used by modules with a GPL compatible license.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
* OWNER 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.
*
* Send feedback to <[email protected]>
*
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <libgen.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/time.h>
#include <linux/can.h>
#include "lib.h"
#define BUFSZ 400 /* for one line in the logfile */
extern int optind, opterr, optopt;
void print_usage(char *prg)
{
fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
fprintf(stderr, "Options: -I <infile> (default stdin)\n");
fprintf(stderr, " -O <outfile> (default stdout)\n");
fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n");
fprintf(stderr, " -n (set newline to cr/lf - default lf)\n");
}
int main(int argc, char **argv)
{
static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
struct canfd_frame cf;
static struct timeval tv, start_tv;
FILE *infile = stdin;
FILE *outfile = stdout;
static int maxdev, devno, i, crlf, d4, opt;
while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) {
switch (opt) {
case 'I':
infile = fopen(optarg, "r");
if (!infile) {
perror("infile");
return 1;
}
break;
case 'O':
outfile = fopen(optarg, "w");
if (!outfile) {
perror("outfile");
return 1;
}
break;
case 'n':
crlf = 1;
break;
case '4':
d4 = 1;
break;
case '?':
print_usage(basename(argv[0]));
return 0;
break;
default:
fprintf(stderr, "Unknown option %c\n", opt);
print_usage(basename(argv[0]));
return 1;
break;
}
}
maxdev = argc - optind; /* find real number of CAN devices */
if (!maxdev) {
fprintf(stderr, "no CAN interfaces defined!\n");
print_usage(basename(argv[0]));
return 1;
}
//printf("Found %d CAN devices!\n", maxdev);
while (fgets(buf, BUFSZ-1, infile)) {
if (strlen(buf) >= BUFSZ-2) {
fprintf(stderr, "line too long for input buffer\n");
return 1;
}
/* check for a comment line */
if (buf[0] != '(')
continue;
if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
device, ascframe) != 4) {
fprintf(stderr, "incorrect line format in logfile\n");
return 1;
}
if (!start_tv.tv_sec) { /* print banner */
start_tv = tv;
fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
fprintf(outfile, "base hex timestamps absolute%s",
(crlf)?"\r\n":"\n");
fprintf(outfile, "no internal events logged%s",
(crlf)?"\r\n":"\n");
}
for (i=0, devno=0; i<maxdev; i++) {
if (!strcmp(device, argv[optind+i])) {
devno = i+1; /* start with channel '1' */
break;
}
}
if (devno) { /* only convert for selected CAN devices */
if (parse_canframe(ascframe, &cf) != CAN_MTU) /* no CAN FD support so far */
return 1;
tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
if (tv.tv_usec < 0)
tv.tv_sec--, tv.tv_usec += 1000000;
if (tv.tv_sec < 0)
tv.tv_sec = tv.tv_usec = 0;
if (d4)
fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
else
fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
if (cf.can_id & CAN_ERR_FLAG)
fprintf(outfile, "ErrorFrame");
else {
sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
(cf.can_id & CAN_EFF_FLAG)?'x':' ');
fprintf(outfile, "%-15s Rx ", id);
if (cf.can_id & CAN_RTR_FLAG)
fprintf(outfile, "r"); /* RTR frame */
else {
fprintf(outfile, "d %d", cf.len); /* data frame */
for (i = 0; i < cf.len; i++) {
fprintf(outfile, " %02X", cf.data[i]);
}
}
}
if (crlf)
fprintf(outfile, "\r");
fprintf(outfile, "\n");
}
}
fflush(outfile);
fclose(outfile);
fclose(infile);
return 0;
}