2 * cangen.c - CAN frames generator for testing purposes
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
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
40 * Send feedback to <linux-can@vger.kernel.org>
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
63 #include <linux/can.h>
64 #include <linux/can/raw.h>
67 #define DEFAULT_GAP 200 /* ms */
70 #define MODE_INCREMENT 1
73 extern int optind, opterr, optopt;
75 static volatile int running = 1;
76 static unsigned long long enobufs_count;
78 void print_usage(char *prg)
80 fprintf(stderr, "\n%s: generate CAN frames\n\n", prg);
81 fprintf(stderr, "Usage: %s [options] <CAN interface>\n", prg);
82 fprintf(stderr, "Options: -g <ms> (gap in milli seconds "
83 "- default: %d ms)\n", DEFAULT_GAP);
84 fprintf(stderr, " -e (generate extended frame mode "
85 "(EFF) CAN frames)\n");
86 fprintf(stderr, " -f (generate CAN FD CAN frames)\n");
87 fprintf(stderr, " -b (generate CAN FD CAN frames"
88 " with bitrate switch (BRS))\n");
89 fprintf(stderr, " -R (send RTR frame)\n");
90 fprintf(stderr, " -m (mix -e -f -b -R frames)\n");
91 fprintf(stderr, " -I <mode> (CAN ID"
92 " generation mode - see below)\n");
93 fprintf(stderr, " -L <mode> (CAN data length code (dlc)"
94 " generation mode - see below)\n");
95 fprintf(stderr, " -D <mode> (CAN data (payload)"
96 " generation mode - see below)\n");
97 fprintf(stderr, " -p <timeout> (poll on -ENOBUFS to write frames"
98 " with <timeout> ms)\n");
99 fprintf(stderr, " -n <count> (terminate after <count> CAN frames "
100 "- default infinite)\n");
101 fprintf(stderr, " -i (ignore -ENOBUFS return values on"
102 " write() syscalls)\n");
103 fprintf(stderr, " -x (disable local loopback of "
104 "generated CAN frames)\n");
105 fprintf(stderr, " -v (increment verbose level for "
106 "printing sent CAN frames)\n\n");
107 fprintf(stderr, "Generation modes:\n");
108 fprintf(stderr, "'r' => random values (default)\n");
109 fprintf(stderr, "'i' => increment values\n");
110 fprintf(stderr, "<hexvalue> => fix value using <hexvalue>\n\n");
111 fprintf(stderr, "When incrementing the CAN data the data length code "
112 "minimum is set to 1.\n");
113 fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
114 fprintf(stderr, "Examples:\n");
115 fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v ", prg);
116 fprintf(stderr, "(fixed CAN ID and length, inc. data)\n");
117 fprintf(stderr, "%s vcan0 -e -L i -v -v -v ", prg);
118 fprintf(stderr, "(generate EFF frames, incr. length)\n");
119 fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8 ", prg);
120 fprintf(stderr, "(fixed CAN data payload and length)\n");
121 fprintf(stderr, "%s vcan0 -g 0 -i -x ", prg);
122 fprintf(stderr, "(full load test ignoring -ENOBUFS)\n");
123 fprintf(stderr, "%s vcan0 -g 0 -p 10 -x ", prg);
124 fprintf(stderr, "(full load test with polling, 10ms timeout)\n");
125 fprintf(stderr, "%s vcan0 ", prg);
126 fprintf(stderr, "(my favourite default :)\n\n");
129 void sigterm(int signo)
134 int main(int argc, char **argv)
136 double gap = DEFAULT_GAP;
137 unsigned long polltimeout = 0;
138 unsigned char ignore_enobufs = 0;
139 unsigned char extended = 0;
140 unsigned char canfd = 0;
141 unsigned char brs = 0;
142 unsigned char mix = 0;
143 unsigned char id_mode = MODE_RANDOM;
144 unsigned char data_mode = MODE_RANDOM;
145 unsigned char dlc_mode = MODE_RANDOM;
146 unsigned char loopback_disable = 0;
147 unsigned char verbose = 0;
148 unsigned char rtr_frame = 0;
151 uint64_t incdata = 0;
153 unsigned char fixdata[CANFD_MAX_DLEN];
159 struct sockaddr_can addr;
160 static struct canfd_frame frame;
168 /* set seed value for pseudo random numbers */
169 gettimeofday(&now, NULL);
170 srandom(now.tv_usec);
172 signal(SIGTERM, sigterm);
173 signal(SIGHUP, sigterm);
174 signal(SIGINT, sigterm);
176 while ((opt = getopt(argc, argv, "ig:ebfmI:L:D:xp:n:vRh?")) != -1) {
184 gap = strtod(optarg, NULL);
196 brs = 1; /* bitrate switch implies CAN FD */
202 canfd = 1; /* to switch the socket into CAN FD mode */
206 if (optarg[0] == 'r') {
207 id_mode = MODE_RANDOM;
208 } else if (optarg[0] == 'i') {
209 id_mode = MODE_INCREMENT;
212 frame.can_id = strtoul(optarg, NULL, 16);
217 if (optarg[0] == 'r') {
218 dlc_mode = MODE_RANDOM;
219 } else if (optarg[0] == 'i') {
220 dlc_mode = MODE_INCREMENT;
223 frame.len = atoi(optarg) & 0xFF; /* is cut to 8 / 64 later */
228 if (optarg[0] == 'r') {
229 data_mode = MODE_RANDOM;
230 } else if (optarg[0] == 'i') {
231 data_mode = MODE_INCREMENT;
233 data_mode = MODE_FIX;
234 if (hexstring2data(optarg, fixdata, CANFD_MAX_DLEN)) {
235 printf ("wrong fix data definition\n");
246 loopback_disable = 1;
254 polltimeout = strtoul(optarg, NULL, 10);
258 count = atoi(optarg);
260 print_usage(basename(argv[0]));
268 print_usage(basename(argv[0]));
274 if (optind == argc) {
275 print_usage(basename(argv[0]));
279 ts.tv_sec = gap / 1000;
280 ts.tv_nsec = (long)(((long long)(gap * 1000000)) % 1000000000ll);
282 /* recognize obviously missing commandline option */
283 if (id_mode == MODE_FIX && frame.can_id > 0x7FF && !extended) {
284 printf("The given CAN-ID is greater than 0x7FF and "
285 "the '-e' option is not set.\n");
289 if (strlen(argv[optind]) >= IFNAMSIZ) {
290 printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
294 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
299 addr.can_family = AF_CAN;
301 strcpy(ifr.ifr_name, argv[optind]);
302 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
303 perror("SIOCGIFINDEX");
306 addr.can_ifindex = ifr.ifr_ifindex;
308 /* disable default receive filter on this RAW socket */
309 /* This is obsolete as we do not read from the socket at all, but for */
310 /* this reason we can remove the receive list in the Kernel to save a */
311 /* little (really a very little!) CPU usage. */
312 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
314 if (loopback_disable) {
317 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
318 &loopback, sizeof(loopback));
322 int enable_canfd = 1;
324 /* check if the frame fits into the CAN netdevice */
325 if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
326 perror("SIOCGIFMTU");
330 if (ifr.ifr_mtu != CANFD_MTU) {
331 printf("CAN interface ist not CAN FD capable - sorry.\n");
335 /* interface is ok - try to switch the socket into CAN FD mode */
336 if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))){
337 printf("error when enabling CAN FD support\n");
341 /* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
342 frame.len = can_dlc2len(can_len2dlc(frame.len));
344 /* sanitize CAN 2.0 frame length */
349 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
356 fds.events = POLLOUT;
361 if (count && (--count == 0))
366 maxdlen = CANFD_MAX_DLEN;
368 frame.flags |= CANFD_BRS;
371 maxdlen = CAN_MAX_DLEN;
374 if (id_mode == MODE_RANDOM)
375 frame.can_id = random();
378 frame.can_id &= CAN_EFF_MASK;
379 frame.can_id |= CAN_EFF_FLAG;
381 frame.can_id &= CAN_SFF_MASK;
383 if (rtr_frame && !canfd)
384 frame.can_id |= CAN_RTR_FLAG;
386 if (dlc_mode == MODE_RANDOM) {
389 frame.len = can_dlc2len(random() & 0xF);
391 frame.len = random() & 0xF;
393 frame.len = 8; /* for about 50% of the frames */
397 if (data_mode == MODE_INCREMENT && !frame.len)
398 frame.len = 1; /* min dlc value for incr. data */
400 if (data_mode == MODE_RANDOM) {
402 /* that's what the 64 bit alignment of data[] is for ... :) */
403 *(unsigned long*)(&frame.data[0]) = random();
404 *(unsigned long*)(&frame.data[4]) = random();
406 /* omit extra random number generation for CAN FD */
407 if (canfd && frame.len > 8) {
408 memcpy(&frame.data[8], &frame.data[0], 8);
409 memcpy(&frame.data[16], &frame.data[0], 16);
410 memcpy(&frame.data[32], &frame.data[0], 32);
414 if (data_mode == MODE_FIX)
415 memcpy(frame.data, fixdata, CANFD_MAX_DLEN);
417 /* set unused payload data to zero like the CAN driver does it on rx */
418 if (frame.len < maxdlen)
419 memset(&frame.data[frame.len], 0, maxdlen - frame.len);
423 printf(" %s ", argv[optind]);
426 fprint_long_canframe(stdout, &frame, "\n", (verbose > 2)?1:0, maxdlen);
428 fprint_canframe(stdout, &frame, "\n", 1, maxdlen);
432 nbytes = write(s, &frame, mtu);
434 if (errno != ENOBUFS) {
438 if (!ignore_enobufs && !polltimeout) {
443 /* wait for the write socket (with timeout) */
444 if (poll(&fds, 1, polltimeout) < 0) {
452 } else if (nbytes < mtu) {
453 fprintf(stderr, "write: incomplete CAN frame\n");
457 if (gap) /* gap == 0 => performance test :-] */
458 if (nanosleep(&ts, NULL))
461 if (id_mode == MODE_INCREMENT)
464 if (dlc_mode == MODE_INCREMENT) {
470 frame.len = can_dlc2len(incdlc);
477 if (data_mode == MODE_INCREMENT) {
482 frame.data[i] = (incdata >> i*8) & 0xFFULL;
491 rtr_frame = ((i&24) == 24); /* reduce RTR frames to 1/4 */
496 printf("\nCounted %llu ENOBUFS return values on write().\n\n",