6 * asc2log.c - convert ASC logfile to compact CAN frame logfile
8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
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
45 * Send feedback to <socketcan-users@lists.berlios.de>
52 #define __USE_XOPEN /* supress warning for strptime */
60 #include <linux/can.h>
61 #include <linux/can/error.h>
65 extern int optind, opterr, optopt;
67 void print_usage(char *prg)
69 fprintf(stderr, "Usage: %s\n", prg);
70 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
71 fprintf(stderr, " -O <outfile> (default stdout)\n");
74 void prframe(FILE *file, struct timeval *tv, int dev, struct can_frame *cf) {
76 fprintf(file, "(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
78 fprintf(file, "can%d ", dev-1);
80 fprintf(file, "canX ");
81 fprint_canframe(file, cf, "\n", 0);
84 void get_can_id(struct can_frame *cf, char *idstring, int base) {
86 if (idstring[strlen(idstring)-1] == 'x') {
87 cf->can_id = CAN_EFF_FLAG;
88 idstring[strlen(idstring)-1] = 0;
92 cf->can_id |= strtoul(idstring, NULL, base);
95 void calc_tv(struct timeval *tv, struct timeval *read_tv,
96 struct timeval *date_tv, char timestamps, int dplace) {
98 if (dplace == 4) /* shift values having only 4 decimal places */
99 read_tv->tv_usec *= 100; /* and need for 6 */
101 if (timestamps == 'a') { /* absolute */
103 tv->tv_sec = date_tv->tv_sec + read_tv->tv_sec;
104 tv->tv_usec = date_tv->tv_usec + read_tv->tv_usec;
106 } else { /* relative */
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;
114 tv->tv_sec += read_tv->tv_sec;
115 tv->tv_usec += read_tv->tv_usec;
118 if (tv->tv_usec > 1000000) {
119 tv->tv_usec -= 1000000;
124 int get_date(struct timeval *tv, char *date) {
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 */
134 if (!setlocale(LC_TIME, "en_US"))
137 if (!strptime(date, "%B %d %r %Y", &tms))
141 /* assume DE date due to non existing am/pm field */
143 if (sscanf(date, "%9s %d %9s %d", ctmp, &itmp, ctmp, &itmp) != 4)
146 if (!setlocale(LC_TIME, "de_DE"))
149 if (!strptime(date, "%B %d %T %Y", &tms))
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);
157 tv->tv_sec = mktime(&tms);
165 int main(int argc, char **argv)
167 char buf[100], tmp1[100], tmp2[100];
169 FILE *infile = stdin;
170 FILE *outfile = stdout;
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 */
186 while ((opt = getopt(argc, argv, "I:O:v")) != -1) {
189 infile = fopen(optarg, "r");
197 outfile = fopen(optarg, "w");
209 fprintf(stderr, "Unknown option %c\n", opt);
210 print_usage(basename(argv[0]));
217 while (fgets(buf, 99, infile)) {
219 if (!dplace) { /* the representation of a valid CAN frame not known */
221 /* check for base and timestamp entries in the header */
223 (sscanf(buf, "base %s timestamps %s", tmp1, tmp2) == 2)) {
225 timestamps = tmp2[0];
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",
233 if ((timestamps != 'a') && (timestamps != 'r')) {
234 printf("invalid timestamps %s (must be 'absolute'"
235 " or 'relative')!\n", tmp2);
241 /* check for the original logging date in the header */
242 if ((!date_tv.tv_sec) &&
243 (!strncmp(buf, "date", 4))) {
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);
252 printf("date %ld => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec));
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);
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);
266 continue; /* dplace remains zero until first found CAN frame */
269 /* the representation of a valid CAN frame is known here */
270 /* so try to get CAN frames and ErrorFrames and convert them */
272 /* 0.002367 1 390x Rx d 8 17 00 14 00 C0 00 08 00 */
274 found = 0; /* found valid CAN frame ? */
276 if (base == 'h') { /* check for CAN frames with hexadecimal values */
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,
281 &data[0], &data[1], &data[2], &data[3],
282 &data[4], &data[5], &data[6], &data[7]
286 get_can_id(&cf, tmp1, 16);
289 } else { /* check for CAN frames with decimal values */
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,
294 &data[0], &data[1], &data[2], &data[3],
295 &data[4], &data[5], &data[6], &data[7]
299 get_can_id(&cf, tmp1, 10);
305 cf.can_id |= CAN_RTR_FLAG;
307 cf.can_dlc = dlc & 0x0FU;
308 for (i=0; i<dlc; i++)
309 cf.data[i] = data[i] & 0xFFU;
311 calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
312 prframe(outfile, &tv, interface, &cf);
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) {
322 if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) {
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;
329 calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
330 prframe(outfile, &tv, interface, &cf);