]> rtime.felk.cvut.cz Git - can-utils.git/blob - cangen.c
- added error frame support in lib.c
[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, "         -v            (verbose)               "
93             "default: don't print sent frames\n");
94 }
95
96 void sigterm(int signo)
97 {
98     running = 0;
99 }
100
101 int main(int argc, char **argv)
102 {
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;
110
111     int opt;
112     int s; /* socket */
113
114     struct sockaddr_can addr;
115     static struct can_frame frame;
116     int nbytes;
117     struct ifreq ifr;
118
119     struct timespec ts;
120
121     signal(SIGTERM, sigterm);
122     signal(SIGHUP, sigterm);
123     signal(SIGINT, sigterm);
124
125     while ((opt = getopt(argc, argv, "g:eIDLf:v")) != -1) {
126         switch (opt) {
127         case 'g':
128             gap = strtoul(optarg, NULL, 10);
129             break;
130
131         case 'e':
132             extended = 1;
133             break;
134
135         case 'I':
136             fix_id = 1;
137             break;
138
139         case 'D':
140             fix_data = 1;
141             break;
142
143         case 'L':
144             fix_dlc = 1;
145             break;
146
147         case 'f':
148             default_frame = 0;
149             if (parse_canframe(optarg, &frame)) {
150                 fprintf(stderr, "'%s' is a wrong CAN frame format.\n", optarg);
151                 exit(1);
152             }
153             break;
154
155         case 'v':
156             verbose = 1;
157             break;
158
159         default:
160             print_usage(basename(argv[0]));
161             exit(1);
162             break;
163         }
164     }
165
166     if (optind == argc) {
167         print_usage(basename(argv[0]));
168         exit(0);
169     }
170
171     ts.tv_sec = gap / 1000;
172     ts.tv_nsec = (gap % 1000) * 1000000;
173
174
175     if (default_frame) {
176         if (extended)
177             frame.can_id = 0x12345678 | CAN_EFF_FLAG;
178         else
179             frame.can_id = 0x123;
180
181         frame.can_dlc = 8;
182
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;
191     }
192
193     if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
194         perror("socket");
195         return 1;
196     }
197
198     addr.can_family = AF_CAN;
199
200     strcpy(ifr.ifr_name, argv[optind]);
201     if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
202         perror("SIOCGIFINDEX");
203     addr.can_ifindex = ifr.ifr_ifindex;
204
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);
210
211     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
212         perror("bind");
213         return 1;
214     }
215
216     while (running) {
217
218         if (!fix_id) {
219             frame.can_id = random();
220             if (extended) {
221                 frame.can_id &= CAN_EFF_MASK;
222                 frame.can_id |= CAN_EFF_FLAG;
223             } else
224                 frame.can_id &= CAN_SFF_MASK;
225         }
226
227         if (!fix_dlc) {
228             frame.can_dlc = random() & 0xF;
229             if (frame.can_dlc & 8)
230                 frame.can_dlc = 8; /* for about 50% of the frames */
231         }
232
233         if (!fix_data) {
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();
237         }
238
239         if ((nbytes = write(s, &frame, sizeof(struct can_frame))) < 0) {
240             perror("write");
241             return 1;
242         } else if (nbytes < sizeof(struct can_frame)) {
243             fprintf(stderr, "write: incomplete CAN frame\n");
244             return 1;
245         }
246
247         if (gap) /* gap == 0 => performance test :-] */
248             if (nanosleep(&ts, NULL))
249                 return 1;
250                     
251         if (verbose)
252 #if 0
253             fprint_long_canframe(stdout, &frame, "\n", 1);
254 #else
255             fprint_canframe(stdout, &frame, "\n", 1);
256 #endif
257     }
258
259     close(s);
260
261     return 0;
262 }