drivers/can: fix sending of RTR frames with nonzero DLC

The message passed to the write can have `cm.hdr.ch_rtr` set and if
nonzero DLC is specified in the frame then invalid number of bytes is
removed from the buffer as RTR frames do not have any data even when DLC
is nonzero.

This was tested on SAMv7 (mcan). With this change a custom RTR can be
sent.

Signed-off-by: Karel Kočí <cynerd@email.cz>
This commit is contained in:
Karel Kočí
2025-10-14 15:34:21 +02:00
committed by Xiang Xiao
parent a19971f1bc
commit b571d2e5e4

View File

@@ -675,8 +675,16 @@ static ssize_t can_write(FAR struct file *filep, FAR const char *buffer,
* CAN message at sutibal.
*/
msg = (FAR struct can_msg_s *)&buffer[nsent];
nbytes = can_dlc2bytes(msg->cm_hdr.ch_dlc);
msg = (FAR struct can_msg_s *)&buffer[nsent];
if (msg->cm_hdr.ch_rtr)
{
nbytes = 0;
}
else
{
nbytes = can_dlc2bytes(msg->cm_hdr.ch_dlc);
}
msglen = CAN_MSGLEN(nbytes);
if (nsent + msglen > buflen)