]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/isotpdump.c
Added missing inclusion of linux/types.h
[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                 case '?':
148                         print_usage(basename(argv[0]));
149                         exit(0);
150                         break;
151
152                 default:
153                         fprintf(stderr, "Unknown option %c\n", opt);
154                         print_usage(basename(argv[0]));
155                         exit(1);
156                         break;
157                 }
158         }
159
160         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
161                 print_usage(basename(argv[0]));
162                 exit(0);
163         }
164
165         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
166                 perror("socket");
167                 return 1;
168         }
169
170
171         if (src & CAN_EFF_FLAG) {
172                 rfilter[0].can_id   = src & (CAN_EFF_MASK | CAN_EFF_FLAG);
173                 rfilter[0].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
174         } else {
175                 rfilter[0].can_id   = src & CAN_SFF_MASK;
176                 rfilter[0].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
177         }
178
179         if (dst & CAN_EFF_FLAG) {
180                 rfilter[1].can_id   = dst & (CAN_EFF_MASK | CAN_EFF_FLAG);
181                 rfilter[1].can_mask = (CAN_EFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
182         } else {
183                 rfilter[1].can_id   = dst & CAN_SFF_MASK;
184                 rfilter[1].can_mask = (CAN_SFF_MASK|CAN_EFF_FLAG|CAN_RTR_FLAG);
185         }
186
187         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
188
189         strcpy(ifr.ifr_name, argv[optind]);
190         ioctl(s, SIOCGIFINDEX, &ifr);
191         ifindex = ifr.ifr_ifindex;
192
193         addr.can_family = AF_CAN;
194         addr.can_ifindex = ifindex;
195
196         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
197                 perror("bind");
198                 return 1;
199         }
200
201         while (1) {
202
203                 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
204                         perror("read");
205                         return 1;
206                 } else if (nbytes < sizeof(struct can_frame)) {
207                         fprintf(stderr, "read: incomplete CAN frame\n");
208                         return 1;
209                 } else {
210
211                         if (ext && !extany && extaddr != frame.data[0])
212                                 continue;
213
214                         if (color)
215                                 printf("%s", (frame.can_id == src)? FGRED:FGBLUE);
216
217                         if (timestamp) {
218                                 ioctl(s, SIOCGSTAMP, &tv);
219
220
221                                 switch (timestamp) {
222
223                                 case 'a': /* absolute with timestamp */
224                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
225                                         break;
226
227                                 case 'A': /* absolute with date */
228                                 {
229                                         struct tm tm;
230                                         char timestring[25];
231
232                                         tm = *localtime(&tv.tv_sec);
233                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
234                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
235                                 }
236                                 break;
237
238                                 case 'd': /* delta */
239                                 case 'z': /* starting with zero */
240                                 {
241                                         struct timeval diff;
242
243                                         if (last_tv.tv_sec == 0)   /* first init */
244                                                 last_tv = tv;
245                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
246                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
247                                         if (diff.tv_usec < 0)
248                                                 diff.tv_sec--, diff.tv_usec += 1000000;
249                                         if (diff.tv_sec < 0)
250                                                 diff.tv_sec = diff.tv_usec = 0;
251                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
252
253                                         if (timestamp == 'd')
254                                                 last_tv = tv; /* update for delta calculation */
255                                 }
256                                 break;
257
258                                 default: /* no timestamp output */
259                                         break;
260                                 }
261                         }
262
263                         if (frame.can_id & CAN_EFF_FLAG)
264                                 printf(" %s  %8X", argv[optind], frame.can_id & CAN_EFF_MASK);
265                         else
266                                 printf(" %s  %3X", argv[optind], frame.can_id & CAN_SFF_MASK);
267
268                         if (ext)
269                                 printf("{%02X}", frame.data[0]);
270
271                         printf("  [%d]  ", frame.can_dlc);
272
273                         datidx = 0;
274                         n_pci = frame.data[ext];
275             
276                         switch (n_pci & 0xF0) {
277                         case 0x00:
278                                 printf("[SF] ln: %-4d data:", n_pci & 0x0F);
279                                 datidx = ext+1;
280                                 break;
281
282                         case 0x10:
283                                 printf("[FF] ln: %-4d data:",
284                                        ((n_pci & 0x0F)<<8) + frame.data[ext+1] );
285                                 datidx = ext+2;
286                                 break;
287
288                         case 0x20:
289                                 printf("[CF] sn: %X    data:", n_pci & 0x0F);
290                                 datidx = ext+1;
291                                 break;
292
293                         case 0x30:
294                                 n_pci &= 0x0F;
295                                 printf("[FC] FC: %d ", n_pci);
296
297                                 if (n_pci > 3)
298                                         n_pci = 3;
299
300                                 printf("= %s # ", fc_info[n_pci]);
301
302                                 printf("BS: %d %s# ", frame.data[ext+1],
303                                        (frame.data[ext+1])? "":"= off ");
304
305                                 i = frame.data[ext+2];
306                                 printf("STmin: 0x%02X = ", i);
307
308                                 if (i < 0x80)
309                                         printf("%d ms", i);
310                                 else if (i > 0xF0 && i < 0xFA)
311                                         printf("%d us", (i & 0x0F) * 100);
312                                 else
313                                         printf("reserved");
314                                 break;
315
316                         default:
317                                 printf("[??]");
318                         }
319
320                         if (datidx && frame.can_dlc > datidx) {
321                                 printf(" ");
322                                 for (i = datidx; i < frame.can_dlc; i++) {
323                                         printf("%02X ", frame.data[i]);
324                                 }
325
326                                 if (asc) {
327                                         printf("%*s", ((7-ext) - (frame.can_dlc-datidx))*3 + 5 ,
328                                                "-  '");
329                                         for (i = datidx; i < frame.can_dlc; i++) {
330                                                 printf("%c",((frame.data[i] > 0x1F) &&
331                                                              (frame.data[i] < 0x7F))?
332                                                        frame.data[i] : '.');
333                                         }
334                                         printf("'");
335                                 }
336                         }
337
338                         if (color)
339                                 printf("%s", ATTRESET);
340                         printf("\n");
341                         fflush(stdout);
342                 }
343         }
344
345         close(s);
346
347         return 0;
348 }