]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - asc2log.c
Removed possibility to terminate candump with an input from stdin.
[sojka/can-utils.git] / asc2log.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * asc2log.c - convert ASC logfile to compact CAN frame 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 <sys/time.h>
52 #define __USE_XOPEN /* supress warning for strptime */
53 #include <time.h>
54 #include <libgen.h>
55 #include <unistd.h>
56 #include <stdlib.h>
57 #include <locale.h>
58
59 #include <net/if.h>
60 #include <linux/can.h>
61 #include <linux/can/error.h>
62
63 #include "lib.h"
64
65 extern int optind, opterr, optopt;
66
67 void print_usage(char *prg)
68 {
69     fprintf(stderr, "Usage: %s\n", prg);
70     fprintf(stderr, "Options: -I <infile>  (default stdin)\n");
71     fprintf(stderr, "         -O <outfile> (default stdout)\n");
72 }
73
74 void prframe(FILE *file, struct timeval *tv, int dev, struct can_frame *cf) {
75
76     fprintf(file, "(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
77     if (dev > 0)
78         fprintf(file, "can%d ", dev-1);
79     else
80         fprintf(file, "canX ");
81     fprint_canframe(file, cf, "\n", 0);
82 }
83
84 void get_can_id(struct can_frame *cf, char *idstring, int base) {
85
86     if (idstring[strlen(idstring)-1] == 'x') {
87         cf->can_id = CAN_EFF_FLAG;
88         idstring[strlen(idstring)-1] = 0;
89     } else
90         cf->can_id = 0;
91     
92     cf->can_id |= strtoul(idstring, NULL, base);
93 }
94
95 void calc_tv(struct timeval *tv, struct timeval *read_tv,
96              struct timeval *date_tv, char timestamps, int dplace) {
97
98     if (dplace == 4) /* shift values having only 4 decimal places */
99         read_tv->tv_usec *= 100;                /* and need for 6 */
100
101     if (timestamps == 'a') { /* absolute */
102
103         tv->tv_sec  = date_tv->tv_sec  + read_tv->tv_sec;
104         tv->tv_usec = date_tv->tv_usec + read_tv->tv_usec;
105
106     } else { /* relative */
107
108         if (((!tv->tv_sec) && (!tv->tv_usec)) && 
109             (date_tv->tv_sec || date_tv->tv_usec)) {
110             tv->tv_sec  = date_tv->tv_sec; /* initial date/time */
111             tv->tv_usec = date_tv->tv_usec;
112         }
113
114         tv->tv_sec  += read_tv->tv_sec;
115         tv->tv_usec += read_tv->tv_usec;
116     }
117
118     if (tv->tv_usec > 1000000) {
119         tv->tv_usec -= 1000000;
120         tv->tv_sec++;
121     }
122 }
123
124 int get_date(struct timeval *tv, char *date) {
125
126     char ctmp[10];
127     int  itmp;
128
129     struct tm tms;
130
131     if (sscanf(date, "%9s %d %9s %9s %d", ctmp, &itmp, ctmp, ctmp, &itmp) == 5) {
132         /* assume EN/US date due to existing am/pm field */
133
134         if (!setlocale(LC_TIME, "en_US"))
135             return 1;
136
137         if (!strptime(date, "%B %d %r %Y", &tms))
138             return 1;
139
140     } else {
141         /* assume DE date due to non existing am/pm field */
142
143         if (sscanf(date, "%9s %d %9s %d", ctmp, &itmp, ctmp, &itmp) != 4)
144             return 1;
145
146         if (!setlocale(LC_TIME, "de_DE"))
147             return 1;
148
149         if (!strptime(date, "%B %d %T %Y", &tms))
150             return 1;
151     }
152     
153     //printf("h %d m %d s %d d %d m %d y %d\n",
154     //tms.tm_hour, tms.tm_min, tms.tm_sec,
155     //tms.tm_mday, tms.tm_mon+1, tms.tm_year+1900);
156
157     tv->tv_sec = mktime(&tms);
158
159     if (tv->tv_sec < 0)
160         return 1;
161
162     return 0;
163 }
164
165 int main(int argc, char **argv)
166 {
167     char buf[100], tmp1[100], tmp2[100];
168
169     FILE *infile = stdin;
170     FILE *outfile = stdout;
171     static int verbose;
172     struct can_frame cf;
173     static struct timeval tv; /* current frame timestamp */
174     static struct timeval read_tv; /* frame timestamp from ASC file */
175     static struct timeval date_tv; /* date of the ASC file */
176     static int dplace; /* decimal place 4 or 6 or uninitialized */
177     static char base; /* 'd'ec or 'h'ex */
178     static char timestamps; /* 'a'bsolute or 'r'elative */
179
180     int interface;
181     char rtr;
182     int dlc = 0;
183     int data[8];
184     int i, found, opt;
185
186     while ((opt = getopt(argc, argv, "I:O:v")) != -1) {
187         switch (opt) {
188         case 'I':
189             infile = fopen(optarg, "r");
190             if (!infile) {
191                 perror("infile");
192                 return 1;
193             }
194             break;
195
196         case 'O':
197             outfile = fopen(optarg, "w");
198             if (!outfile) {
199                 perror("outfile");
200                 return 1;
201             }
202             break;
203
204         case 'v':
205             verbose = 1;
206             break;
207
208         default:
209             fprintf(stderr, "Unknown option %c\n", opt);
210             print_usage(basename(argv[0]));
211             return 1;
212             break;
213         }
214     }
215
216
217     while (fgets(buf, 99, infile)) {
218
219         if (!dplace) { /* the representation of a valid CAN frame not known */
220
221             /* check for base and timestamp entries in the header */
222             if ((!base) &&
223                 (sscanf(buf, "base %s timestamps %s", tmp1, tmp2) == 2)) {
224                 base = tmp1[0];
225                 timestamps = tmp2[0];
226                 if (verbose)
227                     printf("base %c timestamps %c\n", base, timestamps);
228                 if ((base != 'h') && (base != 'd')) {
229                     printf("invalid base %s (must be 'hex' or 'dez')!\n",
230                            tmp1);
231                     return 1;
232                 }
233                 if ((timestamps != 'a') && (timestamps != 'r')) {
234                     printf("invalid timestamps %s (must be 'absolute'"
235                            " or 'relative')!\n", tmp2);
236                     return 1;
237                 }
238                 continue;
239             }
240
241             /* check for the original logging date in the header */ 
242             if ((!date_tv.tv_sec) &&
243                 (!strncmp(buf, "date", 4))) {
244
245                 if (get_date(&date_tv, &buf[9])) { /* skip 'date day ' */
246                     fprintf(stderr, "Not able to determine original log "
247                             "file date. Using current time.\n");
248                     /* use current date as default */
249                     gettimeofday(&date_tv, NULL);
250                 }
251                 if (verbose)
252                     printf("date %ld => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec));
253                 continue;
254             }
255
256             /* check for decimal places length in valid CAN frames */
257             if (sscanf(buf, "%ld.%s %d ", &tv.tv_sec, tmp2, &i) == 3){
258                 dplace = strlen(tmp2);
259                 if (verbose)
260                     printf("decimal place %d, e.g. '%s'\n", dplace, tmp2);
261                 if ((dplace != 4) && (dplace != 6)) {
262                     printf("invalid dplace %d (must be 4 or 6)!\n", dplace);
263                     return 1;
264                 }
265             } else
266                 continue; /* dplace remains zero until first found CAN frame */
267         } 
268
269         /* the representation of a valid CAN frame is known here */
270         /* so try to get CAN frames and ErrorFrames and convert them */
271
272         /* 0.002367 1 390x Rx d 8 17 00 14 00 C0 00 08 00 */
273
274         found = 0; /* found valid CAN frame ? */
275
276         if (base == 'h') { /* check for CAN frames with hexadecimal values */
277
278             if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %x %x %x %x %x %x %x %x",
279                        &read_tv.tv_sec, &read_tv.tv_usec, &interface,
280                        tmp1, &rtr, &dlc,
281                        &data[0], &data[1], &data[2], &data[3],
282                        &data[4], &data[5], &data[6], &data[7]
283                        ) == dlc + 6 ) {
284
285                 found = 1;
286                 get_can_id(&cf, tmp1, 16);
287             }
288
289         } else { /* check for CAN frames with decimal values */
290
291             if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %d %d %d %d %d %d %d %d",
292                        &read_tv.tv_sec, &read_tv.tv_usec, &interface,
293                        tmp1, &rtr, &dlc,
294                        &data[0], &data[1], &data[2], &data[3],
295                        &data[4], &data[5], &data[6], &data[7]
296                        ) == dlc + 6 ) {
297
298                 found = 1;
299                 get_can_id(&cf, tmp1, 10);
300             }
301         }
302
303         if (found) {
304             if (rtr == 'r')
305                 cf.can_id |= CAN_RTR_FLAG;
306  
307             cf.can_dlc = dlc & 0x0FU;
308             for (i=0; i<dlc; i++)
309                 cf.data[i] = data[i] & 0xFFU;
310
311             calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
312             prframe(outfile, &tv, interface, &cf);
313             fflush(outfile);
314             continue;
315         }
316
317         /* check for ErrorFrames */
318         if (sscanf(buf, "%ld.%ld %d %s",
319                    &read_tv.tv_sec, &read_tv.tv_usec,
320                    &interface, tmp1) == 4) {
321                 
322             if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) {
323
324                 memset(&cf, 0, sizeof(cf));
325                 /* do not know more than 'Error' */
326                 cf.can_id  = (CAN_ERR_FLAG | CAN_ERR_BUSERROR);
327                 cf.can_dlc =  CAN_ERR_DLC;
328                     
329                 calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
330                 prframe(outfile, &tv, interface, &cf);
331                 fflush(outfile);
332             }
333         }
334     }
335     fclose(outfile);
336     return 0;
337 }