]> rtime.felk.cvut.cz Git - can-utils.git/blob - log2asc.c
candump: Enable HW timestamping before using it
[can-utils.git] / log2asc.c
1 /*
2  * log2asc.c - convert compact CAN frame logfile to ASC logfile
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
23  *
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.
26  *
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
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <string.h>
46 #include <time.h>
47 #include <libgen.h>
48 #include <unistd.h>
49
50 #include <net/if.h>
51 #include <sys/time.h>
52 #include <linux/can.h>
53
54 #include "lib.h"
55
56 #define BUFSZ 400 /* for one line in the logfile */
57
58 extern int optind, opterr, optopt;
59
60 void print_usage(char *prg)
61 {
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");
67 }
68
69 int main(int argc, char **argv)
70 {
71         static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
72
73         struct canfd_frame cf;
74         static struct timeval tv, start_tv;
75         FILE *infile = stdin;
76         FILE *outfile = stdout;
77         static int maxdev, devno, i, crlf, d4, opt;
78
79         while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) {
80                 switch (opt) {
81                 case 'I':
82                         infile = fopen(optarg, "r");
83                         if (!infile) {
84                                 perror("infile");
85                                 return 1;
86                         }
87                         break;
88
89                 case 'O':
90                         outfile = fopen(optarg, "w");
91                         if (!outfile) {
92                                 perror("outfile");
93                                 return 1;
94                         }
95                         break;
96
97                 case 'n':
98                         crlf = 1;
99                         break;
100
101                 case '4':
102                         d4 = 1;
103                         break;
104
105                 case '?':
106                         print_usage(basename(argv[0]));
107                         return 0;
108                         break;
109
110                 default:
111                         fprintf(stderr, "Unknown option %c\n", opt);
112                         print_usage(basename(argv[0]));
113                         return 1;
114                         break;
115                 }
116         }
117
118         maxdev = argc - optind; /* find real number of CAN devices */
119
120         if (!maxdev) {
121                 fprintf(stderr, "no CAN interfaces defined!\n");
122                 print_usage(basename(argv[0]));
123                 return 1;
124         }
125         
126         //printf("Found %d CAN devices!\n", maxdev);
127
128         while (fgets(buf, BUFSZ-1, infile)) {
129
130                 if (strlen(buf) >= BUFSZ-2) {
131                         fprintf(stderr, "line too long for input buffer\n");
132                         return 1;
133                 }
134
135                 /* check for a comment line */
136                 if (buf[0] != '(')
137                         continue;
138
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");
142                         return 1;
143                 }
144
145                 if (!start_tv.tv_sec) { /* print banner */
146                         start_tv = tv;
147                         fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
148                         fprintf(outfile, "base hex  timestamps absolute%s",
149                                 (crlf)?"\r\n":"\n");
150                         fprintf(outfile, "no internal events logged%s",
151                                 (crlf)?"\r\n":"\n");
152                 }
153
154                 for (i=0, devno=0; i<maxdev; i++) {
155                         if (!strcmp(device, argv[optind+i])) {
156                                 devno = i+1; /* start with channel '1' */
157                                 break;
158                         }
159                 }
160
161                 if (devno) { /* only convert for selected CAN devices */
162                         if (parse_canframe(ascframe, &cf) != CAN_MTU) /* no CAN FD support so far */
163                                 return 1;
164
165                         tv.tv_sec  = tv.tv_sec - start_tv.tv_sec;
166                         tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
167                         if (tv.tv_usec < 0)
168                                 tv.tv_sec--, tv.tv_usec += 1000000;
169                         if (tv.tv_sec < 0)
170                                 tv.tv_sec = tv.tv_usec = 0;
171
172                         if (d4)
173                                 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
174                         else
175                                 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
176
177                         fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
178
179                         if (cf.can_id & CAN_ERR_FLAG)
180                                 fprintf(outfile, "ErrorFrame");
181                         else {
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);
185                 
186                                 if (cf.can_id & CAN_RTR_FLAG)
187                                         fprintf(outfile, "r"); /* RTR frame */
188                                 else {
189                                         fprintf(outfile, "d %d", cf.len); /* data frame */
190                     
191                                         for (i = 0; i < cf.len; i++) {
192                                                 fprintf(outfile, " %02X", cf.data[i]);
193                                         }
194                                 }
195                         }
196                         if (crlf)
197                                 fprintf(outfile, "\r");
198                         fprintf(outfile, "\n");
199                 }
200         }
201         fflush(outfile);
202         fclose(outfile);
203         fclose(infile);
204
205         return 0;
206 }