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, the following disclaimer and
16 * the referenced file 'COPYING'.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Volkswagen nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * Alternatively, provided that this notice is retained in full, this
25 * software may be distributed under the terms of the GNU General
26 * Public License ("GPL") version 2 as distributed in the 'COPYING'
27 * file from the main directory of the linux kernel source.
29 * The provided data structures and external interfaces from this code
30 * are not restricted to be used by modules with a GPL compatible license.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 * Send feedback to <socketcan-users@lists.berlios.de>
57 #include <sys/ioctl.h>
59 #include <linux/can.h>
60 #include <linux/can/raw.h>
64 #define DEFAULT_GAP 1 /* ms */
65 #define DEFAULT_LOOPS 1 /* only one replay */
66 #define CHANNELS 20 /* anyone using more than 20 CAN interfaces at a time? */
67 #define BUFSZ 100 /* for one line in the logfile */
74 static struct assignment asgn[CHANNELS];
76 extern int optind, opterr, optopt;
78 void print_usage(char *prg)
80 fprintf(stderr, "\nUsage: %s <options> [interface assignment]*\n\n", prg);
81 fprintf(stderr, "Options: -I <infile> (default stdin)\n");
82 fprintf(stderr, " -l <num> "
83 "(process input file <num> times)\n"
85 "(Use 'i' for infinite loop - default: %d)\n", DEFAULT_LOOPS);
86 fprintf(stderr, " -t (ignore timestamps: "
87 "send frames immediately)\n");
88 fprintf(stderr, " -g <ms> (gap in milli "
89 "seconds - default: %d ms)\n", DEFAULT_GAP);
90 fprintf(stderr, " -v (verbose: print "
91 "sent CAN frames)\n\n");
92 fprintf(stderr, "Interface assignment: 0..n assignments like "
93 "<write-if>=<log-if>\n");
94 fprintf(stderr, "e.g. vcan2=can0 ( send frames received from can0 on "
96 fprintf(stderr, "No assignments => send frames to the interface(s) they "
97 "had been received from.\n\n");
100 /* copied from /usr/src/linux/include/linux/time.h ...
101 * lhs < rhs: return <0
102 * lhs == rhs: return 0
103 * lhs > rhs: return >0
105 static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs)
107 if (lhs->tv_sec < rhs->tv_sec)
109 if (lhs->tv_sec > rhs->tv_sec)
111 return lhs->tv_usec - rhs->tv_usec;
114 static inline int frames_to_send(struct timeval *today, struct timeval *diff,
117 /* return value <0 when log + diff < today */
121 cmp.tv_sec = log->tv_sec + diff->tv_sec;
122 cmp.tv_usec = log->tv_usec + diff->tv_usec;
124 if (cmp.tv_usec > 1000000) {
125 cmp.tv_usec -= 1000000;
129 return timeval_compare(&cmp, today);
132 int get_txidx(char *logif_name) {
136 for (i=0; i<CHANNELS; i++) {
137 if (asgn[i].rxif[0] == 0) /* end of table content */
139 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
143 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
144 return 0; /* not found */
146 return asgn[i].txifidx; /* return interface index */
149 char *get_txname(char *logif_name) {
153 for (i=0; i<CHANNELS; i++) {
154 if (asgn[i].rxif[0] == 0) /* end of table content */
156 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
160 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
161 return 0; /* not found */
163 return asgn[i].txif; /* return interface name */
166 int add_assignment(char *mode, int socket, char *txname, char *rxname,
172 /* find free entry */
173 for (i=0; i<CHANNELS; i++) {
174 if (asgn[i].txif[0] == 0)
179 fprintf(stderr, "Assignment table exceeded!\n");
183 if (strlen(txname) >= IFNAMSIZ) {
184 fprintf(stderr, "write-if interface name '%s' too long!", txname);
187 strcpy(asgn[i].txif, txname);
189 if (strlen(rxname) >= IFNAMSIZ) {
190 fprintf(stderr, "log-if interface name '%s' too long!", rxname);
193 strcpy(asgn[i].rxif, rxname);
195 strcpy(ifr.ifr_name, txname);
196 if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
197 perror("SIOCGIFINDEX");
198 fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
201 asgn[i].txifidx = ifr.ifr_ifindex;
203 if (verbose > 1) /* use -v -v to see this */
204 printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
205 mode, asgn[i].rxif, asgn[i].txif, asgn[i].txifidx);
210 int main(int argc, char **argv)
212 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
213 struct sockaddr_can addr;
214 static struct can_frame frame;
215 static struct timeval today_tv, log_tv, diff_tv;
216 struct timespec sleep_ts;
217 int s; /* CAN_RAW socket */
218 FILE *infile = stdin;
219 unsigned long gap = DEFAULT_GAP;
220 int use_timestamps = 1;
221 static int verbose, opt, delay_loops;
222 static int infinite_loops = 0;
223 static int loops = DEFAULT_LOOPS;
224 int assignments; /* assignments defined on the commandline */
225 int txidx; /* sendto() interface index */
226 int eof, nbytes, i, j;
228 while ((opt = getopt(argc, argv, "I:l:tg:v")) != -1) {
231 infile = fopen(optarg, "r");
239 if (optarg[0] == 'i')
242 if (!(loops = atoi(optarg))) {
243 fprintf(stderr, "Invalid argument for option -l !\n");
253 gap = strtoul(optarg, NULL, 10);
261 print_usage(basename(argv[0]));
267 assignments = argc - optind; /* find real number of user assignments */
269 if (infile == stdin) { /* no jokes with stdin */
274 if (verbose > 1) /* use -v -v to see this */
276 printf("infinite_loops\n");
278 printf("%d loops\n", loops);
280 sleep_ts.tv_sec = gap / 1000;
281 sleep_ts.tv_nsec = (gap % 1000) * 1000000;
284 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
289 addr.can_family = AF_CAN;
290 addr.can_ifindex = 0;
292 /* disable unneeded default receive filter on this RAW socket */
293 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
295 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
301 /* add & check user assginments from commandline */
302 for (i=0; i<assignments; i++) {
303 if (strlen(argv[optind+i]) >= BUFSZ) {
304 fprintf(stderr, "Assignment too long!\n");
305 print_usage(basename(argv[0]));
308 strcpy(buf, argv[optind+i]);
309 for (j=0; j<BUFSZ; j++) { /* find '=' in assignment */
314 fprintf(stderr, "'=' missing in assignment!\n");
315 print_usage(basename(argv[0]));
318 buf[j] = 0; /* cut string in two pieces */
319 if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
324 while (infinite_loops || loops--) {
327 rewind(infile); /* for each loop */
329 if (verbose > 1) /* use -v -v to see this */
330 printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
332 if (!fgets(buf, BUFSZ-1, infile)) /* read first frame from logfile */
333 goto out; /* nothing to read */
337 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
338 device, ascframe) != 4)
341 if (use_timestamps) { /* throttle sending due to logfile timestamps */
343 gettimeofday(&today_tv, NULL);
345 /* omit crazy comparations with negative diff_tv */
346 if (timeval_compare(&today_tv, &log_tv) < 1) {
347 fprintf(stderr, "logfile timestamps newer than time of day!\n");
351 /* create diff_tv so that log_tv + diff_tv = today_tv */
352 diff_tv.tv_sec = today_tv.tv_sec - log_tv.tv_sec;
353 diff_tv.tv_usec = today_tv.tv_usec - log_tv.tv_usec;
354 if (diff_tv.tv_usec < 0)
355 diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
360 while ((!use_timestamps) ||
361 (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
363 /* log_tv/device/ascframe are valid here */
365 if (strlen(device) >= IFNAMSIZ) {
366 fprintf(stderr, "log interface name '%s' too long!", device);
370 txidx = get_txidx(device); /* get ifindex for sending the frame */
372 if ((!txidx) && (!assignments)) {
373 /* ifindex not found and no user assignments */
374 /* => assign this device automatically */
375 if (add_assignment("auto", s, device, device, verbose))
377 txidx = get_txidx(device);
380 if (txidx) { /* only send to valid CAN devices */
382 if (parse_canframe(ascframe, &frame)) {
383 fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
387 addr.can_family = AF_CAN;
388 addr.can_ifindex = txidx; /* send via this interface */
390 nbytes = sendto(s, &frame, sizeof(struct can_frame), 0,
391 (struct sockaddr*)&addr, sizeof(addr));
393 if (nbytes != sizeof(struct can_frame)) {
399 printf("%s (%s) ", get_txname(device), device);
400 fprint_long_canframe(stdout, &frame, "\n", 1);
404 /* read next frame from logfile */
405 if (!fgets(buf, BUFSZ-1, infile)) {
406 eof = 1; /* this file is completely processed */
410 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
411 device, ascframe) != 4)
414 if (use_timestamps) /* save a syscall if possible */
415 gettimeofday(&today_tv, NULL);
417 } /* while frames_to_send ... */
419 if (nanosleep(&sleep_ts, NULL))
422 delay_loops++; /* private statistics */
423 gettimeofday(&today_tv, NULL);
427 } /* while (infinite_loops || loops--) */
434 if (verbose > 1) /* use -v -v to see this */
435 printf("%d delay_loops\n", delay_loops);