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