From 3014d390b4b4d27d2626621c740372599bce061a Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Sun, 10 May 2020 11:22:05 +0200 Subject: [PATCH] log2asc: support CANFD format also for Classic CAN The CANFD tagged format also supports the encapsulation of Classic CAN frames which is selected when logging on CAN FD capable CAN interfaces. With a new option '-f' this CANFD format is selected. Although this representation is even more inefficient it is seems to be the common way and increases the readability for mixed CAN/CANFD logs. Signed-off-by: Oliver Hartkopp --- log2asc.c | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/log2asc.c b/log2asc.c index ad9fe8e..9180144 100644 --- a/log2asc.c +++ b/log2asc.c @@ -67,6 +67,7 @@ void print_usage(char *prg) fprintf(stderr, " -O (default stdout)\n"); fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n"); fprintf(stderr, " -n (set newline to cr/lf - default lf)\n"); + fprintf(stderr, " -f (use CANFD format also for Classic CAN)\n"); } void can_asc(struct canfd_frame *cf, int devno, FILE *outfile) @@ -95,11 +96,18 @@ void can_asc(struct canfd_frame *cf, int devno, FILE *outfile) } } -void canfd_asc(struct canfd_frame *cf, int devno, FILE *outfile) +void canfd_asc(struct canfd_frame *cf, int devno, int mtu, FILE *outfile) { int i; char id[10]; - unsigned int flags; + unsigned int flags = 0; + unsigned int dlen = cf->len; + + /* relevant flags in Flags field */ +#define ASC_F_RTR 0x00000010 +#define ASC_F_FDF 0x00001000 +#define ASC_F_BRS 0x00002000 +#define ASC_F_ESI 0x00004000 fprintf(outfile, "CANFD %3d Rx ", devno); /* 3 column channel number right aligned */ @@ -108,23 +116,27 @@ void canfd_asc(struct canfd_frame *cf, int devno, FILE *outfile) fprintf(outfile, "%11s ", id); fprintf(outfile, "%c ", (cf->flags & CANFD_BRS)?'1':'0'); fprintf(outfile, "%c ", (cf->flags & CANFD_ESI)?'1':'0'); - fprintf(outfile, "%x ", can_len2dlc(cf->len)); - fprintf(outfile, "%2d", cf->len); + fprintf(outfile, "%x ", can_len2dlc(dlen)); - for (i = 0; i < cf->len; i++) { - fprintf(outfile, " %02X", cf->data[i]); + if (mtu == CAN_MTU) { + if (cf->can_id & CAN_RTR_FLAG) { + /* no data length but dlc for RTR frames */ + dlen = 0; + flags = ASC_F_RTR; + } + } else { + flags = ASC_F_FDF; + if (cf->flags & CANFD_BRS) + flags |= ASC_F_BRS; + if (cf->flags & CANFD_ESI) + flags |= ASC_F_ESI; } - /* relevant flags in Flags field */ -#define ASC_F_FDF 0x00001000 -#define ASC_F_BRS 0x00002000 -#define ASC_F_ESI 0x00004000 + fprintf(outfile, "%2d", dlen); - flags = ASC_F_FDF; - if (cf->flags & CANFD_BRS) - flags |= ASC_F_BRS; - if (cf->flags & CANFD_ESI) - flags |= ASC_F_ESI; + for (i = 0; i < dlen; i++) { + fprintf(outfile, " %02X", cf->data[i]); + } fprintf(outfile, " %8d %4d %8X 0 0 0 0 0", 130000, 130, flags); } @@ -137,9 +149,9 @@ int main(int argc, char **argv) static struct timeval tv, start_tv; FILE *infile = stdin; FILE *outfile = stdout; - static int maxdev, devno, i, crlf, d4, opt, mtu; + static int maxdev, devno, i, crlf, fdfmt, d4, opt, mtu; - while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) { + while ((opt = getopt(argc, argv, "I:O:4nf?")) != -1) { switch (opt) { case 'I': infile = fopen(optarg, "r"); @@ -161,6 +173,10 @@ int main(int argc, char **argv) crlf = 1; break; + case 'f': + fdfmt = 1; + break; + case '4': d4 = 1; break; @@ -242,10 +258,10 @@ int main(int argc, char **argv) else fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec); - if (mtu == CAN_MTU) + if ((mtu == CAN_MTU) && (fdfmt == 0)) can_asc(&cf, devno, outfile); else - canfd_asc(&cf, devno, outfile); + canfd_asc(&cf, devno, mtu, outfile); if (crlf) fprintf(outfile, "\r");