]> rtime.felk.cvut.cz Git - sojka/can-utils.git/commitdiff
lib: add support for non-zero dlc values in CAN 2.0B RTR frames
authorOliver Hartkopp <socketcan@hartkopp.net>
Fri, 15 Feb 2013 22:25:13 +0000 (23:25 +0100)
committerOliver Hartkopp <socketcan@hartkopp.net>
Fri, 15 Feb 2013 22:25:13 +0000 (23:25 +0100)
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>
lib.c
lib.h

diff --git a/lib.c b/lib.c
index 4857e8081c66cfc34ee8b738df5840c6f60c3c97..9fb10567402ae83e04d7d54206f0220e2242c6cc 100644 (file)
--- 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 7c79d0b7dea6ce38950e1312292790258281098d..4f361389e346747e74fadf8c1e1cccb906c264bf 100644 (file)
--- 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 <can_id>#{R|data}
- * - {data} has 0 to 8 hex-values that can (optionally) be seperated by '.'
+ * - string layout <can_id>#{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