]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - isotpdump.c
can-utils: AOSP build clean up
[sojka/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
64 void print_usage(char *prg)
65 {
66         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
67         fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
68         fprintf(stderr, "         -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
69         fprintf(stderr, "         -x <addr>   (extended addressing mode. Use 'any' for all addresses)\n");
70         fprintf(stderr, "         -c          (color mode)\n");
71         fprintf(stderr, "         -a          (print data also in ASCII-chars)\n");
72         fprintf(stderr, "         -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
73         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
74         fprintf(stderr, "\n");
75 }
76
77 int main(int argc, char **argv)
78 {
79         int s;
80         struct sockaddr_can addr;
81         struct can_filter rfilter[2];
82         struct can_frame frame;
83         int nbytes, i;
84         canid_t src = NO_CAN_ID;
85         canid_t dst = NO_CAN_ID;
86         int ext = 0;
87         int extaddr = 0;
88         int extany = 0;
89         int asc = 0;
90         int color = 0;
91         int timestamp = 0;
92         int datidx = 0;
93         struct ifreq ifr;
94         int ifindex;
95         struct timeval tv, last_tv;
96         unsigned int n_pci;
97         int opt;
98
99         last_tv.tv_sec  = 0;
100         last_tv.tv_usec = 0;
101
102         while ((opt = getopt(argc, argv, "s:d:ax:ct:?")) != -1) {
103                 switch (opt) {
104                 case 's':
105                         src = strtoul(optarg, (char **)NULL, 16);
106                         if (strlen(optarg) > 7)
107                                 src |= CAN_EFF_FLAG;
108                         break;
109
110                 case 'd':
111                         dst = strtoul(optarg, (char **)NULL, 16);
112                         if (strlen(optarg) > 7)
113                                 dst |= CAN_EFF_FLAG;
114                         break;
115
116                 case 'c':
117                         color = 1;
118                         break;
119
120                 case 'a':
121                         asc = 1;
122                         break;
123
124                 case 'x':
125                         ext = 1;
126                         if (!strncmp(optarg, "any", 3))
127                                 extany = 1;
128                         else
129                                 extaddr = strtoul(optarg, (char **)NULL, 16) & 0xFF;
130
131                         break;
132
133                 case 't':
134                         timestamp = optarg[0];
135                         if ((timestamp != 'a') && (timestamp != 'A') &&
136                             (timestamp != 'd') && (timestamp != 'z')) {
137                                 printf("%s: unknown timestamp mode '%c' - ignored\n",
138                                        basename(argv[0]), optarg[0]);
139                                 timestamp = 0;
140                         }
141                         break;
142
143                 case '?':
144                         print_usage(basename(argv[0]));
145                         exit(0);
146                         break;
147
148                 default:
149                         fprintf(stderr, "Unknown option %c\n", opt);
150                         print_usage(basename(argv[0]));
151                         exit(1);
152                         break;
153                 }
154         }
155
156         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
157                 print_usage(basename(argv[0]));
158                 exit(0);
159         }
160
161         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
162                 perror("socket");
163                 return 1;
164         }
165
166
167         if (src & CAN_EFF_FLAG) {
168                 rfilter[0].can_id   = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
169                 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
170         } else {
171                 rfilter[0].can_id   = src & CAN_SFF_MASK;
172                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
173         }
174
175         if (dst & CAN_EFF_FLAG) {
176                 rfilter[1].can_id   = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
177                 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
178         } else {
179                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
180                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
181         }
182
183         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
184
185         strcpy(ifr.ifr_name, argv[optind]);
186         ioctl(s, SIOCGIFINDEX, &ifr);
187         ifindex = ifr.ifr_ifindex;
188
189         addr.can_family = AF_CAN;
190         addr.can_ifindex = ifindex;
191
192         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
193                 perror("bind");
194                 return 1;
195         }
196
197         while (1) {
198
199                 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
200                         perror("read");
201                         return 1;
202                 } else if (nbytes < sizeof(struct can_frame)) {
203                         fprintf(stderr, "read: incomplete CAN frame\n");
204                         return 1;
205                 } else {
206
207                         if (ext && !extany && extaddr != frame.data[0])
208                                 continue;
209
210                         if (color)
211                                 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
212
213                         if (timestamp) {
214                                 ioctl(s, SIOCGSTAMP, &tv);
215
216
217                                 switch (timestamp) {
218
219                                 case 'a': /* absolute with timestamp */
220                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
221                                         break;
222
223                                 case 'A': /* absolute with date */
224                                 {
225                                         struct tm tm;
226                                         char timestring[25];
227
228                                         tm = *localtime(&tv.tv_sec);
229                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
230                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
231                                 }
232                                 break;
233
234                                 case 'd': /* delta */
235                                 case 'z': /* starting with zero */
236                                 {
237                                         struct timeval diff;
238
239                                         if (last_tv.tv_sec == 0)   /* first init */
240                                                 last_tv = tv;
241                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
242                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
243                                         if (diff.tv_usec < 0)
244                                                 diff.tv_sec--, diff.tv_usec += 1000000;
245                                         if (diff.tv_sec < 0)
246                                                 diff.tv_sec = diff.tv_usec = 0;
247                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
248
249                                         if (timestamp == 'd')
250                                                 last_tv = tv; /* update for delta calculation */
251                                 }
252                                 break;
253
254                                 default: /* no timestamp output */
255                                         break;
256                                 }
257                         }
258
259                         if (frame.can_id & CAN_EFF_FLAG)
260                                 printf(" %s  %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
261                         else
262                                 printf(" %s  %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
263
264                         if (ext)
265                                 printf("{%02X}", frame.data[0]);
266
267                         printf("  [%d]  ", frame.can_dlc);
268
269                         datidx = 0;
270                         n_pci = frame.data[ext];
271             
272                         switch (n_pci & 0xF0) {
273                         case 0x00:
274                                 printf("[SF] ln: %-4d data:", n_pci & 0x0F);
275                                 datidx = ext+1;
276                                 break;
277
278                         case 0x10:
279                                 printf("[FF] ln: %-4d data:",
280                                        ((n_pci & 0x0F)<<8) + frame.data[ext+1] );
281                                 datidx = ext+2;
282                                 break;
283
284                         case 0x20:
285                                 printf("[CF] sn: %X    data:", n_pci & 0x0F);
286                                 datidx = ext+1;
287                                 break;
288
289                         case 0x30:
290                                 n_pci &= 0x0F;
291                                 printf("[FC] FC: %d ", n_pci);
292
293                                 if (n_pci > 3)
294                                         n_pci = 3;
295
296                                 printf("= %s # ", fc_info[n_pci]);
297
298                                 printf("BS: %d %s# ", frame.data[ext+1],
299                                        (frame.data[ext+1])? "":"= off ");
300
301                                 i = frame.data[ext+2];
302                                 printf("STmin: 0x%02X = ", i);
303
304                                 if (i < 0x80)
305                                         printf("%d ms", i);
306                                 else if (i > 0xF0 && i < 0xFA)
307                                         printf("%d us", (i & 0x0F) * 100);
308                                 else
309                                         printf("reserved");
310                                 break;
311
312                         default:
313                                 printf("[??]");
314                         }
315
316                         if (datidx && frame.can_dlc > datidx) {
317                                 printf(" ");
318                                 for (i = datidx; i < frame.can_dlc; i++) {
319                                         printf("%02X ", frame.data[i]);
320                                 }
321
322                                 if (asc) {
323                                         printf("%*s", ((7-ext) - (frame.can_dlc-datidx))*3 + 5 ,
324                                                "-  '");
325                                         for (i = datidx; i < frame.can_dlc; i++) {
326                                                 printf("%c",((frame.data[i] > 0x1F) &&
327                                                              (frame.data[i] < 0x7F))?
328                                                        frame.data[i] : '.');
329                                         }
330                                         printf("'");
331                                 }
332                         }
333
334                         if (color)
335                                 printf("%s", ATTRESET);
336                         printf("\n");
337                         fflush(stdout);
338                 }
339         }
340
341         close(s);
342
343         return 0;
344 }