]> rtime.felk.cvut.cz Git - can-utils.git/blob - log2asc.c
Fixed contradiction in Sourcecode discalimer.
[can-utils.git] / log2asc.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * log2asc.c - convert compact CAN frame logfile to ASC logfile
7  *
8  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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.
27  *
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.
30  *
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
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <string.h>
50 #include <time.h>
51 #include <libgen.h>
52 #include <unistd.h>
53
54 #include <net/if.h>
55 #include <linux/can.h>
56
57 #include "lib.h"
58
59 extern int optind, opterr, optopt;
60
61 void print_usage(char *prg)
62 {
63     fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
64     fprintf(stderr, "Options: -I <infile>  (default stdin)\n");
65     fprintf(stderr, "         -O <outfile> (default stdout)\n");
66     fprintf(stderr, "         -4 (reduce decimal place to 4 digits)\n");
67     fprintf(stderr, "         -n (set newline to cr/lf - default lf)\n");
68 }
69
70 int main(int argc, char **argv)
71 {
72     char buf[100], device[100], ascframe[100], id[10];
73
74     struct can_frame cf;
75     static struct timeval tv, start_tv;
76     FILE *infile = stdin;
77     FILE *outfile = stdout;
78     static int maxdev, devno, i, crlf, d4, opt;
79
80     while ((opt = getopt(argc, argv, "I:O:4n")) != -1) {
81         switch (opt) {
82         case 'I':
83             infile = fopen(optarg, "r");
84             if (!infile) {
85                 perror("infile");
86                 return 1;
87             }
88             break;
89
90         case 'O':
91             outfile = fopen(optarg, "w");
92             if (!outfile) {
93                 perror("outfile");
94                 return 1;
95             }
96             break;
97
98         case 'n':
99             crlf = 1;
100             break;
101
102         case '4':
103             d4 = 1;
104             break;
105
106         default:
107             fprintf(stderr, "Unknown option %c\n", opt);
108             print_usage(basename(argv[0]));
109             return 1;
110             break;
111         }
112     }
113
114     maxdev = argc - optind; /* find real number of CAN devices */
115
116     if (!maxdev) {
117         fprintf(stderr, "no CAN interfaces defined!\n");
118         print_usage(basename(argv[0]));
119         return 1;
120     }
121         
122     //printf("Found %d CAN devices!\n", maxdev);
123
124     while (fgets(buf, 99, infile)) {
125         if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
126                    device, ascframe) != 4)
127             return 1;
128
129         if (!start_tv.tv_sec) { /* print banner */
130             start_tv = tv;
131             fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
132             fprintf(outfile, "base hex  timestamps absolute%s",
133                     (crlf)?"\r\n":"\n");
134             fprintf(outfile, "no internal events logged%s",
135                     (crlf)?"\r\n":"\n");
136         }
137
138         for (i=0, devno=0; i<maxdev; i++) {
139             if (!strcmp(device, argv[optind+i])) {
140                 devno = i+1; /* start with channel '1' */
141                 break;
142             }
143         }
144
145         if (devno) { /* only convert for selected CAN devices */
146             if (parse_canframe(ascframe, &cf))
147                 return 1;
148
149             tv.tv_sec  = tv.tv_sec - start_tv.tv_sec;
150             tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
151             if (tv.tv_usec < 0)
152                 tv.tv_sec--, tv.tv_usec += 1000000;
153             if (tv.tv_sec < 0)
154                 tv.tv_sec = tv.tv_usec = 0;
155
156             if (d4)
157                 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
158             else
159                 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
160
161             fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
162
163             if (cf.can_id & CAN_ERR_FLAG)
164                 fprintf(outfile, "ErrorFrame");
165             else {
166                 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
167                         (cf.can_id & CAN_EFF_FLAG)?'x':' ');
168                 fprintf(outfile, "%-15s Rx   ", id);
169                 
170                 if (cf.can_id & CAN_RTR_FLAG)
171                     fprintf(outfile, "r"); /* RTR frame */
172                 else {
173                     fprintf(outfile, "d %d", cf.can_dlc); /* data frame */
174                     
175                     for (i = 0; i < cf.can_dlc; i++) {
176                         fprintf(outfile, " %02X", cf.data[i]);
177                     }
178                 }
179             }
180             if (crlf)
181                 fprintf(outfile, "\r");
182             fprintf(outfile, "\n");
183         }
184     }
185     fflush(outfile);
186
187     return 0;
188 }