]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpdump.c
candump: Enable HW timestamping before using it
[can-utils.git] / isotpdump.c
1 /*
2  * isotpdump.c - dump and explain ISO15765-2 protocol CAN frames
3  *
4  * Copyright (c) 2008 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
23  *
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.
26  *
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
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <libgen.h>
49 #include <time.h>
50
51 #include <net/if.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55
56 #include <linux/can.h>
57 #include <linux/can/raw.h>
58 #include "terminal.h"
59
60 #define NO_CAN_ID 0xFFFFFFFFU
61
62 const char fc_info [4][9] = { "CTS", "WT", "OVFLW", "reserved" };
63 const int canfd_on = 1;
64
65 void print_usage(char *prg)
66 {
67         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
68         fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
69         fprintf(stderr, "         -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
70         fprintf(stderr, "         -x <addr>   (extended addressing mode. Use 'any' for all addresses)\n");
71         fprintf(stderr, "         -X <addr>   (extended addressing mode (rx addr). Use 'any' for all)\n");
72         fprintf(stderr, "         -c          (color mode)\n");
73         fprintf(stderr, "         -a          (print data also in ASCII-chars)\n");
74         fprintf(stderr, "         -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
75         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
76         fprintf(stderr, "\n");
77 }
78
79 int main(int argc, char **argv)
80 {
81         int s;
82         struct sockaddr_can addr;
83         struct can_filter rfilter[2];
84         struct canfd_frame frame;
85         int nbytes, i;
86         canid_t src = NO_CAN_ID;
87         canid_t dst = NO_CAN_ID;
88         int ext = 0;
89         int extaddr = 0;
90         int extany = 0;
91         int rx_ext = 0;
92         int rx_extaddr = 0;
93         int rx_extany = 0;
94         int asc = 0;
95         int color = 0;
96         int timestamp = 0;
97         int datidx = 0;
98         unsigned long fflen = 0;
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:X: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                         break;
135
136                 case 'X':
137                         rx_ext = 1;
138                         if (!strncmp(optarg, "any", 3))
139                                 rx_extany = 1;
140                         else
141                                 rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
142                         break;
143
144                 case 't':
145                         timestamp = optarg[0];
146                         if ((timestamp != 'a') && (timestamp != 'A') &&
147                             (timestamp != 'd') && (timestamp != 'z')) {
148                                 printf("%s: unknown timestamp mode '%c' - ignored\n",
149                                        basename(argv[0]), optarg[0]);
150                                 timestamp = 0;
151                         }
152                         break;
153
154                 case '?':
155                         print_usage(basename(argv[0]));
156                         exit(0);
157                         break;
158
159                 default:
160                         fprintf(stderr, "Unknown option %c\n", opt);
161                         print_usage(basename(argv[0]));
162                         exit(1);
163                         break;
164                 }
165         }
166
167         if (rx_ext && !ext) {
168                 print_usage(basename(argv[0]));
169                 exit(0);
170         }
171
172         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
173                 print_usage(basename(argv[0]));
174                 exit(0);
175         }
176
177         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
178                 perror("socket");
179                 return 1;
180         }
181
182         /* try to switch the socket into CAN FD mode */
183         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
184
185         if (src & CAN_EFF_FLAG) {
186                 rfilter[0].can_id   = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
187                 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
188         } else {
189                 rfilter[0].can_id   = src & CAN_SFF_MASK;
190                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
191         }
192
193         if (dst & CAN_EFF_FLAG) {
194                 rfilter[1].can_id   = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
195                 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
196         } else {
197                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
198                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
199         }
200
201         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
202
203         addr.can_family = AF_CAN;
204         addr.can_ifindex = if_nametoindex(argv[optind]);
205
206         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
207                 perror("bind");
208                 return 1;
209         }
210
211         while (1) {
212                 nbytes = read(s, &frame, sizeof(frame));
213                 if (nbytes < 0) {
214                         perror("read");
215                         return 1;
216                 } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
217                         fprintf(stderr, "read: incomplete CAN frame %zu %d\n", sizeof(frame), nbytes);
218                         return 1;
219                 } else {
220
221                         if (frame.can_id == src && ext && !extany && extaddr != frame.data[0])
222                                 continue;
223
224                         if (frame.can_id == dst && rx_ext && !rx_extany && rx_extaddr != frame.data[0])
225                                 continue;
226
227                         if (color)
228                                 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
229
230                         if (timestamp) {
231                                 ioctl(s, SIOCGSTAMP, &tv);
232
233
234                                 switch (timestamp) {
235
236                                 case 'a': /* absolute with timestamp */
237                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
238                                         break;
239
240                                 case 'A': /* absolute with date */
241                                 {
242                                         struct tm tm;
243                                         char timestring[25];
244
245                                         tm = *localtime(&tv.tv_sec);
246                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
247                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
248                                 }
249                                 break;
250
251                                 case 'd': /* delta */
252                                 case 'z': /* starting with zero */
253                                 {
254                                         struct timeval diff;
255
256                                         if (last_tv.tv_sec == 0)   /* first init */
257                                                 last_tv = tv;
258                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
259                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
260                                         if (diff.tv_usec < 0)
261                                                 diff.tv_sec--, diff.tv_usec += 1000000;
262                                         if (diff.tv_sec < 0)
263                                                 diff.tv_sec = diff.tv_usec = 0;
264                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
265
266                                         if (timestamp == 'd')
267                                                 last_tv = tv; /* update for delta calculation */
268                                 }
269                                 break;
270
271                                 default: /* no timestamp output */
272                                         break;
273                                 }
274                         }
275
276                         if (frame.can_id & CAN_EFF_FLAG)
277                                 printf(" %s  %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
278                         else
279                                 printf(" %s  %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
280
281                         if (ext)
282                                 printf("{%02X}", frame.data[0]);
283
284                         if (nbytes == CAN_MTU)
285                                 printf("  [%d]  ", frame.len);
286                         else
287                                 printf(" [%02d]  ", frame.len);
288
289                         datidx = 0;
290                         n_pci = frame.data[ext];
291             
292                         switch (n_pci & 0xF0) {
293                         case 0x00:
294                                 if (n_pci & 0xF) {
295                                         printf("[SF] ln: %-4d data:", n_pci & 0xF);
296                                         datidx = ext+1;
297                                 } else {
298                                         printf("[SF] ln: %-4d data:", frame.data[ext + 1]);
299                                         datidx = ext+2;
300                                 }
301                                 break;
302
303                         case 0x10:
304                                 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
305                                 if (fflen)
306                                         datidx = ext+2;
307                                 else {
308                                         fflen = (frame.data[ext+2]<<24) +
309                                                 (frame.data[ext+3]<<16) +
310                                                 (frame.data[ext+4]<<8) +
311                                                 frame.data[ext+5];
312                                         datidx = ext+6;
313                                 }
314                                 printf("[FF] ln: %-4lu data:", fflen);
315                                 break;
316
317                         case 0x20:
318                                 printf("[CF] sn: %X    data:", n_pci & 0x0F);
319                                 datidx = ext+1;
320                                 break;
321
322                         case 0x30:
323                                 n_pci &= 0x0F;
324                                 printf("[FC] FC: %d ", n_pci);
325
326                                 if (n_pci > 3)
327                                         n_pci = 3;
328
329                                 printf("= %s # ", fc_info[n_pci]);
330
331                                 printf("BS: %d %s# ", frame.data[ext+1],
332                                        (frame.data[ext+1])? "":"= off ");
333
334                                 i = frame.data[ext+2];
335                                 printf("STmin: 0x%02X = ", i);
336
337                                 if (i < 0x80)
338                                         printf("%d ms", i);
339                                 else if (i > 0xF0 && i < 0xFA)
340                                         printf("%d us", (i & 0x0F) * 100);
341                                 else
342                                         printf("reserved");
343                                 break;
344
345                         default:
346                                 printf("[??]");
347                         }
348
349                         if (datidx && frame.len > datidx) {
350                                 printf(" ");
351                                 for (i = datidx; i < frame.len; i++) {
352                                         printf("%02X ", frame.data[i]);
353                                 }
354
355                                 if (asc) {
356                                         printf("%*s", ((7-ext) - (frame.len-datidx))*3 + 5 ,
357                                                "-  '");
358                                         for (i = datidx; i < frame.len; i++) {
359                                                 printf("%c",((frame.data[i] > 0x1F) &&
360                                                              (frame.data[i] < 0x7F))?
361                                                        frame.data[i] : '.');
362                                         }
363                                         printf("'");
364                                 }
365                         }
366
367                         if (color)
368                                 printf("%s", ATTRESET);
369                         printf("\n");
370                         fflush(stdout);
371                 }
372         }
373
374         close(s);
375
376         return 0;
377 }