From d92adf68e8de385e23e7d931cd5d1c5b6919c13d Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:08:35 +0200 Subject: [PATCH 1/8] canfdtest: main(): remove double ";" Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canfdtest.c b/canfdtest.c index 8de160a..79ac30a 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -331,7 +331,7 @@ int main(int argc, char *argv[]) break; case 'l': - test_loops = atoi(optarg);; + test_loops = atoi(optarg); break; case 'g': From 29605e331439834a7db1a1b028faa3e9fe2e0438 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:09:24 +0200 Subject: [PATCH 2/8] canfdtest: compare_frame(): remove stray space in front of "!" Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canfdtest.c b/canfdtest.c index 79ac30a..033201c 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -114,7 +114,7 @@ static void compare_frame(struct can_frame *exp, struct can_frame *rec, int inc) } else { for (i = 0; i < rec->can_dlc; i++) { if (rec->data[i] != ((exp->data[i] + inc) & 0xff)) { - printf("Databyte %x mismatch !\n", i); + printf("Databyte %x mismatch!\n", i); print_compare(exp, rec, inc); running = 0; From ff3d0e0d05ec26352ca7e82ea6917b2946a361ec Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:10:25 +0200 Subject: [PATCH 3/8] canfdtest: print_frame()/print_compare(): mark parameters "struct can_frame *" as const Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/canfdtest.c b/canfdtest.c index 033201c..366ab08 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -76,7 +76,7 @@ static void print_usage(char *prg) exit(1); } -static void print_frame(struct can_frame *frame, int inc) +static void print_frame(const struct can_frame *frame, int inc) { int i; @@ -91,7 +91,7 @@ static void print_frame(struct can_frame *frame, int inc) printf("\n"); } -static void print_compare(struct can_frame *exp, struct can_frame *rec, int inc) +static void print_compare(const struct can_frame *exp, const struct can_frame *rec, int inc) { printf("expected: "); print_frame(exp, inc); From aa5386f1cbd710df08a356e850de7583267d622a Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 2 Apr 2020 10:57:43 +0200 Subject: [PATCH 4/8] canfdtest: print_frame(): add missing cast to uint8_t to avoid overruns While there, use cast in compare_frame(), too. Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/canfdtest.c b/canfdtest.c index 366ab08..8828504 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -86,7 +87,7 @@ static void print_frame(const struct can_frame *frame, int inc) } else { printf("[%d]", frame->can_dlc); for (i = 0; i < frame->can_dlc; i++) - printf(" %02x", frame->data[i] + inc); + printf(" %02x", (uint8_t)(frame->data[i] + inc)); } printf("\n"); } @@ -113,7 +114,7 @@ static void compare_frame(struct can_frame *exp, struct can_frame *rec, int inc) running = 0; } else { for (i = 0; i < rec->can_dlc; i++) { - if (rec->data[i] != ((exp->data[i] + inc) & 0xff)) { + if (rec->data[i] != (uint8_t)(exp->data[i] + inc)) { printf("Databyte %x mismatch!\n", i); print_compare(exp, rec, inc); From 645cd1b1ba91e40983592f078e88eda86dc30a22 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:12:11 +0200 Subject: [PATCH 5/8] canfdtest: can_echo_dut(): make use of print_frame() instead of open coding it Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/canfdtest.c b/canfdtest.c index 8828504..e88a637 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -210,15 +210,7 @@ static int can_echo_dut(void) if (verbose == 1) { echo_progress(frame.data[0]); } else if (verbose > 1) { - printf("%04x: ", frame.can_id); - if (frame.can_id & CAN_RTR_FLAG) { - printf("remote request"); - } else { - printf("[%d]", frame.can_dlc); - for (i = 0; i < frame.can_dlc; i++) - printf(" %02x", frame.data[i]); - } - printf("\n"); + print_frame(&frame, 0); } frame.can_id++; for (i = 0; i < frame.can_dlc; i++) From e393697112e4c2c01a999c704d65b76858ec51ae Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:13:43 +0200 Subject: [PATCH 6/8] canfdtest: can_echo_dut(): move functionality to increment frame data into separate function Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/canfdtest.c b/canfdtest.c index e88a637..1f68f67 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -197,11 +197,19 @@ static int send_frame(struct can_frame *frame) return 0; } +static void inc_frame(struct can_frame *frame) +{ + int i; + + frame->can_id++; + for (i = 0; i < frame->can_dlc; i++) + frame->data[i]++; +} + static int can_echo_dut(void) { unsigned int frame_count = 0; struct can_frame frame; - int i; while (running) { if (recv_frame(&frame)) @@ -212,9 +220,8 @@ static int can_echo_dut(void) } else if (verbose > 1) { print_frame(&frame, 0); } - frame.can_id++; - for (i = 0; i < frame.can_dlc; i++) - frame.data[i]++; + + inc_frame(&frame); if (send_frame(&frame)) return -1; From d7f28a0ffe36f73fe08729202f9c49ca5cac1dd4 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:23:12 +0200 Subject: [PATCH 7/8] canfdtest: can_echo_dut(): check received frame for consistency Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/canfdtest.c b/canfdtest.c index 1f68f67..ba5c239 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -197,6 +197,34 @@ static int send_frame(struct can_frame *frame) return 0; } +static int check_frame(const struct can_frame *frame) +{ + int err = 0; + int i; + + if (frame->can_id != CAN_MSG_ID) { + printf("unexpected Message ID 0x%04x!\n", frame->can_id); + err = -1; + } + + if (frame->can_dlc != CAN_MSG_LEN) { + printf("unexpected Message length %d!\n", frame->can_dlc); + err = -1; + } + + for (i = 1; i < frame->can_dlc; i++) { + if (frame->data[i] != frame->data[i-1]) { + printf("Frame inconsistent!\n"); + print_frame(frame, 0); + err = -1; + goto out; + } + } + + out: + return err; +} + static void inc_frame(struct can_frame *frame) { int i; @@ -221,6 +249,7 @@ static int can_echo_dut(void) print_frame(&frame, 0); } + check_frame(&frame); inc_frame(&frame); if (send_frame(&frame)) return -1; From 13ecaa6d7ee862bb32c6b4fcb0bfc5132044c150 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 22 Jun 2020 11:24:26 +0200 Subject: [PATCH 8/8] canfdtest: make number of frames in flight a command line option Signed-off-by: Marc Kleine-Budde --- canfdtest.c | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/canfdtest.c b/canfdtest.c index ba5c239..ba4bfe6 100644 --- a/canfdtest.c +++ b/canfdtest.c @@ -49,6 +49,7 @@ static int verbose; static int sockfd; static int test_loops; static int exit_sig; +static int inflight_count = CAN_MSG_COUNT; static void print_usage(char *prg) { @@ -61,6 +62,7 @@ static void print_usage(char *prg) " -vv (high verbosity)\n" " -g (generate messages)\n" " -l COUNT (test loop count)\n" + " -f COUNT (number of frames in flight, default: %d)\n" "\n" "With the option '-g' CAN messages are generated and checked\n" "on , otherwise all messages received on the\n" @@ -72,7 +74,7 @@ static void print_usage(char *prg) "%s -v can0\n" "\ton Host:\n" "%s -g -v can2\n", - prg, prg, prg, prg); + prg, prg, CAN_MSG_COUNT, prg, prg); exit(1); } @@ -269,15 +271,26 @@ static int can_echo_dut(void) static int can_echo_gen(void) { - struct can_frame tx_frames[CAN_MSG_COUNT] = { }; - int recv_tx[CAN_MSG_COUNT]; + struct can_frame *tx_frames; + int *recv_tx; struct can_frame rx_frame; unsigned char counter = 0; int send_pos = 0, recv_rx_pos = 0, recv_tx_pos = 0, unprocessed = 0, loops = 0; + int err = 0; int i; + tx_frames = calloc(inflight_count, sizeof(* tx_frames)); + if (!tx_frames) + return -1; + + recv_tx = calloc(inflight_count, sizeof(* recv_tx)); + if (!recv_tx) { + err = -1; + goto out_free_tx_frames; + } + while (running) { - if (unprocessed < CAN_MSG_COUNT) { + if (unprocessed < inflight_count) { /* still send messages */ tx_frames[send_pos].can_dlc = CAN_MSG_LEN; tx_frames[send_pos].can_id = CAN_MSG_ID; @@ -285,11 +298,13 @@ static int can_echo_gen(void) for (i = 0; i < CAN_MSG_LEN; i++) tx_frames[send_pos].data[i] = counter + i; - if (send_frame(&tx_frames[send_pos])) - return -1; + if (send_frame(&tx_frames[send_pos])) { + err = -1; + goto out_free; + } send_pos++; - if (send_pos == CAN_MSG_COUNT) + if (send_pos == inflight_count) send_pos = 0; unprocessed++; if (verbose == 1) @@ -301,8 +316,10 @@ static int can_echo_gen(void) else millisleep(1); } else { - if (recv_frame(&rx_frame)) - return -1; + if (recv_frame(&rx_frame)) { + err = -1; + goto out_free; + } if (verbose > 1) print_frame(&rx_frame, 0); @@ -312,7 +329,7 @@ static int can_echo_gen(void) compare_frame(&tx_frames[recv_tx_pos], &rx_frame, 0); recv_tx[recv_tx_pos] = 1; recv_tx_pos++; - if (recv_tx_pos == CAN_MSG_COUNT) + if (recv_tx_pos == inflight_count) recv_tx_pos = 0; continue; } else { @@ -323,7 +340,7 @@ static int can_echo_gen(void) /* compare with expected */ compare_frame(&tx_frames[recv_rx_pos], &rx_frame, 1); recv_rx_pos++; - if (recv_rx_pos == CAN_MSG_COUNT) + if (recv_rx_pos == inflight_count) recv_rx_pos = 0; } @@ -337,7 +354,12 @@ static int can_echo_gen(void) printf("\nTest messages sent and received: %d\n", loops); - return 0; + out_free: + free(recv_tx); + out_free_tx_frames: + free(tx_frames); + + return err; } int main(int argc, char *argv[]) @@ -353,11 +375,14 @@ int main(int argc, char *argv[]) signal(SIGHUP, signal_handler); signal(SIGINT, signal_handler); - while ((opt = getopt(argc, argv, "gl:v?")) != -1) { + while ((opt = getopt(argc, argv, "f:gl:v?")) != -1) { switch (opt) { case 'v': verbose++; break; + case 'f': + inflight_count = atoi(optarg); + break; case 'l': test_loops = atoi(optarg);