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 <H.Engblom@tele-radio.com>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
pull/7/head
Oliver Hartkopp 2013-02-15 23:25:13 +01:00
parent e5c81aa221
commit eaf87a3bf6
2 changed files with 20 additions and 5 deletions

13
lib.c
View File

@ -169,6 +169,11 @@ int parse_canframe(char *cs, struct canfd_frame *cf) {
if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */ if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
cf->can_id |= CAN_RTR_FLAG; 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; 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 */ /* 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) { 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; return;
} }

12
lib.h
View File

@ -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. * Transfers a valid ASCII string decribing a CAN frame into struct canfd_frame.
* *
* CAN 2.0 frames * CAN 2.0 frames
* - string layout <can_id>#{R|data} * - string layout <can_id>#{R{len}|data}
* - {data} has 0 to 8 hex-values that can (optionally) be seperated by '.' * - {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 * - return value on successful parsing: CAN_MTU
* *
* CAN FD frames * CAN FD frames
@ -115,6 +116,8 @@ int parse_canframe(char *cs, struct canfd_frame *cf);
* 123# -> standard CAN-Id = 0x123, len = 0 * 123# -> standard CAN-Id = 0x123, len = 0
* 12345678# -> extended CAN-Id = 0x12345678, len = 0 * 12345678# -> extended CAN-Id = 0x12345678, len = 0
* 123#R -> standard CAN-Id = 0x123, len = 0, RTR-frame * 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 * 7A1#r -> standard CAN-Id = 0x7A1, len = 0, RTR-frame
* *
* 123#00 -> standard CAN-Id = 0x123, len = 1, data[0] = 0x00 * 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 = 8 -> CAN2.0 frame
* maxdlen = 64 -> CAN FD frame * maxdlen = 64 -> CAN FD frame
* *
* 12345678#112233 -> exended CAN-Id = 0x12345678, dlc = 3, data, sep = 0 * 12345678#112233 -> exended CAN-Id = 0x12345678, len = 3, data, sep = 0
* 12345678#R -> exended CAN-Id = 0x12345678, RTR * 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 * 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 * 32345678#112233 -> error frame with CAN_ERR_FLAG (0x2000000) set
* 123##0112233 -> CAN FD frame standard CAN-Id = 0x123, flags = 0, len = 3 * 123##0112233 -> CAN FD frame standard CAN-Id = 0x123, flags = 0, len = 3