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, the following disclaimer and
16 * the referenced file 'COPYING'.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Volkswagen nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * Alternatively, provided that this notice is retained in full, this
25 * software may be distributed under the terms of the GNU General
26 * Public License ("GPL") version 2 as distributed in the 'COPYING'
27 * file from the main directory of the linux kernel source.
29 * The provided data structures and external interfaces from this code
30 * are not restricted to be used by modules with a GPL compatible license.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 * Send feedback to <socketcan-users@lists.berlios.de>
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
66 #include <linux/can.h>
67 #include <linux/can/raw.h>
70 #define DEFAULT_GAP 200 /* ms */
72 extern int optind, opterr, optopt;
74 static volatile int running = 1;
76 void print_usage(char *prg)
78 fprintf(stderr, "\n%s: generate random CAN frames\n\n", prg);
79 fprintf(stderr, "Usage: %s [can-interface]\n", prg);
80 fprintf(stderr, "Options: -g <ms> (gap in milli seconds) "
81 "default: %d\n", DEFAULT_GAP);
82 fprintf(stderr, " -e (extended frame mode) "
83 "default: standard frame format \n");
84 fprintf(stderr, " -I (fixed CAN ID) "
86 fprintf(stderr, " -D (fixed CAN Data) "
87 "default: 01 23 45 67 89 AB CD EF\n");
88 fprintf(stderr, " -L (fixed CAN DLC) "
90 fprintf(stderr, " -f <canframe> (other fixed CAN frame) "
91 "default: 123#0123456789ABCDEF\n");
92 fprintf(stderr, " -v (verbose) "
93 "default: don't print sent frames\n");
96 void sigterm(int signo)
101 int main(int argc, char **argv)
103 unsigned long gap = DEFAULT_GAP;
104 unsigned char extended = 0;
105 unsigned char fix_id = 0;
106 unsigned char fix_data = 0;
107 unsigned char fix_dlc = 0;
108 unsigned char default_frame = 1;
109 unsigned char verbose = 0;
114 struct sockaddr_can addr;
115 static struct can_frame frame;
121 signal(SIGTERM, sigterm);
122 signal(SIGHUP, sigterm);
123 signal(SIGINT, sigterm);
125 while ((opt = getopt(argc, argv, "g:eIDLf:v")) != -1) {
128 gap = strtoul(optarg, NULL, 10);
149 if (parse_canframe(optarg, &frame)) {
150 fprintf(stderr, "'%s' is a wrong CAN frame format.\n", optarg);
160 print_usage(basename(argv[0]));
166 if (optind == argc) {
167 print_usage(basename(argv[0]));
171 ts.tv_sec = gap / 1000;
172 ts.tv_nsec = (gap % 1000) * 1000000;
177 frame.can_id = 0x12345678 | CAN_EFF_FLAG;
179 frame.can_id = 0x123;
183 frame.data[0] = 0x01;
184 frame.data[1] = 0x23;
185 frame.data[2] = 0x45;
186 frame.data[3] = 0x67;
187 frame.data[4] = 0x89;
188 frame.data[5] = 0xAB;
189 frame.data[6] = 0xCD;
190 frame.data[7] = 0xEF;
193 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
198 addr.can_family = AF_CAN;
200 strcpy(ifr.ifr_name, argv[optind]);
201 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
202 perror("SIOCGIFINDEX");
203 addr.can_ifindex = ifr.ifr_ifindex;
205 /* disable default receive filter on this RAW socket */
206 /* This is obsolete as we do not read from the socket at all, but for */
207 /* this reason we can remove the receive list in the Kernel to save a */
208 /* little (really a very little!) CPU usage. */
209 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
211 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
219 frame.can_id = random();
221 frame.can_id &= CAN_EFF_MASK;
222 frame.can_id |= CAN_EFF_FLAG;
224 frame.can_id &= CAN_SFF_MASK;
228 frame.can_dlc = random() & 0xF;
229 if (frame.can_dlc & 8)
230 frame.can_dlc = 8; /* for about 50% of the frames */
234 /* that's what the 64 bit alignment of data[] is for ... :) */
235 *(unsigned long*)(&frame.data[0]) = random();
236 *(unsigned long*)(&frame.data[4]) = random();
239 if ((nbytes = write(s, &frame, sizeof(struct can_frame))) < 0) {
242 } else if (nbytes < sizeof(struct can_frame)) {
243 fprintf(stderr, "write: incomplete CAN frame\n");
247 if (gap) /* gap == 0 => performance test :-] */
248 if (nanosleep(&ts, NULL))
253 fprint_long_canframe(stdout, &frame, "\n", 1);
255 fprint_canframe(stdout, &frame, "\n", 1);