]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - asc2log.c
Added checksum functionality and some documentation in gw.h
[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 and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <string.h>
50 #include <sys/time.h>
51 #define __USE_XOPEN /* supress warning for strptime */
52 #include <time.h>
53 #include <libgen.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <locale.h>
57
58 #include <net/if.h>
59 #include <linux/can.h>
60 #include <linux/can/error.h>
61
62 #include "lib.h"
63
64 extern int optind, opterr, optopt;
65
66 void print_usage(char *prg)
67 {
68         fprintf(stderr, "Usage: %s\n", prg);
69         fprintf(stderr, "Options: -I <infile>  (default stdin)\n");
70         fprintf(stderr, "         -O <outfile> (default stdout)\n");
71 }
72
73 void prframe(FILE *file, struct timeval *tv, int dev, struct can_frame *cf) {
74
75         fprintf(file, "(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
76         if (dev > 0)
77                 fprintf(file, "can%d ", dev-1);
78         else
79                 fprintf(file, "canX ");
80         fprint_canframe(file, cf, "\n", 0);
81 }
82
83 void get_can_id(struct can_frame *cf, char *idstring, int base) {
84
85         if (idstring[strlen(idstring)-1] == 'x') {
86                 cf->can_id = CAN_EFF_FLAG;
87                 idstring[strlen(idstring)-1] = 0;
88         } else
89                 cf->can_id = 0;
90     
91         cf->can_id |= strtoul(idstring, NULL, base);
92 }
93
94 void calc_tv(struct timeval *tv, struct timeval *read_tv,
95              struct timeval *date_tv, char timestamps, int dplace) {
96
97         if (dplace == 4) /* shift values having only 4 decimal places */
98                 read_tv->tv_usec *= 100;                /* and need for 6 */
99
100         if (timestamps == 'a') { /* absolute */
101
102                 tv->tv_sec  = date_tv->tv_sec  + read_tv->tv_sec;
103                 tv->tv_usec = date_tv->tv_usec + read_tv->tv_usec;
104
105         } else { /* relative */
106
107                 if (((!tv->tv_sec) && (!tv->tv_usec)) && 
108                     (date_tv->tv_sec || date_tv->tv_usec)) {
109                         tv->tv_sec  = date_tv->tv_sec; /* initial date/time */
110                         tv->tv_usec = date_tv->tv_usec;
111                 }
112
113                 tv->tv_sec  += read_tv->tv_sec;
114                 tv->tv_usec += read_tv->tv_usec;
115         }
116
117         if (tv->tv_usec > 1000000) {
118                 tv->tv_usec -= 1000000;
119                 tv->tv_sec++;
120         }
121 }
122
123 int get_date(struct timeval *tv, char *date) {
124
125         char ctmp[10];
126         int  itmp;
127
128         struct tm tms;
129
130         if (sscanf(date, "%9s %d %9s %9s %d", ctmp, &itmp, ctmp, ctmp, &itmp) == 5) {
131                 /* assume EN/US date due to existing am/pm field */
132
133                 if (!setlocale(LC_TIME, "en_US"))
134                         return 1;
135
136                 if (!strptime(date, "%B %d %r %Y", &tms))
137                         return 1;
138
139         } else {
140                 /* assume DE date due to non existing am/pm field */
141
142                 if (sscanf(date, "%9s %d %9s %d", ctmp, &itmp, ctmp, &itmp) != 4)
143                         return 1;
144
145                 if (!setlocale(LC_TIME, "de_DE"))
146                         return 1;
147
148                 if (!strptime(date, "%B %d %T %Y", &tms))
149                         return 1;
150         }
151     
152         //printf("h %d m %d s %d d %d m %d y %d\n",
153         //tms.tm_hour, tms.tm_min, tms.tm_sec,
154         //tms.tm_mday, tms.tm_mon+1, tms.tm_year+1900);
155
156         tv->tv_sec = mktime(&tms);
157
158         if (tv->tv_sec < 0)
159                 return 1;
160
161         return 0;
162 }
163
164 int main(int argc, char **argv)
165 {
166         char buf[100], tmp1[100], tmp2[100];
167
168         FILE *infile = stdin;
169         FILE *outfile = stdout;
170         static int verbose;
171         struct can_frame cf;
172         static struct timeval tv; /* current frame timestamp */
173         static struct timeval read_tv; /* frame timestamp from ASC file */
174         static struct timeval date_tv; /* date of the ASC file */
175         static int dplace; /* decimal place 4 or 6 or uninitialized */
176         static char base; /* 'd'ec or 'h'ex */
177         static char timestamps; /* 'a'bsolute or 'r'elative */
178
179         int interface;
180         char rtr;
181         int dlc = 0;
182         int data[8];
183         int i, found, opt;
184
185         while ((opt = getopt(argc, argv, "I:O:v?")) != -1) {
186                 switch (opt) {
187                 case 'I':
188                         infile = fopen(optarg, "r");
189                         if (!infile) {
190                                 perror("infile");
191                                 return 1;
192                         }
193                         break;
194
195                 case 'O':
196                         outfile = fopen(optarg, "w");
197                         if (!outfile) {
198                                 perror("outfile");
199                                 return 1;
200                         }
201                         break;
202
203                 case 'v':
204                         verbose = 1;
205                         break;
206
207                 case '?':
208                         print_usage(basename(argv[0]));
209                         return 0;
210                         break;
211
212                 default:
213                         fprintf(stderr, "Unknown option %c\n", opt);
214                         print_usage(basename(argv[0]));
215                         return 1;
216                         break;
217                 }
218         }
219
220
221         while (fgets(buf, 99, infile)) {
222
223                 if (!dplace) { /* the representation of a valid CAN frame not known */
224
225                         /* check for base and timestamp entries in the header */
226                         if ((!base) &&
227                             (sscanf(buf, "base %s timestamps %s", tmp1, tmp2) == 2)) {
228                                 base = tmp1[0];
229                                 timestamps = tmp2[0];
230                                 if (verbose)
231                                         printf("base %c timestamps %c\n", base, timestamps);
232                                 if ((base != 'h') && (base != 'd')) {
233                                         printf("invalid base %s (must be 'hex' or 'dez')!\n",
234                                                tmp1);
235                                         return 1;
236                                 }
237                                 if ((timestamps != 'a') && (timestamps != 'r')) {
238                                         printf("invalid timestamps %s (must be 'absolute'"
239                                                " or 'relative')!\n", tmp2);
240                                         return 1;
241                                 }
242                                 continue;
243                         }
244
245                         /* check for the original logging date in the header */ 
246                         if ((!date_tv.tv_sec) &&
247                             (!strncmp(buf, "date", 4))) {
248
249                                 if (get_date(&date_tv, &buf[9])) { /* skip 'date day ' */
250                                         fprintf(stderr, "Not able to determine original log "
251                                                 "file date. Using current time.\n");
252                                         /* use current date as default */
253                                         gettimeofday(&date_tv, NULL);
254                                 }
255                                 if (verbose)
256                                         printf("date %ld => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec));
257                                 continue;
258                         }
259
260                         /* check for decimal places length in valid CAN frames */
261                         if (sscanf(buf, "%ld.%s %d ", &tv.tv_sec, tmp2, &i) == 3){
262                                 dplace = strlen(tmp2);
263                                 if (verbose)
264                                         printf("decimal place %d, e.g. '%s'\n", dplace, tmp2);
265                                 if ((dplace != 4) && (dplace != 6)) {
266                                         printf("invalid dplace %d (must be 4 or 6)!\n", dplace);
267                                         return 1;
268                                 }
269                         } else
270                                 continue; /* dplace remains zero until first found CAN frame */
271                 } 
272
273                 /* the representation of a valid CAN frame is known here */
274                 /* so try to get CAN frames and ErrorFrames and convert them */
275
276                 /* 0.002367 1 390x Rx d 8 17 00 14 00 C0 00 08 00 */
277
278                 found = 0; /* found valid CAN frame ? */
279
280                 if (base == 'h') { /* check for CAN frames with hexadecimal values */
281
282                         if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %x %x %x %x %x %x %x %x",
283                                    &read_tv.tv_sec, &read_tv.tv_usec, &interface,
284                                    tmp1, &rtr, &dlc,
285                                    &data[0], &data[1], &data[2], &data[3],
286                                    &data[4], &data[5], &data[6], &data[7]
287                                     ) == dlc + 6 ) {
288
289                                 found = 1;
290                                 get_can_id(&cf, tmp1, 16);
291                         }
292
293                 } else { /* check for CAN frames with decimal values */
294
295                         if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %d %d %d %d %d %d %d %d",
296                                    &read_tv.tv_sec, &read_tv.tv_usec, &interface,
297                                    tmp1, &rtr, &dlc,
298                                    &data[0], &data[1], &data[2], &data[3],
299                                    &data[4], &data[5], &data[6], &data[7]
300                                     ) == dlc + 6 ) {
301
302                                 found = 1;
303                                 get_can_id(&cf, tmp1, 10);
304                         }
305                 }
306
307                 if (found) {
308                         if (rtr == 'r')
309                                 cf.can_id |= CAN_RTR_FLAG;
310  
311                         cf.can_dlc = dlc & 0x0FU;
312                         for (i=0; i<dlc; i++)
313                                 cf.data[i] = data[i] & 0xFFU;
314
315                         calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
316                         prframe(outfile, &tv, interface, &cf);
317                         fflush(outfile);
318                         continue;
319                 }
320
321                 /* check for ErrorFrames */
322                 if (sscanf(buf, "%ld.%ld %d %s",
323                            &read_tv.tv_sec, &read_tv.tv_usec,
324                            &interface, tmp1) == 4) {
325                 
326                         if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) {
327
328                                 memset(&cf, 0, sizeof(cf));
329                                 /* do not know more than 'Error' */
330                                 cf.can_id  = (CAN_ERR_FLAG | CAN_ERR_BUSERROR);
331                                 cf.can_dlc =  CAN_ERR_DLC;
332                     
333                                 calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
334                                 prframe(outfile, &tv, interface, &cf);
335                                 fflush(outfile);
336                         }
337                 }
338         }
339         fclose(outfile);
340         return 0;
341 }