2 * log2asc.c - convert compact CAN frame logfile to ASC logfile
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
51 #include <linux/can.h>
55 #define BUFSZ 400 /* for one line in the logfile */
57 extern int optind, opterr, optopt;
59 void print_usage(char *prg)
61 fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
62 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
63 fprintf(stderr, " -O <outfile> (default stdout)\n");
64 fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n");
65 fprintf(stderr, " -n (set newline to cr/lf - default lf)\n");
68 int main(int argc, char **argv)
70 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
72 struct canfd_frame cf;
73 static struct timeval tv, start_tv;
75 FILE *outfile = stdout;
76 static int maxdev, devno, i, crlf, d4, opt;
78 while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) {
81 infile = fopen(optarg, "r");
89 outfile = fopen(optarg, "w");
105 print_usage(basename(argv[0]));
110 fprintf(stderr, "Unknown option %c\n", opt);
111 print_usage(basename(argv[0]));
117 maxdev = argc - optind; /* find real number of CAN devices */
120 fprintf(stderr, "no CAN interfaces defined!\n");
121 print_usage(basename(argv[0]));
125 //printf("Found %d CAN devices!\n", maxdev);
127 while (fgets(buf, BUFSZ-1, infile)) {
129 if (strlen(buf) >= BUFSZ-2) {
130 fprintf(stderr, "line too long for input buffer\n");
134 /* check for a comment line */
138 if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
139 device, ascframe) != 4) {
140 fprintf(stderr, "incorrect line format in logfile\n");
144 if (!start_tv.tv_sec) { /* print banner */
146 fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
147 fprintf(outfile, "base hex timestamps absolute%s",
149 fprintf(outfile, "no internal events logged%s",
153 for (i=0, devno=0; i<maxdev; i++) {
154 if (!strcmp(device, argv[optind+i])) {
155 devno = i+1; /* start with channel '1' */
160 if (devno) { /* only convert for selected CAN devices */
161 if (parse_canframe(ascframe, &cf) != CAN_MTU) /* no CAN FD support so far */
164 tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
165 tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
167 tv.tv_sec--, tv.tv_usec += 1000000;
169 tv.tv_sec = tv.tv_usec = 0;
172 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
174 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
176 fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
178 if (cf.can_id & CAN_ERR_FLAG)
179 fprintf(outfile, "ErrorFrame");
181 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
182 (cf.can_id & CAN_EFF_FLAG)?'x':' ');
183 fprintf(outfile, "%-15s Rx ", id);
185 if (cf.can_id & CAN_RTR_FLAG)
186 fprintf(outfile, "r"); /* RTR frame */
188 fprintf(outfile, "d %d", cf.len); /* data frame */
190 for (i = 0; i < cf.len; i++) {
191 fprintf(outfile, " %02X", cf.data[i]);
196 fprintf(outfile, "\r");
197 fprintf(outfile, "\n");