]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/log2asc.c
- added error frame support in lib.c
[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, 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, "         -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, opt;
79
80     while ((opt = getopt(argc, argv, "I:O:n")) != -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         default:
103             fprintf(stderr, "Unknown option %c\n", opt);
104             print_usage(basename(argv[0]));
105             return 1;
106             break;
107         }
108     }
109
110     maxdev = argc - optind; /* find real number of CAN devices */
111
112     if (!maxdev) {
113         fprintf(stderr, "no CAN interfaces defined!\n");
114         print_usage(basename(argv[0]));
115         return 1;
116     }
117         
118     //printf("Found %d CAN devices!\n", maxdev);
119
120     while (fgets(buf, 99, infile)) {
121         if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
122                    device, ascframe) != 4)
123             return 1;
124
125         if (!start_tv.tv_sec) { /* print banner */
126             start_tv = tv;
127             fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
128             fprintf(outfile, "base hex  timestamps absolute%s",
129                     (crlf)?"\r\n":"\n");
130             fprintf(outfile, "no internal events logged%s",
131                     (crlf)?"\r\n":"\n");
132         }
133
134         for (i=0, devno=0; i<maxdev; i++) {
135             if (!strcmp(device, argv[optind+i])) {
136                 devno = i+1; /* start with channel '1' */
137                 break;
138             }
139         }
140
141         if (devno) { /* only convert for selected CAN devices */
142             if (parse_canframe(ascframe, &cf))
143                 return 1;
144
145             tv.tv_sec  = tv.tv_sec - start_tv.tv_sec;
146             tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
147             if (tv.tv_usec < 0)
148                 tv.tv_sec--, tv.tv_usec += 1000000;
149             if (tv.tv_sec < 0)
150                 tv.tv_sec = tv.tv_usec = 0;
151             fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
152
153             fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
154
155             if (cf.can_id & CAN_ERR_FLAG)
156                 fprintf(outfile, "(errorframe)");
157             else {
158                 sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
159                         (cf.can_id & CAN_EFF_FLAG)?'x':' ');
160                 fprintf(outfile, "%-15s Rx   ", id);
161                 
162                 if (cf.can_id & CAN_RTR_FLAG)
163                     fprintf(outfile, "r"); /* RTR frame */
164                 else {
165                     fprintf(outfile, "d %d", cf.can_dlc); /* data frame */
166                     
167                     for (i = 0; i < cf.can_dlc; i++) {
168                         fprintf(outfile, " %02X", cf.data[i]);
169                     }
170                 }
171             }
172             if (crlf)
173                 fprintf(outfile, "\r");
174             fprintf(outfile, "\n");
175         }
176     }
177     fflush(outfile);
178
179     return 0;
180 }