From c68b7f25c8c7ded27f3a4d72b540804bedf4d65b Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Tue, 9 Sep 2008 15:18:51 +0000 Subject: * 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 --- src/cansend.c | 178 +++++++++++++++++++++++++++--------------------------- src/cansequence.c | 157 ++++++++++++++++++++++++----------------------- 2 files changed, 171 insertions(+), 164 deletions(-) diff --git a/src/cansend.c b/src/cansend.c index 07a1019..fb534e9 100644 --- a/src/cansend.c +++ b/src/cansend.c @@ -23,26 +23,26 @@ extern int optind, opterr, optopt; static void print_usage(char *prg) { - fprintf(stderr, "Usage: %s [Options] \n" - " can consist of up to 8 bytes given as a space separated list\n" - "Options:\n" - " -i, --identifier=ID CAN Identifier (default = 1)\n" - " -r --rtr send remote request\n" - " -e --extended send extended frame\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" - " -l send message infinite times\n" - " --loop=COUNT send message COUNT times\n" - " -v, --verbose be verbose\n" - " -h, --help this help\n" - " --version print version information and exit\n", - prg, PF_CAN, SOCK_RAW, CAN_RAW); -} - -enum -{ - VERSION_OPTION = CHAR_MAX + 1, + fprintf(stderr, + "Usage: %s [Options] \n" + " can consist of up to 8 bytes given as a space separated list\n" + "Options:\n" + " -i, --identifier=ID CAN Identifier (default = 1)\n" + " -r --rtr send remote request\n" + " -e --extended send extended frame\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" + " -l COUNT send message COUNT times, infinite if omitted\n" + " --loop=COUNT \n" + " -v, --verbose be verbose\n" + " -h, --help this help\n" + " --version print version information and exit\n", + prg, PF_CAN, SOCK_RAW, CAN_RAW); +} + +enum { + VERSION_OPTION = CHAR_MAX + 1, }; int main(int argc, char **argv) @@ -55,93 +55,95 @@ int main(int argc, char **argv) int loopcount = 1, infinite = 0; struct ifreq ifr; - struct option long_options[] = { - { "help", no_argument, 0, 'h' }, - { "identifier", required_argument, 0, 'i'}, - { "rtr", no_argument, 0, 'r'}, - { "extended", no_argument, 0, 'e'}, - { "family", required_argument, 0, 'f' }, - { "protocol", required_argument, 0, 'p' }, - { "type", required_argument, 0, 't' }, - { "version", no_argument, 0, VERSION_OPTION}, - { "verbose", no_argument, 0, 'v'}, - { "loop", required_argument, 0, 'l'}, - { 0, 0, 0, 0}, + struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { "identifier", required_argument, 0, 'i' }, + { "rtr", no_argument, 0, 'r' }, + { "extended", no_argument, 0, 'e' }, + { "family", required_argument, 0, 'f' }, + { "protocol", required_argument, 0, 'p' }, + { "type", required_argument, 0, 't' }, + { "version", no_argument, 0, VERSION_OPTION}, + { "verbose", no_argument, 0, 'v'}, + { "loop", optional_argument, 0, 'l'}, + { 0, 0, 0, 0 }, }; frame.can_id = 1; - while ((opt = getopt_long(argc, argv, "hf:t:p:vi:lre", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "hf:t:p:vi:l::re", 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 'v': - verbose = 1; - break; - - case 'l': - if(optarg) - loopcount = strtoul(optarg, NULL, 0); - else - infinite = 1; - break; - case 'i': - frame.can_id = strtoul(optarg, NULL, 0); - break; - - case 'r': - rtr = 1; - break; - - case 'e': - extended = 1; - break; - - case VERSION_OPTION: - printf("cansend %s\n",VERSION); - exit(0); - - default: - fprintf(stderr, "Unknown option %c\n", opt); - break; + 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 'v': + verbose = 1; + break; + + case 'l': + if (optarg) + loopcount = strtoul(optarg, NULL, 0); + else + infinite = 1; + break; + case 'i': + frame.can_id = strtoul(optarg, NULL, 0); + break; + + case 'r': + rtr = 1; + break; + + case 'e': + extended = 1; + break; + + case VERSION_OPTION: + printf("cansend %s\n", VERSION); + exit(0); + + default: + fprintf(stderr, "Unknown option %c\n", opt); + break; } } if (optind == argc) { print_usage(basename(argv[0])); exit(0); - } - + } + if (argv[optind] == NULL) { fprintf(stderr, "No Interface supplied\n"); exit(-1); } - if(verbose) + if (verbose) printf("interface = %s, family = %d, type = %d, proto = %d\n", argv[optind], family, type, proto); - if ((s = socket(family, type, proto)) < 0) { + + s = socket(family, type, proto); + if (s < 0) { perror("socket"); return 1; } addr.can_family = family; strcpy(ifr.ifr_name, argv[optind]); - if( ioctl(s, SIOCGIFINDEX, &ifr) ) { + if (ioctl(s, SIOCGIFINDEX, &ifr)) { perror("ioctl"); return 1; } @@ -168,16 +170,16 @@ int main(int argc, char **argv) frame.can_id |= CAN_EFF_FLAG; if (verbose) { - printf("id: %d ",frame.can_id); - printf("dlc: %d\n",frame.can_dlc); - for(i = 0; i < frame.can_dlc; i++) - printf("0x%02x ",frame.data[i]); + printf("id: %d ", frame.can_id); + printf("dlc: %d\n", frame.can_dlc); + for (i = 0; i < frame.can_dlc; i++) + printf("0x%02x ", frame.data[i]); printf("\n"); } while (infinite || loopcount--) { ret = write(s, &frame, sizeof(frame)); - if( ret == -1 ) { + if (ret == -1) { perror("write"); break; } diff --git a/src/cansequence.c b/src/cansequence.c index 40839c8..c2baf39 100644 --- a/src/cansequence.c +++ b/src/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) { -- cgit v1.2.3