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>
59 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
66 #include <linux/can.h>
67 #include <linux/can/bcm.h>
71 #define U64_DATA(p) (*(unsigned long long*)(p)->data)
73 #define SETFNAME "sniffset."
78 #define ENABLE 1 /* by filter or user */
79 #define DISPLAY 2 /* is on the screen */
80 #define UPDATE 4 /* needs to be printed on the screen */
81 #define CLRSCR 8 /* clear screen in next loop */
83 /* flags testing & setting */
85 #define is_set(id, flag) (sniftab[id].flags & flag)
86 #define is_clr(id, flag) (!(sniftab[id].flags & flag))
88 #define do_set(id, flag) (sniftab[id].flags |= flag)
89 #define do_clr(id, flag) (sniftab[id].flags &= ~flag)
93 #define TIMEOUT 50 /* in 100ms */
94 #define HOLD 10 /* in 100ms */
95 #define LOOP 2 /* in 100ms */
98 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
100 #define ATTCOLOR ATTBOLD FGRED
102 #define STARTLINESTR "X time ID data ... "
108 struct timeval laststamp;
109 struct timeval currstamp;
110 struct can_frame last;
111 struct can_frame current;
112 struct can_frame marker;
113 struct can_frame notch;
117 extern int optind, opterr, optopt;
119 static int running = 1;
120 static int clearscreen = 1;
122 static int filter_id_only;
123 static long timeout = TIMEOUT;
124 static long hold = HOLD;
125 static long loop = LOOP;
126 static unsigned char binary;
127 static unsigned char binary_gap;
128 static unsigned char color;
129 static char *interface;
131 void rx_setup (int fd, int id);
132 void rx_delete (int fd, int id);
133 void print_snifline(int id);
134 int handle_keyb(int fd);
135 int handle_bcm(int fd, long currcms);
136 int handle_timeo(int fd, long currcms);
137 void writesettings(char* name);
138 void readsettings(char* name, int sockfd);
140 void print_usage(char *prg)
142 const char manual [] = {
143 "commands that can be entered at runtime:\n"
146 "b<ENTER> - toggle binary / HEX-ASCII output\n"
147 "B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
148 "c<ENTER> - toggle color mode\n"
149 "#<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
150 "*<ENTER> - clear notched marked\n"
151 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
152 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
153 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
154 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
156 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
157 "+1F5<ENTER> - add CAN-ID 0x1F5\n"
158 "-42E<ENTER> - remove CAN-ID 0x42E\n"
159 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
160 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
161 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
162 "+000000<ENTER> - add all CAN-IDs\n"
163 "-000000<ENTER> - remove all CAN-IDs\n"
165 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
166 "which is quite easy when the filter is 000\n"
170 fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
171 fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
172 fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
173 fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
174 fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
175 fprintf(stderr, " -b (start with binary mode)\n");
176 fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
177 fprintf(stderr, " -c (color changes)\n");
178 fprintf(stderr, " -f (filter on CAN-ID only)\n");
179 fprintf(stderr, " -t <time> (timeout for ID display [x100ms] default: %d, 0 = OFF)\n", TIMEOUT);
180 fprintf(stderr, " -h <time> (hold marker on changes [x100ms] default: %d)\n", HOLD);
181 fprintf(stderr, " -l <time> (loop time (display) [x100ms] default: %d)\n", LOOP);
182 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
183 fprintf(stderr, "\n");
184 fprintf(stderr, "%s", manual);
187 void sigterm(int signo)
192 int main(int argc, char **argv)
200 unsigned char quiet = 0;
202 struct timeval timeo, start_tv, tv;
203 struct sockaddr_can addr;
208 signal(SIGTERM, sigterm);
209 signal(SIGHUP, sigterm);
210 signal(SIGINT, sigterm);
212 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
215 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -1) {
218 sscanf(optarg, "%x", &mask);
222 sscanf(optarg, "%x", &value);
226 readsettings(optarg, 0); /* no BCM-setting here */
230 sscanf(optarg, "%ld", &timeout);
234 sscanf(optarg, "%ld", &hold);
238 sscanf(optarg, "%ld", &loop);
267 fprintf(stderr, "Unknown option %c\n", opt);
272 if (optind == argc) {
273 print_usage(basename(argv[0]));
278 for (i=0; i < 2048 ;i++) {
279 if ((i & mask) == (value & mask))
287 for (i=0; i < 2048 ;i++)
290 if (strlen(argv[optind]) >= IFNAMSIZ) {
291 printf("name of CAN device '%s' is too long!\n", argv[optind]);
295 interface = argv[optind];
297 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
302 addr.can_family = AF_CAN;
304 if (strcmp(ANYDEV, argv[optind])) {
305 strcpy(ifr.ifr_name, argv[optind]);
306 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
307 perror("SIOCGIFINDEX");
310 addr.can_ifindex = ifr.ifr_ifindex;
313 addr.can_ifindex = 0; /* any can interface */
315 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
320 for (i=0; i < 2048 ;i++) /* initial BCM setup */
321 if (is_set(i, ENABLE))
324 gettimeofday(&start_tv, NULL);
325 tv.tv_sec = tv.tv_usec = 0;
327 printf("%s", CSR_HIDE); /* hide cursor */
336 timeo.tv_usec = 100000 * loop;
338 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
344 gettimeofday(&tv, NULL);
345 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
347 if (FD_ISSET(0, &rdfs))
348 running &= handle_keyb(s);
350 if (FD_ISSET(s, &rdfs))
351 running &= handle_bcm(s, currcms);
353 if (currcms - lastcms >= loop) {
354 running &= handle_timeo(s, currcms);
359 printf("%s", CSR_SHOW); /* show cursor */
365 void rx_setup (int fd, int id){
368 struct bcm_msg_head msg_head;
369 struct can_frame frame;
372 txmsg.msg_head.opcode = RX_SETUP;
373 txmsg.msg_head.can_id = id;
374 txmsg.msg_head.flags = RX_CHECK_DLC;
375 txmsg.msg_head.ival1.tv_sec = 0;
376 txmsg.msg_head.ival1.tv_usec = 0;
377 txmsg.msg_head.ival2.tv_sec = 0;
378 txmsg.msg_head.ival2.tv_usec = 0;
379 txmsg.msg_head.nframes = 1;
380 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
383 txmsg.msg_head.flags |= RX_FILTER_ID;
385 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
389 void rx_delete (int fd, int id){
391 struct bcm_msg_head msg_head;
393 msg_head.opcode = RX_DELETE;
394 msg_head.can_id = id;
395 msg_head.nframes = 0;
397 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
401 int handle_keyb(int fd){
408 if (read(0, cmd, 19) > strlen("+123456\n"))
409 return 1; /* ignore */
412 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
418 sscanf(&cmd[1], "%x", &value);
419 if (strlen(&cmd[1]) > 3) {
420 mask = value & 0xFFF;
427 for (i=0; i < 2048 ;i++) {
428 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
435 for (i=0; i < 2048 ;i++) {
436 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
445 writesettings(&cmd[1]);
449 readsettings(&cmd[1], fd);
487 for (i=0; i < 2048; i++)
488 U64_DATA(&sniftab[i].notch) = (__u64) 0;
500 int handle_bcm(int fd, long currcms){
505 struct bcm_msg_head msg_head;
506 struct can_frame frame;
509 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
514 id = bmsg.msg_head.can_id;
515 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
517 if (bmsg.msg_head.opcode != RX_CHANGED) {
518 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
522 if (nbytes != sizeof(bmsg)) {
523 printf("received strange BCM data length %d!\n", nbytes);
527 sniftab[id].current = bmsg.frame;
528 U64_DATA(&sniftab[id].marker) |=
529 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
530 sniftab[id].timeout = (timeout)?(currcms + timeout):0;
532 if (is_clr(id, DISPLAY))
533 clearscreen = 1; /* new entry -> new drawing */
541 int handle_timeo(int fd, long currcms){
544 int force_redraw = 0;
548 printf("%s%s", CLR_SCREEN, CSR_HOME);
549 snprintf(startline, 79, "< cansniffer %s # l=%ld h=%ld t=%ld >", interface, loop, hold, timeout);
550 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
556 for (i=0; i < 2048; i++)
557 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
561 printf("%s", CSR_HOME);
562 printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
564 for (i=0; i < 2048; i++) {
566 if is_set(i, ENABLE) {
568 if is_set(i, DISPLAY) {
570 if (is_set(i, UPDATE) || (force_redraw)){
572 sniftab[i].hold = currcms + hold;
576 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
577 U64_DATA(&sniftab[i].marker) = (__u64) 0;
579 sniftab[i].hold = 0; /* disable update by hold */
582 printf("%s", CSR_DOWN); /* skip my line */
584 if (sniftab[i].timeout && sniftab[i].timeout < currcms) {
587 clearscreen = 1; /* removed entry -> new drawing next time */
590 sniftab[i].last = sniftab[i].current;
591 sniftab[i].laststamp = sniftab[i].currstamp;
599 void print_snifline(int id){
601 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
602 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
603 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
607 diffsec--, diffusec += 1000000;
610 diffsec = diffusec = 0;
613 diffsec = 9, diffusec = 999999;
615 printf("%ld.%06ld %3x ", diffsec, diffusec, id);
619 for (i=0; i<sniftab[id].current.can_dlc; i++) {
620 for (j=7; j>=0; j--) {
621 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
622 (!(sniftab[id].notch.data[i] & 1<<j)))
623 if (sniftab[id].current.data[i] & 1<<j)
624 printf("%s1%s", ATTCOLOR, ATTRESET);
626 printf("%s0%s", ATTCOLOR, ATTRESET);
628 if (sniftab[id].current.data[i] & 1<<j)
638 * when the can_dlc decreased (dlc_diff > 0),
639 * we need to blank the former data printout
641 for (i=0; i<dlc_diff; i++) {
649 for (i=0; i<sniftab[id].current.can_dlc; i++)
650 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
651 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
653 printf("%02X ", sniftab[id].current.data[i]);
655 if (sniftab[id].current.can_dlc < 8)
656 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
658 for (i=0; i<sniftab[id].current.can_dlc; i++)
659 if ((sniftab[id].current.data[i] > 0x1F) &&
660 (sniftab[id].current.data[i] < 0x7F))
661 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
662 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
664 putchar(sniftab[id].current.data[i]);
669 * when the can_dlc decreased (dlc_diff > 0),
670 * we need to blank the former data printout
672 for (i=0; i<dlc_diff; i++)
678 U64_DATA(&sniftab[id].marker) = (__u64) 0;
683 void writesettings(char* name){
686 char fname[30] = SETFNAME;
690 strncat(fname, name, 29 - strlen(fname));
691 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
695 for (i=0; i < 2048 ;i++) {
696 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
698 for (j=0; j<8 ; j++){
699 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
703 /* 7 + 16 + 1 = 24 bytes per entry */
708 printf("unable to write setting file '%s'!\n", fname);
711 void readsettings(char* name, int sockfd){
714 char fname[30] = SETFNAME;
718 strncat(fname, name, 29 - strlen(fname));
719 fd = open(fname, O_RDONLY);
723 printf("reading setting file '%s' ... ", fname);
725 for (i=0; i < 2048 ;i++) {
726 if (read(fd, &buf, 24) == 24) {
728 if (is_clr(i, ENABLE)) {
735 if (is_set(i, ENABLE)) {
738 rx_delete(sockfd, i);
740 for (j=7; j>=0 ; j--){
741 sniftab[i].notch.data[j] =
742 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
743 buf[2*j+7] = 0; /* cut off each time */
748 printf("was only able to read until index %d from setting file '%s'!\n",
759 printf("unable to read setting file '%s'!\n", fname);