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