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, " -R (send RTR frame)\n");
88 fprintf(stderr, " -m (mix -e -f -R 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, " -p <timeout> (poll on -ENOBUFS to write frames"
96 " with <timeout> ms)\n");
97 fprintf(stderr, " -n <count> (terminate after <count> CAN frames "
98 "- default infinite)\n");
99 fprintf(stderr, " -i (ignore -ENOBUFS return values on"
100 " write() syscalls)\n");
101 fprintf(stderr, " -x (disable local loopback of "
102 "generated CAN frames)\n");
103 fprintf(stderr, " -v (increment verbose level for "
104 "printing sent CAN frames)\n\n");
105 fprintf(stderr, "Generation modes:\n");
106 fprintf(stderr, "'r' => random values (default)\n");
107 fprintf(stderr, "'i' => increment values\n");
108 fprintf(stderr, "<hexvalue> => fix value using <hexvalue>\n\n");
109 fprintf(stderr, "When incrementing the CAN data the data length code "
110 "minimum is set to 1.\n");
111 fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
112 fprintf(stderr, "Examples:\n");
113 fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v ", prg);
114 fprintf(stderr, "(fixed CAN ID and length, inc. data)\n");
115 fprintf(stderr, "%s vcan0 -e -L i -v -v -v ", prg);
116 fprintf(stderr, "(generate EFF frames, incr. length)\n");
117 fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8 ", prg);
118 fprintf(stderr, "(fixed CAN data payload and length)\n");
119 fprintf(stderr, "%s vcan0 -g 0 -i -x ", prg);
120 fprintf(stderr, "(full load test ignoring -ENOBUFS)\n");
121 fprintf(stderr, "%s vcan0 -g 0 -p 10 -x ", prg);
122 fprintf(stderr, "(full load test with polling, 10ms timeout)\n");
123 fprintf(stderr, "%s vcan0 ", prg);
124 fprintf(stderr, "(my favourite default :)\n\n");
127 void sigterm(int signo)
132 int main(int argc, char **argv)
134 double gap = DEFAULT_GAP;
135 unsigned long polltimeout = 0;
136 unsigned char ignore_enobufs = 0;
137 unsigned char extended = 0;
138 unsigned char canfd = 0;
139 unsigned char mix = 0;
140 unsigned char id_mode = MODE_RANDOM;
141 unsigned char data_mode = MODE_RANDOM;
142 unsigned char dlc_mode = MODE_RANDOM;
143 unsigned char loopback_disable = 0;
144 unsigned char verbose = 0;
145 unsigned char rtr_frame = 0;
148 uint64_t incdata = 0;
150 unsigned char fixdata[CANFD_MAX_DLEN];
156 struct sockaddr_can addr;
157 static struct canfd_frame frame;
165 /* set seed value for pseudo random numbers */
166 gettimeofday(&now, NULL);
167 srandom(now.tv_usec);
169 signal(SIGTERM, sigterm);
170 signal(SIGHUP, sigterm);
171 signal(SIGINT, sigterm);
173 while ((opt = getopt(argc, argv, "ig:efmI:L:D:xp:n:vRh?")) != -1) {
181 gap = strtod(optarg, NULL);
194 canfd = 1; /* to switch the socket into CAN FD mode */
198 if (optarg[0] == 'r') {
199 id_mode = MODE_RANDOM;
200 } else if (optarg[0] == 'i') {
201 id_mode = MODE_INCREMENT;
204 frame.can_id = strtoul(optarg, NULL, 16);
209 if (optarg[0] == 'r') {
210 dlc_mode = MODE_RANDOM;
211 } else if (optarg[0] == 'i') {
212 dlc_mode = MODE_INCREMENT;
215 frame.len = atoi(optarg) & 0xFF; /* is cut to 8 / 64 later */
220 if (optarg[0] == 'r') {
221 data_mode = MODE_RANDOM;
222 } else if (optarg[0] == 'i') {
223 data_mode = MODE_INCREMENT;
225 data_mode = MODE_FIX;
226 if (hexstring2data(optarg, fixdata, CANFD_MAX_DLEN)) {
227 printf ("wrong fix data definition\n");
238 loopback_disable = 1;
246 polltimeout = strtoul(optarg, NULL, 10);
250 count = atoi(optarg);
252 print_usage(basename(argv[0]));
260 print_usage(basename(argv[0]));
266 if (optind == argc) {
267 print_usage(basename(argv[0]));
271 ts.tv_sec = gap / 1000;
272 ts.tv_nsec = ((int)(gap * 1000000)) % 1000000000;
274 /* recognize obviously missing commandline option */
275 if (id_mode == MODE_FIX && frame.can_id > 0x7FF && !extended) {
276 printf("The given CAN-ID is greater than 0x7FF and "
277 "the '-e' option is not set.\n");
281 if (strlen(argv[optind]) >= IFNAMSIZ) {
282 printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
286 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
291 addr.can_family = AF_CAN;
293 strcpy(ifr.ifr_name, argv[optind]);
294 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
295 perror("SIOCGIFINDEX");
298 addr.can_ifindex = ifr.ifr_ifindex;
300 /* disable default receive filter on this RAW socket */
301 /* This is obsolete as we do not read from the socket at all, but for */
302 /* this reason we can remove the receive list in the Kernel to save a */
303 /* little (really a very little!) CPU usage. */
304 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
306 if (loopback_disable) {
309 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
310 &loopback, sizeof(loopback));
314 int enable_canfd = 1;
316 /* check if the frame fits into the CAN netdevice */
317 if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
318 perror("SIOCGIFMTU");
322 if (ifr.ifr_mtu != CANFD_MTU) {
323 printf("CAN interface ist not CAN FD capable - sorry.\n");
327 /* interface is ok - try to switch the socket into CAN FD mode */
328 if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd, sizeof(enable_canfd))){
329 printf("error when enabling CAN FD support\n");
333 /* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
334 frame.len = can_dlc2len(can_len2dlc(frame.len));
336 /* sanitize CAN 2.0 frame length */
341 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
348 fds.events = POLLOUT;
353 if (count && (--count == 0))
358 maxdlen = CANFD_MAX_DLEN;
361 maxdlen = CAN_MAX_DLEN;
364 if (id_mode == MODE_RANDOM)
365 frame.can_id = random();
368 frame.can_id &= CAN_EFF_MASK;
369 frame.can_id |= CAN_EFF_FLAG;
371 frame.can_id &= CAN_SFF_MASK;
373 if (rtr_frame && !canfd)
374 frame.can_id |= CAN_RTR_FLAG;
376 if (dlc_mode == MODE_RANDOM) {
379 frame.len = can_dlc2len(random() & 0xF);
381 frame.len = random() & 0xF;
383 frame.len = 8; /* for about 50% of the frames */
387 if (data_mode == MODE_INCREMENT && !frame.len)
388 frame.len = 1; /* min dlc value for incr. data */
390 if (data_mode == MODE_RANDOM) {
392 /* that's what the 64 bit alignment of data[] is for ... :) */
393 *(unsigned long*)(&frame.data[0]) = random();
394 *(unsigned long*)(&frame.data[4]) = random();
396 /* omit extra random number generation for CAN FD */
397 if (canfd && frame.len > 8) {
398 memcpy(&frame.data[8], &frame.data[0], 8);
399 memcpy(&frame.data[16], &frame.data[0], 16);
400 memcpy(&frame.data[32], &frame.data[0], 32);
404 if (data_mode == MODE_FIX)
405 memcpy(frame.data, fixdata, CANFD_MAX_DLEN);
407 /* set unused payload data to zero like the CAN driver does it on rx */
408 if (frame.len < maxdlen)
409 memset(&frame.data[frame.len], 0, maxdlen - frame.len);
413 printf(" %s ", argv[optind]);
416 fprint_long_canframe(stdout, &frame, "\n", (verbose > 2)?1:0, maxdlen);
418 fprint_canframe(stdout, &frame, "\n", 1, maxdlen);
422 nbytes = write(s, &frame, mtu);
424 if (errno != ENOBUFS) {
428 if (!ignore_enobufs && !polltimeout) {
433 /* wait for the write socket (with timeout) */
434 if (poll(&fds, 1, polltimeout) < 0) {
442 } else if (nbytes < mtu) {
443 fprintf(stderr, "write: incomplete CAN frame\n");
447 if (gap) /* gap == 0 => performance test :-] */
448 if (nanosleep(&ts, NULL))
451 if (id_mode == MODE_INCREMENT)
454 if (dlc_mode == MODE_INCREMENT) {
460 frame.len = can_dlc2len(incdlc);
467 if (data_mode == MODE_INCREMENT) {
472 frame.data[i] = (incdata >> i*8) & 0xFFULL;
479 rtr_frame = ((i&12) == 12); /* reduce RTR frames to 1/4 */
484 printf("\nCounted %llu ENOBUFS return values on write().\n\n",