4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
55 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
62 #include <linux/can.h>
63 #include <linux/can/bcm.h>
67 #define U64_DATA(p) (*(unsigned long long*)(p)->data)
69 #define SETFNAME "sniffset."
74 #define ENABLE 1 /* by filter or user */
75 #define DISPLAY 2 /* is on the screen */
76 #define UPDATE 4 /* needs to be printed on the screen */
77 #define CLRSCR 8 /* clear screen in next loop */
79 /* flags testing & setting */
81 #define is_set(id, flag) (sniftab[id].flags & flag)
82 #define is_clr(id, flag) (!(sniftab[id].flags & flag))
84 #define do_set(id, flag) (sniftab[id].flags |= flag)
85 #define do_clr(id, flag) (sniftab[id].flags &= ~flag)
89 #define TIMEOUT 50 /* in 100ms */
90 #define HOLD 10 /* in 100ms */
91 #define LOOP 2 /* in 100ms */
94 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
96 #define ATTCOLOR ATTBOLD FGRED
98 #define STARTLINESTR "X time ID data ... "
104 struct timeval laststamp;
105 struct timeval currstamp;
106 struct can_frame last;
107 struct can_frame current;
108 struct can_frame marker;
109 struct can_frame notch;
113 extern int optind, opterr, optopt;
115 static int running = 1;
116 static int clearscreen = 1;
118 static int filter_id_only;
119 static long timeout = TIMEOUT;
120 static long hold = HOLD;
121 static long loop = LOOP;
122 static unsigned char binary;
123 static unsigned char binary_gap;
124 static unsigned char color;
125 static char *interface;
127 void rx_setup (int fd, int id);
128 void rx_delete (int fd, int id);
129 void print_snifline(int id);
130 int handle_keyb(int fd);
131 int handle_bcm(int fd, long currcms);
132 int handle_timeo(int fd, long currcms);
133 void writesettings(char* name);
134 void readsettings(char* name, int sockfd);
136 void print_usage(char *prg)
138 const char manual [] = {
139 "commands that can be entered at runtime:\n"
142 "b<ENTER> - toggle binary / HEX-ASCII output\n"
143 "B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
144 "c<ENTER> - toggle color mode\n"
145 "#<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
146 "*<ENTER> - clear notched marked\n"
147 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
148 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
149 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
150 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
152 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
153 "+1F5<ENTER> - add CAN-ID 0x1F5\n"
154 "-42E<ENTER> - remove CAN-ID 0x42E\n"
155 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
156 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
157 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
158 "+000000<ENTER> - add all CAN-IDs\n"
159 "-000000<ENTER> - remove all CAN-IDs\n"
161 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
162 "which is quite easy when the filter is 000\n"
166 fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
167 fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
168 fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
169 fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
170 fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
171 fprintf(stderr, " -b (start with binary mode)\n");
172 fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
173 fprintf(stderr, " -c (color changes)\n");
174 fprintf(stderr, " -f (filter on CAN-ID only)\n");
175 fprintf(stderr, " -t <time> (timeout for ID display [x100ms] default: %d, 0 = OFF)\n", TIMEOUT);
176 fprintf(stderr, " -h <time> (hold marker on changes [x100ms] default: %d)\n", HOLD);
177 fprintf(stderr, " -l <time> (loop time (display) [x100ms] default: %d)\n", LOOP);
178 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
179 fprintf(stderr, "\n");
180 fprintf(stderr, "%s", manual);
183 void sigterm(int signo)
188 int main(int argc, char **argv)
196 unsigned char quiet = 0;
198 struct timeval timeo, start_tv, tv;
199 struct sockaddr_can addr;
204 signal(SIGTERM, sigterm);
205 signal(SIGHUP, sigterm);
206 signal(SIGINT, sigterm);
208 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
211 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -1) {
214 sscanf(optarg, "%x", &mask);
218 sscanf(optarg, "%x", &value);
222 readsettings(optarg, 0); /* no BCM-setting here */
226 sscanf(optarg, "%ld", &timeout);
230 sscanf(optarg, "%ld", &hold);
234 sscanf(optarg, "%ld", &loop);
263 fprintf(stderr, "Unknown option %c\n", opt);
268 if (optind == argc) {
269 print_usage(basename(argv[0]));
274 for (i=0; i < 2048 ;i++) {
275 if ((i & mask) == (value & mask))
283 for (i=0; i < 2048 ;i++)
286 if (strlen(argv[optind]) >= IFNAMSIZ) {
287 printf("name of CAN device '%s' is too long!\n", argv[optind]);
291 interface = argv[optind];
293 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
298 addr.can_family = AF_CAN;
300 if (strcmp(ANYDEV, argv[optind])) {
301 strcpy(ifr.ifr_name, argv[optind]);
302 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
303 perror("SIOCGIFINDEX");
306 addr.can_ifindex = ifr.ifr_ifindex;
309 addr.can_ifindex = 0; /* any can interface */
311 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
316 for (i=0; i < 2048 ;i++) /* initial BCM setup */
317 if (is_set(i, ENABLE))
320 gettimeofday(&start_tv, NULL);
321 tv.tv_sec = tv.tv_usec = 0;
323 printf("%s", CSR_HIDE); /* hide cursor */
332 timeo.tv_usec = 100000 * loop;
334 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
340 gettimeofday(&tv, NULL);
341 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
343 if (FD_ISSET(0, &rdfs))
344 running &= handle_keyb(s);
346 if (FD_ISSET(s, &rdfs))
347 running &= handle_bcm(s, currcms);
349 if (currcms - lastcms >= loop) {
350 running &= handle_timeo(s, currcms);
355 printf("%s", CSR_SHOW); /* show cursor */
361 void rx_setup (int fd, int id){
364 struct bcm_msg_head msg_head;
365 struct can_frame frame;
368 txmsg.msg_head.opcode = RX_SETUP;
369 txmsg.msg_head.can_id = id;
370 txmsg.msg_head.flags = RX_CHECK_DLC;
371 txmsg.msg_head.ival1.tv_sec = 0;
372 txmsg.msg_head.ival1.tv_usec = 0;
373 txmsg.msg_head.ival2.tv_sec = 0;
374 txmsg.msg_head.ival2.tv_usec = 0;
375 txmsg.msg_head.nframes = 1;
376 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
379 txmsg.msg_head.flags |= RX_FILTER_ID;
381 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
385 void rx_delete (int fd, int id){
387 struct bcm_msg_head msg_head;
389 msg_head.opcode = RX_DELETE;
390 msg_head.can_id = id;
391 msg_head.nframes = 0;
393 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
397 int handle_keyb(int fd){
404 if (read(0, cmd, 19) > strlen("+123456\n"))
405 return 1; /* ignore */
408 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
414 sscanf(&cmd[1], "%x", &value);
415 if (strlen(&cmd[1]) > 3) {
416 mask = value & 0xFFF;
423 for (i=0; i < 2048 ;i++) {
424 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
431 for (i=0; i < 2048 ;i++) {
432 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
441 writesettings(&cmd[1]);
445 readsettings(&cmd[1], fd);
483 for (i=0; i < 2048; i++)
484 U64_DATA(&sniftab[i].notch) = (__u64) 0;
496 int handle_bcm(int fd, long currcms){
501 struct bcm_msg_head msg_head;
502 struct can_frame frame;
505 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
510 id = bmsg.msg_head.can_id;
511 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
513 if (bmsg.msg_head.opcode != RX_CHANGED) {
514 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
518 if (nbytes != sizeof(bmsg)) {
519 printf("received strange BCM data length %d!\n", nbytes);
523 sniftab[id].current = bmsg.frame;
524 U64_DATA(&sniftab[id].marker) |=
525 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
526 sniftab[id].timeout = (timeout)?(currcms + timeout):0;
528 if (is_clr(id, DISPLAY))
529 clearscreen = 1; /* new entry -> new drawing */
537 int handle_timeo(int fd, long currcms){
540 int force_redraw = 0;
544 printf("%s%s", CLR_SCREEN, CSR_HOME);
545 snprintf(startline, 79, "< cansniffer %s # l=%ld h=%ld t=%ld >", interface, loop, hold, timeout);
546 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
552 for (i=0; i < 2048; i++)
553 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
557 printf("%s", CSR_HOME);
558 printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
560 for (i=0; i < 2048; i++) {
562 if is_set(i, ENABLE) {
564 if is_set(i, DISPLAY) {
566 if (is_set(i, UPDATE) || (force_redraw)){
568 sniftab[i].hold = currcms + hold;
572 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
573 U64_DATA(&sniftab[i].marker) = (__u64) 0;
575 sniftab[i].hold = 0; /* disable update by hold */
578 printf("%s", CSR_DOWN); /* skip my line */
580 if (sniftab[i].timeout && sniftab[i].timeout < currcms) {
583 clearscreen = 1; /* removed entry -> new drawing next time */
586 sniftab[i].last = sniftab[i].current;
587 sniftab[i].laststamp = sniftab[i].currstamp;
595 void print_snifline(int id){
597 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
598 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
599 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
603 diffsec--, diffusec += 1000000;
606 diffsec = diffusec = 0;
609 diffsec = 9, diffusec = 999999;
611 printf("%ld.%06ld %3x ", diffsec, diffusec, id);
615 for (i=0; i<sniftab[id].current.can_dlc; i++) {
616 for (j=7; j>=0; j--) {
617 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
618 (!(sniftab[id].notch.data[i] & 1<<j)))
619 if (sniftab[id].current.data[i] & 1<<j)
620 printf("%s1%s", ATTCOLOR, ATTRESET);
622 printf("%s0%s", ATTCOLOR, ATTRESET);
624 if (sniftab[id].current.data[i] & 1<<j)
634 * when the can_dlc decreased (dlc_diff > 0),
635 * we need to blank the former data printout
637 for (i=0; i<dlc_diff; i++) {
645 for (i=0; i<sniftab[id].current.can_dlc; i++)
646 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
647 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
649 printf("%02X ", sniftab[id].current.data[i]);
651 if (sniftab[id].current.can_dlc < 8)
652 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
654 for (i=0; i<sniftab[id].current.can_dlc; i++)
655 if ((sniftab[id].current.data[i] > 0x1F) &&
656 (sniftab[id].current.data[i] < 0x7F))
657 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
658 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
660 putchar(sniftab[id].current.data[i]);
665 * when the can_dlc decreased (dlc_diff > 0),
666 * we need to blank the former data printout
668 for (i=0; i<dlc_diff; i++)
674 U64_DATA(&sniftab[id].marker) = (__u64) 0;
679 void writesettings(char* name){
682 char fname[30] = SETFNAME;
686 strncat(fname, name, 29 - strlen(fname));
687 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
691 for (i=0; i < 2048 ;i++) {
692 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
694 for (j=0; j<8 ; j++){
695 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
699 /* 7 + 16 + 1 = 24 bytes per entry */
704 printf("unable to write setting file '%s'!\n", fname);
707 void readsettings(char* name, int sockfd){
710 char fname[30] = SETFNAME;
714 strncat(fname, name, 29 - strlen(fname));
715 fd = open(fname, O_RDONLY);
719 printf("reading setting file '%s' ... ", fname);
721 for (i=0; i < 2048 ;i++) {
722 if (read(fd, &buf, 24) == 24) {
724 if (is_clr(i, ENABLE)) {
731 if (is_set(i, ENABLE)) {
734 rx_delete(sockfd, i);
736 for (j=7; j>=0 ; j--){
737 sniftab[i].notch.data[j] =
738 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
739 buf[2*j+7] = 0; /* cut off each time */
744 printf("was only able to read until index %d from setting file '%s'!\n",
755 printf("unable to read setting file '%s'!\n", fname);