6 * isotpsniffer.c - dump ISO15765-2 datagrams using PF_CAN isotp protocol
8 * Copyright (c) 2008 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>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
60 #include <linux/can.h>
61 #include <linux/can/isotp.h>
64 #define NO_CAN_ID 0xFFFFFFFFU
67 #define FORMAT_ASCII 2
68 #define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
70 void print_usage(char *prg)
72 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
73 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
74 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
75 fprintf(stderr, " -x <addr> (extended addressing mode.)\n");
76 fprintf(stderr, " -c (color mode)\n");
77 fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
78 fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
79 fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
80 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
81 fprintf(stderr, "\n");
84 void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
85 int format, struct timeval *tv, struct timeval *last_tv,
86 canid_t src, int socket, char *candevice, int head)
97 ioctl(socket, SIOCGSTAMP, tv);
101 case 'a': /* absolute with timestamp */
102 printf("(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
105 case 'A': /* absolute with date */
110 tm = *localtime(&tv->tv_sec);
111 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
112 printf("(%s.%06ld) ", timestring, tv->tv_usec);
116 case 'd': /* delta */
117 case 'z': /* starting with zero */
121 if (last_tv->tv_sec == 0) /* first init */
123 diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
124 diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
125 if (diff.tv_usec < 0)
126 diff.tv_sec--, diff.tv_usec += 1000000;
128 diff.tv_sec = diff.tv_usec = 0;
129 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
131 if (timestamp == 'd')
132 *last_tv = *tv; /* update for delta calculation */
136 default: /* no timestamp output */
141 /* the source socket gets pdu data from the detination id */
142 printf(" %s %03X [%d] ", candevice, src & CAN_EFF_MASK, nbytes);
143 if (format & FORMAT_HEX) {
144 for (i=0; i<nbytes; i++) {
145 printf("%02X ", buffer[i]);
146 if (head && i+1 >= head) {
151 if (format & FORMAT_ASCII)
154 if (format & FORMAT_ASCII) {
156 for (i=0; i<nbytes; i++) {
157 if ((buffer[i] > 0x1F) && (buffer[i] < 0x7F))
158 printf("%c", buffer[i]);
161 if (head && i+1 >= head)
165 if (head && i+1 >= head)
170 printf("%s", ATTRESET);
176 int main(int argc, char **argv)
180 struct sockaddr_can addr;
182 static struct can_isotp_options opts;
187 int format = FORMAT_DEFAULT;
188 canid_t src = NO_CAN_ID;
189 canid_t dst = NO_CAN_ID;
190 extern int optind, opterr, optopt;
191 static struct timeval tv, last_tv;
193 unsigned char buffer[4096];
196 while ((opt = getopt(argc, argv, "s:d:x:h:ct:f:?")) != -1) {
199 src = strtoul(optarg, (char **)NULL, 16);
200 if (strlen(optarg) > 7)
205 dst = strtoul(optarg, (char **)NULL, 16);
206 if (strlen(optarg) > 7)
211 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
212 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
216 format = (atoi(optarg) & (FORMAT_ASCII | FORMAT_HEX));
228 timestamp = optarg[0];
229 if ((timestamp != 'a') && (timestamp != 'A') &&
230 (timestamp != 'd') && (timestamp != 'z')) {
231 printf("%s: unknown timestamp mode '%c' - ignored\n",
232 basename(argv[0]), optarg[0]);
238 print_usage(basename(argv[0]));
243 fprintf(stderr, "Unknown option %c\n", opt);
244 print_usage(basename(argv[0]));
250 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
251 print_usage(basename(argv[0]));
255 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
260 if ((t = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
265 opts.flags |= CAN_ISOTP_LISTEN_MODE;
267 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
268 setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
270 addr.can_family = AF_CAN;
271 strcpy(ifr.ifr_name, argv[optind]);
272 ioctl(s, SIOCGIFINDEX, &ifr);
273 addr.can_ifindex = ifr.ifr_ifindex;
275 addr.can_addr.tp.tx_id = dst;
276 addr.can_addr.tp.rx_id = src;
278 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
284 addr.can_addr.tp.tx_id = src;
285 addr.can_addr.tp.rx_id = dst;
287 if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
300 if ((nbytes = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
305 if (FD_ISSET(0, &rdfs)) {
308 printf("quit due to keyboard input.\n");
311 if (FD_ISSET(s, &rdfs)) {
312 nbytes = read(s, buffer, 4096);
314 perror("read socket s");
319 printbuf(buffer, nbytes, color?1:0, timestamp, format,
320 &tv, &last_tv, src, s, ifr.ifr_name, head);
323 if (FD_ISSET(t, &rdfs)) {
324 nbytes = read(t, buffer, 4096);
326 perror("read socket t");
331 printbuf(buffer, nbytes, color?2:0, timestamp, format,
332 &tv, &last_tv, dst, t, ifr.ifr_name, head);