2 * canplayer.c - replay a compact CAN frame logfile to CAN devices
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
55 #include <linux/can.h>
56 #include <linux/can/raw.h>
60 #define DEFAULT_GAP 1 /* ms */
61 #define DEFAULT_LOOPS 1 /* only one replay */
62 #define CHANNELS 20 /* anyone using more than 20 CAN interfaces at a time? */
64 #define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */
65 #define STDOUTIDX 65536 /* interface index for printing on stdout - bigger than max uint16 */
72 static struct assignment asgn[CHANNELS];
73 const int canfd_on = 1;
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, "extra hook: stdout=can0 ( print logfile line marked with can0 on "
101 fprintf(stderr, "No assignments => send frames to the interface(s) they "
102 "had been received from.\n\n");
103 fprintf(stderr, "Lines in the logfile not beginning with '(' (start of "
104 "timestamp) are ignored.\n\n");
107 /* copied from /usr/src/linux/include/linux/time.h ...
108 * lhs < rhs: return <0
109 * lhs == rhs: return 0
110 * lhs > rhs: return >0
112 static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs)
114 if (lhs->tv_sec < rhs->tv_sec)
116 if (lhs->tv_sec > rhs->tv_sec)
118 return lhs->tv_usec - rhs->tv_usec;
121 static inline void create_diff_tv(struct timeval *today, struct timeval *diff,
122 struct timeval *log) {
124 /* create diff_tv so that log_tv + diff_tv = today_tv */
125 diff->tv_sec = today->tv_sec - log->tv_sec;
126 diff->tv_usec = today->tv_usec - log->tv_usec;
129 static inline int frames_to_send(struct timeval *today, struct timeval *diff,
132 /* return value <0 when log + diff < today */
136 cmp.tv_sec = log->tv_sec + diff->tv_sec;
137 cmp.tv_usec = log->tv_usec + diff->tv_usec;
139 if (cmp.tv_usec > 1000000) {
140 cmp.tv_usec -= 1000000;
144 if (cmp.tv_usec < 0) {
145 cmp.tv_usec += 1000000;
149 return timeval_compare(&cmp, today);
152 int get_txidx(char *logif_name) {
156 for (i=0; i<CHANNELS; i++) {
157 if (asgn[i].rxif[0] == 0) /* end of table content */
159 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
163 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
164 return 0; /* not found */
166 return asgn[i].txifidx; /* return interface index */
169 char *get_txname(char *logif_name) {
173 for (i=0; i<CHANNELS; i++) {
174 if (asgn[i].rxif[0] == 0) /* end of table content */
176 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
180 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
181 return 0; /* not found */
183 return asgn[i].txif; /* return interface name */
186 int add_assignment(char *mode, int socket, char *txname, char *rxname,
192 /* find free entry */
193 for (i=0; i<CHANNELS; i++) {
194 if (asgn[i].txif[0] == 0)
199 fprintf(stderr, "Assignment table exceeded!\n");
203 if (strlen(txname) >= IFNAMSIZ) {
204 fprintf(stderr, "write-if interface name '%s' too long!", txname);
207 strcpy(asgn[i].txif, txname);
209 if (strlen(rxname) >= IFNAMSIZ) {
210 fprintf(stderr, "log-if interface name '%s' too long!", rxname);
213 strcpy(asgn[i].rxif, rxname);
215 if (strcmp(txname, "stdout")) {
216 strcpy(ifr.ifr_name, txname);
217 if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
218 perror("SIOCGIFINDEX");
219 fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
222 asgn[i].txifidx = ifr.ifr_ifindex;
224 asgn[i].txifidx = STDOUTIDX;
226 if (verbose > 1) /* use -v -v to see this */
227 printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
228 mode, asgn[i].rxif, asgn[i].txif, asgn[i].txifidx);
233 int main(int argc, char **argv)
235 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
236 struct sockaddr_can addr;
237 static struct canfd_frame frame;
238 static struct timeval today_tv, log_tv, last_log_tv, diff_tv;
239 struct timespec sleep_ts;
240 int s; /* CAN_RAW socket */
241 FILE *infile = stdin;
242 unsigned long gap = DEFAULT_GAP;
243 int use_timestamps = 1;
244 static int verbose, opt, delay_loops, skipgap;
245 static int loopback_disable = 0;
246 static int infinite_loops = 0;
247 static int loops = DEFAULT_LOOPS;
248 int assignments; /* assignments defined on the commandline */
249 int txidx; /* sendto() interface index */
250 int eof, txmtu, i, j;
253 while ((opt = getopt(argc, argv, "I:l:tg:s:xv?")) != -1) {
256 infile = fopen(optarg, "r");
264 if (optarg[0] == 'i')
267 if (!(loops = atoi(optarg))) {
268 fprintf(stderr, "Invalid argument for option -l !\n");
278 gap = strtoul(optarg, NULL, 10);
282 skipgap = strtoul(optarg, NULL, 10);
284 fprintf(stderr, "Invalid argument for option -s !\n");
290 loopback_disable = 1;
299 print_usage(basename(argv[0]));
305 assignments = argc - optind; /* find real number of user assignments */
307 if (infile == stdin) { /* no jokes with stdin */
312 if (verbose > 1) { /* use -v -v to see this */
314 printf("infinite_loops\n");
316 printf("%d loops\n", loops);
319 sleep_ts.tv_sec = gap / 1000;
320 sleep_ts.tv_nsec = (gap % 1000) * 1000000;
323 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
328 addr.can_family = AF_CAN;
329 addr.can_ifindex = 0;
331 /* disable unneeded default receive filter on this RAW socket */
332 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
334 /* try to switch the socket into CAN FD mode */
335 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
337 if (loopback_disable) {
340 setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
341 &loopback, sizeof(loopback));
344 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
350 /* add & check user assginments from commandline */
351 for (i=0; i<assignments; i++) {
352 if (strlen(argv[optind+i]) >= BUFSZ) {
353 fprintf(stderr, "Assignment too long!\n");
354 print_usage(basename(argv[0]));
357 strcpy(buf, argv[optind+i]);
358 for (j=0; j<BUFSZ; j++) { /* find '=' in assignment */
362 if ((j == BUFSZ) || (buf[j] != '=')) {
363 fprintf(stderr, "'=' missing in assignment!\n");
364 print_usage(basename(argv[0]));
367 buf[j] = 0; /* cut string in two pieces */
368 if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
373 while (infinite_loops || loops--) {
376 rewind(infile); /* for each loop */
378 if (verbose > 1) /* use -v -v to see this */
379 printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
381 /* read first non-comment frame from logfile */
382 while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
383 if (strlen(buf) >= BUFSZ-2) {
384 fprintf(stderr, "comment line too long for input buffer\n");
390 goto out; /* nothing to read */
394 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
395 device, ascframe) != 4) {
396 fprintf(stderr, "incorrect line format in logfile\n");
400 if (use_timestamps) { /* throttle sending due to logfile timestamps */
402 gettimeofday(&today_tv, NULL);
403 create_diff_tv(&today_tv, &diff_tv, &log_tv);
404 last_log_tv = log_tv;
409 while ((!use_timestamps) ||
410 (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
412 /* log_tv/device/ascframe are valid here */
414 if (strlen(device) >= IFNAMSIZ) {
415 fprintf(stderr, "log interface name '%s' too long!", device);
419 txidx = get_txidx(device); /* get ifindex for sending the frame */
421 if ((!txidx) && (!assignments)) {
422 /* ifindex not found and no user assignments */
423 /* => assign this device automatically */
424 if (add_assignment("auto", s, device, device, verbose))
426 txidx = get_txidx(device);
429 if (txidx == STDOUTIDX) { /* hook to print logfile lines on stdout */
431 printf("%s", buf); /* print the line AS-IS without extra \n */
434 } else if (txidx > 0) { /* only send to valid CAN devices */
436 txmtu = parse_canframe(ascframe, &frame);
438 fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
442 addr.can_family = AF_CAN;
443 addr.can_ifindex = txidx; /* send via this interface */
445 if (sendto(s, &frame, txmtu, 0, (struct sockaddr*)&addr, sizeof(addr)) != txmtu) {
451 printf("%s (%s) ", get_txname(device), device);
453 if (txmtu == CAN_MTU)
454 fprint_long_canframe(stdout, &frame, "\n", CANLIB_VIEW_INDENT_SFF, CAN_MAX_DLEN);
456 fprint_long_canframe(stdout, &frame, "\n", CANLIB_VIEW_INDENT_SFF, CANFD_MAX_DLEN);
460 /* read next non-comment frame from logfile */
461 while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
462 if (strlen(buf) >= BUFSZ-2) {
463 fprintf(stderr, "comment line too long for input buffer\n");
469 eof = 1; /* this file is completely processed */
473 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
474 device, ascframe) != 4) {
475 fprintf(stderr, "incorrect line format in logfile\n");
479 if (use_timestamps) {
480 gettimeofday(&today_tv, NULL);
482 /* test for logfile timestamps jumping backwards OR */
483 /* if the user likes to skip long gaps in the timestamps */
484 if ((last_log_tv.tv_sec > log_tv.tv_sec) ||
485 (skipgap && abs(last_log_tv.tv_sec - log_tv.tv_sec) > skipgap))
486 create_diff_tv(&today_tv, &diff_tv, &log_tv);
488 last_log_tv = log_tv;
491 } /* while frames_to_send ... */
493 if (nanosleep(&sleep_ts, NULL))
496 delay_loops++; /* private statistics */
497 gettimeofday(&today_tv, NULL);
501 } /* while (infinite_loops || loops--) */
508 if (verbose > 1) /* use -v -v to see this */
509 printf("%d delay_loops\n", delay_loops);