2 * $Id: candump.c,v 2.0 2006/04/13 10:37:20 ethuerm Exp $
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 <llcf@volkswagen.de>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
68 #define USE_RECVFROM /* use read() or recvfrom() syscall */
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 static const char col_on [MAXDEV][19] = {BOLD, MAGENTA, GREEN, BLUE, CYAN, RED};
83 static const char col_off [] = ATTRESET;
86 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
88 extern int optind, opterr, optopt;
90 static int running = 1;
92 void print_usage(char *prg)
94 fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
95 fprintf(stderr, "Options: -m <mask> (default 0x00000000)\n");
96 fprintf(stderr, " -v <value> (default 0x00000000)\n");
97 fprintf(stderr, " -i <0|1> (inv_filter)\n");
98 fprintf(stderr, " -t <type> (timestamp: Absolute/Delta/Zero)\n");
99 fprintf(stderr, " -c (color mode)\n");
100 fprintf(stderr, " -s <level> (silent mode - 1: animation 2: nothing)\n");
101 fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\n");
102 fprintf(stderr, " -a (create ASC compatible output)\n");
103 fprintf(stderr, " -1 (increment interface numbering in ASC mode)\n");
104 fprintf(stderr, " -A (enable ASCII output)\n");
105 fprintf(stderr, "\n");
106 fprintf(stderr, "When using more than one CAN interface the options\n");
107 fprintf(stderr, "m/v/i have comma seperated values e.g. '-m 0,7FF,0'\n");
108 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
111 void sigterm(int signo)
116 int main(int argc, char **argv)
121 canid_t mask[MAXDEV] = {0};
122 canid_t value[MAXDEV] = {0};
123 int inv_filter[MAXDEV] = {0};
124 char devname[MAXDEV][IFNAMSIZ];
125 unsigned char timestamp = 0;
126 unsigned char silent = 0;
127 unsigned char silentani = 0;
128 unsigned char color = 0;
129 unsigned char ascii = 0;
130 unsigned char asc = 0;
131 unsigned char asc_inc_channel = 0;
132 int max_devname_len = 0;
134 int currmax = 1; /* we assume at least one can bus ;-) */
135 struct sockaddr_can addr;
136 struct can_filter rfilter;
137 struct can_frame frame;
142 struct timeval tv, last_tv;
144 signal(SIGTERM, sigterm);
145 signal(SIGHUP, sigterm);
146 signal(SIGINT, sigterm);
148 last_tv.tv_sec = 0; /* init */
150 while ((opt = getopt(argc, argv, "m:v:i:b:s:ca1At:")) != -1) {
153 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
154 &mask[0], &mask[1], &mask[2],
155 &mask[3], &mask[4], &mask[5]);
161 i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
162 &value[0], &value[1], &value[2],
163 &value[3], &value[4], &value[5]);
169 i = sscanf(optarg, "%d,%d,%d,%d,%d,%d",
170 &inv_filter[0], &inv_filter[1], &inv_filter[2],
171 &inv_filter[3], &inv_filter[4], &inv_filter[5]);
177 if (strlen(optarg) >= IFNAMSIZ) {
178 printf("Name of CAN device '%s' is too long!\n\n", optarg);
182 if ((bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
183 perror("bridge socket");
186 addr.can_family = AF_CAN;
187 strcpy(ifr.ifr_name, optarg);
188 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
189 perror("SIOCGIFINDEX");
190 addr.can_ifindex = ifr.ifr_ifindex;
192 if (!addr.can_ifindex) {
193 perror("invalid bridge interface");
197 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
198 perror("bridge bind");
205 silent = atoi(optarg);
225 timestamp = optarg[0];
226 if ((timestamp != 'a') && (timestamp != 'A') && (timestamp != 'd') && (timestamp != 'z')) {
227 printf("%s: unknown timestamp mode '%c' - ignored\n",
228 basename(argv[0]), optarg[0]);
237 fprintf(stderr, "Unknown option %c\n", opt);
242 if (optind == argc) {
243 print_usage(basename(argv[0]));
247 /* count in options higher than device count ? */
248 if (optind + currmax > argc) {
249 printf("low count of CAN devices!\n");
253 currmax = argc - optind; /* find real number of CAN devices */
255 if (currmax > MAXDEV) {
256 printf("More than %d CAN devices!\n", MAXDEV);
260 for (i=0; i<currmax; i++) {
263 printf("open %d '%s' m%08X v%08X i%d.\n",
264 i, argv[optind+i], mask[i], value[i], inv_filter[i]);
267 if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
272 if (mask[i] || value[i]) {
274 if (!(asc)) /* this output is not asc compatible! */
275 printf("CAN ID filter[%d] for %s set to mask = %08X, value = %08X %s\n",
276 i, argv[optind+i], mask[i], value[i],
277 (inv_filter[i]) ? "(inv_filter)" : "");
279 rfilter.can_id = value[i];
280 rfilter.can_mask = mask[i];
282 rfilter.can_id |= CAN_INV_FILTER;
284 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
287 j = strlen(argv[optind+i]);
289 if (!(j < IFNAMSIZ)) {
290 printf("name of CAN device '%s' is too long!\n", argv[optind+i]);
294 strcpy(devname[i], argv[optind+i]);
296 if (j > max_devname_len)
297 max_devname_len = j; /* for nice printing */
299 addr.can_family = AF_CAN;
301 if (strcmp(ANYDEV, argv[optind+i])) {
302 strcpy(ifr.ifr_name, argv[optind+i]);
303 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0)
304 perror("SIOCGIFINDEX");
305 addr.can_ifindex = ifr.ifr_ifindex;
308 addr.can_ifindex = 0; /* any can interface */
310 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
319 /* print banner for ASC mode */
321 if (timestamp != 'd') /* delta time is allowed, else ... */
322 timestamp = 'z'; /* ASC-files always start with zero time */
324 if (time(&currtime) == (time_t)-1) {
328 strncpy(datestring, ctime(&currtime), 39); /* copy to private buffer */
329 datestring[strlen(datestring)-1] = 0; /* chop off trailing newline */
330 printf("date %s%s", datestring, ANL); /* print with own new line representation */
332 printf("base hex timestamps %s%s", (timestamp == 'd')?"relative":"absolute", ANL);
333 printf("no internal events logged%s", ANL);
341 for (i=0; i<currmax; i++)
344 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
350 for (i=0; i<currmax; i++) {
352 if (FD_ISSET(s[i], &rdfs)) {
355 socklen_t len = sizeof(addr);
356 if ((nbytes = recvfrom(s[i], &frame, sizeof(struct can_frame), 0, (struct sockaddr*)&addr, &len)) < 0) {
358 if ((nbytes = read(s[i], &frame, sizeof(struct can_frame))) < 0) {
362 } else if (nbytes < sizeof(struct can_frame)) {
363 fprintf(stderr, "read: incomplete CAN frame\n");
367 if ((nbytes = write(bridge, &frame, sizeof(struct can_frame))) < 0) {
368 perror("bridge write");
370 } else if (nbytes < sizeof(struct can_frame)) {
371 fprintf(stderr, "bridge write: incomplete CAN frame\n");
378 printf("%c\b", anichar[silentani%=MAXANI]), silentani++;
384 case 'a': /* absolute with timestamp */
385 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
386 perror("SIOCGSTAMP");
388 printf("%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
390 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
393 case 'A': /* absolute with date */
397 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
398 perror("SIOCGSTAMP");
399 tm = *localtime(&tv.tv_sec);
400 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
402 printf("%s.%04ld ", timestring, tv.tv_usec/100);
404 printf("(%s.%06ld) ", timestring, tv.tv_usec);
408 case 'd': /* delta */
409 case 'z': /* starting with zero */
413 if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
414 perror("SIOCGSTAMP");
415 if (last_tv.tv_sec == 0) /* first init */
417 diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
418 diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
419 if (diff.tv_usec < 0)
420 diff.tv_sec--, diff.tv_usec += 1000000;
422 diff.tv_sec = diff.tv_usec = 0;
424 printf("%4ld.%04ld ", diff.tv_sec, diff.tv_usec/100);
426 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
428 if (timestamp == 'd')
429 last_tv = tv; /* update for delta calculation */
433 default: /* no timestamp output */
440 printf("%-2d ", i + asc_inc_channel); /* channel number - left aligned */
442 sprintf(id, "%X%c", frame.can_id & CAN_EFF_MASK,
443 (frame.can_id & CAN_EFF_FLAG)?'x':' ');
444 printf("%-15s Rx ", id);
446 if (frame.can_id & CAN_RTR_FLAG)
447 printf("r"); /* RTR frame: nothing else to print */
449 printf("d %d ", frame.can_dlc); /* data frame */
451 for (j = 0; j < frame.can_dlc; j++) {
452 printf("%02X ", frame.data[j]);
458 printf(" %s",(color)?col_on[i]:"");
460 ifr.ifr_ifindex = addr.can_ifindex;
461 if (ioctl(s[i], SIOCGIFNAME, &ifr) < 0)
462 perror("SIOCGIFNAME");
464 if (max_devname_len < strlen(ifr.ifr_name))
465 max_devname_len = strlen(ifr.ifr_name);
467 printf("%*s", max_devname_len, ifr.ifr_name);
469 printf("%*s", max_devname_len, devname[i]); /* device name */
471 printf("%s ",(color)?col_off:"");
472 if (frame.can_id & CAN_EFF_FLAG)
473 printf("%8X ", frame.can_id & CAN_EFF_MASK);
475 printf("%3X ", frame.can_id & CAN_SFF_MASK);
477 printf("[%d] ", frame.can_dlc);
479 for (j = 0; j < frame.can_dlc; j++) {
480 printf("%02X ", frame.data[j]);
483 printf("%*s", 3*(8-frame.can_dlc)+3, "'");
484 for (j = 0; j < frame.can_dlc; j++)
485 if ((frame.data[j] > 0x1F) && (frame.data[j] < 0x7F))
486 putchar(frame.data[j]);
491 if (frame.can_id & CAN_RTR_FLAG)
492 printf("remote request");
502 for (i=0; i<currmax; i++)