6 * isotpdump.c - dump and explain ISO15765-2 protocol CAN frames
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/raw.h>
64 #define NO_CAN_ID 0xFFFFFFFFU
66 const char fc_info [4][9] = { "CTS", "WT", "OVFLW", "reserved" };
68 void print_usage(char *prg)
70 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
71 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
72 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
73 fprintf(stderr, " -x <addr> (extended addressing mode. Use 'any' for all addresses)\n");
74 fprintf(stderr, " -c (color mode)\n");
75 fprintf(stderr, " -a (print data also in ASCII-chars)\n");
76 fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
77 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
78 fprintf(stderr, "\n");
81 int main(int argc, char **argv)
84 struct sockaddr_can addr;
85 struct can_filter rfilter[2];
86 struct can_frame frame;
88 canid_t src = NO_CAN_ID;
89 canid_t dst = NO_CAN_ID;
99 struct timeval tv, last_tv;
106 while ((opt = getopt(argc, argv, "s:d:ax:ct:?")) != -1) {
109 src = strtoul(optarg, (char **)NULL, 16);
110 if (strlen(optarg) > 7)
115 dst = strtoul(optarg, (char **)NULL, 16);
116 if (strlen(optarg) > 7)
130 if (!strncmp(optarg, "any", 3))
133 extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
138 timestamp = optarg[0];
139 if ((timestamp != 'a') && (timestamp != 'A') &&
140 (timestamp != 'd') && (timestamp != 'z')) {
141 printf("%s: unknown timestamp mode '%c' - ignored\n",
142 basename(argv[0]), optarg[0]);
148 print_usage(basename(argv[0]));
153 fprintf(stderr, "Unknown option %c\n", opt);
154 print_usage(basename(argv[0]));
160 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
161 print_usage(basename(argv[0]));
165 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
171 if (src & CAN_EFF_FLAG) {
172 rfilter[0].can_id = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
173 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
175 rfilter[0].can_id = src & CAN_SFF_MASK;
176 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
179 if (dst & CAN_EFF_FLAG) {
180 rfilter[1].can_id = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
181 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
183 rfilter[1].can_id = dst & CAN_SFF_MASK;
184 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
187 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
189 strcpy(ifr.ifr_name, argv[optind]);
190 ioctl(s, SIOCGIFINDEX, &ifr);
191 ifindex = ifr.ifr_ifindex;
193 addr.can_family = AF_CAN;
194 addr.can_ifindex = ifindex;
196 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
203 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
206 } else if (nbytes < sizeof(struct can_frame)) {
207 fprintf(stderr, "read: incomplete CAN frame\n");
211 if (ext && !extany && extaddr != frame.data[0])
215 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
218 ioctl(s, SIOCGSTAMP, &tv);
223 case 'a': /* absolute with timestamp */
224 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
227 case 'A': /* absolute with date */
232 tm = *localtime(&tv.tv_sec);
233 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
234 printf("(%s.%06ld) ", timestring, tv.tv_usec);
238 case 'd': /* delta */
239 case 'z': /* starting with zero */
243 if (last_tv.tv_sec == 0) /* first init */
245 diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
246 diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
247 if (diff.tv_usec < 0)
248 diff.tv_sec--, diff.tv_usec += 1000000;
250 diff.tv_sec = diff.tv_usec = 0;
251 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
253 if (timestamp == 'd')
254 last_tv = tv; /* update for delta calculation */
258 default: /* no timestamp output */
263 if (frame.can_id & CAN_EFF_FLAG)
264 printf(" %s %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
266 printf(" %s %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
269 printf("{%02X}", frame.data[0]);
271 printf(" [%d] ", frame.can_dlc);
274 n_pci = frame.data[ext];
276 switch (n_pci & 0xF0) {
278 printf("[SF] ln: %-4d data:", n_pci & 0x0F);
283 printf("[FF] ln: %-4d data:",
284 ((n_pci & 0x0F)<<8) + frame.data[ext+1] );
289 printf("[CF] sn: %X data:", n_pci & 0x0F);
295 printf("[FC] FC: %d ", n_pci);
300 printf("= %s # ", fc_info[n_pci]);
302 printf("BS: %d %s# ", frame.data[ext+1],
303 (frame.data[ext+1])? "":"= off ");
305 i = frame.data[ext+2];
306 printf("STmin: 0x%02X = ", i);
310 else if (i > 0xF0 && i < 0xFA)
311 printf("%d us", (i & 0x0F) * 100);
320 if (datidx && frame.can_dlc > datidx) {
322 for (i = datidx; i < frame.can_dlc; i++) {
323 printf("%02X ", frame.data[i]);
327 printf("%*s", ((7-ext) - (frame.can_dlc-datidx))*3 + 5 ,
329 for (i = datidx; i < frame.can_dlc; i++) {
330 printf("%c",((frame.data[i] > 0x1F) &&
331 (frame.data[i] < 0x7F))?
332 frame.data[i] : '.');
339 printf("%s", ATTRESET);