You probably missed this reply ;-)
https://lore.kernel.org/linux-can/2ef6fb30-9fa8-99ee-39f7-e39bfb06f06c@xxxxxxxxxxxx/
On 01.11.20 17:26, Vincent Mailhol wrote:
The variable 'frame' is declared on the stack and is reused at each
iteration of the while loop.
If not flushed, garbage data from the previous iteration of the loop
could remain causing unexpected behavior. This is the case in DLC
random mode: the field frame.len8_dlc is not cleared when the dlc is
exactly 8 and the len8_dlc value of the previous iteration will be
sent again.
This can be observed by running 'cangen -8 can0'. Once a frame of DLC
in the range 9..15 is generated, no more frames of DLC 8 show out in
the log.
Signed-off-by: Vincent Mailhol <mailhol.vincent@xxxxxxxxxx>
--
Changes in v2: v1 was dumb and broke everything which was not in
random mode. Just clear the len8_dlc.
---
cangen.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/cangen.c b/cangen.c
index ab8122c..14887c8 100644
--- a/cangen.c
+++ b/cangen.c
@@ -171,7 +171,7 @@ int main(int argc, char **argv)
struct pollfd fds;
struct sockaddr_can addr;
- static struct canfd_frame frame;
+ static struct canfd_frame frame = { 0 };
int nbytes;
int i;
struct ifreq ifr;
@@ -418,16 +418,16 @@ int main(int argc, char **argv)
if (canfd)
frame.len = can_dlc2len(random() & 0xF);
else {
+ struct can_frame *ccf = (struct can_frame *)&frame;
+
frame.len = random() & 0xF;
+ ccf->len8_dlc = 0;
if (frame.len > CAN_MAX_DLEN) {
- if (len8_dlc) {
- struct can_frame *ccf = (struct can_frame *)&frame;
-
+ if (len8_dlc)
/* generate Classic CAN len8 DLCs */
ccf->len8_dlc = frame.len;
- }
- frame.len = 8; /* for about 50% of the frames */
+ frame.len = CAN_MAX_DLEN; /* for about 50% of the frames */
}
}
}