]> rtime.felk.cvut.cz Git - can-utils.git/blob - cangen.c
Changed option name '-l' to '-x' as this option should also go into canplayer.
[can-utils.git] / cangen.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * cangen.c - CAN frames generator for testing purposes
7  *
8  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
23  *
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.
28  *
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.
31  *
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
43  * DAMAGE.
44  *
45  * Send feedback to <socketcan-users@lists.berlios.de>
46  *
47  */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <signal.h>
54 #include <ctype.h>
55 #include <libgen.h>
56 #include <time.h>
57 #include <errno.h>
58
59 #include <sys/time.h>
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
63 #include <sys/uio.h>
64 #include <net/if.h>
65
66 #include <linux/can.h>
67 #include <linux/can/raw.h>
68 #include "lib.h"
69
70 #define DEFAULT_GAP 200 /* ms */
71
72 extern int optind, opterr, optopt;
73
74 static volatile int running = 1;
75
76 void print_usage(char *prg)
77 {
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)          "
85             "default: 0x123\n");
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)         "
89             "default: 8\n");
90     fprintf(stderr, "         -f <canframe> (other fixed CAN frame) "
91             "default: 123#0123456789ABCDEF\n");
92     fprintf(stderr, "         -x            (disable loopback)      "
93             "default: standard loopback\n");
94     fprintf(stderr, "         -v            (verbose)               "
95             "default: don't print sent frames\n");
96 }
97
98 void sigterm(int signo)
99 {
100     running = 0;
101 }
102
103 int main(int argc, char **argv)
104 {
105     unsigned long gap = DEFAULT_GAP; 
106     unsigned char extended = 0;
107     unsigned char fix_id = 0;
108     unsigned char fix_data = 0;
109     unsigned char fix_dlc = 0;
110     unsigned char default_frame = 1;
111     unsigned char loopback_disable = 0;
112     unsigned char verbose = 0;
113
114     int opt;
115     int s; /* socket */
116
117     struct sockaddr_can addr;
118     static struct can_frame frame;
119     int nbytes;
120     struct ifreq ifr;
121
122     struct timespec ts;
123
124     signal(SIGTERM, sigterm);
125     signal(SIGHUP, sigterm);
126     signal(SIGINT, sigterm);
127
128     while ((opt = getopt(argc, argv, "g:eIDLf:xv")) != -1) {
129         switch (opt) {
130         case 'g':
131             gap = strtoul(optarg, NULL, 10);
132             break;
133
134         case 'e':
135             extended = 1;
136             break;
137
138         case 'I':
139             fix_id = 1;
140             break;
141
142         case 'D':
143             fix_data = 1;
144             break;
145
146         case 'L':
147             fix_dlc = 1;
148             break;
149
150         case 'f':
151             default_frame = 0;
152             if (parse_canframe(optarg, &frame)) {
153                 fprintf(stderr, "'%s' is a wrong CAN frame format.\n", optarg);
154                 exit(1);
155             }
156             break;
157
158         case 'v':
159             verbose = 1;
160             break;
161
162         case 'x':
163             loopback_disable = 1;
164             break;
165
166         default:
167             print_usage(basename(argv[0]));
168             exit(1);
169             break;
170         }
171     }
172
173     if (optind == argc) {
174         print_usage(basename(argv[0]));
175         exit(0);
176     }
177
178     ts.tv_sec = gap / 1000;
179     ts.tv_nsec = (gap % 1000) * 1000000;
180
181
182     if (default_frame) {
183         if (extended)
184             frame.can_id = 0x12345678 | CAN_EFF_FLAG;
185         else
186             frame.can_id = 0x123;
187
188         frame.can_dlc = 8;
189
190         frame.data[0] = 0x01;
191         frame.data[1] = 0x23;
192         frame.data[2] = 0x45;
193         frame.data[3] = 0x67;
194         frame.data[4] = 0x89;
195         frame.data[5] = 0xAB;
196         frame.data[6] = 0xCD;
197         frame.data[7] = 0xEF;
198     }
199
200     if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
201         perror("socket");
202         return 1;
203     }
204
205     addr.can_family = AF_CAN;
206
207     strcpy(ifr.ifr_name, argv[optind]);
208     if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
209         perror("SIOCGIFINDEX");
210         return 1;
211     }
212     addr.can_ifindex = ifr.ifr_ifindex;
213
214     /* disable default receive filter on this RAW socket */
215     /* This is obsolete as we do not read from the socket at all, but for */
216     /* this reason we can remove the receive list in the Kernel to save a */
217     /* little (really a very little!) CPU usage.                          */
218     setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
219
220     if (loopback_disable) {
221         int loopback = 0;
222
223         setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
224                    &loopback, sizeof(loopback));
225     }
226
227     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
228         perror("bind");
229         return 1;
230     }
231
232     while (running) {
233
234         if (!fix_id) {
235             frame.can_id = random();
236             if (extended) {
237                 frame.can_id &= CAN_EFF_MASK;
238                 frame.can_id |= CAN_EFF_FLAG;
239             } else
240                 frame.can_id &= CAN_SFF_MASK;
241         }
242
243         if (!fix_dlc) {
244             frame.can_dlc = random() & 0xF;
245             if (frame.can_dlc & 8)
246                 frame.can_dlc = 8; /* for about 50% of the frames */
247         }
248
249         if (!fix_data) {
250             /* that's what the 64 bit alignment of data[] is for ... :) */
251             *(unsigned long*)(&frame.data[0]) = random();
252             *(unsigned long*)(&frame.data[4]) = random();
253         }
254
255         if ((nbytes = write(s, &frame, sizeof(struct can_frame))) < 0) {
256             perror("write");
257             return 1;
258         } else if (nbytes < sizeof(struct can_frame)) {
259             fprintf(stderr, "write: incomplete CAN frame\n");
260             return 1;
261         }
262
263         if (gap) /* gap == 0 => performance test :-] */
264             if (nanosleep(&ts, NULL))
265                 return 1;
266                     
267         if (verbose)
268 #if 0
269             fprint_long_canframe(stdout, &frame, "\n", 1);
270 #else
271             fprint_canframe(stdout, &frame, "\n", 1);
272 #endif
273     }
274
275     close(s);
276
277     return 0;
278 }