From 5c433340c3b6306a0cce33d68864dccaa5db71f0 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 17:22:20 +0100 Subject: [PATCH 1/7] cansequence: convert towards kernel coding style --- cansequence.c | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/cansequence.c b/cansequence.c index 15a5540..7dc9b4d 100644 --- a/cansequence.c +++ b/cansequence.c @@ -27,8 +27,8 @@ #include #include -#define CAN_ID_DEFAULT (2) -#define ANYDEV "any" /* name of interface to receive from any CAN interface */ +#define CAN_ID_DEFAULT (2) +#define ANYDEV "any" /* name of interface to receive from any CAN interface */ extern int optind, opterr, optopt; @@ -47,12 +47,15 @@ static struct can_frame frame = { .can_dlc = 1, }; static struct can_filter filter[] = { - { .can_id = CAN_ID_DEFAULT, }, + { + .can_id = CAN_ID_DEFAULT, + }, }; static void print_usage(char *prg) { - fprintf(stderr, "Usage: %s [] [Options]\n" + fprintf(stderr, + "Usage: %s [] [Options]\n" "\n" "cansequence sends CAN messages with a rising sequence number as payload.\n" "When the -r option is given, cansequence expects to receive these messages\n" @@ -77,7 +80,6 @@ static void sig_handler(int signo) signal_num = signo; } - static void do_receive() { uint8_t ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))]; @@ -181,14 +183,13 @@ static void do_receive() sequence = sequence_rx; overflow_old = overflow; - } else if (verbose > 1) { + } else if (verbose > 1) { printf("sequence CNT: 0x%07x RX: 0x%02x\n", sequence, sequence_rx); } sequence++; if (verbose && !(sequence & sequence_mask)) printf("sequence wrap around (%d)\n", sequence_wrap++); - } } @@ -203,7 +204,7 @@ static void do_send() if (verbose > 1) printf("sending frame. sequence number: %d\n", sequence); - again: +again: len = write(s, &frame, sizeof(frame)); if (len == -1) { switch (errno) { @@ -211,8 +212,8 @@ static void do_send() int err; struct pollfd fds[] = { { - .fd = s, - .events = POLLOUT, + .fd = s, + .events = POLLOUT, }, }; @@ -227,7 +228,7 @@ static void do_send() exit(EXIT_FAILURE); } } - case EINTR: /* fallthrough */ + case EINTR: /* fallthrough */ goto again; default: perror("write"); @@ -261,15 +262,15 @@ int main(int argc, char **argv) sigaction(SIGHUP, &act, NULL); struct option long_options[] = { - { "extended", no_argument, 0, 'e' }, - { "identifier", required_argument, 0, 'i' }, - { "loop", required_argument, 0, 'l' }, - { "poll", no_argument, 0, 'p' }, - { "quit", optional_argument, 0, 'q' }, - { "receive", no_argument, 0, 'r' }, - { "verbose", no_argument, 0, 'v' }, - { "help", no_argument, 0, 'h' }, - { 0, 0, 0, 0}, + { "extended", no_argument, 0, 'e' }, + { "identifier", required_argument, 0, 'i' }, + { "loop", required_argument, 0, 'l' }, + { "poll", no_argument, 0, 'p' }, + { "quit", optional_argument, 0, 'q' }, + { "receive", no_argument, 0, 'r' }, + { "verbose", no_argument, 0, 'v' }, + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0 }, }; while ((opt = getopt_long(argc, argv, "ei:pq::rvh", long_options, NULL)) != -1) { @@ -327,11 +328,11 @@ int main(int argc, char **argv) if (extended) { filter->can_mask = CAN_EFF_MASK; - filter->can_id &= CAN_EFF_MASK; - filter->can_id |= CAN_EFF_FLAG; + filter->can_id &= CAN_EFF_MASK; + filter->can_id |= CAN_EFF_FLAG; } else { filter->can_mask = CAN_SFF_MASK; - filter->can_id &= CAN_SFF_MASK; + filter->can_id &= CAN_SFF_MASK; } frame.can_id = filter->can_id; filter->can_mask |= CAN_EFF_FLAG; From e8a808e2ab2f38590bee729395d7c1320c674884 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 17:35:51 +0100 Subject: [PATCH 2/7] cansequence: convert extended, receive from int -> bool --- cansequence.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cansequence.c b/cansequence.c index 7dc9b4d..63322b8 100644 --- a/cansequence.c +++ b/cansequence.c @@ -253,8 +253,8 @@ int main(int argc, char **argv) .can_family = AF_CAN, }; char *interface = "can0"; - int extended = 0; - int receive = 0; + bool extended = false; + bool receive = false; int opt; sigaction(SIGINT, &act, NULL); From 0b0bce87c41f681e1bea8538060c31c6d5345558 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 17:31:21 +0100 Subject: [PATCH 3/7] cansequence: also show help for option "-?" This avoids the "invalid option" error message in the auto generated man pages. | DESCRIPTION | ./cansequence: invalid option -- '?' --- cansequence.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index 63322b8..5d09e8a 100644 --- a/cansequence.c +++ b/cansequence.c @@ -273,7 +273,7 @@ int main(int argc, char **argv) { 0, 0, 0, 0 }, }; - while ((opt = getopt_long(argc, argv, "ei:pq::rvh", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "ei:pq::rvh?", long_options, NULL)) != -1) { switch (opt) { case 'e': extended = true; @@ -312,6 +312,7 @@ int main(int argc, char **argv) break; case 'h': + case '?': print_usage(basename(argv[0])); exit(EXIT_SUCCESS); break; From 92a99c33ad360952d4979cfac013ed821cadccef Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 17:37:31 +0100 Subject: [PATCH 4/7] cansequence: print_usage(): convert tabs to spaces in help text --- cansequence.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cansequence.c b/cansequence.c index 5d09e8a..d5c5ad4 100644 --- a/cansequence.c +++ b/cansequence.c @@ -63,14 +63,14 @@ static void print_usage(char *prg) "The main purpose of this program is to test the reliability of CAN links.\n" "\n" "Options:\n" - " -e, --extended send extended frame\n" - " -i, --identifier=ID CAN Identifier (default = %u)\n" - " --loop=COUNT send message COUNT times\n" - " -p, --poll use poll(2) to wait for buffer space while sending\n" - " -q, --quit quit if wrong sequences are encountered\n" - " -r, --receive work as receiver\n" - " -v, --verbose be verbose (twice to be even more verbose\n" - " -h, --help this help\n", + " -e, --extended send extended frame\n" + " -i, --identifier=ID CAN Identifier (default = %u)\n" + " --loop=COUNT send message COUNT times\n" + " -p, --poll use poll(2) to wait for buffer space while sending\n" + " -q, --quit quit if wrong sequences are encountered\n" + " -r, --receive work as receiver\n" + " -v, --verbose be verbose (twice to be even more verbose\n" + " -h, --help this help\n", prg, CAN_ID_DEFAULT); } From 6695f6f5575d9f83c9ee05feff3b8a2577fc54e0 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 18:21:51 +0100 Subject: [PATCH 5/7] cansequence: print_usage(): clarify help for -e, --extended option --- cansequence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index d5c5ad4..4e933ec 100644 --- a/cansequence.c +++ b/cansequence.c @@ -63,7 +63,7 @@ static void print_usage(char *prg) "The main purpose of this program is to test the reliability of CAN links.\n" "\n" "Options:\n" - " -e, --extended send extended frame\n" + " -e, --extended send/receive extended frames\n" " -i, --identifier=ID CAN Identifier (default = %u)\n" " --loop=COUNT send message COUNT times\n" " -p, --poll use poll(2) to wait for buffer space while sending\n" From bd835f2ca2929077da25dfc771dbe1cc312ce70d Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 18:06:25 +0100 Subject: [PATCH 6/7] cansequence: add support to send and receive CAN-FD --- cansequence.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/cansequence.c b/cansequence.c index 4e933ec..4e97879 100644 --- a/cansequence.c +++ b/cansequence.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -// Copyright (c) 2007, 2008, 2009, 2010, 2014, 2015, 2019 Pengutronix, +// Copyright (c) 2007, 2008, 2009, 2010, 2014, 2015, 2019, 2023 Pengutronix, // Marc Kleine-Budde // Copyright (c) 2005 Pengutronix, // Sascha Hauer @@ -36,6 +36,7 @@ static int s = -1; static bool running = true; static volatile sig_atomic_t signal_num; static bool infinite = true; +static bool canfd = false; static unsigned int drop_until_quit; static unsigned int drop_count; static bool use_poll = false; @@ -43,8 +44,8 @@ static bool use_poll = false; static unsigned int loopcount = 1; static int verbose; -static struct can_frame frame = { - .can_dlc = 1, +static struct canfd_frame frame = { + .len = 1, }; static struct can_filter filter[] = { { @@ -64,6 +65,8 @@ static void print_usage(char *prg) "\n" "Options:\n" " -e, --extended send/receive extended frames\n" + " -f, --canfd send/receive CAN-FD CAN frames\n" + " -b, --brs send CAN-FD CAN frames with bitrate switch (BRS)\n" " -i, --identifier=ID CAN Identifier (default = %u)\n" " --loop=COUNT send message COUNT times\n" " -p, --poll use poll(2) to wait for buffer space while sending\n" @@ -100,6 +103,12 @@ static void do_receive() uint32_t sequence = 0; unsigned int overflow_old = 0; can_err_mask_t err_mask = CAN_ERR_MASK; + size_t mtu; + + if (canfd) + mtu = CANFD_MTU; + else + mtu = CAN_MTU; if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL, &dropmonitor_on, sizeof(dropmonitor_on)) < 0) { @@ -121,7 +130,7 @@ static void do_receive() while ((infinite || loopcount--) && running) { ssize_t nbytes; - msg.msg_iov[0].iov_len = sizeof(frame); + msg.msg_iov[0].iov_len = mtu; msg.msg_controllen = sizeof(ctrlmsg); msg.msg_flags = 0; @@ -197,6 +206,12 @@ static void do_send() { unsigned int seq_wrap = 0; uint8_t sequence = 0; + size_t mtu; + + if (canfd) + mtu = CANFD_MTU; + else + mtu = CAN_MTU; while ((infinite || loopcount--) && running) { ssize_t len; @@ -205,7 +220,7 @@ static void do_send() printf("sending frame. sequence number: %d\n", sequence); again: - len = write(s, &frame, sizeof(frame)); + len = write(s, &frame, mtu); if (len == -1) { switch (errno) { case ENOBUFS: { @@ -254,6 +269,7 @@ int main(int argc, char **argv) }; char *interface = "can0"; bool extended = false; + bool brs = false; bool receive = false; int opt; @@ -263,6 +279,8 @@ int main(int argc, char **argv) struct option long_options[] = { { "extended", no_argument, 0, 'e' }, + { "canfd", no_argument, 0, 'f' }, + { "brs", no_argument, 0, 'b' }, { "identifier", required_argument, 0, 'i' }, { "loop", required_argument, 0, 'l' }, { "poll", no_argument, 0, 'p' }, @@ -273,12 +291,21 @@ int main(int argc, char **argv) { 0, 0, 0, 0 }, }; - while ((opt = getopt_long(argc, argv, "ei:pq::rvh?", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "efbi:pq::rvh?", long_options, NULL)) != -1) { switch (opt) { case 'e': extended = true; break; + case 'f': + canfd = true; + break; + + case 'b': + brs = true; /* bitrate switch implies CAN-FD */ + canfd = true; + break; + case 'i': filter->can_id = strtoul(optarg, NULL, 0); break; @@ -360,6 +387,33 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } + if (canfd) { + const int enable_canfd = 1; + struct ifreq ifr; + + strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); + + /* check if the frame fits into the CAN netdevice */ + if (ioctl(s, SIOCGIFMTU, &ifr) < 0) { + perror("SIOCGIFMTU"); + exit(EXIT_FAILURE); + } + + if (ifr.ifr_mtu != CANFD_MTU && ifr.ifr_mtu != CANXL_MTU) { + printf("CAN interface is only Classical CAN capable - sorry.\n"); + exit(EXIT_FAILURE); + } + + /* interface is ok - try to switch the socket into CAN FD mode */ + if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))) { + printf("error when enabling CAN FD support\n"); + exit(EXIT_FAILURE); + } + } + + if (brs) + frame.flags |= CANFD_BRS; + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind()"); exit(EXIT_FAILURE); From 642190647e8ebe7c869239bd27a30f6c2c473eca Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 29 Nov 2023 18:28:32 +0100 Subject: [PATCH 7/7] cansequence: add option to ignore classical CAN frames --- cansequence.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index 4e97879..f739b7a 100644 --- a/cansequence.c +++ b/cansequence.c @@ -37,6 +37,7 @@ static bool running = true; static volatile sig_atomic_t signal_num; static bool infinite = true; static bool canfd = false; +static bool canfd_strict = false; static unsigned int drop_until_quit; static unsigned int drop_count; static bool use_poll = false; @@ -66,6 +67,7 @@ static void print_usage(char *prg) "Options:\n" " -e, --extended send/receive extended frames\n" " -f, --canfd send/receive CAN-FD CAN frames\n" + " -s, --strict refuse classical CAN frames in CAN-FD mode\n" " -b, --brs send CAN-FD CAN frames with bitrate switch (BRS)\n" " -i, --identifier=ID CAN Identifier (default = %u)\n" " --loop=COUNT send message COUNT times\n" @@ -151,6 +153,12 @@ static void do_receive() sequence_rx = frame.data[0]; + if (canfd_strict && nbytes == CAN_MTU) { + if (verbose > 1) + printf("sequence CNT: 0x%07x RX: 0x%02x (ignoring classical CAN frame)\n", sequence, sequence_rx); + continue; + } + if (sequence_init) { sequence_init = false; sequence = sequence_rx; @@ -291,7 +299,7 @@ int main(int argc, char **argv) { 0, 0, 0, 0 }, }; - while ((opt = getopt_long(argc, argv, "efbi:pq::rvh?", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "efsbi:pq::rvh?", long_options, NULL)) != -1) { switch (opt) { case 'e': extended = true; @@ -301,6 +309,10 @@ int main(int argc, char **argv) canfd = true; break; + case 's': + canfd_strict = true; + break; + case 'b': brs = true; /* bitrate switch implies CAN-FD */ canfd = true; @@ -409,6 +421,8 @@ int main(int argc, char **argv) printf("error when enabling CAN FD support\n"); exit(EXIT_FAILURE); } + } else { + canfd_strict = false; } if (brs)