6 * log2asc.c - convert compact CAN frame logfile to ASC logfile
8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Volkswagen nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
28 * The provided data structures and external interfaces from this code
29 * are not restricted to be used by modules with a GPL compatible license.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44 * Send feedback to <socketcan-users@lists.berlios.de>
55 #include <linux/can.h>
59 #define BUFSZ 400 /* for one line in the logfile */
61 extern int optind, opterr, optopt;
63 void print_usage(char *prg)
65 fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
66 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
67 fprintf(stderr, " -O <outfile> (default stdout)\n");
68 fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n");
69 fprintf(stderr, " -n (set newline to cr/lf - default lf)\n");
72 int main(int argc, char **argv)
74 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
77 static struct timeval tv, start_tv;
79 FILE *outfile = stdout;
80 static int maxdev, devno, i, crlf, d4, opt;
82 while ((opt = getopt(argc, argv, "I:O:4n")) != -1) {
85 infile = fopen(optarg, "r");
93 outfile = fopen(optarg, "w");
109 fprintf(stderr, "Unknown option %c\n", opt);
110 print_usage(basename(argv[0]));
116 maxdev = argc - optind; /* find real number of CAN devices */
119 fprintf(stderr, "no CAN interfaces defined!\n");
120 print_usage(basename(argv[0]));
124 //printf("Found %d CAN devices!\n", maxdev);
126 while (fgets(buf, BUFSZ-1, infile)) {
128 if (strlen(buf) >= BUFSZ-2) {
129 fprintf(stderr, "line too long for input buffer\n");
133 /* check for a comment line */
137 if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
138 device, ascframe) != 4) {
139 fprintf(stderr, "incorrect line format in logfile\n");
143 if (!start_tv.tv_sec) { /* print banner */
145 fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
146 fprintf(outfile, "base hex timestamps absolute%s",
148 fprintf(outfile, "no internal events logged%s",
152 for (i=0, devno=0; i<maxdev; i++) {
153 if (!strcmp(device, argv[optind+i])) {
154 devno = i+1; /* start with channel '1' */
159 if (devno) { /* only convert for selected CAN devices */
160 if (parse_canframe(ascframe, &cf))
163 tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
164 tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
166 tv.tv_sec--, tv.tv_usec += 1000000;
168 tv.tv_sec = tv.tv_usec = 0;
171 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
173 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
175 fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
177 if (cf.can_id & CAN_ERR_FLAG)
178 fprintf(outfile, "ErrorFrame");
180 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
181 (cf.can_id & CAN_EFF_FLAG)?'x':' ');
182 fprintf(outfile, "%-15s Rx ", id);
184 if (cf.can_id & CAN_RTR_FLAG)
185 fprintf(outfile, "r"); /* RTR frame */
187 fprintf(outfile, "d %d", cf.can_dlc); /* data frame */
189 for (i = 0; i < cf.can_dlc; i++) {
190 fprintf(outfile, " %02X", cf.data[i]);
195 fprintf(outfile, "\r");
196 fprintf(outfile, "\n");