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 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, " -t (ignore timestamps: "
82 "send frames immediately)\n");
83 fprintf(stderr, " -g <ms> (gap in milli "
84 "seconds - default: %d ms)\n", DEFAULT_GAP);
85 fprintf(stderr, " -v (verbose: print "
86 "sent CAN frames)\n\n");
87 fprintf(stderr, "Interface assignment: 0..n assignments like "
88 "<write-if>=<log-if>\n");
89 fprintf(stderr, "e.g. vcan2=can0 ( send frames received from can0 on "
91 fprintf(stderr, "No assignments => send frames to the interface(s) they "
92 "had been received from.\n\n");
95 /* copied from /usr/src/linux/include/linux/time.h ...
96 * lhs < rhs: return <0
97 * lhs == rhs: return 0
98 * lhs > rhs: return >0
100 static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs)
102 if (lhs->tv_sec < rhs->tv_sec)
104 if (lhs->tv_sec > rhs->tv_sec)
106 return lhs->tv_usec - rhs->tv_usec;
109 static inline int frames_to_send(struct timeval *today, struct timeval *diff,
112 /* return value <0 when log + diff < today */
116 cmp.tv_sec = log->tv_sec + diff->tv_sec;
117 cmp.tv_usec = log->tv_usec + diff->tv_usec;
119 if (cmp.tv_usec > 1000000) {
120 cmp.tv_usec -= 1000000;
124 return timeval_compare(&cmp, today);
127 int get_txidx(char *logif_name) {
131 for (i=0; i<CHANNELS; i++) {
132 if (asgn[i].rxif[0] == 0) /* end of table content */
134 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
138 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
139 return 0; /* not found */
141 return asgn[i].txifidx; /* return interface index */
144 char *get_txname(char *logif_name) {
148 for (i=0; i<CHANNELS; i++) {
149 if (asgn[i].rxif[0] == 0) /* end of table content */
151 if (strcmp(asgn[i].rxif, logif_name) == 0) /* found device name */
155 if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
156 return 0; /* not found */
158 return asgn[i].txif; /* return interface name */
161 int add_assignment(char *mode, int socket, char *txname, char *rxname,
167 /* find free entry */
168 for (i=0; i<CHANNELS; i++) {
169 if (asgn[i].txif[0] == 0)
174 fprintf(stderr, "Assignment table exceeded!\n");
178 if (strlen(txname) >= IFNAMSIZ) {
179 fprintf(stderr, "write-if interface name '%s' too long!", txname);
182 strcpy(asgn[i].txif, txname);
184 if (strlen(rxname) >= IFNAMSIZ) {
185 fprintf(stderr, "log-if interface name '%s' too long!", rxname);
188 strcpy(asgn[i].rxif, rxname);
190 strcpy(ifr.ifr_name, txname);
191 if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
192 perror("SIOCGIFINDEX");
193 fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
196 asgn[i].txifidx = ifr.ifr_ifindex;
198 if (verbose > 1) /* use -v -v to see this */
199 printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
200 mode, asgn[i].rxif, asgn[i].txif, asgn[i].txifidx);
205 int main(int argc, char **argv)
207 static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
208 struct sockaddr_can addr;
209 static struct can_frame frame;
210 static struct timeval today_tv, log_tv, diff_tv;
211 struct timespec sleep_ts;
212 int s; /* CAN_RAW socket */
213 FILE *infile = stdin;
214 unsigned long gap = DEFAULT_GAP;
215 int use_timestamps = 1;
216 static int verbose, opt, loops;
217 int assignments; /* assignments defined on the commandline */
218 int txidx; /* sendto() interface index */
221 while ((opt = getopt(argc, argv, "I:tg:v")) != -1) {
224 infile = fopen(optarg, "r");
236 gap = strtoul(optarg, NULL, 10);
244 print_usage(basename(argv[0]));
250 assignments = argc - optind; /* find real number of user assignments */
252 sleep_ts.tv_sec = gap / 1000;
253 sleep_ts.tv_nsec = (gap % 1000) * 1000000;
256 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
261 addr.can_family = AF_CAN;
262 addr.can_ifindex = 0;
264 /* disable unneeded default receive filter on this RAW socket */
265 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
267 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
273 /* add & check user assginments from commandline */
274 for (i=0; i<assignments; i++) {
275 if (strlen(argv[optind+i]) >= BUFSZ) {
276 fprintf(stderr, "Assignment too long!\n");
277 print_usage(basename(argv[0]));
280 strcpy(buf, argv[optind+i]);
281 for (j=0; j<BUFSZ; j++) { /* find '=' in assignment */
286 fprintf(stderr, "'=' missing in assignment!\n");
287 print_usage(basename(argv[0]));
290 buf[j] = 0; /* cut string in two pieces */
291 if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
296 if (!fgets(buf, BUFSZ-1, infile)) /* read first frame from logfile */
297 goto out; /* nothing to read */
299 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
300 device, ascframe) != 4)
303 if (use_timestamps) { /* throttle sending due to logfile timestamps */
305 gettimeofday(&today_tv, NULL);
307 /* omit crazy comparations with negative diff_tv */
308 if (timeval_compare(&today_tv, &log_tv) < 1) {
309 fprintf(stderr, "logfile timestamps newer than time of day!\n");
313 /* create diff_tv so that log_tv + diff_tv = today_tv */
314 diff_tv.tv_sec = today_tv.tv_sec - log_tv.tv_sec;
315 diff_tv.tv_usec = today_tv.tv_usec - log_tv.tv_usec;
316 if (diff_tv.tv_usec < 0)
317 diff_tv.tv_sec--, diff_tv.tv_usec += 1000000;
322 while ((!use_timestamps) ||
323 (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
325 /* log_tv/device/ascframe are valid here */
327 if (strlen(device) >= IFNAMSIZ) {
328 fprintf(stderr, "log interface name '%s' too long!", device);
332 txidx = get_txidx(device); /* get ifindex for sending the frame */
334 if ((!txidx) && (!assignments)) {
335 /* ifindex not found and no user assignments */
336 /* => assign this device automatically */
337 if (add_assignment("auto", s, device, device, verbose))
339 txidx = get_txidx(device);
342 if (txidx) { /* only send to valid CAN devices */
344 if (parse_canframe(ascframe, &frame)) {
345 fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
349 addr.can_family = AF_CAN;
350 addr.can_ifindex = txidx; /* send via this interface */
352 nbytes = sendto(s, &frame, sizeof(struct can_frame), 0,
353 (struct sockaddr*)&addr, sizeof(addr));
355 if (nbytes != sizeof(struct can_frame)) {
361 printf("%s (%s) ", get_txname(device), device);
362 fprint_long_canframe(stdout, &frame, "\n", 1);
366 /* read next frame from logfile */
367 if (!fgets(buf, BUFSZ-1, infile))
368 goto out; /* nothing to read */
370 if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
371 device, ascframe) != 4)
374 if (use_timestamps) /* save a syscall if possible */
375 gettimeofday(&today_tv, NULL);
378 if (nanosleep(&sleep_ts, NULL))
381 loops++; /* private statistics */
382 gettimeofday(&today_tv, NULL);
390 if (verbose > 1) /* use -v -v to see this */
391 printf("%d loops\n", loops);