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