]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotpsniffer.c
candump: Enable HW timestamping before using it
[can-utils.git] / isotpsniffer.c
1 /*
2  * isotpsniffer.c - dump ISO15765-2 datagrams using PF_CAN isotp protocol 
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 <unistd.h>
46 #include <stdlib.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/isotp.h>
58 #include "terminal.h"
59
60 #define NO_CAN_ID 0xFFFFFFFFU
61
62 #define FORMAT_HEX 1
63 #define FORMAT_ASCII 2
64 #define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
65
66 void print_usage(char *prg)
67 {
68         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
69         fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
70         fprintf(stderr, "         -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
71         fprintf(stderr, "         -x <addr>   (extended addressing mode)\n");
72         fprintf(stderr, "         -X <addr>   (extended addressing mode - rx addr)\n");
73         fprintf(stderr, "         -c          (color mode)\n");
74         fprintf(stderr, "         -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
75         fprintf(stderr, "         -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
76         fprintf(stderr, "         -h <len>    (head: print only first <len> bytes)\n");
77         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
78         fprintf(stderr, "\n");
79 }
80
81 void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
82               int format, struct timeval *tv, struct timeval *last_tv,
83               canid_t src, int socket, char *candevice, int head)
84 {
85         int i;
86
87         if (color == 1)
88                 printf("%s", FGRED);
89
90         if (color == 2)
91                 printf("%s", FGBLUE);
92
93         if (timestamp) {
94                 ioctl(socket, SIOCGSTAMP, tv);
95
96                 switch (timestamp) {
97
98                 case 'a': /* absolute with timestamp */
99                         printf("(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
100                         break;
101
102                 case 'A': /* absolute with date */
103                 {
104                         struct tm tm;
105                         char timestring[25];
106
107                         tm = *localtime(&tv->tv_sec);
108                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
109                         printf("(%s.%06ld) ", timestring, tv->tv_usec);
110                 }
111                 break;
112
113                 case 'd': /* delta */
114                 case 'z': /* starting with zero */
115                 {
116                         struct timeval diff;
117
118                         if (last_tv->tv_sec == 0)   /* first init */
119                                 *last_tv = *tv;
120                         diff.tv_sec  = tv->tv_sec  - last_tv->tv_sec;
121                         diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
122                         if (diff.tv_usec < 0)
123                                 diff.tv_sec--, diff.tv_usec += 1000000;
124                         if (diff.tv_sec < 0)
125                                 diff.tv_sec = diff.tv_usec = 0;
126                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
127
128                         if (timestamp == 'd')
129                                 *last_tv = *tv; /* update for delta calculation */
130                 }
131                 break;
132
133                 default: /* no timestamp output */
134                         break;
135                 }
136         }
137
138         /* the source socket gets pdu data from the detination id */
139         printf(" %s  %03X  [%d]  ", candevice, src & CAN_EFF_MASK, nbytes);
140         if (format & FORMAT_HEX) {
141                 for (i=0; i<nbytes; i++) {
142                         printf("%02X ", buffer[i]);
143                         if (head && i+1 >= head) {
144                                 printf("... ");
145                                 break;
146                         }
147                 }
148                 if (format & FORMAT_ASCII)
149                         printf(" - ");
150         }
151         if (format & FORMAT_ASCII) {
152                 printf("'");
153                 for (i=0; i<nbytes; i++) {
154                         if ((buffer[i] > 0x1F) && (buffer[i] < 0x7F))
155                                 printf("%c", buffer[i]);
156                         else
157                                 printf(".");
158                         if (head && i+1 >= head)
159                                 break;
160                 }
161                 printf("'");
162                 if (head && i+1 >= head)
163                         printf(" ... ");
164         }
165
166         if (color)
167                 printf("%s", ATTRESET);
168
169         printf("\n");
170         fflush(stdout);
171 }
172
173 int main(int argc, char **argv)
174 {
175         fd_set rdfs;
176         int s, t;
177         struct sockaddr_can addr;
178         char if_name[IFNAMSIZ];
179         static struct can_isotp_options opts;
180         int opt, quit = 0;
181         int color = 0;
182         int head = 0;
183         int timestamp = 0;
184         int format = FORMAT_DEFAULT;
185         canid_t src = NO_CAN_ID;
186         canid_t dst = NO_CAN_ID;
187         extern int optind, opterr, optopt;
188         static struct timeval tv, last_tv;
189
190         unsigned char buffer[4096];
191         int nbytes;
192
193         while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:?")) != -1) {
194                 switch (opt) {
195                 case 's':
196                         src = strtoul(optarg, (char **)NULL, 16);
197                         if (strlen(optarg) > 7)
198                                 src |= CAN_EFF_FLAG;
199                         break;
200
201                 case 'd':
202                         dst = strtoul(optarg, (char **)NULL, 16);
203                         if (strlen(optarg) > 7)
204                                 dst |= CAN_EFF_FLAG;
205                         break;
206
207                 case 'x':
208                         opts.flags |= CAN_ISOTP_EXTEND_ADDR;
209                         opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
210                         break;
211
212                 case 'X':
213                         opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
214                         opts.rx_ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
215                         break;
216
217                 case 'f':
218                         format = (atoi(optarg) & (FORMAT_ASCII | FORMAT_HEX));
219                         break;
220
221                 case 'h':
222                         head = atoi(optarg);
223                         break;
224
225                 case 'c':
226                         color = 1;
227                         break;
228
229                 case 't':
230                         timestamp = optarg[0];
231                         if ((timestamp != 'a') && (timestamp != 'A') &&
232                             (timestamp != 'd') && (timestamp != 'z')) {
233                                 printf("%s: unknown timestamp mode '%c' - ignored\n",
234                                        basename(argv[0]), optarg[0]);
235                                 timestamp = 0;
236                         }
237                         break;
238
239                 case '?':
240                         print_usage(basename(argv[0]));
241                         exit(0);
242                         break;
243
244                 default:
245                         fprintf(stderr, "Unknown option %c\n", opt);
246                         print_usage(basename(argv[0]));
247                         exit(1);
248                         break;
249                 }
250         }
251
252         if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
253                 print_usage(basename(argv[0]));
254                 exit(1);
255         }
256   
257         if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
258                 print_usage(basename(argv[0]));
259                 exit(1);
260         }
261
262         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
263                 perror("socket");
264                 exit(1);
265         }
266
267         if ((t = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
268                 perror("socket");
269                 exit(1);
270         }
271
272         opts.flags |= CAN_ISOTP_LISTEN_MODE;
273
274         strncpy(if_name, argv[optind], IFNAMSIZ - 1);
275         if_name[IFNAMSIZ - 1] = '\0';
276
277         addr.can_family = AF_CAN;
278         addr.can_ifindex = if_nametoindex(if_name);
279
280         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
281
282         addr.can_addr.tp.tx_id = src;
283         addr.can_addr.tp.rx_id = dst;
284
285         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
286                 perror("bind");
287                 close(s);
288                 exit(1);
289         }
290
291         if (opts.flags & CAN_ISOTP_RX_EXT_ADDR) {
292                 /* flip extended address info due to separate rx ext addr */
293                 __u8 tmpext;
294
295                 tmpext = opts.ext_address;
296                 opts.ext_address = opts.rx_ext_address;
297                 opts.rx_ext_address = tmpext;
298         }
299
300         setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
301
302         addr.can_addr.tp.tx_id = dst;
303         addr.can_addr.tp.rx_id = src;
304
305         if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
306                 perror("bind");
307                 close(s);
308                 exit(1);
309         }
310
311         while (!quit) {
312
313                 FD_ZERO(&rdfs);
314                 FD_SET(s, &rdfs);
315                 FD_SET(t, &rdfs);
316                 FD_SET(0, &rdfs);
317
318                 if ((nbytes = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
319                         perror("select");
320                         continue;
321                 }
322
323                 if (FD_ISSET(0, &rdfs)) {
324                         getchar();
325                         quit = 1;
326                         printf("quit due to keyboard input.\n");
327                 }
328
329                 if (FD_ISSET(s, &rdfs)) {
330                         nbytes = read(s, buffer, 4096);
331                         if (nbytes < 0) {
332                                 perror("read socket s");
333                                 return -1;
334                         }
335                         if (nbytes > 4095)
336                                 return -1;
337                         printbuf(buffer, nbytes, color?2:0, timestamp, format,
338                                  &tv, &last_tv, dst, s, if_name, head);
339                 }
340
341                 if (FD_ISSET(t, &rdfs)) {
342                         nbytes = read(t, buffer, 4096);
343                         if (nbytes < 0) {
344                                 perror("read socket t");
345                                 return -1;
346                         }
347                         if (nbytes > 4095)
348                                 return -1;
349                         printbuf(buffer, nbytes, color?1:0, timestamp, format,
350                                  &tv, &last_tv, src, t, if_name, head);
351                 }
352         }
353
354         close(s);
355
356         return 0;
357 }