From f0cd9585a4dfb5e413afb354316936e4c5a5c601 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Thu, 21 Jul 2005 15:09:46 +0000 Subject: - add longopt support - add version info - add loop option for sending more than one message - take message data as integers rather than a string git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-1.0-trunk@28 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c --- cansend.c | 102 +++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 27 deletions(-) diff --git a/cansend.c b/cansend.c index d42cdcf..eced013 100644 --- a/cansend.c +++ b/cansend.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -11,6 +12,8 @@ #include #include +#include + #include extern int optind, opterr, optopt; @@ -23,28 +26,55 @@ void sigterm(int signo) void print_usage(char *prg) { - fprintf(stderr, "Usage: %s [Options] -i \n", prg); - fprintf(stderr, "Options: -f (default PF_CAN = %d)\n", PF_CAN); - fprintf(stderr, " -t (default SOCK_RAW = %d)\n", SOCK_RAW); - fprintf(stderr, " -p (default CAN_PROTO_RAW = %d)\n", CAN_PROTO_RAW); - fprintf(stderr, " -v (verbose)\n"); + 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\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" + " -l send message infinite times\n" + " --loop=COUNT send message COUNT times\n" + " -v, --verbose be verbose\n" + " --version print version information and exit\n", + prg, PF_CAN, SOCK_RAW, CAN_PROTO_RAW); } +enum +{ + VERSION_OPTION = CHAR_MAX + 1, +}; + int main(int argc, char **argv) { int family = PF_CAN, type = SOCK_RAW, proto = CAN_PROTO_RAW; struct sockaddr_can addr; - int s, opt, ret, i; + int s, opt, ret, i, dlc = 0; struct can_frame frame; int verbose = 0; - int num = 0; + int loopcount = 1, infinite = 0; struct ifreq ifr; signal(SIGTERM, sigterm); signal(SIGHUP, sigterm); - while ((opt = getopt(argc, argv, "f:t:p:vi:")) != -1) { + struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { "identifier", required_argument, 0, 'i'}, + { "family", required_argument, 0, 'f' }, + { "protocol", required_argument, 0, 'p' }, + { "version", no_argument, 0, VERSION_OPTION}, + { "verbose", no_argument, 0, 'v'}, + { "loop", required_argument, 0, 'l'}, + { 0, 0, 0, 0}, + }; + + while ((opt = getopt_long(argc, argv, "hf:t:p:vi:l", long_options, NULL)) != -1) { switch (opt) { + case 'h': + print_usage(basename(argv[0])); + exit(0); + case 'f': family = strtoul(optarg, NULL, 0); break; @@ -60,10 +90,19 @@ int main(int argc, char **argv) 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); + case VERSION_OPTION: + printf("cansend %s\n",VERSION); + exit(0); + case '?': break; @@ -83,8 +122,9 @@ int main(int argc, char **argv) exit(-1); } - printf("interface = %s, family = %d, type = %d, proto = %d\n", - argv[optind], family, type, proto); + if(verbose) + 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; @@ -92,8 +132,10 @@ int main(int argc, char **argv) addr.can_family = family; strcpy(ifr.ifr_name, argv[optind]); - ret = ioctl(s, SIOCGIFINDEX, &ifr); - printf("ioctl: %d ", ret); + if( ioctl(s, SIOCGIFINDEX, &ifr) ) { + perror("ioctl"); + return 1; + } addr.can_ifindex = ifr.ifr_ifindex; addr.can_id = frame.can_id; @@ -102,23 +144,29 @@ int main(int argc, char **argv) return 1; } - if (argv[optind+1] != NULL) { - frame.can_dlc = strlen(argv[optind+1]); - frame.can_dlc = frame.can_dlc > 8 ? 8 : frame.can_dlc; - strncpy(frame.payload.data, argv[optind+1],8); - } else { - frame.can_dlc = 0; - } - 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.payload.data[i]); - printf("\n"); + for(i = optind + 1; i < argc; i++) { + frame.payload.data[dlc] = strtoul(argv[i], NULL, 0); + dlc++; + if( dlc == 8 ) + break; + } + frame.can_dlc = dlc; + + 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.payload.data[i]); + printf("\n"); + } - num = 1; - while (num--) { + while (infinite || loopcount--) { ret = write(s, &frame, sizeof(frame)); + if( ret == -1 ) { + perror("write"); + break; + } } close(s); -- cgit v1.2.3