6 * cangen.c - CAN frames generator for testing purposes
8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
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
44 * Send feedback to <linux-can@vger.kernel.org>
61 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <sys/ioctl.h>
67 #include <linux/can.h>
68 #include <linux/can/raw.h>
71 #define DEFAULT_GAP 200 /* ms */
74 #define MODE_INCREMENT 1
77 extern int optind, opterr, optopt;
79 static volatile int running = 1;
80 static unsigned long long enobufs_count;
82 void print_usage(char *prg)
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, " -f (generate CAN FD CAN frames)\n");
91 fprintf(stderr, " -R (send RTR frame)\n");
92 fprintf(stderr, " -m (mix -e -f -R frames)\n");
93 fprintf(stderr, " -I <mode> (CAN ID"
94 " generation mode - see below)\n");
95 fprintf(stderr, " -L <mode> (CAN data length code (dlc)"
96 " generation mode - see below)\n");
97 fprintf(stderr, " -D <mode> (CAN data (payload)"
98 " generation mode - see below)\n");
99 fprintf(stderr, " -p <timeout> (poll on -ENOBUFS to write frames"
100 " with <timeout> ms)\n");
101 fprintf(stderr, " -n <count> (terminate after <count> CAN frames "
102 "- default infinite)\n");
103 fprintf(stderr, " -i (ignore -ENOBUFS return values on"
104 " write() syscalls)\n");
105 fprintf(stderr, " -x (disable local loopback of "
106 "generated CAN frames)\n");
107 fprintf(stderr, " -v (increment verbose level for "
108 "printing sent CAN frames)\n\n");
109 fprintf(stderr, "Generation modes:\n");
110 fprintf(stderr, "'r' => random values (default)\n");
111 fprintf(stderr, "'i' => increment values\n");
112 fprintf(stderr, "<hexvalue> => fix value using <hexvalue>\n\n");
113 fprintf(stderr, "When incrementing the CAN data the data length code "
114 "minimum is set to 1.\n");
115 fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
116 fprintf(stderr, "Examples:\n");
117 fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v ", prg);
118 fprintf(stderr, "(fixed CAN ID and length, inc. data)\n");
119 fprintf(stderr, "%s vcan0 -e -L i -v -v -v ", prg);
120 fprintf(stderr, "(generate EFF frames, incr. length)\n");
121 fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8 ", prg);
122 fprintf(stderr, "(fixed CAN data payload and length)\n");
123 fprintf(stderr, "%s vcan0 -g 0 -i -x ", prg);
124 fprintf(stderr, "(full load test ignoring -ENOBUFS)\n");
125 fprintf(stderr, "%s vcan0 -g 0 -p 10 -x ", prg);
126 fprintf(stderr, "(full load test with polling, 10ms timeout)\n");
127 fprintf(stderr, "%s vcan0 ", prg);
128 fprintf(stderr, "(my favourite default :)\n\n");
131 void sigterm(int signo)
136 int main(int argc, char **argv)
138 double gap = DEFAULT_GAP;
139 unsigned long polltimeout = 0;
140 unsigned char ignore_enobufs = 0;
141 unsigned char extended = 0;
142 unsigned char canfd = 0;
143 unsigned char mix = 0;
144 unsigned char id_mode = MODE_RANDOM;
145 unsigned char data_mode = MODE_RANDOM;
146 unsigned char dlc_mode = MODE_RANDOM;
147 unsigned char loopback_disable = 0;
148 unsigned char verbose = 0;
149 unsigned char rtr_frame = 0;
152 uint64_t incdata = 0;
154 unsigned char fixdata[CANFD_MAX_DLEN];
160 struct sockaddr_can addr;
161 static struct canfd_frame frame;
169 /* set seed value for pseudo random numbers */
170 gettimeofday(&now, NULL);
171 srandom(now.tv_usec);
173 signal(SIGTERM, sigterm);
174 signal(SIGHUP, sigterm);
175 signal(SIGINT, sigterm);
177 while ((opt = getopt(argc, argv, "ig:efmI:L:D:xp:n:vRh?")) != -1) {
185 gap = strtod(optarg, NULL);
198 canfd = 1; /* to switch the socket into CAN FD mode */
202 if (optarg[0] == 'r') {
203 id_mode = MODE_RANDOM;
204 } else if (optarg[0] == 'i') {
205 id_mode = MODE_INCREMENT;
208 frame.can_id = strtoul(optarg, NULL, 16);
213 if (optarg[0] == 'r') {
214 dlc_mode = MODE_RANDOM;
215 } else if (optarg[0] == 'i') {
216 dlc_mode = MODE_INCREMENT;
219 frame.len = atoi(optarg) & 0xFF; /* is cut to 8 / 64 later */
224 if (optarg[0] == 'r') {
225 data_mode = MODE_RANDOM;
226 } else if (optarg[0] == 'i') {
227 data_mode = MODE_INCREMENT;
229 data_mode = MODE_FIX;
230 if (hexstring2data(optarg, fixdata, CANFD_MAX_DLEN)) {
231 printf ("wrong fix data definition\n");
242 loopback_disable = 1;
250 polltimeout = strtoul(optarg, NULL, 10);
254 count = atoi(optarg);
256 print_usage(basename(argv[0]));
264 print_usage(basename(argv[0]));
270 if (optind == argc) {
271 print_usage(basename(argv[0]));
275 ts.tv_sec = gap / 1000;
276 ts.tv_nsec = ((int)(gap * 1000000)) % 1000000000;
278 /* recognize obviously missing commandline option */
279 if (id_mode == MODE_FIX && frame.can_id > 0x7FF && !extended) {
280 printf("The given CAN-ID is greater than 0x7FF and "
281 "the '-e' option is not set.\n");
285 if (strlen(argv[optind]) >= IFNAMSIZ) {
286 printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
290 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
295 addr.can_family = AF_CAN;
297 strcpy(ifr.ifr_name, argv[optind]);
298 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
299 perror("SIOCGIFINDEX");
302 addr.can_ifindex = ifr.ifr_ifindex;
304 /* disable default receive filter on this RAW socket */
305 /* This is obsolete as we do not read from the socket at all, but for */
306 /* this reason we can remove the receive list in the Kernel to save a */
307 /* little (really a very little!) CPU usage. */
308 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
310 if (loopback_disable) {
313 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
314 &loopback, sizeof(loopback));
318 int enable_canfd = 1;
320 /* check if the frame fits into the CAN netdevice */
321 if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
322 perror("SIOCGIFMTU");
326 if (ifr.ifr_mtu != CANFD_MTU) {
327 printf("CAN interface ist not CAN FD capable - sorry.\n");
331 /* interface is ok - try to switch the socket into CAN FD mode */
332 if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))){
333 printf("error when enabling CAN FD support\n");
337 /* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
338 frame.len = can_dlc2len(can_len2dlc(frame.len));
340 /* sanitize CAN 2.0 frame length */
345 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
352 fds.events = POLLOUT;
357 if (count && (--count == 0))
362 maxdlen = CANFD_MAX_DLEN;
365 maxdlen = CAN_MAX_DLEN;
368 if (id_mode == MODE_RANDOM)
369 frame.can_id = random();
372 frame.can_id &= CAN_EFF_MASK;
373 frame.can_id |= CAN_EFF_FLAG;
375 frame.can_id &= CAN_SFF_MASK;
377 if (rtr_frame && !canfd)
378 frame.can_id |= CAN_RTR_FLAG;
380 if (dlc_mode == MODE_RANDOM) {
383 frame.len = can_dlc2len(random() & 0xF);
385 frame.len = random() & 0xF;
387 frame.len = 8; /* for about 50% of the frames */
391 if (data_mode == MODE_INCREMENT && !frame.len)
392 frame.len = 1; /* min dlc value for incr. data */
394 if (data_mode == MODE_RANDOM) {
396 /* that's what the 64 bit alignment of data[] is for ... :) */
397 *(unsigned long*)(&frame.data[0]) = random();
398 *(unsigned long*)(&frame.data[4]) = random();
400 /* omit extra random number generation for CAN FD */
401 if (canfd && frame.len > 8) {
402 memcpy(&frame.data[8], &frame.data[0], 8);
403 memcpy(&frame.data[16], &frame.data[0], 16);
404 memcpy(&frame.data[32], &frame.data[0], 32);
408 if (data_mode == MODE_FIX)
409 memcpy(frame.data, fixdata, CANFD_MAX_DLEN);
411 /* set unused payload data to zero like the CAN driver does it on rx */
412 if (frame.len < maxdlen)
413 memset(&frame.data[frame.len], 0, maxdlen - frame.len);
417 printf(" %s ", argv[optind]);
420 fprint_long_canframe(stdout, &frame, "\n", (verbose > 2)?1:0, maxdlen);
422 fprint_canframe(stdout, &frame, "\n", 1, maxdlen);
426 nbytes = write(s, &frame, mtu);
428 if (errno != ENOBUFS) {
432 if (!ignore_enobufs && !polltimeout) {
437 /* wait for the write socket (with timeout) */
438 if (poll(&fds, 1, polltimeout) < 0) {
446 } else if (nbytes < mtu) {
447 fprintf(stderr, "write: incomplete CAN frame\n");
451 if (gap) /* gap == 0 => performance test :-] */
452 if (nanosleep(&ts, NULL))
455 if (id_mode == MODE_INCREMENT)
458 if (dlc_mode == MODE_INCREMENT) {
464 frame.len = can_dlc2len(incdlc);
471 if (data_mode == MODE_INCREMENT) {
476 frame.data[i] = (incdata >> i*8) & 0xFFULL;
483 rtr_frame = ((i&12) == 12); /* reduce RTR frames to 1/4 */
488 printf("\nCounted %llu ENOBUFS return values on write().\n\n",