]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - cangen.c
To simplify packaging of can-utils, add DESTDIR and PREFIX support to
[sojka/can-utils.git] / cangen.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * cangen.c - CAN frames generator for testing purposes
7  *
8  * Copyright (c) 2002-2007 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 <stdint.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <signal.h>
54 #include <poll.h>
55 #include <ctype.h>
56 #include <libgen.h>
57 #include <time.h>
58 #include <errno.h>
59
60 #include <sys/time.h>
61 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <sys/ioctl.h>
64 #include <sys/uio.h>
65 #include <net/if.h>
66
67 #include <linux/can.h>
68 #include <linux/can/raw.h>
69 #include "lib.h"
70
71 #define DEFAULT_GAP 200 /* ms */
72
73 #define MODE_RANDOM     0
74 #define MODE_INCREMENT  1
75 #define MODE_FIX        2
76
77 extern int optind, opterr, optopt;
78
79 static volatile int running = 1;
80 static unsigned long long enobufs_count;
81
82 void print_usage(char *prg)
83 {
84         fprintf(stderr, "\n%s: generate CAN frames\n\n", prg);
85         fprintf(stderr, "Usage: %s [options] <CAN interface>\n", prg);
86         fprintf(stderr, "Options: -g <ms>       (gap in milli seconds "
87                 "- default: %d ms)\n", DEFAULT_GAP);
88         fprintf(stderr, "         -e            (generate extended frame mode "
89                 "(EFF) CAN frames)\n");
90         fprintf(stderr, "         -I <mode>     (CAN ID"
91                 " generation mode - see below)\n");
92         fprintf(stderr, "         -L <mode>     (CAN data length code (dlc)"
93                 " generation mode - see below)\n");
94         fprintf(stderr, "         -D <mode>     (CAN data (payload)"
95                 " generation mode - see below)\n");
96         fprintf(stderr, "         -p <timeout>  (poll on -ENOBUFS to write frames"
97                 " with <timeout> ms)\n");
98         fprintf(stderr, "         -i            (ignore -ENOBUFS return values on"
99                 " write() syscalls)\n");
100         fprintf(stderr, "         -x            (disable local loopback of "
101                 "generated CAN frames)\n");
102         fprintf(stderr, "         -v            (increment verbose level for "
103                 "printing sent CAN frames)\n\n");
104         fprintf(stderr, "Generation modes:\n");
105         fprintf(stderr, "'r'        => random values (default)\n");
106         fprintf(stderr, "'i'        => increment values\n");
107         fprintf(stderr, "<hexvalue> => fix value using <hexvalue>\n\n");
108         fprintf(stderr, "When incrementing the CAN data the data length code "
109                 "minimum is set to 1.\n");
110         fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
111         fprintf(stderr, "Examples:\n");
112         fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v   ", prg);
113         fprintf(stderr, "(fixed CAN ID and length, inc. data)\n");
114         fprintf(stderr, "%s vcan0 -e -L i -v -v -v              ", prg);
115         fprintf(stderr, "(generate EFF frames, incr. length)\n");
116         fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8      ", prg);
117         fprintf(stderr, "(fixed CAN data payload and length)\n");
118         fprintf(stderr, "%s vcan0 -g 0 -i -x                    ", prg);
119         fprintf(stderr, "(full load test ignoring -ENOBUFS)\n");
120         fprintf(stderr, "%s vcan0 -g 0 -p 10 -x                 ", prg);
121         fprintf(stderr, "(full load test with polling, 10ms timeout)\n");
122         fprintf(stderr, "%s vcan0                               ", prg);
123         fprintf(stderr, "(my favourite default :)\n\n");
124 }
125
126 void sigterm(int signo)
127 {
128         running = 0;
129 }
130
131 int main(int argc, char **argv)
132 {
133         unsigned long gap = DEFAULT_GAP; 
134         unsigned long polltimeout = 0;
135         unsigned char ignore_enobufs = 0;
136         unsigned char extended = 0;
137         unsigned char id_mode = MODE_RANDOM;
138         unsigned char data_mode = MODE_RANDOM;
139         unsigned char dlc_mode = MODE_RANDOM;
140         unsigned char loopback_disable = 0;
141         unsigned char verbose = 0;
142         uint64_t incdata = 0;
143
144         int opt;
145         int s; /* socket */
146         struct pollfd fds;
147
148         struct sockaddr_can addr;
149         static struct can_frame frame;
150         int nbytes;
151         int i;
152         struct ifreq ifr;
153
154         struct timespec ts;
155
156         signal(SIGTERM, sigterm);
157         signal(SIGHUP, sigterm);
158         signal(SIGINT, sigterm);
159
160         while ((opt = getopt(argc, argv, "ig:eI:L:D:xp:vh?")) != -1) {
161                 switch (opt) {
162
163                 case 'i':
164                         ignore_enobufs = 1;
165                         break;
166
167                 case 'g':
168                         gap = strtoul(optarg, NULL, 10);
169                         break;
170
171                 case 'e':
172                         extended = 1;
173                         break;
174
175                 case 'I':
176                         if (optarg[0] == 'r') {
177                                 id_mode = MODE_RANDOM;
178                         } else if (optarg[0] == 'i') {
179                                 id_mode = MODE_INCREMENT;
180                         } else {
181                                 id_mode = MODE_FIX;
182                                 frame.can_id = strtoul(optarg, NULL, 16);
183                         }
184                         break;
185
186                 case 'L':
187                         if (optarg[0] == 'r') {
188                                 dlc_mode = MODE_RANDOM;
189                         } else if (optarg[0] == 'i') {
190                                 dlc_mode = MODE_INCREMENT;
191                         } else {
192                                 dlc_mode = MODE_FIX;
193                                 frame.can_dlc = atoi(optarg)%9;
194                         }
195                         break;
196
197                 case 'D':
198                         if (optarg[0] == 'r') {
199                                 data_mode = MODE_RANDOM;
200                         } else if (optarg[0] == 'i') {
201                                 data_mode = MODE_INCREMENT;
202                         } else {
203                                 data_mode = MODE_FIX;
204                                 if (hexstring2candata(optarg, &frame)) {
205                                         printf ("wrong fix data definition\n");
206                                         return 1;
207                                 }
208                         }
209                         break;
210
211                 case 'v':
212                         verbose++;
213                         break;
214
215                 case 'x':
216                         loopback_disable = 1;
217                         break;
218
219                 case 'p':
220                         polltimeout = strtoul(optarg, NULL, 10);
221                         break;
222
223                 case '?':
224                 case 'h':
225                 default:
226                         print_usage(basename(argv[0]));
227                         return 1;
228                         break;
229                 }
230         }
231
232         if (optind == argc) {
233                 print_usage(basename(argv[0]));
234                 return 1;
235         }
236
237         ts.tv_sec = gap / 1000;
238         ts.tv_nsec = (gap % 1000) * 1000000;
239
240         if (id_mode == MODE_FIX) {
241
242                 /* recognize obviously missing commandline option */
243                 if ((frame.can_id > 0x7FF) && !extended) {
244                         printf("The given CAN-ID is greater than 0x7FF and "
245                                "the '-e' option is not set.\n");
246                         return 1;
247                 }
248
249                 if (extended)
250                         frame.can_id &= CAN_EFF_MASK;
251                 else
252                         frame.can_id &= CAN_SFF_MASK;
253         }
254
255         if (extended)
256                 frame.can_id |=  CAN_EFF_FLAG;
257
258         if ((data_mode == MODE_INCREMENT) && !frame.can_dlc)
259                 frame.can_dlc = 1; /* min dlc value for incr. data */
260
261         if (strlen(argv[optind]) >= IFNAMSIZ) {
262                 printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
263                 return 1;
264         }
265
266         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
267                 perror("socket");
268                 return 1;
269         }
270
271         addr.can_family = AF_CAN;
272
273         strcpy(ifr.ifr_name, argv[optind]);
274         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
275                 perror("SIOCGIFINDEX");
276                 return 1;
277         }
278         addr.can_ifindex = ifr.ifr_ifindex;
279
280         /* disable default receive filter on this RAW socket */
281         /* This is obsolete as we do not read from the socket at all, but for */
282         /* this reason we can remove the receive list in the Kernel to save a */
283         /* little (really a very little!) CPU usage.                          */
284         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
285
286         if (loopback_disable) {
287                 int loopback = 0;
288
289                 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
290                            &loopback, sizeof(loopback));
291         }
292
293         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
294                 perror("bind");
295                 return 1;
296         }
297
298         if (polltimeout) {
299                 fds.fd = s;
300                 fds.events = POLLOUT;
301         }
302
303         while (running) {
304
305                 if (id_mode == MODE_RANDOM) {
306
307                         frame.can_id = random();
308
309                         if (extended) {
310                                 frame.can_id &= CAN_EFF_MASK;
311                                 frame.can_id |= CAN_EFF_FLAG;
312                         } else
313                                 frame.can_id &= CAN_SFF_MASK;
314                 }
315
316                 if (dlc_mode == MODE_RANDOM) {
317
318                         frame.can_dlc = random() & 0xF;
319
320                         if (frame.can_dlc & 8)
321                                 frame.can_dlc = 8; /* for about 50% of the frames */
322
323                         if ((data_mode == MODE_INCREMENT) && !frame.can_dlc)
324                                 frame.can_dlc = 1; /* min dlc value for incr. data */
325                 }
326
327                 if (data_mode == MODE_RANDOM) {
328
329                         /* that's what the 64 bit alignment of data[] is for ... :) */
330                         *(unsigned long*)(&frame.data[0]) = random();
331                         *(unsigned long*)(&frame.data[4]) = random();
332                 }
333
334                 if (verbose) {
335
336                         printf("  %s  ", argv[optind]);
337
338                         if (verbose > 1)
339                                 fprint_long_canframe(stdout, &frame, "\n", (verbose > 2)?1:0);
340                         else
341                                 fprint_canframe(stdout, &frame, "\n", 1);
342                 }
343
344 resend:
345                 nbytes = write(s, &frame, sizeof(struct can_frame));
346                 if (nbytes < 0) {
347                         if (errno != ENOBUFS) {
348                                 perror("write");
349                                 return 1;
350                         }
351                         if (!ignore_enobufs && !polltimeout) {
352                                 perror("write");
353                                 return 1;
354                         }
355                         if (polltimeout) {
356                                 /* wait for the write socket (with timeout) */
357                                 if (poll(&fds, 1, polltimeout) < 0) {
358                                         perror("poll");
359                                         return 1;
360                                 } else
361                                         goto resend;
362                         } else
363                                 enobufs_count++;
364
365                 } else if (nbytes < sizeof(struct can_frame)) {
366                         fprintf(stderr, "write: incomplete CAN frame\n");
367                         return 1;
368                 }
369
370                 if (gap) /* gap == 0 => performance test :-] */
371                         if (nanosleep(&ts, NULL))
372                                 return 1;
373                     
374                 if (id_mode == MODE_INCREMENT) {
375
376                         frame.can_id++;
377
378                         if (extended) {
379                                 frame.can_id &= CAN_EFF_MASK;
380                                 frame.can_id |= CAN_EFF_FLAG;
381                         } else
382                                 frame.can_id &= CAN_SFF_MASK;
383                 }
384
385                 if (dlc_mode == MODE_INCREMENT) {
386
387                         frame.can_dlc++;
388                         frame.can_dlc %= 9;
389
390                         if ((data_mode == MODE_INCREMENT) && !frame.can_dlc)
391                                 frame.can_dlc = 1; /* min dlc value for incr. data */
392                 }
393
394                 if (data_mode == MODE_INCREMENT) {
395
396                         incdata++;
397
398                         for (i=0; i<8 ;i++)
399                                 frame.data[i] = (incdata >> i*8) & 0xFFULL;
400                 }
401         }
402
403         if (enobufs_count)
404                 printf("\nCounted %llu ENOBUFS return values on write().\n\n",
405                        enobufs_count);
406
407         close(s);
408
409         return 0;
410 }