summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Kleine-Budde <mkl@pengutronix.de>2008-09-10 11:24:25 +0000
committerMarc Kleine-Budde <mkl@pengutronix.de>2008-09-10 11:24:25 +0000
commit4dc7f6538ccbddf40af2bd6b03e56127d721b456 (patch)
tree96fc93f910f29647a4fd96090ee74687011fbf3e
parent86f71be175210e715261432b94784269a2fc2052 (diff)
downloadcanutils-4dc7f6538ccbddf40af2bd6b03e56127d721b456.tar.gz
git-svn-id: https://iocaste.extern.pengutronix.de/svn/canutils/trunks/canutils-3.0-trunk@92 5fd5a299-6ef2-0310-aa18-8b01d7c39d8c
-rw-r--r--src/candump.c63
-rw-r--r--src/canecho.c87
-rw-r--r--src/cansend.c30
-rw-r--r--src/cansequence.c30
4 files changed, 102 insertions, 108 deletions
diff --git a/src/candump.c b/src/candump.c
index 130e3c2..ce5ca41 100644
--- a/src/candump.c
+++ b/src/candump.c
@@ -25,25 +25,24 @@ extern int optind, opterr, optopt;
static int s = -1;
static int running = 1;
-enum
-{
+enum {
VERSION_OPTION = CHAR_MAX + 1,
FILTER_OPTION,
};
static void print_usage(char *prg)
{
- fprintf(stderr, "Usage: %s <can-interface> [Options]\n"
- "Options:\n"
- " -f, --family=FAMILY\t" "protocol family (default PF_CAN = %d)\n"
- " -t, --type=TYPE\t" "socket type, see man 2 socket (default SOCK_RAW = %d)\n"
- " -p, --protocol=PROTO\t" "CAN protocol (default CAN_RAW = %d)\n"
- " --filter=id:mask[:id:mask]...\n"
- "\t\t\t" "apply filter\n"
- " -h, --help\t\t" "this help\n"
- " -o <filename>\t\t" "output into filename\n"
- " -d\t\t\t" "daemonize\n"
- " --version\t\t" "print version information and exit\n",
+ fprintf(stderr, "Usage: %s [<can-interface>] [Options]\n"
+ "Options:\n"
+ " -f, --family=FAMILY\t" "protocol family (default PF_CAN = %d)\n"
+ " -t, --type=TYPE\t" "socket type, see man 2 socket (default SOCK_RAW = %d)\n"
+ " -p, --protocol=PROTO\t" "CAN protocol (default CAN_RAW = %d)\n"
+ " --filter=id:mask[:id:mask]...\n"
+ "\t\t\t" "apply filter\n"
+ " -h, --help\t\t" "this help\n"
+ " -o <filename>\t\t" "output into filename\n"
+ " -d\t\t\t" "daemonize\n"
+ " --version\t\t" "print version information and exit\n",
prg, PF_CAN, SOCK_RAW, CAN_RAW);
}
@@ -73,18 +72,19 @@ int add_filter(u_int32_t id, u_int32_t mask)
int main(int argc, char **argv)
{
- int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
- int opt, optdaemon = 0;
- struct sockaddr_can addr;
struct can_frame frame;
- int nbytes, i;
struct ifreq ifr;
- char *ptr;
- char *optout = NULL;
- u_int32_t id, mask;
+ struct sockaddr_can addr;
FILE *out = stdout;
+ char *interface = "can0";
+ char *optout = NULL;
+ char *ptr;
char buf[BUF_SIZ];
+ int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
int n = 0, err;
+ int nbytes, i;
+ int opt, optdaemon = 0;
+ uint32_t id, mask;
signal(SIGPIPE, SIG_IGN);
@@ -153,21 +153,23 @@ int main(int argc, char **argv)
}
}
- if (optind == argc) {
- print_usage(basename(argv[0]));
- exit (EXIT_SUCCESS);
- }
+ if (optind != argc)
+ interface = argv[optind];
- fprintf(out, "interface = %s, family = %d, type = %d, proto = %d\n",
- argv[optind], family, type, proto);
+ printf("interface = %s, family = %d, type = %d, proto = %d\n",
+ interface, 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);
+ 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) {
@@ -175,8 +177,9 @@ int main(int argc, char **argv)
return 1;
}
- if(filter) {
- if(setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter, filter_count * sizeof(struct can_filter)) != 0) {
+ if (filter) {
+ if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, filter,
+ filter_count * sizeof(struct can_filter)) != 0) {
perror("setsockopt");
exit(1);
}
diff --git a/src/canecho.c b/src/canecho.c
index d55a301..66e3aba 100644
--- a/src/canecho.c
+++ b/src/canecho.c
@@ -20,28 +20,27 @@
extern int optind, opterr, optopt;
-static int running = 1;
+static int running = 1;
-enum
-{
+enum {
VERSION_OPTION = CHAR_MAX + 1,
};
void print_usage(char *prg)
{
fprintf(stderr, "Usage: %s <can-interface> [<can-interface-out>] [Options]\n"
- "\n"
- "Send all messages received on <can-interface> to <can-interface-out>\n"
- "If <can-interface-out> is omitted, then <can_interface> is used for sending\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"
- " -v, --verbose be verbose\n"
- " -h, --help this help\n"
- " --version print version information and exit\n",
- prg, PF_CAN, SOCK_RAW, CAN_RAW);
+ "\n"
+ "Send all messages received on <can-interface> to <can-interface-out>\n"
+ "If <can-interface-out> is omitted, then <can_interface> is used for sending\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"
+ " -v, --verbose be 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)
@@ -51,23 +50,21 @@ void sigterm(int signo)
int main(int argc, char **argv)
{
- int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
- int opt;
-
- int s[2];
- struct sockaddr_can addr[2];
+ struct can_frame frame;
struct ifreq ifr[2];
+ struct sockaddr_can addr[2];
char *intf_name[2];
-
- struct can_frame frame;
+ int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
int nbytes, i, out;
+ int opt;
+ int s[2];
int verbose = 0;
signal(SIGTERM, sigterm);
signal(SIGHUP, sigterm);
signal(SIGINT, sigterm);
- struct option long_options[] = {
+ struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "family", required_argument, 0, 'f' },
{ "protocol", required_argument, 0, 'p' },
@@ -79,33 +76,33 @@ int main(int argc, char **argv)
while ((opt = getopt_long(argc, argv, "hf:t:p:v", long_options, NULL)) != -1) {
switch (opt) {
- case 'f':
- family = atoi(optarg);
- break;
+ case 'f':
+ family = atoi(optarg);
+ break;
- case 't':
- type = atoi(optarg);
- break;
+ case 't':
+ type = atoi(optarg);
+ break;
- case 'p':
- proto = atoi(optarg);
- break;
+ case 'p':
+ proto = atoi(optarg);
+ break;
- case 'v':
- verbose = 1;
- break;
+ case 'v':
+ verbose = 1;
+ break;
- case 'h':
- print_usage(basename(argv[0]));
- exit(0);
+ case 'h':
+ print_usage(basename(argv[0]));
+ exit(0);
- case VERSION_OPTION:
- printf("canecho %s\n",VERSION);
- exit(0);
+ case VERSION_OPTION:
+ printf("canecho %s\n",VERSION);
+ exit(0);
- default:
- fprintf(stderr, "Unknown option %c\n", opt);
- break;
+ default:
+ fprintf(stderr, "Unknown option %c\n", opt);
+ break;
}
}
@@ -121,7 +118,7 @@ int main(int argc, char **argv)
intf_name[1] = argv[optind];
printf("interface-in = %s, interface-out = %s, family = %d, type = %d, proto = %d\n",
- intf_name[0], intf_name[1], family, type, proto);
+ intf_name[0], intf_name[1], family, type, proto);
if (intf_name[0] == intf_name[1])
out = 0;
diff --git a/src/cansend.c b/src/cansend.c
index fb534e9..caec590 100644
--- a/src/cansend.c
+++ b/src/cansend.c
@@ -24,7 +24,7 @@ extern int optind, opterr, optopt;
static void print_usage(char *prg)
{
fprintf(stderr,
- "Usage: %s <can-interface> [Options] <can-msg>\n"
+ "Usage: %s [<can-interface>] [Options] <can-msg>\n"
"<can-msg> can consist of up to 8 bytes given as a space separated list\n"
"Options:\n"
" -i, --identifier=ID CAN Identifier (default = 1)\n"
@@ -47,13 +47,14 @@ enum {
int main(int argc, char **argv)
{
- int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
+ struct can_frame frame;
+ struct ifreq ifr;
struct sockaddr_can addr;
+ char *interface = "can0";
+ int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
+ int loopcount = 1, infinite = 0;
int s, opt, ret, i, dlc = 0, rtr = 0, extended = 0;
- struct can_frame frame;
int verbose = 0;
- int loopcount = 1, infinite = 0;
- struct ifreq ifr;
struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
@@ -121,19 +122,11 @@ 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);
- }
+ if (optind != argc)
+ interface = argv[optind];
- if (verbose)
- printf("interface = %s, family = %d, type = %d, proto = %d\n",
- argv[optind], family, type, proto);
+ printf("interface = %s, family = %d, type = %d, proto = %d\n",
+ interface, family, type, proto);
s = socket(family, type, proto);
if (s < 0) {
@@ -142,7 +135,7 @@ int main(int argc, char **argv)
}
addr.can_family = family;
- strcpy(ifr.ifr_name, argv[optind]);
+ strcpy(ifr.ifr_name, interface);
if (ioctl(s, SIOCGIFINDEX, &ifr)) {
perror("ioctl");
return 1;
@@ -154,7 +147,6 @@ int main(int argc, char **argv)
return 1;
}
-
for (i = optind + 1; i < argc; i++) {
frame.data[dlc] = strtoul(argv[i], NULL, 0);
dlc++;
diff --git a/src/cansequence.c b/src/cansequence.c
index 9ad0b30..8395c65 100644
--- a/src/cansequence.c
+++ b/src/cansequence.c
@@ -30,7 +30,7 @@ enum {
void print_usage(char *prg)
{
- fprintf(stderr, "Usage: %s <can-interface> [Options]\n"
+ fprintf(stderr, "Usage: %s [<can-interface>] [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) {