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 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.
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.
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.
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
44 * Send feedback to <socketcan-users@lists.berlios.de>
51 #define __USE_XOPEN /* supress warning for strptime */
59 #include <linux/can.h>
60 #include <linux/can/error.h>
64 extern int optind, opterr, optopt;
66 void print_usage(char *prg)
68 fprintf(stderr, "Usage: %s\n", prg);
69 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
70 fprintf(stderr, " -O <outfile> (default stdout)\n");
73 void prframe(FILE *file, struct timeval *tv, int dev, struct can_frame *cf) {
75 fprintf(file, "(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
77 fprintf(file, "can%d ", dev-1);
79 fprintf(file, "canX ");
80 fprint_canframe(file, cf, "\n", 0);
83 void get_can_id(struct can_frame *cf, char *idstring, int base) {
85 if (idstring[strlen(idstring)-1] == 'x') {
86 cf->can_id = CAN_EFF_FLAG;
87 idstring[strlen(idstring)-1] = 0;
91 cf->can_id |= strtoul(idstring, NULL, base);
94 void calc_tv(struct timeval *tv, struct timeval *read_tv,
95 struct timeval *date_tv, char timestamps, int dplace) {
97 if (dplace == 4) /* shift values having only 4 decimal places */
98 read_tv->tv_usec *= 100; /* and need for 6 */
100 if (dplace == 5) /* shift values having only 5 decimal places */
101 read_tv->tv_usec *= 10; /* and need for 6 */
103 if (timestamps == 'a') { /* absolute */
105 tv->tv_sec = date_tv->tv_sec + read_tv->tv_sec;
106 tv->tv_usec = date_tv->tv_usec + read_tv->tv_usec;
108 } else { /* relative */
110 if (((!tv->tv_sec) && (!tv->tv_usec)) &&
111 (date_tv->tv_sec || date_tv->tv_usec)) {
112 tv->tv_sec = date_tv->tv_sec; /* initial date/time */
113 tv->tv_usec = date_tv->tv_usec;
116 tv->tv_sec += read_tv->tv_sec;
117 tv->tv_usec += read_tv->tv_usec;
120 if (tv->tv_usec > 1000000) {
121 tv->tv_usec -= 1000000;
126 int get_date(struct timeval *tv, char *date) {
133 if (sscanf(date, "%9s %d %9s %9s %d", ctmp, &itmp, ctmp, ctmp, &itmp) == 5) {
134 /* assume EN/US date due to existing am/pm field */
136 if (!setlocale(LC_TIME, "en_US"))
139 if (!strptime(date, "%B %d %r %Y", &tms))
143 /* assume DE date due to non existing am/pm field */
145 if (sscanf(date, "%9s %d %9s %d", ctmp, &itmp, ctmp, &itmp) != 4)
148 if (!setlocale(LC_TIME, "de_DE"))
151 if (!strptime(date, "%B %d %T %Y", &tms))
155 //printf("h %d m %d s %d d %d m %d y %d\n",
156 //tms.tm_hour, tms.tm_min, tms.tm_sec,
157 //tms.tm_mday, tms.tm_mon+1, tms.tm_year+1900);
159 tv->tv_sec = mktime(&tms);
167 int main(int argc, char **argv)
169 char buf[100], tmp1[100], tmp2[100];
171 FILE *infile = stdin;
172 FILE *outfile = stdout;
175 static struct timeval tv; /* current frame timestamp */
176 static struct timeval read_tv; /* frame timestamp from ASC file */
177 static struct timeval date_tv; /* date of the ASC file */
178 static int dplace; /* decimal place 4, 5 or 6 or uninitialized */
179 static char base; /* 'd'ec or 'h'ex */
180 static char timestamps; /* 'a'bsolute or 'r'elative */
188 while ((opt = getopt(argc, argv, "I:O:v?")) != -1) {
191 infile = fopen(optarg, "r");
199 outfile = fopen(optarg, "w");
211 print_usage(basename(argv[0]));
216 fprintf(stderr, "Unknown option %c\n", opt);
217 print_usage(basename(argv[0]));
224 while (fgets(buf, 99, infile)) {
226 if (!dplace) { /* the representation of a valid CAN frame not known */
228 /* check for base and timestamp entries in the header */
230 (sscanf(buf, "base %s timestamps %s", tmp1, tmp2) == 2)) {
232 timestamps = tmp2[0];
234 printf("base %c timestamps %c\n", base, timestamps);
235 if ((base != 'h') && (base != 'd')) {
236 printf("invalid base %s (must be 'hex' or 'dez')!\n",
240 if ((timestamps != 'a') && (timestamps != 'r')) {
241 printf("invalid timestamps %s (must be 'absolute'"
242 " or 'relative')!\n", tmp2);
248 /* check for the original logging date in the header */
249 if ((!date_tv.tv_sec) &&
250 (!strncmp(buf, "date", 4))) {
252 if (get_date(&date_tv, &buf[9])) { /* skip 'date day ' */
253 fprintf(stderr, "Not able to determine original log "
254 "file date. Using current time.\n");
255 /* use current date as default */
256 gettimeofday(&date_tv, NULL);
259 printf("date %ld => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec));
263 /* check for decimal places length in valid CAN frames */
264 if (sscanf(buf, "%ld.%s %d ", &tv.tv_sec, tmp2, &i) == 3){
265 dplace = strlen(tmp2);
267 printf("decimal place %d, e.g. '%s'\n", dplace, tmp2);
268 if (dplace < 4 || dplace > 6) {
269 printf("invalid dplace %d (must be 4, 5 or 6)!\n", dplace);
273 continue; /* dplace remains zero until first found CAN frame */
276 /* the representation of a valid CAN frame is known here */
277 /* so try to get CAN frames and ErrorFrames and convert them */
279 /* 0.002367 1 390x Rx d 8 17 00 14 00 C0 00 08 00 */
281 found = 0; /* found valid CAN frame ? */
283 if (base == 'h') { /* check for CAN frames with hexadecimal values */
285 if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %x %x %x %x %x %x %x %x",
286 &read_tv.tv_sec, &read_tv.tv_usec, &interface,
288 &data[0], &data[1], &data[2], &data[3],
289 &data[4], &data[5], &data[6], &data[7]
293 get_can_id(&cf, tmp1, 16);
296 } else { /* check for CAN frames with decimal values */
298 if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %d %d %d %d %d %d %d %d",
299 &read_tv.tv_sec, &read_tv.tv_usec, &interface,
301 &data[0], &data[1], &data[2], &data[3],
302 &data[4], &data[5], &data[6], &data[7]
306 get_can_id(&cf, tmp1, 10);
312 cf.can_id |= CAN_RTR_FLAG;
314 cf.can_dlc = dlc & 0x0FU;
315 for (i=0; i<dlc; i++)
316 cf.data[i] = data[i] & 0xFFU;
318 calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
319 prframe(outfile, &tv, interface, &cf);
324 /* check for ErrorFrames */
325 if (sscanf(buf, "%ld.%ld %d %s",
326 &read_tv.tv_sec, &read_tv.tv_usec,
327 &interface, tmp1) == 4) {
329 if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) {
331 memset(&cf, 0, sizeof(cf));
332 /* do not know more than 'Error' */
333 cf.can_id = (CAN_ERR_FLAG | CAN_ERR_BUSERROR);
334 cf.can_dlc = CAN_ERR_DLC;
336 calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
337 prframe(outfile, &tv, interface, &cf);