]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - log2asc.c
Fixed typo in log2asc.c
[sojka/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, the following disclaimer and
16  *    the referenced file 'COPYING'.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of Volkswagen nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * Alternatively, provided that this notice is retained in full, this
25  * software may be distributed under the terms of the GNU General
26  * Public License ("GPL") version 2 as distributed in the 'COPYING'
27  * file from the main directory of the linux kernel source.
28  *
29  * The provided data structures and external interfaces from this code
30  * are not restricted to be used by modules with a GPL compatible license.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43  * DAMAGE.
44  *
45  * Send feedback to <socketcan-users@lists.berlios.de>
46  *
47  */
48
49 #include <stdio.h>
50 #include <string.h>
51 #include <time.h>
52 #include <libgen.h>
53 #include <unistd.h>
54
55 #include <net/if.h>
56 #include <linux/can.h>
57
58 #include "lib.h"
59
60 extern int optind, opterr, optopt;
61
62 void print_usage(char *prg)
63 {
64     fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
65     fprintf(stderr, "Options: -I <infile>  (default stdin)\n");
66     fprintf(stderr, "         -O <outfile> (default stdout)\n");
67     fprintf(stderr, "         -4 (reduce decimal place to 4 digits)\n");
68     fprintf(stderr, "         -n (set newline to cr/lf - default lf)\n");
69 }
70
71 int main(int argc, char **argv)
72 {
73     char buf[100], device[100], ascframe[100], id[10];
74
75     struct can_frame cf;
76     static struct timeval tv, start_tv;
77     FILE *infile = stdin;
78     FILE *outfile = stdout;
79     static int maxdev, devno, i, crlf, d4, opt;
80
81     while ((opt = getopt(argc, argv, "I:O:4n")) != -1) {
82         switch (opt) {
83         case 'I':
84             infile = fopen(optarg, "r");
85             if (!infile) {
86                 perror("infile");
87                 return 1;
88             }
89             break;
90
91         case 'O':
92             outfile = fopen(optarg, "w");
93             if (!outfile) {
94                 perror("outfile");
95                 return 1;
96             }
97             break;
98
99         case 'n':
100             crlf = 1;
101             break;
102
103         case '4':
104             d4 = 1;
105             break;
106
107         default:
108             fprintf(stderr, "Unknown option %c\n", opt);
109             print_usage(basename(argv[0]));
110             return 1;
111             break;
112         }
113     }
114
115     maxdev = argc - optind; /* find real number of CAN devices */
116
117     if (!maxdev) {
118         fprintf(stderr, "no CAN interfaces defined!\n");
119         print_usage(basename(argv[0]));
120         return 1;
121     }
122         
123     //printf("Found %d CAN devices!\n", maxdev);
124
125     while (fgets(buf, 99, infile)) {
126         if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
127                    device, ascframe) != 4)
128             return 1;
129
130         if (!start_tv.tv_sec) { /* print banner */
131             start_tv = tv;
132             fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
133             fprintf(outfile, "base hex  timestamps absolute%s",
134                     (crlf)?"\r\n":"\n");
135             fprintf(outfile, "no internal events logged%s",
136                     (crlf)?"\r\n":"\n");
137         }
138
139         for (i=0, devno=0; i<maxdev; i++) {
140             if (!strcmp(device, argv[optind+i])) {
141                 devno = i+1; /* start with channel '1' */
142                 break;
143             }
144         }
145
146         if (devno) { /* only convert for selected CAN devices */
147             if (parse_canframe(ascframe, &cf))
148                 return 1;
149
150             tv.tv_sec  = tv.tv_sec - start_tv.tv_sec;
151             tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
152             if (tv.tv_usec < 0)
153                 tv.tv_sec--, tv.tv_usec += 1000000;
154             if (tv.tv_sec < 0)
155                 tv.tv_sec = tv.tv_usec = 0;
156
157             if (d4)
158                 fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
159             else
160                 fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
161
162             fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
163
164             if (cf.can_id & CAN_ERR_FLAG)
165                 fprintf(outfile, "ErrorFrame");
166             else {
167                 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
168                         (cf.can_id & CAN_EFF_FLAG)?'x':' ');
169                 fprintf(outfile, "%-15s Rx   ", id);
170                 
171                 if (cf.can_id & CAN_RTR_FLAG)
172                     fprintf(outfile, "r"); /* RTR frame */
173                 else {
174                     fprintf(outfile, "d %d", cf.can_dlc); /* data frame */
175                     
176                     for (i = 0; i < cf.can_dlc; i++) {
177                         fprintf(outfile, " %02X", cf.data[i]);
178                     }
179                 }
180             }
181             if (crlf)
182                 fprintf(outfile, "\r");
183             fprintf(outfile, "\n");
184         }
185     }
186     fflush(outfile);
187
188     return 0;
189 }