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 <socketcan-users@lists.berlios.de>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
65 #include <linux/can.h>
66 #include <linux/can/raw.h>
69 #define DEFAULT_GAP 200 /* ms */
71 extern int optind, opterr, optopt;
73 static volatile int running = 1;
75 void print_usage(char *prg)
77 fprintf(stderr, "\n%s: generate random CAN frames\n\n", prg);
78 fprintf(stderr, "Usage: %s [can-interface]\n", prg);
79 fprintf(stderr, "Options: -g <ms> (gap in milli seconds) "
80 "default: %d\n", DEFAULT_GAP);
81 fprintf(stderr, " -e (extended frame mode) "
82 "default: standard frame format \n");
83 fprintf(stderr, " -I (fixed CAN ID) "
85 fprintf(stderr, " -D (fixed CAN Data) "
86 "default: 01 23 45 67 89 AB CD EF\n");
87 fprintf(stderr, " -L (fixed CAN DLC) "
89 fprintf(stderr, " -f <canframe> (other fixed CAN frame) "
90 "default: 123#0123456789ABCDEF\n");
91 fprintf(stderr, " -x (disable loopback) "
92 "default: standard loopback\n");
93 fprintf(stderr, " -v (verbose) "
94 "default: don't print sent frames\n");
97 void sigterm(int signo)
102 int main(int argc, char **argv)
104 unsigned long gap = DEFAULT_GAP;
105 unsigned char extended = 0;
106 unsigned char fix_id = 0;
107 unsigned char fix_data = 0;
108 unsigned char fix_dlc = 0;
109 unsigned char default_frame = 1;
110 unsigned char loopback_disable = 0;
111 unsigned char verbose = 0;
116 struct sockaddr_can addr;
117 static struct can_frame frame;
123 signal(SIGTERM, sigterm);
124 signal(SIGHUP, sigterm);
125 signal(SIGINT, sigterm);
127 while ((opt = getopt(argc, argv, "g:eIDLf:xv")) != -1) {
130 gap = strtoul(optarg, NULL, 10);
151 if (parse_canframe(optarg, &frame)) {
152 fprintf(stderr, "'%s' is a wrong CAN frame format.\n", optarg);
162 loopback_disable = 1;
166 print_usage(basename(argv[0]));
172 if (optind == argc) {
173 print_usage(basename(argv[0]));
177 ts.tv_sec = gap / 1000;
178 ts.tv_nsec = (gap % 1000) * 1000000;
183 frame.can_id = 0x12345678 | CAN_EFF_FLAG;
185 frame.can_id = 0x123;
189 frame.data[0] = 0x01;
190 frame.data[1] = 0x23;
191 frame.data[2] = 0x45;
192 frame.data[3] = 0x67;
193 frame.data[4] = 0x89;
194 frame.data[5] = 0xAB;
195 frame.data[6] = 0xCD;
196 frame.data[7] = 0xEF;
199 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
204 addr.can_family = AF_CAN;
206 strcpy(ifr.ifr_name, argv[optind]);
207 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
208 perror("SIOCGIFINDEX");
211 addr.can_ifindex = ifr.ifr_ifindex;
213 /* disable default receive filter on this RAW socket */
214 /* This is obsolete as we do not read from the socket at all, but for */
215 /* this reason we can remove the receive list in the Kernel to save a */
216 /* little (really a very little!) CPU usage. */
217 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
219 if (loopback_disable) {
222 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
223 &loopback, sizeof(loopback));
226 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
234 frame.can_id = random();
236 frame.can_id &= CAN_EFF_MASK;
237 frame.can_id |= CAN_EFF_FLAG;
239 frame.can_id &= CAN_SFF_MASK;
243 frame.can_dlc = random() & 0xF;
244 if (frame.can_dlc & 8)
245 frame.can_dlc = 8; /* for about 50% of the frames */
249 /* that's what the 64 bit alignment of data[] is for ... :) */
250 *(unsigned long*)(&frame.data[0]) = random();
251 *(unsigned long*)(&frame.data[4]) = random();
254 if ((nbytes = write(s, &frame, sizeof(struct can_frame))) < 0) {
257 } else if (nbytes < sizeof(struct can_frame)) {
258 fprintf(stderr, "write: incomplete CAN frame\n");
262 if (gap) /* gap == 0 => performance test :-] */
263 if (nanosleep(&ts, NULL))
268 fprint_long_canframe(stdout, &frame, "\n", 1);
270 fprint_canframe(stdout, &frame, "\n", 1);