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