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 500 /* in 10ms */
90 #define HOLD 100 /* in 10ms */
91 #define LOOP 20 /* in 10ms */
93 #define ATTCOLOR ATTBOLD FGRED
95 #define STARTLINESTR "XX delta ID data ... "
101 struct timeval laststamp;
102 struct timeval currstamp;
103 struct can_frame last;
104 struct can_frame current;
105 struct can_frame marker;
106 struct can_frame notch;
110 extern int optind, opterr, optopt;
112 static int running = 1;
113 static int clearscreen = 1;
115 static int filter_id_only;
116 static long timeout = TIMEOUT;
117 static long hold = HOLD;
118 static long loop = LOOP;
119 static unsigned char binary;
120 static unsigned char binary_gap;
121 static unsigned char color;
122 static char *interface;
124 void rx_setup (int fd, int id);
125 void rx_delete (int fd, int id);
126 void print_snifline(int id);
127 int handle_keyb(int fd);
128 int handle_bcm(int fd, long currcms);
129 int handle_timeo(int fd, long currcms);
130 void writesettings(char* name);
131 void readsettings(char* name, int sockfd);
133 void print_usage(char *prg)
135 const char manual [] = {
136 "commands that can be entered at runtime:\n"
139 "b<ENTER> - toggle binary / HEX-ASCII output\n"
140 "B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
141 "c<ENTER> - toggle color mode\n"
142 "#<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
143 "*<ENTER> - clear notched marked\n"
144 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
145 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
146 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
147 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
149 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
150 "+1F5<ENTER> - add CAN-ID 0x1F5\n"
151 "-42E<ENTER> - remove CAN-ID 0x42E\n"
152 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
153 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
154 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
155 "+000000<ENTER> - add all CAN-IDs\n"
156 "-000000<ENTER> - remove all CAN-IDs\n"
158 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
159 "which is quite easy when the filter is 000\n"
163 fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
164 fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
165 fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
166 fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
167 fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
168 fprintf(stderr, " -b (start with binary mode)\n");
169 fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
170 fprintf(stderr, " -c (color changes)\n");
171 fprintf(stderr, " -f (filter on CAN-ID only)\n");
172 fprintf(stderr, " -t <time> (timeout for ID display [x10ms] default: %d, 0 = OFF)\n", TIMEOUT);
173 fprintf(stderr, " -h <time> (hold marker on changes [x10ms] default: %d)\n", HOLD);
174 fprintf(stderr, " -l <time> (loop time (display) [x10ms] default: %d)\n", LOOP);
175 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
176 fprintf(stderr, "\n");
177 fprintf(stderr, "%s", manual);
180 void sigterm(int signo)
185 int main(int argc, char **argv)
193 unsigned char quiet = 0;
195 struct timeval timeo, start_tv, tv;
196 struct sockaddr_can addr;
201 signal(SIGTERM, sigterm);
202 signal(SIGHUP, sigterm);
203 signal(SIGINT, sigterm);
205 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
208 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -1) {
211 sscanf(optarg, "%x", &mask);
215 sscanf(optarg, "%x", &value);
219 readsettings(optarg, 0); /* no BCM-setting here */
223 sscanf(optarg, "%ld", &timeout);
227 sscanf(optarg, "%ld", &hold);
231 sscanf(optarg, "%ld", &loop);
260 fprintf(stderr, "Unknown option %c\n", opt);
265 if (optind == argc) {
266 print_usage(basename(argv[0]));
271 for (i=0; i < 2048 ;i++) {
272 if ((i & mask) == (value & mask))
280 for (i=0; i < 2048 ;i++)
283 if (strlen(argv[optind]) >= IFNAMSIZ) {
284 printf("name of CAN device '%s' is too long!\n", argv[optind]);
288 interface = argv[optind];
290 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
295 addr.can_family = AF_CAN;
297 if (strcmp(ANYDEV, argv[optind])) {
298 strcpy(ifr.ifr_name, argv[optind]);
299 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
300 perror("SIOCGIFINDEX");
303 addr.can_ifindex = ifr.ifr_ifindex;
306 addr.can_ifindex = 0; /* any can interface */
308 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
313 for (i=0; i < 2048 ;i++) /* initial BCM setup */
314 if (is_set(i, ENABLE))
317 gettimeofday(&start_tv, NULL);
318 tv.tv_sec = tv.tv_usec = 0;
320 printf("%s", CSR_HIDE); /* hide cursor */
329 timeo.tv_usec = 10000 * loop;
331 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
337 gettimeofday(&tv, NULL);
338 currcms = (tv.tv_sec - start_tv.tv_sec) * 100 + (tv.tv_usec / 10000);
340 if (FD_ISSET(0, &rdfs))
341 running &= handle_keyb(s);
343 if (FD_ISSET(s, &rdfs))
344 running &= handle_bcm(s, currcms);
346 if (currcms - lastcms >= loop) {
347 running &= handle_timeo(s, currcms);
352 printf("%s", CSR_SHOW); /* show cursor */
358 void rx_setup (int fd, int id){
361 struct bcm_msg_head msg_head;
362 struct can_frame frame;
365 txmsg.msg_head.opcode = RX_SETUP;
366 txmsg.msg_head.can_id = id;
367 txmsg.msg_head.flags = RX_CHECK_DLC;
368 txmsg.msg_head.ival1.tv_sec = 0;
369 txmsg.msg_head.ival1.tv_usec = 0;
370 txmsg.msg_head.ival2.tv_sec = 0;
371 txmsg.msg_head.ival2.tv_usec = 0;
372 txmsg.msg_head.nframes = 1;
373 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
376 txmsg.msg_head.flags |= RX_FILTER_ID;
378 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
382 void rx_delete (int fd, int id){
384 struct bcm_msg_head msg_head;
386 msg_head.opcode = RX_DELETE;
387 msg_head.can_id = id;
388 msg_head.nframes = 0;
390 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
394 int handle_keyb(int fd){
401 if (read(0, cmd, 19) > strlen("+123456\n"))
402 return 1; /* ignore */
405 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
411 sscanf(&cmd[1], "%x", &value);
412 if (strlen(&cmd[1]) > 3) {
413 mask = value & 0xFFF;
420 for (i=0; i < 2048 ;i++) {
421 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
428 for (i=0; i < 2048 ;i++) {
429 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
438 writesettings(&cmd[1]);
442 readsettings(&cmd[1], fd);
480 for (i=0; i < 2048; i++)
481 U64_DATA(&sniftab[i].notch) = (__u64) 0;
493 int handle_bcm(int fd, long currcms){
498 struct bcm_msg_head msg_head;
499 struct can_frame frame;
502 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
507 id = bmsg.msg_head.can_id;
508 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
510 if (bmsg.msg_head.opcode != RX_CHANGED) {
511 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
515 if (nbytes != sizeof(bmsg)) {
516 printf("received strange BCM data length %d!\n", nbytes);
520 sniftab[id].current = bmsg.frame;
521 U64_DATA(&sniftab[id].marker) |=
522 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
523 sniftab[id].timeout = (timeout)?(currcms + timeout):0;
525 if (is_clr(id, DISPLAY))
526 clearscreen = 1; /* new entry -> new drawing */
534 int handle_timeo(int fd, long currcms){
537 int force_redraw = 0;
538 static unsigned int frame_count;
542 printf("%s%s", CLR_SCREEN, CSR_HOME);
543 snprintf(startline, 79, "< cansniffer %s # l=%ld h=%ld t=%ld >", interface, loop, hold, timeout);
544 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
550 for (i=0; i < 2048; i++)
551 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
555 printf("%s", CSR_HOME);
556 printf("%02d\n", frame_count++); /* rolling display update counter */
559 for (i=0; i < 2048; i++) {
561 if is_set(i, ENABLE) {
563 if is_set(i, DISPLAY) {
565 if (is_set(i, UPDATE) || (force_redraw)){
567 sniftab[i].hold = currcms + hold;
571 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
572 U64_DATA(&sniftab[i].marker) = (__u64) 0;
574 sniftab[i].hold = 0; /* disable update by hold */
577 printf("%s", CSR_DOWN); /* skip my line */
579 if (sniftab[i].timeout && sniftab[i].timeout < currcms) {
582 clearscreen = 1; /* removed entry -> new drawing next time */
585 sniftab[i].last = sniftab[i].current;
586 sniftab[i].laststamp = sniftab[i].currstamp;
594 void print_snifline(int id){
596 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
597 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
598 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
602 diffsec--, diffusec += 1000000;
605 diffsec = diffusec = 0;
608 diffsec = 9, diffusec = 999999;
610 printf("%ld.%06ld %3X ", diffsec, diffusec, id);
614 for (i=0; i<sniftab[id].current.can_dlc; i++) {
615 for (j=7; j>=0; j--) {
616 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
617 (!(sniftab[id].notch.data[i] & 1<<j)))
618 if (sniftab[id].current.data[i] & 1<<j)
619 printf("%s1%s", ATTCOLOR, ATTRESET);
621 printf("%s0%s", ATTCOLOR, ATTRESET);
623 if (sniftab[id].current.data[i] & 1<<j)
633 * when the can_dlc decreased (dlc_diff > 0),
634 * we need to blank the former data printout
636 for (i=0; i<dlc_diff; i++) {
644 for (i=0; i<sniftab[id].current.can_dlc; i++)
645 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
646 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
648 printf("%02X ", sniftab[id].current.data[i]);
650 if (sniftab[id].current.can_dlc < 8)
651 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
653 for (i=0; i<sniftab[id].current.can_dlc; i++)
654 if ((sniftab[id].current.data[i] > 0x1F) &&
655 (sniftab[id].current.data[i] < 0x7F))
656 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
657 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
659 putchar(sniftab[id].current.data[i]);
664 * when the can_dlc decreased (dlc_diff > 0),
665 * we need to blank the former data printout
667 for (i=0; i<dlc_diff; i++)
673 U64_DATA(&sniftab[id].marker) = (__u64) 0;
678 void writesettings(char* name){
681 char fname[30] = SETFNAME;
685 strncat(fname, name, 29 - strlen(fname));
686 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
690 for (i=0; i < 2048 ;i++) {
691 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
693 for (j=0; j<8 ; j++){
694 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
698 /* 7 + 16 + 1 = 24 bytes per entry */
703 printf("unable to write setting file '%s'!\n", fname);
706 void readsettings(char* name, int sockfd){
709 char fname[30] = SETFNAME;
713 strncat(fname, name, 29 - strlen(fname));
714 fd = open(fname, O_RDONLY);
718 printf("reading setting file '%s' ... ", fname);
720 for (i=0; i < 2048 ;i++) {
721 if (read(fd, &buf, 24) == 24) {
723 if (is_clr(i, ENABLE)) {
730 if (is_set(i, ENABLE)) {
733 rx_delete(sockfd, i);
735 for (j=7; j>=0 ; j--){
736 sniftab[i].notch.data[j] =
737 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
738 buf[2*j+7] = 0; /* cut off each time */
743 printf("was only able to read until index %d from setting file '%s'!\n",
754 printf("unable to read setting file '%s'!\n", fname);