]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - cangen.c
Fixed contradiction in Sourcecode discalimer.
[sojka/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 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.
22  *
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.
27  *
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.
30  *
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
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <ctype.h>
54 #include <libgen.h>
55 #include <time.h>
56 #include <errno.h>
57
58 #include <sys/time.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
62 #include <sys/uio.h>
63 #include <net/if.h>
64
65 #include <linux/can.h>
66 #include <linux/can/raw.h>
67 #include "lib.h"
68
69 #define DEFAULT_GAP 200 /* ms */
70
71 extern int optind, opterr, optopt;
72
73 static volatile int running = 1;
74
75 void print_usage(char *prg)
76 {
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)          "
84             "default: 0x123\n");
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)         "
88             "default: 8\n");
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");
95 }
96
97 void sigterm(int signo)
98 {
99     running = 0;
100 }
101
102 int main(int argc, char **argv)
103 {
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;
112
113     int opt;
114     int s; /* socket */
115
116     struct sockaddr_can addr;
117     static struct can_frame frame;
118     int nbytes;
119     struct ifreq ifr;
120
121     struct timespec ts;
122
123     signal(SIGTERM, sigterm);
124     signal(SIGHUP, sigterm);
125     signal(SIGINT, sigterm);
126
127     while ((opt = getopt(argc, argv, "g:eIDLf:xv")) != -1) {
128         switch (opt) {
129         case 'g':
130             gap = strtoul(optarg, NULL, 10);
131             break;
132
133         case 'e':
134             extended = 1;
135             break;
136
137         case 'I':
138             fix_id = 1;
139             break;
140
141         case 'D':
142             fix_data = 1;
143             break;
144
145         case 'L':
146             fix_dlc = 1;
147             break;
148
149         case 'f':
150             default_frame = 0;
151             if (parse_canframe(optarg, &frame)) {
152                 fprintf(stderr, "'%s' is a wrong CAN frame format.\n", optarg);
153                 exit(1);
154             }
155             break;
156
157         case 'v':
158             verbose = 1;
159             break;
160
161         case 'x':
162             loopback_disable = 1;
163             break;
164
165         default:
166             print_usage(basename(argv[0]));
167             exit(1);
168             break;
169         }
170     }
171
172     if (optind == argc) {
173         print_usage(basename(argv[0]));
174         exit(0);
175     }
176
177     ts.tv_sec = gap / 1000;
178     ts.tv_nsec = (gap % 1000) * 1000000;
179
180
181     if (default_frame) {
182         if (extended)
183             frame.can_id = 0x12345678 | CAN_EFF_FLAG;
184         else
185             frame.can_id = 0x123;
186
187         frame.can_dlc = 8;
188
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;
197     }
198
199     if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
200         perror("socket");
201         return 1;
202     }
203
204     addr.can_family = AF_CAN;
205
206     strcpy(ifr.ifr_name, argv[optind]);
207     if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
208         perror("SIOCGIFINDEX");
209         return 1;
210     }
211     addr.can_ifindex = ifr.ifr_ifindex;
212
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);
218
219     if (loopback_disable) {
220         int loopback = 0;
221
222         setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
223                    &loopback, sizeof(loopback));
224     }
225
226     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
227         perror("bind");
228         return 1;
229     }
230
231     while (running) {
232
233         if (!fix_id) {
234             frame.can_id = random();
235             if (extended) {
236                 frame.can_id &= CAN_EFF_MASK;
237                 frame.can_id |= CAN_EFF_FLAG;
238             } else
239                 frame.can_id &= CAN_SFF_MASK;
240         }
241
242         if (!fix_dlc) {
243             frame.can_dlc = random() & 0xF;
244             if (frame.can_dlc & 8)
245                 frame.can_dlc = 8; /* for about 50% of the frames */
246         }
247
248         if (!fix_data) {
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();
252         }
253
254         if ((nbytes = write(s, &frame, sizeof(struct can_frame))) < 0) {
255             perror("write");
256             return 1;
257         } else if (nbytes < sizeof(struct can_frame)) {
258             fprintf(stderr, "write: incomplete CAN frame\n");
259             return 1;
260         }
261
262         if (gap) /* gap == 0 => performance test :-] */
263             if (nanosleep(&ts, NULL))
264                 return 1;
265                     
266         if (verbose)
267 #if 0
268             fprint_long_canframe(stdout, &frame, "\n", 1);
269 #else
270             fprint_canframe(stdout, &frame, "\n", 1);
271 #endif
272     }
273
274     close(s);
275
276     return 0;
277 }