]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - cangen.c
slcand: replace char pointers with static char buffers
[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 <stdint.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <signal.h>
54 #include <poll.h>
55 #include <ctype.h>
56 #include <libgen.h>
57 #include <time.h>
58 #include <errno.h>
59
60 #include <sys/time.h>
61 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <sys/ioctl.h>
64 #include <sys/uio.h>
65 #include <net/if.h>
66
67 #include <linux/can.h>
68 #include <linux/can/raw.h>
69 #include "lib.h"
70
71 #define DEFAULT_GAP 200 /* ms */
72
73 #define MODE_RANDOM     0
74 #define MODE_INCREMENT  1
75 #define MODE_FIX        2
76
77 extern int optind, opterr, optopt;
78
79 static volatile int running = 1;
80 static unsigned long long enobufs_count;
81
82 void print_usage(char *prg)
83 {
84         fprintf(stderr, "\n%s: generate CAN frames\n\n", prg);
85         fprintf(stderr, "Usage: %s [options] <CAN interface>\n", prg);
86         fprintf(stderr, "Options: -g <ms>       (gap in milli seconds "
87                 "- default: %d ms)\n", DEFAULT_GAP);
88         fprintf(stderr, "         -e            (generate extended frame mode "
89                 "(EFF) CAN frames)\n");
90         fprintf(stderr, "         -I <mode>     (CAN ID"
91                 " generation mode - see below)\n");
92         fprintf(stderr, "         -L <mode>     (CAN data length code (dlc)"
93                 " generation mode - see below)\n");
94         fprintf(stderr, "         -D <mode>     (CAN data (payload)"
95                 " generation mode - see below)\n");
96         fprintf(stderr, "         -p <timeout>  (poll on -ENOBUFS to write frames"
97                 " with <timeout> ms)\n");
98         fprintf(stderr, "         -n <count>    (terminate after <count> CAN frames "
99                 "- default infinite)\n");
100         fprintf(stderr, "         -i            (ignore -ENOBUFS return values on"
101                 " write() syscalls)\n");
102         fprintf(stderr, "         -x            (disable local loopback of "
103                 "generated CAN frames)\n");
104         fprintf(stderr, "         -v            (increment verbose level for "
105                 "printing sent CAN frames)\n\n");
106         fprintf(stderr, "Generation modes:\n");
107         fprintf(stderr, "'r'        => random values (default)\n");
108         fprintf(stderr, "'i'        => increment values\n");
109         fprintf(stderr, "<hexvalue> => fix value using <hexvalue>\n\n");
110         fprintf(stderr, "When incrementing the CAN data the data length code "
111                 "minimum is set to 1.\n");
112         fprintf(stderr, "CAN IDs and data content are given and expected in hexadecimal values.\n\n");
113         fprintf(stderr, "Examples:\n");
114         fprintf(stderr, "%s vcan0 -g 4 -I 42A -L 1 -D i -v -v   ", prg);
115         fprintf(stderr, "(fixed CAN ID and length, inc. data)\n");
116         fprintf(stderr, "%s vcan0 -e -L i -v -v -v              ", prg);
117         fprintf(stderr, "(generate EFF frames, incr. length)\n");
118         fprintf(stderr, "%s vcan0 -D 11223344DEADBEEF -L 8      ", prg);
119         fprintf(stderr, "(fixed CAN data payload and length)\n");
120         fprintf(stderr, "%s vcan0 -g 0 -i -x                    ", prg);
121         fprintf(stderr, "(full load test ignoring -ENOBUFS)\n");
122         fprintf(stderr, "%s vcan0 -g 0 -p 10 -x                 ", prg);
123         fprintf(stderr, "(full load test with polling, 10ms timeout)\n");
124         fprintf(stderr, "%s vcan0                               ", prg);
125         fprintf(stderr, "(my favourite default :)\n\n");
126 }
127
128 void sigterm(int signo)
129 {
130         running = 0;
131 }
132
133 int main(int argc, char **argv)
134 {
135         unsigned long gap = DEFAULT_GAP; 
136         unsigned long polltimeout = 0;
137         unsigned char ignore_enobufs = 0;
138         unsigned char extended = 0;
139         unsigned char id_mode = MODE_RANDOM;
140         unsigned char data_mode = MODE_RANDOM;
141         unsigned char dlc_mode = MODE_RANDOM;
142         unsigned char loopback_disable = 0;
143         unsigned char verbose = 0;
144         int count = 0;
145         uint64_t incdata = 0;
146
147         int opt;
148         int s; /* socket */
149         struct pollfd fds;
150
151         struct sockaddr_can addr;
152         static struct can_frame frame;
153         int nbytes;
154         int i;
155         struct ifreq ifr;
156
157         struct timespec ts;
158
159         signal(SIGTERM, sigterm);
160         signal(SIGHUP, sigterm);
161         signal(SIGINT, sigterm);
162
163         while ((opt = getopt(argc, argv, "ig:eI:L:D:xp:n:vh?")) != -1) {
164                 switch (opt) {
165
166                 case 'i':
167                         ignore_enobufs = 1;
168                         break;
169
170                 case 'g':
171                         gap = strtoul(optarg, NULL, 10);
172                         break;
173
174                 case 'e':
175                         extended = 1;
176                         break;
177
178                 case 'I':
179                         if (optarg[0] == 'r') {
180                                 id_mode = MODE_RANDOM;
181                         } else if (optarg[0] == 'i') {
182                                 id_mode = MODE_INCREMENT;
183                         } else {
184                                 id_mode = MODE_FIX;
185                                 frame.can_id = strtoul(optarg, NULL, 16);
186                         }
187                         break;
188
189                 case 'L':
190                         if (optarg[0] == 'r') {
191                                 dlc_mode = MODE_RANDOM;
192                         } else if (optarg[0] == 'i') {
193                                 dlc_mode = MODE_INCREMENT;
194                         } else {
195                                 dlc_mode = MODE_FIX;
196                                 frame.can_dlc = atoi(optarg)%9;
197                         }
198                         break;
199
200                 case 'D':
201                         if (optarg[0] == 'r') {
202                                 data_mode = MODE_RANDOM;
203                         } else if (optarg[0] == 'i') {
204                                 data_mode = MODE_INCREMENT;
205                         } else {
206                                 data_mode = MODE_FIX;
207                                 if (hexstring2candata(optarg, &frame)) {
208                                         printf ("wrong fix data definition\n");
209                                         return 1;
210                                 }
211                         }
212                         break;
213
214                 case 'v':
215                         verbose++;
216                         break;
217
218                 case 'x':
219                         loopback_disable = 1;
220                         break;
221
222                 case 'p':
223                         polltimeout = strtoul(optarg, NULL, 10);
224                         break;
225
226                 case 'n':
227                         count = atoi(optarg);
228                         if (count < 1) {
229                                 print_usage(basename(argv[0]));
230                                 return 1;
231                         }
232                         break;
233
234                 case '?':
235                 case 'h':
236                 default:
237                         print_usage(basename(argv[0]));
238                         return 1;
239                         break;
240                 }
241         }
242
243         if (optind == argc) {
244                 print_usage(basename(argv[0]));
245                 return 1;
246         }
247
248         ts.tv_sec = gap / 1000;
249         ts.tv_nsec = (gap % 1000) * 1000000;
250
251         if (id_mode == MODE_FIX) {
252
253                 /* recognize obviously missing commandline option */
254                 if ((frame.can_id > 0x7FF) && !extended) {
255                         printf("The given CAN-ID is greater than 0x7FF and "
256                                "the '-e' option is not set.\n");
257                         return 1;
258                 }
259
260                 if (extended)
261                         frame.can_id &= CAN_EFF_MASK;
262                 else
263                         frame.can_id &= CAN_SFF_MASK;
264         }
265
266         if (extended)
267                 frame.can_id |=  CAN_EFF_FLAG;
268
269         if ((data_mode == MODE_INCREMENT) && !frame.can_dlc)
270                 frame.can_dlc = 1; /* min dlc value for incr. data */
271
272         if (strlen(argv[optind]) >= IFNAMSIZ) {
273                 printf("Name of CAN device '%s' is too long!\n\n", argv[optind]);
274                 return 1;
275         }
276
277         if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
278                 perror("socket");
279                 return 1;
280         }
281
282         addr.can_family = AF_CAN;
283
284         strcpy(ifr.ifr_name, argv[optind]);
285         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
286                 perror("SIOCGIFINDEX");
287                 return 1;
288         }
289         addr.can_ifindex = ifr.ifr_ifindex;
290
291         /* disable default receive filter on this RAW socket */
292         /* This is obsolete as we do not read from the socket at all, but for */
293         /* this reason we can remove the receive list in the Kernel to save a */
294         /* little (really a very little!) CPU usage.                          */
295         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
296
297         if (loopback_disable) {
298                 int loopback = 0;
299
300                 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
301                            &loopback, sizeof(loopback));
302         }
303
304         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
305                 perror("bind");
306                 return 1;
307         }
308
309         if (polltimeout) {
310                 fds.fd = s;
311                 fds.events = POLLOUT;
312         }
313
314         while (running) {
315
316                 if (count && (--count == 0))
317                         running = 0;
318
319                 if (id_mode == MODE_RANDOM) {
320
321                         frame.can_id = random();
322
323                         if (extended) {
324                                 frame.can_id &= CAN_EFF_MASK;
325                                 frame.can_id |= CAN_EFF_FLAG;
326                         } else
327                                 frame.can_id &= CAN_SFF_MASK;
328                 }
329
330                 if (dlc_mode == MODE_RANDOM) {
331
332                         frame.can_dlc = random() & 0xF;
333
334                         if (frame.can_dlc & 8)
335                                 frame.can_dlc = 8; /* for about 50% of the frames */
336
337                         if ((data_mode == MODE_INCREMENT) && !frame.can_dlc)
338                                 frame.can_dlc = 1; /* min dlc value for incr. data */
339                 }
340
341                 if (data_mode == MODE_RANDOM) {
342
343                         /* that's what the 64 bit alignment of data[] is for ... :) */
344                         *(unsigned long*)(&frame.data[0]) = random();
345                         *(unsigned long*)(&frame.data[4]) = random();
346                 }
347
348                 if (verbose) {
349
350                         printf("  %s  ", argv[optind]);
351
352                         if (verbose > 1)
353                                 fprint_long_canframe(stdout, &frame, "\n", (verbose > 2)?1:0);
354                         else
355                                 fprint_canframe(stdout, &frame, "\n", 1);
356                 }
357
358 resend:
359                 nbytes = write(s, &frame, sizeof(struct can_frame));
360                 if (nbytes < 0) {
361                         if (errno != ENOBUFS) {
362                                 perror("write");
363                                 return 1;
364                         }
365                         if (!ignore_enobufs && !polltimeout) {
366                                 perror("write");
367                                 return 1;
368                         }
369                         if (polltimeout) {
370                                 /* wait for the write socket (with timeout) */
371                                 if (poll(&fds, 1, polltimeout) < 0) {
372                                         perror("poll");
373                                         return 1;
374                                 } else
375                                         goto resend;
376                         } else
377                                 enobufs_count++;
378
379                 } else if (nbytes < sizeof(struct can_frame)) {
380                         fprintf(stderr, "write: incomplete CAN frame\n");
381                         return 1;
382                 }
383
384                 if (gap) /* gap == 0 => performance test :-] */
385                         if (nanosleep(&ts, NULL))
386                                 return 1;
387                     
388                 if (id_mode == MODE_INCREMENT) {
389
390                         frame.can_id++;
391
392                         if (extended) {
393                                 frame.can_id &= CAN_EFF_MASK;
394                                 frame.can_id |= CAN_EFF_FLAG;
395                         } else
396                                 frame.can_id &= CAN_SFF_MASK;
397                 }
398
399                 if (dlc_mode == MODE_INCREMENT) {
400
401                         frame.can_dlc++;
402                         frame.can_dlc %= 9;
403
404                         if ((data_mode == MODE_INCREMENT) && !frame.can_dlc)
405                                 frame.can_dlc = 1; /* min dlc value for incr. data */
406                 }
407
408                 if (data_mode == MODE_INCREMENT) {
409
410                         incdata++;
411
412                         for (i=0; i<8 ;i++)
413                                 frame.data[i] = (incdata >> i*8) & 0xFFULL;
414                 }
415         }
416
417         if (enobufs_count)
418                 printf("\nCounted %llu ENOBUFS return values on write().\n\n",
419                        enobufs_count);
420
421         close(s);
422
423         return 0;
424 }