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;
121 static int notch = 0;
122 static long timeout = TIMEOUT;
123 static long hold = HOLD;
124 static long loop = LOOP;
125 static unsigned char binary = 0;
126 static unsigned char binary_gap = 0;
127 static unsigned char color = 0;
129 void rx_setup (int fd, int id);
130 void rx_delete (int fd, int id);
131 void print_snifline(int id);
132 int handle_keyb(int fd);
133 int handle_bcm(int fd, long currcms);
134 int handle_timeo(int fd, long currcms);
135 void writesettings(char* name);
136 void readsettings(char* name, int sockfd);
138 void print_usage(char *prg)
140 const char manual [] = {
141 "commands that can be entered at runtime:\n"
144 "b<ENTER> - toggle binary / HEX-ASCII output\n"
145 "B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
146 "c<ENTER> - toggle color mode\n"
147 "#<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
148 "*<ENTER> - clear notched marked\n"
149 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
150 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
151 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
152 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
154 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
155 "+1F5<ENTER> - add CAN-ID 0x1F5\n"
156 "-42E<ENTER> - remove CAN-ID 0x42E\n"
157 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
158 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
159 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
160 "+000000<ENTER> - add all CAN-IDs\n"
161 "-000000<ENTER> - remove all CAN-IDs\n"
163 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
164 "which is quite easy when the filter is 000\n"
168 fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
169 fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
170 fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
171 fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
172 fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
173 fprintf(stderr, " -b (start with binary mode)\n");
174 fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
175 fprintf(stderr, " -c (color changes)\n");
176 fprintf(stderr, " -t <time> (timeout for ID display [x100ms] default: %d, 0 = OFF)\n", TIMEOUT);
177 fprintf(stderr, " -h <time> (hold marker on changes [x100ms] default: %d)\n", HOLD);
178 fprintf(stderr, " -l <time> (loop time (display) [x100ms] default: %d)\n", LOOP);
179 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
180 fprintf(stderr, "\n");
181 fprintf(stderr, "%s", manual);
184 void sigterm(int signo)
189 int main(int argc, char **argv)
197 unsigned char quiet = 0;
200 struct timeval timeo, start_tv, tv;
201 struct sockaddr_can addr;
206 signal(SIGTERM, sigterm);
207 signal(SIGHUP, sigterm);
208 signal(SIGINT, sigterm);
210 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
213 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBc")) != -1) {
216 sscanf(optarg, "%x", &mask);
220 sscanf(optarg, "%x", &value);
224 readsettings(optarg, 0); /* no BCM-setting here */
228 sscanf(optarg, "%ld", &timeout);
232 sscanf(optarg, "%ld", &hold);
236 sscanf(optarg, "%ld", &loop);
261 fprintf(stderr, "Unknown option %c\n", opt);
266 if (optind == argc) {
267 print_usage(basename(argv[0]));
272 for (i=0; i < 2048 ;i++) {
273 if ((i & mask) == (value & mask))
281 for (i=0; i < 2048 ;i++)
284 if (strlen(argv[optind]) >= IFNAMSIZ) {
285 printf("name of CAN device '%s' is too long!\n", argv[optind]);
289 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
294 addr.can_family = AF_CAN;
296 if (strcmp(ANYDEV, argv[optind])) {
297 strcpy(ifr.ifr_name, argv[optind]);
298 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
299 perror("SIOCGIFINDEX");
302 addr.can_ifindex = ifr.ifr_ifindex;
305 addr.can_ifindex = 0; /* any can interface */
307 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
312 for (i=0; i < 2048 ;i++) /* initial BCM setup */
313 if (is_set(i, ENABLE))
316 gettimeofday(&start_tv, NULL);
317 tv.tv_sec = tv.tv_usec = 0;
319 printf("%s", CSR_HIDE); /* hide cursor */
328 timeo.tv_usec = 100000 * loop;
330 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
336 gettimeofday(&tv, NULL);
337 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
339 if (FD_ISSET(0, &rdfs))
340 running &= handle_keyb(s);
342 if (FD_ISSET(s, &rdfs))
343 running &= handle_bcm(s, currcms);
345 if (currcms - lastcms >= loop) {
346 running &= handle_timeo(s, currcms);
351 printf("%s", CSR_SHOW); /* show cursor */
357 void rx_setup (int fd, int id){
360 struct bcm_msg_head msg_head;
361 struct can_frame frame;
364 txmsg.msg_head.opcode = RX_SETUP;
365 txmsg.msg_head.can_id = id;
366 txmsg.msg_head.flags = RX_CHECK_DLC;
367 txmsg.msg_head.ival1.tv_sec = 0;
368 txmsg.msg_head.ival1.tv_usec = 0;
369 txmsg.msg_head.ival2.tv_sec = 0;
370 txmsg.msg_head.ival2.tv_usec = 0;
371 txmsg.msg_head.nframes = 1;
372 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
374 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
378 void rx_delete (int fd, int id){
380 struct bcm_msg_head msg_head;
382 msg_head.opcode = RX_DELETE;
383 msg_head.can_id = id;
384 msg_head.nframes = 0;
386 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
390 int handle_keyb(int fd){
397 if (read(0, cmd, 19) > strlen("+123456\n"))
398 return 1; /* ignore */
401 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
407 sscanf(&cmd[1], "%x", &value);
408 if (strlen(&cmd[1]) > 3) {
409 mask = value & 0xFFF;
416 for (i=0; i < 2048 ;i++) {
417 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
424 for (i=0; i < 2048 ;i++) {
425 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
434 writesettings(&cmd[1]);
438 readsettings(&cmd[1], fd);
476 for (i=0; i < 2048; i++)
477 U64_DATA(&sniftab[i].notch) = (__u64) 0;
489 int handle_bcm(int fd, long currcms){
494 struct bcm_msg_head msg_head;
495 struct can_frame frame;
498 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
503 id = bmsg.msg_head.can_id;
504 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
506 if (bmsg.msg_head.opcode != RX_CHANGED) {
507 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
511 if (nbytes != sizeof(bmsg)) {
512 printf("received strange BCM data length %d!\n", nbytes);
516 sniftab[id].current = bmsg.frame;
517 U64_DATA(&sniftab[id].marker) |=
518 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
519 sniftab[id].timeout = (timeout)?(currcms + timeout):0;
521 if (is_clr(id, DISPLAY))
522 clearscreen = 1; /* new entry -> new drawing */
530 int handle_timeo(int fd, long currcms){
533 int force_redraw = 0;
537 printf("%s%s", CLR_SCREEN, CSR_HOME);
538 snprintf(startline, 79, "< can-sniffer parameters: l=%ld h=%ld t=%ld >", loop, hold, timeout);
539 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
545 for (i=0; i < 2048; i++)
546 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
550 printf("%s", CSR_HOME);
551 printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
553 for (i=0; i < 2048; i++) {
555 if is_set(i, ENABLE) {
557 if is_set(i, DISPLAY) {
559 if (is_set(i, UPDATE) || (force_redraw)){
561 sniftab[i].hold = currcms + hold;
565 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
566 U64_DATA(&sniftab[i].marker) = (__u64) 0;
568 sniftab[i].hold = 0; /* disable update by hold */
571 printf("%s", CSR_DOWN); /* skip my line */
573 if (sniftab[i].timeout && sniftab[i].timeout < currcms) {
576 clearscreen = 1; /* removed entry -> new drawing next time */
579 sniftab[i].last = sniftab[i].current;
580 sniftab[i].laststamp = sniftab[i].currstamp;
588 void print_snifline(int id){
590 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
591 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
592 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
596 diffsec--, diffusec += 1000000;
599 diffsec = diffusec = 0;
602 diffsec = 9, diffusec = 999999;
604 printf("%ld.%06ld %3x ", diffsec, diffusec, id);
608 for (i=0; i<sniftab[id].current.can_dlc; i++) {
609 for (j=7; j>=0; j--) {
610 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
611 (!(sniftab[id].notch.data[i] & 1<<j)))
612 if (sniftab[id].current.data[i] & 1<<j)
613 printf("%s1%s", ATTCOLOR, ATTRESET);
615 printf("%s0%s", ATTCOLOR, ATTRESET);
617 if (sniftab[id].current.data[i] & 1<<j)
627 * when the can_dlc decreased (dlc_diff > 0),
628 * we need to blank the former data printout
630 for (i=0; i<dlc_diff; i++) {
638 for (i=0; i<sniftab[id].current.can_dlc; i++)
639 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
640 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
642 printf("%02X ", sniftab[id].current.data[i]);
644 if (sniftab[id].current.can_dlc < 8)
645 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
647 for (i=0; i<sniftab[id].current.can_dlc; i++)
648 if ((sniftab[id].current.data[i] > 0x1F) &&
649 (sniftab[id].current.data[i] < 0x7F))
650 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
651 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
653 putchar(sniftab[id].current.data[i]);
658 * when the can_dlc decreased (dlc_diff > 0),
659 * we need to blank the former data printout
661 for (i=0; i<dlc_diff; i++)
667 U64_DATA(&sniftab[id].marker) = (__u64) 0;
672 void writesettings(char* name){
675 char fname[30] = SETFNAME;
679 strncat(fname, name, 29 - strlen(fname));
680 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
684 for (i=0; i < 2048 ;i++) {
685 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
687 for (j=0; j<8 ; j++){
688 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
692 /* 7 + 16 + 1 = 24 bytes per entry */
697 printf("unable to write setting file '%s'!\n", fname);
700 void readsettings(char* name, int sockfd){
703 char fname[30] = SETFNAME;
707 strncat(fname, name, 29 - strlen(fname));
708 fd = open(fname, O_RDONLY);
712 printf("reading setting file '%s' ... ", fname);
714 for (i=0; i < 2048 ;i++) {
715 if (read(fd, &buf, 24) == 24) {
717 if (is_clr(i, ENABLE)) {
724 if (is_set(i, ENABLE)) {
727 rx_delete(sockfd, i);
729 for (j=7; j>=0 ; j--){
730 sniftab[i].notch.data[j] =
731 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
732 buf[2*j+7] = 0; /* cut off each time */
737 printf("was only able to read until index %d from setting file '%s'!\n",
748 printf("unable to read setting file '%s'!\n", fname);