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