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