]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/log2asc.c
Add kernel version depency for Kernel 3.1.x which extended __rtnl_register().
[socketcan-devel.git] / can-utils / 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 #define BUFSZ 400 /* for one line in the logfile */
60
61 extern int optind, opterr, optopt;
62
63 void print_usage(char *prg)
64 {
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");
70 }
71
72 int main(int argc, char **argv)
73 {
74         static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
75
76         struct can_frame cf;
77         static struct timeval tv, start_tv;
78         FILE *infile = stdin;
79         FILE *outfile = stdout;
80         static int maxdev, devno, i, crlf, d4, opt;
81
82         while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) {
83                 switch (opt) {
84                 case 'I':
85                         infile = fopen(optarg, "r");
86                         if (!infile) {
87                                 perror("infile");
88                                 return 1;
89                         }
90                         break;
91
92                 case 'O':
93                         outfile = fopen(optarg, "w");
94                         if (!outfile) {
95                                 perror("outfile");
96                                 return 1;
97                         }
98                         break;
99
100                 case 'n':
101                         crlf = 1;
102                         break;
103
104                 case '4':
105                         d4 = 1;
106                         break;
107
108                 case '?':
109                         print_usage(basename(argv[0]));
110                         return 0;
111                         break;
112
113                 default:
114                         fprintf(stderr, "Unknown option %c\n", opt);
115                         print_usage(basename(argv[0]));
116                         return 1;
117                         break;
118                 }
119         }
120
121         maxdev = argc - optind; /* find real number of CAN devices */
122
123         if (!maxdev) {
124                 fprintf(stderr, "no CAN interfaces defined!\n");
125                 print_usage(basename(argv[0]));
126                 return 1;
127         }
128         
129         //printf("Found %d CAN devices!\n", maxdev);
130
131         while (fgets(buf, BUFSZ-1, infile)) {
132
133                 if (strlen(buf) >= BUFSZ-2) {
134                         fprintf(stderr, "line too long for input buffer\n");
135                         return 1;
136                 }
137
138                 /* check for a comment line */
139                 if (buf[0] != '(')
140                         continue;
141
142                 if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
143                            device, ascframe) != 4) {
144                         fprintf(stderr, "incorrect line format in logfile\n");
145                         return 1;
146                 }
147
148                 if (!start_tv.tv_sec) { /* print banner */
149                         start_tv = tv;
150                         fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
151                         fprintf(outfile, "base hex  timestamps absolute%s",
152                                 (crlf)?"\r\n":"\n");
153                         fprintf(outfile, "no internal events logged%s",
154                                 (crlf)?"\r\n":"\n");
155                 }
156
157                 for (i=0, devno=0; i<maxdev; i++) {
158                         if (!strcmp(device, argv[optind+i])) {
159                                 devno = i+1; /* start with channel '1' */
160                                 break;
161                         }
162                 }
163
164                 if (devno) { /* only convert for selected CAN devices */
165                         if (parse_canframe(ascframe, &cf))
166                                 return 1;
167
168                         tv.tv_sec  = tv.tv_sec - start_tv.tv_sec;
169                         tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
170                         if (tv.tv_usec < 0)
171                                 tv.tv_sec--, tv.tv_usec += 1000000;
172                         if (tv.tv_sec < 0)
173                                 tv.tv_sec = tv.tv_usec = 0;
174
175                         if (d4)
176                                 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
177                         else
178                                 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
179
180                         fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
181
182                         if (cf.can_id & CAN_ERR_FLAG)
183                                 fprintf(outfile, "ErrorFrame");
184                         else {
185                                 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
186                                         (cf.can_id & CAN_EFF_FLAG)?'x':' ');
187                                 fprintf(outfile, "%-15s Rx   ", id);
188                 
189                                 if (cf.can_id & CAN_RTR_FLAG)
190                                         fprintf(outfile, "r"); /* RTR frame */
191                                 else {
192                                         fprintf(outfile, "d %d", cf.can_dlc); /* data frame */
193                     
194                                         for (i = 0; i < cf.can_dlc; i++) {
195                                                 fprintf(outfile, " %02X", cf.data[i]);
196                                         }
197                                 }
198                         }
199                         if (crlf)
200                                 fprintf(outfile, "\r");
201                         fprintf(outfile, "\n");
202                 }
203         }
204         fflush(outfile);
205
206         return 0;
207 }