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>
71 #define MAXDEV 6 /* change sscanf()'s manually if changed here */
73 #define ANL "\r\n" /* newline in ASC mode */
76 #define RED ATTBOLD FGRED
77 #define GREEN ATTBOLD FGGREEN
78 #define YELLOW ATTBOLD FGYELLOW
79 #define BLUE ATTBOLD FGBLUE
80 #define MAGENTA ATTBOLD FGMAGENTA
81 #define CYAN ATTBOLD FGCYAN
83 const char col_on [MAXDEV][19] = {BOLD, MAGENTA, GREEN, BLUE, CYAN, RED};
84 const char col_off [] = ATTRESET;
86 static char devname[MAXDEV][IFNAMSIZ+1];
87 static int dindex[MAXDEV];
88 static int max_devname_len;
91 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
93 extern int optind, opterr, optopt;
95 static volatile int running = 1;
97 void print_usage(char *prg)
99 fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
100 fprintf(stderr, " (use CTRL-C to terminate %s)\n", prg);
101 fprintf(stderr, "Options: -m <mask> (default 0x00000000)\n");
102 fprintf(stderr, " -v <value> (default 0x00000000)\n");
103 fprintf(stderr, " -i <0|1> (inv_filter)\n");
104 fprintf(stderr, " -e <emask> (mask for error frames)\n");
105 fprintf(stderr, " -t <type> (timestamp: Absolute/Delta/Zero)\n");
106 fprintf(stderr, " -c (color mode)\n");
107 fprintf(stderr, " -a (enable additional ASCII output)\n");
108 fprintf(stderr, " -s <level> (silent mode - 1: animation 2: nothing)\n");
109 fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\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, "When using more than one CAN interface the options\n");
114 fprintf(stderr, "m/v/i/e have comma seperated values e.g. '-m 0,7FF,0'\n");
115 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
118 void sigterm(int signo)
123 int idx2dindex(int ifidx, int socket) {
128 for (i=0; i<MAXDEV; i++) {
129 if (dindex[i] == ifidx)
133 /* create new interface index cache entry */
135 for (i=0; i < MAXDEV; i++)
136 if (!dindex[i]) /* free entry */
140 printf("BUG in interface index cache! MAXDEV?\n");
146 ifr.ifr_ifindex = ifidx;
147 if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
148 perror("SIOCGIFNAME");
150 if (max_devname_len < strlen(ifr.ifr_name))
151 max_devname_len = strlen(ifr.ifr_name);
153 strcpy(devname[i], ifr.ifr_name);
156 printf("new index %d (%s)\n", i, devname[i]);
162 int main(int argc, char **argv)
167 canid_t mask[MAXDEV] = {0};
168 canid_t value[MAXDEV] = {0};
169 int inv_filter[MAXDEV] = {0};
170 can_err_mask_t err_mask[MAXDEV] = {0};
171 unsigned char timestamp = 0;
172 unsigned char silent = 0;
173 unsigned char silentani = 0;
174 unsigned char color = 0;
175 unsigned char ascii = 0;
176 unsigned char log = 0;
177 unsigned char logfrmt = 0;
179 int currmax = 1; /* we assume at least one can bus ;-) */
180 struct sockaddr_can addr;
181 struct can_filter rfilter;
182 struct can_frame frame;
185 struct timeval tv, last_tv;
186 FILE *logfile = NULL;
188 signal(SIGTERM, sigterm);
189 signal(SIGHUP, sigterm);
190 signal(SIGINT, sigterm);
195 while ((opt = getopt(argc, argv, "m:v:i:e:t:cas:b:lL")) != -1) {
198 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
199 &mask[0], &mask[1], &mask[2],
200 &mask[3], &mask[4], &mask[5]);
206 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
207 &value[0], &value[1], &value[2],
208 &value[3], &value[4], &value[5]);
214 i = sscanf(optarg, "%d,%d,%d,%d,%d,%d",
215 &inv_filter[0], &inv_filter[1], &inv_filter[2],
216 &inv_filter[3], &inv_filter[4], &inv_filter[5]);
222 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
223 &err_mask[0], &err_mask[1], &err_mask[2],
224 &err_mask[3], &err_mask[4], &err_mask[5]);
230 timestamp = optarg[0];
231 if ((timestamp != 'a') && (timestamp != 'A') &&
232 (timestamp != 'd') && (timestamp != 'z')) {
233 printf("%s: unknown timestamp mode '%c' - ignored\n",
234 basename(argv[0]), optarg[0]);
248 silent = atoi(optarg);
252 if (strlen(optarg) >= IFNAMSIZ) {
253 printf("Name of CAN device '%s' is too long!\n\n", optarg);
257 if ((bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
258 perror("bridge socket");
261 addr.can_family = AF_CAN;
262 strcpy(ifr.ifr_name, optarg);
263 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
264 perror("SIOCGIFINDEX");
265 addr.can_ifindex = ifr.ifr_ifindex;
267 if (!addr.can_ifindex) {
268 perror("invalid bridge interface");
272 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
273 perror("bridge bind");
288 fprintf(stderr, "Unknown option %c\n", opt);
289 print_usage(basename(argv[0]));
295 if (optind == argc) {
296 print_usage(basename(argv[0]));
300 /* count in options higher than device count ? */
301 if (optind + currmax > argc) {
302 printf("low count of CAN devices!\n");
306 currmax = argc - optind; /* find real number of CAN devices */
308 if (currmax > MAXDEV) {
309 printf("More than %d CAN devices!\n", MAXDEV);
313 for (i=0; i<currmax; i++) {
316 printf("open %d '%s' m%08X v%08X i%d e%d.\n",
317 i, argv[optind+i], mask[i], value[i],
318 inv_filter[i], err_mask[i]);
321 if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
326 if (mask[i] || value[i]) {
328 printf("CAN ID filter[%d] for %s set to "
329 "mask = %08X, value = %08X %s\n",
330 i, argv[optind+i], mask[i], value[i],
331 (inv_filter[i]) ? "(inv_filter)" : "");
333 rfilter.can_id = value[i];
334 rfilter.can_mask = mask[i];
336 rfilter.can_id |= CAN_INV_FILTER;
338 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
339 &rfilter, sizeof(rfilter));
343 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
344 &err_mask[i], sizeof(err_mask[i]));
346 j = strlen(argv[optind+i]);
348 if (!(j < IFNAMSIZ)) {
349 printf("name of CAN device '%s' is too long!\n", argv[optind+i]);
353 if (j > max_devname_len)
354 max_devname_len = j; /* for nice printing */
356 addr.can_family = AF_CAN;
358 if (strcmp(ANYDEV, argv[optind+i])) {
359 strcpy(ifr.ifr_name, argv[optind+i]);
360 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
361 perror("SIOCGIFINDEX");
364 addr.can_ifindex = ifr.ifr_ifindex;
367 addr.can_ifindex = 0; /* any can interface */
369 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
378 char fname[sizeof("candump-2006-11-20_202026.log")+1];
380 if (time(&currtime) == (time_t)-1) {
385 localtime_r(&currtime, &now);
387 sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log",
395 printf("\nEnabling Logfile '%s'\n\n", fname);
397 logfile = fopen(fname, "w");
407 for (i=0; i<currmax; i++)
410 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
416 for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
418 if (FD_ISSET(s[i], &rdfs)) {
420 socklen_t len = sizeof(addr);
423 if ((nbytes = recvfrom(s[i], &frame,
424 sizeof(struct can_frame), 0,
425 (struct sockaddr*)&addr, &len)) < 0) {
430 if (nbytes < sizeof(struct can_frame)) {
431 fprintf(stderr, "read: incomplete CAN frame\n");
436 if ((nbytes = write(bridge, &frame,
437 sizeof(struct can_frame))) < 0) {
438 perror("bridge write");
440 } else if (nbytes < sizeof(struct can_frame)) {
441 fprintf(stderr,"bridge write: incomplete CAN frame\n");
446 if (timestamp || log || logfrmt)
447 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
448 perror("SIOCGSTAMP");
451 idx = idx2dindex(addr.can_ifindex, s[i]);
454 /* log CAN frame with absolute timestamp & device */
455 fprintf(logfile, "(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
456 fprintf(logfile, "%*s ", max_devname_len, devname[idx]);
457 /* without seperator as logfile use-case is parsing */
458 fprint_canframe(logfile, &frame, "\n", 0);
462 /* print CAN frame in log file style to stdout */
463 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
464 printf("%*s ", max_devname_len, devname[idx]);
465 fprint_canframe(stdout, &frame, "\n", 0);
466 continue; /* no other output to stdout */
471 printf("%c\b", anichar[silentani%=MAXANI]);
474 continue; /* no other output to stdout */
477 printf(" %s", (color>2)?col_on[idx]:"");
481 case 'a': /* absolute with timestamp */
482 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
485 case 'A': /* absolute with date */
490 tm = *localtime(&tv.tv_sec);
491 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
492 printf("(%s.%06ld) ", timestring, tv.tv_usec);
496 case 'd': /* delta */
497 case 'z': /* starting with zero */
501 if (last_tv.tv_sec == 0) /* first init */
503 diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
504 diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
505 if (diff.tv_usec < 0)
506 diff.tv_sec--, diff.tv_usec += 1000000;
508 diff.tv_sec = diff.tv_usec = 0;
509 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
511 if (timestamp == 'd')
512 last_tv = tv; /* update for delta calculation */
516 default: /* no timestamp output */
520 printf(" %s", (color && (color<3))?col_on[idx]:"");
521 printf("%*s", max_devname_len, devname[idx]);
522 printf("%s ", (color==1)?col_off:"");
524 fprint_long_canframe(stdout, &frame, NULL, ascii);
526 printf("%s", (color>1)?col_off:"");
533 for (i=0; i<currmax; i++)