8 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, the following disclaimer and
16 * the referenced file 'COPYING'.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Volkswagen nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * Alternatively, provided that this notice is retained in full, this
25 * software may be distributed under the terms of the GNU General
26 * Public License ("GPL") version 2 as distributed in the 'COPYING'
27 * file from the main directory of the linux kernel source.
29 * The provided data structures and external interfaces from this code
30 * are not restricted to be used by modules with a GPL compatible license.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 * Send feedback to <socketcan-users@lists.berlios.de>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
65 #include <linux/can.h>
66 #include <linux/can/raw.h>
70 #define USE_RECVFROM /* use read() or recvfrom() syscall */
72 #define MAXDEV 6 /* change sscanf()'s manually if changed here */
74 #define ANL "\r\n" /* newline in ASC mode */
77 #define RED ATTBOLD FGRED
78 #define GREEN ATTBOLD FGGREEN
79 #define YELLOW ATTBOLD FGYELLOW
80 #define BLUE ATTBOLD FGBLUE
81 #define MAGENTA ATTBOLD FGMAGENTA
82 #define CYAN ATTBOLD FGCYAN
84 static const char col_on [MAXDEV][19] = {BOLD, MAGENTA, GREEN, BLUE, CYAN, RED};
85 static const char col_off [] = ATTRESET;
88 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
90 extern int optind, opterr, optopt;
92 static volatile int running = 1;
94 void print_usage(char *prg)
96 fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
97 fprintf(stderr, "Options: -m <mask> (default 0x00000000)\n");
98 fprintf(stderr, " -v <value> (default 0x00000000)\n");
99 fprintf(stderr, " -i <0|1> (inv_filter)\n");
100 fprintf(stderr, " -t <type> (timestamp: Absolute/Delta/Zero)\n");
101 fprintf(stderr, " -c (color mode)\n");
102 fprintf(stderr, " -s <level> (silent mode - 1: animation 2: nothing)\n");
103 fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\n");
104 fprintf(stderr, " -a (create ASC compatible output)\n");
105 fprintf(stderr, " -1 (increment interface numbering in ASC mode)\n");
106 fprintf(stderr, " -A (enable ASCII output)\n");
107 fprintf(stderr, "\n");
108 fprintf(stderr, "When using more than one CAN interface the options\n");
109 fprintf(stderr, "m/v/i have comma seperated values e.g. '-m 0,7FF,0'\n");
110 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
113 void sigterm(int signo)
118 int main(int argc, char **argv)
123 canid_t mask[MAXDEV] = {0};
124 canid_t value[MAXDEV] = {0};
125 int inv_filter[MAXDEV] = {0};
126 char devname[MAXDEV][IFNAMSIZ];
127 unsigned char timestamp = 0;
128 unsigned char silent = 0;
129 unsigned char silentani = 0;
130 unsigned char color = 0;
131 unsigned char ascii = 0;
132 unsigned char asc = 0;
133 unsigned char asc_inc_channel = 0;
134 int max_devname_len = 0;
136 int currmax = 1; /* we assume at least one can bus ;-) */
137 struct sockaddr_can addr;
138 struct can_filter rfilter;
139 struct can_frame frame;
144 struct timeval tv, last_tv;
146 signal(SIGTERM, sigterm);
147 signal(SIGHUP, sigterm);
148 signal(SIGINT, sigterm);
150 last_tv.tv_sec = 0; /* init */
152 while ((opt = getopt(argc, argv, "m:v:i:b:s:ca1At:")) != -1) {
155 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
156 &mask[0], &mask[1], &mask[2],
157 &mask[3], &mask[4], &mask[5]);
163 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
164 &value[0], &value[1], &value[2],
165 &value[3], &value[4], &value[5]);
171 i = sscanf(optarg, "%d,%d,%d,%d,%d,%d",
172 &inv_filter[0], &inv_filter[1], &inv_filter[2],
173 &inv_filter[3], &inv_filter[4], &inv_filter[5]);
179 if (strlen(optarg) >= IFNAMSIZ) {
180 printf("Name of CAN device '%s' is too long!\n\n", optarg);
184 if ((bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
185 perror("bridge socket");
188 addr.can_family = AF_CAN;
189 strcpy(ifr.ifr_name, optarg);
190 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
191 perror("SIOCGIFINDEX");
192 addr.can_ifindex = ifr.ifr_ifindex;
194 if (!addr.can_ifindex) {
195 perror("invalid bridge interface");
199 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
200 perror("bridge bind");
207 silent = atoi(optarg);
227 timestamp = optarg[0];
228 if ((timestamp != 'a') && (timestamp != 'A') && (timestamp != 'd') && (timestamp != 'z')) {
229 printf("%s: unknown timestamp mode '%c' - ignored\n",
230 basename(argv[0]), optarg[0]);
239 fprintf(stderr, "Unknown option %c\n", opt);
244 if (optind == argc) {
245 print_usage(basename(argv[0]));
249 /* count in options higher than device count ? */
250 if (optind + currmax > argc) {
251 printf("low count of CAN devices!\n");
255 currmax = argc - optind; /* find real number of CAN devices */
257 if (currmax > MAXDEV) {
258 printf("More than %d CAN devices!\n", MAXDEV);
262 for (i=0; i<currmax; i++) {
265 printf("open %d '%s' m%08X v%08X i%d.\n",
266 i, argv[optind+i], mask[i], value[i], inv_filter[i]);
269 if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
274 if (mask[i] || value[i]) {
276 if (!(asc)) /* this output is not asc compatible! */
277 printf("CAN ID filter[%d] for %s set to mask = %08X, value = %08X %s\n",
278 i, argv[optind+i], mask[i], value[i],
279 (inv_filter[i]) ? "(inv_filter)" : "");
281 rfilter.can_id = value[i];
282 rfilter.can_mask = mask[i];
284 rfilter.can_id |= CAN_INV_FILTER;
286 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
289 j = strlen(argv[optind+i]);
291 if (!(j < IFNAMSIZ)) {
292 printf("name of CAN device '%s' is too long!\n", argv[optind+i]);
296 strcpy(devname[i], argv[optind+i]);
298 if (j > max_devname_len)
299 max_devname_len = j; /* for nice printing */
301 addr.can_family = AF_CAN;
303 if (strcmp(ANYDEV, argv[optind+i])) {
304 strcpy(ifr.ifr_name, argv[optind+i]);
305 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0)
306 perror("SIOCGIFINDEX");
307 addr.can_ifindex = ifr.ifr_ifindex;
310 addr.can_ifindex = 0; /* any can interface */
312 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
321 /* print banner for ASC mode */
323 if (timestamp != 'd') /* delta time is allowed, else ... */
324 timestamp = 'z'; /* ASC-files always start with zero time */
326 if (time(&currtime) == (time_t)-1) {
330 strncpy(datestring, ctime(&currtime), 39); /* copy to private buffer */
331 datestring[strlen(datestring)-1] = 0; /* chop off trailing newline */
332 printf("date %s%s", datestring, ANL); /* print with own new line representation */
334 printf("base hex timestamps %s%s", (timestamp == 'd')?"relative":"absolute", ANL);
335 printf("no internal events logged%s", ANL);
343 for (i=0; i<currmax; i++)
346 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
352 for (i=0; i<currmax; i++) {
354 if (FD_ISSET(s[i], &rdfs)) {
357 socklen_t len = sizeof(addr);
358 if ((nbytes = recvfrom(s[i], &frame, sizeof(struct can_frame), 0, (struct sockaddr*)&addr, &len)) < 0) {
360 if ((nbytes = read(s[i], &frame, sizeof(struct can_frame))) < 0) {
364 } else if (nbytes < sizeof(struct can_frame)) {
365 fprintf(stderr, "read: incomplete CAN frame\n");
369 if ((nbytes = write(bridge, &frame, sizeof(struct can_frame))) < 0) {
370 perror("bridge write");
372 } else if (nbytes < sizeof(struct can_frame)) {
373 fprintf(stderr, "bridge write: incomplete CAN frame\n");
380 printf("%c\b", anichar[silentani%=MAXANI]), silentani++;
386 case 'a': /* absolute with timestamp */
387 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
388 perror("SIOCGSTAMP");
390 printf("%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
392 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
395 case 'A': /* absolute with date */
399 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
400 perror("SIOCGSTAMP");
401 tm = *localtime(&tv.tv_sec);
402 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
404 printf("%s.%04ld ", timestring, tv.tv_usec/100);
406 printf("(%s.%06ld) ", timestring, tv.tv_usec);
410 case 'd': /* delta */
411 case 'z': /* starting with zero */
415 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
416 perror("SIOCGSTAMP");
417 if (last_tv.tv_sec == 0) /* first init */
419 diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
420 diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
421 if (diff.tv_usec < 0)
422 diff.tv_sec--, diff.tv_usec += 1000000;
424 diff.tv_sec = diff.tv_usec = 0;
426 printf("%4ld.%04ld ", diff.tv_sec, diff.tv_usec/100);
428 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
430 if (timestamp == 'd')
431 last_tv = tv; /* update for delta calculation */
435 default: /* no timestamp output */
442 printf("%-2d ", i + asc_inc_channel); /* channel number - left aligned */
444 sprintf(id, "%X%c", frame.can_id & CAN_EFF_MASK,
445 (frame.can_id & CAN_EFF_FLAG)?'x':' ');
446 printf("%-15s Rx ", id);
448 if (frame.can_id & CAN_RTR_FLAG)
449 printf("r"); /* RTR frame: nothing else to print */
451 printf("d %d ", frame.can_dlc); /* data frame */
453 for (j = 0; j < frame.can_dlc; j++) {
454 printf("%02X ", frame.data[j]);
460 printf(" %s",(color)?col_on[i]:"");
462 ifr.ifr_ifindex = addr.can_ifindex;
463 if (ioctl(s[i], SIOCGIFNAME, &ifr) < 0)
464 perror("SIOCGIFNAME");
466 if (max_devname_len < strlen(ifr.ifr_name))
467 max_devname_len = strlen(ifr.ifr_name);
469 printf("%*s", max_devname_len, ifr.ifr_name);
471 printf("%*s", max_devname_len, devname[i]); /* device name */
473 printf("%s ",(color)?col_off:"");
474 if (frame.can_id & CAN_EFF_FLAG)
475 printf("%8X ", frame.can_id & CAN_EFF_MASK);
477 printf("%3X ", frame.can_id & CAN_SFF_MASK);
479 printf("[%d] ", frame.can_dlc);
481 for (j = 0; j < frame.can_dlc; j++) {
482 printf("%02X ", frame.data[j]);
485 printf("%*s", 3*(8-frame.can_dlc)+3, "'");
486 for (j = 0; j < frame.can_dlc; j++)
487 if ((frame.data[j] > 0x1F) && (frame.data[j] < 0x7F))
488 putchar(frame.data[j]);
493 if (frame.can_id & CAN_RTR_FLAG)
494 printf("remote request");
504 for (i=0; i<currmax; i++)