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 fprintf(stderr, "Unknown option %c\n", opt);
149 print_usage(basename(argv[0]));
155 if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
156 print_usage(basename(argv[0]));
160 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
166 if (src & CAN_EFF_FLAG) {
167 rfilter[0].can_id = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
168 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
170 rfilter[0].can_id = src & CAN_SFF_MASK;
171 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
174 if (dst & CAN_EFF_FLAG) {
175 rfilter[1].can_id = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
176 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
178 rfilter[1].can_id = dst & CAN_SFF_MASK;
179 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
182 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
184 strcpy(ifr.ifr_name, argv[optind]);
185 ioctl(s, SIOCGIFINDEX, &ifr);
186 ifindex = ifr.ifr_ifindex;
188 addr.can_family = AF_CAN;
189 addr.can_ifindex = ifindex;
191 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
198 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
201 } else if (nbytes < sizeof(struct can_frame)) {
202 fprintf(stderr, "read: incomplete CAN frame\n");
206 if (ext && !extany && extaddr != frame.data[0])
210 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
213 ioctl(s, SIOCGSTAMP, &tv);
218 case 'a': /* absolute with timestamp */
219 printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
222 case 'A': /* absolute with date */
227 tm = *localtime(&tv.tv_sec);
228 strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
229 printf("(%s.%06ld) ", timestring, tv.tv_usec);
233 case 'd': /* delta */
234 case 'z': /* starting with zero */
238 if (last_tv.tv_sec == 0) /* first init */
240 diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
241 diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
242 if (diff.tv_usec < 0)
243 diff.tv_sec--, diff.tv_usec += 1000000;
245 diff.tv_sec = diff.tv_usec = 0;
246 printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
248 if (timestamp == 'd')
249 last_tv = tv; /* update for delta calculation */
253 default: /* no timestamp output */
258 if (frame.can_id & CAN_EFF_FLAG)
259 printf(" %s %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
261 printf(" %s %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
264 printf("{%02X}", frame.data[0]);
266 printf(" [%d] ", frame.can_dlc);
269 n_pci = frame.data[ext];
271 switch (n_pci & 0xF0) {
273 printf("[SF] ln: %-4d data:", n_pci & 0x0F);
278 printf("[FF] ln: %-4d data:",
279 ((n_pci & 0x0F)<<8) + frame.data[ext+1] );
284 printf("[CF] sn: %X data:", n_pci & 0x0F);
290 printf("[FC] FC: %d ", n_pci);
295 printf("= %s # ", fc_info[n_pci]);
297 printf("BS: %d %s# ", frame.data[ext+1],
298 (frame.data[ext+1])? "":"= off ");
300 i = frame.data[ext+2];
301 printf("STmin: 0x%02X = ", i);
305 else if (i > 0xF0 && i < 0xFA)
306 printf("%d us", (i & 0x0F) * 100);
315 if (datidx && frame.can_dlc > datidx) {
317 for (i = datidx; i < frame.can_dlc; i++) {
318 printf("%02X ", frame.data[i]);
322 printf("%*s", ((7-ext) - (frame.can_dlc-datidx))*3 + 5 ,
324 for (i = datidx; i < frame.can_dlc; i++) {
325 printf("%c",((frame.data[i] > 0x1F) &&
326 (frame.data[i] < 0x7F))?
327 frame.data[i] : '.');
334 printf("%s", ATTRESET);