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