2 * isotpsniffer.c - dump ISO15765-2 datagrams using PF_CAN isotp protocol
4 * Copyright (c) 2008 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
56 #include <linux/can.h>
57 #include <linux/can/isotp.h>
60 #define NO_CAN_ID 0xFFFFFFFFU
63 #define FORMAT_ASCII 2
64 #define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
66 void print_usage(char *prg)
68 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
69 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
70 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
71 fprintf(stderr, " -x <addr> (extended addressing mode)\n");
72 fprintf(stderr, " -X <addr> (extended addressing mode - rx addr)\n");
73 fprintf(stderr, " -c (color mode)\n");
74 fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
75 fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
76 fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
77 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
78 fprintf(stderr, "\n");
81 void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
82 int format, struct timeval *tv, struct timeval *last_tv,
83 canid_t src, int socket, char *candevice, int head)
94 ioctl(socket, SIOCGSTAMP, tv);
98 case 'a': /* absolute with timestamp */
99 printf("(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
102 case 'A': /* absolute with date */
107 tm = *localtime(&tv->tv_sec);
108 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
109 printf("(%s.%06ld) ", timestring, tv->tv_usec);
113 case 'd': /* delta */
114 case 'z': /* starting with zero */
118 if (last_tv->tv_sec == 0) /* first init */
120 diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
121 diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
122 if (diff.tv_usec < 0)
123 diff.tv_sec--, diff.tv_usec += 1000000;
125 diff.tv_sec = diff.tv_usec = 0;
126 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
128 if (timestamp == 'd')
129 *last_tv = *tv; /* update for delta calculation */
133 default: /* no timestamp output */
138 /* the source socket gets pdu data from the detination id */
139 printf(" %s %03X [%d] ", candevice, src & CAN_EFF_MASK, nbytes);
140 if (format & FORMAT_HEX) {
141 for (i=0; i<nbytes; i++) {
142 printf("%02X ", buffer[i]);
143 if (head && i+1 >= head) {
148 if (format & FORMAT_ASCII)
151 if (format & FORMAT_ASCII) {
153 for (i=0; i<nbytes; i++) {
154 if ((buffer[i] > 0x1F) && (buffer[i] < 0x7F))
155 printf("%c", buffer[i]);
158 if (head && i+1 >= head)
162 if (head && i+1 >= head)
167 printf("%s", ATTRESET);
173 int main(int argc, char **argv)
177 struct sockaddr_can addr;
179 static struct can_isotp_options opts;
184 int format = FORMAT_DEFAULT;
185 canid_t src = NO_CAN_ID;
186 canid_t dst = NO_CAN_ID;
187 extern int optind, opterr, optopt;
188 static struct timeval tv, last_tv;
190 unsigned char buffer[4096];
193 while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:?")) != -1) {
196 src = strtoul(optarg, (char **)NULL, 16);
197 if (strlen(optarg) > 7)
202 dst = strtoul(optarg, (char **)NULL, 16);
203 if (strlen(optarg) > 7)
208 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
209 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
213 opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
214 opts.rx_ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
218 format = (atoi(optarg) & (FORMAT_ASCII | FORMAT_HEX));
230 timestamp = optarg[0];
231 if ((timestamp != 'a') && (timestamp != 'A') &&
232 (timestamp != 'd') && (timestamp != 'z')) {
233 printf("%s: unknown timestamp mode '%c' - ignored\n",
234 basename(argv[0]), optarg[0]);
240 print_usage(basename(argv[0]));
245 fprintf(stderr, "Unknown option %c\n", opt);
246 print_usage(basename(argv[0]));
252 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
253 print_usage(basename(argv[0]));
257 if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
258 print_usage(basename(argv[0]));
262 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
267 if ((t = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
272 opts.flags |= CAN_ISOTP_LISTEN_MODE;
274 addr.can_family = AF_CAN;
275 strcpy(ifr.ifr_name, argv[optind]);
276 ioctl(s, SIOCGIFINDEX, &ifr);
277 addr.can_ifindex = ifr.ifr_ifindex;
279 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
281 addr.can_addr.tp.tx_id = src;
282 addr.can_addr.tp.rx_id = dst;
284 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
290 if (opts.flags & CAN_ISOTP_RX_EXT_ADDR) {
291 /* flip extended address info due to separate rx ext addr */
294 tmpext = opts.ext_address;
295 opts.ext_address = opts.rx_ext_address;
296 opts.rx_ext_address = tmpext;
299 setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
301 addr.can_addr.tp.tx_id = dst;
302 addr.can_addr.tp.rx_id = src;
304 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
317 if ((nbytes = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
322 if (FD_ISSET(0, &rdfs)) {
325 printf("quit due to keyboard input.\n");
328 if (FD_ISSET(s, &rdfs)) {
329 nbytes = read(s, buffer, 4096);
331 perror("read socket s");
336 printbuf(buffer, nbytes, color?2:0, timestamp, format,
337 &tv, &last_tv, dst, s, ifr.ifr_name, head);
340 if (FD_ISSET(t, &rdfs)) {
341 nbytes = read(t, buffer, 4096);
343 perror("read socket t");
348 printbuf(buffer, nbytes, color?1:0, timestamp, format,
349 &tv, &last_tv, src, t, ifr.ifr_name, head);