8 * Copyright (c) 2002-2007 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 and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Volkswagen nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * Alternatively, provided that this notice is retained in full, this
24 * software may be distributed under the terms of the GNU General
25 * Public License ("GPL") version 2, in which case the provisions of the
26 * GPL apply INSTEAD OF those given above.
28 * The provided data structures and external interfaces from this code
29 * are not restricted to be used by modules with a GPL compatible license.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44 * Send feedback to <socketcan-users@lists.berlios.de>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
64 #include <linux/can.h>
65 #include <linux/can/raw.h>
70 #define MAXDEV 6 /* change sscanf()'s manually if changed here */
72 #define ANL "\r\n" /* newline in ASC mode */
75 #define RED ATTBOLD FGRED
76 #define GREEN ATTBOLD FGGREEN
77 #define YELLOW ATTBOLD FGYELLOW
78 #define BLUE ATTBOLD FGBLUE
79 #define MAGENTA ATTBOLD FGMAGENTA
80 #define CYAN ATTBOLD FGCYAN
82 const char col_on [MAXDEV][19] = {BLUE, RED, GREEN, BOLD, MAGENTA, CYAN};
83 const char col_off [] = ATTRESET;
85 static char devname[MAXDEV][IFNAMSIZ+1];
86 static int dindex[MAXDEV];
87 static int max_devname_len;
90 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
92 extern int optind, opterr, optopt;
94 static volatile int running = 1;
96 void print_usage(char *prg)
98 fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
99 fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
100 fprintf(stderr, "Options: -m <mask> (ID filter mask. Default 0x00000000) *\n");
101 fprintf(stderr, " -v <value> (ID filter value. Default 0x00000000) *\n");
102 fprintf(stderr, " -i <0|1> (invert the specified ID filter) *\n");
103 fprintf(stderr, " -e <emask> (mask for error frames)\n");
104 fprintf(stderr, " -t <type> (timestamp: Absolute/Delta/Zero)\n");
105 fprintf(stderr, " -c (color mode)\n");
106 fprintf(stderr, " -a (enable additional ASCII output)\n");
107 fprintf(stderr, " -s <level> (silent mode - 1: animation 2: nothing)\n");
108 fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\n");
109 fprintf(stderr, " -B <can> (bridge mode - like '-b' with disabled loopback)\n");
110 fprintf(stderr, " -l (log CAN-frames into file)\n");
111 fprintf(stderr, " -L (use log file format on stdout)\n");
112 fprintf(stderr, "\n");
113 fprintf(stderr, "* The CAN ID filter matches, when ...\n");
114 fprintf(stderr, " <received_can_id> & mask == value & mask\n");
115 fprintf(stderr, "\n");
116 fprintf(stderr, "When using more than one CAN interface the options\n");
117 fprintf(stderr, "m/v/i/e have comma seperated values e.g. '-m 0,7FF,0'\n");
118 fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n\n", ANYDEV);
121 void sigterm(int signo)
126 int idx2dindex(int ifidx, int socket) {
131 for (i=0; i<MAXDEV; i++) {
132 if (dindex[i] == ifidx)
136 /* create new interface index cache entry */
138 /* remove index cache zombies first */
139 for (i=0; i < MAXDEV; i++) {
141 ifr.ifr_ifindex = dindex[i];
142 if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
147 for (i=0; i < MAXDEV; i++)
148 if (!dindex[i]) /* free entry */
152 printf("Interface index cache only supports %d interfaces.\n", MAXDEV);
158 ifr.ifr_ifindex = ifidx;
159 if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
160 perror("SIOCGIFNAME");
162 if (max_devname_len < strlen(ifr.ifr_name))
163 max_devname_len = strlen(ifr.ifr_name);
165 strcpy(devname[i], ifr.ifr_name);
168 printf("new index %d (%s)\n", i, devname[i]);
174 int main(int argc, char **argv)
179 canid_t mask[MAXDEV] = {0};
180 canid_t value[MAXDEV] = {0};
181 int inv_filter[MAXDEV] = {0};
182 can_err_mask_t err_mask[MAXDEV] = {0};
183 unsigned char timestamp = 0;
184 unsigned char silent = 0;
185 unsigned char silentani = 0;
186 unsigned char color = 0;
187 unsigned char ascii = 0;
188 unsigned char log = 0;
189 unsigned char logfrmt = 0;
191 int currmax = 1; /* we assume at least one can bus ;-) */
192 struct sockaddr_can addr;
193 struct can_filter rfilter;
194 struct can_frame frame;
197 struct timeval tv, last_tv;
198 FILE *logfile = NULL;
200 signal(SIGTERM, sigterm);
201 signal(SIGHUP, sigterm);
202 signal(SIGINT, sigterm);
207 while ((opt = getopt(argc, argv, "m:v:i:e:t:cas:b:B:lL")) != -1) {
210 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
211 &mask[0], &mask[1], &mask[2],
212 &mask[3], &mask[4], &mask[5]);
218 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
219 &value[0], &value[1], &value[2],
220 &value[3], &value[4], &value[5]);
226 i = sscanf(optarg, "%d,%d,%d,%d,%d,%d",
227 &inv_filter[0], &inv_filter[1], &inv_filter[2],
228 &inv_filter[3], &inv_filter[4], &inv_filter[5]);
234 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
235 &err_mask[0], &err_mask[1], &err_mask[2],
236 &err_mask[3], &err_mask[4], &err_mask[5]);
242 timestamp = optarg[0];
243 if ((timestamp != 'a') && (timestamp != 'A') &&
244 (timestamp != 'd') && (timestamp != 'z')) {
245 printf("%s: unknown timestamp mode '%c' - ignored\n",
246 basename(argv[0]), optarg[0]);
260 silent = atoi(optarg);
265 if (strlen(optarg) >= IFNAMSIZ) {
266 printf("Name of CAN device '%s' is too long!\n\n", optarg);
270 if ((bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
271 perror("bridge socket");
274 addr.can_family = AF_CAN;
275 strcpy(ifr.ifr_name, optarg);
276 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
277 perror("SIOCGIFINDEX");
278 addr.can_ifindex = ifr.ifr_ifindex;
280 if (!addr.can_ifindex) {
281 perror("invalid bridge interface");
288 setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback));
291 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
292 perror("bridge bind");
307 fprintf(stderr, "Unknown option %c\n", opt);
308 print_usage(basename(argv[0]));
314 if (optind == argc) {
315 print_usage(basename(argv[0]));
319 /* count in options higher than device count ? */
320 if (optind + currmax > argc) {
321 printf("low count of CAN devices!\n");
325 currmax = argc - optind; /* find real number of CAN devices */
327 if (currmax > MAXDEV) {
328 printf("More than %d CAN devices!\n", MAXDEV);
332 for (i=0; i<currmax; i++) {
335 printf("open %d '%s' m%08X v%08X i%d e%d.\n",
336 i, argv[optind+i], mask[i], value[i],
337 inv_filter[i], err_mask[i]);
340 if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
345 if (mask[i] || value[i]) {
347 printf("CAN ID filter[%d] for %s set to "
348 "mask = %08X, value = %08X %s\n",
349 i, argv[optind+i], mask[i], value[i],
350 (inv_filter[i]) ? "(inv_filter)" : "");
352 rfilter.can_id = value[i];
353 rfilter.can_mask = mask[i];
355 rfilter.can_id |= CAN_INV_FILTER;
357 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
358 &rfilter, sizeof(rfilter));
362 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
363 &err_mask[i], sizeof(err_mask[i]));
365 j = strlen(argv[optind+i]);
367 if (!(j < IFNAMSIZ)) {
368 printf("name of CAN device '%s' is too long!\n", argv[optind+i]);
372 if (j > max_devname_len)
373 max_devname_len = j; /* for nice printing */
375 addr.can_family = AF_CAN;
377 if (strcmp(ANYDEV, argv[optind+i])) {
378 strcpy(ifr.ifr_name, argv[optind+i]);
379 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
380 perror("SIOCGIFINDEX");
383 addr.can_ifindex = ifr.ifr_ifindex;
386 addr.can_ifindex = 0; /* any can interface */
388 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
397 char fname[sizeof("candump-2006-11-20_202026.log")+1];
399 if (time(&currtime) == (time_t)-1) {
404 localtime_r(&currtime, &now);
406 sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log",
414 printf("\nEnabling Logfile '%s'\n\n", fname);
416 logfile = fopen(fname, "w");
426 for (i=0; i<currmax; i++)
429 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
435 for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
437 if (FD_ISSET(s[i], &rdfs)) {
439 socklen_t len = sizeof(addr);
442 if ((nbytes = recvfrom(s[i], &frame,
443 sizeof(struct can_frame), 0,
444 (struct sockaddr*)&addr, &len)) < 0) {
449 if (nbytes < sizeof(struct can_frame)) {
450 fprintf(stderr, "read: incomplete CAN frame\n");
455 if ((nbytes = write(bridge, &frame,
456 sizeof(struct can_frame))) < 0) {
457 perror("bridge write");
459 } else if (nbytes < sizeof(struct can_frame)) {
460 fprintf(stderr,"bridge write: incomplete CAN frame\n");
465 if (timestamp || log || logfrmt)
466 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
467 perror("SIOCGSTAMP");
470 idx = idx2dindex(addr.can_ifindex, s[i]);
473 /* log CAN frame with absolute timestamp & device */
474 fprintf(logfile, "(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
475 fprintf(logfile, "%*s ", max_devname_len, devname[idx]);
476 /* without seperator as logfile use-case is parsing */
477 fprint_canframe(logfile, &frame, "\n", 0);
481 /* print CAN frame in log file style to stdout */
482 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
483 printf("%*s ", max_devname_len, devname[idx]);
484 fprint_canframe(stdout, &frame, "\n", 0);
485 goto out_fflush; /* no other output to stdout */
490 printf("%c\b", anichar[silentani%=MAXANI]);
493 goto out_fflush; /* no other output to stdout */
496 printf(" %s", (color>2)?col_on[idx]:"");
500 case 'a': /* absolute with timestamp */
501 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
504 case 'A': /* absolute with date */
509 tm = *localtime(&tv.tv_sec);
510 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
511 printf("(%s.%06ld) ", timestring, tv.tv_usec);
515 case 'd': /* delta */
516 case 'z': /* starting with zero */
520 if (last_tv.tv_sec == 0) /* first init */
522 diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
523 diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
524 if (diff.tv_usec < 0)
525 diff.tv_sec--, diff.tv_usec += 1000000;
527 diff.tv_sec = diff.tv_usec = 0;
528 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
530 if (timestamp == 'd')
531 last_tv = tv; /* update for delta calculation */
535 default: /* no timestamp output */
539 printf(" %s", (color && (color<3))?col_on[idx]:"");
540 printf("%*s", max_devname_len, devname[idx]);
541 printf("%s ", (color==1)?col_off:"");
543 fprint_long_canframe(stdout, &frame, NULL, ascii);
545 printf("%s", (color>1)?col_off:"");
554 for (i=0; i<currmax; i++)