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