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 400 /* 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");
101 fprintf(stderr, "Lines in the logfile not beginning with '(' (start of "
102 "timestamp) are ignored.\n\n");
105 /* copied from /usr/src/linux/include/linux/time.h ...
106 * lhs < rhs: return <0
107 * lhs == rhs: return 0
108 * lhs > rhs: return >0
110 static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs)
112 if (lhs->tv_sec < rhs->tv_sec)
114 if (lhs->tv_sec > rhs->tv_sec)
116 return lhs->tv_usec - rhs->tv_usec;
119 static inline void create_diff_tv(struct timeval *today, struct timeval *diff,
120 struct timeval *log) {
122 /* create diff_tv so that log_tv + diff_tv = today_tv */
123 diff->tv_sec = today->tv_sec - log->tv_sec;
124 diff->tv_usec = today->tv_usec - log->tv_usec;
127 static inline int frames_to_send(struct timeval *today, struct timeval *diff,
130 /* return value <0 when log + diff < today */
134 cmp.tv_sec = log->tv_sec + diff->tv_sec;
135 cmp.tv_usec = log->tv_usec + diff->tv_usec;
137 if (cmp.tv_usec > 1000000) {
138 cmp.tv_usec -= 1000000;
142 if (cmp.tv_usec < 0) {
143 cmp.tv_usec += 1000000;
147 return timeval_compare(&cmp, today);
150 int get_txidx(char *logif_name) {
154 for (i=0; i<CHANNELS; i++) {
155 if (asgn[i].rxif[0] == 0) /* end of table content */
157 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
161 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
162 return 0; /* not found */
164 return asgn[i].txifidx; /* return interface index */
167 char *get_txname(char *logif_name) {
171 for (i=0; i<CHANNELS; i++) {
172 if (asgn[i].rxif[0] == 0) /* end of table content */
174 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
178 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
179 return 0; /* not found */
181 return asgn[i].txif; /* return interface name */
184 int add_assignment(char *mode, int socket, char *txname, char *rxname,
190 /* find free entry */
191 for (i=0; i<CHANNELS; i++) {
192 if (asgn[i].txif[0] == 0)
197 fprintf(stderr, "Assignment table exceeded!\n");
201 if (strlen(txname) >= IFNAMSIZ) {
202 fprintf(stderr, "write-if interface name '%s' too long!", txname);
205 strcpy(asgn[i].txif, txname);
207 if (strlen(rxname) >= IFNAMSIZ) {
208 fprintf(stderr, "log-if interface name '%s' too long!", rxname);
211 strcpy(asgn[i].rxif, rxname);
213 strcpy(ifr.ifr_name, txname);
214 if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
215 perror("SIOCGIFINDEX");
216 fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
219 asgn[i].txifidx = ifr.ifr_ifindex;
221 if (verbose > 1) /* use -v -v to see this */
222 printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
223 mode, asgn[i].rxif, asgn[i].txif, asgn[i].txifidx);
228 int main(int argc, char **argv)
230 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
231 struct sockaddr_can addr;
232 static struct can_frame frame;
233 static struct timeval today_tv, log_tv, last_log_tv, diff_tv;
234 struct timespec sleep_ts;
235 int s; /* CAN_RAW socket */
236 FILE *infile = stdin;
237 unsigned long gap = DEFAULT_GAP;
238 int use_timestamps = 1;
239 static int verbose, opt, delay_loops, skipgap;
240 static int loopback_disable = 0;
241 static int infinite_loops = 0;
242 static int loops = DEFAULT_LOOPS;
243 int assignments; /* assignments defined on the commandline */
244 int txidx; /* sendto() interface index */
245 int eof, nbytes, i, j;
248 while ((opt = getopt(argc, argv, "I:l:tg:s:xv")) != -1) {
251 infile = fopen(optarg, "r");
259 if (optarg[0] == 'i')
262 if (!(loops = atoi(optarg))) {
263 fprintf(stderr, "Invalid argument for option -l !\n");
273 gap = strtoul(optarg, NULL, 10);
277 skipgap = strtoul(optarg, NULL, 10);
279 fprintf(stderr, "Invalid argument for option -s !\n");
285 loopback_disable = 1;
293 print_usage(basename(argv[0]));
299 assignments = argc - optind; /* find real number of user assignments */
301 if (infile == stdin) { /* no jokes with stdin */
306 if (verbose > 1) /* use -v -v to see this */
308 printf("infinite_loops\n");
310 printf("%d loops\n", loops);
312 sleep_ts.tv_sec = gap / 1000;
313 sleep_ts.tv_nsec = (gap % 1000) * 1000000;
316 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
321 addr.can_family = AF_CAN;
322 addr.can_ifindex = 0;
324 /* disable unneeded default receive filter on this RAW socket */
325 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
327 if (loopback_disable) {
330 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
331 &loopback, sizeof(loopback));
334 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
340 /* add & check user assginments from commandline */
341 for (i=0; i<assignments; i++) {
342 if (strlen(argv[optind+i]) >= BUFSZ) {
343 fprintf(stderr, "Assignment too long!\n");
344 print_usage(basename(argv[0]));
347 strcpy(buf, argv[optind+i]);
348 for (j=0; j<BUFSZ; j++) { /* find '=' in assignment */
352 if ((j == BUFSZ) || (buf[j] != '=')) {
353 fprintf(stderr, "'=' missing in assignment!\n");
354 print_usage(basename(argv[0]));
357 buf[j] = 0; /* cut string in two pieces */
358 if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
363 while (infinite_loops || loops--) {
366 rewind(infile); /* for each loop */
368 if (verbose > 1) /* use -v -v to see this */
369 printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
371 /* read first non-comment frame from logfile */
372 while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
373 if (strlen(buf) >= BUFSZ-2) {
374 fprintf(stderr, "comment line too long for input buffer\n");
380 goto out; /* nothing to read */
384 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
385 device, ascframe) != 4) {
386 fprintf(stderr, "incorrect line format in logfile\n");
390 if (use_timestamps) { /* throttle sending due to logfile timestamps */
392 gettimeofday(&today_tv, NULL);
393 create_diff_tv(&today_tv, &diff_tv, &log_tv);
394 last_log_tv = log_tv;
399 while ((!use_timestamps) ||
400 (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
402 /* log_tv/device/ascframe are valid here */
404 if (strlen(device) >= IFNAMSIZ) {
405 fprintf(stderr, "log interface name '%s' too long!", device);
409 txidx = get_txidx(device); /* get ifindex for sending the frame */
411 if ((!txidx) && (!assignments)) {
412 /* ifindex not found and no user assignments */
413 /* => assign this device automatically */
414 if (add_assignment("auto", s, device, device, verbose))
416 txidx = get_txidx(device);
419 if (txidx) { /* only send to valid CAN devices */
421 if (parse_canframe(ascframe, &frame)) {
422 fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
426 addr.can_family = AF_CAN;
427 addr.can_ifindex = txidx; /* send via this interface */
429 nbytes = sendto(s, &frame, sizeof(struct can_frame), 0,
430 (struct sockaddr*)&addr, sizeof(addr));
432 if (nbytes != sizeof(struct can_frame)) {
438 printf("%s (%s) ", get_txname(device), device);
439 fprint_long_canframe(stdout, &frame, "\n", 1);
443 /* read next non-comment frame from logfile */
444 while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
445 if (strlen(buf) >= BUFSZ-2) {
446 fprintf(stderr, "comment line too long for input buffer\n");
452 eof = 1; /* this file is completely processed */
456 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
457 device, ascframe) != 4) {
458 fprintf(stderr, "incorrect line format in logfile\n");
462 if (use_timestamps) {
463 gettimeofday(&today_tv, NULL);
465 /* test for logfile timestamps jumping backwards OR */
466 /* if the user likes to skip long gaps in the timestamps */
467 if ((last_log_tv.tv_sec > log_tv.tv_sec) ||
468 (skipgap && abs(last_log_tv.tv_sec - log_tv.tv_sec) > skipgap))
469 create_diff_tv(&today_tv, &diff_tv, &log_tv);
471 last_log_tv = log_tv;
474 } /* while frames_to_send ... */
476 if (nanosleep(&sleep_ts, NULL))
479 delay_loops++; /* private statistics */
480 gettimeofday(&today_tv, NULL);
484 } /* while (infinite_loops || loops--) */
491 if (verbose > 1) /* use -v -v to see this */
492 printf("%d delay_loops\n", delay_loops);