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;
130 void rx_setup (int fd, int id);
131 void rx_delete (int fd, int id);
132 void print_snifline(int id);
133 int handle_keyb(int fd);
134 int handle_bcm(int fd, long currcms);
135 int handle_timeo(int fd, long currcms);
136 void writesettings(char* name);
137 void readsettings(char* name, int sockfd);
139 void print_usage(char *prg)
141 const char manual [] = {
142 "commands that can be entered at runtime:\n"
145 "b<ENTER> - toggle binary / HEX-ASCII output\n"
146 "B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
147 "c<ENTER> - toggle color mode\n"
148 "#<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
149 "*<ENTER> - clear notched marked\n"
150 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
151 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
152 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
153 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
155 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
156 "+1F5<ENTER> - add CAN-ID 0x1F5\n"
157 "-42E<ENTER> - remove CAN-ID 0x42E\n"
158 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
159 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
160 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
161 "+000000<ENTER> - add all CAN-IDs\n"
162 "-000000<ENTER> - remove all CAN-IDs\n"
164 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
165 "which is quite easy when the filter is 000\n"
169 fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
170 fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
171 fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
172 fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
173 fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
174 fprintf(stderr, " -b (start with binary mode)\n");
175 fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
176 fprintf(stderr, " -c (color changes)\n");
177 fprintf(stderr, " -f (filter on CAN-ID only)\n");
178 fprintf(stderr, " -t <time> (timeout for ID display [x100ms] default: %d, 0 = OFF)\n", TIMEOUT);
179 fprintf(stderr, " -h <time> (hold marker on changes [x100ms] default: %d)\n", HOLD);
180 fprintf(stderr, " -l <time> (loop time (display) [x100ms] default: %d)\n", LOOP);
181 fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
182 fprintf(stderr, "\n");
183 fprintf(stderr, "%s", manual);
186 void sigterm(int signo)
191 int main(int argc, char **argv)
199 unsigned char quiet = 0;
201 struct timeval timeo, start_tv, tv;
202 struct sockaddr_can addr;
207 signal(SIGTERM, sigterm);
208 signal(SIGHUP, sigterm);
209 signal(SIGINT, sigterm);
211 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
214 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf")) != -1) {
217 sscanf(optarg, "%x", &mask);
221 sscanf(optarg, "%x", &value);
225 readsettings(optarg, 0); /* no BCM-setting here */
229 sscanf(optarg, "%ld", &timeout);
233 sscanf(optarg, "%ld", &hold);
237 sscanf(optarg, "%ld", &loop);
266 fprintf(stderr, "Unknown option %c\n", opt);
271 if (optind == argc) {
272 print_usage(basename(argv[0]));
277 for (i=0; i < 2048 ;i++) {
278 if ((i & mask) == (value & mask))
286 for (i=0; i < 2048 ;i++)
289 if (strlen(argv[optind]) >= IFNAMSIZ) {
290 printf("name of CAN device '%s' is too long!\n", argv[optind]);
294 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
299 addr.can_family = AF_CAN;
301 if (strcmp(ANYDEV, argv[optind])) {
302 strcpy(ifr.ifr_name, argv[optind]);
303 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
304 perror("SIOCGIFINDEX");
307 addr.can_ifindex = ifr.ifr_ifindex;
310 addr.can_ifindex = 0; /* any can interface */
312 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
317 for (i=0; i < 2048 ;i++) /* initial BCM setup */
318 if (is_set(i, ENABLE))
321 gettimeofday(&start_tv, NULL);
322 tv.tv_sec = tv.tv_usec = 0;
324 printf("%s", CSR_HIDE); /* hide cursor */
333 timeo.tv_usec = 100000 * loop;
335 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
341 gettimeofday(&tv, NULL);
342 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
344 if (FD_ISSET(0, &rdfs))
345 running &= handle_keyb(s);
347 if (FD_ISSET(s, &rdfs))
348 running &= handle_bcm(s, currcms);
350 if (currcms - lastcms >= loop) {
351 running &= handle_timeo(s, currcms);
356 printf("%s", CSR_SHOW); /* show cursor */
362 void rx_setup (int fd, int id){
365 struct bcm_msg_head msg_head;
366 struct can_frame frame;
369 txmsg.msg_head.opcode = RX_SETUP;
370 txmsg.msg_head.can_id = id;
371 txmsg.msg_head.flags = RX_CHECK_DLC;
372 txmsg.msg_head.ival1.tv_sec = 0;
373 txmsg.msg_head.ival1.tv_usec = 0;
374 txmsg.msg_head.ival2.tv_sec = 0;
375 txmsg.msg_head.ival2.tv_usec = 0;
376 txmsg.msg_head.nframes = 1;
377 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
380 txmsg.msg_head.flags |= RX_FILTER_ID;
382 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
386 void rx_delete (int fd, int id){
388 struct bcm_msg_head msg_head;
390 msg_head.opcode = RX_DELETE;
391 msg_head.can_id = id;
392 msg_head.nframes = 0;
394 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
398 int handle_keyb(int fd){
405 if (read(0, cmd, 19) > strlen("+123456\n"))
406 return 1; /* ignore */
409 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
415 sscanf(&cmd[1], "%x", &value);
416 if (strlen(&cmd[1]) > 3) {
417 mask = value & 0xFFF;
424 for (i=0; i < 2048 ;i++) {
425 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
432 for (i=0; i < 2048 ;i++) {
433 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
442 writesettings(&cmd[1]);
446 readsettings(&cmd[1], fd);
484 for (i=0; i < 2048; i++)
485 U64_DATA(&sniftab[i].notch) = (__u64) 0;
497 int handle_bcm(int fd, long currcms){
502 struct bcm_msg_head msg_head;
503 struct can_frame frame;
506 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
511 id = bmsg.msg_head.can_id;
512 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
514 if (bmsg.msg_head.opcode != RX_CHANGED) {
515 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
519 if (nbytes != sizeof(bmsg)) {
520 printf("received strange BCM data length %d!\n", nbytes);
524 sniftab[id].current = bmsg.frame;
525 U64_DATA(&sniftab[id].marker) |=
526 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
527 sniftab[id].timeout = (timeout)?(currcms + timeout):0;
529 if (is_clr(id, DISPLAY))
530 clearscreen = 1; /* new entry -> new drawing */
538 int handle_timeo(int fd, long currcms){
541 int force_redraw = 0;
545 printf("%s%s", CLR_SCREEN, CSR_HOME);
546 snprintf(startline, 79, "< can-sniffer parameters: l=%ld h=%ld t=%ld >", loop, hold, timeout);
547 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
553 for (i=0; i < 2048; i++)
554 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
558 printf("%s", CSR_HOME);
559 printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
561 for (i=0; i < 2048; i++) {
563 if is_set(i, ENABLE) {
565 if is_set(i, DISPLAY) {
567 if (is_set(i, UPDATE) || (force_redraw)){
569 sniftab[i].hold = currcms + hold;
573 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
574 U64_DATA(&sniftab[i].marker) = (__u64) 0;
576 sniftab[i].hold = 0; /* disable update by hold */
579 printf("%s", CSR_DOWN); /* skip my line */
581 if (sniftab[i].timeout && sniftab[i].timeout < currcms) {
584 clearscreen = 1; /* removed entry -> new drawing next time */
587 sniftab[i].last = sniftab[i].current;
588 sniftab[i].laststamp = sniftab[i].currstamp;
596 void print_snifline(int id){
598 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
599 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
600 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
604 diffsec--, diffusec += 1000000;
607 diffsec = diffusec = 0;
610 diffsec = 9, diffusec = 999999;
612 printf("%ld.%06ld %3x ", diffsec, diffusec, id);
616 for (i=0; i<sniftab[id].current.can_dlc; i++) {
617 for (j=7; j>=0; j--) {
618 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
619 (!(sniftab[id].notch.data[i] & 1<<j)))
620 if (sniftab[id].current.data[i] & 1<<j)
621 printf("%s1%s", ATTCOLOR, ATTRESET);
623 printf("%s0%s", ATTCOLOR, ATTRESET);
625 if (sniftab[id].current.data[i] & 1<<j)
635 * when the can_dlc decreased (dlc_diff > 0),
636 * we need to blank the former data printout
638 for (i=0; i<dlc_diff; i++) {
646 for (i=0; i<sniftab[id].current.can_dlc; i++)
647 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
648 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
650 printf("%02X ", sniftab[id].current.data[i]);
652 if (sniftab[id].current.can_dlc < 8)
653 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
655 for (i=0; i<sniftab[id].current.can_dlc; i++)
656 if ((sniftab[id].current.data[i] > 0x1F) &&
657 (sniftab[id].current.data[i] < 0x7F))
658 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
659 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
661 putchar(sniftab[id].current.data[i]);
666 * when the can_dlc decreased (dlc_diff > 0),
667 * we need to blank the former data printout
669 for (i=0; i<dlc_diff; i++)
675 U64_DATA(&sniftab[id].marker) = (__u64) 0;
680 void writesettings(char* name){
683 char fname[30] = SETFNAME;
687 strncat(fname, name, 29 - strlen(fname));
688 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
692 for (i=0; i < 2048 ;i++) {
693 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
695 for (j=0; j<8 ; j++){
696 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
700 /* 7 + 16 + 1 = 24 bytes per entry */
705 printf("unable to write setting file '%s'!\n", fname);
708 void readsettings(char* name, int sockfd){
711 char fname[30] = SETFNAME;
715 strncat(fname, name, 29 - strlen(fname));
716 fd = open(fname, O_RDONLY);
720 printf("reading setting file '%s' ... ", fname);
722 for (i=0; i < 2048 ;i++) {
723 if (read(fd, &buf, 24) == 24) {
725 if (is_clr(i, ENABLE)) {
732 if (is_set(i, ENABLE)) {
735 rx_delete(sockfd, i);
737 for (j=7; j>=0 ; j--){
738 sniftab[i].notch.data[j] =
739 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
740 buf[2*j+7] = 0; /* cut off each time */
745 printf("was only able to read until index %d from setting file '%s'!\n",
756 printf("unable to read setting file '%s'!\n", fname);