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, " -c (color mode)\n");
73 fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
74 fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
75 fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
76 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
77 fprintf(stderr, "\n");
80 void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
81 int format, struct timeval *tv, struct timeval *last_tv,
82 canid_t src, int socket, char *candevice, int head)
93 ioctl(socket, SIOCGSTAMP, tv);
97 case 'a': /* absolute with timestamp */
98 printf("(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
101 case 'A': /* absolute with date */
106 tm = *localtime(&tv->tv_sec);
107 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
108 printf("(%s.%06ld) ", timestring, tv->tv_usec);
112 case 'd': /* delta */
113 case 'z': /* starting with zero */
117 if (last_tv->tv_sec == 0) /* first init */
119 diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
120 diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
121 if (diff.tv_usec < 0)
122 diff.tv_sec--, diff.tv_usec += 1000000;
124 diff.tv_sec = diff.tv_usec = 0;
125 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
127 if (timestamp == 'd')
128 *last_tv = *tv; /* update for delta calculation */
132 default: /* no timestamp output */
137 /* the source socket gets pdu data from the detination id */
138 printf(" %s %03X [%d] ", candevice, src & CAN_EFF_MASK, nbytes);
139 if (format & FORMAT_HEX) {
140 for (i=0; i<nbytes; i++) {
141 printf("%02X ", buffer[i]);
142 if (head && i+1 >= head) {
147 if (format & FORMAT_ASCII)
150 if (format & FORMAT_ASCII) {
152 for (i=0; i<nbytes; i++) {
153 if ((buffer[i] > 0x1F) && (buffer[i] < 0x7F))
154 printf("%c", buffer[i]);
157 if (head && i+1 >= head)
161 if (head && i+1 >= head)
166 printf("%s", ATTRESET);
172 int main(int argc, char **argv)
176 struct sockaddr_can addr;
178 static struct can_isotp_options opts;
183 int format = FORMAT_DEFAULT;
184 canid_t src = NO_CAN_ID;
185 canid_t dst = NO_CAN_ID;
186 extern int optind, opterr, optopt;
187 static struct timeval tv, last_tv;
189 unsigned char buffer[4096];
192 while ((opt = getopt(argc, argv, "s:d:x:h:ct:f:?")) != -1) {
195 src = strtoul(optarg, (char **)NULL, 16);
196 if (strlen(optarg) > 7)
201 dst = strtoul(optarg, (char **)NULL, 16);
202 if (strlen(optarg) > 7)
207 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
208 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
212 format = (atoi(optarg) & (FORMAT_ASCII | FORMAT_HEX));
224 timestamp = optarg[0];
225 if ((timestamp != 'a') && (timestamp != 'A') &&
226 (timestamp != 'd') && (timestamp != 'z')) {
227 printf("%s: unknown timestamp mode '%c' - ignored\n",
228 basename(argv[0]), optarg[0]);
234 print_usage(basename(argv[0]));
239 fprintf(stderr, "Unknown option %c\n", opt);
240 print_usage(basename(argv[0]));
246 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
247 print_usage(basename(argv[0]));
251 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
256 if ((t = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
261 opts.flags |= CAN_ISOTP_LISTEN_MODE;
263 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
264 setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
266 addr.can_family = AF_CAN;
267 strcpy(ifr.ifr_name, argv[optind]);
268 ioctl(s, SIOCGIFINDEX, &ifr);
269 addr.can_ifindex = ifr.ifr_ifindex;
271 addr.can_addr.tp.tx_id = dst;
272 addr.can_addr.tp.rx_id = src;
274 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
280 addr.can_addr.tp.tx_id = src;
281 addr.can_addr.tp.rx_id = dst;
283 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
296 if ((nbytes = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
301 if (FD_ISSET(0, &rdfs)) {
304 printf("quit due to keyboard input.\n");
307 if (FD_ISSET(s, &rdfs)) {
308 nbytes = read(s, buffer, 4096);
310 perror("read socket s");
315 printbuf(buffer, nbytes, color?1:0, timestamp, format,
316 &tv, &last_tv, src, s, ifr.ifr_name, head);
319 if (FD_ISSET(t, &rdfs)) {
320 nbytes = read(t, buffer, 4096);
322 perror("read socket t");
327 printbuf(buffer, nbytes, color?2:0, timestamp, format,
328 &tv, &last_tv, dst, t, ifr.ifr_name, head);