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>
52 #include <linux/can.h>
56 #define BUFSZ 400 /* for one line in the logfile */
58 extern int optind, opterr, optopt;
60 void print_usage(char *prg)
62 fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
63 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
64 fprintf(stderr, " -O <outfile> (default stdout)\n");
65 fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n");
66 fprintf(stderr, " -n (set newline to cr/lf - default lf)\n");
69 int main(int argc, char **argv)
71 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
73 struct canfd_frame cf;
74 static struct timeval tv, start_tv;
76 FILE *outfile = stdout;
77 static int maxdev, devno, i, crlf, d4, opt;
79 while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) {
82 infile = fopen(optarg, "r");
90 outfile = fopen(optarg, "w");
106 print_usage(basename(argv[0]));
111 fprintf(stderr, "Unknown option %c\n", opt);
112 print_usage(basename(argv[0]));
118 maxdev = argc - optind; /* find real number of CAN devices */
121 fprintf(stderr, "no CAN interfaces defined!\n");
122 print_usage(basename(argv[0]));
126 //printf("Found %d CAN devices!\n", maxdev);
128 while (fgets(buf, BUFSZ-1, infile)) {
130 if (strlen(buf) >= BUFSZ-2) {
131 fprintf(stderr, "line too long for input buffer\n");
135 /* check for a comment line */
139 if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
140 device, ascframe) != 4) {
141 fprintf(stderr, "incorrect line format in logfile\n");
145 if (!start_tv.tv_sec) { /* print banner */
147 fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
148 fprintf(outfile, "base hex timestamps absolute%s",
150 fprintf(outfile, "no internal events logged%s",
154 for (i=0, devno=0; i<maxdev; i++) {
155 if (!strcmp(device, argv[optind+i])) {
156 devno = i+1; /* start with channel '1' */
161 if (devno) { /* only convert for selected CAN devices */
162 if (parse_canframe(ascframe, &cf) != CAN_MTU) /* no CAN FD support so far */
165 tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
166 tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
168 tv.tv_sec--, tv.tv_usec += 1000000;
170 tv.tv_sec = tv.tv_usec = 0;
173 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
175 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
177 fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
179 if (cf.can_id & CAN_ERR_FLAG)
180 fprintf(outfile, "ErrorFrame");
182 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
183 (cf.can_id & CAN_EFF_FLAG)?'x':' ');
184 fprintf(outfile, "%-15s Rx ", id);
186 if (cf.can_id & CAN_RTR_FLAG)
187 fprintf(outfile, "r"); /* RTR frame */
189 fprintf(outfile, "d %d", cf.len); /* data frame */
191 for (i = 0; i < cf.len; i++) {
192 fprintf(outfile, " %02X", cf.data[i]);
197 fprintf(outfile, "\r");
198 fprintf(outfile, "\n");