8 * Copyright (c) 2002-2005 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, the following disclaimer and
16 * the referenced file 'COPYING'.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Volkswagen nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * Alternatively, provided that this notice is retained in full, this
25 * software may be distributed under the terms of the GNU General
26 * Public License ("GPL") version 2 as distributed in the 'COPYING'
27 * file from the main directory of the linux kernel source.
29 * The provided data structures and external interfaces from this code
30 * are not restricted to be used by modules with a GPL compatible license.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 * Send feedback to <socketcan-users@lists.berlios.de>
60 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <sys/ioctl.h>
67 #include <linux/can.h>
68 #include <linux/can/bcm.h>
72 #define U64_DATA(p) (*(unsigned long long*)(p)->data)
74 #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)\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, "\n");
180 fprintf(stderr, "%s", manual);
183 void sigterm(int signo)
188 int main(int argc, char **argv)
196 unsigned char quiet = 0;
199 struct timeval timeo, start_tv, tv;
200 struct sockaddr_can addr;
205 signal(SIGTERM, sigterm);
206 signal(SIGHUP, sigterm);
207 signal(SIGINT, sigterm);
209 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
212 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBc")) != -1) {
215 sscanf(optarg, "%x", &mask);
219 sscanf(optarg, "%x", &value);
223 readsettings(optarg, 0); /* no BCM-setting here */
227 sscanf(optarg, "%ld", &timeout);
231 sscanf(optarg, "%ld", &hold);
235 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 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
293 addr.can_family = AF_CAN;
294 strcpy(ifr.ifr_name, argv[optind]);
295 ioctl(s, SIOCGIFINDEX, &ifr);
296 addr.can_ifindex = ifr.ifr_ifindex;
298 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
303 for (i=0; i < 2048 ;i++) /* initial BCM setup */
304 if (is_set(i, ENABLE))
307 gettimeofday(&start_tv, NULL);
308 tv.tv_sec = tv.tv_usec = 0;
310 printf("%s", CSR_HIDE); /* hide cursor */
319 timeo.tv_usec = 100000 * loop;
321 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
327 gettimeofday(&tv, NULL);
328 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
330 if (FD_ISSET(0, &rdfs))
331 running &= handle_keyb(s);
333 if (FD_ISSET(s, &rdfs))
334 running &= handle_bcm(s, currcms);
336 if (currcms - lastcms >= loop) {
337 running &= handle_timeo(s, currcms);
342 printf("%s", CSR_SHOW); /* show cursor */
348 void rx_setup (int fd, int id){
351 struct bcm_msg_head msg_head;
352 struct can_frame frame;
355 txmsg.msg_head.opcode = RX_SETUP;
356 txmsg.msg_head.can_id = id;
357 txmsg.msg_head.flags = RX_CHECK_DLC;
358 txmsg.msg_head.ival1.tv_sec = 0;
359 txmsg.msg_head.ival1.tv_usec = 0;
360 txmsg.msg_head.ival2.tv_sec = 0;
361 txmsg.msg_head.ival2.tv_usec = 0;
362 txmsg.msg_head.nframes = 1;
363 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
365 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
369 void rx_delete (int fd, int id){
371 struct bcm_msg_head msg_head;
373 msg_head.opcode = RX_DELETE;
374 msg_head.can_id = id;
375 msg_head.nframes = 0;
377 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
381 int handle_keyb(int fd){
388 if (read(0, cmd, 19) > strlen("+123456\n"))
389 return 1; /* ignore */
392 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
398 sscanf(&cmd[1], "%x", &value);
399 if (strlen(&cmd[1]) > 3) {
400 mask = value & 0xFFF;
407 for (i=0; i < 2048 ;i++) {
408 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
415 for (i=0; i < 2048 ;i++) {
416 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
425 writesettings(&cmd[1]);
429 readsettings(&cmd[1], fd);
467 for (i=0; i < 2048; i++)
468 U64_DATA(&sniftab[i].notch) = (__u64) 0;
480 int handle_bcm(int fd, long currcms){
485 struct bcm_msg_head msg_head;
486 struct can_frame frame;
489 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
494 id = bmsg.msg_head.can_id;
495 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
497 if (bmsg.msg_head.opcode != RX_CHANGED) {
498 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
502 if (nbytes != sizeof(bmsg)) {
503 printf("received strange BCM data length %d!\n", nbytes);
507 sniftab[id].current = bmsg.frame;
508 U64_DATA(&sniftab[id].marker) |=
509 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
510 sniftab[id].timeout = currcms + timeout;
512 if (is_clr(id, DISPLAY))
513 clearscreen = 1; /* new entry -> new drawing */
521 int handle_timeo(int fd, long currcms){
524 int force_redraw = 0;
528 printf("%s%s", CLR_SCREEN, CSR_HOME);
529 snprintf(startline, 79, "< can-sniffer parameters: l=%ld h=%ld t=%ld >", loop, hold, timeout);
530 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
536 for (i=0; i < 2048; i++)
537 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
541 printf("%s", CSR_HOME);
542 printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
544 for (i=0; i < 2048; i++) {
546 if is_set(i, ENABLE) {
548 if is_set(i, DISPLAY) {
550 if (is_set(i, UPDATE) || (force_redraw)){
552 sniftab[i].hold = currcms + hold;
556 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
557 U64_DATA(&sniftab[i].marker) = (__u64) 0;
559 sniftab[i].hold = 0; /* disable update by hold */
562 printf("%s", CSR_DOWN); /* skip my line */
564 if (sniftab[i].timeout < currcms) {
567 clearscreen = 1; /* removed entry -> new drawing next time */
570 sniftab[i].last = sniftab[i].current;
571 sniftab[i].laststamp = sniftab[i].currstamp;
579 void print_snifline(int id){
581 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
582 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
583 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
587 diffsec--, diffusec += 1000000;
590 diffsec = diffusec = 0;
593 diffsec = 9, diffusec = 999999;
595 printf("%ld.%06ld %3x ", diffsec, diffusec, id);
599 for (i=0; i<sniftab[id].current.can_dlc; i++) {
600 for (j=7; j>=0; j--) {
601 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
602 (!(sniftab[id].notch.data[i] & 1<<j)))
603 if (sniftab[id].current.data[i] & 1<<j)
604 printf("%s1%s", ATTCOLOR, ATTRESET);
606 printf("%s0%s", ATTCOLOR, ATTRESET);
608 if (sniftab[id].current.data[i] & 1<<j)
618 * when the can_dlc decreased (dlc_diff > 0),
619 * we need to blank the former data printout
621 for (i=0; i<dlc_diff; i++) {
629 for (i=0; i<sniftab[id].current.can_dlc; i++)
630 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
631 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
633 printf("%02X ", sniftab[id].current.data[i]);
635 if (sniftab[id].current.can_dlc < 8)
636 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
638 for (i=0; i<sniftab[id].current.can_dlc; i++)
639 if ((sniftab[id].current.data[i] > 0x1F) &&
640 (sniftab[id].current.data[i] < 0x7F))
641 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
642 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
644 putchar(sniftab[id].current.data[i]);
649 * when the can_dlc decreased (dlc_diff > 0),
650 * we need to blank the former data printout
652 for (i=0; i<dlc_diff; i++)
658 U64_DATA(&sniftab[id].marker) = (__u64) 0;
663 void writesettings(char* name){
666 char fname[30] = SETFNAME;
670 strncat(fname, name, 29 - strlen(fname));
671 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
675 for (i=0; i < 2048 ;i++) {
676 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
678 for (j=0; j<8 ; j++){
679 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
683 /* 7 + 16 + 1 = 24 bytes per entry */
688 printf("unable to write setting file '%s'!\n", fname);
691 void readsettings(char* name, int sockfd){
694 char fname[30] = SETFNAME;
698 strncat(fname, name, 29 - strlen(fname));
699 fd = open(fname, O_RDONLY);
703 printf("reading setting file '%s' ... ", fname);
705 for (i=0; i < 2048 ;i++) {
706 if (read(fd, &buf, 24) == 24) {
708 if (is_clr(i, ENABLE)) {
715 if (is_set(i, ENABLE)) {
718 rx_delete(sockfd, i);
720 for (j=7; j>=0 ; j--){
721 sniftab[i].notch.data[j] =
722 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
723 buf[2*j+7] = 0; /* cut off each time */
728 printf("was only able to read until index %d from setting file '%s'!\n",
739 printf("unable to read setting file '%s'!\n", fname);