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."
77 #define ENABLE 1 /* by filter or user */
78 #define DISPLAY 2 /* is on the screen */
79 #define UPDATE 4 /* needs to be printed on the screen */
80 #define CLRSCR 8 /* clear screen in next loop */
82 /* flags testing & setting */
84 #define is_set(id, flag) (sniftab[id].flags & flag)
85 #define is_clr(id, flag) (!(sniftab[id].flags & flag))
87 #define do_set(id, flag) (sniftab[id].flags |= flag)
88 #define do_clr(id, flag) (sniftab[id].flags &= ~flag)
92 #define TIMEOUT 50 /* in 100ms */
93 #define HOLD 10 /* in 100ms */
94 #define LOOP 2 /* in 100ms */
97 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
99 #define ATTCOLOR ATTBOLD FGRED
101 #define STARTLINESTR "X time ID data ... "
107 struct timeval laststamp;
108 struct timeval currstamp;
109 struct can_frame last;
110 struct can_frame current;
111 struct can_frame marker;
112 struct can_frame notch;
116 extern int optind, opterr, optopt;
118 static int running = 1;
119 static int clearscreen = 1;
120 static int notch = 0;
121 static long timeout = TIMEOUT;
122 static long hold = HOLD;
123 static long loop = LOOP;
124 static unsigned char binary = 0;
125 static unsigned char binary_gap = 0;
126 static unsigned char color = 0;
128 void rx_setup (int fd, int id);
129 void rx_delete (int fd, int id);
130 void print_snifline(int id);
131 int handle_keyb(int fd);
132 int handle_bcm(int fd, long currcms);
133 int handle_timeo(int fd, long currcms);
134 void writesettings(char* name);
135 void readsettings(char* name, int sockfd);
137 void print_usage(char *prg)
139 const char manual [] = {
140 "commands that can be entered at runtime:\n"
143 "b<ENTER> - toggle binary / HEX-ASCII output\n"
144 "B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
145 "c<ENTER> - toggle color mode\n"
146 "#<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
147 "*<ENTER> - clear notched marked\n"
148 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
149 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
150 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
151 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
153 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
154 "+1F5<ENTER> - add CAN-ID 0x1F5\n"
155 "-42E<ENTER> - remove CAN-ID 0x42E\n"
156 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
157 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
158 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
159 "+000000<ENTER> - add all CAN-IDs\n"
160 "-000000<ENTER> - remove all CAN-IDs\n"
162 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
163 "which is quite easy when the filter is 000\n"
167 fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
168 fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
169 fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
170 fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
171 fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
172 fprintf(stderr, " -b (start with binary mode)\n");
173 fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
174 fprintf(stderr, " -c (color changes)\n");
175 fprintf(stderr, " -t <time> (timeout for ID display [x100ms] default: %d)\n", TIMEOUT);
176 fprintf(stderr, " -h <time> (hold marker on changes [x100ms] default: %d)\n", HOLD);
177 fprintf(stderr, " -l <time> (loop time (display) [x100ms] default: %d)\n", LOOP);
178 fprintf(stderr, "\n");
179 fprintf(stderr, "%s", manual);
182 void sigterm(int signo)
187 int main(int argc, char **argv)
195 unsigned char quiet = 0;
198 struct timeval timeo, start_tv, tv;
199 struct sockaddr_can addr;
204 signal(SIGTERM, sigterm);
205 signal(SIGHUP, sigterm);
206 signal(SIGINT, sigterm);
208 for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
211 while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBc")) != -1) {
214 sscanf(optarg, "%x", &mask);
218 sscanf(optarg, "%x", &value);
222 readsettings(optarg, 0); /* no BCM-setting here */
226 sscanf(optarg, "%ld", &timeout);
230 sscanf(optarg, "%ld", &hold);
234 sscanf(optarg, "%ld", &loop);
259 fprintf(stderr, "Unknown option %c\n", opt);
264 if (optind == argc) {
265 print_usage(basename(argv[0]));
270 for (i=0; i < 2048 ;i++) {
271 if ((i & mask) == (value & mask))
279 for (i=0; i < 2048 ;i++)
282 if (strlen(argv[optind]) >= IFNAMSIZ) {
283 printf("name of CAN device '%s' is too long!\n", argv[optind]);
287 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
292 addr.can_family = AF_CAN;
293 strcpy(ifr.ifr_name, argv[optind]);
294 ioctl(s, SIOCGIFINDEX, &ifr);
295 addr.can_ifindex = ifr.ifr_ifindex;
297 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
302 for (i=0; i < 2048 ;i++) /* initial BCM setup */
303 if (is_set(i, ENABLE))
306 gettimeofday(&start_tv, NULL);
307 tv.tv_sec = tv.tv_usec = 0;
309 printf("%s", CSR_HIDE); /* hide cursor */
318 timeo.tv_usec = 100000 * loop;
320 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
326 gettimeofday(&tv, NULL);
327 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
329 if (FD_ISSET(0, &rdfs))
330 running &= handle_keyb(s);
332 if (FD_ISSET(s, &rdfs))
333 running &= handle_bcm(s, currcms);
335 if (currcms - lastcms >= loop) {
336 running &= handle_timeo(s, currcms);
341 printf("%s", CSR_SHOW); /* show cursor */
347 void rx_setup (int fd, int id){
350 struct bcm_msg_head msg_head;
351 struct can_frame frame;
354 txmsg.msg_head.opcode = RX_SETUP;
355 txmsg.msg_head.can_id = id;
356 txmsg.msg_head.flags = RX_CHECK_DLC;
357 txmsg.msg_head.ival1.tv_sec = 0;
358 txmsg.msg_head.ival1.tv_usec = 0;
359 txmsg.msg_head.ival2.tv_sec = 0;
360 txmsg.msg_head.ival2.tv_usec = 0;
361 txmsg.msg_head.nframes = 1;
362 U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
364 if (write(fd, &txmsg, sizeof(txmsg)) < 0)
368 void rx_delete (int fd, int id){
370 struct bcm_msg_head msg_head;
372 msg_head.opcode = RX_DELETE;
373 msg_head.can_id = id;
374 msg_head.nframes = 0;
376 if (write(fd, &msg_head, sizeof(msg_head)) < 0)
380 int handle_keyb(int fd){
387 if (read(0, cmd, 19) > strlen("+123456\n"))
388 return 1; /* ignore */
391 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
397 sscanf(&cmd[1], "%x", &value);
398 if (strlen(&cmd[1]) > 3) {
399 mask = value & 0xFFF;
406 for (i=0; i < 2048 ;i++) {
407 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
414 for (i=0; i < 2048 ;i++) {
415 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
424 writesettings(&cmd[1]);
428 readsettings(&cmd[1], fd);
466 for (i=0; i < 2048; i++)
467 U64_DATA(&sniftab[i].notch) = (__u64) 0;
479 int handle_bcm(int fd, long currcms){
484 struct bcm_msg_head msg_head;
485 struct can_frame frame;
488 if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
493 id = bmsg.msg_head.can_id;
494 ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
496 if (bmsg.msg_head.opcode != RX_CHANGED) {
497 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
501 if (nbytes != sizeof(bmsg)) {
502 printf("received strange BCM data length %d!\n", nbytes);
506 sniftab[id].current = bmsg.frame;
507 U64_DATA(&sniftab[id].marker) |=
508 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
509 sniftab[id].timeout = currcms + timeout;
511 if (is_clr(id, DISPLAY))
512 clearscreen = 1; /* new entry -> new drawing */
520 int handle_timeo(int fd, long currcms){
523 int force_redraw = 0;
527 printf("%s%s", CLR_SCREEN, CSR_HOME);
528 snprintf(startline, 79, "< can-sniffer parameters: l=%ld h=%ld t=%ld >", loop, hold, timeout);
529 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
535 for (i=0; i < 2048; i++)
536 U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
540 printf("%s", CSR_HOME);
541 printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
543 for (i=0; i < 2048; i++) {
545 if is_set(i, ENABLE) {
547 if is_set(i, DISPLAY) {
549 if (is_set(i, UPDATE) || (force_redraw)){
551 sniftab[i].hold = currcms + hold;
555 if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
556 U64_DATA(&sniftab[i].marker) = (__u64) 0;
558 sniftab[i].hold = 0; /* disable update by hold */
561 printf("%s", CSR_DOWN); /* skip my line */
563 if (sniftab[i].timeout < currcms) {
566 clearscreen = 1; /* removed entry -> new drawing next time */
569 sniftab[i].last = sniftab[i].current;
570 sniftab[i].laststamp = sniftab[i].currstamp;
578 void print_snifline(int id){
580 long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
581 long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
582 int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
586 diffsec--, diffusec += 1000000;
589 diffsec = diffusec = 0;
592 diffsec = 9, diffusec = 999999;
594 printf("%ld.%06ld %3x ", diffsec, diffusec, id);
598 for (i=0; i<sniftab[id].current.can_dlc; i++) {
599 for (j=7; j>=0; j--) {
600 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
601 (!(sniftab[id].notch.data[i] & 1<<j)))
602 if (sniftab[id].current.data[i] & 1<<j)
603 printf("%s1%s", ATTCOLOR, ATTRESET);
605 printf("%s0%s", ATTCOLOR, ATTRESET);
607 if (sniftab[id].current.data[i] & 1<<j)
617 * when the can_dlc decreased (dlc_diff > 0),
618 * we need to blank the former data printout
620 for (i=0; i<dlc_diff; i++) {
628 for (i=0; i<sniftab[id].current.can_dlc; i++)
629 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
630 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
632 printf("%02X ", sniftab[id].current.data[i]);
634 if (sniftab[id].current.can_dlc < 8)
635 printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
637 for (i=0; i<sniftab[id].current.can_dlc; i++)
638 if ((sniftab[id].current.data[i] > 0x1F) &&
639 (sniftab[id].current.data[i] < 0x7F))
640 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
641 printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
643 putchar(sniftab[id].current.data[i]);
648 * when the can_dlc decreased (dlc_diff > 0),
649 * we need to blank the former data printout
651 for (i=0; i<dlc_diff; i++)
657 U64_DATA(&sniftab[id].marker) = (__u64) 0;
662 void writesettings(char* name){
665 char fname[30] = SETFNAME;
669 strncat(fname, name, 29 - strlen(fname));
670 fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
674 for (i=0; i < 2048 ;i++) {
675 sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
677 for (j=0; j<8 ; j++){
678 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
682 /* 7 + 16 + 1 = 24 bytes per entry */
687 printf("unable to write setting file '%s'!\n", fname);
690 void readsettings(char* name, int sockfd){
693 char fname[30] = SETFNAME;
697 strncat(fname, name, 29 - strlen(fname));
698 fd = open(fname, O_RDONLY);
702 printf("reading setting file '%s' ... ", fname);
704 for (i=0; i < 2048 ;i++) {
705 if (read(fd, &buf, 24) == 24) {
707 if (is_clr(i, ENABLE)) {
714 if (is_set(i, ENABLE)) {
717 rx_delete(sockfd, i);
719 for (j=7; j>=0 ; j--){
720 sniftab[i].notch.data[j] =
721 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
722 buf[2*j+7] = 0; /* cut off each time */
727 printf("was only able to read until index %d from setting file '%s'!\n",
738 printf("unable to read setting file '%s'!\n", fname);