]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpdump.c
isotp[server|tun]: update padding and CAN FD features
[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 ifreq ifr;
100         int ifindex;
101         struct timeval tv, last_tv;
102         unsigned int n_pci;
103         int opt;
104
105         last_tv.tv_sec  = 0;
106         last_tv.tv_usec = 0;
107
108         while ((opt = getopt(argc, argv, "s:d:ax:X:ct:?")) != -1) {
109                 switch (opt) {
110                 case 's':
111                         src = strtoul(optarg, (char **)NULL, 16);
112                         if (strlen(optarg) > 7)
113                                 src |= CAN_EFF_FLAG;
114                         break;
115
116                 case 'd':
117                         dst = strtoul(optarg, (char **)NULL, 16);
118                         if (strlen(optarg) > 7)
119                                 dst |= CAN_EFF_FLAG;
120                         break;
121
122                 case 'c':
123                         color = 1;
124                         break;
125
126                 case 'a':
127                         asc = 1;
128                         break;
129
130                 case 'x':
131                         ext = 1;
132                         if (!strncmp(optarg, "any", 3))
133                                 extany = 1;
134                         else
135                                 extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
136                         break;
137
138                 case 'X':
139                         rx_ext = 1;
140                         if (!strncmp(optarg, "any", 3))
141                                 rx_extany = 1;
142                         else
143                                 rx_extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
144                         break;
145
146                 case 't':
147                         timestamp = optarg[0];
148                         if ((timestamp != 'a') && (timestamp != 'A') &&
149                             (timestamp != 'd') && (timestamp != 'z')) {
150                                 printf("%s: unknown timestamp mode '%c' - ignored\n",
151                                        basename(argv[0]), optarg[0]);
152                                 timestamp = 0;
153                         }
154                         break;
155
156                 case '?':
157                         print_usage(basename(argv[0]));
158                         exit(0);
159                         break;
160
161                 default:
162                         fprintf(stderr, "Unknown option %c\n", opt);
163                         print_usage(basename(argv[0]));
164                         exit(1);
165                         break;
166                 }
167         }
168
169         if (rx_ext && !ext) {
170                 print_usage(basename(argv[0]));
171                 exit(0);
172         }
173
174         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
175                 print_usage(basename(argv[0]));
176                 exit(0);
177         }
178
179         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
180                 perror("socket");
181                 return 1;
182         }
183
184         /* try to switch the socket into CAN FD mode */
185         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
186
187         if (src & CAN_EFF_FLAG) {
188                 rfilter[0].can_id   = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
189                 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
190         } else {
191                 rfilter[0].can_id   = src & CAN_SFF_MASK;
192                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
193         }
194
195         if (dst & CAN_EFF_FLAG) {
196                 rfilter[1].can_id   = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
197                 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
198         } else {
199                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
200                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
201         }
202
203         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
204
205         strcpy(ifr.ifr_name, argv[optind]);
206         ioctl(s, SIOCGIFINDEX, &ifr);
207         ifindex = ifr.ifr_ifindex;
208
209         addr.can_family = AF_CAN;
210         addr.can_ifindex = ifindex;
211
212         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
213                 perror("bind");
214                 return 1;
215         }
216
217         while (1) {
218                 nbytes = read(s, &frame, sizeof(frame));
219                 if (nbytes < 0) {
220                         perror("read");
221                         return 1;
222                 } else if (nbytes != CAN_MTU && nbytes != CANFD_MTU) {
223                         fprintf(stderr, "read: incomplete CAN frame %lu %d\n", sizeof(frame), nbytes);
224                         return 1;
225                 } else {
226
227                         if (frame.can_id == src && ext && !extany && extaddr != frame.data[0])
228                                 continue;
229
230                         if (frame.can_id == dst && rx_ext && !rx_extany && rx_extaddr != frame.data[0])
231                                 continue;
232
233                         if (color)
234                                 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
235
236                         if (timestamp) {
237                                 ioctl(s, SIOCGSTAMP, &tv);
238
239
240                                 switch (timestamp) {
241
242                                 case 'a': /* absolute with timestamp */
243                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
244                                         break;
245
246                                 case 'A': /* absolute with date */
247                                 {
248                                         struct tm tm;
249                                         char timestring[25];
250
251                                         tm = *localtime(&tv.tv_sec);
252                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
253                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
254                                 }
255                                 break;
256
257                                 case 'd': /* delta */
258                                 case 'z': /* starting with zero */
259                                 {
260                                         struct timeval diff;
261
262                                         if (last_tv.tv_sec == 0)   /* first init */
263                                                 last_tv = tv;
264                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
265                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
266                                         if (diff.tv_usec < 0)
267                                                 diff.tv_sec--, diff.tv_usec += 1000000;
268                                         if (diff.tv_sec < 0)
269                                                 diff.tv_sec = diff.tv_usec = 0;
270                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
271
272                                         if (timestamp == 'd')
273                                                 last_tv = tv; /* update for delta calculation */
274                                 }
275                                 break;
276
277                                 default: /* no timestamp output */
278                                         break;
279                                 }
280                         }
281
282                         if (frame.can_id & CAN_EFF_FLAG)
283                                 printf(" %s  %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
284                         else
285                                 printf(" %s  %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
286
287                         if (ext)
288                                 printf("{%02X}", frame.data[0]);
289
290                         if (nbytes == CAN_MTU)
291                                 printf("  [%d]  ", frame.len);
292                         else
293                                 printf(" [%02d]  ", frame.len);
294
295                         datidx = 0;
296                         n_pci = frame.data[ext];
297             
298                         switch (n_pci & 0xF0) {
299                         case 0x00:
300                                 if (n_pci & 0xF) {
301                                         printf("[SF] ln: %-4d data:", n_pci & 0xF);
302                                         datidx = ext+1;
303                                 } else {
304                                         printf("[SF] ln: %-4d data:", frame.data[ext + 1]);
305                                         datidx = ext+2;
306                                 }
307                                 break;
308
309                         case 0x10:
310                                 fflen = ((n_pci & 0x0F)<<8) + frame.data[ext+1];
311                                 if (fflen)
312                                         datidx = ext+2;
313                                 else {
314                                         fflen = (frame.data[ext+2]<<24) +
315                                                 (frame.data[ext+3]<<16) +
316                                                 (frame.data[ext+4]<<8) +
317                                                 frame.data[ext+5];
318                                         datidx = ext+6;
319                                 }
320                                 printf("[FF] ln: %-4lu data:", fflen);
321                                 break;
322
323                         case 0x20:
324                                 printf("[CF] sn: %X    data:", n_pci & 0x0F);
325                                 datidx = ext+1;
326                                 break;
327
328                         case 0x30:
329                                 n_pci &= 0x0F;
330                                 printf("[FC] FC: %d ", n_pci);
331
332                                 if (n_pci > 3)
333                                         n_pci = 3;
334
335                                 printf("= %s # ", fc_info[n_pci]);
336
337                                 printf("BS: %d %s# ", frame.data[ext+1],
338                                        (frame.data[ext+1])? "":"= off ");
339
340                                 i = frame.data[ext+2];
341                                 printf("STmin: 0x%02X = ", i);
342
343                                 if (i < 0x80)
344                                         printf("%d ms", i);
345                                 else if (i > 0xF0 && i < 0xFA)
346                                         printf("%d us", (i & 0x0F) * 100);
347                                 else
348                                         printf("reserved");
349                                 break;
350
351                         default:
352                                 printf("[??]");
353                         }
354
355                         if (datidx && frame.len > datidx) {
356                                 printf(" ");
357                                 for (i = datidx; i < frame.len; i++) {
358                                         printf("%02X ", frame.data[i]);
359                                 }
360
361                                 if (asc) {
362                                         printf("%*s", ((7-ext) - (frame.len-datidx))*3 + 5 ,
363                                                "-  '");
364                                         for (i = datidx; i < frame.len; i++) {
365                                                 printf("%c",((frame.data[i] > 0x1F) &&
366                                                              (frame.data[i] < 0x7F))?
367                                                        frame.data[i] : '.');
368                                         }
369                                         printf("'");
370                                 }
371                         }
372
373                         if (color)
374                                 printf("%s", ATTRESET);
375                         printf("\n");
376                         fflush(stdout);
377                 }
378         }
379
380         close(s);
381
382         return 0;
383 }