]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/isotpdump.c
Fix sloppy CAN_(EFF|RTR)_FLAG handling in can_filter.can_mask .
[socketcan-devel.git] / can-utils / isotpdump.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * isotpdump.c - dump and explain ISO15765-2 protocol CAN frames
7  *
8  * Copyright (c) 2008 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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.
27  *
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.
30  *
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
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <libgen.h>
53 #include <time.h>
54
55 #include <net/if.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59
60 #include <linux/can.h>
61 #include <linux/can/raw.h>
62 #include "terminal.h"
63
64 #define NO_CAN_ID 0xFFFFFFFFU
65
66 const char fc_info [4][9] = { "CTS", "WT", "OVFLW", "reserved" };
67
68 void print_usage(char *prg)
69 {
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");
79 }
80
81 int main(int argc, char **argv)
82 {
83         int s;
84         struct sockaddr_can addr;
85         struct can_filter rfilter[2];
86         struct can_frame frame;
87         int nbytes, i;
88         canid_t src = NO_CAN_ID;
89         canid_t dst = NO_CAN_ID;
90         int ext = 0;
91         int extaddr = 0;
92         int extany = 0;
93         int asc = 0;
94         int color = 0;
95         int timestamp = 0;
96         int datidx = 0;
97         struct ifreq ifr;
98         int ifindex;
99         struct timeval tv, last_tv;
100         unsigned int n_pci;
101         int opt;
102
103         last_tv.tv_sec  = 0;
104         last_tv.tv_usec = 0;
105
106         while ((opt = getopt(argc, argv, "s:d:ax:ct:")) != -1) {
107                 switch (opt) {
108                 case 's':
109                         src = strtoul(optarg, (char **)NULL, 16);
110                         if (strlen(optarg) > 7)
111                                 src |= CAN_EFF_FLAG;
112                         break;
113
114                 case 'd':
115                         dst = strtoul(optarg, (char **)NULL, 16);
116                         if (strlen(optarg) > 7)
117                                 dst |= CAN_EFF_FLAG;
118                         break;
119
120                 case 'c':
121                         color = 1;
122                         break;
123
124                 case 'a':
125                         asc = 1;
126                         break;
127
128                 case 'x':
129                         ext = 1;
130                         if (!strncmp(optarg, "any", 3))
131                                 extany = 1;
132                         else
133                                 extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
134
135                         break;
136
137                 case 't':
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]);
143                                 timestamp = 0;
144                         }
145                         break;
146
147                 default:
148                         fprintf(stderr, "Unknown option %c\n", opt);
149                         print_usage(basename(argv[0]));
150                         exit(0);
151                         break;
152                 }
153         }
154
155         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
156                 print_usage(basename(argv[0]));
157                 exit(0);
158         }
159
160         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
161                 perror("socket");
162                 return 1;
163         }
164
165
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);
169         } else {
170                 rfilter[0].can_id   = src & CAN_SFF_MASK;
171                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
172         }
173
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);
177         } else {
178                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
179                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
180         }
181
182         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
183
184         strcpy(ifr.ifr_name, argv[optind]);
185         ioctl(s, SIOCGIFINDEX, &ifr);
186         ifindex = ifr.ifr_ifindex;
187
188         addr.can_family = AF_CAN;
189         addr.can_ifindex = ifindex;
190
191         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
192                 perror("bind");
193                 return 1;
194         }
195
196         while (1) {
197
198                 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
199                         perror("read");
200                         return 1;
201                 } else if (nbytes < sizeof(struct can_frame)) {
202                         fprintf(stderr, "read: incomplete CAN frame\n");
203                         return 1;
204                 } else {
205
206                         if (ext && !extany && extaddr != frame.data[0])
207                                 continue;
208
209                         if (color)
210                                 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
211
212                         if (timestamp) {
213                                 ioctl(s, SIOCGSTAMP, &tv);
214
215
216                                 switch (timestamp) {
217
218                                 case 'a': /* absolute with timestamp */
219                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
220                                         break;
221
222                                 case 'A': /* absolute with date */
223                                 {
224                                         struct tm tm;
225                                         char timestring[25];
226
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);
230                                 }
231                                 break;
232
233                                 case 'd': /* delta */
234                                 case 'z': /* starting with zero */
235                                 {
236                                         struct timeval diff;
237
238                                         if (last_tv.tv_sec == 0)   /* first init */
239                                                 last_tv = tv;
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;
244                                         if (diff.tv_sec < 0)
245                                                 diff.tv_sec = diff.tv_usec = 0;
246                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
247
248                                         if (timestamp == 'd')
249                                                 last_tv = tv; /* update for delta calculation */
250                                 }
251                                 break;
252
253                                 default: /* no timestamp output */
254                                         break;
255                                 }
256                         }
257
258                         if (frame.can_id & CAN_EFF_FLAG)
259                                 printf(" %s  %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
260                         else
261                                 printf(" %s  %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
262
263                         if (ext)
264                                 printf("{%02X}", frame.data[0]);
265
266                         printf("  [%d]  ", frame.can_dlc);
267
268                         datidx = 0;
269                         n_pci = frame.data[ext];
270             
271                         switch (n_pci & 0xF0) {
272                         case 0x00:
273                                 printf("[SF] ln: %-4d data:", n_pci & 0x0F);
274                                 datidx = ext+1;
275                                 break;
276
277                         case 0x10:
278                                 printf("[FF] ln: %-4d data:",
279                                        ((n_pci & 0x0F)<<8) + frame.data[ext+1] );
280                                 datidx = ext+2;
281                                 break;
282
283                         case 0x20:
284                                 printf("[CF] sn: %X    data:", n_pci & 0x0F);
285                                 datidx = ext+1;
286                                 break;
287
288                         case 0x30:
289                                 n_pci &= 0x0F;
290                                 printf("[FC] FC: %d ", n_pci);
291
292                                 if (n_pci > 3)
293                                         n_pci = 3;
294
295                                 printf("= %s # ", fc_info[n_pci]);
296
297                                 printf("BS: %d %s# ", frame.data[ext+1],
298                                        (frame.data[ext+1])? "":"= off ");
299
300                                 i = frame.data[ext+2];
301                                 printf("STmin: 0x%02X = ", i);
302
303                                 if (i < 0x80)
304                                         printf("%d ms", i);
305                                 else if (i > 0xF0 && i < 0xFA)
306                                         printf("%d us", (i & 0x0F) * 100);
307                                 else
308                                         printf("reserved");
309                                 break;
310
311                         default:
312                                 printf("[??]");
313                         }
314
315                         if (datidx && frame.can_dlc > datidx) {
316                                 printf(" ");
317                                 for (i = datidx; i < frame.can_dlc; i++) {
318                                         printf("%02X ", frame.data[i]);
319                                 }
320
321                                 if (asc) {
322                                         printf("%*s", ((7-ext) - (frame.can_dlc-datidx))*3 + 5 ,
323                                                "-  '");
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] : '.');
328                                         }
329                                         printf("'");
330                                 }
331                         }
332
333                         if (color)
334                                 printf("%s", ATTRESET);
335                         printf("\n");
336                         fflush(stdout);
337                 }
338         }
339
340         close(s);
341
342         return 0;
343 }