From: Oliver Hartkopp Date: Fri, 15 Feb 2013 22:25:13 +0000 (+0100) Subject: lib: add support for non-zero dlc values in CAN 2.0B RTR frames X-Git-Url: http://rtime.felk.cvut.cz/gitweb/sojka/can-utils.git/commitdiff_plain/eaf87a3bf69b92a15d0f51c7e3f08e8d900862cc lib: add support for non-zero dlc values in CAN 2.0B RTR frames As Hakan Engblom pointed out a CAN 2.0B RTR frame can contain a non-zero DLC value which was not addressed by parse_canframe() and sprint_canframe(). This patch adds support for non-zero DLC values in a way that legacy logfiles are still usable as the DLC can optionally be added. Reported-by: Hakan Engblom Signed-off-by: Oliver Hartkopp --- diff --git a/lib.c b/lib.c index 4857e80..9fb1056 100644 --- a/lib.c +++ b/lib.c @@ -169,6 +169,11 @@ int parse_canframe(char *cs, struct canfd_frame *cf) { if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */ cf->can_id |= CAN_RTR_FLAG; + + /* check for optional DLC value for CAN 2.0B frames */ + if(cs[++idx] && (tmp = asc2nibble(cs[idx])) <= CAN_MAX_DLC) + cf->len = tmp; + return ret; } @@ -236,7 +241,13 @@ void sprint_canframe(char *buf , struct canfd_frame *cf, int sep, int maxdlen) { /* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */ if (maxdlen == CAN_MAX_DLEN && cf->can_id & CAN_RTR_FLAG) { - sprintf(buf+offset, "R"); + + /* print a given CAN 2.0B DLC if it's not zero */ + if (cf->len && cf->len <= CAN_MAX_DLC) + sprintf(buf+offset, "R%d", cf->len); + else + sprintf(buf+offset, "R"); + return; } diff --git a/lib.h b/lib.h index 7c79d0b..4f36138 100644 --- a/lib.h +++ b/lib.h @@ -94,8 +94,9 @@ int parse_canframe(char *cs, struct canfd_frame *cf); * Transfers a valid ASCII string decribing a CAN frame into struct canfd_frame. * * CAN 2.0 frames - * - string layout #{R|data} - * - {data} has 0 to 8 hex-values that can (optionally) be seperated by '.' + * - string layout #{R{len}|data} + * - {data} has 0 to 8 hex-values that can (optionally) be separated by '.' + * - {len} can take values from 0 to 8 and can be omitted if zero * - return value on successful parsing: CAN_MTU * * CAN FD frames @@ -115,6 +116,8 @@ int parse_canframe(char *cs, struct canfd_frame *cf); * 123# -> standard CAN-Id = 0x123, len = 0 * 12345678# -> extended CAN-Id = 0x12345678, len = 0 * 123#R -> standard CAN-Id = 0x123, len = 0, RTR-frame + * 123#R0 -> standard CAN-Id = 0x123, len = 0, RTR-frame + * 123#R7 -> standard CAN-Id = 0x123, len = 7, RTR-frame * 7A1#r -> standard CAN-Id = 0x7A1, len = 0, RTR-frame * * 123#00 -> standard CAN-Id = 0x123, len = 1, data[0] = 0x00 @@ -149,8 +152,9 @@ void sprint_canframe(char *buf , struct canfd_frame *cf, int sep, int maxdlen); * maxdlen = 8 -> CAN2.0 frame * maxdlen = 64 -> CAN FD frame * - * 12345678#112233 -> exended CAN-Id = 0x12345678, dlc = 3, data, sep = 0 - * 12345678#R -> exended CAN-Id = 0x12345678, RTR + * 12345678#112233 -> exended CAN-Id = 0x12345678, len = 3, data, sep = 0 + * 12345678#R -> exended CAN-Id = 0x12345678, RTR, len = 0 + * 12345678#R5 -> exended CAN-Id = 0x12345678, RTR, len = 5 * 123#11.22.33.44.55.66.77.88 -> standard CAN-Id = 0x123, dlc = 8, sep = 1 * 32345678#112233 -> error frame with CAN_ERR_FLAG (0x2000000) set * 123##0112233 -> CAN FD frame standard CAN-Id = 0x123, flags = 0, len = 3