6 * canplayer.c - replay a compact CAN frame logfile to CAN devices
8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
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
44 * Send feedback to <socketcan-users@lists.berlios.de>
56 #include <sys/ioctl.h>
58 #include <linux/can.h>
59 #include <linux/can/raw.h>
63 #define DEFAULT_GAP 1 /* ms */
64 #define DEFAULT_LOOPS 1 /* only one replay */
65 #define CHANNELS 20 /* anyone using more than 20 CAN interfaces at a time? */
66 #define BUFSZ 100 /* for one line in the logfile */
73 static struct assignment asgn[CHANNELS];
75 extern int optind, opterr, optopt;
77 void print_usage(char *prg)
79 fprintf(stderr, "\nUsage: %s <options> [interface assignment]*\n\n", prg);
80 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
81 fprintf(stderr, " -l <num> "
82 "(process input file <num> times)\n"
84 "(Use 'i' for infinite loop - default: %d)\n", DEFAULT_LOOPS);
85 fprintf(stderr, " -t (ignore timestamps: "
86 "send frames immediately)\n");
87 fprintf(stderr, " -g <ms> (gap in milli "
88 "seconds - default: %d ms)\n", DEFAULT_GAP);
89 fprintf(stderr, " -s <s> (skip gaps in "
90 "timestamps > 's' seconds)\n");
91 fprintf(stderr, " -x (disable local "
92 "loopback of sent CAN frames)\n");
93 fprintf(stderr, " -v (verbose: print "
94 "sent CAN frames)\n\n");
95 fprintf(stderr, "Interface assignment: 0..n assignments like "
96 "<write-if>=<log-if>\n");
97 fprintf(stderr, "e.g. vcan2=can0 ( send frames received from can0 on "
99 fprintf(stderr, "No assignments => send frames to the interface(s) they "
100 "had been received from.\n\n");
103 /* copied from /usr/src/linux/include/linux/time.h ...
104 * lhs < rhs: return <0
105 * lhs == rhs: return 0
106 * lhs > rhs: return >0
108 static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs)
110 if (lhs->tv_sec < rhs->tv_sec)
112 if (lhs->tv_sec > rhs->tv_sec)
114 return lhs->tv_usec - rhs->tv_usec;
117 static inline void create_diff_tv(struct timeval *today, struct timeval *diff,
118 struct timeval *log) {
120 /* create diff_tv so that log_tv + diff_tv = today_tv */
121 diff->tv_sec = today->tv_sec - log->tv_sec;
122 diff->tv_usec = today->tv_usec - log->tv_usec;
125 static inline int frames_to_send(struct timeval *today, struct timeval *diff,
128 /* return value <0 when log + diff < today */
132 cmp.tv_sec = log->tv_sec + diff->tv_sec;
133 cmp.tv_usec = log->tv_usec + diff->tv_usec;
135 if (cmp.tv_usec > 1000000) {
136 cmp.tv_usec -= 1000000;
140 if (cmp.tv_usec < 0) {
141 cmp.tv_usec += 1000000;
145 return timeval_compare(&cmp, today);
148 int get_txidx(char *logif_name) {
152 for (i=0; i<CHANNELS; i++) {
153 if (asgn[i].rxif[0] == 0) /* end of table content */
155 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
159 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
160 return 0; /* not found */
162 return asgn[i].txifidx; /* return interface index */
165 char *get_txname(char *logif_name) {
169 for (i=0; i<CHANNELS; i++) {
170 if (asgn[i].rxif[0] == 0) /* end of table content */
172 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
176 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
177 return 0; /* not found */
179 return asgn[i].txif; /* return interface name */
182 int add_assignment(char *mode, int socket, char *txname, char *rxname,
188 /* find free entry */
189 for (i=0; i<CHANNELS; i++) {
190 if (asgn[i].txif[0] == 0)
195 fprintf(stderr, "Assignment table exceeded!\n");
199 if (strlen(txname) >= IFNAMSIZ) {
200 fprintf(stderr, "write-if interface name '%s' too long!", txname);
203 strcpy(asgn[i].txif, txname);
205 if (strlen(rxname) >= IFNAMSIZ) {
206 fprintf(stderr, "log-if interface name '%s' too long!", rxname);
209 strcpy(asgn[i].rxif, rxname);
211 strcpy(ifr.ifr_name, txname);
212 if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
213 perror("SIOCGIFINDEX");
214 fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
217 asgn[i].txifidx = ifr.ifr_ifindex;
219 if (verbose > 1) /* use -v -v to see this */
220 printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
221 mode, asgn[i].rxif, asgn[i].txif, asgn[i].txifidx);
226 int main(int argc, char **argv)
228 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
229 struct sockaddr_can addr;
230 static struct can_frame frame;
231 static struct timeval today_tv, log_tv, last_log_tv, diff_tv;
232 struct timespec sleep_ts;
233 int s; /* CAN_RAW socket */
234 FILE *infile = stdin;
235 unsigned long gap = DEFAULT_GAP;
236 int use_timestamps = 1;
237 static int verbose, opt, delay_loops, skipgap;
238 static int loopback_disable = 0;
239 static int infinite_loops = 0;
240 static int loops = DEFAULT_LOOPS;
241 int assignments; /* assignments defined on the commandline */
242 int txidx; /* sendto() interface index */
243 int eof, nbytes, i, j;
245 while ((opt = getopt(argc, argv, "I:l:tg:s:xv")) != -1) {
248 infile = fopen(optarg, "r");
256 if (optarg[0] == 'i')
259 if (!(loops = atoi(optarg))) {
260 fprintf(stderr, "Invalid argument for option -l !\n");
270 gap = strtoul(optarg, NULL, 10);
274 skipgap = strtoul(optarg, NULL, 10);
276 fprintf(stderr, "Invalid argument for option -s !\n");
282 loopback_disable = 1;
290 print_usage(basename(argv[0]));
296 assignments = argc - optind; /* find real number of user assignments */
298 if (infile == stdin) { /* no jokes with stdin */
303 if (verbose > 1) /* use -v -v to see this */
305 printf("infinite_loops\n");
307 printf("%d loops\n", loops);
309 sleep_ts.tv_sec = gap / 1000;
310 sleep_ts.tv_nsec = (gap % 1000) * 1000000;
313 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
318 addr.can_family = AF_CAN;
319 addr.can_ifindex = 0;
321 /* disable unneeded default receive filter on this RAW socket */
322 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
324 if (loopback_disable) {
327 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
328 &loopback, sizeof(loopback));
331 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
337 /* add & check user assginments from commandline */
338 for (i=0; i<assignments; i++) {
339 if (strlen(argv[optind+i]) >= BUFSZ) {
340 fprintf(stderr, "Assignment too long!\n");
341 print_usage(basename(argv[0]));
344 strcpy(buf, argv[optind+i]);
345 for (j=0; j<BUFSZ; j++) { /* find '=' in assignment */
350 fprintf(stderr, "'=' missing in assignment!\n");
351 print_usage(basename(argv[0]));
354 buf[j] = 0; /* cut string in two pieces */
355 if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
360 while (infinite_loops || loops--) {
363 rewind(infile); /* for each loop */
365 if (verbose > 1) /* use -v -v to see this */
366 printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
368 if (!fgets(buf, BUFSZ-1, infile)) /* read first frame from logfile */
369 goto out; /* nothing to read */
373 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
374 device, ascframe) != 4)
377 if (use_timestamps) { /* throttle sending due to logfile timestamps */
379 gettimeofday(&today_tv, NULL);
380 create_diff_tv(&today_tv, &diff_tv, &log_tv);
381 last_log_tv = log_tv;
386 while ((!use_timestamps) ||
387 (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
389 /* log_tv/device/ascframe are valid here */
391 if (strlen(device) >= IFNAMSIZ) {
392 fprintf(stderr, "log interface name '%s' too long!", device);
396 txidx = get_txidx(device); /* get ifindex for sending the frame */
398 if ((!txidx) && (!assignments)) {
399 /* ifindex not found and no user assignments */
400 /* => assign this device automatically */
401 if (add_assignment("auto", s, device, device, verbose))
403 txidx = get_txidx(device);
406 if (txidx) { /* only send to valid CAN devices */
408 if (parse_canframe(ascframe, &frame)) {
409 fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
413 addr.can_family = AF_CAN;
414 addr.can_ifindex = txidx; /* send via this interface */
416 nbytes = sendto(s, &frame, sizeof(struct can_frame), 0,
417 (struct sockaddr*)&addr, sizeof(addr));
419 if (nbytes != sizeof(struct can_frame)) {
425 printf("%s (%s) ", get_txname(device), device);
426 fprint_long_canframe(stdout, &frame, "\n", 1);
430 /* read next frame from logfile */
431 if (!fgets(buf, BUFSZ-1, infile)) {
432 eof = 1; /* this file is completely processed */
436 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
437 device, ascframe) != 4)
440 if (use_timestamps) {
441 gettimeofday(&today_tv, NULL);
443 /* test for logfile timestamps jumping backwards OR */
444 /* if the user likes to skip long gaps in the timestamps */
445 if ((last_log_tv.tv_sec > log_tv.tv_sec) ||
446 (skipgap && abs(last_log_tv.tv_sec - log_tv.tv_sec) > skipgap))
447 create_diff_tv(&today_tv, &diff_tv, &log_tv);
449 last_log_tv = log_tv;
452 } /* while frames_to_send ... */
454 if (nanosleep(&sleep_ts, NULL))
457 delay_loops++; /* private statistics */
458 gettimeofday(&today_tv, NULL);
462 } /* while (infinite_loops || loops--) */
469 if (verbose > 1) /* use -v -v to see this */
470 printf("%d delay_loops\n", delay_loops);