From d8435a56afeb4f170c3d0a290c186003d3250143 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Thu, 25 Aug 2005 15:39:17 +0000 Subject: [PATCH 01/34] add cansequence tool git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-1.0-trunk@40 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 cansequence.c diff --git a/cansequence.c b/cansequence.c new file mode 100644 index 0000000..16a81b5 --- /dev/null +++ b/cansequence.c @@ -0,0 +1,193 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +extern int optind, opterr, optopt; + +static int s = -1; +static int running = 1; + +enum +{ + VERSION_OPTION = CHAR_MAX + 1, +}; + +void print_usage(char *prg) +{ + 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" + "and prints an error message if a wrong sequence number is encountered.\n" + "The main purpose of this program is to test the reliability of CAN links.\n" + "\n" + "Options:\n" + " -f, --family=FAMILY Protocol family (default PF_CAN = %d)\n" + " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" + " -p, --protocol=PROTO CAN protocol (default CAN_PROTO_RAW = %d)\n" + " -r, --receive work as receiver\n" + " -l --loop=COUNT send COUNT messages\n" + " -q --quit quit if a wrong sequence is encountered\n" + " -v, --verbose be verbose\n" + " -h --help this help\n" + " --version print version information and exit\n", + prg, PF_CAN, SOCK_RAW, CAN_PROTO_RAW); +} + +void sigterm(int signo) +{ + running = 0; +} + +int main(int argc, char **argv) +{ + int family = PF_CAN, type = SOCK_RAW, proto = CAN_PROTO_RAW; + int opt; + struct sockaddr_can addr; + struct can_frame frame; + int nbytes; + struct ifreq ifr; + int receive = 0; + int loopcount = 1, infinite = 1; + unsigned char sequence = 0; + int sequence_init = 1; + int verbose = 0, quit = 0; + + signal(SIGTERM, sigterm); + signal(SIGHUP, sigterm); + + struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { "family", required_argument, 0, 'f' }, + { "protocol", required_argument, 0, 'p' }, + { "type", required_argument, 0, 't' }, + { "version", no_argument, 0, VERSION_OPTION}, + { "receive", no_argument, 0, 'r'}, + { "quit", no_argument, 0, 'q'}, + { "loop", required_argument, 0, 'l'}, + { "verbose", no_argument, 0, 'v'}, + { 0, 0, 0, 0}, + }; + + while ((opt = getopt_long(argc, argv, "f:t:p:vrl:hq", long_options, NULL)) != -1) { + switch (opt) { + case 'h': + print_usage(basename(argv[0])); + exit(0); + + case 'f': + family = strtoul(optarg, NULL, 0); + break; + + case 't': + type = strtoul(optarg, NULL, 0); + break; + + case 'p': + proto = strtoul(optarg, NULL, 0); + break; + + case 'l': + loopcount = strtoul(optarg, NULL, 0); + infinite = 0; + break; + + case 'r': + receive = 1; + break; + + case 'q': + quit = 1; + break; + + case 'v': + verbose = 1; + break; + + case VERSION_OPTION: + printf("cansequence %s\n",VERSION); + exit(0); + + default: + fprintf(stderr, "Unknown option %c\n", opt); + break; + } + } + + if (optind == argc) { + print_usage(basename(argv[0])); + exit(0); + } + + printf("interface = %s, family = %d, type = %d, proto = %d\n", + argv[optind], family, type, proto); + if ((s = socket(family, type, proto)) < 0) { + perror("socket"); + return 1; + } + + addr.can_family = family; + strncpy(ifr.ifr_name, argv[optind], sizeof(ifr.ifr_name)); + ioctl(s, SIOCGIFINDEX, &ifr); + addr.can_ifindex = ifr.ifr_ifindex; + addr.can_id = CAN_FLAG_ALL; + + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("bind"); + return 1; + } + + if(receive) { + while ((infinite || loopcount--) && running) { + if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) { + perror("read"); + return 1; + } else { + if( sequence_init ) { + sequence_init = 0; + sequence = frame.payload.data[0]; + } + if(verbose) + printf("received frame. sequence number: %d\n",sequence); + if( frame.payload.data[0] != sequence) { + printf("received wrong sequence count. expected: %d, got: %d\n", + frame.payload.data[0], sequence); + if(quit) + exit(1); + sequence = frame.payload.data[0]; + } + sequence++; + } + } + } else { + frame.can_dlc = 1; + frame.can_id = 2; + frame.payload.data[0] = 0; + while ((infinite || loopcount--) && running) { + if(verbose) + printf("sending frame. sequence number: %d\n",sequence); + if( write(s, &frame, sizeof(frame)) < 0) { + perror("write"); + break; + } + (unsigned char)frame.payload.data[0]++; + sequence++; +// printf("%d %d\n",sequence,(unsigned char)frame.payload.data[0]); +// usleep(10000); + } + } + return 0; +} From c55d1813601d253ce792fcb3447e79f94a09bf6c Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Thu, 25 Aug 2005 15:59:21 +0000 Subject: [PATCH 02/34] added twice verbose option git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-1.0-trunk@41 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cansequence.c b/cansequence.c index 16a81b5..5911594 100644 --- a/cansequence.c +++ b/cansequence.c @@ -41,7 +41,7 @@ void print_usage(char *prg) " -r, --receive work as receiver\n" " -l --loop=COUNT send COUNT messages\n" " -q --quit quit if a wrong sequence is encountered\n" - " -v, --verbose be verbose\n" + " -v, --verbose be verbose (twice to be even more verbose\n" " -h --help this help\n" " --version print version information and exit\n", prg, PF_CAN, SOCK_RAW, CAN_PROTO_RAW); @@ -114,7 +114,7 @@ int main(int argc, char **argv) break; case 'v': - verbose = 1; + verbose++; break; case VERSION_OPTION: @@ -160,7 +160,7 @@ int main(int argc, char **argv) sequence_init = 0; sequence = frame.payload.data[0]; } - if(verbose) + if(verbose>1) printf("received frame. sequence number: %d\n",sequence); if( frame.payload.data[0] != sequence) { printf("received wrong sequence count. expected: %d, got: %d\n", @@ -169,6 +169,8 @@ int main(int argc, char **argv) exit(1); sequence = frame.payload.data[0]; } + if(verbose && !sequence) + printf("sequence wrap around\n"); sequence++; } } @@ -177,16 +179,16 @@ int main(int argc, char **argv) frame.can_id = 2; frame.payload.data[0] = 0; while ((infinite || loopcount--) && running) { - if(verbose) + if(verbose>1) printf("sending frame. sequence number: %d\n",sequence); + if(verbose && !sequence) + printf("sequence wrap around\n"); if( write(s, &frame, sizeof(frame)) < 0) { perror("write"); break; } (unsigned char)frame.payload.data[0]++; sequence++; -// printf("%d %d\n",sequence,(unsigned char)frame.payload.data[0]); -// usleep(10000); } } return 0; From fc3b0449a91ac115eb6bd3d15bd005e9751fe5b1 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Wed, 5 Oct 2005 10:16:32 +0000 Subject: [PATCH 03/34] use unsigned elements git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-1.0-trunk@49 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cansequence.c b/cansequence.c index 5911594..e569b1b 100644 --- a/cansequence.c +++ b/cansequence.c @@ -158,16 +158,16 @@ int main(int argc, char **argv) } else { if( sequence_init ) { sequence_init = 0; - sequence = frame.payload.data[0]; + sequence = frame.payload.data_u8[0]; } if(verbose>1) printf("received frame. sequence number: %d\n",sequence); - if( frame.payload.data[0] != sequence) { + if( frame.payload.data_u8[0] != sequence) { printf("received wrong sequence count. expected: %d, got: %d\n", - frame.payload.data[0], sequence); + frame.payload.data_u8[0], sequence); if(quit) exit(1); - sequence = frame.payload.data[0]; + sequence = frame.payload.data_u8[0]; } if(verbose && !sequence) printf("sequence wrap around\n"); From 3794644caa13151679d49271e7a20d79d2efd566 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 20 Sep 2007 11:51:29 +0000 Subject: [PATCH 04/34] git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-2.0-trunk@71 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/cansequence.c b/cansequence.c index e569b1b..c5492ba 100644 --- a/cansequence.c +++ b/cansequence.c @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -12,8 +14,9 @@ #include #include -#include -#include +#include +#include +#include extern int optind, opterr, optopt; @@ -37,14 +40,14 @@ void print_usage(char *prg) "Options:\n" " -f, --family=FAMILY Protocol family (default PF_CAN = %d)\n" " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" - " -p, --protocol=PROTO CAN protocol (default CAN_PROTO_RAW = %d)\n" + " -p, --protocol=PROTO CAN protocol (default CAN_RAW = %d)\n" " -r, --receive work as receiver\n" " -l --loop=COUNT send COUNT messages\n" " -q --quit quit if a wrong sequence is encountered\n" " -v, --verbose be verbose (twice to be even more verbose\n" " -h --help this help\n" " --version print version information and exit\n", - prg, PF_CAN, SOCK_RAW, CAN_PROTO_RAW); + prg, PF_CAN, SOCK_RAW, CAN_RAW); } void sigterm(int signo) @@ -54,7 +57,7 @@ void sigterm(int signo) int main(int argc, char **argv) { - int family = PF_CAN, type = SOCK_RAW, proto = CAN_PROTO_RAW; + int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; int opt; struct sockaddr_can addr; struct can_frame frame; @@ -143,7 +146,6 @@ int main(int argc, char **argv) strncpy(ifr.ifr_name, argv[optind], sizeof(ifr.ifr_name)); ioctl(s, SIOCGIFINDEX, &ifr); addr.can_ifindex = ifr.ifr_ifindex; - addr.can_id = CAN_FLAG_ALL; if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); @@ -156,18 +158,18 @@ int main(int argc, char **argv) perror("read"); return 1; } else { - if( sequence_init ) { + if (sequence_init) { sequence_init = 0; - sequence = frame.payload.data_u8[0]; + sequence = frame.data[0]; } - if(verbose>1) + if (verbose>1) printf("received frame. sequence number: %d\n",sequence); - if( frame.payload.data_u8[0] != sequence) { + if (frame.data[0] != sequence) { printf("received wrong sequence count. expected: %d, got: %d\n", - frame.payload.data_u8[0], sequence); + frame.data[0], sequence); if(quit) exit(1); - sequence = frame.payload.data_u8[0]; + sequence = frame.data[0]; } if(verbose && !sequence) printf("sequence wrap around\n"); @@ -177,17 +179,17 @@ int main(int argc, char **argv) } else { frame.can_dlc = 1; frame.can_id = 2; - frame.payload.data[0] = 0; + frame.data[0] = 0; while ((infinite || loopcount--) && running) { - if(verbose>1) + if (verbose>1) printf("sending frame. sequence number: %d\n",sequence); - if(verbose && !sequence) + if (verbose && !sequence) printf("sequence wrap around\n"); - if( write(s, &frame, sizeof(frame)) < 0) { + if (write(s, &frame, sizeof(frame)) < 0) { perror("write"); break; } - (unsigned char)frame.payload.data[0]++; + (unsigned char)frame.data[0]++; sequence++; } } From ac8ae17727812b26ce7a753aa99b42dd1705ea0f Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 10 Oct 2007 18:28:18 +0000 Subject: [PATCH 05/34] * src/cansequence.c: fixed out of order expected and received value git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-2.0-trunk@84 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index c5492ba..f452c24 100644 --- a/cansequence.c +++ b/cansequence.c @@ -166,7 +166,7 @@ int main(int argc, char **argv) printf("received frame. sequence number: %d\n",sequence); if (frame.data[0] != sequence) { printf("received wrong sequence count. expected: %d, got: %d\n", - frame.data[0], sequence); + sequence, frame.data[0]); if(quit) exit(1); sequence = frame.data[0]; From 0c888d794280156ab056c09ca02905c81a00ae60 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 10 Oct 2007 18:46:08 +0000 Subject: [PATCH 06/34] * src/cansequence.c: fixed print of received frame number git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-2.0-trunk@85 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index f452c24..22d31d9 100644 --- a/cansequence.c +++ b/cansequence.c @@ -163,7 +163,7 @@ int main(int argc, char **argv) sequence = frame.data[0]; } if (verbose>1) - printf("received frame. sequence number: %d\n",sequence); + printf("received frame. sequence number: %d\n", frame.data[0]); if (frame.data[0] != sequence) { printf("received wrong sequence count. expected: %d, got: %d\n", sequence, frame.data[0]); From b3b501d92222ee91a37de742641a2f3e903169fa Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 26 Aug 2008 15:45:01 +0000 Subject: [PATCH 07/34] git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@88 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cansequence.c b/cansequence.c index 22d31d9..3510a91 100644 --- a/cansequence.c +++ b/cansequence.c @@ -1,22 +1,22 @@ #include +#include +#include +#include #include #include -#include #include -#include -#include -#include +#include -#include -#include -#include -#include #include +#include +#include +#include +#include + #include #include -#include extern int optind, opterr, optopt; From d8be4d8a4fdedcb4210bb4cf46ab9787f5c7160d Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Fri, 5 Sep 2008 15:03:33 +0000 Subject: [PATCH 08/34] * src/canecho.c, src/candump.c, src/cansequence.c, src/cansend.c: include limits.h git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@89 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cansequence.c b/cansequence.c index 3510a91..40839c8 100644 --- a/cansequence.c +++ b/cansequence.c @@ -7,6 +7,7 @@ #include #include #include +#include #include From bd3ea25fe1df68ecb74728f39efbd5bfc3e1b467 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 9 Sep 2008 15:18:51 +0000 Subject: [PATCH 09/34] * src/cansequence.c, src/cansend.c: cleanups git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@90 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 157 ++++++++++++++++++++++++++------------------------ 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/cansequence.c b/cansequence.c index 40839c8..c2baf39 100644 --- a/cansequence.c +++ b/cansequence.c @@ -21,34 +21,34 @@ extern int optind, opterr, optopt; -static int s = -1; -static int running = 1; +static int s = -1; +static int running = 1; -enum -{ +enum { VERSION_OPTION = CHAR_MAX + 1, }; void print_usage(char *prg) { - 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" - "and prints an error message if a wrong sequence number is encountered.\n" - "The main purpose of this program is to test the reliability of CAN links.\n" - "\n" - "Options:\n" - " -f, --family=FAMILY Protocol family (default PF_CAN = %d)\n" - " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" - " -p, --protocol=PROTO CAN protocol (default CAN_RAW = %d)\n" - " -r, --receive work as receiver\n" - " -l --loop=COUNT send COUNT messages\n" - " -q --quit quit if a wrong sequence is encountered\n" - " -v, --verbose be verbose (twice to be even more verbose\n" - " -h --help this help\n" - " --version print version information and exit\n", - prg, PF_CAN, SOCK_RAW, CAN_RAW); + 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" + "and prints an error message if a wrong sequence number is encountered.\n" + "The main purpose of this program is to test the reliability of CAN links.\n" + "\n" + "Options:\n" + " -f, --family=FAMILY Protocol family (default PF_CAN = %d)\n" + " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" + " -p, --protocol=PROTO CAN protocol (default CAN_RAW = %d)\n" + " -r, --receive work as receiver\n" + " -l COUNT send COUNT messages, infinite if omitted\n" + " --loop=COUNT \n" + " -q --quit quit if a wrong sequence is encountered\n" + " -v, --verbose be verbose (twice to be even more verbose\n" + " -h --help this help\n" + " --version print version information and exit\n", + prg, PF_CAN, SOCK_RAW, CAN_RAW); } void sigterm(int signo) @@ -65,7 +65,7 @@ int main(int argc, char **argv) int nbytes; struct ifreq ifr; int receive = 0; - int loopcount = 1, infinite = 1; + int loopcount = 1, infinite; unsigned char sequence = 0; int sequence_init = 1; int verbose = 0, quit = 0; @@ -73,61 +73,63 @@ int main(int argc, char **argv) signal(SIGTERM, sigterm); signal(SIGHUP, sigterm); - struct option long_options[] = { - { "help", no_argument, 0, 'h' }, - { "family", required_argument, 0, 'f' }, - { "protocol", required_argument, 0, 'p' }, - { "type", required_argument, 0, 't' }, - { "version", no_argument, 0, VERSION_OPTION}, - { "receive", no_argument, 0, 'r'}, - { "quit", no_argument, 0, 'q'}, - { "loop", required_argument, 0, 'l'}, - { "verbose", no_argument, 0, 'v'}, - { 0, 0, 0, 0}, + struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { "family", required_argument, 0, 'f' }, + { "protocol", required_argument, 0, 'p' }, + { "type", required_argument, 0, 't' }, + { "version", no_argument, 0, VERSION_OPTION}, + { "receive", no_argument, 0, 'r'}, + { "quit", no_argument, 0, 'q'}, + { "loop", optional_argument, 0, 'l'}, + { "verbose", no_argument, 0, 'v'}, + { 0, 0, 0, 0}, }; - while ((opt = getopt_long(argc, argv, "f:t:p:vrl:hq", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "f:t:p:vrl::hq", long_options, NULL)) != -1) { switch (opt) { - case 'h': - print_usage(basename(argv[0])); - exit(0); + case 'h': + print_usage(basename(argv[0])); + exit(0); - case 'f': - family = strtoul(optarg, NULL, 0); - break; + case 'f': + family = strtoul(optarg, NULL, 0); + break; - case 't': - type = strtoul(optarg, NULL, 0); - break; + case 't': + type = strtoul(optarg, NULL, 0); + break; - case 'p': - proto = strtoul(optarg, NULL, 0); - break; + case 'p': + proto = strtoul(optarg, NULL, 0); + break; - case 'l': + case 'l': + if (optarg) loopcount = strtoul(optarg, NULL, 0); - infinite = 0; - break; + else + infinite = 1; + break; - case 'r': - receive = 1; - break; + case 'r': + receive = 1; + break; - case 'q': - quit = 1; - break; + case 'q': + quit = 1; + break; - case 'v': - verbose++; - break; + case 'v': + verbose++; + break; - case VERSION_OPTION: - printf("cansequence %s\n",VERSION); - exit(0); + case VERSION_OPTION: + printf("cansequence %s\n", VERSION); + exit(0); - default: - fprintf(stderr, "Unknown option %c\n", opt); - break; + default: + fprintf(stderr, "Unknown option %c\n", opt); + break; } } @@ -135,10 +137,12 @@ int main(int argc, char **argv) print_usage(basename(argv[0])); exit(0); } - + printf("interface = %s, family = %d, type = %d, proto = %d\n", - argv[optind], family, type, proto); - if ((s = socket(family, type, proto)) < 0) { + argv[optind], family, type, proto); + + s = socket(family, type, proto); + if (s < 0) { perror("socket"); return 1; } @@ -153,9 +157,10 @@ int main(int argc, char **argv) return 1; } - if(receive) { + if (receive) { while ((infinite || loopcount--) && running) { - if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) { + nbytes = read(s, &frame, sizeof(struct can_frame); + if (nbytes < 0) { perror("read"); return 1; } else { @@ -163,16 +168,16 @@ int main(int argc, char **argv) sequence_init = 0; sequence = frame.data[0]; } - if (verbose>1) + if (verbose > 1) printf("received frame. sequence number: %d\n", frame.data[0]); if (frame.data[0] != sequence) { printf("received wrong sequence count. expected: %d, got: %d\n", - sequence, frame.data[0]); - if(quit) + sequence, frame.data[0]); + if (quit) exit(1); sequence = frame.data[0]; } - if(verbose && !sequence) + if (verbose && !sequence) printf("sequence wrap around\n"); sequence++; } @@ -182,8 +187,8 @@ int main(int argc, char **argv) frame.can_id = 2; frame.data[0] = 0; while ((infinite || loopcount--) && running) { - if (verbose>1) - printf("sending frame. sequence number: %d\n",sequence); + if (verbose > 1) + printf("sending frame. sequence number: %d\n", sequence); if (verbose && !sequence) printf("sequence wrap around\n"); if (write(s, &frame, sizeof(frame)) < 0) { From f4cec293dcfe38046fa07e1be91f5bc820214889 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 9 Sep 2008 15:24:28 +0000 Subject: [PATCH 10/34] git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@91 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index c2baf39..9ad0b30 100644 --- a/cansequence.c +++ b/cansequence.c @@ -159,7 +159,7 @@ int main(int argc, char **argv) if (receive) { while ((infinite || loopcount--) && running) { - nbytes = read(s, &frame, sizeof(struct can_frame); + nbytes = read(s, &frame, sizeof(struct can_frame)); if (nbytes < 0) { perror("read"); return 1; From 5fa35cbe2a36a877d796428f0ca74b369b7b19e8 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 10 Sep 2008 11:24:25 +0000 Subject: [PATCH 11/34] git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@92 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/cansequence.c b/cansequence.c index 9ad0b30..8395c65 100644 --- a/cansequence.c +++ b/cansequence.c @@ -30,7 +30,7 @@ enum { 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" @@ -58,15 +58,16 @@ void sigterm(int signo) int main(int argc, char **argv) { - int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; - int opt; - struct sockaddr_can addr; struct can_frame frame; - int nbytes; struct ifreq ifr; + struct sockaddr_can addr; + char *interface = "can0"; + unsigned char sequence = 0; + int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; + int loopcount = 1, infinite = 0; + int nbytes; + int opt; int receive = 0; - int loopcount = 1, infinite; - unsigned char sequence = 0; int sequence_init = 1; int verbose = 0, quit = 0; @@ -133,13 +134,11 @@ int main(int argc, char **argv) } } - if (optind == argc) { - print_usage(basename(argv[0])); - exit(0); - } + if (optind != argc) + interface = argv[optind]; printf("interface = %s, family = %d, type = %d, proto = %d\n", - argv[optind], family, type, proto); + interface, family, type, proto); s = socket(family, type, proto); if (s < 0) { @@ -148,8 +147,11 @@ int main(int argc, char **argv) } addr.can_family = family; - strncpy(ifr.ifr_name, argv[optind], sizeof(ifr.ifr_name)); - ioctl(s, SIOCGIFINDEX, &ifr); + strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFINDEX, &ifr)) { + perror("ioctl"); + return 1; + } addr.can_ifindex = ifr.ifr_ifindex; if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { From c93d3a63c57be3f4ff883a8c1ece3248acd15e6a Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 29 Sep 2008 07:53:31 +0000 Subject: [PATCH 12/34] * src/cansequence.c, src/cansend.c: fix interface cmd line git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@95 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cansequence.c b/cansequence.c index 8395c65..30b5cd6 100644 --- a/cansequence.c +++ b/cansequence.c @@ -42,8 +42,8 @@ void print_usage(char *prg) " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" " -p, --protocol=PROTO CAN protocol (default CAN_RAW = %d)\n" " -r, --receive work as receiver\n" - " -l COUNT send COUNT messages, infinite if omitted\n" - " --loop=COUNT \n" + " -l send message infinite times\n" + " --loop=COUNT send message COUNT times\n" " -q --quit quit if a wrong sequence is encountered\n" " -v, --verbose be verbose (twice to be even more verbose\n" " -h --help this help\n" @@ -82,12 +82,12 @@ int main(int argc, char **argv) { "version", no_argument, 0, VERSION_OPTION}, { "receive", no_argument, 0, 'r'}, { "quit", no_argument, 0, 'q'}, - { "loop", optional_argument, 0, 'l'}, + { "loop", required_argument, 0, 'l'}, { "verbose", no_argument, 0, 'v'}, { 0, 0, 0, 0}, }; - while ((opt = getopt_long(argc, argv, "f:t:p:vrl::hq", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "f:t:p:vrlhq", long_options, NULL)) != -1) { switch (opt) { case 'h': print_usage(basename(argv[0])); @@ -134,8 +134,16 @@ int main(int argc, char **argv) } } - if (optind != argc) - interface = argv[optind]; + if (optind == argc) { + print_usage(basename(argv[0])); + exit(0); + } + + if (argv[optind] == NULL) { + fprintf(stderr, "No Interface supplied\n"); + exit(-1); + } + interface = argv[optind]; printf("interface = %s, family = %d, type = %d, proto = %d\n", interface, family, type, proto); From 6e3b123d5bda89fced739e74cad215aaca8f80d6 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 29 Sep 2008 11:19:04 +0000 Subject: [PATCH 13/34] git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@97 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/cansequence.c b/cansequence.c index 30b5cd6..8f90ee4 100644 --- a/cansequence.c +++ b/cansequence.c @@ -42,8 +42,7 @@ void print_usage(char *prg) " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" " -p, --protocol=PROTO CAN protocol (default CAN_RAW = %d)\n" " -r, --receive work as receiver\n" - " -l send message infinite times\n" - " --loop=COUNT send message COUNT times\n" + " --loop=COUNT send message COUNT times\n" " -q --quit quit if a wrong sequence is encountered\n" " -v, --verbose be verbose (twice to be even more verbose\n" " -h --help this help\n" @@ -64,7 +63,7 @@ int main(int argc, char **argv) char *interface = "can0"; unsigned char sequence = 0; int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; - int loopcount = 1, infinite = 0; + int loopcount = 1, infinite = 1; int nbytes; int opt; int receive = 0; @@ -134,11 +133,6 @@ int main(int argc, char **argv) } } - if (optind == argc) { - print_usage(basename(argv[0])); - exit(0); - } - if (argv[optind] == NULL) { fprintf(stderr, "No Interface supplied\n"); exit(-1); From a16f8ed47152b0c3c4f4cb7a17ec7eaf68c4239c Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 29 Sep 2008 11:20:54 +0000 Subject: [PATCH 14/34] git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@98 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cansequence.c b/cansequence.c index 8f90ee4..d7285f9 100644 --- a/cansequence.c +++ b/cansequence.c @@ -133,11 +133,8 @@ int main(int argc, char **argv) } } - if (argv[optind] == NULL) { - fprintf(stderr, "No Interface supplied\n"); - exit(-1); - } - interface = argv[optind]; + if (argv[optind] != NULL) + interface = argv[optind]; printf("interface = %s, family = %d, type = %d, proto = %d\n", interface, family, type, proto); From 4aaad290826ecb057c900771e08e82cdd67a7b83 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 9 Dec 2008 11:43:30 +0000 Subject: [PATCH 15/34] * src/cansequence.c: obay loop option git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@104 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansequence.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cansequence.c b/cansequence.c index d7285f9..48b879a 100644 --- a/cansequence.c +++ b/cansequence.c @@ -105,9 +105,10 @@ int main(int argc, char **argv) break; case 'l': - if (optarg) + if (optarg) { loopcount = strtoul(optarg, NULL, 0); - else + infinite = 0; + } else infinite = 1; break; From fb5903f226e666f1a2737f9ce174b1ea2609997e Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sun, 13 Sep 2009 18:09:51 +0200 Subject: [PATCH 16/34] [cansequece] added various improvements - added poll support if sender - add cmd line option for can_id - setup canfilter to disable receiving in sender mode Signed-off-by: Marc Kleine-Budde --- cansequence.c | 203 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 139 insertions(+), 64 deletions(-) diff --git a/cansequence.c b/cansequence.c index 48b879a..f86127b 100644 --- a/cansequence.c +++ b/cansequence.c @@ -1,13 +1,15 @@ #include +#include #include #include +#include +#include #include #include #include #include #include -#include #include @@ -28,6 +30,8 @@ enum { VERSION_OPTION = CHAR_MAX + 1, }; +#define CAN_ID_DEFAULT (2) + void print_usage(char *prg) { fprintf(stderr, "Usage: %s [] [Options]\n" @@ -38,16 +42,16 @@ void print_usage(char *prg) "The main purpose of this program is to test the reliability of CAN links.\n" "\n" "Options:\n" - " -f, --family=FAMILY Protocol family (default PF_CAN = %d)\n" - " -t, --type=TYPE Socket type, see man 2 socket (default SOCK_RAW = %d)\n" - " -p, --protocol=PROTO CAN protocol (default CAN_RAW = %d)\n" + " -e --extended send extended frame\n" + " -i, --identifier=ID CAN Identifier (default = %u)\n" " -r, --receive work as receiver\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 a wrong sequence is encountered\n" " -v, --verbose be verbose (twice to be even more verbose\n" " -h --help this help\n" " --version print version information and exit\n", - prg, PF_CAN, SOCK_RAW, CAN_RAW); + prg, CAN_ID_DEFAULT); } void sigterm(int signo) @@ -57,51 +61,75 @@ void sigterm(int signo) int main(int argc, char **argv) { - struct can_frame frame; struct ifreq ifr; struct sockaddr_can addr; + struct can_frame frame = { + .can_dlc = 1, + }; + struct can_filter filter[] = { + { + .can_id = CAN_ID_DEFAULT, + }, + }; char *interface = "can0"; unsigned char sequence = 0; int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; int loopcount = 1, infinite = 1; + int use_poll = 0; + int extended = 0; int nbytes; int opt; int receive = 0; int sequence_init = 1; int verbose = 0, quit = 0; + int exit_value = EXIT_SUCCESS; signal(SIGTERM, sigterm); signal(SIGHUP, sigterm); struct option long_options[] = { + { "extended", no_argument, 0, 'e' }, { "help", no_argument, 0, 'h' }, - { "family", required_argument, 0, 'f' }, - { "protocol", required_argument, 0, 'p' }, - { "type", required_argument, 0, 't' }, + { "poll", no_argument, 0, 'p' }, + { "quit", no_argument, 0, 'q' }, + { "receive", no_argument, 0, 'r' }, + { "verbose", no_argument, 0, 'v' }, { "version", no_argument, 0, VERSION_OPTION}, - { "receive", no_argument, 0, 'r'}, - { "quit", no_argument, 0, 'q'}, - { "loop", required_argument, 0, 'l'}, - { "verbose", no_argument, 0, 'v'}, + { "identifier", required_argument, 0, 'i' }, + { "loop", required_argument, 0, 'l' }, { 0, 0, 0, 0}, }; - while ((opt = getopt_long(argc, argv, "f:t:p:vrlhq", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "ehpqrvi:l:", long_options, NULL)) != -1) { switch (opt) { - case 'h': - print_usage(basename(argv[0])); - exit(0); - - case 'f': - family = strtoul(optarg, NULL, 0); + case 'e': + extended = 1; break; - case 't': - type = strtoul(optarg, NULL, 0); + case 'h': + print_usage(basename(argv[0])); + exit(EXIT_SUCCESS); break; case 'p': - proto = strtoul(optarg, NULL, 0); + use_poll = 1; + break; + + case 'q': + quit = 1; + break; + + case 'r': + receive = 1; + break; + + case 'v': + verbose++; + break; + + case VERSION_OPTION: + printf("cansequence %s\n", VERSION); + exit(EXIT_SUCCESS); break; case 'l': @@ -112,21 +140,10 @@ int main(int argc, char **argv) infinite = 1; break; - case 'r': - receive = 1; + case 'i': + filter->can_id = strtoul(optarg, NULL, 0); break; - case 'q': - quit = 1; - break; - - case 'v': - verbose++; - break; - - case VERSION_OPTION: - printf("cansequence %s\n", VERSION); - exit(0); default: fprintf(stderr, "Unknown option %c\n", opt); @@ -137,6 +154,16 @@ int main(int argc, char **argv) if (argv[optind] != NULL) interface = argv[optind]; + if (extended) { + filter->can_mask = CAN_EFF_MASK; + 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; + } + frame.can_id = filter->can_id; + printf("interface = %s, family = %d, type = %d, proto = %d\n", interface, family, type, proto); @@ -154,52 +181,100 @@ int main(int argc, char **argv) } addr.can_ifindex = ifr.ifr_ifindex; + /* first don't recv. any msgs */ + if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; } if (receive) { + /* enable recv. now */ + if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(filter))) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } + while ((infinite || loopcount--) && running) { nbytes = read(s, &frame, sizeof(struct can_frame)); if (nbytes < 0) { perror("read"); return 1; - } else { - if (sequence_init) { - sequence_init = 0; - sequence = frame.data[0]; - } - if (verbose > 1) - printf("received frame. sequence number: %d\n", frame.data[0]); - if (frame.data[0] != sequence) { - printf("received wrong sequence count. expected: %d, got: %d\n", - sequence, frame.data[0]); - if (quit) - exit(1); - sequence = frame.data[0]; - } - if (verbose && !sequence) - printf("sequence wrap around\n"); - sequence++; } - } - } else { - frame.can_dlc = 1; - frame.can_id = 2; - frame.data[0] = 0; - while ((infinite || loopcount--) && running) { + + if (sequence_init) { + sequence_init = 0; + sequence = frame.data[0]; + } + if (verbose > 1) - printf("sending frame. sequence number: %d\n", sequence); + printf("received frame. sequence number: %d\n", frame.data[0]); + + if (frame.data[0] != sequence) { + printf("received wrong sequence count. expected: %d, got: %d\n", + sequence, frame.data[0]); + if (quit) { + exit_value = EXIT_FAILURE; + break; + } + sequence = frame.data[0]; + } + + sequence++; if (verbose && !sequence) printf("sequence wrap around\n"); - if (write(s, &frame, sizeof(frame)) < 0) { - perror("write"); - break; + + } + } else { + while ((infinite || loopcount--) && running) { + ssize_t len; + + if (verbose > 1) + printf("sending frame. sequence number: %d\n", sequence); + + again: + len = write(s, &frame, sizeof(frame)); + if (len == -1) { + switch (errno) { + case ENOBUFS: { + int err; + struct pollfd fds[] = { + { + .fd = s, + .events = POLLOUT, + }, + }; + + if (!use_poll) { + perror("write"); + exit(EXIT_FAILURE); + } + + err = poll(fds, 1, 1000); + if (err == -1 && errno != -EINTR) { + perror("poll()"); + exit(EXIT_FAILURE); + } + } + case EINTR: /* fallthrough */ + goto again; + default: + perror("write"); + exit(EXIT_FAILURE); + } } + (unsigned char)frame.data[0]++; sequence++; + + if (verbose && !sequence) + printf("sequence wrap around\n"); } } - return 0; + + exit(exit_value); } From 9841820ea06a5ed9e8e474253c15cd3fdc3fe4cf Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sun, 13 Sep 2009 18:16:07 +0200 Subject: [PATCH 17/34] [cansequence] add sequence wrap around counter Signed-off-by: Marc Kleine-Budde --- cansequence.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cansequence.c b/cansequence.c index f86127b..b787e79 100644 --- a/cansequence.c +++ b/cansequence.c @@ -73,6 +73,7 @@ int main(int argc, char **argv) }; char *interface = "can0"; unsigned char sequence = 0; + int seq_wrap = 0; int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; int loopcount = 1, infinite = 1; int use_poll = 0; @@ -226,7 +227,7 @@ int main(int argc, char **argv) sequence++; if (verbose && !sequence) - printf("sequence wrap around\n"); + printf("sequence wrap around (%d)\n", seq_wrap++); } } else { @@ -272,7 +273,7 @@ int main(int argc, char **argv) sequence++; if (verbose && !sequence) - printf("sequence wrap around\n"); + printf("sequence wrap around (%d)\n", seq_wrap++); } } From 2f125e44ce8de0ce7df2c7830c5c2a3df1f1ac6f Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sat, 22 Mar 2014 18:38:37 +0100 Subject: [PATCH 18/34] cansequence: mark functions as static Signed-off-by: Marc Kleine-Budde --- cansequence.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cansequence.c b/cansequence.c index b787e79..4236825 100644 --- a/cansequence.c +++ b/cansequence.c @@ -32,7 +32,7 @@ enum { #define CAN_ID_DEFAULT (2) -void print_usage(char *prg) +static void print_usage(char *prg) { fprintf(stderr, "Usage: %s [] [Options]\n" "\n" @@ -54,7 +54,7 @@ void print_usage(char *prg) prg, CAN_ID_DEFAULT); } -void sigterm(int signo) +static void sigterm(int signo) { running = 0; } From c9bb22022d83c51b036c66c04f839bd6295d4328 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sat, 22 Mar 2014 20:15:27 +0100 Subject: [PATCH 19/34] cansequence: move rx and tx loop into separate functions Signed-off-by: Marc Kleine-Budde --- cansequence.c | 222 +++++++++++++++++++++++++++----------------------- 1 file changed, 119 insertions(+), 103 deletions(-) diff --git a/cansequence.c b/cansequence.c index 4236825..8bcfc52 100644 --- a/cansequence.c +++ b/cansequence.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -21,16 +23,30 @@ #include #include +#define CAN_ID_DEFAULT (2) + extern int optind, opterr, optopt; static int s = -1; static int running = 1; +static bool infinite = true; +static bool sequence_init = true; +static bool quit = false; +static bool use_poll = false; +static unsigned int loopcount = 1; +static int verbose; + +static struct can_frame frame = { + .can_dlc = 1, +}; +static struct can_filter filter[] = { + { .can_id = CAN_ID_DEFAULT, }, +}; enum { VERSION_OPTION = CHAR_MAX + 1, }; -#define CAN_ID_DEFAULT (2) static void print_usage(char *prg) { @@ -59,31 +75,110 @@ static void sigterm(int signo) running = 0; } + +static void do_receive() +{ + unsigned int seq_wrap = 0; + uint8_t sequence = 0; + ssize_t nbytes; + + /* enable recv. now */ + if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(filter))) { + perror("setsockopt()"); + exit(EXIT_FAILURE); + } + + while ((infinite || loopcount--) && running) { + nbytes = read(s, &frame, sizeof(struct can_frame)); + if (nbytes < 0) { + perror("read()"); + exit(EXIT_FAILURE); + } + + if (sequence_init) { + sequence_init = 0; + sequence = frame.data[0]; + } + + if (verbose > 1) + printf("received frame. sequence number: %d\n", frame.data[0]); + + if (frame.data[0] != sequence) { + printf("received wrong sequence count. expected: %d, got: %d\n", + sequence, frame.data[0]); + if (quit) + exit(EXIT_FAILURE); + + sequence = frame.data[0]; + } + + sequence++; + if (verbose && !sequence) + printf("sequence wrap around (%d)\n", seq_wrap++); + + } +} + +static void do_send() +{ + unsigned int seq_wrap = 0; + uint8_t sequence = 0; + + while ((infinite || loopcount--) && running) { + ssize_t len; + + if (verbose > 1) + printf("sending frame. sequence number: %d\n", sequence); + + again: + len = write(s, &frame, sizeof(frame)); + if (len == -1) { + switch (errno) { + case ENOBUFS: { + int err; + struct pollfd fds[] = { + { + .fd = s, + .events = POLLOUT, + }, + }; + + if (!use_poll) { + perror("write"); + exit(EXIT_FAILURE); + } + + err = poll(fds, 1, 1000); + if (err == -1 && errno != -EINTR) { + perror("poll()"); + exit(EXIT_FAILURE); + } + } + case EINTR: /* fallthrough */ + goto again; + default: + perror("write"); + exit(EXIT_FAILURE); + } + } + + (unsigned char)frame.data[0]++; + sequence++; + + if (verbose && !sequence) + printf("sequence wrap around (%d)\n", seq_wrap++); + } +} + int main(int argc, char **argv) { struct ifreq ifr; struct sockaddr_can addr; - struct can_frame frame = { - .can_dlc = 1, - }; - struct can_filter filter[] = { - { - .can_id = CAN_ID_DEFAULT, - }, - }; char *interface = "can0"; - unsigned char sequence = 0; - int seq_wrap = 0; int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW; - int loopcount = 1, infinite = 1; - int use_poll = 0; int extended = 0; - int nbytes; - int opt; int receive = 0; - int sequence_init = 1; - int verbose = 0, quit = 0; - int exit_value = EXIT_SUCCESS; + int opt; signal(SIGTERM, sigterm); signal(SIGHUP, sigterm); @@ -117,7 +212,7 @@ int main(int argc, char **argv) break; case 'q': - quit = 1; + quit = true; break; case 'r': @@ -193,89 +288,10 @@ int main(int argc, char **argv) return 1; } - if (receive) { - /* enable recv. now */ - if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(filter))) { - perror("setsockopt"); - exit(EXIT_FAILURE); - } + if (receive) + do_receive(); + else + do_send(); - while ((infinite || loopcount--) && running) { - nbytes = read(s, &frame, sizeof(struct can_frame)); - if (nbytes < 0) { - perror("read"); - return 1; - } - - if (sequence_init) { - sequence_init = 0; - sequence = frame.data[0]; - } - - if (verbose > 1) - printf("received frame. sequence number: %d\n", frame.data[0]); - - if (frame.data[0] != sequence) { - printf("received wrong sequence count. expected: %d, got: %d\n", - sequence, frame.data[0]); - if (quit) { - exit_value = EXIT_FAILURE; - break; - } - sequence = frame.data[0]; - } - - sequence++; - if (verbose && !sequence) - printf("sequence wrap around (%d)\n", seq_wrap++); - - } - } else { - while ((infinite || loopcount--) && running) { - ssize_t len; - - if (verbose > 1) - printf("sending frame. sequence number: %d\n", sequence); - - again: - len = write(s, &frame, sizeof(frame)); - if (len == -1) { - switch (errno) { - case ENOBUFS: { - int err; - struct pollfd fds[] = { - { - .fd = s, - .events = POLLOUT, - }, - }; - - if (!use_poll) { - perror("write"); - exit(EXIT_FAILURE); - } - - err = poll(fds, 1, 1000); - if (err == -1 && errno != -EINTR) { - perror("poll()"); - exit(EXIT_FAILURE); - } - } - case EINTR: /* fallthrough */ - goto again; - default: - perror("write"); - exit(EXIT_FAILURE); - } - } - - (unsigned char)frame.data[0]++; - sequence++; - - if (verbose && !sequence) - printf("sequence wrap around (%d)\n", seq_wrap++); - } - } - - exit(exit_value); + exit(EXIT_SUCCESS); } From 7975f9859c3d3230adb3cbf48bb9a959a86037a0 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sat, 22 Mar 2014 20:17:32 +0100 Subject: [PATCH 20/34] cansequence: make use of bool variables Signed-off-by: Marc Kleine-Budde --- cansequence.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cansequence.c b/cansequence.c index 8bcfc52..25a247c 100644 --- a/cansequence.c +++ b/cansequence.c @@ -28,7 +28,7 @@ extern int optind, opterr, optopt; static int s = -1; -static int running = 1; +static bool running = true; static bool infinite = true; static bool sequence_init = true; static bool quit = false; @@ -72,7 +72,7 @@ static void print_usage(char *prg) static void sigterm(int signo) { - running = 0; + running = false; } @@ -199,7 +199,7 @@ int main(int argc, char **argv) while ((opt = getopt_long(argc, argv, "ehpqrvi:l:", long_options, NULL)) != -1) { switch (opt) { case 'e': - extended = 1; + extended = true; break; case 'h': @@ -208,7 +208,7 @@ int main(int argc, char **argv) break; case 'p': - use_poll = 1; + use_poll = true; break; case 'q': @@ -216,7 +216,7 @@ int main(int argc, char **argv) break; case 'r': - receive = 1; + receive = true; break; case 'v': @@ -231,16 +231,16 @@ int main(int argc, char **argv) case 'l': if (optarg) { loopcount = strtoul(optarg, NULL, 0); - infinite = 0; - } else - infinite = 1; + infinite = false; + } else { + infinite = true; + } break; case 'i': filter->can_id = strtoul(optarg, NULL, 0); break; - default: fprintf(stderr, "Unknown option %c\n", opt); break; From 985121aaad9f0e4b8ef7a8c9d7da59aceebdb77e Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sat, 22 Mar 2014 20:17:49 +0100 Subject: [PATCH 21/34] cansequence: cleanup perror() and exit() Signed-off-by: Marc Kleine-Budde --- cansequence.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cansequence.c b/cansequence.c index 25a247c..394a0bf 100644 --- a/cansequence.c +++ b/cansequence.c @@ -265,27 +265,27 @@ int main(int argc, char **argv) s = socket(family, type, proto); if (s < 0) { - perror("socket"); - return 1; + perror("socket()"); + exit(EXIT_FAILURE); } addr.can_family = family; strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); if (ioctl(s, SIOCGIFINDEX, &ifr)) { - perror("ioctl"); - return 1; + perror("ioctl()"); + exit(EXIT_FAILURE); } addr.can_ifindex = ifr.ifr_ifindex; /* first don't recv. any msgs */ if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) { - perror("setsockopt"); + perror("setsockopt()"); exit(EXIT_FAILURE); } if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - perror("bind"); - return 1; + perror("bind()"); + exit(EXIT_FAILURE); } if (receive) From 9567230494ea9aafa7d7eac33e73e775f0317a53 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sun, 10 May 2015 22:48:36 +0200 Subject: [PATCH 22/34] cansequence: print out dropped packages by socket counter Signed-off-by: Marc Kleine-Budde --- cansequence.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/cansequence.c b/cansequence.c index 394a0bf..0939b9e 100644 --- a/cansequence.c +++ b/cansequence.c @@ -78,10 +78,26 @@ static void sigterm(int signo) static void do_receive() { + uint8_t ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))]; + struct iovec iov = { + .iov_base = &frame, + }; + struct msghdr msg = { + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = &ctrlmsg, + }; + struct cmsghdr *cmsg; + const int dropmonitor_on = 1; unsigned int seq_wrap = 0; uint8_t sequence = 0; ssize_t nbytes; + if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL, + &dropmonitor_on, sizeof(dropmonitor_on)) < 0) { + perror("setsockopt() SO_RXQ_OVFL not supported by your Linux Kernel"); + } + /* enable recv. now */ if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(filter))) { perror("setsockopt()"); @@ -89,7 +105,11 @@ static void do_receive() } while ((infinite || loopcount--) && running) { - nbytes = read(s, &frame, sizeof(struct can_frame)); + msg.msg_iov[0].iov_len = sizeof(frame); + msg.msg_controllen = sizeof(ctrlmsg); + msg.msg_flags = 0; + nbytes = recvmsg(s, &msg, 0); + if (nbytes < 0) { perror("read()"); exit(EXIT_FAILURE); @@ -104,8 +124,20 @@ static void do_receive() printf("received frame. sequence number: %d\n", frame.data[0]); if (frame.data[0] != sequence) { - printf("received wrong sequence count. expected: %d, got: %d\n", - sequence, frame.data[0]); + uint32_t overflows = 0; + + for (cmsg = CMSG_FIRSTHDR(&msg); + cmsg && (cmsg->cmsg_level == SOL_SOCKET); + cmsg = CMSG_NXTHDR(&msg,cmsg)) { + if (cmsg->cmsg_type == SO_RXQ_OVFL) { + memcpy(&overflows, CMSG_DATA(cmsg), sizeof(overflows)); + break; + } + } + + fprintf(stderr, "received wrong sequence count. expected: %d, got: %d, socket overflows: %u\n", + sequence, frame.data[0], overflows); + if (quit) exit(EXIT_FAILURE); From 1e0fb39e8bd7a57833e5f8078929e28388b9b356 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Sun, 10 May 2015 22:59:58 +0200 Subject: [PATCH 23/34] cansequence: add support for drop after a certain number of incidents Signed-off-by: Marc Kleine-Budde --- cansequence.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cansequence.c b/cansequence.c index 0939b9e..62eb744 100644 --- a/cansequence.c +++ b/cansequence.c @@ -31,7 +31,8 @@ static int s = -1; static bool running = true; static bool infinite = true; static bool sequence_init = true; -static bool quit = false; +static unsigned int drop_until_quit; +static unsigned int drop_count; static bool use_poll = false; static unsigned int loopcount = 1; @@ -63,7 +64,7 @@ static void print_usage(char *prg) " -r, --receive work as receiver\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 a wrong sequence is encountered\n" + " -q --quit quit if wrong sequences are encountered\n" " -v, --verbose be verbose (twice to be even more verbose\n" " -h --help this help\n" " --version print version information and exit\n", @@ -126,6 +127,8 @@ static void do_receive() if (frame.data[0] != sequence) { uint32_t overflows = 0; + drop_count++; + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg && (cmsg->cmsg_level == SOL_SOCKET); cmsg = CMSG_NXTHDR(&msg,cmsg)) { @@ -135,10 +138,10 @@ static void do_receive() } } - fprintf(stderr, "received wrong sequence count. expected: %d, got: %d, socket overflows: %u\n", - sequence, frame.data[0], overflows); + fprintf(stderr, "[%d] received wrong sequence count. expected: %d, got: %d, socket overflows: %u\n", + drop_count, sequence, frame.data[0], overflows); - if (quit) + if (drop_count == drop_until_quit) exit(EXIT_FAILURE); sequence = frame.data[0]; @@ -219,7 +222,7 @@ int main(int argc, char **argv) { "extended", no_argument, 0, 'e' }, { "help", no_argument, 0, 'h' }, { "poll", no_argument, 0, 'p' }, - { "quit", no_argument, 0, 'q' }, + { "quit", optional_argument, 0, 'q' }, { "receive", no_argument, 0, 'r' }, { "verbose", no_argument, 0, 'v' }, { "version", no_argument, 0, VERSION_OPTION}, @@ -228,7 +231,7 @@ int main(int argc, char **argv) { 0, 0, 0, 0}, }; - while ((opt = getopt_long(argc, argv, "ehpqrvi:l:", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "ehpq:rvi:l:", long_options, NULL)) != -1) { switch (opt) { case 'e': extended = true; @@ -244,7 +247,10 @@ int main(int argc, char **argv) break; case 'q': - quit = true; + if (optarg) + drop_until_quit = strtoul(optarg, NULL, 0); + else + drop_until_quit = 1; break; case 'r': From 0bcb5d9c2c3ff0174836c2fcf9806b85d4ba21f0 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Mon, 25 Oct 2010 12:30:52 +0200 Subject: [PATCH 24/34] cansequence: codingstyle cleanups Signed-off-by: Marc Kleine-Budde --- cansequence.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cansequence.c b/cansequence.c index 62eb744..ff6ef97 100644 --- a/cansequence.c +++ b/cansequence.c @@ -30,7 +30,6 @@ extern int optind, opterr, optopt; static int s = -1; static bool running = true; static bool infinite = true; -static bool sequence_init = true; static unsigned int drop_until_quit; static unsigned int drop_count; static bool use_poll = false; @@ -90,6 +89,7 @@ static void do_receive() }; struct cmsghdr *cmsg; const int dropmonitor_on = 1; + bool sequence_init = true; unsigned int seq_wrap = 0; uint8_t sequence = 0; ssize_t nbytes; @@ -117,7 +117,7 @@ static void do_receive() } if (sequence_init) { - sequence_init = 0; + sequence_init = false; sequence = frame.data[0]; } From ac9124c776d87b2215fc18a4bed43af4bddf7ce9 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 17 Sep 2015 14:36:34 +0200 Subject: [PATCH 25/34] cansequence: sort options more alphabetically Signed-off-by: Marc Kleine-Budde --- cansequence.c | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/cansequence.c b/cansequence.c index ff6ef97..11b429e 100644 --- a/cansequence.c +++ b/cansequence.c @@ -58,14 +58,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" + " -e, --extended send extended frame\n" " -i, --identifier=ID CAN Identifier (default = %u)\n" - " -r, --receive work as receiver\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" + " -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" + " -h, --help this help\n" " --version print version information and exit\n", prg, CAN_ID_DEFAULT); } @@ -220,26 +220,38 @@ int main(int argc, char **argv) struct option long_options[] = { { "extended", no_argument, 0, 'e' }, - { "help", no_argument, 0, 'h' }, + { "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' }, { "version", no_argument, 0, VERSION_OPTION}, - { "identifier", required_argument, 0, 'i' }, - { "loop", required_argument, 0, 'l' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0}, }; - while ((opt = getopt_long(argc, argv, "ehpq:rvi:l:", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "ei:pq::rvh", long_options, NULL)) != -1) { switch (opt) { case 'e': extended = true; break; - case 'h': - print_usage(basename(argv[0])); - exit(EXIT_SUCCESS); + case 'i': + filter->can_id = strtoul(optarg, NULL, 0); + break; + + case 'r': + receive = true; + break; + + case 'l': + if (optarg) { + loopcount = strtoul(optarg, NULL, 0); + infinite = false; + } else { + infinite = true; + } break; case 'p': @@ -253,32 +265,20 @@ int main(int argc, char **argv) drop_until_quit = 1; break; - case 'r': - receive = true; - break; - case 'v': verbose++; break; + case 'h': + print_usage(basename(argv[0])); + exit(EXIT_SUCCESS); + break; + case VERSION_OPTION: printf("cansequence %s\n", VERSION); exit(EXIT_SUCCESS); break; - case 'l': - if (optarg) { - loopcount = strtoul(optarg, NULL, 0); - infinite = false; - } else { - infinite = true; - } - break; - - case 'i': - filter->can_id = strtoul(optarg, NULL, 0); - break; - default: fprintf(stderr, "Unknown option %c\n", opt); break; From 4d50109a69acc69d05d66d989e6dc5462f5a040e Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 17 Sep 2015 13:41:38 +0200 Subject: [PATCH 26/34] cansequence: do_receive: move variables into loop Signed-off-by: Marc Kleine-Budde --- cansequence.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cansequence.c b/cansequence.c index 11b429e..1e1af29 100644 --- a/cansequence.c +++ b/cansequence.c @@ -87,12 +87,10 @@ static void do_receive() .msg_iovlen = 1, .msg_control = &ctrlmsg, }; - struct cmsghdr *cmsg; const int dropmonitor_on = 1; bool sequence_init = true; unsigned int seq_wrap = 0; uint8_t sequence = 0; - ssize_t nbytes; if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL, &dropmonitor_on, sizeof(dropmonitor_on)) < 0) { @@ -106,11 +104,13 @@ static void do_receive() } while ((infinite || loopcount--) && running) { + ssize_t nbytes; + msg.msg_iov[0].iov_len = sizeof(frame); msg.msg_controllen = sizeof(ctrlmsg); msg.msg_flags = 0; - nbytes = recvmsg(s, &msg, 0); + nbytes = recvmsg(s, &msg, 0); if (nbytes < 0) { perror("read()"); exit(EXIT_FAILURE); @@ -125,6 +125,7 @@ static void do_receive() printf("received frame. sequence number: %d\n", frame.data[0]); if (frame.data[0] != sequence) { + struct cmsghdr *cmsg; uint32_t overflows = 0; drop_count++; From 68d5f87889080614bce4bcb911398d1e03c98e99 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Fri, 18 Sep 2015 11:45:22 +0200 Subject: [PATCH 27/34] cansequence: exit with failure in case of unknown option Signed-off-by: Marc Kleine-Budde --- cansequence.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index 1e1af29..6fa3abb 100644 --- a/cansequence.c +++ b/cansequence.c @@ -281,7 +281,8 @@ int main(int argc, char **argv) break; default: - fprintf(stderr, "Unknown option %c\n", opt); + print_usage(basename(argv[0])); + exit(EXIT_FAILURE); break; } } From 36fc75bdb7b3d42480baf57d4fa5a30136195f14 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Fri, 18 Sep 2015 11:50:41 +0200 Subject: [PATCH 28/34] cansequence: use sigaction() instead of signal Signed-off-by: Marc Kleine-Budde --- cansequence.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cansequence.c b/cansequence.c index 6fa3abb..d916576 100644 --- a/cansequence.c +++ b/cansequence.c @@ -70,7 +70,7 @@ static void print_usage(char *prg) prg, CAN_ID_DEFAULT); } -static void sigterm(int signo) +static void sig_handler(int signo) { running = false; } @@ -208,6 +208,9 @@ static void do_send() int main(int argc, char **argv) { + struct sigaction act = { + .sa_handler = sig_handler, + }; struct ifreq ifr; struct sockaddr_can addr; char *interface = "can0"; @@ -216,8 +219,9 @@ int main(int argc, char **argv) int receive = 0; int opt; - signal(SIGTERM, sigterm); - signal(SIGHUP, sigterm); + sigaction(SIGINT, &act, NULL); + sigaction(SIGTERM, &act, NULL); + sigaction(SIGHUP, &act, NULL); struct option long_options[] = { { "extended", no_argument, 0, 'e' }, From de5b9607dcb0dc9a503ccbe3ed79281a48b84748 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Fri, 18 Sep 2015 14:04:47 +0200 Subject: [PATCH 29/34] cansequence: add internal 32 bit sequence counter --- cansequence.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/cansequence.c b/cansequence.c index d916576..197e9e8 100644 --- a/cansequence.c +++ b/cansequence.c @@ -89,8 +89,11 @@ static void do_receive() }; const int dropmonitor_on = 1; bool sequence_init = true; - unsigned int seq_wrap = 0; - uint8_t sequence = 0; + unsigned int sequence_wrap = 0; + uint32_t sequence_mask = 0xff; + uint32_t sequence_rx = 0; + uint32_t sequence_delta = 0; + uint32_t sequence = 0; if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL, &dropmonitor_on, sizeof(dropmonitor_on)) < 0) { @@ -116,15 +119,15 @@ static void do_receive() exit(EXIT_FAILURE); } + sequence_rx = frame.data[0]; + if (sequence_init) { sequence_init = false; - sequence = frame.data[0]; + sequence = sequence_rx; } - if (verbose > 1) - printf("received frame. sequence number: %d\n", frame.data[0]); - - if (frame.data[0] != sequence) { + sequence_delta = (sequence_rx - sequence) & sequence_mask; + if (sequence_delta) { struct cmsghdr *cmsg; uint32_t overflows = 0; @@ -140,17 +143,19 @@ static void do_receive() } fprintf(stderr, "[%d] received wrong sequence count. expected: %d, got: %d, socket overflows: %u\n", - drop_count, sequence, frame.data[0], overflows); + drop_count, sequence & sequence_mask, sequence_rx, overflows); if (drop_count == drop_until_quit) exit(EXIT_FAILURE); - sequence = frame.data[0]; + sequence = sequence_rx; + } else if (verbose > 1) { + printf("sequence CNT: %6u, RX: %6u\n", sequence, sequence_rx); } sequence++; - if (verbose && !sequence) - printf("sequence wrap around (%d)\n", seq_wrap++); + if (verbose && !(sequence & sequence_mask)) + printf("sequence wrap around (%d)\n", sequence_wrap++); } } From d6991358332a8d567fd8c113eeff2e24f7bb4530 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 17 Sep 2015 13:47:24 +0200 Subject: [PATCH 30/34] cansequence: show delta and obsolute overflows Signed-off-by: Marc Kleine-Budde --- cansequence.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cansequence.c b/cansequence.c index 197e9e8..87347d6 100644 --- a/cansequence.c +++ b/cansequence.c @@ -94,6 +94,7 @@ static void do_receive() uint32_t sequence_rx = 0; uint32_t sequence_delta = 0; uint32_t sequence = 0; + unsigned int overflow_old = 0; if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL, &dropmonitor_on, sizeof(dropmonitor_on)) < 0) { @@ -129,7 +130,8 @@ static void do_receive() sequence_delta = (sequence_rx - sequence) & sequence_mask; if (sequence_delta) { struct cmsghdr *cmsg; - uint32_t overflows = 0; + uint32_t overflow = 0; + uint32_t overflow_delta; drop_count++; @@ -137,18 +139,26 @@ static void do_receive() cmsg && (cmsg->cmsg_level == SOL_SOCKET); cmsg = CMSG_NXTHDR(&msg,cmsg)) { if (cmsg->cmsg_type == SO_RXQ_OVFL) { - memcpy(&overflows, CMSG_DATA(cmsg), sizeof(overflows)); + memcpy(&overflow, CMSG_DATA(cmsg), sizeof(overflow)); break; } } - fprintf(stderr, "[%d] received wrong sequence count. expected: %d, got: %d, socket overflows: %u\n", - drop_count, sequence & sequence_mask, sequence_rx, overflows); + overflow_delta = overflow - overflow_old; + + fprintf(stderr, + "sequence CNT: %6u, RX: %6u expected: %3u missing: %4u skt overfl d: %4u a: %4u delta: %3u incident: %u\n", + sequence, sequence_rx, + sequence & sequence_mask, sequence_delta, + overflow_delta, overflow, + sequence_delta - overflow_delta, + drop_count); if (drop_count == drop_until_quit) exit(EXIT_FAILURE); sequence = sequence_rx; + overflow_old = overflow; } else if (verbose > 1) { printf("sequence CNT: %6u, RX: %6u\n", sequence, sequence_rx); } From a1c20aeb45617aa75198be9f57dafcc0e0c73020 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Fri, 25 Oct 2019 18:51:50 +0200 Subject: [PATCH 31/34] cansequence: receive snd show error frames Signed-off-by: Marc Kleine-Budde --- cansequence.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cansequence.c b/cansequence.c index 87347d6..ce25009 100644 --- a/cansequence.c +++ b/cansequence.c @@ -95,12 +95,19 @@ static void do_receive() uint32_t sequence_delta = 0; uint32_t sequence = 0; unsigned int overflow_old = 0; + can_err_mask_t err_mask = CAN_ERR_MASK; if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL, &dropmonitor_on, sizeof(dropmonitor_on)) < 0) { perror("setsockopt() SO_RXQ_OVFL not supported by your Linux Kernel"); } + /* enable recv. of error messages */ + if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask))) { + perror("setsockopt()"); + exit(EXIT_FAILURE); + } + /* enable recv. now */ if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, sizeof(filter))) { perror("setsockopt()"); @@ -120,6 +127,15 @@ static void do_receive() exit(EXIT_FAILURE); } + if (frame.can_id & CAN_ERR_FLAG) { + fprintf(stderr, + "sequence CNT: %6u, ERRORFRAME %7x %02u %02u %02u %02u %02u %02u %02u %02u\n", + sequence, frame.can_id, + frame.data[0], frame.data[1], frame.data[2], frame.data[3], + frame.data[4], frame.data[5], frame.data[6], frame.data[7]); + continue; + } + sequence_rx = frame.data[0]; if (sequence_init) { From 00134246c2a54f93456e5f30e6b3254ad3390b61 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 19 Nov 2019 09:34:13 +0100 Subject: [PATCH 32/34] cansequence.c: add extended identifier flag to mask Without this, the extended identifier flag was not important when filtering on CAN frames. Add this to mask to only receive the frame type we want to recieve. Signed-off-by: Marc Kleine-Budde --- cansequence.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cansequence.c b/cansequence.c index ce25009..2008081 100644 --- a/cansequence.c +++ b/cansequence.c @@ -334,6 +334,7 @@ int main(int argc, char **argv) filter->can_id &= CAN_SFF_MASK; } frame.can_id = filter->can_id; + filter->can_mask |= CAN_EFF_FLAG; printf("interface = %s, family = %d, type = %d, proto = %d\n", interface, family, type, proto); From 2afcbd2feecda6163c69b2a03c704726baa64ac9 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Fri, 22 Nov 2019 22:06:20 +0100 Subject: [PATCH 33/34] cansequence: print error frames in hex Fixes: faad20983348 ("cansequence: receive snd show error frames") Signed-off-by: Marc Kleine-Budde --- cansequence.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cansequence.c b/cansequence.c index 2008081..68733b2 100644 --- a/cansequence.c +++ b/cansequence.c @@ -129,7 +129,7 @@ static void do_receive() if (frame.can_id & CAN_ERR_FLAG) { fprintf(stderr, - "sequence CNT: %6u, ERRORFRAME %7x %02u %02u %02u %02u %02u %02u %02u %02u\n", + "sequence CNT: %6u, ERRORFRAME %7x %02x %02x %02x %02x %02x %02x %02x %02x\n", sequence, frame.can_id, frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5], frame.data[6], frame.data[7]); From a8131b799698e2840a2bb3df29e455fea3763c01 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Wed, 14 Oct 2020 18:32:50 +0200 Subject: [PATCH 34/34] cansequence: add SPDX identifier for GPL-2.0-only and copyrights Message-Id: 20201019111239.GM12463@pengutronix.de Signed-off-by: Sascha Hauer Signed-off-by: Marc Kleine-Budde --- cansequence.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cansequence.c b/cansequence.c index 68733b2..ec165dd 100644 --- a/cansequence.c +++ b/cansequence.c @@ -1,3 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +// Copyright (c) 2007, 2008, 2009, 2010, 2014, 2015, 2019 Pengutronix, +// Marc Kleine-Budde +// Copyright (c) 2005 Pengutronix, +// Sascha Hauer + #include #include