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